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
164 fHistograms->Clear();
168 // Delete TArray objects
173 //_______________________________________________________________________
174 void AliModule::Copy(AliModule & /* mod */) const
177 // Copy *this onto mod, not implemented for AliModule
179 Fatal("Copy","Not implemented!\n");
182 //_______________________________________________________________________
183 void AliModule::Disable()
186 // Disable Module on viewer
192 // Loop through geometry to disable all
193 // nodes for this Module
194 while((node = dynamic_cast<TNode*>(next()))) {
195 node->SetVisibility(-1);
199 //_______________________________________________________________________
200 Int_t AliModule::DistancetoPrimitive(Int_t, Int_t) const
203 // Return distance from mouse pointer to object
204 // Dummy routine for the moment
209 //_______________________________________________________________________
210 void AliModule::Enable()
213 // Enable Module on the viewver
219 // Loop through geometry to enable all
220 // nodes for this Module
221 while((node = dynamic_cast<TNode*>(next()))) {
222 node->SetVisibility(3);
226 //_______________________________________________________________________
227 void AliModule::AliMaterial(Int_t imat, const char* name, Float_t a,
228 Float_t z, Float_t dens, Float_t radl,
229 Float_t absl, Float_t *buf, Int_t nwbuf) const
232 // Store the parameters for a material
234 // imat the material index will be stored in (*fIdmate)[imat]
235 // name material name
239 // radl radiation length
240 // absl absorbtion length
241 // buf adress of an array user words
242 // nwbuf number of user words
245 gMC->Material(kmat, name, a, z, dens, radl, absl, buf, nwbuf);
246 (*fIdmate)[imat]=kmat;
249 //_______________________________________________________________________
250 void AliModule::AliGetMaterial(Int_t imat, char* name, Float_t &a,
251 Float_t &z, Float_t &dens, Float_t &radl,
255 // Store the parameters for a material
257 // imat the material index will be stored in (*fIdmate)[imat]
258 // name material name
262 // radl radiation length
263 // absl absorbtion length
264 // buf adress of an array user words
265 // nwbuf number of user words
270 kmat=(*fIdmate)[imat];
271 gMC->Gfmate(kmat, name, a, z, dens, radl, absl, buf, nwbuf);
275 //_______________________________________________________________________
276 void AliModule::AliMixture(Int_t imat, const char *name, Float_t *a,
277 Float_t *z, Float_t dens, Int_t nlmat,
281 // Defines mixture or compound imat as composed by
282 // nlmat materials defined by arrays a, z and wmat
284 // If nlmat > 0 wmat contains the proportion by
285 // weights of each basic material in the mixture
287 // If nlmat < 0 wmat contains the number of atoms
288 // of eack kind in the molecule of the compound
289 // In this case, wmat is changed on output to the relative weigths.
291 // imat the material index will be stored in (*fIdmate)[imat]
292 // name material name
293 // a array of atomic masses
294 // z array of atomic numbers
296 // nlmat number of components
297 // wmat array of concentrations
300 gMC->Mixture(kmat, name, a, z, dens, nlmat, wmat);
301 (*fIdmate)[imat]=kmat;
304 //_______________________________________________________________________
305 void AliModule::AliMedium(Int_t numed, const char *name, Int_t nmat,
306 Int_t isvol, Int_t ifield, Float_t fieldm,
307 Float_t tmaxfd, Float_t stemax, Float_t deemax,
308 Float_t epsil, Float_t stmin, Float_t *ubuf,
312 // Store the parameters of a tracking medium
314 // numed the medium number is stored into (*fIdtmed)[numed]
316 // nmat the material number is stored into (*fIdmate)[nmat]
317 // isvol sensitive volume if isvol!=0
318 // ifield magnetic field flag (see below)
319 // fieldm maximum magnetic field
320 // tmaxfd maximum deflection angle due to magnetic field
321 // stemax maximum step allowed
322 // deemax maximum fractional energy loss in one step
323 // epsil tracking precision in cm
324 // stmin minimum step due to continuous processes
326 // ifield = 0 no magnetic field
327 // = -1 user decision in guswim
328 // = 1 tracking performed with Runge Kutta
329 // = 2 tracking performed with helix
330 // = 3 constant magnetic field along z
333 gMC->Medium(kmed,name, (*fIdmate)[nmat], isvol, ifield, fieldm,
334 tmaxfd, stemax, deemax, epsil, stmin, ubuf, nbuf);
335 (*fIdtmed)[numed]=kmed;
338 //_______________________________________________________________________
339 void AliModule::AliMatrix(Int_t &nmat, Float_t theta1, Float_t phi1,
340 Float_t theta2, Float_t phi2, Float_t theta3,
344 // Define a rotation matrix. Angles are in degrees.
346 // nmat on output contains the number assigned to the rotation matrix
347 // theta1 polar angle for axis I
348 // phi1 azimuthal angle for axis I
349 // theta2 polar angle for axis II
350 // phi2 azimuthal angle for axis II
351 // theta3 polar angle for axis III
352 // phi3 azimuthal angle for axis III
354 gMC->Matrix(nmat, theta1, phi1, theta2, phi2, theta3, phi3);
357 //_______________________________________________________________________
358 Float_t AliModule::ZMin() const
363 //_______________________________________________________________________
364 Float_t AliModule::ZMax() const
369 //_______________________________________________________________________
370 void AliModule::SetEuclidFile(char* material, char* geometry)
373 // Sets the name of the Euclid file
375 fEuclidMaterial=material;
377 fEuclidGeometry=geometry;
379 char* name = new char[strlen(material)];
380 strcpy(name,material);
381 strcpy(&name[strlen(name)-4],".euc");
382 fEuclidGeometry=name;
387 //_______________________________________________________________________
388 void AliModule::ReadEuclid(const char* filnam, char* topvol)
391 // read in the geometry of the detector in euclid file format
393 // id_det : the detector identification (2=its,...)
394 // topvol : return parameter describing the name of the top
395 // volume of geometry.
400 // several changes have been made by miroslav helbich
401 // subroutine is rewrited to follow the new established way of memory
402 // booking for tracking medias and rotation matrices.
403 // all used tracking media have to be defined first, for this you can use
404 // subroutine greutmed.
405 // top volume is searched as only volume not positioned into another
408 Int_t i, nvol, iret, itmed, irot, numed, npar, ndiv, iaxe;
409 Int_t ndvmx, nr, flag;
410 char key[5], card[77], natmed[21];
411 char name[5], mother[5], shape[5], konly[5], volst[7000][5];
414 Float_t teta1, phi1, teta2, phi2, teta3, phi3, orig, step;
416 const Int_t kMaxRot=5000;
417 Int_t idrot[kMaxRot],istop[7000];
420 // *** The input filnam name will be with extension '.euc'
421 filtmp=gSystem->ExpandPathName(filnam);
422 lun=fopen(filtmp,"r");
425 Error("ReadEuclid","Could not open file %s\n",filnam);
428 //* --- definition of rotation matrix 0 ---
429 TArrayI &idtmed = *fIdtmed;
430 for(i=1; i<kMaxRot; ++i) idrot[i]=-99;
434 for(i=0;i<77;i++) card[i]=0;
435 iret=fscanf(lun,"%77[^\n]",card);
436 if(iret<=0) goto L20;
441 if (!strcmp(key,"TMED")) {
442 sscanf(&card[5],"%d '%[^']'",&itmed,natmed);
443 if( itmed<0 || itmed>=100 ) {
444 Error("ReadEuclid","TMED illegal medium number %d for %s\n",itmed,natmed);
447 //Pad the string with blanks
450 while(i<20) natmed[i++]=' ';
453 if( idtmed[itmed]<=0 ) {
454 Error("ReadEuclid","TMED undefined medium number %d for %s\n",itmed,natmed);
457 gMC->Gckmat(idtmed[itmed],natmed);
459 } else if (!strcmp(key,"ROTM")) {
460 sscanf(&card[4],"%d %f %f %f %f %f %f",&irot,&teta1,&phi1,&teta2,&phi2,&teta3,&phi3);
461 if( irot<=0 || irot>=kMaxRot ) {
462 Error("ReadEuclid","ROTM rotation matrix number %d illegal\n",irot);
465 AliMatrix(idrot[irot],teta1,phi1,teta2,phi2,teta3,phi3);
467 } else if (!strcmp(key,"VOLU")) {
468 sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, shape, &numed, &npar);
470 for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
473 gMC->Gsvolu( name, shape, idtmed[numed], par, npar);
474 //* save the defined volumes
475 strcpy(volst[++nvol],name);
478 } else if (!strcmp(key,"DIVN")) {
479 sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, mother, &ndiv, &iaxe);
480 gMC->Gsdvn ( name, mother, ndiv, iaxe );
482 } else if (!strcmp(key,"DVN2")) {
483 sscanf(&card[5],"'%[^']' '%[^']' %d %d %f %d",name, mother, &ndiv, &iaxe, &orig, &numed);
484 gMC->Gsdvn2( name, mother, ndiv, iaxe, orig,idtmed[numed]);
486 } else if (!strcmp(key,"DIVT")) {
487 sscanf(&card[5],"'%[^']' '%[^']' %f %d %d %d", name, mother, &step, &iaxe, &numed, &ndvmx);
488 gMC->Gsdvt ( name, mother, step, iaxe, idtmed[numed], ndvmx);
490 } else if (!strcmp(key,"DVT2")) {
491 sscanf(&card[5],"'%[^']' '%[^']' %f %d %f %d %d", name, mother, &step, &iaxe, &orig, &numed, &ndvmx);
492 gMC->Gsdvt2 ( name, mother, step, iaxe, orig, idtmed[numed], ndvmx );
494 } else if (!strcmp(key,"POSI")) {
495 sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']'", name, &nr, mother, &xo, &yo, &zo, &irot, konly);
496 if( irot<0 || irot>=kMaxRot ) {
497 Error("ReadEuclid","POSI %s#%d rotation matrix number %d illegal\n",name,nr,irot);
500 if( idrot[irot] == -99) {
501 Error("ReadEuclid","POSI %s#%d undefined matrix number %d\n",name,nr,irot);
504 //*** volume name cannot be the top volume
505 for(i=1;i<=nvol;i++) {
506 if (!strcmp(volst[i],name)) istop[i]=0;
509 gMC->Gspos ( name, nr, mother, xo, yo, zo, idrot[irot], konly );
511 } else if (!strcmp(key,"POSP")) {
512 sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']' %d", name, &nr, mother, &xo, &yo, &zo, &irot, konly, &npar);
513 if( irot<0 || irot>=kMaxRot ) {
514 Error("ReadEuclid","POSP %s#%d rotation matrix number %d illegal\n",name,nr,irot);
517 if( idrot[irot] == -99) {
518 Error("ReadEuclid","POSP %s#%d undefined matrix number %d\n",name,nr,irot);
522 for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
525 //*** volume name cannot be the top volume
526 for(i=1;i<=nvol;i++) {
527 if (!strcmp(volst[i],name)) istop[i]=0;
530 gMC->Gsposp ( name, nr, mother, xo,yo,zo, idrot[irot], konly, par, npar);
533 if (strcmp(key,"END")) goto L10;
534 //* find top volume in the geometry
536 for(i=1;i<=nvol;i++) {
537 if (istop[i] && flag) {
538 Warning("ReadEuclid"," %s is another possible top volume\n",volst[i]);
540 if (istop[i] && !flag) {
541 strcpy(topvol,volst[i]);
542 if(fDebug) printf("%s::ReadEuclid: volume %s taken as a top volume\n",ClassName(),topvol);
547 Warning("ReadEuclid","top volume not found\n");
551 //* commented out only for the not cernlib version
552 if(fDebug) printf("%s::ReadEuclid: file: %s is now read in\n",ClassName(),filnam);
557 Error("ReadEuclid","reading error or premature end of file\n");
560 //_______________________________________________________________________
561 void AliModule::ReadEuclidMedia(const char* filnam)
564 // read in the materials and tracking media for the detector
565 // in euclid file format
567 // filnam: name of the input file
568 // id_det: id_det is the detector identification (2=its,...)
570 // author : miroslav helbich
572 Float_t sxmgmx = gAlice->Field()->Max();
573 Int_t isxfld = gAlice->Field()->Integ();
574 Int_t end, i, iret, itmed;
575 char key[5], card[130], natmed[21], namate[21];
580 Int_t nwbuf, isvol, ifield, nmat;
581 Float_t a, z, dens, radl, absl, fieldm, tmaxfd, stemax, deemax, epsil, stmin;
584 for(i=0;i<end;i++) if(filnam[i]=='.') {
589 // *** The input filnam name will be with extension '.euc'
590 if(fDebug) printf("%s::ReadEuclid: The file name is %s\n",ClassName(),filnam); //Debug
591 filtmp=gSystem->ExpandPathName(filnam);
592 lun=fopen(filtmp,"r");
595 Warning("ReadEuclidMedia","Could not open file %s\n",filnam);
599 // Retrieve Mag Field parameters
600 Int_t globField=gAlice->Field()->Integ();
601 Float_t globMaxField=gAlice->Field()->Max();
602 // TArrayI &idtmed = *fIdtmed;
605 for(i=0;i<130;i++) card[i]=0;
606 iret=fscanf(lun,"%4s %[^\n]",key,card);
607 if(iret<=0) goto L20;
611 if (!strcmp(key,"MATE")) {
612 sscanf(card,"%d '%[^']' %f %f %f %f %f %d",&imate,namate,&a,&z,&dens,&radl,&absl,&nwbuf);
613 if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
614 //Pad the string with blanks
617 while(i<20) namate[i++]=' ';
620 AliMaterial(imate,namate,a,z,dens,radl,absl,ubuf,nwbuf);
621 //* read tracking medium
622 } else if (!strcmp(key,"TMED")) {
623 sscanf(card,"%d '%[^']' %d %d %d %f %f %f %f %f %f %d",
624 &itmed,natmed,&nmat,&isvol,&ifield,&fieldm,&tmaxfd,
625 &stemax,&deemax,&epsil,&stmin,&nwbuf);
626 if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
627 if (ifield<0) ifield=isxfld;
628 if (fieldm<0) fieldm=sxmgmx;
629 //Pad the string with blanks
632 while(i<20) natmed[i++]=' ';
635 AliMedium(itmed,natmed,nmat,isvol,globField,globMaxField,tmaxfd,
636 stemax,deemax,epsil,stmin,ubuf,nwbuf);
637 // (*fImedia)[idtmed[itmed]-1]=id_det;
641 if (strcmp(key,"END")) goto L10;
644 //* commented out only for the not cernlib version
645 if(fDebug) printf("%s::ReadEuclidMedia: file %s is now read in\n",
651 Warning("ReadEuclidMedia","reading error or premature end of file\n");
654 //_______________________________________________________________________
655 void AliModule::RemapTrackReferencesIDs(Int_t *map)
658 // Remapping track reference
659 // Called at finish primary
661 if (!fTrackReferences) return;
662 for (Int_t i=0;i<fTrackReferences->GetEntries();i++){
663 AliTrackReference * ref = dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(i));
665 Int_t newID = map[ref->GetTrack()];
666 if (newID>=0) ref->SetTrack(newID);
667 else ref->SetTrack(-1);
674 //_______________________________________________________________________
675 AliTrackReference* AliModule::FirstTrackReference(Int_t track)
678 // Initialise the hit iterator
679 // Return the address of the first hit for track
680 // If track>=0 the track is read from disk
681 // while if track<0 the first hit of the current
685 gAlice->ResetTrackReferences();
686 gAlice->TreeTR()->GetEvent(track);
689 fMaxIterTrackRef = fTrackReferences->GetEntriesFast();
690 fCurrentIterTrackRef = 0;
691 if(fMaxIterTrackRef) return dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(0));
695 //_______________________________________________________________________
696 AliTrackReference* AliModule::NextTrackReference()
699 // Return the next hit for the current track
701 if(fMaxIterTrackRef) {
702 if(++fCurrentIterTrackRef<fMaxIterTrackRef)
703 return dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(fCurrentIterTrackRef));
707 printf("* AliDetector::NextTrackReference * TrackReference Iterator called without calling FistTrackReference before\n");
713 //_______________________________________________________________________
714 void AliModule::ResetTrackReferences()
717 // Reset number of hits and the hits array
719 fMaxIterTrackRef = 0;
720 if (fTrackReferences) fTrackReferences->Clear();
725 void AliModule::SetTreeAddress()
728 // Set branch address for the Hits and Digits Trees
732 sprintf(branchname,"%s",GetName());
733 // Branch address for track reference tree
734 TTree *treeTR = gAlice->TreeTR();
735 if (treeTR && fTrackReferences) {
736 branch = treeTR->GetBranch(branchname);
737 if (branch) branch->SetAddress(&fTrackReferences);
741 void AliModule::AddTrackReference(Int_t label){
743 // add a trackrefernce to the list
744 if (!fTrackReferences) {
745 cerr<<"Container trackrefernce not active\n";
748 Int_t nref = fTrackReferences->GetEntriesFast();
749 TClonesArray &lref = *fTrackReferences;
750 new(lref[nref]) AliTrackReference(label);
754 void AliModule::MakeBranchTR(Option_t *option, const char *file)
757 // Makes branch in treeTR
760 sprintf(name,"%s",GetName());
763 printf("* MakeBranch * Making Branch %s \n",name);
765 TDirectory *cwd = gDirectory;
767 TTree* tree = gAlice->TreeTR();
769 branch = tree->Branch(name, &fTrackReferences, 1600);
771 char * outFile = new char[strlen(gAlice->GetBaseFile())+strlen(file)+2];
772 sprintf(outFile,"%s/%s",gAlice->GetBaseFile(),file);
773 branch->SetFile(outFile);
774 TIter next( branch->GetListOfBranches());
775 while ((branch=dynamic_cast<TBranch*>(next()))) {
776 branch->SetFile(outFile);
783 printf("* MakeBranch * Diverting Branch %s to file %s\n",name,file);