1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
20 /**********************************************/
22 /* FILE: AliGeometry.cxx */
23 /* PURPOSE: To store and retrieve a complete */
24 /* geometry in/from a root file. */
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 */
31 /**********************************************/
36 #include "AliGConfig.h"
37 #include "AliGeometry.h"
39 #include "AliGSphere.h"
42 #include "AliGTransform.h"
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 );
48 TVector *gMatrix = new TVector(0,15,1.,0.,0.,0.,0.,1.,0.,0.,0.,0.,1.,0.,0.,0.,0.,1., "END");
49 AliGeometry *gAliGeometry = 0;
54 // ---------------------------------------------------------------------------
56 AliGeometry::AliGeometry( AliGeometry* Geom )
59 /* Copy Constructor */
60 fBomb = Geom->fBomb; //Bomb factor for exploded geometry
61 fGeomLevel = Geom->fGeomLevel;
62 fMaxDepth = Geom->fMaxDepth;
67 for( int i=0; i<Geom->fRules->GetSize(); i++ )
68 fRules->AddLast(Geom->fRules->At(i));
71 fTitle = Geom->fTitle;
72 fTopNode = Geom->fTopNode;
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));
82 /* Default constructor */
83 fBomb = 1.; //Bomb factor for exploded geometry
85 fMaxDepth = 0; // 0 = No Depth limitation
87 fRules = new TList(); // Antes estaba a NULL
90 fTransformation = new TList(); // Antes estaba a NULL
95 // ---------------------------------------------------------------------------
97 AliGeometry::~AliGeometry()
106 if( fTransformation ) {
107 fTransformation->Delete();
108 fTransformation = NULL;
112 // ---------------------------------------------------------------------------
114 AliGeometry* AliGeometry::operator=( const AliGeometry* Geom )
117 if( this == Geom ) return this; // special case.
119 fBomb = Geom->fBomb; //Bomb factor for exploded geometry
120 fGeomLevel = Geom->fGeomLevel;
121 fMaxDepth = Geom->fMaxDepth;
129 for( int i=0; i<Geom->fRules->GetSize(); i++ )
130 fRules->AddLast(Geom->fRules->At(i));
133 fTitle = Geom->fTitle;
134 fTopNode = Geom->fTopNode;
136 if( fTransformation )
137 fTransformation->Delete();
139 fTransformation = new TList();
141 if( Geom->fTransformation ) {
142 for( int i=0; i<Geom->fTransformation->GetSize(); i++ )
143 fTransformation->AddLast(Geom->fTransformation->At(i));
149 // ---------------------------------------------------------------------------
151 AliGNode* AliGeometry::FileToMemTree( TFile* file )
153 // Geometry constructor that retrieves a whole geometry from the root file
154 // and stores it in memory as a tree structure
156 // Read the name of my geometry's first node
158 const char* node_name = (const char*) GetTop().Data();
161 AliGNode* new_node = new AliGNode( RecurToMem( node_name, file, NULL, NULL ) );
166 // ---------------------------------------------------------------------------
168 AliGNode* AliGeometry::RecurToMem( const char* node_name, TFile* file, AliGNode* father, char *subtransf )
170 // Recursive function used to retrieve the root_file structure into memory
172 const int LenName = strlen(node_name);
175 for( i=0; i<LenName; i++ )
176 if( node_name[i] == '_' )
179 char* name = new char[i+1];
180 strncpy( name, node_name, i );
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];
187 ids[LenName-i-1] = '\x0';
189 const int id = atoi( ids );
193 AliGNode* node = NULL;
198 AliGTransform* trans = NULL;
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()) ) {
208 node = new AliGNode( (Text_t*)name, id, (Text_t*)name, (AliGBox*) NULL );
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())) {
220 trans = new AliGTransform();
221 file->cd(father->GetPath());
222 trans->Read(subtransf);
226 if ( l == 0 ) node = new AliGNode( (Text_t*)name, id, (Text_t*)name, (AliGBox*) NULL );
228 trans = new AliGTransform();
229 trans->Read(subtransf);
234 father->Add(node,father->GetTransFromfTrans(m));
235 file->cd(node->GetPath());
238 father->Add(node,trans);
239 file->cd(node->GetPath());
244 father->Add(father->GetNodeFromfNode(l),father->GetTransFromfTrans(m));
245 file->cd(father->GetPath());
248 father->Add(father->GetNodeFromfNode(l),trans);
249 file->cd(father->GetPath());
253 //if( trans ) delete trans;
255 else { // There is no father
256 node = new AliGNode( (Text_t*)name, id, (Text_t*)name, (AliGBox*) NULL );
257 file->cd(node->GetPath());
264 // Read the configuration name
265 char* configs = NULL;
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() );
273 for( w=0; w<strlen(rule); w++ )
277 configs = new char[w+1];
278 strncpy( configs, rule, w );
281 if( !strcmp(node->GetName(), configs) ) {
294 printf( " ERROR: I couldn't find the fRule inside RecurToMem.\n" );
298 // Build Configuration
299 AliGConfig* Config = new AliGConfig();
301 Config->Read(configs);
302 node->AddConf(Config);
304 // Read Shape, material and formula from configuration
305 const Text_t* shapetype = Config->GetShapeType().Data();
306 const Text_t* shapename = Config->GetShapeName().Data();
310 if( !strcmp(shapetype,"AliGBox") ) {
311 AliGBox* shape = new AliGBox();
312 shape->Read(shapename);
313 node->AddShape(shape);
316 if (!strcmp(shapetype,"AliGSphere")) {
317 AliGSphere* shape = new AliGSphere();
318 shape->Read(shapename);
319 node->AddShape(shape);
322 if (!strcmp(shapetype,"AliGTube")) {
323 AliGTube* shape = new AliGTube();
324 shape->Read(shapename);
325 node->AddShape(shape);
328 if (!strcmp(shapetype,"AliGCone")) {
329 AliGCone* shape = new AliGCone();
330 shape->Read(shapename);
331 node->AddShape(shape);
334 if (!strcmp(shapetype,"AliGPCone")) {
335 AliGPCone* shape = new AliGPCone();
336 shape->Read(shapename);
337 node->AddShape(shape);
340 if (!strcmp(shapetype,"AliGTRD1")) {
341 AliGTRD1* shape = new AliGTRD1();
342 shape->Read(shapename);
343 node->AddShape(shape);
352 const Text_t* materialname = Config->GetMaterialName().Data();
353 AliGMaterial *material = new AliGMaterial();
354 material->Read(materialname);
355 node->AddMaterial(material);
358 const char* formula = Config->GetFormula();
360 int Len = strlen(formula);
363 while( formula[j] ) {
364 char* subnode = new char[Len];
365 char* subtransf = new char[Len];
368 for( k=0; formula[j] != ':'; k++, j++ )
369 subnode[k] = formula[j];
372 if( formula[j] ) j++;
374 for( k=0; formula[j] && formula[j] != '+'; k++, j++ )
375 subtransf[k] = formula[j];
377 subtransf[k] = '\x0';
378 if( formula[j] ) j++;
380 AliGNode* node1 = this->RecurToMem( subnode, file, node, subtransf );
387 // ---------------------------------------------------------------------------
389 AliGeometry::AliGeometry( Text_t* name, Text_t* title, AliGNode* topNode, int maxDepth ): TNamed(name, title)
391 /* Geometry constructor that stores in the root file the geometry file that corresponds to one geometry */
394 fBomb = 1.; //Bomb factor for exploded geometry
396 fMaxDepth = maxDepth;
397 fRules = new TList();
398 fTopNode = topNode->GetName();
399 fTransformation = new TList();
401 RulesList( fRules, topNode, 0 );
405 // ---------------------------------------------------------------------------
407 void AliGeometry::RulesList( TList *fRules, AliGNode* node, int position )
409 /* Recursive function used to create the list of rules for the Geometry file */
411 int len = strlen(node->GetName()) + strlen(node->GetConfig()->GetName());
413 char* path = new char[len+2];
414 strcpy( path, node->GetName() );
416 strcat( path, node->GetConfig()->GetName());
417 TNamed* rule = new TNamed( path, "" );
420 fRules->AddLast( rule );
421 //cout << " Rule: " << rule->GetName() << " added." << endl;
422 //if( rule ) delete rule;
424 while( (position < node->SizefNode()) && (node->GetNodeFromfNode(position) != NULL) ) {
425 this->RulesList( fRules, node->GetNodeFromfNode(position), 0 );
431 // ---------------------------------------------------------------------------
433 void AliGeometry::UpdateMatrix(AliGTransform* trans)
435 // Update global rotation matrix/translation vector for this node
436 // this function must be called before invoking Local2Master
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");
442 // printf("Inside updatematrix, this is the son's matrix\n");
444 // printf("Inside updatematrix, this is the global matrix before\n");
446 /* Multiplication of matrices*/
447 for( int i=0; i<4; i++ )
448 for( int j=0; j<4; j++ ) {
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);
455 //if(gMatrix) delete gMatrix;
458 // printf("Inside updatematrix, this is the global matrix after\n");
461 // for (int k = 0; k < 16; k++) {
462 //cout << "gMatrix[" << k << "]" << (*gMatrix)(k) << endl;
463 //cout << "matrix[" << k << "]" << (*matrix)(k) << endl;
466 //if (temp) delete [] temp;
467 //if (matrix) delete [] matrix;
471 // ---------------------------------------------------------------------------
473 void AliGeometry::Local2Master(Float_t *local, Float_t *master)
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.
483 if( gNode->GetParent() && gMatrix) {
488 for(int i=0;i<3;i++) loc[i]=local[i];
491 TVector &mat = *gMatrix;
493 for (int i=0; i<3; i++) {
495 for (int j=0; j<4; j++) tmp+= mat(i*4+j) * loc[j];
500 memcpy( master, local, sizeof(Double_t)* kVectorSize );
503 //----------------------------------------------------------------------------
505 void AliGeometry::PushMatrix(TVector* matrix)
507 //if( fTransformation == NULL )
508 //fTransformation = new TList();
510 //cout << " Va a dar error " << endl;
511 if (matrix) fTransformation->AddLast((TObject*)matrix);
512 //cout << " Ves como aqui no llega? :-) " << endl;
515 //----------------------------------------------------------------------------
517 TVector* AliGeometry::PopMatrix()
520 gMatrix = (TVector*)fTransformation->Last();
521 fTransformation->Remove(fTransformation->Last());
525 //----------------------------------------------------------------------------