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.1 2001/07/09 11:43:01 morsch
19 Class Responsible for G3 -> Root geometry conversion.
24 #include "AliG3toRoot.h"
25 #include "AliG3Volume.h"
26 #include "AliG3Medium.h"
27 #include "AliG3Material.h"
31 #include <TClonesArray.h>
33 #include <TObjArray.h>
35 #include <TRotMatrix.h>
37 #include <TGeometry.h>
41 AliG3toRoot::AliG3toRoot()
44 TFolder* top = gROOT->GetRootFolder ()->AddFolder ("G3toRoot", "Geometry Tree");
45 fTopFolder=top->AddFolder("ALIC", "Top Volume");
46 gROOT->GetListOfBrowsables ()->Add (top, "G3toRoot Folders");
49 fVolumes = new TClonesArray("AliG3Volume",1000);
50 fMaterials = new TClonesArray("AliG3Material",1000);
51 fMedia = new TClonesArray("AliG3Medium",1000);
55 // Store local copy of zebra bank entries
56 TGeant3 *geant3 = (TGeant3*) gMC;
61 fGclink=geant3->Gclink();
62 fGcnum=geant3->Gcnum();
65 fGeometry = new TGeometry("AliceGeom","Detailed Geometry");
67 void AliG3toRoot::G3toRoot()
72 ConvertToRootShapes();
75 TFolder* topFolder = gROOT->GetRootFolder ()->AddFolder ("G3", "G3");
76 TFolder* geoFolder = topFolder->AddFolder("G3toRoot Shapes", " ");
77 geoFolder->Add(fGeometry);
78 gROOT->GetListOfBrowsables ()->Add(geoFolder, "G3toRoot Shapes");
80 TFile* file = new TFile("g3toRootShapes.root", "recreate");
84 file = new TFile("g3toRootFolders.root", "recreate");
90 void AliG3toRoot::ReadGeometryTree()
93 // Copy zebra volume tree into ROOT LisTree
96 char /* *namec, */ *tmp;
103 Int_t newlevel=nlevel;
104 Float_t mpar[3] = {2000., 2000., 2000.};
107 volume = new AliG3Volume("ALIC");
108 volume->SetIdVolume(((TGeant3*)gMC)->VolId("ALIC"));
109 volume->SetIdCopy(0);
111 volume->SetPosition(0.,0.,0.);
112 volume->SetParameters(3,mpar);
113 volume->SetItem(fTopFolder);
114 (*fVolumes)[0]=volume;
116 // Loop over volumes for which information has been collected
118 for (Int_t i=nst; i<nlevel; i++)
120 TFolder *itemi, *item2;
121 // GEANT3 volume number
122 Int_t ivol=TMath::Abs(Volume(i)->GetIdVolume());
124 // icopy=1 normal positioning
125 // icopy>1 positioning with parposp
127 Int_t icopy = Volume(i)->GetIdCopy();
128 // Medium and material number, handle special case of divisions
133 imat=Material(-ivol);
140 pos = Volume(i)->Position(0);
143 // Number of children
144 Int_t nch = NChildren(ivol);
145 strcpy(namec,((TGeant3*)gMC)->VolName(ivol));
147 printf("\n %s has %d children \n ", namec, nch);
149 printf("\n %s has divisions \n ", namec);
152 // Name to be used in ListTree
154 strncpy(vname,namec, 4);
158 sprintf(namec,"%s*%3dPos",namec,icopy);
159 } else if (icopy <0) {
160 sprintf(namec,"%s*%3dDiv",namec,-icopy);
163 itemi=(TFolder*) Volume(i)->GetItem();
167 Volume(i)->SetName(namec);
168 Volume(i)->SetIdMedium(imed);
169 Volume(i)->SetIdMaterial(imat);
172 // Add volume to list tree
175 item2 = AddVolume(new AliG3Volume(*Volume(i)), itemi, namec);
178 fTopFolder->Add(new AliG3Volume(*Volume(i)));
181 // Collect children information
183 // nch < 0: Children by division
186 // Geant volume number
187 Int_t icvol=Child(ivol,1);
189 strcpy(namec,((TGeant3*)gMC)->VolName(-icvol));
191 strncpy(tmp,(char *) &namec, 4);
192 volume = new AliG3Volume(namec);
193 volume->SetIdVolume(-icvol);
195 Int_t jvo = fZlq[fGclink->jvolum-ivol];
196 Int_t jdiv = fZlq[jvo-1];
198 Int_t iaxe = Int_t (fZq[jdiv+1]);
199 // System volume number
200 // Int_t ivin = Int_t (fZq[jdiv+2]);
201 // Number of divisions
202 Int_t ndiv = Int_t (fZq[jdiv+3]);
204 Float_t startc = fZq[jdiv+4];
206 Float_t step = fZq[jdiv+5];
208 Int_t ish = Volume(i)->Shape();
209 Int_t npar = Volume(i)->NParam();
210 // Copy here shape parameters from mother
211 // Expand divisions later (when needed)
214 Volume(i)->Parameters(0, apar);
215 Float_t* par = new Float_t[npar];
216 for (Int_t jj=0; jj<npar; jj++) par[jj]=apar.At(jj);
217 volume->SetIdCopy(-ndiv);
218 volume->SetDivision(ndiv, iaxe, startc, step);
219 volume->SetParameters(npar, par);
220 volume->SetPosition(0.,0.,0.);
221 volume->SetShape(ish);
222 volume->SetRotMatrix(0);
224 volume->SetItem(item2);
225 (*fVolumes)[newlevel]=volume;
228 // Children by positioning
229 } else if (nch > 0) {
231 // Loop over children
232 for (Int_t j=0; j<nch; j++)
234 Int_t jvo = fZlq[fGclink->jvolum-ivol];
235 Int_t jin = fZlq[jvo-j-1];
236 Int_t ivin = Int_t (fZq[jin + 2 ]);
237 Int_t jvin = fZlq[fGclink->jvolum - ivin];
238 Int_t ish = Int_t (fZq[jvin + 2]);
239 Int_t irot = Int_t(fZq[jin +4]);
240 Float_t x = fZq[jin +5];
241 Float_t y = fZq[jin +6];
242 Float_t z = fZq[jin +7];
243 Int_t ndata = Int_t(fZiq[jin-1]);
247 // Get shape parameters
249 // parp or normal positioning ?
251 npar = Int_t (fZq[jvin+5]);
252 par = new Float_t[npar];
254 for(Int_t jj=0; jj<npar; jj++)
255 par[jj] = fZq[jvin+7+jj];
258 npar = Int_t (fZq[jin+9]);
259 par = new Float_t[npar];
260 for(Int_t jj=0;jj<npar;jj++)
261 par[jj]=fZq[jin+10+jj];
264 // Find out if this volume was already positioned and count copies
265 Int_t icvol=Child(ivol,j+1);
266 icvol = TMath::Abs(icvol);
267 Bool_t inList=kFALSE;
268 AliG3Volume* copy0=0;
270 for (Int_t k=0; k<nnew; k++) {
272 Volume(newlevel-k-1)->GetIdVolume())
274 copy0 = Volume(newlevel-k-1);
275 Volume(newlevel-k-1)->AddCopy();
283 strcpy(namec,((TGeant3*)gMC)->VolName(icvol));
285 strncpy(tmp,(char *) &namec, 4);
286 volume = new AliG3Volume(namec);
287 volume->SetPosition(x,y,z);
288 volume->SetShape(ish);
289 volume->SetParameters(npar, par);
290 volume->SetRotMatrix(irot);
291 if (ndata != 8) volume->SetPosp(kTRUE);
292 volume->SetIdVolume(icvol);
293 volume->SetIdCopy(1);
295 volume->SetItem(item2);
297 (*fVolumes)[newlevel]=volume;
301 copy0->AddCopy(volume);
306 // Move one level deaper
312 void AliG3toRoot::ReadMaterials()
315 // Puts media and material names into ComboBox and
316 // collects material information
318 Float_t a, z, dens, radl, absl;
319 a=z=dens=radl=absl=-1;
321 Int_t imat, isvol, ifield;
322 Float_t fieldm, tmaxfd, stemax, deemax, epsil, stmin;
327 char natmed[21], namate[21];
332 for(Int_t itm=1;itm<=fGcnum->ntmed;itm++) {
333 Int_t jtm = fZlq[fGclink->jtmed-itm];
337 // Get medium parameters
339 Gftmed(itm, natmed, imat, isvol, ifield, fieldm,
340 tmaxfd, stemax, deemax, epsil, stmin, ubuf, &nwbuf);
341 strncpy(natmed,(char*)&fZiq[jtm+1],20);
344 // Create new medium object
345 AliG3Medium * medium =
346 new AliG3Medium(itm, imat, natmed, isvol, ifield, fieldm,
347 tmaxfd, stemax, deemax, epsil, stmin);
348 (*fMedia)[nEntries-1]=medium;
349 { //Begin local scope for j
350 for (Int_t j=1; j<=22; j++) {
351 medium->SetPar(j,Cut(itm,j));
353 } //End local scope for j
354 { //Begin local scope for j
355 for (Int_t j=23; j<=26; j++) {
356 medium->SetPar(j,Cut(itm,j+3));
358 } //End local scope for j
359 { //Begin local scope for j
360 for (Int_t j=27; j<=29; j++) {
361 medium->SetPar(j,Cut(itm,j+4));
363 } //End local scope for j
365 // Associated material
366 imat = Int_t (fZq[jtm+6]);
367 Int_t jma = Int_t (fZlq[fGclink->jmate-imat]);
369 // Get material parameters
370 ((TGeant3*)(gMC))->Gfmate (imat,namate,a,z,dens,radl,absl,par,npar);
371 strncpy(namate,(char *)&fZiq[jma+1],20);
374 // Create new material object
375 AliG3Material* material = new AliG3Material(namate, " ", a,z,dens,radl,absl);
376 material->SetId(imat);
378 (*fMaterials)[nEntries-1] = material;
384 void AliG3toRoot::ReadRotations()
386 // Read rotation matrices
390 if(fGclink->jrotm) nrot = Int_t(fZiq[fGclink->jrotm-2]);
392 fRotations = new TObjArray(nrot);
393 for(irm=1;irm<=nrot;irm++) {
394 jrm = fZlq[fGclink->jrotm-irm];
395 sprintf(name, "R%d", irm);
396 //PH (*fRotations)[irm-1] =
397 //PH new TRotMatrix(name, "Rotation", fZq[jrm+11], fZq[jrm+12], fZq[jrm+13],
398 //PH fZq[jrm+14], fZq[jrm+15], fZq[jrm+16]);
400 new TRotMatrix(name, "Rotation", fZq[jrm+11], fZq[jrm+12], fZq[jrm+13],
401 fZq[jrm+14], fZq[jrm+15], fZq[jrm+16]), irm-1);
406 Int_t AliG3toRoot::NChildren(Int_t idvol)
409 // Return number of children for volume idvol
410 Int_t jvo = fZlq[fGclink->jvolum-idvol];
411 Int_t nin = Int_t(fZq[jvo+3]);
415 Int_t AliG3toRoot::Child(Int_t idvol, Int_t idc)
418 // Return GEANT id of child number idc of volume idvol
419 Int_t jvo = fZlq[fGclink->jvolum-idvol];
421 Int_t jin = fZlq[jvo-nin];
422 Int_t numb = Int_t (fZq[jin +3]);
424 return -Int_t(fZq[jin+2]);
426 return Int_t(fZq[jin+2]);
430 Int_t AliG3toRoot::Medium(Int_t idvol)
433 // Return medium number for volume idvol.
434 // If idvol is negative the volume results from a division.
437 Int_t jvo = fZlq[fGclink->jvolum-idvol];
438 imed = Int_t(fZq[jvo+4]);
441 Int_t jdiv = fZlq[fGclink->jvolum-idvol];
442 Int_t ivin = Int_t ( fZq[jdiv+2]);
443 Int_t jvin = fZlq[fGclink->jvolum-ivin];
444 imed = Int_t (fZq[jvin+4]);
449 Int_t AliG3toRoot::Material(Int_t idvol)
451 // Return material number for volume idvol.
452 // If idvol is negative the volume results from a division.
454 Int_t imed=Medium(idvol);
455 Int_t jtm = fZlq[fGclink->jtmed-imed];
456 return Int_t (fZq[jtm+6]);
460 Float_t AliG3toRoot::Cut(Int_t imed, Int_t icut)
462 // Return cuts icut for medium idmed
464 Int_t jtm = fZlq[fGclink->jtmed-imed];
466 // Have the defaults been modified ??
467 Int_t jtmn = fZlq[jtm];
475 return Float_t (fZq[jtm+icut]);
478 TFolder* AliG3toRoot::AddVolume(TObject * obj, TFolder *parent, const char* name)
480 // Add item to the list tree
481 TFolder* newFolder = parent->AddFolder(name, "volume");
486 AliG3Volume* AliG3toRoot::Volume(Int_t id)
490 return (AliG3Volume *) (fVolumes->UncheckedAt(id));
494 void AliG3toRoot::ConvertToRootShapes(TFolder *item, AliNode** node, Int_t nNodes)
496 // Convert the geometry represented by TFolders into root shapes
511 if (nNodes == 0) nNodes = 1;
513 TList* folders = (TList*) item->GetListOfFolders();
517 volume = ((AliG3Volume *) item->FindObject(item->GetName()));
518 Int_t ncopy = volume->NCopies();
519 AliNode** newnode = new AliNode*[(ncopy+1)*nNodes];
521 for (Int_t ino=0; ino<nNodes; ino++) {
523 pos = volume->Position(0);
524 irot = volume->RotMatrix();
525 imat = volume->Material();
527 sprintf(nameV,"v%s%d", volume->GetName(), npos);
528 sprintf(nameN,"%s%d" , volume->GetName(), npos);
531 imat = volume->Material();
532 Int_t nmat = fMaterials->GetEntriesFast();
533 AliG3Material* mat=0;
535 for (Int_t mati=0; mati<nmat; mati++) {
536 mat = (AliG3Material*)
537 (fMaterials->UncheckedAt(mati));
538 if ((mat->Id())==imat) break;
542 volume->CreateTShape(nameV, mat);
546 newnode[npos] = new AliNode(nameN," ",nameV, pos[0], pos[1], pos[2]);
547 newnode[npos]->SetDivision(volume->Ndiv(), volume->Axis(),
548 volume->Step(), volume->StartC());
549 if (irot > 0) newnode[npos]->SetMatrix((TRotMatrix*) (*fRotations)[irot-1]);
553 new TMaterial("void","Vacuum",0,0,0); //Everything is void
554 TBRIK *brik = new TBRIK("D_alice","alice volume",
555 "void",2000,2000,3000);
556 brik->SetVisibility(0);
557 newnode[npos] = new AliNode("alice","alice","D_alice");
562 for (Int_t icop = 0; icop < ncopy; icop++) {
563 AliG3Volume* copy = volume->Copy(icop);
565 sprintf(nameN,"%s%d" , volume->GetName(), icop+1);
567 sprintf(nameV,"v%s%d", volume->GetName(), icop+1);
568 volume->CreateTShape(nameV, mat);
571 pos = copy->Position(0);
572 irot = copy->RotMatrix();
573 newnode[npos] = new AliNode(nameN," ",nameV, pos[0], pos[1], pos[2]);
574 newnode[npos]->SetDivision(volume->Ndiv(), volume->Axis(),
575 volume->Step(), volume->StartC());
576 if (irot >0) newnode[npos]->SetMatrix((TRotMatrix*) (*fRotations)[irot-1]);
583 while ((obj = next()))
585 if ((AliG3Volume*) obj == volume) continue;
586 item = (TFolder*) obj;
588 ConvertToRootShapes(item, newnode, npos);
591 // Expand divisions of demand
592 if (fExpand) ExpandDivisions();
595 void AliG3toRoot::ExpandDivisions(AliNode* node)
597 // Expand volume divisions
603 node = (AliNode*) fGeometry->GetNode("alice");
606 if (!top && node->Ndiv() > 0 && node->Axis()>0) {
608 node->ExpandDivisions();
609 node->GetParent()->RecursiveRemove(node);
613 TList* sons = node->GetListOfNodes();
617 while((son = (AliNode*)next())) {
618 ExpandDivisions(son);