CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 #include <iostream>
14 #include <cassert>
15 #include <sstream>
16 #include <stdexcept>
17 #include <algorithm>
18 
20 {}
21 
23 {}
24 
25 TFile*
27 {
28  std::string searchPath = ".";
29 
30  if (gSystem->Getenv( "CMSSW_SEARCH_PATH" ))
31  {
32 
33  TString paths = gSystem->Getenv( "CMSSW_SEARCH_PATH" );
34 
35  TObjArray* tokens = paths.Tokenize( ":" );
36  for( int i = 0; i < tokens->GetEntries(); ++i )
37  {
38  TObjString* path = (TObjString*)tokens->At( i );
39  searchPath += ":";
40  searchPath += path->GetString();
41  if (gSystem->Getenv("CMSSW_VERSION"))
42  searchPath += "/Fireworks/Geometry/data/";
43  }
44  }
45 
46  TString fn = fileName;
47  const char* fp = gSystem->FindFile(searchPath.c_str(), fn, kFileExists);
48  return fp ? TFile::Open( fp) : 0;
49 }
50 
51 void
53 {
54  TFile* file = findFile( fileName );
55  if( ! file )
56  {
57  throw std::runtime_error( "ERROR: failed to find geometry file. Initialization failed." );
58  return;
59  }
60 
61  TTree* tree = static_cast<TTree*>(file->Get( "idToGeo" ));
62  if( ! tree )
63  {
64  throw std::runtime_error( "ERROR: cannot find detector id map in the file. Initialization failed." );
65  return;
66  }
67 
68  unsigned int id;
69  Float_t points[24];
70  Float_t topology[9];
71  Float_t shape[5];
72  Float_t translation[3];
73  Float_t matrix[9];
74  bool loadPoints = tree->GetBranch( "points" ) != 0;
75  bool loadParameters = tree->GetBranch( "topology" ) != 0;
76  bool loadShape = tree->GetBranch( "shape" ) != 0;
77  bool loadTranslation = tree->GetBranch( "translation" ) != 0;
78  bool loadMatrix = tree->GetBranch( "matrix" ) != 0;
79  tree->SetBranchAddress( "id", &id );
80  if( loadPoints )
81  tree->SetBranchAddress( "points", &points );
82  if( loadParameters )
83  tree->SetBranchAddress( "topology", &topology );
84  if( loadShape )
85  tree->SetBranchAddress( "shape", &shape );
86  if( loadTranslation )
87  tree->SetBranchAddress( "translation", &translation );
88  if( loadMatrix )
89  tree->SetBranchAddress( "matrix", &matrix );
90 
91  unsigned int treeSize = tree->GetEntries();
92  if( m_idToInfo.size() != treeSize )
93  m_idToInfo.resize( treeSize );
94  for( unsigned int i = 0; i < treeSize; ++i )
95  {
96  tree->GetEntry( i );
97 
98  m_idToInfo[i].id = id;
99  if( loadPoints )
100  {
101  for( unsigned int j = 0; j < 24; ++j )
102  m_idToInfo[i].points[j] = points[j];
103  }
104  if( loadParameters )
105  {
106  for( unsigned int j = 0; j < 9; ++j )
107  m_idToInfo[i].parameters[j] = topology[j];
108  }
109  if( loadShape )
110  {
111  for( unsigned int j = 0; j < 5; ++j )
112  m_idToInfo[i].shape[j] = shape[j];
113  }
114  if( loadTranslation )
115  {
116  for( unsigned int j = 0; j < 3; ++j )
117  m_idToInfo[i].translation[j] = translation[j];
118  }
119  if( loadMatrix )
120  {
121  for( unsigned int j = 0; j < 9; ++j )
122  m_idToInfo[i].matrix[j] = matrix[j];
123  }
124  }
125 
126 
127  m_versionInfo.productionTag = static_cast<TNamed*>(file->Get( "tag" ));
128  m_versionInfo.cmsswVersion = static_cast<TNamed*>(file->Get( "CMSSW_VERSION" ));
129  m_versionInfo.extraDetectors = static_cast<TObjArray*>(file->Get( "ExtraDetectors" ));
130 
131  TString path = file->GetPath();
132  if (path.EndsWith(":/")) path.Resize(path.Length() -2);
133 
135  fwLog( fwlog::kInfo ) << Form("Load %s %s from %s\n ", tree->GetName(), m_versionInfo.productionTag->GetName(), path.Data());
136  else
137  fwLog( fwlog::kInfo ) << Form("Load %s from %s\n ", tree->GetName(), path.Data());
138 
139  file->Close();
140 }
141 
142 void
144 {
145  FWRecoGeom::InfoMapItr begin = map.begin();
146  FWRecoGeom::InfoMapItr end = map.end();
147  unsigned int mapSize = map.size();
148  if( m_idToInfo.size() != mapSize )
149  m_idToInfo.resize( mapSize );
150  unsigned int i = 0;
151  for( FWRecoGeom::InfoMapItr it = begin;
152  it != end; ++it, ++i )
153  {
154  m_idToInfo[i].id = it->id;
155  for( unsigned int j = 0; j < 24; ++j )
156  m_idToInfo[i].points[j] = it->points[j];
157  for( unsigned int j = 0; j < 9; ++j )
158  m_idToInfo[i].parameters[j] = it->topology[j];
159  for( unsigned int j = 0; j < 5; ++j )
160  m_idToInfo[i].shape[j] = it->shape[j];
161  for( unsigned int j = 0; j < 3; ++j )
162  m_idToInfo[i].translation[j] = it->translation[j];
163  for( unsigned int j = 0; j < 9; ++j )
164  m_idToInfo[i].matrix[j] = it->matrix[j];
165  }
166 }
167 
168 const TGeoMatrix*
169 FWGeometry::getMatrix( unsigned int id ) const
170 {
171  std::map<unsigned int, TGeoMatrix*>::iterator mit = m_idToMatrix.find( id );
172  if( mit != m_idToMatrix.end()) return mit->second;
173 
174  IdToInfoItr it = FWGeometry::find( id );
175  if( it == m_idToInfo.end())
176  {
177  fwLog( fwlog::kWarning ) << "no reco geometry found for id " << id << std::endl;
178  return 0;
179  }
180  else
181  {
182  const GeomDetInfo& info = *it;
183  TGeoTranslation trans( info.translation[0], info.translation[1], info.translation[2] );
184  TGeoRotation rotation;
185  const Double_t matrix[9] = { info.matrix[0], info.matrix[1], info.matrix[2],
186  info.matrix[3], info.matrix[4], info.matrix[5],
187  info.matrix[6], info.matrix[7], info.matrix[8]
188  };
189  rotation.SetMatrix( matrix );
190 
191  m_idToMatrix[id] = new TGeoCombiTrans( trans, rotation );
192  return m_idToMatrix[id];
193  }
194 }
195 
196 std::vector<unsigned int>
198 {
199  std::vector<unsigned int> ids;
200  unsigned int mask = ( det << 4 ) | ( subdet );
201  for( IdToInfoItr it = m_idToInfo.begin(), itEnd = m_idToInfo.end();
202  it != itEnd; ++it )
203  {
204  if( FWGeometry::match_id( *it, mask ))
205  ids.push_back(( *it ).id );
206  }
207 
208  return ids;
209 }
210 
211 TGeoShape*
212 FWGeometry::getShape( unsigned int id ) const
213 {
214  IdToInfoItr it = FWGeometry::find( id );
215  if( it == m_idToInfo.end())
216  {
217  fwLog( fwlog::kWarning ) << "no reco geoemtry found for id " << id << std::endl;
218  return 0;
219  }
220  else
221  {
222  return getShape( *it );
223  }
224 }
225 
226 TGeoShape*
228 {
229  TEveGeoManagerHolder gmgr( TEveGeoShape::GetGeoMangeur());
230  TGeoShape* geoShape = 0;
231  if( info.shape[0] == 1 )
232  {
233  geoShape = new TGeoTrap(
234  info.shape[3], //dz
235  0, //theta
236  0, //phi
237  info.shape[4], //dy1
238  info.shape[1], //dx1
239  info.shape[2], //dx2
240  0, //alpha1
241  info.shape[4], //dy2
242  info.shape[1], //dx3
243  info.shape[2], //dx4
244  0); //alpha2
245  }
246  else
247  geoShape = new TGeoBBox( info.shape[1], info.shape[2], info.shape[3] );
248 
249  return geoShape;
250 }
251 
252 TEveGeoShape*
253 FWGeometry::getEveShape( unsigned int id ) const
254 {
255  IdToInfoItr it = FWGeometry::find( id );
256  if( it == m_idToInfo.end())
257  {
258  fwLog( fwlog::kWarning ) << "no reco geoemtry found for id " << id << std::endl;
259  return 0;
260  }
261  else
262  {
263  const GeomDetInfo& info = *it;
264  double array[16] = { info.matrix[0], info.matrix[3], info.matrix[6], 0.,
265  info.matrix[1], info.matrix[4], info.matrix[7], 0.,
266  info.matrix[2], info.matrix[5], info.matrix[8], 0.,
267  info.translation[0], info.translation[1], info.translation[2], 1.
268  };
269  TEveGeoManagerHolder gmgr( TEveGeoShape::GetGeoMangeur());
270  TEveGeoShape* shape = new TEveGeoShape(TString::Format("RecoGeom Id=%u", id));
271  TGeoShape* geoShape = getShape( info );
272  shape->SetShape( geoShape );
273  // Set transformation matrix from a column-major array
274  shape->SetTransMatrix( array );
275  return shape;
276  }
277 }
278 
279 const float*
280 FWGeometry::getCorners( unsigned int id ) const
281 {
282  // reco geometry points
283  IdToInfoItr it = FWGeometry::find( id );
284  if( it == m_idToInfo.end())
285  {
286  fwLog( fwlog::kWarning ) << "no reco geometry found for id " << id << std::endl;
287  return 0;
288  }
289  else
290  {
291  return ( *it ).points;
292  }
293 }
294 
295 const float*
296 FWGeometry::getParameters( unsigned int id ) const
297 {
298  // reco geometry parameters
299  IdToInfoItr it = FWGeometry::find( id );
300  if( it == m_idToInfo.end())
301  {
302  fwLog( fwlog::kWarning ) << "no reco geometry found for id " << id << std::endl;
303  return 0;
304  }
305  else
306  {
307  return ( *it ).parameters;
308  }
309 }
310 
311 const float*
312 FWGeometry::getShapePars( unsigned int id ) const
313 {
314  // reco geometry parameters
315  IdToInfoItr it = FWGeometry::find( id );
316  if( it == m_idToInfo.end())
317  {
318  fwLog( fwlog::kWarning ) << "no reco geometry found for id " << id << std::endl;
319  return 0;
320  }
321  else
322  {
323  return ( *it ).shape;
324  }
325 }
326 
327 void
328 FWGeometry::localToGlobal( unsigned int id, const float* local, float* global, bool translatep ) const
329 {
330  IdToInfoItr it = FWGeometry::find( id );
331  if( it == m_idToInfo.end())
332  {
333  fwLog( fwlog::kWarning ) << "no reco geometry found for id " << id << std::endl;
334  }
335  else
336  {
337  localToGlobal( *it, local, global, translatep );
338  }
339 }
340 
341 void
342 FWGeometry::localToGlobal( unsigned int id, const float* local1, float* global1,
343  const float* local2, float* global2, bool translatep ) const
344 {
345  IdToInfoItr it = FWGeometry::find( id );
346  if( it == m_idToInfo.end())
347  {
348  fwLog( fwlog::kWarning ) << "no reco geometry found for id " << id << std::endl;
349  }
350  else
351  {
352  localToGlobal( *it, local1, global1, translatep );
353  localToGlobal( *it, local2, global2, translatep );
354  }
355 }
356 
358 FWGeometry::find( unsigned int id ) const
359 {
362  return std::lower_bound( begin, end, id );
363 }
364 
365 void
366 FWGeometry::localToGlobal( const GeomDetInfo& info, const float* local, float* global, bool translatep ) const
367 {
368  for( int i = 0; i < 3; ++i )
369  {
370  global[i] = translatep ? info.translation[i] : 0;
371  global[i] += local[0] * info.matrix[3 * i]
372  + local[1] * info.matrix[3 * i + 1]
373  + local[2] * info.matrix[3 * i + 2];
374  }
375 }
376 
377 //______________________________________________________________________________
378 
379 bool FWGeometry::VersionInfo::haveExtraDet(const char* det) const
380 {
381 
382  return (extraDetectors && extraDetectors->FindObject(det)) ? true : false;
383 }
static TFile * findFile(const char *fileName)
Definition: FWGeometry.cc:26
std::vector< FWRecoGeom::Info >::const_iterator InfoMapItr
Definition: FWRecoGeom.h:28
int i
Definition: DBlmapReader.cc:9
dictionary parameters
Definition: Parameters.py:2
static const TGPicture * info(bool iBackgroundIsBlack)
TObjArray * extraDetectors
Definition: FWGeometry.h:46
static void loadMatrix(DOMElement *elem, unsigned int n, TMatrixDBase &matrix)
void initMap(const FWRecoGeom::InfoMap &map)
Definition: FWGeometry.cc:143
CaloTopology const * topology(0)
IdToInfoItr find(unsigned int) const
Definition: FWGeometry.cc:358
const float * getParameters(unsigned int id) const
Definition: FWGeometry.cc:296
FWGeometry(void)
Definition: FWGeometry.cc:19
~FWGeometry(void)
Definition: FWGeometry.cc:22
bool match_id(const GeomDetInfo &o, unsigned int mask) const
Definition: FWGeometry.h:101
TGeoShape * getShape(unsigned int id) const
Definition: FWGeometry.cc:212
const TGeoMatrix * getMatrix(unsigned int id) const
Definition: FWGeometry.cc:169
std::map< unsigned int, TGeoMatrix * > m_idToMatrix
Definition: FWGeometry.h:120
const float * getShapePars(unsigned int id) const
Definition: FWGeometry.cc:312
bool haveExtraDet(const char *) const
Definition: FWGeometry.cc:379
tuple path
else: Piece not in the list, fine.
VersionInfo m_versionInfo
Definition: FWGeometry.h:126
TEveGeoShape * getEveShape(unsigned int id) const
Definition: FWGeometry.cc:253
void loadMap(const char *fileName)
Definition: FWGeometry.cc:52
void localToGlobal(unsigned int id, const float *local, float *global, bool translatep=true) const
Definition: FWGeometry.cc:328
int j
Definition: DBlmapReader.cc:9
#define end
Definition: vmac.h:37
std::vector< FWRecoGeom::Info > InfoMap
Definition: FWRecoGeom.h:27
const float * getCorners(unsigned int id) const
Definition: FWGeometry.cc:280
#define fwLog(_level_)
Definition: fwLog.h:50
#define begin
Definition: vmac.h:30
std::vector< unsigned int > getMatchedIds(Detector det, SubDetector subdet) const
Definition: FWGeometry.cc:197
IdToInfo m_idToInfo
Definition: FWGeometry.h:122
std::vector< FWGeometry::GeomDetInfo >::const_iterator IdToInfoItr
Definition: FWGeometry.h:107