ATLAS Offline Software
Loading...
Searching...
No Matches
IParticlesLookUpTable.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef PARTICLESINCONETOOLS_IPARTICLELOOKUPTABLE_H
6#define PARTICLESINCONETOOLS_IPARTICLELOOKUPTABLE_H
7
8#include <limits>
9#include <cmath>
11
12namespace xAOD {
13
14
16 template<class T>
18 public:
20 IParticlesLookUpTable( unsigned int nbins = 50 , float minPt=1e-3) :m_container(0),
21 m_nphiBins(nbins),
23 m_minPt(minPt),
25 {}
26
28 void init( const DataVector<T>& particles );
29
31 void clear();
32
34 template<class O>
35 bool iParticlesInCone( float eta, float phi, float dr, O& output ) const;
36
38 bool isInitialized() const;
39
40 private:
42 static constexpr float m_2PI = 2*M_PI;
43
45 float phiInRange(float phi) const {
46 while (phi >= M_PI) phi -= m_2PI;
47 while (phi < -M_PI) phi += m_2PI;
48 return phi;
49 }
50
52 int phiIndex(float phi) const { return (phi + M_PI)/m_phiBinSize; };
53
55 void addEntry( int i, std::vector< const T* >& output ) const;
56
58 void addEntry( int i, std::vector< ElementLink<DataVector<T> > >& output ) const;
59
60
61 const DataVector<T>* m_container; //internal container
64 float m_minPt;
65 std::vector< std::vector< int > > m_phiBinnedLookUpTable;
66 };
67
68 template<class T>
70
71
72 template<class T>
76
77 template<class T>
81 m_container = &particles;
84 unsigned int size = particles.size();
85 for( unsigned int i=0; i<size;++i ){
86 if(particles[i]->pt()>m_minPt){//sanity check (mainly due to Truth)
87 int index = phiIndex(particles[i]->phi());
88 m_phiBinnedLookUpTable[index].push_back(i);
89 }
90 }
91 //std::cout << " initializing lookup " << m_container->size() << std::endl;
92 for( auto& vec : m_phiBinnedLookUpTable ) {
93 if( vec.empty() ) continue;
94 std::stable_sort(vec.begin(),vec.end(),[&](int i, int j) { return (*m_container)[i]->eta() < (*m_container)[j]->eta(); });
95 // std::cout << " new phi slice " << vec.size() << " eta " ;
96 // for( auto index : vec ){
97 // std::cout << " " << (*m_container)[index]->eta();
98 // }
99 // std::cout << std::endl;
100 }
101 }
102
103 template<class T> template<class O>
104 bool IParticlesLookUpTable<T>::iParticlesInCone( float eta, float phi, float dr, O& output ) const {
105
107 if( m_phiBinnedLookUpTable.empty() ) return false;
108
110 auto compEta1 = [&](int i,double val ) { return (*m_container)[i]->eta() < val; };
111 auto compEta2 = [&](double val,int i ) { return val < (*m_container)[i]->eta(); };
112
114 int indexMin = phiIndex( phiInRange(phi-dr) );
115 int indexMax = phiIndex( phiInRange(phi+dr) );
116 //std::cout << " eta " << eta << " phi " << phi << " phi ranges " << indexMin << " " << indexMax << std::endl;
117 // special treatment for boundary region
118 std::vector< std::pair<int,int> > ranges;
119 if( indexMin > indexMax ) {
120 ranges.push_back( std::make_pair(0,indexMax) );
121 ranges.push_back( std::make_pair(indexMin,m_nphiBins-1) );
122 }else{
123 ranges.push_back( std::make_pair(indexMin,indexMax) );
124 }
125
126 float dr2Cut = dr*dr;
127
128 // loop over ranges
129 for( auto& range : ranges ){
130 indexMin = range.first;
131 indexMax = range.second;
132 for( ; indexMin <= indexMax; ++indexMin ){
133 // get iterators for iparticles to be included
134 const std::vector< int >& tps = m_phiBinnedLookUpTable[indexMin];
135 auto it_min = std::lower_bound (tps.begin(),tps.end(),eta-dr,compEta1 );
136 auto it_max = std::upper_bound (it_min,tps.end(),eta+dr,compEta2 );
137 //std::cout << " new range, entries in bounds " << std::distance(it_max,it_min) << std::endl;
138 // add iparticles in cone
139 for( ;it_min!=it_max;++it_min ){
140 const T* entry = (*m_container)[*it_min];
141 float deta = eta- entry->eta();
142 float dphi = phiInRange(phi-entry->phi());
143 float dr2 = deta*deta + dphi*dphi;
144 //std::cout << " new entry: " << *it_min << " eta,phi " << entry->eta() << " phi " << entry->phi() << " dr " << sqrt(dr2) << std::endl;
145 if( dr2 < dr2Cut ) addEntry( *it_min, output );
146 }
147 }
148 }
149 return true;
150 }
151
152 template<class T>
153 void IParticlesLookUpTable<T>::addEntry( int i, std::vector< const T* >& output ) const {
154 output.push_back( (*m_container)[i]);
155 }
156
157 template<class T>
158 void IParticlesLookUpTable<T>::addEntry( int i, std::vector< ElementLink<DataVector<T> > >& output ) const {
159 output.push_back( ElementLink<DataVector<T> >(*m_container,i) );
160 }
161
162} // end of namespace
163
164#endif
165
166
#define M_PI
Scalar eta() const
pseudorapidity method
std::vector< size_t > vec
An STL vector of pointers that by default owns its pointed-to elements.
Derived DataVector<T>.
Definition DataVector.h:795
void addEntry(int i, std::vector< ElementLink< DataVector< T > > > &output) const
add an entry into a vector of ElementLinks
const DataVector< CaloCluster > * m_container
float phiInRange(float phi) const
hepler function to ensure phi is within +-Pi
bool iParticlesInCone(float eta, float phi, float dr, O &output) const
collect IParticles in a given cone
void addEntry(int i, std::vector< const T * > &output) const
add an entry into a vector of pointers
bool isInitialized() const
return whether table is initialized
int phiIndex(float phi) const
calculate phi index for a given phi
IParticlesLookUpTable(unsigned int nbins=50, float minPt=1e-3)
constructor taking the desired binsize
void init(const DataVector< T > &particles)
initialize the look up table with an iParticle container
std::vector< std::vector< int > > m_phiBinnedLookUpTable
void clear()
clear the look up table
Definition index.py:1
void stable_sort(DataModel_detail::iterator< DVL > beg, DataModel_detail::iterator< DVL > end)
Specialization of stable_sort for DataVector/List.
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
setRcore setEtHad setFside pt