ATLAS Offline Software
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 
12 namespace 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
62  int m_nphiBins;
63  float m_phiBinSize;
64  float m_minPt;
65  std::vector< std::vector< int > > m_phiBinnedLookUpTable;
66  };
67 
68  template<class T>
69  bool IParticlesLookUpTable<T>::isInitialized() const { return !m_phiBinnedLookUpTable.empty(); }
70 
71 
72  template<class T>
74  m_phiBinnedLookUpTable.clear();
75  }
76 
77  template<class T>
80  m_phiBinnedLookUpTable.resize(m_nphiBins);
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 
trigbs_pickEvents.ranges
ranges
Definition: trigbs_pickEvents.py:60
xAOD::IParticlesLookUpTable::m_nphiBins
int m_nphiBins
Definition: IParticlesLookUpTable.h:62
xAOD::IParticlesLookUpTable::phiInRange
float phiInRange(float phi) const
hepler function to ensure phi is within +-Pi
Definition: IParticlesLookUpTable.h:45
xAOD::IParticlesLookUpTable::m_minPt
float m_minPt
bin size
Definition: IParticlesLookUpTable.h:64
xAOD::IParticlesLookUpTable::clear
void clear()
clear the look up table
Definition: IParticlesLookUpTable.h:73
index
Definition: index.py:1
M_PI
#define M_PI
Definition: ActiveFraction.h:11
xAOD
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
Definition: ICaloAffectedTool.h:24
vec
std::vector< size_t > vec
Definition: CombinationsGeneratorTest.cxx:12
python.TurnDataReader.dr
dr
Definition: TurnDataReader.py:112
xAOD::pt
setRcore setEtHad setFside pt
Definition: TrigPhoton_v1.cxx:106
xAOD::phi
setEt phi
Definition: TrigEMCluster_v1.cxx:29
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
xAOD::IParticlesLookUpTable::m_container
const DataVector< T > * m_container
Definition: IParticlesLookUpTable.h:61
xAOD::IParticlesLookUpTable::iParticlesInCone
bool iParticlesInCone(float eta, float phi, float dr, O &output) const
collect IParticles in a given cone
Definition: IParticlesLookUpTable.h:104
lumiFormat.i
int i
Definition: lumiFormat.py:85
xAOD::IParticlesLookUpTable::m_phiBinSize
float m_phiBinSize
number of bins
Definition: IParticlesLookUpTable.h:63
xAOD::e
setPy e
Definition: CompositeParticle_v1.cxx:166
eflowRec::phiInRange
float phiInRange(float phi)
hepler function to ensure phi is within +-Pi
Definition: EtaPhiLUT.cxx:16
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
xAOD::IParticlesLookUpTable::phiIndex
int phiIndex(float phi) const
calculate phi index for a given phi
Definition: IParticlesLookUpTable.h:52
xAOD::IParticlesLookUpTable::m_phiBinnedLookUpTable
std::vector< std::vector< int > > m_phiBinnedLookUpTable
cut on minimum Pt for the considered particles
Definition: IParticlesLookUpTable.h:65
DataVector< T >
xAOD::IParticlesLookUpTable::addEntry
void addEntry(int i, std::vector< const T * > &output) const
add an entry into a vector of pointers
Definition: IParticlesLookUpTable.h:153
merge.output
output
Definition: merge.py:17
GetAllXsec.entry
list entry
Definition: GetAllXsec.py:132
xAOD::IParticlesLookUpTable::m_2PI
static constexpr float m_2PI
define 2*Pi
Definition: IParticlesLookUpTable.h:42
SCT_CalibAlgs::nbins
@ nbins
Definition: SCT_CalibNumbers.h:10
eflowRec::phiIndex
unsigned int phiIndex(float phi, float binsize)
calculate phi index for a given phi
Definition: EtaPhiLUT.cxx:23
DataVector.h
An STL vector of pointers that by default owns its pointed-to elements.
DeMoScan.index
string index
Definition: DeMoScan.py:364
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
xAOD::IParticlesLookUpTable
2D look up table for iParticles
Definition: IParticlesLookUpTable.h:17
LArG4FSStartPointFilter.particles
list particles
Definition: LArG4FSStartPointFilter.py:84
xAOD::IParticlesLookUpTable::isInitialized
bool isInitialized() const
return whether table is initialized
Definition: IParticlesLookUpTable.h:69
xAOD::IParticlesLookUpTable::init
void init(const DataVector< T > &particles)
initialize the look up table with an iParticle container
Definition: IParticlesLookUpTable.h:78
xAOD::IParticlesLookUpTable::IParticlesLookUpTable
IParticlesLookUpTable(unsigned int nbins=50, float minPt=1e-3)
constructor taking the desired binsize
Definition: IParticlesLookUpTable.h:20