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  */
12 
13 #include <cmath>
14 
15 #include <Math/Transform3D.h>
16 #include <Math/EulerAngles.h>
17 
19 typedef std::vector<float> ParmVec;
20 
21 //#define EDM_ML_DEBUG
22 
24  : m_topology( topology_ ),
25  m_cellVec( topology_.totalGeomModules()),
26  m_validGeomIds( topology_.totalGeomModules()),
27  m_halfType( topology_.detectorType()),
28  m_subdet( topology_.subDetector()) {
29 
30  m_validIds.reserve( topology().totalModules());
31 #ifdef EDM_ML_DEBUG
32  std::cout << "Expected total # of Geometry Modules "
33  << topology().totalGeomModules() << std::endl;
34 #endif
35 }
36 
38 
40 
42 }
43 
45  const CCGFloat* pv,
46  unsigned int i,
47  Pt3D& ref) {
48  FlatTrd::localCorners( lc, pv, ref ) ;
49 }
50 
52  const GlobalPoint& f2 ,
53  const GlobalPoint& f3 ,
54  const CCGFloat* parm ,
55  const DetId& detId ) {
56 
57  DetId geomId;
58  int cells;
60  if ((topology().dddConstants().geomMode() == HGCalGeometryMode::Hexagon) ||
61  (topology().dddConstants().geomMode() == HGCalGeometryMode::HexagonFull)) {
62  geomId = (DetId)(HGCalDetId(detId).geometryCell());
63  cells = topology().dddConstants().numberCellsHexagon(id.iSec);
64 #ifdef EDM_ML_DEBUG
65  std::cout << "NewCell " << HGCalDetId(detId) << " GEOM "
66  << HGCalDetId(geomId) << std::endl;
67 #endif
68  } else {
69  return;
70  }
71  const uint32_t cellIndex (topology().detId2denseGeomId(detId));
72 
73  m_cellVec.at( cellIndex ) = FlatTrd( cornersMgr(), f1, f2, f3, parm ) ;
74  m_validGeomIds.at( cellIndex ) = geomId ;
75 
76 #ifdef EDM_ML_DEBUG
77  std::cout << "Store for DetId " << std::hex << detId.rawId() << " GeomId "
78  << geomId.rawId() << std::dec << " Index " << cellIndex
79  << " cells " << cells << std::endl;
80  unsigned int nOld = m_validIds.size();
81 #endif
82  for (int cell = 0; cell < cells; ++cell) {
83  id.iCell = cell;
84  DetId idc = topology().encode(id);
85  if (topology().valid(idc)) {
86  m_validIds.emplace_back(idc);
87  if ((topology().dddConstants().geomMode() == HGCalGeometryMode::Square) &&
88  (!m_halfType)) {
89  id.iSubSec = -id.iSubSec;
90  m_validIds.emplace_back( topology().encode(id));
91  id.iSubSec = -id.iSubSec;
92  }
93  }
94  }
95 
96 #ifdef EDM_ML_DEBUG
97  std::cout << "HGCalGeometry::newCell-> [" << cellIndex << "]"
98  << " front:" << f1.x() << '/' << f1.y() << '/' << f1.z()
99  << " back:" << f2.x() << '/' << f2.y() << '/' << f2.z()
100  << " eta|phi " << m_cellVec[cellIndex].etaPos() << ":"
101  << m_cellVec[cellIndex].phiPos() << " id:";
102  if ((topology().dddConstants().geomMode() == HGCalGeometryMode::Hexagon) ||
103  (topology().dddConstants().geomMode() == HGCalGeometryMode::HexagonFull)) {
104  std::cout << HGCalDetId(detId);
105  } else {
106  std::cout << std::hex << detId.rawId() << std::dec;
107  }
108  unsigned int nNew = m_validIds.size();
109  std::cout << " with valid DetId from " << nOld << " to " << nNew
110  << std::endl;
111  std::cout << "Cell[" << cellIndex << "] " << std::hex << geomId.rawId()
112  << ":" << m_validGeomIds[cellIndex].rawId() << std::dec
113  << std::endl;
114 #endif
115 }
116 
117 std::shared_ptr<const CaloCellGeometry> HGCalGeometry::getGeometry(const DetId& id) const {
118  if (id == DetId()) return nullptr; // nothing to get
119  DetId geoId;
120  if ((topology().dddConstants().geomMode() == HGCalGeometryMode::Hexagon) ||
121  (topology().dddConstants().geomMode() == HGCalGeometryMode::HexagonFull)) {
122  geoId = (DetId)(HGCalDetId(id).geometryCell());
123  } else {
124  return nullptr;
125  }
126  const uint32_t cellIndex (topology().detId2denseGeomId(geoId));
127  const GlobalPoint pos = (id != geoId) ? getPosition(id) : GlobalPoint();
128  return cellGeomPtr (cellIndex, pos);
129 
130 }
131 
132 bool HGCalGeometry::present(const DetId& id) const {
133  if (id == DetId()) return false;
134  DetId geoId;
135  if ((topology().dddConstants().geomMode() == HGCalGeometryMode::Hexagon) ||
136  (topology().dddConstants().geomMode() == HGCalGeometryMode::HexagonFull)) {
137  geoId = (DetId)(HGCalDetId(id).geometryCell());
138  } else {
139  return false;
140  }
141  const uint32_t index (topology().detId2denseGeomId(geoId));
142  return (nullptr != getGeometryRawPtr(index)) ;
143 }
144 
146 
147  unsigned int cellIndex = indexFor(id);
148  GlobalPoint glob;
149  if (cellIndex < m_cellVec.size()) {
151  std::pair<float,float> xy;
152  if ((topology().dddConstants().geomMode() == HGCalGeometryMode::Hexagon) ||
153  (topology().dddConstants().geomMode() == HGCalGeometryMode::HexagonFull)) {
154  xy = topology().dddConstants().locateCellHex(id_.iCell,id_.iSec,true);
155  } else {
156  xy = std::pair<float,float>(0,0);
157  }
158  const HepGeom::Point3D<float> lcoord(xy.first,xy.second,0);
159  glob = m_cellVec[cellIndex].getPosition(lcoord);
160 #ifdef EDM_ML_DEBUG
161  std::cout << "getPosition:: index " << cellIndex << " Local " << lcoord.x()
162  << ":" << lcoord.y() << " ID " << id_.iCell << ":" << id_.iLay
163  << " Global " << glob << std::endl;
164 #endif
165  }
166  return glob;
167 }
168 
170 
171  HGCalGeometry::CornersVec co (8, GlobalPoint(0,0,0));
172  unsigned int cellIndex = indexFor(id);
173  if (cellIndex < m_cellVec.size()) {
175  std::pair<float,float> xy;
176  if ((topology().dddConstants().geomMode() == HGCalGeometryMode::Hexagon) ||
177  (topology().dddConstants().geomMode() == HGCalGeometryMode::HexagonFull)) {
178  xy = topology().dddConstants().locateCellHex(id_.iCell,id_.iSec,true);
179  } else {
180  xy = std::pair<float,float>(0,0);
181  }
182  float dz = m_cellVec[cellIndex].param()[0];
183  float dx = 0.5*m_cellVec[cellIndex].param()[11];
184  static const int signx[] = {-1,-1,1,1,-1,-1,1,1};
185  static const int signy[] = {-1,1,1,-1,-1,1,1,-1};
186  static const int signz[] = {-1,-1,-1,-1,1,1,1,1};
187  for (unsigned int i = 0; i != 8; ++i) {
188  const HepGeom::Point3D<float> lcoord(xy.first+signx[i]*dx,xy.second+signy[i]*dx,signz[i]*dz);
189  co[i] = m_cellVec[cellIndex].getPosition(lcoord);
190  }
191  }
192  return co;
193 }
194 
196  unsigned int cellIndex = getClosestCellIndex(r);
197  if (cellIndex < m_cellVec.size()) {
199  HepGeom::Point3D<float> local;
200  if ((topology().dddConstants().geomMode() == HGCalGeometryMode::Hexagon) ||
201  (topology().dddConstants().geomMode() == HGCalGeometryMode::HexagonFull)) {
202  if (r.z() > 0) local = HepGeom::Point3D<float>(r.x(),r.y(),0);
203  else local = HepGeom::Point3D<float>(-r.x(),r.y(),0);
204  std::pair<int,int> kxy =
205  topology().dddConstants().assignCell(local.x(),local.y(),id_.iLay,
206  id_.iSubSec,true);
207  id_.iCell = kxy.second;
208  id_.iSec = kxy.first;
209  id_.iSubSec = topology().dddConstants().waferTypeT(kxy.first);
210  if (id_.iSubSec != 1) id_.iSubSec = -1;
211 #ifdef EDM_ML_DEBUG
212  std::cout << "getClosestCell: local " << local << " Id " << id_.zside
213  << ":" << id_.iLay << ":" << id_.iSec << ":" << id_.iSubSec
214  << ":" << id_.iCell << std::endl;
215 #endif
216 
217  //check if returned cell is valid
218  if (id_.iCell>=0) return topology().encode(id_);
219  }
220  }
221 
222  //if not valid or out of bounds return a null DetId
223  return DetId();
224 }
225 
228  return dss;
229 }
230 
232  if (m_subdet == HGCEE) return "HGCalEE";
233  else if (m_subdet == HGCHEF) return "HGCalHEFront";
234  else if (m_subdet == HGCHEB) return "HGCalHEBack";
235  else return "Unknown";
236 }
237 
238 unsigned int HGCalGeometry::indexFor(const DetId& id) const {
239  unsigned int cellIndex = m_cellVec.size();
240  if (id != DetId()) {
241  DetId geoId;
242  if ((topology().dddConstants().geomMode() == HGCalGeometryMode::Hexagon) ||
243  (topology().dddConstants().geomMode() == HGCalGeometryMode::HexagonFull)) {
244  geoId = (DetId)(HGCalDetId(id).geometryCell());
245  cellIndex = topology().detId2denseGeomId(geoId);
246 #ifdef EDM_ML_DEBUG
247  std::cout << "indexFor " << std::hex << id.rawId() << ":"
248  << geoId.rawId() << std::dec << " index " << cellIndex
249  << std::endl;
250 #endif
251  }
252  }
253  return cellIndex;
254 }
255 
256 unsigned int HGCalGeometry::sizeForDenseIndex() const {
257  return topology().totalGeomModules();
258 }
259 
261  // Modify the RawPtr class
262  const CaloCellGeometry* cell(&m_cellVec[index]);
263  return (m_cellVec.size() < index ||
264  nullptr == cell->param() ? nullptr : cell);
265 }
266 
267 std::shared_ptr<const CaloCellGeometry> HGCalGeometry::cellGeomPtr(uint32_t index) const {
268  if ((index >= m_cellVec.size()) || (m_validGeomIds[index].rawId() == 0))
269  return nullptr;
270  static const auto do_not_delete = [](const void*){};
271  auto cell = std::shared_ptr<const CaloCellGeometry>(&m_cellVec[index],do_not_delete);
272  if (nullptr == cell->param()) return nullptr;
273  return cell;
274 }
275 
276 std::shared_ptr<const CaloCellGeometry> HGCalGeometry::cellGeomPtr(uint32_t index, const GlobalPoint& pos) const {
277  if ((index >= m_cellVec.size()) || (m_validGeomIds[index].rawId() == 0))
278  return nullptr;
279  if (pos == GlobalPoint()) return cellGeomPtr(index);
280  auto cell = std::make_shared<FlatTrd>(m_cellVec[index]);
281  cell->setPosition(pos);
282 #ifdef EDM_ML_DEBUG
283 //std::cout << "cellGeomPtr " << newcell << ":" << cell << std::endl;
284 #endif
285  if (nullptr == cell->param()) return nullptr;
286  return cell;
287 }
288 
290  edm::LogError("HGCalGeom") << "HGCalGeometry::addValidID is not implemented";
291 }
292 
293 
294 unsigned int HGCalGeometry::getClosestCellIndex (const GlobalPoint& r) const {
295 
296  float phip = r.phi();
297  float zp = r.z();
298  unsigned int cellIndex = m_cellVec.size();
299  float dzmin(9999), dphimin(9999), dphi10(0.175);
300  for (unsigned int k=0; k<m_cellVec.size(); ++k) {
301  float dphi = phip-m_cellVec[k].phiPos();
302  while (dphi > M_PI) dphi -= 2*M_PI;
303  while (dphi <= -M_PI) dphi += 2*M_PI;
304  if (fabs(dphi) < dphi10) {
305  float dz = fabs(zp - m_cellVec[k].getPosition().z());
306  if (dz < (dzmin+0.001)) {
307  dzmin = dz;
308  if (fabs(dphi) < (dphimin+0.01)) {
309  cellIndex = k;
310  dphimin = fabs(dphi);
311  } else {
312  if (cellIndex >= m_cellVec.size()) cellIndex = k;
313  }
314  }
315  }
316  }
317 #ifdef EDM_ML_DEBUG
318  std::cout << "getClosestCellIndex::Input " << zp << ":" << phip << " Index "
319  << cellIndex;
320  if (cellIndex < m_cellVec.size())
321  std::cout << " Cell z " << m_cellVec[cellIndex].getPosition().z() << ":"
322  << dzmin << " phi " << m_cellVec[cellIndex].phiPos() << ":"
323  << dphimin;
324  std::cout << std::endl;
325 
326 #endif
327  return cellIndex;
328 }
329 
330 // FIXME: Change sorting algorithm if needed
331 namespace {
332  struct rawIdSort {
333  bool operator()( const DetId& a, const DetId& b ) {
334  return( a.rawId() < b.rawId());
335  }
336  };
337 }
338 
340  m_validIds.shrink_to_fit();
341  std::sort( m_validIds.begin(), m_validIds.end(), rawIdSort());
342 }
343 
347  CaloSubdetectorGeometry::IVec& dinsVector ) const {
348 
349  unsigned int numberOfCells = topology().totalGeomModules(); // total Geom Modules both sides
352 
353  trVector.reserve( numberOfCells * numberOfTransformParms());
354  iVector.reserve( numberOfCells );
355  dimVector.reserve( numberOfShapes * numberOfParametersPerShape );
356  dinsVector.reserve( numberOfCells );
357 
358  if ((topology().dddConstants().geomMode() == HGCalGeometryMode::Hexagon) ||
359  (topology().dddConstants().geomMode() == HGCalGeometryMode::HexagonFull)) {
360  for (unsigned itr=0; itr<topology().dddConstants().getTrFormN(); ++itr) {
362  int layer = mytr.lay;
363  for (int wafer=0; wafer<topology().dddConstants().sectors(); ++wafer) {
364  if (topology().dddConstants().waferInLayer(wafer,layer,true)) {
365  HGCalParameters::hgtrap vol = topology().dddConstants().getModule(wafer, true, true);
367  params[0] = vol.dz;
368  params[1] = params[2] = 0;
369  params[3] = params[7] = vol.h;
370  params[4] = params[8] = vol.bl;
371  params[5] = params[9] = vol.tl;
372  params[6] = params[10]= vol.alpha;
373  params[11]= vol.cellSize;
374  dimVector.insert( dimVector.end(), params.begin(), params.end());
375  }
376  }
377  }
378  }
379 
380  for (unsigned int i( 0 ); i < numberOfCells; ++i) {
381  DetId detId = m_validGeomIds[i];
382  int layer = HGCalDetId(detId).layer();
383  dinsVector.emplace_back( topology().detId2denseGeomId( detId ));
384  iVector.emplace_back( layer );
385 
386  Tr3D tr;
387  auto ptr = cellGeomPtr( i );
388  if ( nullptr != ptr ) {
389  ptr->getTransform( tr, ( Pt3DVec* ) nullptr );
390 
391  if( Tr3D() == tr ) { // there is no rotation
392  const GlobalPoint& gp( ptr->getPosition());
393  tr = HepGeom::Translate3D( gp.x(), gp.y(), gp.z());
394  }
395 
396  const CLHEP::Hep3Vector tt( tr.getTranslation());
397  trVector.emplace_back( tt.x());
398  trVector.emplace_back( tt.y());
399  trVector.emplace_back( tt.z());
400  if (6 == numberOfTransformParms()) {
401  const CLHEP::HepRotation rr( tr.getRotation());
402  const ROOT::Math::Transform3D rtr( rr.xx(), rr.xy(), rr.xz(), tt.x(),
403  rr.yx(), rr.yy(), rr.yz(), tt.y(),
404  rr.zx(), rr.zy(), rr.zz(), tt.z());
406  rtr.GetRotation( ea );
407  trVector.emplace_back( ea.Phi());
408  trVector.emplace_back( ea.Theta());
409  trVector.emplace_back( ea.Psi());
410  }
411  }
412  }
413 }
414 
416 
std::vector< DetId > m_validGeomIds
A base class to handle the particular shape of HGCal volumes.
Definition: FlatTrd.h:19
std::vector< CCGFloat > DimVec
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
unsigned int getClosestCellIndex(const GlobalPoint &r) const
~HGCalGeometry() override
Geom::Phi< T > phi() const
Definition: PV3DBase.h:69
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
std::vector< unsigned int > IVec
T y() const
Definition: PV3DBase.h:63
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:38
std::vector< CCGFloat > TrVec
GlobalPoint getPosition(const DetId &id) const
HepGeom::Transform3D Tr3D
CaloCellGeometry::CCGFloat CCGFloat
CornersVec getCorners(const DetId &id) const
Returns the corner points of this cell&#39;s volume.
CaloCellGeometry::Tr3D Tr3D
CaloCellGeometry::Pt3DVec Pt3DVec
Definition: HGCalGeometry.h:35
std::pair< float, float > locateCellHex(int cell, int wafer, bool reco) const
uint32_t rawId() const
get the raw id
Definition: DetId.h:44
unsigned int totalGeomModules() const
Definition: HGCalTopology.h:98
HGCalGeometry(const HGCalTopology &topology)
HGCalDetId geometryCell() const
Definition: HGCalDetId.h:36
void initializeParms() override
unsigned int getTrFormN() const
const CCGFloat * param() const
virtual uint32_t detId2denseGeomId(const DetId &id) const
int numberCellsHexagon(int wafer) const
virtual unsigned int numberOfShapes() const
std::vector< float > ParmVec
DetId encode(const DecodedDetId &id_) const
std::vector< DetId > m_validIds
T z() const
Definition: PV3DBase.h:64
const HGCalTopology & topology() const
Definition: HGCalGeometry.h:99
def pv(vc)
Definition: MetAnalyzer.py:6
std::set< DetId > DetIdSet
Definition: HGCalGeometry.h:37
int sectors() const
static void localCorners(Pt3DVec &vec, const CCGFloat *pv, Pt3D &ref)
Definition: FlatTrd.cc:162
DetIdSet getCells(const GlobalPoint &r, double dR) const override
Get a list of all cells within a dR of the given cell.
void localCorners(Pt3DVec &lc, const CCGFloat *pv, unsigned int i, Pt3D &ref)
#define M_PI
int k[5][pyjets_maxn]
#define TYPELOOKUP_DATA_REG(_dataclass_)
Definition: typelookup.h:96
DecodedDetId decode(const DetId &id) const
Definition: DetId.h:18
AlgebraicVector EulerAngles
Definition: Definitions.h:36
ForwardSubdetector m_subdet
const HGCalDDDConstants & dddConstants() const
DetId getClosestCell(const GlobalPoint &r) const override
CaloCellGeometry::CornersMgr * cornersMgr()
void getSummary(CaloSubdetectorGeometry::TrVec &trVector, CaloSubdetectorGeometry::IVec &iVector, CaloSubdetectorGeometry::DimVec &dimVector, CaloSubdetectorGeometry::IVec &dinsVector) const override
double b
Definition: hdecay.h:120
def encode(args, files)
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
CaloCellGeometry::Pt3D Pt3D
Definition: HGCalGeometry.h:34
double a
Definition: hdecay.h:121
virtual void fillNamedParams(DDFilteredView fv)
std::pair< int, int > assignCell(float x, float y, int lay, int subSec, bool reco) const
const CaloCellGeometry * getGeometryRawPtr(uint32_t index) const override
void addValidID(const DetId &id)
virtual unsigned int numberOfTransformParms() const
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
CellVec m_cellVec
bool waferInLayer(int wafer, int lay, bool reco) const