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 **************************************************************************/
18 Revision 1.3 2002/03/19 09:36:17 morsch
19 Unnecessary includes removes, one included. (Jacek M. Holeczek)
21 Revision 1.2 2001/08/30 09:32:30 hristov
22 The operator[] is replaced by At() or AddAt() in case of TObjArray.
24 Revision 1.1 2001/07/09 11:43:01 morsch
25 Class Responsible for G3 -> Root geometry conversion.
30 #include "AliG3toRoot.h"
31 #include "AliG3Volume.h"
32 #include "AliG3Medium.h"
33 #include "AliG3Material.h"
36 #include <TClonesArray.h>
38 #include <TObjArray.h>
40 #include <TRotMatrix.h>
42 #include <TGeometry.h>
46 AliG3toRoot::AliG3toRoot()
49 TFolder* top = gROOT->GetRootFolder ()->AddFolder ("G3toRoot", "Geometry Tree");
50 fTopFolder=top->AddFolder("ALIC", "Top Volume");
51 gROOT->GetListOfBrowsables ()->Add (top, "G3toRoot Folders");
54 fVolumes = new TClonesArray("AliG3Volume",1000);
55 fMaterials = new TClonesArray("AliG3Material",1000);
56 fMedia = new TClonesArray("AliG3Medium",1000);
60 // Store local copy of zebra bank entries
61 TGeant3 *geant3 = (TGeant3*) gMC;
66 fGclink=geant3->Gclink();
67 fGcnum=geant3->Gcnum();
70 fGeometry = new TGeometry("AliceGeom","Detailed Geometry");
72 void AliG3toRoot::G3toRoot()
77 ConvertToRootShapes();
80 TFolder* topFolder = gROOT->GetRootFolder ()->AddFolder ("G3", "G3");
81 TFolder* geoFolder = topFolder->AddFolder("G3toRoot Shapes", " ");
82 geoFolder->Add(fGeometry);
83 gROOT->GetListOfBrowsables ()->Add(geoFolder, "G3toRoot Shapes");
85 TFile* file = new TFile("g3toRootShapes.root", "recreate");
89 file = new TFile("g3toRootFolders.root", "recreate");
95 void AliG3toRoot::ReadGeometryTree()
98 // Copy zebra volume tree into ROOT LisTree
101 char /* *namec, */ *tmp;
108 Int_t newlevel=nlevel;
109 Float_t mpar[3] = {2000., 2000., 2000.};
112 volume = new AliG3Volume("ALIC");
113 volume->SetIdVolume(((TGeant3*)gMC)->VolId("ALIC"));
114 volume->SetIdCopy(0);
116 volume->SetPosition(0.,0.,0.);
117 volume->SetParameters(3,mpar);
118 volume->SetItem(fTopFolder);
119 (*fVolumes)[0]=volume;
121 // Loop over volumes for which information has been collected
123 for (Int_t i=nst; i<nlevel; i++)
125 TFolder *itemi, *item2;
126 // GEANT3 volume number
127 Int_t ivol=TMath::Abs(Volume(i)->GetIdVolume());
129 // icopy=1 normal positioning
130 // icopy>1 positioning with parposp
132 Int_t icopy = Volume(i)->GetIdCopy();
133 // Medium and material number, handle special case of divisions
138 imat=Material(-ivol);
145 pos = Volume(i)->Position(0);
148 // Number of children
149 Int_t nch = NChildren(ivol);
150 strcpy(namec,((TGeant3*)gMC)->VolName(ivol));
152 printf("\n %s has %d children \n ", namec, nch);
154 printf("\n %s has divisions \n ", namec);
157 // Name to be used in ListTree
159 strncpy(vname,namec, 4);
163 sprintf(namec,"%s*%3dPos",namec,icopy);
164 } else if (icopy <0) {
165 sprintf(namec,"%s*%3dDiv",namec,-icopy);
168 itemi=(TFolder*) Volume(i)->GetItem();
172 Volume(i)->SetName(namec);
173 Volume(i)->SetIdMedium(imed);
174 Volume(i)->SetIdMaterial(imat);
177 // Add volume to list tree
180 item2 = AddVolume(new AliG3Volume(*Volume(i)), itemi, namec);
183 fTopFolder->Add(new AliG3Volume(*Volume(i)));
186 // Collect children information
188 // nch < 0: Children by division
191 // Geant volume number
192 Int_t icvol=Child(ivol,1);
194 strcpy(namec,((TGeant3*)gMC)->VolName(-icvol));
196 strncpy(tmp,(char *) &namec, 4);
197 volume = new AliG3Volume(namec);
198 volume->SetIdVolume(-icvol);
200 Int_t jvo = fZlq[fGclink->jvolum-ivol];
201 Int_t jdiv = fZlq[jvo-1];
203 Int_t iaxe = Int_t (fZq[jdiv+1]);
204 // System volume number
205 // Int_t ivin = Int_t (fZq[jdiv+2]);
206 // Number of divisions
207 Int_t ndiv = Int_t (fZq[jdiv+3]);
209 Float_t startc = fZq[jdiv+4];
211 Float_t step = fZq[jdiv+5];
213 Int_t ish = Volume(i)->Shape();
214 Int_t npar = Volume(i)->NParam();
215 // Copy here shape parameters from mother
216 // Expand divisions later (when needed)
219 Volume(i)->Parameters(0, apar);
220 Float_t* par = new Float_t[npar];
221 for (Int_t jj=0; jj<npar; jj++) par[jj]=apar.At(jj);
222 volume->SetIdCopy(-ndiv);
223 volume->SetDivision(ndiv, iaxe, startc, step);
224 volume->SetParameters(npar, par);
225 volume->SetPosition(0.,0.,0.);
226 volume->SetShape(ish);
227 volume->SetRotMatrix(0);
229 volume->SetItem(item2);
230 (*fVolumes)[newlevel]=volume;
233 // Children by positioning
234 } else if (nch > 0) {
236 // Loop over children
237 for (Int_t j=0; j<nch; j++)
239 Int_t jvo = fZlq[fGclink->jvolum-ivol];
240 Int_t jin = fZlq[jvo-j-1];
241 Int_t ivin = Int_t (fZq[jin + 2 ]);
242 Int_t jvin = fZlq[fGclink->jvolum - ivin];
243 Int_t ish = Int_t (fZq[jvin + 2]);
244 Int_t irot = Int_t(fZq[jin +4]);
245 Float_t x = fZq[jin +5];
246 Float_t y = fZq[jin +6];
247 Float_t z = fZq[jin +7];
248 Int_t ndata = Int_t(fZiq[jin-1]);
252 // Get shape parameters
254 // parp or normal positioning ?
256 npar = Int_t (fZq[jvin+5]);
257 par = new Float_t[npar];
259 for(Int_t jj=0; jj<npar; jj++)
260 par[jj] = fZq[jvin+7+jj];
263 npar = Int_t (fZq[jin+9]);
264 par = new Float_t[npar];
265 for(Int_t jj=0;jj<npar;jj++)
266 par[jj]=fZq[jin+10+jj];
269 // Find out if this volume was already positioned and count copies
270 Int_t icvol=Child(ivol,j+1);
271 icvol = TMath::Abs(icvol);
272 Bool_t inList=kFALSE;
273 AliG3Volume* copy0=0;
275 for (Int_t k=0; k<nnew; k++) {
277 Volume(newlevel-k-1)->GetIdVolume())
279 copy0 = Volume(newlevel-k-1);
280 Volume(newlevel-k-1)->AddCopy();
288 strcpy(namec,((TGeant3*)gMC)->VolName(icvol));
290 strncpy(tmp,(char *) &namec, 4);
291 volume = new AliG3Volume(namec);
292 volume->SetPosition(x,y,z);
293 volume->SetShape(ish);
294 volume->SetParameters(npar, par);
295 volume->SetRotMatrix(irot);
296 if (ndata != 8) volume->SetPosp(kTRUE);
297 volume->SetIdVolume(icvol);
298 volume->SetIdCopy(1);
300 volume->SetItem(item2);
302 (*fVolumes)[newlevel]=volume;
306 copy0->AddCopy(volume);
311 // Move one level deaper
317 void AliG3toRoot::ReadMaterials()
320 // Puts media and material names into ComboBox and
321 // collects material information
323 Float_t a, z, dens, radl, absl;
324 a=z=dens=radl=absl=-1;
326 Int_t imat, isvol, ifield;
327 Float_t fieldm, tmaxfd, stemax, deemax, epsil, stmin;
332 char natmed[21], namate[21];
337 for(Int_t itm=1;itm<=fGcnum->ntmed;itm++) {
338 Int_t jtm = fZlq[fGclink->jtmed-itm];
342 // Get medium parameters
344 Gftmed(itm, natmed, imat, isvol, ifield, fieldm,
345 tmaxfd, stemax, deemax, epsil, stmin, ubuf, &nwbuf);
346 strncpy(natmed,(char*)&fZiq[jtm+1],20);
349 // Create new medium object
350 AliG3Medium * medium =
351 new AliG3Medium(itm, imat, natmed, isvol, ifield, fieldm,
352 tmaxfd, stemax, deemax, epsil, stmin);
353 (*fMedia)[nEntries-1]=medium;
354 { //Begin local scope for j
355 for (Int_t j=1; j<=22; j++) {
356 medium->SetPar(j,Cut(itm,j));
358 } //End local scope for j
359 { //Begin local scope for j
360 for (Int_t j=23; j<=26; j++) {
361 medium->SetPar(j,Cut(itm,j+3));
363 } //End local scope for j
364 { //Begin local scope for j
365 for (Int_t j=27; j<=29; j++) {
366 medium->SetPar(j,Cut(itm,j+4));
368 } //End local scope for j
370 // Associated material
371 imat = Int_t (fZq[jtm+6]);
372 Int_t jma = Int_t (fZlq[fGclink->jmate-imat]);
374 // Get material parameters
375 ((TGeant3*)(gMC))->Gfmate (imat,namate,a,z,dens,radl,absl,par,npar);
376 strncpy(namate,(char *)&fZiq[jma+1],20);
379 // Create new material object
380 AliG3Material* material = new AliG3Material(namate, " ", a,z,dens,radl,absl);
381 material->SetId(imat);
383 (*fMaterials)[nEntries-1] = material;
389 void AliG3toRoot::ReadRotations()
391 // Read rotation matrices
395 if(fGclink->jrotm) nrot = Int_t(fZiq[fGclink->jrotm-2]);
397 fRotations = new TObjArray(nrot);
398 for(irm=1;irm<=nrot;irm++) {
399 jrm = fZlq[fGclink->jrotm-irm];
400 sprintf(name, "R%d", irm);
401 //PH (*fRotations)[irm-1] =
402 //PH new TRotMatrix(name, "Rotation", fZq[jrm+11], fZq[jrm+12], fZq[jrm+13],
403 //PH fZq[jrm+14], fZq[jrm+15], fZq[jrm+16]);
405 new TRotMatrix(name, "Rotation", fZq[jrm+11], fZq[jrm+12], fZq[jrm+13],
406 fZq[jrm+14], fZq[jrm+15], fZq[jrm+16]), irm-1);
411 Int_t AliG3toRoot::NChildren(Int_t idvol)
414 // Return number of children for volume idvol
415 Int_t jvo = fZlq[fGclink->jvolum-idvol];
416 Int_t nin = Int_t(fZq[jvo+3]);
420 Int_t AliG3toRoot::Child(Int_t idvol, Int_t idc)
423 // Return GEANT id of child number idc of volume idvol
424 Int_t jvo = fZlq[fGclink->jvolum-idvol];
426 Int_t jin = fZlq[jvo-nin];
427 Int_t numb = Int_t (fZq[jin +3]);
429 return -Int_t(fZq[jin+2]);
431 return Int_t(fZq[jin+2]);
435 Int_t AliG3toRoot::Medium(Int_t idvol)
438 // Return medium number for volume idvol.
439 // If idvol is negative the volume results from a division.
442 Int_t jvo = fZlq[fGclink->jvolum-idvol];
443 imed = Int_t(fZq[jvo+4]);
446 Int_t jdiv = fZlq[fGclink->jvolum-idvol];
447 Int_t ivin = Int_t ( fZq[jdiv+2]);
448 Int_t jvin = fZlq[fGclink->jvolum-ivin];
449 imed = Int_t (fZq[jvin+4]);
454 Int_t AliG3toRoot::Material(Int_t idvol)
456 // Return material number for volume idvol.
457 // If idvol is negative the volume results from a division.
459 Int_t imed=Medium(idvol);
460 Int_t jtm = fZlq[fGclink->jtmed-imed];
461 return Int_t (fZq[jtm+6]);
465 Float_t AliG3toRoot::Cut(Int_t imed, Int_t icut)
467 // Return cuts icut for medium idmed
469 Int_t jtm = fZlq[fGclink->jtmed-imed];
471 // Have the defaults been modified ??
472 Int_t jtmn = fZlq[jtm];
480 return Float_t (fZq[jtm+icut]);
483 TFolder* AliG3toRoot::AddVolume(TObject * obj, TFolder *parent, const char* name)
485 // Add item to the list tree
486 TFolder* newFolder = parent->AddFolder(name, "volume");
491 AliG3Volume* AliG3toRoot::Volume(Int_t id)
495 return (AliG3Volume *) (fVolumes->UncheckedAt(id));
499 void AliG3toRoot::ConvertToRootShapes(TFolder *item, AliNode** node, Int_t nNodes)
501 // Convert the geometry represented by TFolders into root shapes
516 if (nNodes == 0) nNodes = 1;
518 TList* folders = (TList*) item->GetListOfFolders();
522 volume = ((AliG3Volume *) item->FindObject(item->GetName()));
523 Int_t ncopy = volume->NCopies();
524 AliNode** newnode = new AliNode*[(ncopy+1)*nNodes];
526 for (Int_t ino=0; ino<nNodes; ino++) {
528 pos = volume->Position(0);
529 irot = volume->RotMatrix();
530 imat = volume->Material();
532 sprintf(nameV,"v%s%d", volume->GetName(), npos);
533 sprintf(nameN,"%s%d" , volume->GetName(), npos);
536 imat = volume->Material();
537 Int_t nmat = fMaterials->GetEntriesFast();
538 AliG3Material* mat=0;
540 for (Int_t mati=0; mati<nmat; mati++) {
541 mat = (AliG3Material*)
542 (fMaterials->UncheckedAt(mati));
543 if ((mat->Id())==imat) break;
547 volume->CreateTShape(nameV, mat);
551 newnode[npos] = new AliNode(nameN," ",nameV, pos[0], pos[1], pos[2]);
552 newnode[npos]->SetDivision(volume->Ndiv(), volume->Axis(),
553 volume->Step(), volume->StartC());
554 if (irot > 0) newnode[npos]->SetMatrix((TRotMatrix*) (*fRotations)[irot-1]);
558 new TMaterial("void","Vacuum",0,0,0); //Everything is void
559 TBRIK *brik = new TBRIK("D_alice","alice volume",
560 "void",2000,2000,3000);
561 brik->SetVisibility(0);
562 newnode[npos] = new AliNode("alice","alice","D_alice");
567 for (Int_t icop = 0; icop < ncopy; icop++) {
568 AliG3Volume* copy = volume->Copy(icop);
570 sprintf(nameN,"%s%d" , volume->GetName(), icop+1);
572 sprintf(nameV,"v%s%d", volume->GetName(), icop+1);
573 volume->CreateTShape(nameV, mat);
576 pos = copy->Position(0);
577 irot = copy->RotMatrix();
578 newnode[npos] = new AliNode(nameN," ",nameV, pos[0], pos[1], pos[2]);
579 newnode[npos]->SetDivision(volume->Ndiv(), volume->Axis(),
580 volume->Step(), volume->StartC());
581 if (irot >0) newnode[npos]->SetMatrix((TRotMatrix*) (*fRotations)[irot-1]);
588 while ((obj = next()))
590 if ((AliG3Volume*) obj == volume) continue;
591 item = (TFolder*) obj;
593 ConvertToRootShapes(item, newnode, npos);
596 // Expand divisions of demand
597 if (fExpand) ExpandDivisions();
600 void AliG3toRoot::ExpandDivisions(AliNode* node)
602 // Expand volume divisions
608 node = (AliNode*) fGeometry->GetNode("alice");
611 if (!top && node->Ndiv() > 0 && node->Axis()>0) {
613 node->ExpandDivisions();
614 node->GetParent()->RecursiveRemove(node);
618 TList* sons = node->GetListOfNodes();
622 while((son = (AliNode*)next())) {
623 ExpandDivisions(son);