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 ///////////////////////////////////////////////////////////////////////////////
20 // Base class for ALICE modules. Both sensitive modules (Modules) and //
21 // non-sensitive ones are described by this base class. This class //
22 // supports the hit and digit trees produced by the simulation and also //
23 // the objects produced by the reconstruction. //
25 // This class is also responsible for building the geometry of the //
30 <img src="picts/AliModuleClass.gif">
34 ///////////////////////////////////////////////////////////////////////////////
36 #include <TObjArray.h>
37 #include <TClonesArray.h>
40 #include <TDirectory.h>
42 #include "AliModule.h"
45 #include "AliConfig.h"
46 #include "AliTrackReference.h"
50 //_______________________________________________________________________
51 AliModule::AliModule():
65 fCurrentIterTrackRef(0)
68 // Default constructor for the AliModule class
72 //_______________________________________________________________________
73 AliModule::AliModule(const char* name,const char *title):
77 fIdtmed(new TArrayI(100)),
78 fIdmate(new TArrayI(100)),
82 fHistograms(new TList()),
86 fTrackReferences(new TClonesArray("AliTrackReference", 100)),
88 fCurrentIterTrackRef(0)
91 // Normal constructor invoked by all Modules.
92 // Create the list for Module specific histograms
93 // Add this Module to the global list of Modules in Run.
95 // Get the Module numeric ID
96 Int_t id = gAlice->GetModuleID(name);
98 // Module already added !
99 Warning("Ctor","Module: %s already present at %d\n",name,id);
103 // Add this Module to the list of Modules
104 gAlice->Modules()->Add(this);
109 // Clear space for tracking media and material indexes
111 for(Int_t i=0;i<100;i++) (*fIdmate)[i]=(*fIdtmed)[i]=0;
113 AliConfig::Instance()->Add(this);
115 SetDebug(gAlice->GetDebug());
118 //_______________________________________________________________________
119 AliModule::AliModule(const AliModule &mod):
137 fCurrentIterTrackRef(0)
145 //_______________________________________________________________________
146 AliModule::~AliModule()
152 // Remove this Module from the list of Modules
154 TObjArray * modules = gAlice->Modules();
155 if (modules) modules->Remove(this);
157 // Delete ROOT geometry
165 fHistograms->Clear();
169 // Delete track references
170 if (fTrackReferences) {
171 fTrackReferences->Delete();
172 delete fTrackReferences;
173 fTrackReferences = 0;
175 // Delete TArray objects
180 //_______________________________________________________________________
181 void AliModule::Copy(AliModule & /* mod */) const
184 // Copy *this onto mod, not implemented for AliModule
186 Fatal("Copy","Not implemented!\n");
189 //_______________________________________________________________________
190 void AliModule::Disable()
193 // Disable Module on viewer
199 // Loop through geometry to disable all
200 // nodes for this Module
201 while((node = dynamic_cast<TNode*>(next()))) {
202 node->SetVisibility(-1);
206 //_______________________________________________________________________
207 Int_t AliModule::DistancetoPrimitive(Int_t, Int_t) const
210 // Return distance from mouse pointer to object
211 // Dummy routine for the moment
216 //_______________________________________________________________________
217 void AliModule::Enable()
220 // Enable Module on the viewver
226 // Loop through geometry to enable all
227 // nodes for this Module
228 while((node = dynamic_cast<TNode*>(next()))) {
229 node->SetVisibility(3);
233 //_______________________________________________________________________
234 void AliModule::AliMaterial(Int_t imat, const char* name, Float_t a,
235 Float_t z, Float_t dens, Float_t radl,
236 Float_t absl, Float_t *buf, Int_t nwbuf) const
239 // Store the parameters for a material
241 // imat the material index will be stored in (*fIdmate)[imat]
242 // name material name
246 // radl radiation length
247 // absl absorbtion length
248 // buf adress of an array user words
249 // nwbuf number of user words
252 gMC->Material(kmat, name, a, z, dens, radl, absl, buf, nwbuf);
253 (*fIdmate)[imat]=kmat;
256 //_______________________________________________________________________
257 void AliModule::AliGetMaterial(Int_t imat, char* name, Float_t &a,
258 Float_t &z, Float_t &dens, Float_t &radl,
262 // Store the parameters for a material
264 // imat the material index will be stored in (*fIdmate)[imat]
265 // name material name
269 // radl radiation length
270 // absl absorbtion length
271 // buf adress of an array user words
272 // nwbuf number of user words
277 kmat=(*fIdmate)[imat];
278 gMC->Gfmate(kmat, name, a, z, dens, radl, absl, buf, nwbuf);
282 //_______________________________________________________________________
283 void AliModule::AliMixture(Int_t imat, const char *name, Float_t *a,
284 Float_t *z, Float_t dens, Int_t nlmat,
288 // Defines mixture or compound imat as composed by
289 // nlmat materials defined by arrays a, z and wmat
291 // If nlmat > 0 wmat contains the proportion by
292 // weights of each basic material in the mixture
294 // If nlmat < 0 wmat contains the number of atoms
295 // of eack kind in the molecule of the compound
296 // In this case, wmat is changed on output to the relative weigths.
298 // imat the material index will be stored in (*fIdmate)[imat]
299 // name material name
300 // a array of atomic masses
301 // z array of atomic numbers
303 // nlmat number of components
304 // wmat array of concentrations
307 gMC->Mixture(kmat, name, a, z, dens, nlmat, wmat);
308 (*fIdmate)[imat]=kmat;
311 //_______________________________________________________________________
312 void AliModule::AliMedium(Int_t numed, const char *name, Int_t nmat,
313 Int_t isvol, Int_t ifield, Float_t fieldm,
314 Float_t tmaxfd, Float_t stemax, Float_t deemax,
315 Float_t epsil, Float_t stmin, Float_t *ubuf,
319 // Store the parameters of a tracking medium
321 // numed the medium number is stored into (*fIdtmed)[numed]
323 // nmat the material number is stored into (*fIdmate)[nmat]
324 // isvol sensitive volume if isvol!=0
325 // ifield magnetic field flag (see below)
326 // fieldm maximum magnetic field
327 // tmaxfd maximum deflection angle due to magnetic field
328 // stemax maximum step allowed
329 // deemax maximum fractional energy loss in one step
330 // epsil tracking precision in cm
331 // stmin minimum step due to continuous processes
333 // ifield = 0 no magnetic field
334 // = -1 user decision in guswim
335 // = 1 tracking performed with Runge Kutta
336 // = 2 tracking performed with helix
337 // = 3 constant magnetic field along z
340 gMC->Medium(kmed,name, (*fIdmate)[nmat], isvol, ifield, fieldm,
341 tmaxfd, stemax, deemax, epsil, stmin, ubuf, nbuf);
342 (*fIdtmed)[numed]=kmed;
345 //_______________________________________________________________________
346 void AliModule::AliMatrix(Int_t &nmat, Float_t theta1, Float_t phi1,
347 Float_t theta2, Float_t phi2, Float_t theta3,
351 // Define a rotation matrix. Angles are in degrees.
353 // nmat on output contains the number assigned to the rotation matrix
354 // theta1 polar angle for axis I
355 // phi1 azimuthal angle for axis I
356 // theta2 polar angle for axis II
357 // phi2 azimuthal angle for axis II
358 // theta3 polar angle for axis III
359 // phi3 azimuthal angle for axis III
361 gMC->Matrix(nmat, theta1, phi1, theta2, phi2, theta3, phi3);
364 //_______________________________________________________________________
365 Float_t AliModule::ZMin() const
370 //_______________________________________________________________________
371 Float_t AliModule::ZMax() const
376 //_______________________________________________________________________
377 void AliModule::SetEuclidFile(char* material, char* geometry)
380 // Sets the name of the Euclid file
382 fEuclidMaterial=material;
384 fEuclidGeometry=geometry;
386 char* name = new char[strlen(material)];
387 strcpy(name,material);
388 strcpy(&name[strlen(name)-4],".euc");
389 fEuclidGeometry=name;
394 //_______________________________________________________________________
395 void AliModule::ReadEuclid(const char* filnam, char* topvol)
398 // read in the geometry of the detector in euclid file format
400 // id_det : the detector identification (2=its,...)
401 // topvol : return parameter describing the name of the top
402 // volume of geometry.
407 // several changes have been made by miroslav helbich
408 // subroutine is rewrited to follow the new established way of memory
409 // booking for tracking medias and rotation matrices.
410 // all used tracking media have to be defined first, for this you can use
411 // subroutine greutmed.
412 // top volume is searched as only volume not positioned into another
415 Int_t i, nvol, iret, itmed, irot, numed, npar, ndiv, iaxe;
416 Int_t ndvmx, nr, flag;
417 char key[5], card[77], natmed[21];
418 char name[5], mother[5], shape[5], konly[5], volst[7000][5];
421 Float_t teta1, phi1, teta2, phi2, teta3, phi3, orig, step;
423 const Int_t kMaxRot=5000;
424 Int_t idrot[kMaxRot],istop[7000];
427 // *** The input filnam name will be with extension '.euc'
428 filtmp=gSystem->ExpandPathName(filnam);
429 lun=fopen(filtmp,"r");
432 Error("ReadEuclid","Could not open file %s\n",filnam);
435 //* --- definition of rotation matrix 0 ---
436 TArrayI &idtmed = *fIdtmed;
437 for(i=1; i<kMaxRot; ++i) idrot[i]=-99;
441 for(i=0;i<77;i++) card[i]=0;
442 iret=fscanf(lun,"%77[^\n]",card);
443 if(iret<=0) goto L20;
448 if (!strcmp(key,"TMED")) {
449 sscanf(&card[5],"%d '%[^']'",&itmed,natmed);
450 if( itmed<0 || itmed>=100 ) {
451 Error("ReadEuclid","TMED illegal medium number %d for %s\n",itmed,natmed);
454 //Pad the string with blanks
457 while(i<20) natmed[i++]=' ';
460 if( idtmed[itmed]<=0 ) {
461 Error("ReadEuclid","TMED undefined medium number %d for %s\n",itmed,natmed);
464 gMC->Gckmat(idtmed[itmed],natmed);
466 } else if (!strcmp(key,"ROTM")) {
467 sscanf(&card[4],"%d %f %f %f %f %f %f",&irot,&teta1,&phi1,&teta2,&phi2,&teta3,&phi3);
468 if( irot<=0 || irot>=kMaxRot ) {
469 Error("ReadEuclid","ROTM rotation matrix number %d illegal\n",irot);
472 AliMatrix(idrot[irot],teta1,phi1,teta2,phi2,teta3,phi3);
474 } else if (!strcmp(key,"VOLU")) {
475 sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, shape, &numed, &npar);
477 for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
480 gMC->Gsvolu( name, shape, idtmed[numed], par, npar);
481 //* save the defined volumes
482 strcpy(volst[++nvol],name);
485 } else if (!strcmp(key,"DIVN")) {
486 sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, mother, &ndiv, &iaxe);
487 gMC->Gsdvn ( name, mother, ndiv, iaxe );
489 } else if (!strcmp(key,"DVN2")) {
490 sscanf(&card[5],"'%[^']' '%[^']' %d %d %f %d",name, mother, &ndiv, &iaxe, &orig, &numed);
491 gMC->Gsdvn2( name, mother, ndiv, iaxe, orig,idtmed[numed]);
493 } else if (!strcmp(key,"DIVT")) {
494 sscanf(&card[5],"'%[^']' '%[^']' %f %d %d %d", name, mother, &step, &iaxe, &numed, &ndvmx);
495 gMC->Gsdvt ( name, mother, step, iaxe, idtmed[numed], ndvmx);
497 } else if (!strcmp(key,"DVT2")) {
498 sscanf(&card[5],"'%[^']' '%[^']' %f %d %f %d %d", name, mother, &step, &iaxe, &orig, &numed, &ndvmx);
499 gMC->Gsdvt2 ( name, mother, step, iaxe, orig, idtmed[numed], ndvmx );
501 } else if (!strcmp(key,"POSI")) {
502 sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']'", name, &nr, mother, &xo, &yo, &zo, &irot, konly);
503 if( irot<0 || irot>=kMaxRot ) {
504 Error("ReadEuclid","POSI %s#%d rotation matrix number %d illegal\n",name,nr,irot);
507 if( idrot[irot] == -99) {
508 Error("ReadEuclid","POSI %s#%d undefined matrix number %d\n",name,nr,irot);
511 //*** volume name cannot be the top volume
512 for(i=1;i<=nvol;i++) {
513 if (!strcmp(volst[i],name)) istop[i]=0;
516 gMC->Gspos ( name, nr, mother, xo, yo, zo, idrot[irot], konly );
518 } else if (!strcmp(key,"POSP")) {
519 sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']' %d", name, &nr, mother, &xo, &yo, &zo, &irot, konly, &npar);
520 if( irot<0 || irot>=kMaxRot ) {
521 Error("ReadEuclid","POSP %s#%d rotation matrix number %d illegal\n",name,nr,irot);
524 if( idrot[irot] == -99) {
525 Error("ReadEuclid","POSP %s#%d undefined matrix number %d\n",name,nr,irot);
529 for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
532 //*** volume name cannot be the top volume
533 for(i=1;i<=nvol;i++) {
534 if (!strcmp(volst[i],name)) istop[i]=0;
537 gMC->Gsposp ( name, nr, mother, xo,yo,zo, idrot[irot], konly, par, npar);
540 if (strcmp(key,"END")) goto L10;
541 //* find top volume in the geometry
543 for(i=1;i<=nvol;i++) {
544 if (istop[i] && flag) {
545 Warning("ReadEuclid"," %s is another possible top volume\n",volst[i]);
547 if (istop[i] && !flag) {
548 strcpy(topvol,volst[i]);
549 if(fDebug) printf("%s::ReadEuclid: volume %s taken as a top volume\n",ClassName(),topvol);
554 Warning("ReadEuclid","top volume not found\n");
558 //* commented out only for the not cernlib version
559 if(fDebug) printf("%s::ReadEuclid: file: %s is now read in\n",ClassName(),filnam);
564 Error("ReadEuclid","reading error or premature end of file\n");
567 //_______________________________________________________________________
568 void AliModule::ReadEuclidMedia(const char* filnam)
571 // read in the materials and tracking media for the detector
572 // in euclid file format
574 // filnam: name of the input file
575 // id_det: id_det is the detector identification (2=its,...)
577 // author : miroslav helbich
579 Float_t sxmgmx = gAlice->Field()->Max();
580 Int_t isxfld = gAlice->Field()->Integ();
581 Int_t end, i, iret, itmed;
582 char key[5], card[130], natmed[21], namate[21];
587 Int_t nwbuf, isvol, ifield, nmat;
588 Float_t a, z, dens, radl, absl, fieldm, tmaxfd, stemax, deemax, epsil, stmin;
591 for(i=0;i<end;i++) if(filnam[i]=='.') {
596 // *** The input filnam name will be with extension '.euc'
597 if(fDebug) printf("%s::ReadEuclid: The file name is %s\n",ClassName(),filnam); //Debug
598 filtmp=gSystem->ExpandPathName(filnam);
599 lun=fopen(filtmp,"r");
602 Warning("ReadEuclidMedia","Could not open file %s\n",filnam);
606 // Retrieve Mag Field parameters
607 Int_t globField=gAlice->Field()->Integ();
608 Float_t globMaxField=gAlice->Field()->Max();
609 // TArrayI &idtmed = *fIdtmed;
612 for(i=0;i<130;i++) card[i]=0;
613 iret=fscanf(lun,"%4s %[^\n]",key,card);
614 if(iret<=0) goto L20;
618 if (!strcmp(key,"MATE")) {
619 sscanf(card,"%d '%[^']' %f %f %f %f %f %d",&imate,namate,&a,&z,&dens,&radl,&absl,&nwbuf);
620 if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
621 //Pad the string with blanks
624 while(i<20) namate[i++]=' ';
627 AliMaterial(imate,namate,a,z,dens,radl,absl,ubuf,nwbuf);
628 //* read tracking medium
629 } else if (!strcmp(key,"TMED")) {
630 sscanf(card,"%d '%[^']' %d %d %d %f %f %f %f %f %f %d",
631 &itmed,natmed,&nmat,&isvol,&ifield,&fieldm,&tmaxfd,
632 &stemax,&deemax,&epsil,&stmin,&nwbuf);
633 if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
634 if (ifield<0) ifield=isxfld;
635 if (fieldm<0) fieldm=sxmgmx;
636 //Pad the string with blanks
639 while(i<20) natmed[i++]=' ';
642 AliMedium(itmed,natmed,nmat,isvol,globField,globMaxField,tmaxfd,
643 stemax,deemax,epsil,stmin,ubuf,nwbuf);
644 // (*fImedia)[idtmed[itmed]-1]=id_det;
648 if (strcmp(key,"END")) goto L10;
651 //* commented out only for the not cernlib version
652 if(fDebug) printf("%s::ReadEuclidMedia: file %s is now read in\n",
658 Warning("ReadEuclidMedia","reading error or premature end of file\n");
661 //_______________________________________________________________________
662 void AliModule::RemapTrackReferencesIDs(Int_t *map)
665 // Remapping track reference
666 // Called at finish primary
668 if (!fTrackReferences) return;
669 for (Int_t i=0;i<fTrackReferences->GetEntries();i++){
670 AliTrackReference * ref = dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(i));
672 Int_t newID = map[ref->GetTrack()];
673 if (newID>=0) ref->SetTrack(newID);
674 else ref->SetTrack(-1);
681 //_______________________________________________________________________
682 AliTrackReference* AliModule::FirstTrackReference(Int_t track)
685 // Initialise the hit iterator
686 // Return the address of the first hit for track
687 // If track>=0 the track is read from disk
688 // while if track<0 the first hit of the current
692 gAlice->ResetTrackReferences();
693 gAlice->TreeTR()->GetEvent(track);
696 fMaxIterTrackRef = fTrackReferences->GetEntriesFast();
697 fCurrentIterTrackRef = 0;
698 if(fMaxIterTrackRef) return dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(0));
702 //_______________________________________________________________________
703 AliTrackReference* AliModule::NextTrackReference()
706 // Return the next hit for the current track
708 if(fMaxIterTrackRef) {
709 if(++fCurrentIterTrackRef<fMaxIterTrackRef)
710 return dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(fCurrentIterTrackRef));
714 printf("* AliDetector::NextTrackReference * TrackReference Iterator called without calling FistTrackReference before\n");
720 //_______________________________________________________________________
721 void AliModule::ResetTrackReferences()
724 // Reset number of hits and the hits array
726 fMaxIterTrackRef = 0;
727 if (fTrackReferences) fTrackReferences->Clear();
732 void AliModule::SetTreeAddress()
735 // Set branch address for the Hits and Digits Trees
739 sprintf(branchname,"%s",GetName());
740 // Branch address for track reference tree
741 TTree *treeTR = gAlice->TreeTR();
742 if (treeTR && fTrackReferences) {
743 branch = treeTR->GetBranch(branchname);
744 if (branch) branch->SetAddress(&fTrackReferences);
748 void AliModule::AddTrackReference(Int_t label){
750 // add a trackrefernce to the list
751 if (!fTrackReferences) {
752 cerr<<"Container trackrefernce not active\n";
755 Int_t nref = fTrackReferences->GetEntriesFast();
756 TClonesArray &lref = *fTrackReferences;
757 new(lref[nref]) AliTrackReference(label);
761 void AliModule::MakeBranchTR(Option_t *option, const char *file)
764 // Makes branch in treeTR
767 sprintf(name,"%s",GetName());
770 printf("* MakeBranch * Making Branch %s \n",name);
772 TDirectory *cwd = gDirectory;
774 TTree* tree = gAlice->TreeTR();
776 branch = tree->Branch(name, &fTrackReferences, 1600);
778 char * outFile = new char[strlen(gAlice->GetBaseFile())+strlen(file)+2];
779 sprintf(outFile,"%s/%s",gAlice->GetBaseFile(),file);
780 branch->SetFile(outFile);
781 TIter next( branch->GetListOfBranches());
782 while ((branch=dynamic_cast<TBranch*>(next()))) {
783 branch->SetFile(outFile);
790 printf("* MakeBranch * Diverting Branch %s to file %s\n",name,file);