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 TGeoShape*
229 FWGeometry::getShape( unsigned int id ) const
230 {
231  IdToInfoItr it = FWGeometry::find( id );
232  if( it == m_idToInfo.end())
233  {
234  fwLog( fwlog::kWarning ) << "no reco geoemtry found for id " << id << std::endl;
235  return nullptr;
236  }
237  else
238  {
239  return getShape( *it );
240  }
241 }
242 
243 TGeoShape*
245 {
246  TEveGeoManagerHolder gmgr( TEveGeoShape::GetGeoMangeur());
247  TGeoShape* geoShape = nullptr;
248  if( info.shape[0] == 1 )
249  {
250  geoShape = new TGeoTrap(
251  info.shape[3], //dz
252  0, //theta
253  0, //phi
254  info.shape[4], //dy1
255  info.shape[1], //dx1
256  info.shape[2], //dx2
257  0, //alpha1
258  info.shape[4], //dy2
259  info.shape[1], //dx3
260  info.shape[2], //dx4
261  0); //alpha2
262  }
263  else
264  geoShape = new TGeoBBox( info.shape[1], info.shape[2], info.shape[3] );
265 
266  return geoShape;
267 }
268 
269 TEveGeoShape*
270 FWGeometry::getEveShape( unsigned int id ) const
271 {
272  IdToInfoItr it = FWGeometry::find( id );
273  if( it == m_idToInfo.end())
274  {
275  fwLog( fwlog::kWarning ) << "no reco geoemtry found for id " << id << std::endl;
276  return nullptr;
277  }
278  else
279  {
280  const GeomDetInfo& info = *it;
281  double array[16] = { info.matrix[0], info.matrix[3], info.matrix[6], 0.,
282  info.matrix[1], info.matrix[4], info.matrix[7], 0.,
283  info.matrix[2], info.matrix[5], info.matrix[8], 0.,
284  info.translation[0], info.translation[1], info.translation[2], 1.
285  };
286  TEveGeoManagerHolder gmgr( TEveGeoShape::GetGeoMangeur());
287  TEveGeoShape* shape = new TEveGeoShape(TString::Format("RecoGeom Id=%u", id));
288  TGeoShape* geoShape = getShape( info );
289  shape->SetShape( geoShape );
290  // Set transformation matrix from a column-major array
291  shape->SetTransMatrix( array );
292  return shape;
293  }
294 }
295 
296 const float*
297 FWGeometry::getCorners( unsigned int id ) const
298 {
299  // reco geometry points
300  IdToInfoItr it = FWGeometry::find( id );
301  if( it == m_idToInfo.end())
302  {
303  fwLog( fwlog::kWarning ) << "no reco geometry found for id " << id << std::endl;
304  return nullptr;
305  }
306  else
307  {
308  return ( *it ).points;
309  }
310 }
311 
312 const float*
313 FWGeometry::getParameters( unsigned int id ) const
314 {
315  // reco geometry parameters
316  IdToInfoItr it = FWGeometry::find( id );
317  if( it == m_idToInfo.end())
318  {
319  fwLog( fwlog::kWarning ) << "no reco geometry found for id " << id << std::endl;
320  return nullptr;
321  }
322  else
323  {
324  return ( *it ).parameters;
325  }
326 }
327 
328 const float*
329 FWGeometry::getShapePars( unsigned int id ) const
330 {
331  // reco geometry parameters
332  IdToInfoItr it = FWGeometry::find( id );
333  if( it == m_idToInfo.end())
334  {
335  fwLog( fwlog::kWarning ) << "no reco geometry found for id " << id << std::endl;
336  return nullptr;
337  }
338  else
339  {
340  return ( *it ).shape;
341  }
342 }
343 
344 void
345 FWGeometry::localToGlobal( unsigned int id, const float* local, float* global, bool translatep ) const
346 {
347  IdToInfoItr it = FWGeometry::find( id );
348  if( it == m_idToInfo.end())
349  {
350  fwLog( fwlog::kWarning ) << "no reco geometry found for id " << id << std::endl;
351  }
352  else
353  {
354  localToGlobal( *it, local, global, translatep );
355  }
356 }
357 
358 void
359 FWGeometry::localToGlobal( unsigned int id, const float* local1, float* global1,
360  const float* local2, float* global2, bool translatep ) const
361 {
362  IdToInfoItr it = FWGeometry::find( id );
363  if( it == m_idToInfo.end())
364  {
365  fwLog( fwlog::kWarning ) << "no reco geometry found for id " << id << std::endl;
366  }
367  else
368  {
369  localToGlobal( *it, local1, global1, translatep );
370  localToGlobal( *it, local2, global2, translatep );
371  }
372 }
373 
375 FWGeometry::find( unsigned int id ) const
376 {
379  return std::lower_bound( begin, end, id );
380 }
381 
382 void
383 FWGeometry::localToGlobal( const GeomDetInfo& info, const float* local, float* global, bool translatep ) const
384 {
385  for( int i = 0; i < 3; ++i )
386  {
387  global[i] = translatep ? info.translation[i] : 0;
388  global[i] += local[0] * info.matrix[3 * i]
389  + local[1] * info.matrix[3 * i + 1]
390  + local[2] * info.matrix[3 * i + 2];
391  }
392 }
393 
394 //______________________________________________________________________________
395 
396 bool FWGeometry::VersionInfo::haveExtraDet(const char* det) const
397 {
398 
399  return (extraDetectors && extraDetectors->FindObject(det)) ? true : false;
400 }
std::unique_ptr< TrackerTopology > m_trackerTopology
Definition: FWGeometry.h:142
static TFile * findFile(const char *fileName)
Definition: FWGeometry.cc:30
std::vector< FWRecoGeom::Info >::const_iterator InfoMapItr
Definition: FWRecoGeom.h:28
static const TGPicture * info(bool iBackgroundIsBlack)
TObjArray * extraDetectors
Definition: FWGeometry.h:49
static void loadMatrix(DOMElement *elem, unsigned int n, TMatrixDBase &matrix)
void initMap(const FWRecoGeom::InfoMap &map)
Definition: FWGeometry.cc:160
CaloTopology const * topology(0)
IdToInfoItr find(unsigned int) const
Definition: FWGeometry.cc:375
const float * getParameters(unsigned int id) const
Definition: FWGeometry.cc:313
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:104
TGeoShape * getShape(unsigned int id) const
Definition: FWGeometry.cc:229
const TGeoMatrix * getMatrix(unsigned int id) const
Definition: FWGeometry.cc:186
std::map< unsigned int, TGeoMatrix * > m_idToMatrix
Definition: FWGeometry.h:132
const float * getShapePars(unsigned int id) const
Definition: FWGeometry.cc:329
bool haveExtraDet(const char *) const
Definition: FWGeometry.cc:396
VersionInfo m_versionInfo
Definition: FWGeometry.h:138
TEveGeoShape * getEveShape(unsigned int id) const
Definition: FWGeometry.cc:270
void loadMap(const char *fileName)
Definition: FWGeometry.cc:56
int m_producerVersion
Definition: FWGeometry.h:140
void localToGlobal(unsigned int id, const float *local, float *global, bool translatep=true) const
Definition: FWGeometry.cc:345
#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:297
#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)
Definition: tree.py:1
IdToInfo m_idToInfo
Definition: FWGeometry.h:134
std::vector< FWGeometry::GeomDetInfo >::const_iterator IdToInfoItr
Definition: FWGeometry.h:110