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