CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EgammaTowerIsolation.h
Go to the documentation of this file.
1 #ifndef EgammaTowerIsolation_h
2 #define EgammaTowerIsolation_h
3 
4 //*****************************************************************************
5 // File: EgammaTowerIsolation.h
6 // ----------------------------------------------------------------------------
7 // OrigAuth: Matthias Mozer
8 // Institute: IIHE-VUB
9 // Adding feature to exclude towers used by H/E
10 //
11 // 11/11/12 Hack by VI to make it 100 times faster
12 //=============================================================================
13 //*****************************************************************************
14 
15 #include<vector>
16 
17 
18 //CMSSW includes
23 
24 
25 #include <cmath>
26 #include <algorithm>
27 #include <cstdint>
28 #include <atomic>
29 
31 
32 
33 
34 /*
35  for each set of cuts it will compute Et for all, depth1 and depth2 twice:
36  one between inner and outer and once inside outer vetoid the tower to excude
37 
38  */
39 template<unsigned int NC>
41  public:
42 
43  struct Sum {
44  Sum(): he{0},h2{0},heBC{0},h2BC{0}
45  {}
46  float he[NC];
47  float h2[NC];
48  float heBC[NC];
49  float h2BC[NC];
50  };
51 
52  // number of cuts
53  constexpr static unsigned int NCuts = NC;
54 
55  //constructors
56 
58  EgammaTowerIsolationNew (float extRadius[NC],
59  float intRadius[NC],
60  CaloTowerCollection const & towers) ;
61 
62 
64 
65  void compute(bool et, Sum&sum, reco::Candidate const & cand, CaloTowerDetId const * first, CaloTowerDetId const * last) const {
66  reco::SuperCluster const & sc = *cand.get<reco::SuperClusterRef>().get();
67  return compute(et,sum,sc,first,last);
68  }
69  void compute(bool et, Sum &sum, reco::SuperCluster const & sc, CaloTowerDetId const * first, CaloTowerDetId const * last) const;
70 
71  void setRadius(float const extRadius[NC],float const intRadius[NC]) {
72  for (std::size_t i=0; i!=NCuts; ++i) {
73  extRadius2_[i]=extRadius[i]*extRadius[i];
74  intRadius2_[i]=intRadius[i]*intRadius[i];
75  }
76  maxEta = *std::max_element(extRadius,extRadius+NC);
77  }
78 
79 public:
80 
81  float extRadius2_[NCuts] ;
82  float intRadius2_[NCuts] ;
83 
84  float maxEta;
85  //SOA
86  const uint32_t nt;
87  float * eta;
88  float * phi;
89  float * he;
90  float * h2;
91  float * st;
92  uint32_t * id;
93  uint32_t * mem=nullptr;
94  void initSoa() {
95  mem = new uint32_t[nt*6];
96  eta = (float*)(mem); phi = eta+nt; he = phi+nt; h2 = he+nt; st = h2+nt;
97  id = (uint32_t*)(st) + nt;
98  }
99 
100 
101 };
102 
103 /*#define ETISTATDEBUG*/
104 #ifdef ETISTATDEBUG
105 namespace etiStat {
106 
107  struct Count {
108  std::atomic<uint32_t> create=0;
109  std::atomic<uint32_t> comp=0;
110  std::atomic<uint32_t> span=0;
111  static Count count;
112  ~Count();
113  };
114 
115 }
116 #endif
117 
118 template<unsigned int NC>
119 inline
121  float intRadius[NC],
122  CaloTowerCollection const & towers) :
123  maxEta(*std::max_element(extRadius,extRadius+NC)),
124  nt(towers.size()) {
125  if (nt==0) return;
126  initSoa();
127 
128 #ifdef ETISTATDEBUG
129  etiStat::Count::count.create++;
130 #endif
131 
132  for (std::size_t i=0; i!=NCuts; ++i) {
133  extRadius2_[i]=extRadius[i]*extRadius[i];
134  intRadius2_[i]=intRadius[i]*intRadius[i];
135  }
136 
137  // sort in eta (kd-tree anoverkill,does not vectorize...)
138  uint32_t index[nt];
139  float e[nt];
140  for (std::size_t k=0; k!=nt; ++k) {
141  e[k]=towers[k].eta();
142  index[k]=k;
143  std::push_heap(index,index+k+1,[&e](uint32_t i, uint32_t j){ return e[i]<e[j];});
144  }
145  std::sort_heap(index,index+nt,[&e](uint32_t i, uint32_t j){ return e[i]<e[j];});
146 
147 
148  for (std::size_t i=0;i!=nt; ++i) {
149  auto j = index[i];
150  eta[i]=towers[j].eta();
151  phi[i]=towers[j].phi();
152  id[i]=towers[j].id();
153  st[i] = 1.f/std::cosh(eta[i]); // std::sin(towers[j].theta()); //;
154  he[i] = towers[j].hadEnergy();
155  h2[i] = towers[j].hadEnergyHeOuterLayer();
156  }
157 }
158 
159 
160 template<unsigned int NC>
161 inline
162 void
164  if (nt==0) return;
165 
166 #ifdef ETISTATDEBUG
167  etiStat::Count::count.comp++;
168 #endif
169 
170  float candEta = sc.eta();
171  float candPhi = sc.phi();
172 
173 
174  auto lb = std::lower_bound(eta,eta+nt,candEta-maxEta);
175  auto ub = std::upper_bound(lb,eta+nt,candEta+maxEta);
176  uint32_t il = lb-eta;
177  uint32_t iu = std::min(nt,uint32_t(ub-eta+1));
178 
179 #ifdef ETISTATDEBUG
180  etiStat::Count::count.span += (iu-il);
181 #endif
182 
183  bool ok[iu-il];
184  for (std::size_t i=il;i!=iu; ++i)
185  ok[i-il] = (std::find(first,last,id[i])==last);
186 
187 
188 
189  // should be restricted in eta....
190  for (std::size_t i=il;i!=iu; ++i) {
191  float dr2 = reco::deltaR2(candEta,candPhi,eta[i], phi[i]);
192  float tt = et ? st[i] : 1.f;
193  for (std::size_t j=0; j!=NCuts; ++j) {
194  if (dr2<extRadius2_[j]) {
195  if (dr2>=intRadius2_[j]) {
196  sum.he[j] +=he[i]*tt;
197  sum.h2[j] +=h2[i]*tt;
198  }
199  if(ok[i-il]) {
200  sum.heBC[j] +=he[i]*tt;
201  sum.h2BC[j] +=h2[i]*tt;
202  }
203  }
204  }
205  }
206 }
207 
209 public:
210 
212 
213  //constructors
214  EgammaTowerIsolation (float extRadiusI,
215  float intRadiusI,
216  float etLow,
217  signed int depth,
218  const CaloTowerCollection* towers );
219 
220  double getTowerEtSum (const reco::Candidate* cand, const std::vector<CaloTowerDetId> * detIdToExclude=0 ) const{
221  reco::SuperCluster const & sc = *cand->get<reco::SuperClusterRef>().get();
222  return getSum(true,sc,detIdToExclude);
223  }
224  double getTowerESum (const reco::Candidate* cand, const std::vector<CaloTowerDetId> * detIdToExclude=0) const{
225  reco::SuperCluster const & sc = *cand->get<reco::SuperClusterRef>().get();
226  return getSum(false,sc,detIdToExclude);
227  }
228  double getTowerEtSum (reco::SuperCluster const * sc, const std::vector<CaloTowerDetId> * detIdToExclude=0 ) const{
229  return getSum(true,*sc,detIdToExclude);
230  }
231  double getTowerESum (reco::SuperCluster const * sc, const std::vector<CaloTowerDetId> * detIdToExclude=0) const{
232  return getSum(false,*sc,detIdToExclude);
233  }
234  private:
235  double getSum (bool et, reco::SuperCluster const & sc, const std::vector<CaloTowerDetId> * detIdToExclude) const;
236 
237 
238 private:
239  thread_local static EgammaTowerIsolationNew<1> * newAlgo;
240  thread_local static const CaloTowerCollection* oldTowers;
241  thread_local static uint32_t id15;
242  signed int depth_;
243  float extRadius;
244  float intRadius;
245 };
246 
247 
248 
249 #endif
int i
Definition: DBlmapReader.cc:9
double getSum(bool et, reco::SuperCluster const &sc, const std::vector< CaloTowerDetId > *detIdToExclude) const
static thread_local uint32_t id15
static thread_local EgammaTowerIsolationNew< 1 > * newAlgo
double maxEta
static constexpr unsigned int NCuts
T eta() const
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
static thread_local const CaloTowerCollection * oldTowers
double getTowerESum(reco::SuperCluster const *sc, const std::vector< CaloTowerDetId > *detIdToExclude=0) const
double eta() const
pseudorapidity of cluster centroid
Definition: CaloCluster.h:161
void setRadius(float const extRadius[NC], float const intRadius[NC])
int j
Definition: DBlmapReader.cc:9
bool first
Definition: L1TdeRCT.cc:75
double getTowerESum(const reco::Candidate *cand, const std::vector< CaloTowerDetId > *detIdToExclude=0) const
int nt
Definition: AMPTWrapper.h:32
int k[5][pyjets_maxn]
T1 deltaR2(T1 eta1, T2 phi1, T3 eta2, T4 phi2)
Definition: deltaR.h:58
double getTowerEtSum(const reco::Candidate *cand, const std::vector< CaloTowerDetId > *detIdToExclude=0) const
T get() const
get a component
Definition: Candidate.h:219
EgammaTowerIsolation(float extRadiusI, float intRadiusI, float etLow, signed int depth, const CaloTowerCollection *towers)
double getTowerEtSum(reco::SuperCluster const *sc, const std::vector< CaloTowerDetId > *detIdToExclude=0) const
double phi() const
azimuthal angle of cluster centroid
Definition: CaloCluster.h:164
SurfaceDeformation * create(int type, const std::vector< double > &params)
tuple size
Write out results.
#define constexpr
void compute(bool et, Sum &sum, reco::Candidate const &cand, CaloTowerDetId const *first, CaloTowerDetId const *last) const
Definition: DDAxes.h:10