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