CMS 3D CMS Logo

FWGeometry.cc
Go to the documentation of this file.
1 #include "TFile.h"
2 #include "TTree.h"
3 #include "TEveGeoNode.h"
4 #include "TEveGeoShape.h"
5 #include "TPRegexp.h"
6 #include "TSystem.h"
7 #include "TGeoArb8.h"
8 #include "TObjArray.h"
9 #include "TObjString.h"
10 #include "TPRegexp.h"
11 
15 
16 // AMT deprication of tracker specific DetIds
18 
19 #include <cassert>
20 #include <iostream>
21 #include <memory>
22 
23 #include <sstream>
24 #include <stdexcept>
25 #include <algorithm>
26 
27 FWGeometry::FWGeometry(void) : m_producerVersion(0) {}
28 
30 
31 bool FWGeometry::isEmpty() const {
32  // AMT this is a check if geomtery is not loaded
33  // e.g. cmsShow starts with no data file and without given explicit argument ( --geometry-file option )
34 
35  return m_idToInfo.empty();
36 }
37 
38 TFile* FWGeometry::findFile(const char* fileName) {
39  std::string searchPath = ".";
40 
41  if (gSystem->Getenv("CMSSW_SEARCH_PATH")) {
42  TString paths = gSystem->Getenv("CMSSW_SEARCH_PATH");
43 
44  TObjArray* tokens = paths.Tokenize(":");
45  for (int i = 0; i < tokens->GetEntries(); ++i) {
46  TObjString* path = (TObjString*)tokens->At(i);
47  searchPath += ":";
48  searchPath += static_cast<const char*>(path->GetString());
49  if (gSystem->Getenv("CMSSW_VERSION"))
50  searchPath += "/Fireworks/Geometry/data/";
51  }
52  }
53 
54  TString fn = fileName;
55  const char* fp = gSystem->FindFile(searchPath.c_str(), fn, kFileExists);
56  return fp ? TFile::Open(fp) : nullptr;
57 }
58 
60  const std::string fnRun2 = "cmsGeomRun2.root";
61  const std::string fnRun3 = "cmsGeom2021.root";
62  const std::string fnSLHC = "cmsGeom2026.root";
63 
64  TPMERegexp year_re("^[^_]+_[a-zA-Z]*20(\\d\\d)_");
65  TPMERegexp run_re("^[^_]+_[a-zA-Z]*Run(\\d)_");
66 
67  TString test = globalTag.c_str();
68  std::string cfn;
69  if (year_re.Match(test)) {
70  TString r = year_re[1];
71  int year = atoi(r.Data());
72  if (year < 18) {
73  cfn = fnRun2;
74  } else if (year < 21) {
75  cfn = fnRun3;
76  } else {
77  cfn = fnSLHC;
78  }
79  } else if (run_re.Match(test)) {
80  TString rn = run_re[1];
81  if (rn == "1") {
82  fwLog(fwlog::kWarning) << "Run1 geometry not included. Using Run2 geometry." << std::endl;
83  cfn = fnRun2;
84  } else if (rn == "2") {
85  cfn = fnRun2;
86  } else if (rn == "4") {
87  cfn = fnSLHC;
88  } else {
89  fwLog(fwlog::kWarning) << "Detected Run" << rn << ". Using geometry scenario 2021.\n";
90  cfn = fnRun3;
91  }
92  } else {
93  fwLog(fwlog::kWarning) << "Could not guess geometry from global tag. Using geometry scenario 2021.\n";
94  cfn = fnRun3;
95  }
96 
97  fwLog(fwlog::kInfo) << "Guessed geometry " << cfn << " from global tag " << globalTag << std::endl;
98  if (cfn.compare(m_fileName)) {
99  loadMap(cfn.c_str());
100  }
101 }
102 
103 void FWGeometry::loadMap(const char* iFileName) {
104  TFile* file = findFile(iFileName);
105  if (!file) {
106  throw std::runtime_error("ERROR: failed to find geometry file. Initialization failed.");
107  return;
108  }
109  m_fileName = iFileName;
110  TTree* tree = static_cast<TTree*>(file->Get("idToGeo"));
111  if (!tree) {
112  throw std::runtime_error("ERROR: cannot find detector id map in the file. Initialization failed.");
113  return;
114  }
115 
116  unsigned int id;
117  Float_t points[24];
118  Float_t topology[9];
119  Float_t shape[5];
120  Float_t translation[3];
121  Float_t matrix[9];
122  bool loadPoints = tree->GetBranch("points") != nullptr;
123  bool loadParameters = tree->GetBranch("topology") != nullptr;
124  bool loadShape = tree->GetBranch("shape") != nullptr;
125  bool loadTranslation = tree->GetBranch("translation") != nullptr;
126  bool loadMatrix = tree->GetBranch("matrix") != nullptr;
127  tree->SetBranchAddress("id", &id);
128  if (loadPoints)
129  tree->SetBranchAddress("points", &points);
130  if (loadParameters)
131  tree->SetBranchAddress("topology", &topology);
132  if (loadShape)
133  tree->SetBranchAddress("shape", &shape);
134  if (loadTranslation)
135  tree->SetBranchAddress("translation", &translation);
136  if (loadMatrix)
137  tree->SetBranchAddress("matrix", &matrix);
138 
139  // reset previous values
140  m_idToInfo.clear();
141  for (const auto& p : m_idToMatrix)
142  delete p.second;
143  m_trackerTopology.reset();
144 
145  unsigned int treeSize = tree->GetEntries();
146  if (m_idToInfo.size() != treeSize)
147  m_idToInfo.resize(treeSize);
148  for (unsigned int i = 0; i < treeSize; ++i) {
149  tree->GetEntry(i);
150 
151  m_idToInfo[i].id = id;
152  if (loadPoints) {
153  for (unsigned int j = 0; j < 24; ++j)
154  m_idToInfo[i].points[j] = points[j];
155  }
156  if (loadParameters) {
157  for (unsigned int j = 0; j < 9; ++j)
158  m_idToInfo[i].parameters[j] = topology[j];
159  }
160  if (loadShape) {
161  for (unsigned int j = 0; j < 5; ++j)
162  m_idToInfo[i].shape[j] = shape[j];
163  }
164  if (loadTranslation) {
165  for (unsigned int j = 0; j < 3; ++j)
166  m_idToInfo[i].translation[j] = translation[j];
167  }
168  if (loadMatrix) {
169  for (unsigned int j = 0; j < 9; ++j)
170  m_idToInfo[i].matrix[j] = matrix[j];
171  }
172  }
173 
174  m_versionInfo.productionTag = static_cast<TNamed*>(file->Get("tag"));
175  m_versionInfo.cmsswVersion = static_cast<TNamed*>(file->Get("CMSSW_VERSION"));
176  m_versionInfo.extraDetectors = static_cast<TObjArray*>(file->Get("ExtraDetectors"));
177 
178  TString path = file->GetPath();
179  if (path.EndsWith(":/"))
180  path.Resize(path.Length() - 2);
181 
183  fwLog(fwlog::kInfo) << Form(
184  "Load %s %s from %s\n", tree->GetName(), m_versionInfo.productionTag->GetTitle(), path.Data());
185  else
186  fwLog(fwlog::kInfo) << Form("Load %s from %s\n", tree->GetName(), path.Data());
187 
188  TNamed* producerInfo = static_cast<TNamed*>(file->Get("PRODUCER_VERSION"));
189  if (producerInfo) {
190  m_producerVersion = atoi(producerInfo->GetTitle());
191  }
192 
193  TNamed* ttopology = static_cast<TNamed*>(file->Get("TrackerTopology"));
194  if (ttopology) {
195  std::string xml = ttopology->GetTitle();
197  std::make_unique<TrackerTopology>(StandaloneTrackerTopology::fromTrackerParametersXMLString(xml));
198  }
199 
200  file->Close();
201 }
202 
204  FWRecoGeom::InfoMapItr begin = map.begin();
206  unsigned int mapSize = map.size();
207  if (m_idToInfo.size() != mapSize)
208  m_idToInfo.resize(mapSize);
209  unsigned int i = 0;
210  for (FWRecoGeom::InfoMapItr it = begin; it != end; ++it, ++i) {
211  m_idToInfo[i].id = it->id;
212  for (unsigned int j = 0; j < 24; ++j)
213  m_idToInfo[i].points[j] = it->points[j];
214  for (unsigned int j = 0; j < 9; ++j)
215  m_idToInfo[i].parameters[j] = it->topology[j];
216  for (unsigned int j = 0; j < 5; ++j)
217  m_idToInfo[i].shape[j] = it->shape[j];
218  for (unsigned int j = 0; j < 3; ++j)
219  m_idToInfo[i].translation[j] = it->translation[j];
220  for (unsigned int j = 0; j < 9; ++j)
221  m_idToInfo[i].matrix[j] = it->matrix[j];
222  }
223 }
224 
225 const TGeoMatrix* FWGeometry::getMatrix(unsigned int id) const {
226  std::map<unsigned int, TGeoMatrix*>::iterator mit = m_idToMatrix.find(id);
227  if (mit != m_idToMatrix.end())
228  return mit->second;
229 
230  IdToInfoItr it = FWGeometry::find(id);
231  if (it == m_idToInfo.end()) {
232  fwLog(fwlog::kWarning) << "no reco geometry found for id " << id << std::endl;
233  return nullptr;
234  } else {
235  const GeomDetInfo& info = *it;
236  TGeoTranslation trans(info.translation[0], info.translation[1], info.translation[2]);
237  TGeoRotation rotation;
238  const Double_t matrix[9] = {info.matrix[0],
239  info.matrix[1],
240  info.matrix[2],
241  info.matrix[3],
242  info.matrix[4],
243  info.matrix[5],
244  info.matrix[6],
245  info.matrix[7],
246  info.matrix[8]};
247  rotation.SetMatrix(matrix);
248 
249  m_idToMatrix[id] = new TGeoCombiTrans(trans, rotation);
250  return m_idToMatrix[id];
251  }
252 }
253 
254 std::vector<unsigned int> FWGeometry::getMatchedIds(Detector det, SubDetector subdet) const {
255  std::vector<unsigned int> ids;
256  unsigned int mask = (det << 4) | (subdet);
257  for (IdToInfoItr it = m_idToInfo.begin(), itEnd = m_idToInfo.end(); it != itEnd; ++it) {
258  if (FWGeometry::match_id(*it, mask))
259  ids.push_back((*it).id);
260  }
261 
262  return ids;
263 }
264 
265 std::vector<unsigned int> FWGeometry::getMatchedIds(Detector det) const {
266  std::vector<unsigned int> ids;
267  std::set<unsigned int> cached_wafers;
268  for (const auto& it : m_idToInfo) {
269  if (((it.id >> kDetOffset) & 0xF) != det)
270  continue;
271 
272  // select only the first cell of each wafer
273  if (det != HGCalHSc) {
274  auto key = ~0x3FF & it.id;
275  if (cached_wafers.find(key) == cached_wafers.end()) {
276  cached_wafers.insert(key);
277  ids.push_back(it.id);
278  }
279  continue;
280  }
281 
282  ids.push_back(it.id);
283  }
284  return ids;
285 }
286 
287 TGeoShape* FWGeometry::getShape(unsigned int id) const {
288  IdToInfoItr it = FWGeometry::find(id);
289  if (it == m_idToInfo.end()) {
290  fwLog(fwlog::kWarning) << "no reco geoemtry found for id " << id << std::endl;
291  return nullptr;
292  } else {
293  return getShape(*it);
294  }
295 }
296 
297 TGeoShape* FWGeometry::getShape(const GeomDetInfo& info) const {
298  TEveGeoManagerHolder gmgr(TEveGeoShape::GetGeoMangeur());
299  TGeoShape* geoShape = nullptr;
300  if (info.shape[0] == 1) {
301  geoShape = new TGeoTrap(info.shape[3], //dz
302  0, //theta
303  0, //phi
304  info.shape[4], //dy1
305  info.shape[1], //dx1
306  info.shape[2], //dx2
307  0, //alpha1
308  info.shape[4], //dy2
309  info.shape[1], //dx3
310  info.shape[2], //dx4
311  0); //alpha2
312  } else
313  geoShape = new TGeoBBox(info.shape[1], info.shape[2], info.shape[3]);
314 
315  return geoShape;
316 }
317 
318 TEveGeoShape* FWGeometry::getEveShape(unsigned int id) const {
319  IdToInfoItr it = FWGeometry::find(id);
320  if (it == m_idToInfo.end()) {
321  fwLog(fwlog::kWarning) << "no reco geoemtry found for id " << id << std::endl;
322  return nullptr;
323  } else {
324  const GeomDetInfo& info = *it;
325  double array[16] = {info.matrix[0],
326  info.matrix[3],
327  info.matrix[6],
328  0.,
329  info.matrix[1],
330  info.matrix[4],
331  info.matrix[7],
332  0.,
333  info.matrix[2],
334  info.matrix[5],
335  info.matrix[8],
336  0.,
337  info.translation[0],
338  info.translation[1],
339  info.translation[2],
340  1.};
341  TEveGeoManagerHolder gmgr(TEveGeoShape::GetGeoMangeur());
342  TEveGeoShape* shape = new TEveGeoShape(TString::Format("RecoGeom Id=%u", id));
343  TGeoShape* geoShape = getShape(info);
344  shape->SetShape(geoShape);
345  // Set transformation matrix from a column-major array
346  shape->SetTransMatrix(array);
347  return shape;
348  }
349 }
350 
351 TEveGeoShape* FWGeometry::getHGCSiliconEveShape(unsigned int id) const {
352 #if 0
353  const unsigned int type = (id>>26)&0x3;
354  // select the middle cell of each waifer
355  id &= ~0x3FF;
356  id |= (type == 0) ? 0x16B : 0xE7;
357 #else
358  float sideToSideWaferSize = 16.7441f;
359  float dx = sideToSideWaferSize / 2;
360  float sidey = dx / sqrt(3);
361  float dy = 2 * sidey;
362 
363  int waferUint = (id >> 10) & 0xF;
364  int waferVint = (id >> 15) & 0xF;
365  float waferU = ((id >> 14) & 0x1) ? -sideToSideWaferSize * waferUint : sideToSideWaferSize * waferUint;
366  float waferV = ((id >> 19) & 0x1) ? -sideToSideWaferSize * waferVint : sideToSideWaferSize * waferVint;
367 
368  float waferX = (-2 * waferU + waferV) / 2;
369  float waferY = waferV * sqrt(3) / 2;
370 #endif
371  IdToInfoItr it = FWGeometry::find(id);
372  if (it == m_idToInfo.end()) {
373  fwLog(fwlog::kWarning) << "no reco geometry found for id " << id << std::endl;
374  return nullptr;
375  }
376 
377  GeomDetInfo info = *it;
378 
379  TEveGeoManagerHolder gmgr(TEveGeoShape::GetGeoMangeur());
380  TEveGeoShape* shape = new TEveGeoShape(TString::Format("RecoGeom Id=%u", id));
381 
382  float dz = fabs(info.points[14] - info.points[2]) * 0.5;
383 
384  info.translation[2] = (info.points[14] + info.points[2]) / 2.0f;
385  info.translation[0] = waferX * ((0 < info.translation[2]) - (info.translation[2] < 0));
386  info.translation[1] = waferY;
387 
388  TGeoXtru* geoShape = new TGeoXtru(2);
389  Double_t x[6] = {-dx, -dx, 0.0, dx, dx, 0.0};
390  Double_t y[6] = {-sidey, sidey, dy, sidey, -sidey, -dy};
391  geoShape->DefinePolygon(6, x, y);
392  geoShape->DefineSection(0, -dz);
393  geoShape->DefineSection(1, dz);
394 
395  shape->SetShape(geoShape);
396  double array[16] = {info.matrix[0],
397  info.matrix[3],
398  info.matrix[6],
399  0.,
400  info.matrix[1],
401  info.matrix[4],
402  info.matrix[7],
403  0.,
404  info.matrix[2],
405  info.matrix[5],
406  info.matrix[8],
407  0.,
408  info.translation[0],
409  info.translation[1],
410  info.translation[2],
411  1.};
412  // Set transformation matrix from a column-major array
413  shape->SetTransMatrix(array);
414  return shape;
415 }
416 
417 TEveGeoShape* FWGeometry::getHGCScintillatorEveShape(unsigned int id) const {
418  IdToInfoItr it = FWGeometry::find(id);
419  if (it == m_idToInfo.end()) {
420  fwLog(fwlog::kWarning) << "no reco geometry found for id " << id << std::endl;
421  return nullptr;
422  }
423 
424  GeomDetInfo info = *it;
425 
426  TEveGeoManagerHolder gmgr(TEveGeoShape::GetGeoMangeur());
427  TEveGeoShape* shape = new TEveGeoShape(TString::Format("RecoGeom Id=%u", id));
428 
429  TGeoXtru* geoShape = new TGeoXtru(2);
430  Double_t x[4] = {info.points[0], info.points[3], info.points[6], info.points[9]};
431  Double_t y[4] = {info.points[1], info.points[4], info.points[7], info.points[10]};
432 
433  bool isNeg = info.shape[3] < 0;
434  geoShape->DefinePolygon(4, x, y);
435  geoShape->DefineSection(0, isNeg * info.shape[3]);
436  geoShape->DefineSection(1, !isNeg * info.shape[3]);
437  info.translation[2] = info.points[2];
438 
439  shape->SetShape(geoShape);
440  double array[16] = {info.matrix[0],
441  info.matrix[3],
442  info.matrix[6],
443  0.,
444  info.matrix[1],
445  info.matrix[4],
446  info.matrix[7],
447  0.,
448  info.matrix[2],
449  info.matrix[5],
450  info.matrix[8],
451  0.,
452  info.translation[0],
453  info.translation[1],
454  info.translation[2],
455  1.};
456  // Set transformation matrix from a column-major array
457  shape->SetTransMatrix(array);
458  return shape;
459 }
460 
461 const float* FWGeometry::getCorners(unsigned int id) const {
462  // reco geometry points
463  IdToInfoItr it = FWGeometry::find(id);
464  if (it == m_idToInfo.end()) {
465  fwLog(fwlog::kWarning) << "no reco geometry found for id " << id << std::endl;
466  return nullptr;
467  } else {
468  return (*it).points;
469  }
470 }
471 
472 const float* FWGeometry::getParameters(unsigned int id) const {
473  // reco geometry parameters
474  IdToInfoItr it = FWGeometry::find(id);
475  if (it == m_idToInfo.end()) {
476  fwLog(fwlog::kWarning) << "no reco geometry found for id " << id << std::endl;
477  return nullptr;
478  } else {
479  return (*it).parameters;
480  }
481 }
482 
483 const float* FWGeometry::getShapePars(unsigned int id) const {
484  // reco geometry parameters
485  IdToInfoItr it = FWGeometry::find(id);
486  if (it == m_idToInfo.end()) {
487  fwLog(fwlog::kWarning) << "no reco geometry found for id " << id << std::endl;
488  return nullptr;
489  } else {
490  return (*it).shape;
491  }
492 }
493 
494 void FWGeometry::localToGlobal(unsigned int id, const float* local, float* global, bool translatep) const {
495  IdToInfoItr it = FWGeometry::find(id);
496  if (it == m_idToInfo.end()) {
497  fwLog(fwlog::kWarning) << "no reco geometry found for id " << id << std::endl;
498  } else {
499  localToGlobal(*it, local, global, translatep);
500  }
501 }
502 
504  unsigned int id, const float* local1, float* global1, const float* local2, float* global2, bool translatep) const {
505  IdToInfoItr it = FWGeometry::find(id);
506  if (it == m_idToInfo.end()) {
507  fwLog(fwlog::kWarning) << "no reco geometry found for id " << id << std::endl;
508  } else {
509  localToGlobal(*it, local1, global1, translatep);
510  localToGlobal(*it, local2, global2, translatep);
511  }
512 }
513 
515  FWGeometry::IdToInfoItr begin = m_idToInfo.begin();
517  return std::lower_bound(begin, end, id);
518 }
519 
520 void FWGeometry::localToGlobal(const GeomDetInfo& info, const float* local, float* global, bool translatep) const {
521  for (int i = 0; i < 3; ++i) {
522  global[i] = translatep ? info.translation[i] : 0;
523  global[i] += local[0] * info.matrix[3 * i] + local[1] * info.matrix[3 * i + 1] + local[2] * info.matrix[3 * i + 2];
524  }
525 }
526 
527 //______________________________________________________________________________
528 
529 bool FWGeometry::VersionInfo::haveExtraDet(const char* det) const {
530  return (extraDetectors && extraDetectors->FindObject(det)) ? true : false;
531 }
std::unique_ptr< TrackerTopology > m_trackerTopology
Definition: FWGeometry.h:175
static TFile * findFile(const char *fileName)
Definition: FWGeometry.cc:38
std::vector< FWRecoGeom::Info >::const_iterator InfoMapItr
Definition: FWRecoGeom.h:24
static const TGPicture * info(bool iBackgroundIsBlack)
TObjArray * extraDetectors
Definition: FWGeometry.h:70
void initMap(const FWRecoGeom::InfoMap &map)
Definition: FWGeometry.cc:203
std::vector< unsigned int > getMatchedIds(Detector det, SubDetector subdet) const
Definition: FWGeometry.cc:254
int32_t waferU(const int32_t index)
for(int i=first, nt=offsets[nh];i< nt;i+=gridDim.x *blockDim.x)
FWGeometry(void)
Definition: FWGeometry.cc:27
~FWGeometry(void)
Definition: FWGeometry.cc:29
bool haveExtraDet(const char *) const
Definition: FWGeometry.cc:529
constexpr uint32_t mask
Definition: gpuClustering.h:24
std::map< unsigned int, TGeoMatrix * > m_idToMatrix
Definition: FWGeometry.h:163
IdToInfoItr find(unsigned int) const
Definition: FWGeometry.cc:514
TEveGeoShape * getHGCScintillatorEveShape(unsigned int id) const
Definition: FWGeometry.cc:417
VersionInfo m_versionInfo
Definition: FWGeometry.h:169
const float * getCorners(unsigned int id) const
Definition: FWGeometry.cc:461
TEveGeoShape * getHGCSiliconEveShape(unsigned int id) const
Definition: FWGeometry.cc:351
void loadMap(const char *fileName)
Definition: FWGeometry.cc:103
T sqrt(T t)
Definition: SSEVec.h:19
int m_producerVersion
Definition: FWGeometry.h:171
double f[11][100]
std::vector< FWRecoGeom::Info > InfoMap
Definition: FWRecoGeom.h:23
std::string m_fileName
Definition: FWGeometry.h:173
TEveGeoShape * getEveShape(unsigned int id) const
Definition: FWGeometry.cc:318
TGeoShape * getShape(unsigned int id) const
Definition: FWGeometry.cc:287
bool isEmpty() const
Definition: FWGeometry.cc:31
#define fwLog(_level_)
Definition: fwLog.h:45
void localToGlobal(unsigned int id, const float *local, float *global, bool translatep=true) const
Definition: FWGeometry.cc:494
const float * getShapePars(unsigned int id) const
Definition: FWGeometry.cc:483
const float * getParameters(unsigned int id) const
Definition: FWGeometry.cc:472
const TGeoMatrix * getMatrix(unsigned int id) const
Definition: FWGeometry.cc:225
int32_t waferV(const int32_t index)
TrackerTopology fromTrackerParametersXMLString(const std::string &xmlContent)
void applyGlobalTag(const std::string &gt)
Definition: FWGeometry.cc:59
static const int kDetOffset
Definition: FWGeometry.h:29
Definition: tree.py:1
IdToInfo m_idToInfo
Definition: FWGeometry.h:165
bool match_id(const GeomDetInfo &o, unsigned int mask) const
Definition: FWGeometry.h:131
std::vector< FWGeometry::GeomDetInfo >::const_iterator IdToInfoItr
Definition: FWGeometry.h:137