]>
Commit | Line | Data |
---|---|---|
ab2f6604 | 1 | /**********************************************/ |
2 | /* */ | |
3 | /* FILE: AliGeometry.cxx */ | |
4 | /* PURPOSE: To store and retrieve a complete */ | |
5 | /* geometry in/from a root file. */ | |
6 | /* LANGUAGE: C++ */ | |
7 | /* COMPILER: CC for HP-UX 9.x and 10. */ | |
8 | /* AUTHOR: Joana && David */ | |
9 | /* DATE: May 28, 1999 */ | |
10 | /* ADDRESS: jesanto@cern.ch, dcollado@cern.ch */ | |
11 | /* */ | |
12 | /**********************************************/ | |
13 | ||
14 | #include <iostream.h> | |
15 | #include <TFile.h> | |
16 | #include <TROOT.h> | |
17 | #include "AliGConfig.h" | |
18 | #include "AliGeometry.h" | |
19 | #include "AliGBox.h" | |
20 | #include "AliGSphere.h" | |
21 | #include "AliGCone.h" | |
22 | #include "AliGTube.h" | |
23 | #include "AliGTransform.h" | |
24 | ||
25 | #define FormLeng 80 | |
26 | ||
27 | //float matrix[16] = {1.,0.,0.,0.,0.,1.,0.,0.,0.,0.,1.,0.,0.,0.,0.,1.}; | |
28 | //TArrayF* gMatrix = new TArrayF( 16, matrix ); | |
29 | TVector *gMatrix = new TVector(0,15,1.,0.,0.,0.,0.,1.,0.,0.,0.,0.,1.,0.,0.,0.,0.,1., "END"); | |
30 | AliGeometry *gAliGeometry = 0; | |
31 | ||
32 | ClassImp(AliGeometry) | |
33 | ||
34 | ||
35 | // --------------------------------------------------------------------------- | |
36 | ||
37 | AliGeometry::AliGeometry( AliGeometry* Geom ) | |
38 | { | |
39 | if( Geom ) { | |
40 | /* Copy Constructor */ | |
41 | fBomb = Geom->fBomb; //Bomb factor for exploded geometry | |
42 | fGeomLevel = Geom->fGeomLevel; | |
43 | fMaxDepth = Geom->fMaxDepth; | |
44 | fName = Geom->fName; | |
45 | ||
46 | fRules = new TList(); | |
47 | if( Geom->fRules ) { | |
48 | for( int i=0; i<Geom->fRules->GetSize(); i++ ) | |
49 | fRules->AddLast(Geom->fRules->At(i)); | |
50 | } | |
51 | ||
52 | fTitle = Geom->fTitle; | |
53 | fTopNode = Geom->fTopNode; | |
54 | ||
55 | gAliGeometry = this; | |
56 | fTransformation = new TList(); | |
57 | if( Geom->fTransformation ) { | |
58 | for( int i=0; i<Geom->fTransformation->GetSize(); i++ ) | |
59 | fTransformation->AddLast(Geom->fTransformation->At(i)); | |
60 | } | |
61 | } | |
62 | else { | |
63 | /* Default constructor */ | |
64 | fBomb = 1.; //Bomb factor for exploded geometry | |
65 | fGeomLevel = 0; | |
66 | fMaxDepth = 0; // 0 = No Depth limitation | |
67 | fName = ""; | |
68 | fRules = new TList(); // Antes estaba a NULL | |
69 | fTitle = ""; | |
70 | fTopNode = ""; | |
71 | fTransformation = new TList(); // Antes estaba a NULL | |
72 | gAliGeometry = this; | |
73 | } | |
74 | } | |
75 | ||
76 | // --------------------------------------------------------------------------- | |
77 | ||
78 | AliGeometry::~AliGeometry() | |
79 | { | |
80 | /* Destructor */ | |
81 | ||
82 | if( fRules ) { | |
83 | fRules->Delete(); | |
84 | fRules = NULL; | |
85 | } | |
86 | ||
87 | if( fTransformation ) { | |
88 | fTransformation->Delete(); | |
89 | fTransformation = NULL; | |
90 | } | |
91 | } | |
92 | ||
93 | // --------------------------------------------------------------------------- | |
94 | ||
95 | AliGeometry* AliGeometry::operator=( const AliGeometry* Geom ) | |
96 | { | |
97 | /* Operator = */ | |
98 | if( this == Geom ) return this; // special case. | |
99 | ||
100 | fBomb = Geom->fBomb; //Bomb factor for exploded geometry | |
101 | fGeomLevel = Geom->fGeomLevel; | |
102 | fMaxDepth = Geom->fMaxDepth; | |
103 | fName = Geom->fName; | |
104 | gAliGeometry = this; | |
105 | ||
106 | if( fRules ) | |
107 | fRules->Delete(); | |
108 | ||
109 | if( Geom->fRules ) { | |
110 | for( int i=0; i<Geom->fRules->GetSize(); i++ ) | |
111 | fRules->AddLast(Geom->fRules->At(i)); | |
112 | } | |
113 | ||
114 | fTitle = Geom->fTitle; | |
115 | fTopNode = Geom->fTopNode; | |
116 | ||
117 | if( fTransformation ) | |
118 | fTransformation->Delete(); | |
119 | ||
120 | fTransformation = new TList(); | |
121 | ||
122 | if( Geom->fTransformation ) { | |
123 | for( int i=0; i<Geom->fTransformation->GetSize(); i++ ) | |
124 | fTransformation->AddLast(Geom->fTransformation->At(i)); | |
125 | } | |
126 | ||
127 | return this; | |
128 | } | |
129 | ||
130 | // --------------------------------------------------------------------------- | |
131 | ||
132 | AliGNode* AliGeometry::FileToMemTree( TFile* file ) | |
133 | { | |
134 | // Geometry constructor that retrieves a whole geometry from the root file | |
135 | // and stores it in memory as a tree structure | |
136 | ||
137 | // Read the name of my geometry's first node | |
138 | file->cd(); | |
139 | const char* node_name = (const char*) GetTop().Data(); | |
140 | ||
141 | // Call recursion | |
142 | AliGNode* new_node = new AliGNode( RecurToMem( node_name, file, NULL, NULL ) ); | |
143 | ||
144 | return new_node; | |
145 | } | |
146 | ||
147 | // --------------------------------------------------------------------------- | |
148 | ||
149 | AliGNode* AliGeometry::RecurToMem( const char* node_name, TFile* file, AliGNode* father, char *subtransf ) | |
150 | { | |
151 | // Recursive function used to retrieve the root_file structure into memory | |
152 | ||
153 | const int LenName = strlen(node_name); | |
154 | ||
155 | int i; | |
156 | for( i=0; i<LenName; i++ ) | |
157 | if( node_name[i] == '_' ) | |
158 | break; | |
159 | ||
160 | char* name = new char[i+1]; | |
161 | strncpy( name, node_name, i ); | |
162 | name[i] = '\x0'; | |
163 | ||
164 | char* ids = new char[LenName-i]; | |
165 | for( int g1=0, g2=i+1; g2<LenName; g1++, g2++ ) | |
166 | ids[g1] = node_name[g2]; | |
167 | ||
168 | ids[LenName-i-1] = '\x0'; | |
169 | ||
170 | const int id = atoi( ids ); | |
171 | if( ids ) | |
172 | delete [] ids; | |
173 | ||
174 | AliGNode* node = NULL; | |
175 | ||
176 | int new_node = 1; | |
177 | ||
178 | if (father) { | |
179 | AliGTransform* trans = NULL; | |
180 | ||
181 | int l; | |
182 | for( l=0; father->GetNodeFromfNode(l); l++ ) { | |
183 | // check if the node has been already added | |
184 | if ( !strcmp( node_name, (father->GetNodeFromfNode(l))->GetName()) ) { | |
185 | new_node = 0; | |
186 | break; | |
187 | } | |
188 | else | |
189 | node = new AliGNode( (Text_t*)name, id, (Text_t*)name, (AliGBox*) NULL ); | |
190 | } | |
191 | ||
192 | int new_trans = 1; | |
193 | int m; | |
194 | for( m=0; father->GetTransFromfTrans(m); m++ ) { | |
195 | // check if this tranformations has already been used with this father | |
196 | if (!strcmp(subtransf,(father->GetTransFromfTrans(m))->GetName())) { | |
197 | new_trans=0; | |
198 | break; | |
199 | } | |
200 | else { | |
201 | trans = new AliGTransform(); | |
202 | file->cd(father->GetPath()); | |
203 | trans->Read(subtransf); | |
204 | } | |
205 | } | |
206 | ||
207 | if ( l == 0 ) node = new AliGNode( (Text_t*)name, id, (Text_t*)name, (AliGBox*) NULL ); | |
208 | if ( m == 0 ) { | |
209 | trans = new AliGTransform(); | |
210 | trans->Read(subtransf); | |
211 | } | |
212 | ||
213 | if (new_node) { | |
214 | if(!new_trans) { | |
215 | father->Add(node,father->GetTransFromfTrans(m)); | |
216 | file->cd(node->GetPath()); | |
217 | } | |
218 | else { | |
219 | father->Add(node,trans); | |
220 | file->cd(node->GetPath()); | |
221 | } | |
222 | } | |
223 | else { | |
224 | if(!new_trans) { | |
225 | father->Add(father->GetNodeFromfNode(l),father->GetTransFromfTrans(m)); | |
226 | file->cd(father->GetPath()); | |
227 | } | |
228 | else { | |
229 | father->Add(father->GetNodeFromfNode(l),trans); | |
230 | file->cd(father->GetPath()); | |
231 | } | |
232 | } | |
233 | ||
234 | //if( trans ) delete trans; | |
235 | } | |
236 | else { // There is no father | |
237 | node = new AliGNode( (Text_t*)name, id, (Text_t*)name, (AliGBox*) NULL ); | |
238 | file->cd(node->GetPath()); | |
239 | } | |
240 | ||
241 | if( name ) | |
242 | delete [] name; | |
243 | ||
244 | if( new_node ) { | |
245 | // Read the configuration name | |
246 | char* configs = NULL; | |
247 | ||
248 | if( fRules ) { | |
249 | for( int k=0; k<fRules->GetSize(); k++ ) { | |
250 | char* rule = new char[strlen((char*)fRules->At(k)->GetName())]; | |
251 | strcpy( rule, (char*)fRules->At(k)->GetName() ); | |
252 | ||
253 | int w; | |
254 | for( w=0; w<strlen(rule); w++ ) | |
255 | if( rule[w] == '/' ) | |
256 | break; | |
257 | ||
258 | configs = new char[w+1]; | |
259 | strncpy( configs, rule, w ); | |
260 | configs[w] = '\x0'; | |
261 | ||
262 | if( !strcmp(node->GetName(), configs) ) { | |
263 | delete [] rule; | |
264 | break; | |
265 | } | |
266 | else { | |
267 | delete [] configs; | |
268 | configs = NULL; | |
269 | } | |
270 | ||
271 | delete [] rule; | |
272 | } | |
273 | } | |
274 | else { | |
275 | printf( " ERROR: I couldn't find the fRule inside RecurToMem.\n" ); | |
276 | return NULL; | |
277 | } | |
278 | ||
279 | // Build Configuration | |
280 | AliGConfig* Config = new AliGConfig(); | |
281 | ||
282 | Config->Read(configs); | |
283 | node->AddConf(Config); | |
284 | ||
285 | // Read Shape, material and formula from configuration | |
286 | const Text_t* shapetype = Config->GetShapeType().Data(); | |
287 | const Text_t* shapename = Config->GetShapeName().Data(); | |
288 | ||
289 | // Build Shape | |
290 | ||
291 | if( !strcmp(shapetype,"AliGBox") ) { | |
292 | AliGBox* shape = new AliGBox(); | |
293 | shape->Read(shapename); | |
294 | node->AddShape(shape); | |
295 | } | |
296 | else { | |
297 | if (!strcmp(shapetype,"AliGSphere")) { | |
298 | AliGSphere* shape = new AliGSphere(); | |
299 | shape->Read(shapename); | |
300 | node->AddShape(shape); | |
301 | } | |
302 | else { | |
303 | if (!strcmp(shapetype,"AliGTube")) { | |
304 | AliGTube* shape = new AliGTube(); | |
305 | shape->Read(shapename); | |
306 | node->AddShape(shape); | |
307 | } | |
308 | else { | |
309 | if (!strcmp(shapetype,"AliGCone")) { | |
310 | AliGCone* shape = new AliGCone(); | |
311 | shape->Read(shapename); | |
312 | node->AddShape(shape); | |
313 | } | |
314 | else { | |
315 | if (!strcmp(shapetype,"AliGPCone")) { | |
316 | AliGPCone* shape = new AliGPCone(); | |
317 | shape->Read(shapename); | |
318 | node->AddShape(shape); | |
319 | } | |
320 | else { | |
321 | if (!strcmp(shapetype,"AliGTRD1")) { | |
322 | AliGTRD1* shape = new AliGTRD1(); | |
323 | shape->Read(shapename); | |
324 | node->AddShape(shape); | |
325 | } | |
326 | } | |
327 | } | |
328 | } | |
329 | } | |
330 | } | |
331 | ||
332 | // Build Material | |
333 | const Text_t* materialname = Config->GetMaterialName().Data(); | |
334 | AliGMaterial *material = new AliGMaterial(); | |
335 | material->Read(materialname); | |
336 | node->AddMaterial(material); | |
337 | ||
338 | // Read formula | |
339 | const char* formula = Config->GetFormula(); | |
340 | ||
341 | int Len = strlen(formula); | |
342 | int j = 0; | |
343 | ||
344 | while( formula[j] ) { | |
345 | char* subnode = new char[Len]; | |
346 | char* subtransf = new char[Len]; | |
347 | int k; | |
348 | ||
349 | for( k=0; formula[j] != ':'; k++, j++ ) | |
350 | subnode[k] = formula[j]; | |
351 | ||
352 | subnode[k] = '\x0'; | |
353 | if( formula[j] ) j++; | |
354 | ||
355 | for( k=0; formula[j] && formula[j] != '+'; k++, j++ ) | |
356 | subtransf[k] = formula[j]; | |
357 | ||
358 | subtransf[k] = '\x0'; | |
359 | if( formula[j] ) j++; | |
360 | ||
361 | AliGNode* node1 = this->RecurToMem( subnode, file, node, subtransf ); | |
362 | } | |
363 | } | |
364 | ||
365 | return node; | |
366 | } | |
367 | ||
368 | // --------------------------------------------------------------------------- | |
369 | ||
370 | AliGeometry::AliGeometry( Text_t* name, Text_t* title, AliGNode* topNode, int maxDepth ): TNamed(name, title) | |
371 | { | |
372 | /* Geometry constructor that stores in the root file the geometry file that corresponds to one geometry */ | |
373 | ||
374 | gAliGeometry = this; | |
375 | fBomb = 1.; //Bomb factor for exploded geometry | |
376 | fGeomLevel = 0; | |
377 | fMaxDepth = maxDepth; | |
378 | fRules = new TList(); | |
379 | fTopNode = topNode->GetName(); | |
380 | fTransformation = new TList(); | |
381 | ||
382 | RulesList( fRules, topNode, 0 ); | |
383 | ||
384 | } | |
385 | ||
386 | // --------------------------------------------------------------------------- | |
387 | ||
388 | void AliGeometry::RulesList( TList *fRules, AliGNode* node, int position ) | |
389 | { | |
390 | /* Recursive function used to create the list of rules for the Geometry file */ | |
391 | ||
392 | int len = strlen(node->GetName()) + strlen(node->GetConfig()->GetName()); | |
393 | ||
394 | char* path = new char[len+2]; | |
395 | strcpy( path, node->GetName() ); | |
396 | strcat( path, "/" ); | |
397 | strcat( path, node->GetConfig()->GetName()); | |
398 | TNamed* rule = new TNamed( path, "" ); | |
399 | delete [] path; | |
400 | ||
401 | fRules->AddLast( rule ); | |
402 | //cout << " Rule: " << rule->GetName() << " added." << endl; | |
403 | //if( rule ) delete rule; | |
404 | ||
405 | while( (position < node->SizefNode()) && (node->GetNodeFromfNode(position) != NULL) ) { | |
406 | this->RulesList( fRules, node->GetNodeFromfNode(position), 0 ); | |
407 | position++; | |
408 | } | |
409 | ||
410 | } | |
411 | ||
412 | // --------------------------------------------------------------------------- | |
413 | ||
414 | void AliGeometry::UpdateMatrix(AliGTransform* trans) | |
415 | { | |
416 | // Update global rotation matrix/translation vector for this node | |
417 | // this function must be called before invoking Local2Master | |
418 | ||
419 | TVector* matrix = trans->GetMatrix(); // Get father's transf. matrix | |
420 | TVector* temp = new TVector( 0,15,1.,0.,0.,0.,0.,1.,0.,0.,0.,0.,1.,0.,0.,0.,0.,1., "END"); | |
421 | ||
422 | ||
423 | // printf("Inside updatematrix, this is the son's matrix\n"); | |
424 | // matrix->Print(); | |
425 | // printf("Inside updatematrix, this is the global matrix before\n"); | |
426 | // gMatrix->Print(); | |
427 | /* Multiplication of matrices*/ | |
428 | for( int i=0; i<4; i++ ) | |
429 | for( int j=0; j<4; j++ ) { | |
430 | double tmp = 0; | |
431 | for( int k=0; k<4; k++ ) | |
432 | // tmp += (*matrix)(i*4+k) * (*gMatrix)(j + k*4); | |
433 | tmp += (*gMatrix)(i*4+k) * (*matrix)(j + k*4); | |
434 | (*temp)(i*4+j)= tmp; | |
435 | } | |
436 | //if(gMatrix) delete gMatrix; | |
437 | gMatrix = temp; | |
438 | ||
439 | // printf("Inside updatematrix, this is the global matrix after\n"); | |
440 | // gMatrix->Print(); | |
441 | ||
442 | // for (int k = 0; k < 16; k++) { | |
443 | //cout << "gMatrix[" << k << "]" << (*gMatrix)(k) << endl; | |
444 | //cout << "matrix[" << k << "]" << (*matrix)(k) << endl; | |
445 | //} | |
446 | ||
447 | //if (temp) delete [] temp; | |
448 | //if (matrix) delete [] matrix; | |
449 | ||
450 | } | |
451 | ||
452 | // --------------------------------------------------------------------------- | |
453 | ||
454 | void AliGeometry::Local2Master(Float_t *local, Float_t *master) | |
455 | { | |
456 | // *-*-*-*-*Convert one point from local system to master reference system*-*-* | |
457 | // *-* ============================================================== | |
458 | // Note that before invoking this function, the matrix of the transformation | |
459 | // for this node must have been computed. | |
460 | // AliGeometry::UpdateMatrix should be called before. | |
461 | ||
462 | ||
463 | // gMatrix->Print(); | |
464 | if( gNode->GetParent() && gMatrix) { | |
465 | ||
466 | Double_t tmp; | |
467 | Float_t loc[4]; | |
468 | ||
469 | for(int i=0;i<3;i++) loc[i]=local[i]; | |
470 | loc[3]=1; | |
471 | ||
472 | TVector &mat = *gMatrix; | |
473 | ||
474 | for (int i=0; i<3; i++) { | |
475 | tmp=0; | |
476 | for (int j=0; j<4; j++) tmp+= mat(i*4+j) * loc[j]; | |
477 | master[i] = tmp; | |
478 | } | |
479 | } | |
480 | else | |
481 | memcpy( master, local, sizeof(Double_t)* kVectorSize ); | |
482 | } | |
483 | ||
484 | //---------------------------------------------------------------------------- | |
485 | ||
486 | void AliGeometry::PushMatrix(TVector* matrix) | |
487 | { | |
488 | //if( fTransformation == NULL ) | |
489 | //fTransformation = new TList(); | |
490 | ||
491 | //cout << " Va a dar error " << endl; | |
492 | if (matrix) fTransformation->AddLast((TObject*)matrix); | |
493 | //cout << " Ves como aqui no llega? :-) " << endl; | |
494 | } | |
495 | ||
496 | //---------------------------------------------------------------------------- | |
497 | ||
498 | TVector* AliGeometry::PopMatrix() | |
499 | { | |
500 | ||
501 | gMatrix = (TVector*)fTransformation->Last(); | |
502 | fTransformation->Remove(fTransformation->Last()); | |
503 | return gMatrix; | |
504 | } | |
505 | ||
506 | //---------------------------------------------------------------------------- | |
507 | ||
508 | ||
509 |