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