CMS 3D CMS Logo

HGCalGeometry.cc
Go to the documentation of this file.
1 /* for High Granularity Calorimeter
2  * This geometry is essentially driven by topology,
3  * which is thus encapsulated in this class.
4  * This makes this geometry not suitable to be loaded
5  * by regular CaloGeometryLoader<T>
6  */
15 
16 #include <cmath>
17 
18 #include <Math/Transform3D.h>
19 #include <Math/EulerAngles.h>
20 
22 typedef std::vector<float> ParmVec;
23 
24 //#define EDM_ML_DEBUG
25 
27  : m_topology( topology_ ),
28  m_validGeomIds( topology_.totalGeomModules()),
29  mode_( topology_.geomMode()),
30  m_det( topology_.detector()),
31  m_subdet( topology_.subDetector()),
32  twoBysqrt3_(2.0/std::sqrt(3.0)) {
33 
34  if (m_det == DetId::HGCalHSc) {
35  m_cellVec2 = CellVec2(topology_.totalGeomModules());
36  } else {
37  m_cellVec = CellVec(topology_.totalGeomModules());
38  }
40 #ifdef EDM_ML_DEBUG
41  edm::LogVerbatim("HGCalGeom") << "Expected total # of Geometry Modules "
43 #endif
44 }
45 
47 
49 
51 }
52 
54  const CCGFloat* pv,
55  unsigned int i,
56  Pt3D& ref) {
57  if (m_det == DetId::HGCalHSc) {
58  FlatTrd::localCorners( lc, pv, ref ) ;
59  } else {
60  FlatHexagon::localCorners( lc, pv, ref ) ;
61  }
62 }
63 
65  const GlobalPoint& f2 ,
66  const GlobalPoint& f3 ,
67  const CCGFloat* parm ,
68  const DetId& detId ) {
69 
70  DetId geomId = getGeometryDetId(detId);
71  int cells (0);
75  cells = m_topology.dddConstants().numberCellsHexagon(id.iSec1);
76 #ifdef EDM_ML_DEBUG
77  edm::LogVerbatim("HGCalGeom") << "NewCell " << HGCalDetId(detId)
78  << " GEOM " << HGCalDetId(geomId);
79 #endif
80  } else if (mode_ == HGCalGeometryMode::Trapezoid) {
81  cells = 1;
82 #ifdef EDM_ML_DEBUG
83  edm::LogVerbatim("HGCalGeom") << "NewCell " << HGCScintillatorDetId(detId)
84  << " GEOM " << HGCScintillatorDetId(geomId);
85 #endif
86  } else {
87  cells = m_topology.dddConstants().numberCellsHexagon(id.iLay,id.iSec1,
88  id.iSec2,false);
89 #ifdef EDM_ML_DEBUG
90  edm::LogVerbatim("HGCalGeom") << "NewCell " << HGCSiliconDetId(detId)
91  << " GEOM " << HGCSiliconDetId(geomId);
92 #endif
93  }
94  const uint32_t cellIndex (m_topology.detId2denseGeomId(geomId));
95 
96  if (m_det == DetId::HGCalHSc) {
97  m_cellVec2.at( cellIndex ) = FlatTrd( cornersMgr(), f1, f2, f3, parm ) ;
98  } else {
99  m_cellVec.at( cellIndex ) = FlatHexagon( cornersMgr(), f1, f2, f3, parm ) ;
100  }
101  m_validGeomIds.at( cellIndex ) = geomId ;
102 
103 #ifdef EDM_ML_DEBUG
104  edm::LogVerbatim("HGCalGeom") << "Store for DetId " << std::hex
105  << detId.rawId() << " GeomId "
106  << geomId.rawId() << std::dec << " Index "
107  << cellIndex << " cells " << cells;
108  unsigned int nOld = m_validIds.size();
109 #endif
110  if ((mode_ == HGCalGeometryMode::Hexagon) ||
112  for (int cell = 0; cell < cells; ++cell) {
113  id.iCell1 = cell;
114  DetId idc = m_topology.encode(id);
115  if (m_topology.valid(idc)) {
116  m_validIds.emplace_back(idc);
117 #ifdef EDM_ML_DEBUG
118  edm::LogVerbatim("HGCalGeom") << "Valid Id [" << cell << "] "
119  << HGCalDetId(idc);
120 #endif
121  }
122  }
123  } else if (mode_ == HGCalGeometryMode::Trapezoid) {
124  DetId idc = m_topology.encode(id);
125  if (m_topology.valid(idc)) {
126  m_validIds.emplace_back(idc);
127 #ifdef EDM_ML_DEBUG
128  edm::LogVerbatim("HGCalGeom") << "Valid Id [0] "
129  << HGCScintillatorDetId(idc);
130 #endif
131  } else {
132  edm::LogWarning("HGCalGeom") << "Check " << HGCScintillatorDetId(idc)
133  << " from " << HGCScintillatorDetId(detId)
134  << " ERROR ???";
135  }
136  } else {
137 #ifdef EDM_ML_DEBUG
138  unsigned int cellAll(0), cellSelect(0);
139 #endif
140  for (int u=0; u<2*cells; ++u) {
141  for (int v=0; v<2*cells; ++v) {
142  if (((v-u) < cells) && (u-v) <= cells) {
143  id.iCell1 = u; id.iCell2 = v;
144  DetId idc = m_topology.encode(id);
145 #ifdef EDM_ML_DEBUG
146  ++cellAll;
147 #endif
148  if (m_topology.dddConstants().cellInLayer(id.iSec1,id.iSec2,u,v,
149  id.iLay,true)) {
150  m_validIds.emplace_back(idc);
151 #ifdef EDM_ML_DEBUG
152  ++cellSelect;
153  edm::LogVerbatim("HGCalGeom") << "Valid Id [" << u << ", " << v
154  << "] " << HGCSiliconDetId(idc);
155 #endif
156  }
157  }
158  }
159  }
160 #ifdef EDM_ML_DEBUG
161  edm::LogVerbatim("HGCalGeom") << "HGCalGeometry keeps " << cellSelect
162  << " out of " << cellAll << " for wafer "
163  << id.iSec1 << ":" << id.iSec2 << " in "
164  << " layer " << id.iLay;
165 #endif
166  }
167 #ifdef EDM_ML_DEBUG
168  if (m_det == DetId::HGCalHSc) {
169  edm::LogVerbatim("HGCalGeom") << "HGCalGeometry::newCell-> [" << cellIndex
170  << "]" << " front:" << f1.x() << '/'
171  << f1.y() << '/' << f1.z() << " back:"
172  << f2.x() << '/' << f2.y() << '/' << f2.z()
173  << " eta|phi "
174  << m_cellVec2[cellIndex].etaPos() << ":"
175  << m_cellVec2[cellIndex].phiPos();
176  } else {
177  edm::LogVerbatim("HGCalGeom") << "HGCalGeometry::newCell-> [" << cellIndex
178  << "]" << " front:" << f1.x() << '/'
179  << f1.y() << '/' << f1.z() << " back:"
180  << f2.x() << '/' << f2.y() << '/' << f2.z()
181  << " eta|phi "
182  << m_cellVec[cellIndex].etaPos() << ":"
183  << m_cellVec[cellIndex].phiPos();
184  }
185  unsigned int nNew = m_validIds.size();
186  if ((mode_ == HGCalGeometryMode::Hexagon) ||
188  edm::LogVerbatim("HGCalGeom") << "ID: " << HGCalDetId(detId)
189  << " with valid DetId from " << nOld
190  << " to " << nNew;
191  } else if (mode_ == HGCalGeometryMode::Trapezoid) {
192  edm::LogVerbatim("HGCalGeom") << "ID: " << HGCScintillatorDetId(detId)
193  << " with valid DetId from " << nOld
194  << " to " << nNew;
195  } else if (m_topology.isHFNose()) {
196  edm::LogVerbatim("HGCalGeom") << "ID: " << HFNoseDetId(detId)
197  << " with valid DetId from " << nOld
198  << " to " << nNew;
199  } else {
200  edm::LogVerbatim("HGCalGeom") << "ID: " << HGCSiliconDetId(detId)
201  << " with valid DetId from " << nOld
202  << " to " << nNew;
203  }
204  edm::LogVerbatim("HGCalGeom") << "Cell[" << cellIndex << "] " << std::hex
205  << geomId.rawId() << ":"
206  << m_validGeomIds[cellIndex].rawId()
207  << std::dec;
208 #endif
209 }
210 
211 std::shared_ptr<const CaloCellGeometry> HGCalGeometry::getGeometry(const DetId& detId) const {
212  if (detId == DetId()) return nullptr; // nothing to get
213  DetId geomId = getGeometryDetId(detId);
214  const uint32_t cellIndex (m_topology.detId2denseGeomId(geomId));
215  const GlobalPoint pos = (detId != geomId) ? getPosition(detId) : GlobalPoint();
216  return cellGeomPtr (cellIndex, pos);
217 
218 }
219 
220 bool HGCalGeometry::present(const DetId& detId) const {
221  if (detId == DetId()) return false;
222  DetId geomId = getGeometryDetId(detId);
223  const uint32_t index (m_topology.detId2denseGeomId(geomId));
224  return (nullptr != getGeometryRawPtr(index)) ;
225 }
226 
228 
229  unsigned int cellIndex = indexFor(detid);
230  GlobalPoint glob;
231  unsigned int maxSize = ((mode_ == HGCalGeometryMode::Trapezoid) ?
232  m_cellVec2.size() : m_cellVec.size());
233  if (cellIndex < maxSize) {
235  std::pair<float,float> xy;
238  xy = m_topology.dddConstants().locateCellHex(id.iCell1,id.iSec1,true);
239  const HepGeom::Point3D<float> lcoord(xy.first,xy.second,0);
240  glob = m_cellVec[cellIndex].getPosition(lcoord);
241 #ifdef EDM_ML_DEBUG
242  edm::LogVerbatim("HGCalGeom") << "getPosition:: index " << cellIndex
243  << " Local " << lcoord.x() << ":"
244  << lcoord.y() << " ID " << id.iCell1
245  << ":" << id.iSec1 << " Global " << glob;
246 #endif
247  } else if (mode_ == HGCalGeometryMode::Trapezoid) {
248  const HepGeom::Point3D<float> lcoord(0,0,0);
249  glob = m_cellVec2[cellIndex].getPosition(lcoord);
250 #ifdef EDM_ML_DEBUG
251  edm::LogVerbatim("HGCalGeom") << "getPositionTrap:: index " << cellIndex
252  << " Local " << lcoord.x() << ":"
253  << lcoord.y() << " ID " << id.iLay << ":"
254  << id.iSec1 << ":" << id.iCell1
255  << " Global " << glob;
256 #endif
257  } else {
258  xy = m_topology.dddConstants().locateCell(id.iLay,id.iSec1,id.iSec2,
259  id.iCell1,id.iCell2,true,false);
260  const HepGeom::Point3D<float> lcoord(xy.first,xy.second,0);
261  glob = m_cellVec[cellIndex].getPosition(lcoord);
262 #ifdef EDM_ML_DEBUG
263  edm::LogVerbatim("HGCalGeom") << "getPositionWafer:: index " << cellIndex
264  << " Local " << lcoord.x() << ":"
265  << lcoord.y() << " ID " << id.iLay << ":"
266  << id.iSec1 << ":" << id.iSec2 << ":"
267  << id.iCell1 << ":" << id.iCell2
268  << " Global " << glob;
269 #endif
270  }
271  }
272  return glob;
273 }
274 
275 double HGCalGeometry::getArea(const DetId& detid) const {
276 
277  HGCalGeometry::CornersVec corners = getNewCorners(detid);
278  double area(0);
279  if (corners.size() > 1) {
280  int n = corners.size()-1;
281  int j = n-1;
282  for (int i=0; i<n; ++i) {
283  area += ((corners[j].x()+corners[i].x())*
284  (corners[i].y()-corners[j].y()));
285  j = i;
286  }
287  }
288  return (0.5*area);
289 }
290 
292 
293  unsigned int ncorner = ((m_det == DetId::HGCalHSc) ? FlatTrd::ncorner_ :
295  HGCalGeometry::CornersVec co (ncorner, GlobalPoint(0,0,0));
296  unsigned int cellIndex = indexFor(detid);
298  if (cellIndex < m_cellVec2.size() && m_det == DetId::HGCalHSc) {
299  GlobalPoint v = getPosition(detid);
300  std::pair<double,double> rr = m_topology.dddConstants().cellSizeTrap(id.iType,id.iSec1);
301  float dr = k_half*(rr.second-rr.first);
302  float dfi= m_cellVec2[cellIndex].param()[FlatTrd::k_Cell];
303  float dz = id.zSide*m_cellVec2[cellIndex].param()[FlatTrd::k_dZ];
304  float r = v.perp();
305  float fi = v.phi();
306  static const int signr[] = {1,1,-1,-1,1,1,-1,-1};
307  static const int signf[] = {-1,1,1,-1,-1,1,1,-1};
308  static const int signz[] = {-1,-1,-1,-1,1,1,1,1};
309  for (unsigned int i = 0; i < ncorner; ++i) {
310  co[i] = GlobalPoint((r+signr[i]*dr)*cos(fi+signf[i]*dfi),
311  (r+signr[i]*dr)*sin(fi+signf[i]*dfi),
312  (v.z()+signz[i]*dz));
313  }
314  } else if (cellIndex < m_cellVec.size() && m_det != DetId::HGCalHSc) {
315  std::pair<float,float> xy;
318  xy = m_topology.dddConstants().locateCellHex(id.iCell1,id.iSec1,true);
319  float dx = m_cellVec[cellIndex].param()[FlatHexagon::k_r];
320  float dy = k_half*m_cellVec[cellIndex].param()[FlatHexagon::k_R];
321  float dz = m_cellVec[cellIndex].param()[FlatHexagon::k_dZ];
322  static const int signx[] = {0,-1,-1,0,1,1,0,-1,-1,0,1,1};
323  static const int signy[] = {-2,-1,1,2,1,-1,-2,-1,1,2,1,-1};
324  static const int signz[] = {-1,-1,-1,-1,-1,-1,1,1,1,1,1,1};
325  for (unsigned int i = 0; i < ncorner; ++i) {
326  const HepGeom::Point3D<float> lcoord(xy.first+signx[i]*dx,xy.second+signy[i]*dy,signz[i]*dz);
327  co[i] = m_cellVec[cellIndex].getPosition(lcoord);
328  }
329  } else {
330  xy = m_topology.dddConstants().locateCell(id.iLay,id.iSec1,id.iSec2,
331  id.iCell1,id.iCell2,true,false);
332  float dx = k_fac2*m_cellVec[cellIndex].param()[FlatHexagon::k_r];
333  float dy = k_fac1*m_cellVec[cellIndex].param()[FlatHexagon::k_R];
334  float dz =-id.zSide*m_cellVec[cellIndex].param()[FlatHexagon::k_dZ];
335  static const int signx[] = {1,-1,-2,-1, 1,2,1,-1,-2,-1, 1,2};
336  static const int signy[] = {1, 1, 0,-1,-1,0,1, 1, 0,-1,-1,0};
337  static const int signz[] = {-1,-1,-1,-1,-1,-1,1,1,1,1,1,1};
338  for (unsigned int i = 0; i < ncorner; ++i) {
339  const HepGeom::Point3D<float> lcoord(xy.first+signx[i]*dx,xy.second+signy[i]*dy,signz[i]*dz);
340  co[i] = m_cellVec[cellIndex].getPosition(lcoord);
341  }
342  }
343  }
344  return co;
345 }
346 
348 
349  unsigned int ncorner = FlatTrd::ncorner_;
350  HGCalGeometry::CornersVec co (ncorner, GlobalPoint(0,0,0));
351  unsigned int cellIndex = indexFor(detid);
353  if (cellIndex < m_cellVec2.size() && m_det == DetId::HGCalHSc) {
354  GlobalPoint v = getPosition(detid);
355  std::pair<double,double> rr = m_topology.dddConstants().cellSizeTrap(id.iType,id.iSec1);
356  float dr = k_half*(rr.second-rr.first);
357  float dfi= m_cellVec2[cellIndex].param()[FlatTrd::k_Cell];
358  float dz = id.zSide*m_cellVec2[cellIndex].param()[FlatTrd::k_dZ];
359  float r = v.perp();
360  float fi = v.phi();
361  static const int signr[] = {1,1,-1,-1,1,1,-1,-1};
362  static const int signf[] = {-1,1,1,-1,-1,1,1,-1};
363  static const int signz[] = {-1,-1,-1,-1,1,1,1,1};
364  for (unsigned int i = 0; i < ncorner; ++i) {
365  co[i] = GlobalPoint((r+signr[i]*dr)*cos(fi+signf[i]*dfi),
366  (r+signr[i]*dr)*sin(fi+signf[i]*dfi),
367  (v.z()+signz[i]*dz));
368  }
369  } else if (cellIndex < m_cellVec.size() && m_det != DetId::HGCalHSc) {
370  std::pair<float,float> xy;
371  float dx(0);
374  xy = m_topology.dddConstants().locateCellHex(id.iCell1,id.iSec1,true);
375  dx = m_cellVec[cellIndex].param()[FlatHexagon::k_r];
376  } else {
377  xy = m_topology.dddConstants().locateCell(id.iLay,id.iSec1,id.iSec2,
378  id.iCell1,id.iCell2,true,false);
379  dx = k_fac2*m_cellVec[cellIndex].param()[FlatHexagon::k_r];
380  }
381  static const int signx[] = {-1,-1,1,1,-1,-1,1,1};
382  static const int signy[] = {-1,1,1,-1,-1,1,1,-1};
383  static const int signz[] = {-1,-1,-1,-1,1,1,1,1};
384  float dz = m_cellVec[cellIndex].param()[FlatHexagon::k_dZ];
385  for (unsigned int i = 0; i < ncorner; ++i) {
386  const HepGeom::Point3D<float> lcoord(xy.first+signx[i]*dx,
387  xy.second+signy[i]*dx,signz[i]*dz);
388  co[i] = m_cellVec[cellIndex].getPosition(lcoord);
389  }
390  }
391  return co;
392 }
393 
395 
396  unsigned int ncorner = (m_det == DetId::HGCalHSc) ? 5 : 7;
397  HGCalGeometry::CornersVec co (ncorner, GlobalPoint(0,0,0));
398  unsigned int cellIndex = indexFor(detid);
400  if (cellIndex < m_cellVec2.size() && m_det == DetId::HGCalHSc) {
401  GlobalPoint v = getPosition(detid);
402  std::pair<double,double> rr = m_topology.dddConstants().cellSizeTrap(id.iType,id.iSec1);
403  float dr = k_half*(rr.second-rr.first);
404  float dfi= m_cellVec2[cellIndex].param()[FlatTrd::k_Cell];
405  float dz =-id.zSide*m_cellVec2[cellIndex].param()[FlatTrd::k_dZ];
406  float r = v.perp();
407  float fi = v.phi();
408  static const int signr[] = {1,1,-1,-1};
409  static const int signf[] = {-1,1,1,-1};
410  for (unsigned int i = 0; i < ncorner-1; ++i) {
411  co[i] = GlobalPoint((r+signr[i]*dr)*cos(fi+signf[i]*dfi),
412  (r+signr[i]*dr)*sin(fi+signf[i]*dfi),
413  (v.z()+dz));
414  }
415  co[ncorner-1] = GlobalPoint(0,0,-2*dz);
416  } else if (cellIndex < m_cellVec.size() && m_det != DetId::HGCalHSc) {
417  std::pair<float,float> xy;
420  xy = m_topology.dddConstants().locateCellHex(id.iCell1,id.iSec1,true);
421  } else {
422  xy = m_topology.dddConstants().locateCell(id.iLay,id.iSec1,id.iSec2,
423  id.iCell1,id.iCell2,true,false);
424  }
425  float dx = k_fac2*m_cellVec[cellIndex].param()[FlatHexagon::k_r];
426  float dy = k_fac1*m_cellVec[cellIndex].param()[FlatHexagon::k_R];
427  float dz =-id.zSide*m_cellVec[cellIndex].param()[FlatHexagon::k_dZ];
428  static const int signx[] = {1,-1,-2,-1, 1,2};
429  static const int signy[] = {1, 1, 0,-1,-1,0};
430  for (unsigned int i = 0; i < ncorner-1; ++i) {
431  const HepGeom::Point3D<float> lcoord(xy.first+signx[i]*dx,xy.second+signy[i]*dy,dz);
432  co[i] = m_cellVec[cellIndex].getPosition(lcoord);
433  }
434  co[ncorner-1] = GlobalPoint(0,0,-2*dz);
435  }
436  return co;
437 }
438 
440  const GlobalVector& momentum) const {
441  DetId idnew;
443  int lay = ((momentum.z()*id.zSide > 0) ? (id.iLay+1) : (id.iLay-1));
444 #ifdef EDM_ML_DEBUG
445  edm::LogVerbatim("HGCalGeom") << "neighborz1:: ID " << id.iLay << ":"
446  << id.iSec1 << ":" << id.iSec2 << ":"
447  << id.iCell1 << ":" << id.iCell2
448  << " New Layer " << lay << " Range "
449  << m_topology.dddConstants().firstLayer() <<":"
451  << " pz " << momentum.z();
452 #endif
453  if ((lay >= m_topology.dddConstants().firstLayer()) &&
454  (lay <= m_topology.dddConstants().lastLayer(true)) &&
455  (momentum.z() != 0.0)) {
456  GlobalPoint v = getPosition(idin);
457  double z = id.zSide*m_topology.dddConstants().waferZ(lay,true);
458  double grad = (z - v.z())/momentum.z();
459  GlobalPoint p(v.x()+grad*momentum.x(),v.y()+grad*momentum.y(),z);
460  double r = p.perp();
461  auto rlimit = topology().dddConstants().rangeR(z,true);
462  if (r >= rlimit.first && r <= rlimit.second) idnew = getClosestCell(p);
463 #ifdef EDM_ML_DEBUG
464  edm::LogVerbatim("HGCalGeom") << "neighborz1:: Position " << v << " New Z "
465  << z << ":" << grad << " new position " << p
466  << " r-limit " << rlimit.first << ":"
467  << rlimit.second;
468 #endif
469  }
470  return idnew;
471 }
472 
474  const MagneticField* bField, int charge,
475  const GlobalVector& momentum) const {
476  DetId idnew;
478  int lay = ((momentum.z()*id.zSide > 0) ? (id.iLay+1) : (id.iLay-1));
479 #ifdef EDM_ML_DEBUG
480  edm::LogVerbatim("HGCalGeom") << "neighborz2:: ID " << id.iLay << ":"
481  << id.iSec1 << ":" << id.iSec2 << ":"
482  << id.iCell1 << ":" << id.iCell2
483  << " New Layer " << lay << " Range "
484  << m_topology.dddConstants().firstLayer() <<":"
486  << " pz " << momentum.z();
487 #endif
488  if ((lay >= m_topology.dddConstants().firstLayer()) &&
489  (lay <= m_topology.dddConstants().lastLayer(true)) &&
490  (momentum.z() != 0.0)) {
491  GlobalPoint v = getPosition(idin);
492  double z = id.zSide*m_topology.dddConstants().waferZ(lay,true);
493  FreeTrajectoryState fts (v, momentum, charge, bField);
496  AnalyticalPropagator myAP (bField, alongMomentum, 2*M_PI);
497  TrajectoryStateOnSurface tsos = myAP.propagate(fts, *nPlane);
498  GlobalPoint p;
499  auto rlimit = topology().dddConstants().rangeR(z,true);
500  if (tsos.isValid()) {
501  p = tsos.globalPosition();
502  double r = p.perp();
503  if (r >= rlimit.first && r <= rlimit.second) idnew = getClosestCell(p);
504  }
505 #ifdef EDM_ML_DEBUG
506  edm::LogVerbatim("HGCalGeom") << "neighborz2:: Position " << v << " New Z "
507  << z << ":" << charge << ":" << tsos.isValid()
508  << " new position " << p << " r limits "
509  << rlimit.first << ":" << rlimit.second;
510 #endif
511  }
512  return idnew;
513 }
514 
516  unsigned int cellIndex = getClosestCellIndex(r);
517  if ((cellIndex < m_cellVec.size() && m_det != DetId::HGCalHSc) ||
518  (cellIndex < m_cellVec2.size() && m_det == DetId::HGCalHSc)) {
520  HepGeom::Point3D<float> local;
521  if (r.z() > 0) {
522  local = HepGeom::Point3D<float>(r.x(),r.y(),0);
523  id.zSide = 1;
524  } else {
525  local = HepGeom::Point3D<float>(-r.x(),r.y(),0);
526  id.zSide =-1;
527  }
530  const auto & kxy =
531  m_topology.dddConstants().assignCell(local.x(),local.y(),id.iLay,
532  id.iType,true);
533  id.iCell1 = kxy.second;
534  id.iSec1 = kxy.first;
535  id.iType = m_topology.dddConstants().waferTypeT(kxy.first);
536  if (id.iType != 1) id.iType = -1;
537  } else if (mode_ == HGCalGeometryMode::Trapezoid) {
538  id.iLay = m_topology.dddConstants().getLayer(r.z(),true);
539  const auto & kxy =
540  m_topology.dddConstants().assignCellTrap(r.x(),r.y(),r.z(),
541  id.iLay,true);
542  id.iSec1 = kxy[0];
543  id.iCell1 = kxy[1];
544  id.iType = kxy[2];
545  } else {
546  id.iLay = m_topology.dddConstants().getLayer(r.z(),true);
547  const auto & kxy =
548  m_topology.dddConstants().assignCellHex(local.x(),local.y(),id.iLay,
549  true);
550  id.iSec1 = kxy[0]; id.iSec2 = kxy[1]; id.iType = kxy[2];
551  id.iCell1 = kxy[3]; id.iCell2 = kxy[4];
552  }
553 #ifdef EDM_ML_DEBUG
554  edm::LogVerbatim("HGCalGeom") << "getClosestCell: local " << local
555  << " Id " << id.zSide << ":" << id.iLay
556  << ":" << id.iSec1 << ":" << id.iSec2
557  << ":" << id.iType << ":" << id.iCell1
558  << ":" << id.iCell2;
559 #endif
560 
561  //check if returned cell is valid
562  if (id.iCell1>=0) return m_topology.encode(id);
563  }
564 
565  //if not valid or out of bounds return a null DetId
566  return DetId();
567 }
568 
571  return dss;
572 }
573 
575  if (m_subdet == HGCEE || m_det == DetId::HGCalEE) return "HGCalEE";
576  else if (m_subdet == HGCHEF || m_det == DetId::HGCalHSi) return "HGCalHEFront";
577  else if (m_subdet == HGCHEB || m_det == DetId::HGCalHSc) return "HGCalHEBack";
578  else return "Unknown";
579 }
580 
581 unsigned int HGCalGeometry::indexFor(const DetId& detId) const {
582  unsigned int cellIndex = ((m_det == DetId::HGCalHSc) ? m_cellVec2.size() :
583  m_cellVec.size());
584  if (detId != DetId()) {
585  DetId geomId = getGeometryDetId(detId);
586  cellIndex = m_topology.detId2denseGeomId(geomId);
587 #ifdef EDM_ML_DEBUG
588  edm::LogVerbatim("HGCalGeom") << "indexFor " << std::hex << detId.rawId()
589  << ":" << geomId.rawId() << std::dec
590  << " index " << cellIndex;
591 #endif
592  }
593  return cellIndex;
594 }
595 
596 unsigned int HGCalGeometry::sizeForDenseIndex() const {
597  return m_topology.totalGeomModules();
598 }
599 
601  // Modify the RawPtr class
602  if (m_det == DetId::HGCalHSc) {
603  if (m_cellVec2.size() < index) return nullptr;
604  const CaloCellGeometry* cell(&m_cellVec2[index]);
605  return (nullptr == cell->param() ? nullptr : cell);
606  } else {
607  if (m_cellVec2.size() < index) return nullptr;
608  const CaloCellGeometry* cell(&m_cellVec[index]);
609  return (nullptr == cell->param() ? nullptr : cell);
610  }
611 }
612 
613 std::shared_ptr<const CaloCellGeometry> HGCalGeometry::cellGeomPtr(uint32_t index) const {
614  if ((index >= m_cellVec.size() && m_det != DetId::HGCalHSc) ||
615  (index >= m_cellVec2.size() && m_det == DetId::HGCalHSc) ||
616  (m_validGeomIds[index].rawId() == 0)) return nullptr;
617  static const auto do_not_delete = [](const void*){};
618  if (m_det == DetId::HGCalHSc) {
619  auto cell = std::shared_ptr<const CaloCellGeometry>(&m_cellVec2[index],do_not_delete);
620  if (nullptr == cell->param()) return nullptr;
621  return cell;
622  } else {
623  auto cell = std::shared_ptr<const CaloCellGeometry>(&m_cellVec[index],do_not_delete);
624  if (nullptr == cell->param()) return nullptr;
625  return cell;
626  }
627 }
628 
629 std::shared_ptr<const CaloCellGeometry> HGCalGeometry::cellGeomPtr(uint32_t index, const GlobalPoint& pos) const {
630  if ((index >= m_cellVec.size() && m_det != DetId::HGCalHSc) ||
631  (index >= m_cellVec2.size() && m_det == DetId::HGCalHSc) ||
632  (m_validGeomIds[index].rawId() == 0)) return nullptr;
633  if (pos == GlobalPoint()) return cellGeomPtr(index);
634  if (m_det == DetId::HGCalHSc) {
635  auto cell = std::make_shared<FlatTrd>(m_cellVec2[index]);
636  cell->setPosition(pos);
637 #ifdef EDM_ML_DEBUG
638  edm::LogVerbatim("HGCalGeom") << "cellGeomPtr " << index << ":" << cell;
639 #endif
640  if (nullptr == cell->param()) return nullptr;
641  return cell;
642  } else {
643  auto cell = std::make_shared<FlatHexagon>(m_cellVec[index]);
644  cell->setPosition(pos);
645 #ifdef EDM_ML_DEBUG
646  edm::LogVerbatim("HGCalGeom") << "cellGeomPtr " << index << ":" << cell;
647 #endif
648  if (nullptr == cell->param()) return nullptr;
649  return cell;
650  }
651 }
652 
654  edm::LogError("HGCalGeom") << "HGCalGeometry::addValidID is not implemented";
655 }
656 
657 unsigned int HGCalGeometry::getClosestCellIndex (const GlobalPoint& r) const {
658 
660 }
661 
662 template<class T>
664  const std::vector<T>& vec) const {
665 
666  float phip = r.phi();
667  float zp = r.z();
668  float dzmin(9999), dphimin(9999), dphi10(0.175);
669  unsigned int cellIndex = vec.size();
670  for (unsigned int k=0; k<vec.size(); ++k) {
671  float dphi = phip-vec[k].phiPos();
672  while (dphi > M_PI) dphi -= 2*M_PI;
673  while (dphi <= -M_PI) dphi += 2*M_PI;
674  if (std::abs(dphi) < dphi10) {
675  float dz = std::abs(zp - vec[k].getPosition().z());
676  if (dz < (dzmin+0.001)) {
677  dzmin = dz;
678  if (std::abs(dphi) < (dphimin+0.01)) {
679  cellIndex = k;
680  dphimin = std::abs(dphi);
681  } else {
682  if (cellIndex >= vec.size()) cellIndex = k;
683  }
684  }
685  }
686  }
687 #ifdef EDM_ML_DEBUG
688  edm::LogVerbatim("HGCalGeom") << "getClosestCellIndex::Input " << zp << ":"
689  << phip << " Index " << cellIndex;
690  if (cellIndex < vec.size())
691  edm::LogVerbatim("HGCalGeom") << " Cell z "
692  << vec[cellIndex].getPosition().z()
693  << ":" << dzmin << " phi "
694  << vec[cellIndex].phiPos() << ":" << dphimin;
695 #endif
696  return cellIndex;
697 }
698 
699 
700 // FIXME: Change sorting algorithm if needed
701 namespace {
702  struct rawIdSort {
703  bool operator()( const DetId& a, const DetId& b ) {
704  return( a.rawId() < b.rawId());
705  }
706  };
707 }
708 
710  m_validIds.shrink_to_fit();
711  std::sort( m_validIds.begin(), m_validIds.end(), rawIdSort());
712 }
713 
717  CaloSubdetectorGeometry::IVec& dinsVector ) const {
718 
719  unsigned int numberOfCells = m_topology.totalGeomModules(); // total Geom Modules both sides
720  unsigned int numberOfShapes = k_NumberOfShapes;
721  unsigned int numberOfParametersPerShape =
722  ((m_det == DetId::HGCalHSc) ? (unsigned int)(k_NumberOfParametersPerTrd) :
723  (unsigned int)(k_NumberOfParametersPerHex));
724 
725  trVector.reserve( numberOfCells * numberOfTransformParms());
726  iVector.reserve( numberOfCells );
727  dimVector.reserve( numberOfShapes * numberOfParametersPerShape );
728  dinsVector.reserve( numberOfCells );
729 
730  for (unsigned itr=0; itr<m_topology.dddConstants().getTrFormN(); ++itr) {
732  int layer = mytr.lay;
733 
736  for (int wafer=0; wafer<m_topology.dddConstants().sectors(); ++wafer) {
737  if (m_topology.dddConstants().waferInLayer(wafer,layer,true)) {
738  HGCalParameters::hgtrap vol = m_topology.dddConstants().getModule(wafer, true, true);
739  ParmVec params( numberOfParametersPerShape, 0 );
740  params[FlatHexagon::k_dZ] = vol.dz;
741  params[FlatHexagon::k_r] = vol.cellSize;
743  dimVector.insert( dimVector.end(), params.begin(), params.end());
744  }
745  }
746  } else if (mode_ == HGCalGeometryMode::Trapezoid) {
747  int indx = m_topology.dddConstants().layerIndex(layer,true);
748  for (int md=m_topology.dddConstants().getParameter()->firstModule_[indx];
750  ++md) {
752  ParmVec params( numberOfParametersPerShape, 0 );
753  params[FlatTrd::k_dZ] = vol.dz;
754  params[FlatTrd::k_Theta] = params[FlatTrd::k_Phi] = 0;
755  params[FlatTrd::k_dY1] = params[FlatTrd::k_dY2] = vol.h;
756  params[FlatTrd::k_dX1] = params[FlatTrd::k_dX3] = vol.bl;
757  params[FlatTrd::k_dX2] = params[FlatTrd::k_dX4] = vol.tl;
758  params[FlatTrd::k_Alp1] = params[FlatTrd::k_Alp2]= vol.alpha;
759  params[FlatTrd::k_Cell] = vol.cellSize;
760  dimVector.insert( dimVector.end(), params.begin(), params.end());
761  }
762  } else {
763  for (int wafer=0; wafer<m_topology.dddConstants().sectors(); ++wafer) {
764  if (m_topology.dddConstants().waferInLayer(wafer,layer,true)) {
765  HGCalParameters::hgtrap vol = m_topology.dddConstants().getModule(wafer, true, true);
766  ParmVec params( numberOfParametersPerShape, 0 );
767  params[FlatHexagon::k_dZ] = vol.dz;
768  params[FlatHexagon::k_r] = vol.cellSize;
770  dimVector.insert( dimVector.end(), params.begin(), params.end());
771  }
772  }
773  }
774  }
775 
776  for (unsigned int i( 0 ); i < numberOfCells; ++i) {
777  DetId detId = m_validGeomIds[i];
778  int layer(0);
781  layer = HGCalDetId(detId).layer();
782  } else if (mode_ == HGCalGeometryMode::Trapezoid) {
783  layer = HGCScintillatorDetId(detId).layer();
784  } else if (m_topology.isHFNose()) {
785  layer = HFNoseDetId(detId).layer();
786  } else {
787  layer = HGCSiliconDetId(detId).layer();
788  }
789  dinsVector.emplace_back(m_topology.detId2denseGeomId( detId ));
790  iVector.emplace_back( layer );
791 
792  Tr3D tr;
793  auto ptr = cellGeomPtr( i );
794  if ( nullptr != ptr ) {
795  ptr->getTransform( tr, ( Pt3DVec* ) nullptr );
796 
797  if( Tr3D() == tr ) { // there is no rotation
798  const GlobalPoint& gp( ptr->getPosition());
799  tr = HepGeom::Translate3D( gp.x(), gp.y(), gp.z());
800  }
801 
802  const CLHEP::Hep3Vector tt( tr.getTranslation());
803  trVector.emplace_back( tt.x());
804  trVector.emplace_back( tt.y());
805  trVector.emplace_back( tt.z());
806  if (6 == numberOfTransformParms()) {
807  const CLHEP::HepRotation rr( tr.getRotation());
808  const ROOT::Math::Transform3D rtr( rr.xx(), rr.xy(), rr.xz(), tt.x(),
809  rr.yx(), rr.yy(), rr.yz(), tt.y(),
810  rr.zx(), rr.zy(), rr.zz(), tt.z());
812  rtr.GetRotation( ea );
813  trVector.emplace_back( ea.Phi());
814  trVector.emplace_back( ea.Theta());
815  trVector.emplace_back( ea.Psi());
816  }
817  }
818  }
819 }
820 
822  DetId geomId;
823  if ((mode_ == HGCalGeometryMode::Hexagon) ||
825  geomId = static_cast<DetId>(HGCalDetId(detId).geometryCell());
826  } else if (mode_ == HGCalGeometryMode::Trapezoid) {
827  geomId = static_cast<DetId>(HGCScintillatorDetId(detId).geometryCell());
828  } else if (m_topology.isHFNose()) {
829  geomId = static_cast<DetId>(HFNoseDetId(detId).geometryCell());
830  } else {
831  geomId = static_cast<DetId>(HGCSiliconDetId(detId).geometryCell());
832  }
833  return geomId;
834 }
835 
837 
std::vector< DetId > m_validGeomIds
std::pair< double, double > cellSizeTrap(int type, int irad) const
HGCalGeometryMode::GeometryMode mode_
A base class to handle the particular shape of HGCal volumes.
Definition: FlatTrd.h:19
int getLayer(double z, bool reco) const
std::vector< CCGFloat > DimVec
T perp() const
Definition: PV3DBase.h:72
std::vector< FlatHexagon > CellVec
Definition: HGCalGeometry.h:34
virtual unsigned int numberOfParametersPerShape() const
std::string cellElement() const
std::shared_ptr< const CaloCellGeometry > cellGeomPtr(uint32_t index) const override
unsigned int sizeForDenseIndex() const
HGCalParameters::hgtrform getTrForm(unsigned int k) const
int lastLayer(bool reco) const
unsigned int getClosestCellIndex(const GlobalPoint &r) const
static double k_fac1
DetId neighborZ(const DetId &idin, const GlobalVector &p) const
const HGCalParameters * getParameter() const
const HGCalTopology & m_topology
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
~HGCalGeometry() override
bool cellInLayer(int waferU, int waferV, int cellU, int cellV, int lay, bool reco) const
Geom::Phi< T > phi() const
Definition: PV3DBase.h:69
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:50
std::vector< unsigned int > IVec
T y() const
Definition: PV3DBase.h:63
static constexpr uint32_t k_Theta
Definition: FlatTrd.h:28
GlobalPoint globalPosition() const
void newCell(const GlobalPoint &f1, const GlobalPoint &f2, const GlobalPoint &f3, const CCGFloat *parm, const DetId &detId) override
std::vector< float > ParmVec
std::vector< GlobalPoint > CornersVec
Definition: HGCalGeometry.h:41
std::vector< CCGFloat > TrVec
GlobalPoint getPosition(const DetId &id) const
HepGeom::Transform3D Tr3D
double getArea(const DetId &detid) const
Returns area of a cell.
HFNoseDetId geometryCell() const
Definition: HFNoseDetId.h:49
static constexpr uint32_t k_Cell
Definition: FlatTrd.h:46
static constexpr uint32_t k_dY1
Definition: FlatTrd.h:32
std::pair< float, float > locateCell(int cell, int lay, int type, bool reco) const
CaloCellGeometry::CCGFloat CCGFloat
CornersVec getCorners(const DetId &id) const
Returns the corner points of this cell&#39;s volume.
CaloCellGeometry::Tr3D Tr3D
static void localCorners(Pt3DVec &vec, const CCGFloat *pv, Pt3D &ref)
Definition: FlatHexagon.cc:175
int layerIndex(int lay, bool reco) const
static constexpr unsigned int ncorner_
Definition: FlatHexagon.h:82
CaloCellGeometry::Pt3DVec Pt3DVec
Definition: HGCalGeometry.h:38
std::pair< float, float > locateCellHex(int cell, int wafer, bool reco) const
const double twoBysqrt3_
std::pair< double, double > rangeR(double z, bool reco) const
unsigned int totalGeomModules() const
HGCalGeometry(const HGCalTopology &topology)
static constexpr unsigned int ncorner_
Definition: FlatTrd.h:98
std::vector< int > firstModule_
static unsigned int k_NumberOfShapes
Definition: HGCalGeometry.h:49
HGCalDetId geometryCell() const
Definition: HGCalDetId.h:36
static constexpr uint32_t k_R
Definition: FlatHexagon.h:30
void initializeParms() override
unsigned int getTrFormN() const
HGCSiliconDetId geometryCell() const
const CCGFloat * param() const
static unsigned int k_NumberOfParametersPerHex
Definition: HGCalGeometry.h:47
virtual uint32_t detId2denseGeomId(const DetId &id) const
int numberCellsHexagon(int wafer) const
static constexpr uint32_t k_r
Definition: FlatHexagon.h:29
int layer() const
get the layer #
Definition: HFNoseDetId.h:58
virtual unsigned int numberOfShapes() const
std::vector< float > ParmVec
DetId encode(const DecodedDetId &id_) const
T sqrt(T t)
Definition: SSEVec.h:18
int layer() const
get the layer #
static constexpr uint32_t k_dX1
Definition: FlatTrd.h:33
std::vector< DetId > m_validIds
static PlanePointer build(Args &&...args)
Definition: Plane.h:33
T z() const
Definition: PV3DBase.h:64
const HGCalTopology & topology() const
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
A base class to handle the hexagonal shape of HGCal silicon volumes.
Definition: FlatHexagon.h:20
def pv(vc)
Definition: MetAnalyzer.py:7
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::set< DetId > DetIdSet
Definition: HGCalGeometry.h:40
static constexpr uint32_t k_dY2
Definition: FlatTrd.h:39
HGCScintillatorDetId geometryCell() const
CornersVec get8Corners(const DetId &id) const
CellVec2 m_cellVec2
static constexpr uint32_t k_dX4
Definition: FlatTrd.h:42
static unsigned int k_NumberOfParametersPerTrd
Definition: HGCalGeometry.h:46
static void localCorners(Pt3DVec &vec, const CCGFloat *pv, Pt3D &ref)
Definition: FlatTrd.cc:175
DetIdSet getCells(const GlobalPoint &r, double dR) const override
Get a list of all cells within a dR of the given cell.
double waferZ(int layer, bool reco) const
void localCorners(Pt3DVec &lc, const CCGFloat *pv, unsigned int i, Pt3D &ref)
#define M_PI
bool isHFNose() const
int k[5][pyjets_maxn]
#define TYPELOOKUP_DATA_REG(_dataclass_)
Definition: typelookup.h:102
static double k_fac2
DecodedDetId decode(const DetId &id) const
Definition: DetId.h:18
std::vector< int > lastModule_
AlgebraicVector EulerAngles
Definition: Definitions.h:36
ForwardSubdetector m_subdet
static constexpr uint32_t k_dZ
Definition: FlatHexagon.h:28
std::vector< FlatTrd > CellVec2
Definition: HGCalGeometry.h:35
const HGCalDDDConstants & dddConstants() const
DetId getClosestCell(const GlobalPoint &r) const override
static constexpr uint32_t k_Phi
Definition: FlatTrd.h:30
CaloCellGeometry::CornersMgr * cornersMgr()
CornersVec getNewCorners(const DetId &id) const
void getSummary(CaloSubdetectorGeometry::TrVec &trVector, CaloSubdetectorGeometry::IVec &iVector, CaloSubdetectorGeometry::DimVec &dimVector, CaloSubdetectorGeometry::IVec &dinsVector) const override
double b
Definition: hdecay.h:120
TrajectoryStateOnSurface propagate(STA const &state, SUR const &surface) const
Definition: Propagator.h:53
unsigned int totalModules() const
Definition: HGCalTopology.h:99
std::shared_ptr< const CaloCellGeometry > getGeometry(const DetId &id) const override
Get the cell geometry of a given detector id. Should return false if not found.
CaloCellGeometry::Tr3D Tr3D
int layer() const
get the layer #
CaloCellGeometry::Pt3D Pt3D
Definition: HGCalGeometry.h:37
std::array< int, 5 > assignCellHex(float x, float y, int lay, bool reco) const
double a
Definition: hdecay.h:121
static constexpr uint32_t k_dZ
Definition: FlatTrd.h:27
virtual void fillNamedParams(DDFilteredView fv)
static double k_half
std::pair< int, int > assignCell(float x, float y, int lay, int subSec, bool reco) const
DetId::Detector m_det
std::array< int, 3 > assignCellTrap(float x, float y, float z, int lay, bool reco) const
const CaloCellGeometry * getGeometryRawPtr(uint32_t index) const override
int firstLayer() const
static constexpr uint32_t k_Alp1
Definition: FlatTrd.h:37
void addValidID(const DetId &id)
bool valid(const DetId &id) const override
Is this a valid cell id.
virtual unsigned int numberOfTransformParms() const
static constexpr uint32_t k_dX3
Definition: FlatTrd.h:40
HGCalParameters::hgtrap getModule(unsigned int k, bool hexType, bool reco) const
T x() const
Definition: PV3DBase.h:62
int waferTypeT(int wafer) const
unsigned int indexFor(const DetId &id) const override
bool present(const DetId &id) const override
is this detid present in the geometry?
int layer() const
get the layer #
Definition: HGCalDetId.h:48
static constexpr uint32_t k_dX2
Definition: FlatTrd.h:35
CellVec m_cellVec
bool waferInLayer(int wafer, int lay, bool reco) const
DetId getGeometryDetId(DetId detId) const
static constexpr uint32_t k_Alp2
Definition: FlatTrd.h:44