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