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