CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HcalTopology.cc
Go to the documentation of this file.
2 #include <cmath>
3 #include <iostream>
4 #include <cassert>
5 #include <algorithm>
6 
7 static const int IPHI_MAX=72;
8 
10  excludeHB_(false),
11  excludeHE_(false),
12  excludeHO_(false),
13  excludeHF_(false),
14  h2mode_(h2_mode),
15  firstHBRing_(1),
16  lastHBRing_(16),
17  firstHERing_(16),
18  lastHERing_(29),
19  firstHFRing_(29),
20  lastHFRing_(41),
21  firstHORing_(1),
22  lastHORing_(15),
23  firstHEDoublePhiRing_((h2_mode)?(22):(21)),
24  firstHFQuadPhiRing_(40),
25  firstHETripleDepthRing_((h2_mode)?(24):(27)),
26  singlePhiBins_(72),
27  doublePhiBins_(36)
28 {
29 }
30 
31 
32 bool HcalTopology::valid(const HcalDetId& id) const {
33  // check the raw rules
34  bool ok=validRaw(id);
35 
36  ok=ok && !isExcluded(id);
37 
38  return ok;
39 }
40 
41 bool HcalTopology::isExcluded(const HcalDetId& id) const {
42  bool exed=false;
43  // first, check the full detector exclusions... (fast)
44  switch (id.subdet()) {
45  case(HcalBarrel): exed=excludeHB_; break;
46  case(HcalEndcap): exed=excludeHE_; break;
47  case(HcalOuter): exed=excludeHO_; break;
48  case(HcalForward): exed=excludeHF_; break;
49  default: exed=false;
50  }
51  // next, check the list (slower)
52  if (!exed && !exclusionList_.empty()) {
53  std::vector<HcalDetId>::const_iterator i=std::lower_bound(exclusionList_.begin(),exclusionList_.end(),id);
54  if (i!=exclusionList_.end() && *i==id) exed=true;
55  }
56  return exed;
57 }
58 
60  std::vector<HcalDetId>::iterator i=std::lower_bound(exclusionList_.begin(),exclusionList_.end(),id);
61  if (i==exclusionList_.end() || *i!=id) {
62  exclusionList_.insert(i,id);
63  }
64 }
65 
67  switch (subdet) {
68  case(HcalBarrel): excludeHB_=true; break;
69  case(HcalEndcap): excludeHE_=true; break;
70  case(HcalOuter): excludeHO_=true; break;
71  case(HcalForward): excludeHF_=true; break;
72  default: break;
73  }
74 }
75 
76 std::vector<DetId> HcalTopology::east(const DetId& id) const {
77  std::vector<DetId> vNeighborsDetId;
78  HcalDetId neighbors[2];
79  for (int i=0;i<decIEta(HcalDetId(id),neighbors);i++)
80  vNeighborsDetId.push_back(DetId(neighbors[i].rawId()));
81  return vNeighborsDetId;
82 }
83 
84 std::vector<DetId> HcalTopology::west(const DetId& id) const {
85  std::vector<DetId> vNeighborsDetId;
86  HcalDetId neighbors[2];
87  for (int i=0;i<incIEta(HcalDetId(id),neighbors);i++)
88  vNeighborsDetId.push_back(DetId(neighbors[i].rawId()));
89  return vNeighborsDetId;
90 }
91 
92 std::vector<DetId> HcalTopology::north(const DetId& id) const {
93  std::vector<DetId> vNeighborsDetId;
94  HcalDetId neighbor;
95  if (incIPhi(HcalDetId(id),neighbor))
96  vNeighborsDetId.push_back(DetId(neighbor.rawId()));
97  return vNeighborsDetId;
98 }
99 
100 std::vector<DetId> HcalTopology::south(const DetId& id) const {
101  std::vector<DetId> vNeighborsDetId;
102  HcalDetId neighbor;
103  if (decIPhi(HcalDetId(id),neighbor))
104  vNeighborsDetId.push_back(DetId(neighbor.rawId()));
105  return vNeighborsDetId;
106 }
107 
108 std::vector<DetId> HcalTopology::up(const DetId& id) const {
109  HcalDetId neighbor = id;
110  //A.N.
111  // incrementDepth(neighbor);
112  std::vector<DetId> vNeighborsDetId;
113  if(incrementDepth(neighbor))
114  {
115  vNeighborsDetId.push_back(neighbor);
116  }
117  return vNeighborsDetId;
118 }
119 
120 std::vector<DetId> HcalTopology::down(const DetId& /*id*/) const {
121  std::cout << "HcalTopology::down() not yet implemented" << std::endl;
122  std::vector<DetId> vNeighborsDetId;
123  return vNeighborsDetId;
124 }
125 
126 int HcalTopology::exclude(HcalSubdetector subdet, int ieta1, int ieta2, int iphi1, int iphi2, int depth1, int depth2) {
127 
128  bool exed=false;
129  // first, check the full detector exclusions... (fast)
130  switch (subdet) {
131  case(HcalBarrel): exed=excludeHB_; break;
132  case(HcalEndcap): exed=excludeHE_; break;
133  case(HcalOuter): exed=excludeHO_; break;
134  case(HcalForward): exed=excludeHF_; break;
135  default: exed=false;
136  }
137  if (exed) return 0; // if the whole detector is excluded...
138 
139  int ieta_l=std::min(ieta1,ieta2);
140  int ieta_h=std::max(ieta1,ieta2);
141  int iphi_l=std::min(iphi1,iphi2);
142  int iphi_h=std::max(iphi1,iphi2);
143  int depth_l=std::min(depth1,depth2);
144  int depth_h=std::max(depth1,depth2);
145 
146  int n=0;
147  for (int ieta=ieta_l; ieta<=ieta_h; ieta++)
148  for (int iphi=iphi_l; iphi<=iphi_h; iphi++)
149  for (int depth=depth_l; depth<=depth_h; depth++) {
150  HcalDetId id(subdet,ieta,iphi,depth);
151  if (validRaw(id)) { // use 'validRaw' to include check validity in "uncut" detector
152  exclude(id);
153  n++;
154  }
155  }
156  return n;
157 }
158 
186 bool HcalTopology::validRaw(const HcalDetId& id) const {
187  bool ok=true;
188  int ieta=id.ieta();
189  int aieta=id.ietaAbs();
190  int depth=id.depth();
191  int iphi=id.iphi();
192 
193  if ((ieta==0 || iphi<=0 || iphi>IPHI_MAX) || aieta>41) return false; // outer limits
194 
195  if (ok) {
196  HcalSubdetector subdet=id.subdet();
197  if (subdet==HcalBarrel) {
198  if (aieta>16 || depth>2 || (aieta<=14 && depth>1)) ok=false;
199  } else if (subdet==HcalEndcap) {
200  if (depth>3 || aieta<16 || aieta>lastHERing() ||
201  (aieta==16 && depth!=3) ||
202  (aieta==17 && depth!=1 && !h2mode_) || // special case at H2
203  (((aieta>=17 && aieta<firstHETripleDepthRing()) || aieta==29) && depth>2) ||
204  (aieta>=firstHEDoublePhiRing() && (iphi%2)==0)) ok=false;
205  } else if (subdet==HcalOuter) {
206  if (aieta>15 || iphi>IPHI_MAX || depth!=4) ok=false;
207  } else if (subdet==HcalForward) {
208  if (aieta<firstHFRing() || aieta>lastHFRing() ||
209  ((iphi%2)==0) ||
210  (depth>2) ||
211  (aieta>=firstHFQuadPhiRing() && ((iphi+1)%4)!=0)) ok=false;
212  } else ok=false;
213  }
214 
215  return ok;
216 }
217 
218 
219 bool HcalTopology::incIPhi(const HcalDetId& id, HcalDetId &neighbor) const {
220  bool ok=valid(id);
221  if (ok) {
222  switch (id.subdet()) {
223  case (HcalBarrel):
224  case (HcalOuter):
225  if (id.iphi()==IPHI_MAX) neighbor=HcalDetId(id.subdet(),id.ieta(),1,id.depth());
226  else neighbor=HcalDetId(id.subdet(),id.ieta(),id.iphi()+1,id.depth());
227  break;
228  case (HcalEndcap):
229  if (id.ietaAbs()>=firstHEDoublePhiRing()) {
230  if (id.iphi()==IPHI_MAX-1) neighbor=HcalDetId(id.subdet(),id.ieta(),1,id.depth());
231  else neighbor=HcalDetId(id.subdet(),id.ieta(),id.iphi()+2,id.depth());
232  } else {
233  if (id.iphi()==IPHI_MAX) neighbor=HcalDetId(id.subdet(),id.ieta(),1,id.depth());
234  else neighbor=HcalDetId(id.subdet(),id.ieta(),id.iphi()+1,id.depth());
235  }
236  break;
237  case (HcalForward):
238  if (id.ietaAbs()>=firstHFQuadPhiRing()) {
239  if (id.iphi()==IPHI_MAX-1) neighbor=HcalDetId(id.subdet(),id.ieta(),3,id.depth());
240  else neighbor=HcalDetId(id.subdet(),id.ieta(),id.iphi()+4,id.depth());
241  } else {
242  if (id.iphi()==IPHI_MAX-1) neighbor=HcalDetId(id.subdet(),id.ieta(),1,id.depth());
243  else neighbor=HcalDetId(id.subdet(),id.ieta(),id.iphi()+2,id.depth());
244  }
245  break;
246  default: ok=false;
247  }
248  }
249  return ok;
250 }
251 
253 bool HcalTopology::decIPhi(const HcalDetId& id, HcalDetId &neighbor) const {
254  bool ok=valid(id);
255  if (ok) {
256  switch (id.subdet()) {
257  case (HcalBarrel):
258  case (HcalOuter):
259  if (id.iphi()==1) neighbor=HcalDetId(id.subdet(),id.ieta(),IPHI_MAX,id.depth());
260  else neighbor=HcalDetId(id.subdet(),id.ieta(),id.iphi()-1,id.depth());
261  break;
262  case (HcalEndcap):
263  if (id.ietaAbs()>=firstHEDoublePhiRing()) {
264  if (id.iphi()==1) neighbor=HcalDetId(id.subdet(),id.ieta(),IPHI_MAX-1,id.depth());
265  else neighbor=HcalDetId(id.subdet(),id.ieta(),id.iphi()-2,id.depth());
266  } else {
267  if (id.iphi()==1) neighbor=HcalDetId(id.subdet(),id.ieta(),IPHI_MAX,id.depth());
268  else neighbor=HcalDetId(id.subdet(),id.ieta(),id.iphi()-1,id.depth());
269  }
270  break;
271  case (HcalForward):
272  if (id.ietaAbs()>=firstHFQuadPhiRing()) {
273  if (id.iphi()==3) neighbor=HcalDetId(id.subdet(),id.ieta(),IPHI_MAX-1,id.depth());
274  else neighbor=HcalDetId(id.subdet(),id.ieta(),id.iphi()-4,id.depth());
275  } else {
276  if (id.iphi()==1) neighbor=HcalDetId(id.subdet(),id.ieta(),IPHI_MAX-1,id.depth());
277  else neighbor=HcalDetId(id.subdet(),id.ieta(),id.iphi()-2,id.depth());
278  }
279  break;
280  default: ok=false;
281  }
282  }
283  return ok;
284 }
285 
286 int HcalTopology::incIEta(const HcalDetId& id, HcalDetId neighbors[2]) const {
287  if (id.zside()==1) return incAIEta(id,neighbors);
288  else return decAIEta(id,neighbors);
289 }
290 
291 int HcalTopology::decIEta(const HcalDetId& id, HcalDetId neighbors[2]) const {
292  if (id.zside()==1) return decAIEta(id,neighbors);
293  else return incAIEta(id,neighbors);
294 }
295 
297 int HcalTopology::incAIEta(const HcalDetId& id, HcalDetId neighbors[2]) const {
298  int n=1;
299  int aieta=id.ietaAbs();
300 
301  if (aieta==firstHEDoublePhiRing()-1 && (id.iphi()%2)==0)
302  neighbors[0]=HcalDetId(HcalEndcap,(aieta+1)*id.zside(),id.iphi()-1,id.depth());
303  else if (aieta==firstHFQuadPhiRing()-1 && ((id.iphi()+1)%4)!=0)
304  neighbors[0]=(id.iphi()==1)? HcalDetId(HcalForward,(aieta+1)*id.zside(),71,id.depth()) : HcalDetId(HcalForward,(aieta+1)*id.zside(),(id.iphi()-2),id.depth());
305  else if (aieta==lastHBRing())
306  neighbors[0]=HcalDetId(HcalEndcap,(aieta+1)*id.zside(),id.iphi(),1);
307  else if (aieta==lastHERing())
308  neighbors[0]=HcalDetId(HcalForward,(aieta+1)*id.zside(),id.iphi(),1);
309  else
310  neighbors[0]=HcalDetId(id.subdet(),(aieta+1)*id.zside(),id.iphi(),id.depth());
311 
312  if (!valid(neighbors[0])) n=0;
313  return n;
314 }
315 
317 int HcalTopology::decAIEta(const HcalDetId& id, HcalDetId neighbors[2]) const {
318  int n=1;
319  int aieta=id.ietaAbs();
320 
321  if (aieta==firstHEDoublePhiRing()) {
322  n=2;
323  neighbors[0]=HcalDetId(HcalEndcap,(aieta-1)*id.zside(),id.iphi(),id.depth());
324  neighbors[1]=HcalDetId(HcalEndcap,(aieta-1)*id.zside(),id.iphi()+1,id.depth());
325  } else if (aieta==firstHFQuadPhiRing()) {
326  n=2;
327  neighbors[0]=HcalDetId(HcalForward,(aieta-1)*id.zside(),id.iphi(),id.depth());
328  if (id.iphi()==IPHI_MAX-1) neighbors[1]=HcalDetId(HcalForward,(aieta-1)*id.zside(),1,id.depth());
329  else neighbors[1]=HcalDetId(HcalForward,(aieta-1)*id.zside(),id.iphi()+2,id.depth());
330  } else if (aieta==1) {
331  neighbors[0]=HcalDetId(id.subdet(),-aieta*id.zside(),id.iphi(),id.depth());
332  } else if (aieta==lastHBRing()+1) {
333  neighbors[0]=HcalDetId(HcalBarrel,(aieta-1)*id.zside(),id.iphi(),id.depth());
334  } else if (aieta==lastHERing()+1) {
335  neighbors[0]=HcalDetId(HcalEndcap,(aieta-1)*id.zside(),id.iphi(),id.depth());
336  } else
337  neighbors[0]=HcalDetId(id.subdet(),(aieta-1)*id.zside(),id.iphi(),id.depth());
338 
339  if (!valid(neighbors[0]) && n==2) {
340  if (!valid(neighbors[1])) n=0;
341  else {
342  n=1;
343  neighbors[0]=neighbors[1];
344  }
345  }
346  if (n==2 && !valid(neighbors[1])) n=1;
347  if (n==1 && !valid(neighbors[0])) n=0;
348 
349  return n;
350 }
351 
352 
354  int & nDepthBins, int & startingBin) const {
355  if(subdet == HcalBarrel) {
356  if (etaRing<=14) {
357  nDepthBins = 1;
358  startingBin = 1;
359  } else {
360  nDepthBins = 2;
361  startingBin = 1;
362  }
363  } else if(subdet == HcalEndcap) {
364  if (etaRing==16) {
365  nDepthBins = 1;
366  startingBin = 3;
367  } else if (etaRing==17) {
368  nDepthBins = 1;
369  startingBin = 1;
370  } else if (etaRing==lastHERing()) {
371  nDepthBins = 2;
372  startingBin = 1;
373  }
374  else {
375  nDepthBins = (etaRing >= firstHETripleDepthRing()) ? 3 : 2;
376  startingBin = 1;
377  }
378  }
379 
380  else if(subdet == HcalForward) {
381  nDepthBins = 2;
382  startingBin = 1;
383  }
384 
385  else if(subdet == HcalOuter) {
386  nDepthBins = 1;
387  startingBin = 4;
388  }
389 
390  else {
391  std::cerr << "Bad HCAL subdetector " << subdet << std::endl;
392  }
393 }
394 
395 
397 {
398  HcalSubdetector subdet = detId.subdet();
399  int ieta = detId.ieta();
400  int etaRing = detId.ietaAbs();
401  int depth = detId.depth();
402  int nDepthBins, startingBin;
403  depthBinInformation(subdet, etaRing, nDepthBins, startingBin);
404 
405  // see if the new depth bin exists
406  ++depth;
407  if(depth > nDepthBins)
408  {
409  // handle on a case-by-case basis
410  if(subdet == HcalBarrel && etaRing < lastHORing())
411  {
412  // HO
413  subdet = HcalOuter;
414  depth = 4;
415  }
416  else if(subdet == HcalBarrel && etaRing == lastHBRing())
417  {
418  // overlap
419  subdet = HcalEndcap;
420  }
421  else if(subdet == HcalEndcap && etaRing == lastHERing()-1)
422  {
423  // guard ring HF29 is behind HE 28
424  subdet = HcalForward;
425  (ieta > 0) ? ++ieta : --ieta;
426  depth = 1;
427  }
428  else if(subdet == HcalEndcap && etaRing == lastHERing())
429  {
430  // split cells go to bigger granularity. Ring 29 -> 28
431  (ieta > 0) ? --ieta : ++ieta;
432  }
433  else
434  {
435  // no more chances
436  detId = HcalDetId();
437  return false;
438  }
439  }
440  detId = HcalDetId(subdet, ieta, detId.iphi(), depth);
441  //A.N.
442  // assert(validRaw(detId));
443  return validRaw(detId);
444  //A.N. return true;
445 }
446 
447 
448 int HcalTopology::nPhiBins(int etaRing) const {
449  int lastPhiBin=singlePhiBins_;
450  if (etaRing>= firstHFQuadPhiRing()) lastPhiBin=doublePhiBins_/2;
451  else if (etaRing>= firstHEDoublePhiRing()) lastPhiBin=doublePhiBins_;
452  return lastPhiBin;
453 }
454 
455 
456 
int firstHFRing() const
Definition: HcalTopology.h:65
int i
Definition: DBlmapReader.cc:9
int decIEta(const HcalDetId &id, HcalDetId neighbors[2]) const
void excludeSubdetector(HcalSubdetector subdet)
Definition: HcalTopology.cc:66
HcalSubdetector subdet() const
get the subdetector
Definition: HcalDetId.h:32
virtual bool valid(const HcalDetId &id) const
Definition: HcalTopology.cc:32
virtual std::vector< DetId > down(const DetId &id) const
virtual std::vector< DetId > south(const DetId &id) const
void exclude(const HcalDetId &id)
Definition: HcalTopology.cc:59
int nPhiBins(int etaRing) const
how many phi segments in this ring
int incIEta(const HcalDetId &id, HcalDetId neighbors[2]) const
int lastHBRing() const
Definition: HcalTopology.h:62
#define min(a, b)
Definition: mlp_lapack.h:161
bool decIPhi(const HcalDetId &id, HcalDetId &neighbor) const
uint32_t rawId() const
get the raw id
Definition: DetId.h:45
bool isExcluded(const HcalDetId &id) const
Definition: HcalTopology.cc:41
int depth() const
get the tower depth
Definition: HcalDetId.h:42
bool incIPhi(const HcalDetId &id, HcalDetId &neighbor) const
bool validRaw(const HcalDetId &id) const
HcalTopology(bool h2_mode=false)
Definition: HcalTopology.cc:9
const T & max(const T &a, const T &b)
int decAIEta(const HcalDetId &id, HcalDetId neighbors[2]) const
int firstHETripleDepthRing() const
Definition: HcalTopology.h:72
int ieta() const
get the cell ieta
Definition: HcalDetId.h:38
int lastHFRing() const
Definition: HcalTopology.h:66
HcalSubdetector
Definition: HcalAssistant.h:32
virtual std::vector< DetId > west(const DetId &id) const
Definition: HcalTopology.cc:84
void depthBinInformation(HcalSubdetector subdet, int etaRing, int &nDepthBins, int &startingBin) const
finds the number of depth bins and which is the number to start with
int firstHEDoublePhiRing() const
Definition: HcalTopology.h:70
int ietaAbs() const
get the absolute value of the cell ieta
Definition: HcalDetId.h:36
int iphi() const
get the cell iphi
Definition: HcalDetId.h:40
Definition: DetId.h:20
bool incrementDepth(HcalDetId &id) const
virtual std::vector< DetId > north(const DetId &id) const
Definition: HcalTopology.cc:92
int firstHFQuadPhiRing() const
Definition: HcalTopology.h:71
virtual std::vector< DetId > up(const DetId &id) const
const int singlePhiBins_
Definition: HcalTopology.h:110
const int doublePhiBins_
Definition: HcalTopology.h:111
std::vector< HcalDetId > exclusionList_
Definition: HcalTopology.h:92
tuple cout
Definition: gather_cfg.py:121
virtual std::vector< DetId > east(const DetId &id) const
Definition: HcalTopology.cc:76
int incAIEta(const HcalDetId &id, HcalDetId neighbors[2]) const
int lastHORing() const
Definition: HcalTopology.h:68
int lastHERing() const
Definition: HcalTopology.h:64
static const int IPHI_MAX
Definition: HcalTopology.cc:7