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