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: AliGNode.cxx */
23 /* PURPOSE: Tree elemnt of the geometry */
25 /* COMPILER: CC for HP-UX 9.x and 10. */
26 /* AUTHOR: Joana && David */
27 /* DATE: May 28, 1999 */
28 /* ADDRESS: jesanto@cern.ch, dcollado@cern.ch */
30 /**********************************************/
34 #include <TPadView3D.h>
38 #include "AliGeometry.h"
41 #include "AliGPCone.h"
42 #include "AliGSphere.h"
45 const Int_t kSonsInvisible = BIT(17);
46 Text_t* ROOT_FILE = "AliGeoDB.root";
50 //----------------------------------------------------------------------
52 AliGNode::AliGNode( AliGNode* node )
56 /* Copy Constructor */
57 fConfig = new AliGConfig( node->fConfig );
59 fMaterial = new AliGMaterial( node->fMaterial );
61 fNcopy = new TArrayI( *(node->fNcopy) );
62 fNname = node->fNname;
63 fNnode = new TArrayI( *(node->fNnode) );
64 fNode = new TObjArray( *(node->fNode) );
65 fNsons = node->fNsons;
66 fNtrans = new TArrayI( *(node->fNtrans) );
67 fParent = node->fParent;
69 fTitle = node->fTitle;
70 fTrans = new TObjArray(*(node->fTrans));
71 // fParent = new AliGNode(node->fParent);
74 if( !strcmp(node->fShape->ClassName(), "AliGBox") ) {
75 fShape = new AliGBox( (AliGBox*) node->fShape );
76 //fShape = new AliGBox(*(AliGBox*)node->fShape);
79 if( !strcmp(node->fShape->ClassName(), "AliGSphere") )
80 fShape = new AliGSphere( (AliGSphere*) node->fShape );
82 if( !strcmp(node->fShape->ClassName(), "AliGTube") )
83 fShape = new AliGTube( (AliGTube*) node->fShape );
85 if( !strcmp(node->fShape->ClassName(), "AliGCone") )
86 fShape = new AliGCone( (AliGCone*) node->fShape );
88 if( !strcmp(node->fShape->ClassName(), "AliGPCone") )
89 fShape = new AliGPCone( (AliGPCone*) node->fShape );
91 if( !strcmp(node->fShape->ClassName(), "AliGTRD1") )
92 fShape = new AliGTRD1( (AliGTRD1*) node->fShape );
102 /*cout << endl << " Comparacion." << endl;
103 cout << " El que paso por parametro:" << endl;
105 cout << " El resultado de la copia:" << endl;
110 /* Default Constructor */
118 fNode = new TObjArray();
124 fTrans = new TObjArray();
129 //----------------------------------------------------------------------
131 AliGNode::AliGNode( Text_t* name, Int_t id, Text_t* title, AliGBox* box, AliGMaterial *material, const Text_t *matrixname, AliGConfig* config )
133 /* AliGNode Constructor for AliGBox shape */
136 fShape = new AliGBox(box); // Shape inside node
140 EndConstructor( name, id, title, material, matrixname, config );
143 //----------------------------------------------------------------------
145 AliGNode::AliGNode( Text_t* name, Int_t id, Text_t* title, AliGSphere* sphere, AliGMaterial *material, const Text_t *matrixname, AliGConfig* config )
147 /* AliGNode Constructor for AliGSphere shape */
150 fShape = new AliGSphere(sphere); // Shape inside node
154 EndConstructor( name, id, title, material, matrixname, config );
157 //----------------------------------------------------------------------
159 AliGNode::AliGNode( Text_t* name, Int_t id, Text_t* title, AliGTube* tube, AliGMaterial *material, const Text_t *matrixname, AliGConfig* config )
161 /* AliGNode Constructor for AliGTube shape */
164 fShape = new AliGTube(tube); // Shape inside node
168 EndConstructor( name, id, title, material, matrixname, config );
171 //----------------------------------------------------------------------
173 AliGNode::AliGNode( Text_t* name, Int_t id, Text_t* title, AliGCone* cone, AliGMaterial *material, const Text_t *matrixname, AliGConfig* config )
175 /* AliGNode Constructor for AliGCone shape */
178 fShape = new AliGCone(cone); // Shape inside node
182 EndConstructor( name, id, title, material, matrixname, config );
185 //----------------------------------------------------------------------
188 AliGNode::AliGNode( Text_t* name, Int_t id, Text_t* title, AliGPCone* pcone, AliGMaterial *material, const Text_t *matrixname, AliGConfig* config )
190 /* AliGNode Constructor for AliGPCone shape */
193 fShape = new AliGPCone(pcone); // Shape inside node
197 EndConstructor( name, id, title, material, matrixname, config );
200 //----------------------------------------------------------------------
203 AliGNode::AliGNode( Text_t* name, Int_t id, Text_t* title, AliGTRD1* trd1, AliGMaterial *material, const Text_t *matrixname, AliGConfig* config )
205 /* AliGNode Constructor for AliGTRD1 shape */
208 fShape = new AliGTRD1(trd1); // Shape inside node
212 EndConstructor( name, id, title, material, matrixname, config );
215 //----------------------------------------------------------------------
217 void AliGNode::EndConstructor( Text_t* name, Int_t id, Text_t* title, AliGMaterial *material, const Text_t *matrixname, AliGConfig* config )
219 /* Finish the construction of AliGNodes */
221 fConfig = new AliGConfig(config);
222 fId = id; // Node id number
223 fMaterial = new AliGMaterial(material); // Material inside shape
225 char* TmpName = new char[strlen(name)+int(TMath::Log10((double) id)+3)];
226 sprintf(TmpName,"%s_%d",name,id);
227 fName = TmpName; // Complete node name
230 fNcopy = new TArrayI(50); // Array of integers containing the copy number (by family name)
231 fNname = name; // Node family name
232 fNnode = new TArrayI(50); // Array of integers containing every node son index
233 fNode = new TObjArray(100,0); // Array containing every (different)node son
235 fNtrans = new TArrayI(50); // Array of integers containing every transformation index
238 fTrans = new TObjArray(100,0); // Array containing every (different)transformation used
243 //-----------------------------------------------------------------------
245 AliGNode::AliGNode( Text_t* name, Int_t id, Text_t* title, AliGShape* shape, AliGMaterial* material, const Text_t* matrixname, AliGConfig* config )
247 /* AliGNode Constructor for an AliGShape */
251 EndConstructor( name, id, title, material, matrixname, config );
255 //----------------------------------------------------------------------
257 AliGNode::~AliGNode()
261 if(fConfig) delete fConfig;
262 if(fMaterial) delete fMaterial;
263 if(fNcopy) delete fNcopy;
264 if(fNnode) delete fNnode;
265 if(fNode) delete fNode;
266 if(fNtrans) delete fNtrans;
267 if(fShape) delete fShape;
268 if(fTrans) delete fTrans;
271 //----------------------------------------------------------------------
273 AliGNode* AliGNode::operator=( const AliGNode* node )
275 if( this == node ) return this; // special case.
277 fConfig = node->fConfig;
279 fMaterial = node->fMaterial;
281 fNcopy = node->fNcopy;
282 fNname = node->fNname;
283 fNnode = node->fNnode;
285 if( fNode ) fNode->Delete();
288 //for( int i=0; i<node->fNode->GetSize(); i++ )
289 for( int i=0; i<node->fNode->GetSize(); i++ )
290 fNode->AddLast(node->fNode->At(i));
293 fNsons = node->fNsons;
294 fNtrans = node->fNtrans;
295 fParent = node->fParent;
296 fShape = node->fShape;
297 fTitle = node->fTitle;
299 if( fTrans ) fTrans->Delete();
301 for( int i=0; i<node->fTrans->GetSize(); i++ )
302 fTrans->AddLast(node->fTrans->At(i));
304 //fTrans = node->fTrans;
309 //----------------------------------------------------------------------
311 void AliGNode::Add(AliGNode* son, AliGTransform* tran)
314 Int_t index = fNode->IndexOf((TObject*)son);
317 fNode->Add(son); // fNode is TObjArray
318 index = fNode->IndexOf((TObject*)son);
322 Int_t tranumber = fTrans->IndexOf((TObject*)tran);
323 if( tranumber == -1 ) {
325 tranumber = fTrans->IndexOf((TObject*)tran);
328 Int_t Nsons = this->GetfNsons();
331 for( Int_t i = 0; i<Nsons; i++ ) {
332 if( index == fNnode->At(i) ) {
333 if( fNtrans->At(i) == tranumber ) {
334 cout << "Error: Node already exists!";
340 if( ((AliGNode*)fNode->At(fNnode->At(i)))->fNname == son->fNname )
345 if( fNnode->GetSize() == Nsons ) {
347 IncreaseSize( Nsons+100, *fNnode );
348 IncreaseSize( Nsons+100, *fNtrans );
349 IncreaseSize( Nsons+100, *fNcopy );
352 fNnode->AddAt( index, Nsons ); // Add value of index at position Nsons
354 fNtrans->AddAt( tranumber, Nsons );
355 fNcopy->AddAt( copy, Nsons );
356 fNsons++; // Because we have added a new node
360 //----------------------------------------------------------------------
362 void AliGNode::IncreaseSize(Int_t total,TArrayI &array)
364 TArrayI *newarray = new TArrayI(array);
367 for( int i=0; i<newarray->GetSize(); i++ )
368 array[i] = (*newarray)[i];
373 //----------------------------------------------------------------------
375 Text_t* AliGNode::GetPath()
377 const int kMAXDEPTH = 128;
378 const AliGNode* d[kMAXDEPTH];
379 AliGNode* node = this;
380 int depth = 0, len = 0;
383 len = strlen(node->GetName()) + 1;
385 while (node->fParent && depth < kMAXDEPTH) {
386 node = node->fParent;
388 len += strlen (node->GetName()) + 1;
391 char* path = new char[len + 2];
393 for (int i = depth-1; i>=0; i--) {
394 if (i == depth-1) { // file or TROOT name
395 strcpy(path, d[i]->GetName());
396 if (i == 0) strcat(path, "/");
400 strcat(path, d[i]->GetName());
408 //----------------------------------------------------------------------
410 void AliGNode::Save( TFile* file )
412 if( fParent ) { // it's not the root node
413 // So I must check if the father node is in disk
415 if( file->cd(fParent->GetPath()) ) {
417 if( !gDirectory->Get(GetName()) )
418 file->mkdir( GetName() );
420 file->cd( GetPath() );
422 if( !gDirectory->Get( fShape->GetName() ) )
424 /* const Text_t* shapetype = fShape->ClassName();
425 if( !strcmp(shapetype,"AliGBox") ) {
429 if (!strcmp(shapetype,"AliGSphere")) {
433 if (!strcmp(shapetype,"AliGTube")) {
437 if (!strcmp(shapetype,"AliGCone")) {
441 if (!strcmp(shapetype,"AliGPCone")) {
442 cout << " Saving an AliGPCone:" << endl;
448 if (!strcmp(shapetype,"AliGTRD1")) {
460 if( !gDirectory->Get( fMaterial->GetName() ) )
465 /*cout << " Con capacidad: " << fConfig->GetFormula().Capacity() << " voy a grabar esta configuracion: " << fConfig->GetFormula().Data() << endl;*/
473 cout << " ERROR : Father exists but I can't find it. See AliGNode::Save, please..." << endl;
475 else { // if it's the root node
476 file->mkdir( GetName() );
477 file->cd( GetPath() );
486 /*cout << " Con capacidad: " << fConfig->GetFormula().Capacity() << " voy a grabar esta configuracion: " << fConfig->GetFormula().Data() << endl;*/
498 //----------------------------------------------------------------------
500 void AliGNode::SaveAll( TFile* file )
503 file->cd( fParent->GetPath() );
509 for( int i=0; i<fNsons; i++ ) {
510 cout << " Saving node: " << GetPath() << " with copy number " << << endl;
511 GetNodeFromfNnode(i)->SaveAll( file );
515 if( !gDirectory->Get(GetName()) ) {
518 for( int i=0; i<fNsons; i++ )
519 GetNodeFromfNnode(i)->SaveAll( file );
522 /* if (!file->cd(GetPath())) {
525 for( int i=0; i<fNsons; i++ )
526 GetNodeFromfNnode(i)->SaveAll( file );
531 //----------------------------------------------------------------------
533 void AliGNode::AddConfig( Text_t* name, Text_t* title, Text_t* detail, Int_t beg, Int_t end )
535 Int_t Nsons = GetfNsons();
537 TStringLong formula = "";
539 for( int i=0; i<Nsons; i++ ) {
540 Int_t index = fNnode->At(i); // Gets every node son's index
541 const Text_t* NodeName = ((AliGNode*)fNode->At(index))->GetName();
542 Int_t tranumber = fNtrans->At(i); // And the correspondent transform
543 const Text_t* TransfName = ((AliGTransform*)fTrans->At(tranumber))->GetName();
545 formula.Append(NodeName);
547 formula.Append(TransfName);
553 //cout << " Acabo de crear la formula: " << formula << endl << endl;
555 const Text_t* shapetype = fShape->ClassName();
556 const Text_t* shapename = fShape->GetName();
557 const Text_t* materialname = fMaterial->GetName();
559 AliGConfig* tmp = new AliGConfig( name, title, formula, detail, shapetype, shapename, materialname, beg, end );
561 //if(fConfig) delete fConfig;
566 //----------------------------------------------------------------------
568 /*void AliGNode::AddConfig( Text_t* name, Text_t* title, Text_t* detail, Int_t beg, Int_t end )
570 // Creates the configuration of a node using the information passed as arguments
571 // and building the formula of the level below by building the list of its sons
574 Int_t Nsons = GetfNsons();
576 char* formula = NULL;
578 cout << " Nsons = " << Nsons << endl;
580 for( int i=0; i<Nsons; i++ ) {
581 Int_t index = fNnode->At(i); // Gets every node son's index
582 const Text_t* NodeName = ((AliGNode*)fNode->At(index))->GetName();
583 Int_t tranumber = fNtrans->At(i); // And the correspondent transform
584 const Text_t* TransfName = ((AliGTransform*)fTrans->At(tranumber))->GetName();
586 int lenF, lenN, lenT;
587 lenF = lenN = lenT = 0;
589 cout << " i = " << i << " and fNsons = " << fNsons << endl;
592 lenF = strlen(formula);
593 cout << " formula = #" << formula << "#" << endl;
597 lenN = strlen(NodeName);
598 cout << " NodeName = #" << NodeName << "#" << endl;
602 lenT = strlen(TransfName);
603 cout << " TransfName = #" << TransfName << "#" << endl;
606 cout << " lenF: " << lenF << " - lenN: " << lenN << " - lenT: " << lenT << endl;
610 if( i != fNsons-1 ) {
611 cout << " Arriba" << endl;
612 formula2 = new char[lenF+lenN+lenT+2];
615 cout << " Embaixo" << endl;
616 formula2 = new char[lenF+lenN+lenT+1];
621 cout << "@@@ :) @@@" << endl;
624 strcpy( formula2, formula );
625 cout << "1 formula2 = #" << formula2 << "#" << endl;
629 strcat( formula2, NodeName );
630 cout << "2 formula2 = #" << formula2 << "#" << endl;
633 strcat( formula2, ":" );
636 strcat( formula2, TransfName );
637 cout << "3 formula2 = #" << formula2 << "#" << endl;
640 if( i != fNsons-1 ) {
641 strcat( formula2, "+" );
642 cout << "4 formula2 = #" << formula2 << "#" << endl;
645 //strcat( formula2, "\x0" );
646 //cout << "5 formula2 = #" << formula2 << "#" << endl;
648 cout << " Y formula2 tiene un tamanyo de: " << strlen(formula2) << " caracteres." << endl;
650 //if( formula ) delete [] formula;
651 formula = new char[strlen(formula2)];
652 strcpy( formula, formula2 );
653 if( formula2 ) delete [] formula2;
656 const Text_t* shapetype = fShape->ClassName();
657 const Text_t* shapename = fShape->GetName();
658 const Text_t* materialname = fMaterial->GetName();
660 AliGConfig* tmp = new AliGConfig( name, title, formula, detail, shapetype, shapename, materialname, beg, end );
662 if(fConfig) delete fConfig;
668 //-------------------------------------------------------------------------
670 void AliGNode::DrawShape(Option_t *option)
676 //----------------------------------------------------------------------
678 void AliGNode::Draw(Option_t* option)
681 TString opt = option;
683 //*-*- Clear pad if option "same" not given
685 if (!gROOT->GetMakeDefCanvas()) return;
686 (gROOT->GetMakeDefCanvas())();
688 if (!opt.Contains("same")) gPad->Clear();
690 //*-*- Draw Referenced node
691 //gGeometry->SetGeomLevel();
692 //gGeometry->UpdateTempMatrix();
696 //*-*- Create a 3-D View
697 TView *view = gPad->GetView();
700 view->SetAutoRange(kTRUE);
702 view->SetAutoRange(kFALSE);
706 //------------------------------------------------------------------
708 void AliGNode::Paint(Option_t *option)
711 TPadView3D *view3D=gPad->GetView3D();
713 //Int_t level = gGeometry->GeomLevel();
714 // restrict the levels for "range" option
715 //if (level > 3 && strcmp(option,"range") == 0) return;
716 //*-*- Update translation vector and rotation matrix for new level
718 // gGeometry->UpdateTempMatrix(fX,fY,fZ,fMatrix->GetMatrix(),fMatrix->IsReflection());
720 // view3D->UpdateNodeMatrix(this,option);
723 //*-*- Paint Referenced shape
724 // Int_t vis = fShape->GetVisibility();
725 // if ( vis == -1) return;
727 // if (nsons == 0) vis = 1;
731 // TAttLine::Modify();
732 // TAttFill::Modify();
733 //if (fVisibility && fShape->GetVisibility()) {
735 //fShape->SetLineColor(GetLineColor());
736 //fShape->SetLineStyle(GetLineStyle());
737 //fShape->SetLineWidth(GetLineWidth());
738 //fShape->SetFillColor(GetFillColor());
739 //fShape->SetFillStyle(GetFillStyle());
742 if (fNnode) nsons = GetfNsons();
744 view3D->SetAttNode((TNode*)this,option);
749 fShape->Paint(option);
751 cout << " Intente dibujar donde no habia shape!!! " << endl;
754 if ( TestBit(kSonsInvisible) ) return;
756 //*-*- Paint all sons
759 //gGeometry->PushLevel();
760 for( int i=0; i<nsons; i++ ) {
762 gAliGeometry->PushMatrix(gMatrix);
764 AliGNode* node = (AliGNode*) fNode->At(fNnode->At(i));
766 AliGTransform* trans = (AliGTransform*) fTrans->At(fNtrans->At(i));
767 gAliGeometry->UpdateMatrix(trans);
769 gAliGeometry->PopMatrix();
772 //gGeometry->PopLevel();
777 //--------------------------------------------------------------------
779 Int_t AliGNode::DistancetoPrimitive(Int_t px, Int_t py)
781 TView *view = gPad->GetView();
782 gPad->SetSelected(view);
786 //--------------------------------------------------------------------