CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CaloGeometryHelper.cc
Go to the documentation of this file.
2 
3 //#include "FWCore/ParameterSet/interface/ParameterSet.h"
4 
5 // needed for the debugging
10 
17 
20 
22 
23 #include <algorithm>
24 
26 {
28  psLayer1Z_ = 303;
29  psLayer2Z_ = 307;
30 }
31 
33 {
34  // std::cout << " In the constructor with ParameterSet " << std::endl;
35  psLayer1Z_ = 303;
36  psLayer2Z_ = 307;
37 }
38 
40 {
43  bfield_ = bField;
45 
47  {
48  ESDetId cps1(getEcalPreshowerGeometry()->getClosestCellInPlane(GlobalPoint(80.,80.,303.),1));
50  ESDetId cps2(getEcalPreshowerGeometry()->getClosestCellInPlane(GlobalPoint(80.,80.,307.),2));
52  LogDebug("CaloGeometryTools") << " Preshower layer positions " << psLayer1Z_ << " " << psLayer2Z_ << std::endl;
53  }
54  else
55  LogDebug("CaloGeometryTools") << " No preshower present" << std::endl;
56 
57  // std::cout << " Preshower layer positions " << psLayer1Z_ << " " << psLayer2Z_ << std::endl;
58 
59 }
60 
62 {;
63 }
64 
65 DetId CaloGeometryHelper::getClosestCell(const XYZPoint& point, bool ecal, bool central) const
66 {
67  DetId result;
68  if(ecal)
69  {
70  if(central)
71  {
72  // std::cout << "EcalBarrelGeometry_" << " " << EcalBarrelGeometry_ << std::endl;
73  result = EcalBarrelGeometry_->getClosestCell(GlobalPoint(point.X(),point.Y(),point.Z()));
74 #ifdef DEBUGGCC
75  if(result.null()) return result;
76  GlobalPoint ip=GlobalPoint(point.X(),point.Y(),point.Z());
78  float deltaeta2 = ip.eta()-cc.eta();
79  deltaeta2 *= deltaeta2;
80  float deltaphi2 = acos(cos(ip.phi()-cc.phi()));
81  deltaphi2 *= deltaphi2;
82  Histos::instance()->fill("h100",point.eta(),sqrt(deltaeta2+deltaphi2));
83 #endif
84  }
85  else
86  {
87  result = EcalEndcapGeometry_->getClosestCell(GlobalPoint(point.X(),point.Y(),point.Z()));
88 #ifdef DEBUGGCC
89  if(result.null())
90  {
91  return result;
92  }
93  GlobalPoint ip=GlobalPoint(point.X(),point.Y(),point.Z());
95  Histos::instance()->fill("h110",point.eta(),(ip-cc).perp());
96 #endif
97  }
98  }
99  else
100  {
101  result=HcalGeometry_->getClosestCell(GlobalPoint(point.X(),point.Y(),point.Z()));
102  HcalDetId myDetId(result);
103 
104  // special patch for HF
105  if ( myDetId.subdetId() == HcalForward ) {
106  int mylayer;
107  if ( fabs(point.Z()) > 1132. ) {
108  mylayer = 2;
109  } else {
110  mylayer = 1;
111  }
112  HcalDetId myDetId2((HcalSubdetector)myDetId.subdetId(),myDetId.ieta(),myDetId.iphi(),mylayer);
113  result = myDetId2;
114  return result;
115  }
116 
117 
118  if(result.subdetId()!=HcalEndcap) return result;
119  // Special patch to correct the HCAL geometry
120  if(myDetId.depth()==3) return result;
121 
122  int ieta=myDetId.ietaAbs();
123  float azmin=400.458;
124 
125  if(ieta<=17)
126  return result;
127  else if(ieta>=18 && ieta<=26)
128  azmin += 35.0; // don't consider ieta=18 nose separately
129  else if(ieta>=27)
130  azmin += 21.0;
131 
132  HcalDetId first(HcalEndcap,myDetId.ieta(),myDetId.iphi(),1);
133  bool layer2=(fabs(point.Z())>azmin);
134  if(!layer2)
135  {
136  return first;
137  }
138  else
139  {
140  HcalDetId second(HcalEndcap,myDetId.ieta(),myDetId.iphi(),2);
141  if(second!=HcalDetId()) result=second;
142  }
143 #ifdef DEBUGGCC
144  if(result.null())
145  {
146  return result;
147  }
148  GlobalPoint ip=GlobalPoint(point.x(),point.y(),point.z());
150  float deltaeta2 = ip.eta()-cc.eta();
151  deltaeta2 *= deltaeta2;
152  float deltaphi2 = acos(cos(ip.phi()-cc.phi()));
153  deltaphi2 *= deltaphi2;
154 
155  Histos::instance()->fill("h120",point.eta(),sqrt(deltaeta2+deltaphi2));
156 #endif
157 
158  }
159  return result;
160 }
161 
162 void CaloGeometryHelper::getWindow(const DetId& pivot,int s1,int s2,std::vector<DetId>& vec) const
163 {
164  // currently the getWindow method is the same for EcalBarrelTopology and EndcapTopology
165  // (implemented in CaloSubDetectorTopology)
166  // optimized versions are foreseen
167  vec=getEcalTopology(pivot.subdetId())->getWindow(pivot,s1,s2);
168  DistanceToCell distance(getEcalGeometry(pivot.subdetId()),pivot);
169  sort(vec.begin(),vec.end(),distance);
170 }
171 
172 void CaloGeometryHelper::buildCrystal(const DetId & cell,Crystal& xtal) const
173 {
174  if(cell.subdetId()==EcalBarrel)
175  {
176  xtal=Crystal(cell,&barrelCrystals_[EBDetId(cell).hashedIndex()]);
177  return;
178  }
179  if(cell.subdetId()==EcalEndcap)
180  {
181  xtal=Crystal(cell,&endcapCrystals_[EEDetId(cell).hashedIndex()]);
182  return;
183  }
184 }
185 
186 // Build the array of (max)8 neighbors
188 {
189 
190  static const CaloDirection orderedDir[8]={SOUTHWEST,SOUTH,SOUTHEAST,WEST,EAST,NORTHWEST,NORTH,
191  NORTHEAST};
192 
193  const unsigned nbarrel = EBDetId::kSizeForDenseIndexing;
194  // Barrel first. The hashed index runs from 0 to 61199
195  barrelNeighbours_.resize(nbarrel);
196 
197  //std::cout << " Building the array of neighbours (barrel) " ;
198 
199  const std::vector<DetId>& vec(EcalBarrelGeometry_->getValidDetIds(DetId::Ecal,EcalBarrel));
200  unsigned size=vec.size();
201  for(unsigned ic=0; ic<size; ++ic)
202  {
203  // We get the 9 cells in a square.
204  std::vector<DetId> neighbours(EcalBarrelTopology_->getWindow(vec[ic],3,3));
205  // std::cout << " Cell " << EBDetId(vec[ic]) << std::endl;
206  unsigned nneighbours=neighbours.size();
207 
208  unsigned hashedindex=EBDetId(vec[ic]).hashedIndex();
209  if(hashedindex>=nbarrel)
210  {
211  LogDebug("CaloGeometryTools") << " Array overflow " << std::endl;
212  }
213 
214 
215  // If there are 9 cells, it is easy, and this order is know:
216 // 6 7 8
217 // 3 4 5
218 // 0 1 2 (0 = SOUTHWEST)
219 
220  if(nneighbours==9)
221  {
222  barrelNeighbours_[hashedindex].reserve(8);
223  for(unsigned in=0;in<nneighbours;++in)
224  {
225  // remove the centre
226  if(neighbours[in]!=vec[ic])
227  {
228  barrelNeighbours_[hashedindex].push_back(neighbours[in]);
229  // std::cout << " Neighbour " << in << " " << EBDetId(neighbours[in]) << std::endl;
230  }
231  }
232  }
233  else
234  {
235  DetId central(vec[ic]);
236  barrelNeighbours_[hashedindex].resize(8,DetId(0));
237  for(unsigned idir=0;idir<8;++idir)
238  {
239  DetId testid=central;
240  bool status=move(testid,orderedDir[idir],false);
241  if(status) barrelNeighbours_[hashedindex][idir]=testid;
242  }
243 
244  }
245  }
246 
247  // Moved to the endcap
248 
249  // std::cout << " done " << size << std::endl;
250  // std::cout << " Building the array of neighbours (endcap) " ;
251 
252 
253  const std::vector<DetId> & vece(EcalEndcapGeometry_->getValidDetIds(DetId::Ecal,EcalEndcap));
254  size=vece.size();
255  // There are some holes in the hashedIndex for the EE. Hence the array is bigger than the number
256  // of crystals
257  const unsigned nendcap=EEDetId::kSizeForDenseIndexing;
258 
259  endcapNeighbours_.resize(nendcap);
260  for(unsigned ic=0; ic<size; ++ic)
261  {
262  // We get the 9 cells in a square.
263  std::vector<DetId> neighbours(EcalEndcapTopology_->getWindow(vece[ic],3,3));
264  unsigned nneighbours=neighbours.size();
265  // remove the centre
266  unsigned hashedindex=EEDetId(vece[ic]).hashedIndex();
267 
268  if(hashedindex>=nendcap)
269  {
270  LogDebug("CaloGeometryTools") << " Array overflow " << std::endl;
271  }
272 
273  if(nneighbours==9)
274  {
275  endcapNeighbours_[hashedindex].reserve(8);
276  for(unsigned in=0;in<nneighbours;++in)
277  {
278  // remove the centre
279  if(neighbours[in]!=vece[ic])
280  {
281  endcapNeighbours_[hashedindex].push_back(neighbours[in]);
282  }
283  }
284  }
285  else
286  {
287  DetId central(vece[ic]);
288  endcapNeighbours_[hashedindex].resize(8,DetId(0));
289  for(unsigned idir=0;idir<8;++idir)
290  {
291  DetId testid=central;
292  bool status=move(testid,orderedDir[idir],false);
293  if(status) endcapNeighbours_[hashedindex][idir]=testid;
294  }
295 
296  }
297  }
298  // std::cout << " done " << size <<std::endl;
300 }
301 
302 const std::vector<DetId>& CaloGeometryHelper::getNeighbours(const DetId& detid) const
303 {
304  return (detid.subdetId()==EcalBarrel)?barrelNeighbours_[EBDetId(detid).hashedIndex()]:
306 }
307 
308 bool CaloGeometryHelper::move(DetId& cell, const CaloDirection&dir,bool fast) const
309 {
310  DetId originalcell = cell;
311  if(dir==NONE || cell==DetId(0)) return false;
312 
313  // Conversion CaloDirection and index in the table
314  // CaloDirection :NONE,SOUTH,SOUTHEAST,SOUTHWEST,EAST,WEST, NORTHEAST,NORTHWEST,NORTH
315  // Table : SOUTHWEST,SOUTH,SOUTHEAST,WEST,EAST,NORTHWEST,NORTH, NORTHEAST
316  static const int calodirections[9]={-1,1,2,0,4,3,7,5,6};
317 
318  if(fast&&neighbourmapcalculated_)
319  {
320  DetId result = (originalcell.subdetId()==EcalBarrel) ?
321  barrelNeighbours_[EBDetId(originalcell).hashedIndex()][calodirections[dir]]:
322  endcapNeighbours_[EEDetId(originalcell).hashedIndex()][calodirections[dir]];
323  bool status = !result.null();
324  cell = result;
325  return status;
326  }
327 
328  if(dir==NORTH || dir ==SOUTH || dir==EAST || dir==WEST)
329  {
330  return simplemove(cell,dir);
331  }
332  else
333  {
334  if(dir == NORTHEAST || dir==NORTHWEST || dir==SOUTHEAST || dir==SOUTHWEST)
335  return diagonalmove(cell,dir);
336  }
337 
338  cell = DetId(0);
339  return false;
340 }
341 
342 
344 {
345  std::vector<DetId> neighbours;
346  if(cell.subdetId()==EcalBarrel)
347  neighbours = EcalBarrelTopology_->getNeighbours(cell,dir);
348  else if(cell.subdetId()==EcalEndcap)
349  neighbours= EcalEndcapTopology_->getNeighbours(cell,dir);
350 
351  if(neighbours.size()>0 && !neighbours[0].null())
352  {
353  cell = neighbours[0];
354  return true;
355  }
356  else
357  {
358  cell = DetId(0);
359  return false;
360  }
361 }
362 
364 {
365  bool result;
366  // One has to try both paths
367  if(dir==NORTHEAST)
368  {
369  result = simplemove(cell,NORTH);
370  if(result)
371  return simplemove(cell,EAST);
372  else
373  {
374  result = simplemove(cell,EAST);
375  if(result)
376  return simplemove(cell,NORTH);
377  else
378  return false;
379  }
380  }
381  else if(dir==NORTHWEST)
382  {
383  result = simplemove(cell,NORTH);
384  if(result)
385  return simplemove(cell,WEST);
386  else
387  {
388  result = simplemove(cell,WEST);
389  if(result)
390  return simplemove(cell,NORTH);
391  else
392  return false;
393  }
394  }
395  else if(dir == SOUTHEAST)
396  {
397  result = simplemove(cell,SOUTH);
398  if(result)
399  return simplemove(cell,EAST);
400  else
401  {
402  result = simplemove(cell,EAST);
403  if(result)
404  return simplemove(cell,SOUTH);
405  else
406  return false;
407  }
408  }
409  else if(dir == SOUTHWEST)
410  {
411  result = simplemove(cell,SOUTH);
412  if(result)
413  return simplemove(cell,WEST);
414  else
415  {
416  result = simplemove(cell,SOUTH);
417  if(result)
418  return simplemove(cell,WEST);
419  else
420  return false;
421  }
422  }
423  cell = DetId(0);
424  return false;
425 }
426 
427 bool CaloGeometryHelper::borderCrossing(const DetId& c1, const DetId& c2) const
428 {
429  if(c1.subdetId()!=c2.subdetId()) return false;
430 
431  if(c1.subdetId()==EcalBarrel)
432  {
433  // there is a crack if the two cells don't belong to the same
434  // module
435  EBDetId cc1(c1);
436  EBDetId cc2(c2);
437  return (cc1.im()!=cc2.im()||cc1.ism()!=cc2.ism() );
438  }
439 
440 if(c1.subdetId()==EcalEndcap)
441  {
442  // there is a crack if the two cells don't belong to the same
443  // module
444  return (EEDetId(c1).isc()!=EEDetId(c2).isc());
445  }
446  return false;
447 }
448 
450 {
451  const unsigned nbarrel = EBDetId::kSizeForDenseIndexing;
452  // Barrel first. The hashed index runs from 0 to 61199
453  barrelCrystals_.resize(nbarrel,BaseCrystal());
454 
455  //std::cout << " Building the array of crystals (barrel) " ;
456  const std::vector<DetId>& vec(EcalBarrelGeometry_->getValidDetIds(DetId::Ecal,EcalBarrel));
457  unsigned size=vec.size();
458  const CaloCellGeometry * geom=0;
459  for(unsigned ic=0; ic<size; ++ic)
460  {
461  unsigned hashedindex=EBDetId(vec[ic]).hashedIndex();
462  geom = EcalBarrelGeometry_->getGeometry(vec[ic]);
463  BaseCrystal xtal(vec[ic]);
464  xtal.setCorners(geom->getCorners(),geom->getPosition());
465  barrelCrystals_[hashedindex]=xtal;
466  }
467 
468  // std::cout << " done " << size << std::endl;
469  // std::cout << " Building the array of crystals (endcap) " ;
470 
471 
472  const std::vector<DetId>& vece(EcalEndcapGeometry_->getValidDetIds(DetId::Ecal,EcalEndcap));
473  size=vece.size();
474  // There are some holes in the hashedIndex for the EE. Hence the array is bigger than the number
475  // of crystals
476  const unsigned nendcap=EEDetId::kSizeForDenseIndexing;
477 
478  endcapCrystals_.resize(nendcap,BaseCrystal());
479  for(unsigned ic=0; ic<size; ++ic)
480  {
481  unsigned hashedindex=EEDetId(vece[ic]).hashedIndex();
482  geom = EcalEndcapGeometry_->getGeometry(vece[ic]);
483  BaseCrystal xtal(vece[ic]);
484  xtal.setCorners(geom->getCorners(),geom->getPosition());
485  endcapCrystals_[hashedindex]=xtal;
486  }
487  // std::cout << " done " << size << std::endl;
488 }
#define LogDebug(id)
virtual DetId getClosestCell(const GlobalPoint &r) const
int hashedIndex() const
get a compact index for arrays
Definition: EBDetId.h:73
void setCorners(const CaloCellGeometry::CornersVec &vec, const GlobalPoint &pos)
Definition: BaseCrystal.cc:14
const std::vector< DetId > & getNeighbours(const DetId &det) const
virtual std::vector< DetId > getNeighbours(const DetId &id, const CaloDirection &dir) const
void buildCrystal(const DetId &id, Crystal &) const
virtual DetId getClosestCell(const GlobalPoint &r) const
const EcalEndcapGeometry * EcalEndcapGeometry_
Definition: Calorimeter.h:78
int isc() const
Definition: EEDetId.cc:301
Geom::Phi< T > phi() const
Definition: PV3DBase.h:68
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
const EcalPreshowerGeometry * getEcalPreshowerGeometry() const
Definition: Calorimeter.h:55
std::vector< std::vector< DetId > > endcapNeighbours_
const CaloSubdetectorTopology * EcalEndcapTopology_
Definition: Calorimeter.h:84
const GlobalPoint & getPosition() const
Returns the position of reference for this cell.
bool borderCrossing(const DetId &, const DetId &) const
int ism() const
get the ECAL/SM id
Definition: EBDetId.cc:79
virtual const std::vector< DetId > & getValidDetIds(DetId::Detector det=DetId::Detector(0), int subdet=0) const
Get a list of valid detector ids (for the given subdetector)
virtual const CaloCellGeometry * getGeometry(const DetId &id) const
Get the cell geometry of a given detector id. Should return false if not found.
tuple s2
Definition: indexGen.py:106
const CaloSubdetectorGeometry * getEcalGeometry(int subdetn) const
Definition: Calorimeter.cc:133
math::XYZVector XYZPoint
U second(std::pair< T, U > const &p)
int im() const
get the number of module inside the SM (1-4)
Definition: EBDetId.cc:85
int hashedIndex(int ieta, int iphi)
Definition: EcalPyUtils.cc:42
void fill(const std::string &name, float val1, float val2=1., float val3=1.)
Fill an histogram.
Definition: Histos.cc:168
int depth() const
get the tower depth
Definition: HcalDetId.h:42
T sqrt(T t)
Definition: SSEVec.h:46
T z() const
Definition: PV3DBase.h:63
tuple result
Definition: query.py:137
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
int ieta() const
get the cell ieta
Definition: HcalDetId.h:38
std::vector< BaseCrystal > endcapCrystals_
static Histos * instance()
Definition: Histos.cc:15
HcalSubdetector
Definition: HcalAssistant.h:32
const EcalBarrelGeometry * EcalBarrelGeometry_
Definition: Calorimeter.h:77
const CaloSubdetectorTopology * getEcalTopology(int subdetn) const
Definition: Calorimeter.cc:142
bool first
Definition: L1TdeRCT.cc:94
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:39
int ietaAbs() const
get the absolute value of the cell ieta
Definition: HcalDetId.h:36
virtual DetId getClosestCell(const GlobalPoint &r) const
int iphi() const
get the cell iphi
Definition: HcalDetId.h:40
Definition: DetId.h:20
bool diagonalmove(DetId &cell, const CaloDirection &dir) const
int hashedIndex() const
Definition: EEDetId.h:177
virtual std::vector< DetId > getWindow(const DetId &id, const int &northSouthSize, const int &eastWestSize) const
void getWindow(const DetId &pivot, int s1, int s2, std::vector< DetId > &) const
bool null() const
is this a null id ?
Definition: DetId.h:47
const CaloSubdetectorGeometry * HcalGeometry_
Definition: Calorimeter.h:79
T eta() const
Definition: PV3DBase.h:75
T perp() const
Magnitude of transverse component.
const CaloSubdetectorTopology * EcalBarrelTopology_
Definition: Calorimeter.h:83
std::vector< BaseCrystal > barrelCrystals_
dbl *** dir
Definition: mlp_gen.cc:35
CaloDirection
Codes the local directions in the cell lattice.
Definition: CaloDirection.h:9
DetId getClosestCell(const XYZPoint &point, bool ecal, bool central) const
tuple status
Definition: ntuplemaker.py:245
void initialize(double bField)
std::vector< std::vector< DetId > > barrelNeighbours_
tuple size
Write out results.
bool simplemove(DetId &cell, const CaloDirection &dir) const
*vegas h *****************************************************used in the default bin number in original ***version of VEGAS is ***a higher bin number might help to derive a more precise ***grade subtle point
Definition: invegas.h:5
virtual const CornersVec & getCorners() const =0
Returns the corner points of this cell&#39;s volume.
bool move(DetId &cell, const CaloDirection &dir, bool fast=true) const