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 ///////////////////////////////////////////////////////////////////////////////
37 #include <TObjArray.h>
38 #include <TClonesArray.h>
41 #include <TDirectory.h>
42 #include <TVirtualMC.h>
45 #include "AliConfig.h"
46 #include "AliLoader.h"
48 #include "AliModule.h"
50 #include "AliTrackReference.h"
52 #include "../RAW/AliRawDataHeader.h"
56 //_______________________________________________________________________
57 AliModule::AliModule():
71 fCurrentIterTrackRef(0),
75 // Default constructor for the AliModule class
79 //_______________________________________________________________________
80 AliModule::AliModule(const char* name,const char *title):
84 fIdtmed(new TArrayI(100)),
85 fIdmate(new TArrayI(100)),
89 fHistograms(new TList()),
93 fTrackReferences(new TClonesArray("AliTrackReference", 100)),
95 fCurrentIterTrackRef(0),
99 // Normal constructor invoked by all Modules.
100 // Create the list for Module specific histograms
101 // Add this Module to the global list of Modules in Run.
103 // Get the Module numeric ID
104 Int_t id = gAlice->GetModuleID(name);
106 // Module already added !
107 AliWarning(Form("Module: %s already present at %d",name,id));
111 // Add this Module to the list of Modules
113 gAlice->AddModule(this);
117 // Clear space for tracking media and material indexes
119 for(Int_t i=0;i<100;i++) (*fIdmate)[i]=(*fIdtmed)[i]=0;
122 //_______________________________________________________________________
123 AliModule::AliModule(const AliModule &mod):
141 fCurrentIterTrackRef(0),
150 //_______________________________________________________________________
151 AliModule::~AliModule()
157 // Remove this Module from the list of Modules
159 TObjArray * modules = gAlice->Modules();
160 if (modules) modules->Remove(this);
162 // Delete ROOT geometry
170 fHistograms->Clear();
174 // Delete track references
175 if (fTrackReferences) {
176 fTrackReferences->Delete();
177 delete fTrackReferences;
178 fTrackReferences = 0;
180 // Delete TArray objects
186 //_______________________________________________________________________
187 void AliModule::Copy(TObject & /* mod */) const
190 // Copy *this onto mod, not implemented for AliModule
192 AliFatal("Not implemented!");
195 //_______________________________________________________________________
196 void AliModule::Disable()
199 // Disable Module on viewer
205 // Loop through geometry to disable all
206 // nodes for this Module
207 while((node = dynamic_cast<TNode*>(next()))) {
208 node->SetVisibility(-1);
212 //_______________________________________________________________________
213 void AliModule::Enable()
216 // Enable Module on the viewver
222 // Loop through geometry to enable all
223 // nodes for this Module
224 while((node = dynamic_cast<TNode*>(next()))) {
225 node->SetVisibility(3);
229 //_______________________________________________________________________
230 void AliModule::AliMaterial(Int_t imat, const char* name, Float_t a,
231 Float_t z, Float_t dens, Float_t radl,
232 Float_t absl, Float_t *buf, Int_t nwbuf) const
235 // Store the parameters for a material
237 // imat the material index will be stored in (*fIdmate)[imat]
238 // name material name
242 // radl radiation length
243 // absl absorbtion length
244 // buf adress of an array user words
245 // nwbuf number of user words
248 gMC->Material(kmat, name, a, z, dens, radl, absl, buf, nwbuf);
249 (*fIdmate)[imat]=kmat;
252 //_______________________________________________________________________
253 void AliModule::AliGetMaterial(Int_t imat, char* name, Float_t &a,
254 Float_t &z, Float_t &dens, Float_t &radl,
258 // Store the parameters for a material
260 // imat the material index will be stored in (*fIdmate)[imat]
261 // name material name
265 // radl radiation length
266 // absl absorbtion length
267 // buf adress of an array user words
268 // nwbuf number of user words
273 kmat=(*fIdmate)[imat];
274 gMC->Gfmate(kmat, name, a, z, dens, radl, absl, buf, nwbuf);
278 //_______________________________________________________________________
279 void AliModule::AliMixture(Int_t imat, const char *name, Float_t *a,
280 Float_t *z, Float_t dens, Int_t nlmat,
284 // Defines mixture or compound imat as composed by
285 // nlmat materials defined by arrays a, z and wmat
287 // If nlmat > 0 wmat contains the proportion by
288 // weights of each basic material in the mixture
290 // If nlmat < 0 wmat contains the number of atoms
291 // of eack kind in the molecule of the compound
292 // In this case, wmat is changed on output to the relative weigths.
294 // imat the material index will be stored in (*fIdmate)[imat]
295 // name material name
296 // a array of atomic masses
297 // z array of atomic numbers
299 // nlmat number of components
300 // wmat array of concentrations
303 gMC->Mixture(kmat, name, a, z, dens, nlmat, wmat);
304 (*fIdmate)[imat]=kmat;
307 //_______________________________________________________________________
308 void AliModule::AliMedium(Int_t numed, const char *name, Int_t nmat,
309 Int_t isvol, Int_t ifield, Float_t fieldm,
310 Float_t tmaxfd, Float_t stemax, Float_t deemax,
311 Float_t epsil, Float_t stmin, Float_t *ubuf,
315 // Store the parameters of a tracking medium
317 // numed the medium number is stored into (*fIdtmed)[numed]
319 // nmat the material number is stored into (*fIdmate)[nmat]
320 // isvol sensitive volume if isvol!=0
321 // ifield magnetic field flag (see below)
322 // fieldm maximum magnetic field
323 // tmaxfd maximum deflection angle due to magnetic field
324 // stemax maximum step allowed
325 // deemax maximum fractional energy loss in one step
326 // epsil tracking precision in cm
327 // stmin minimum step due to continuous processes
329 // ifield = 0 no magnetic field
330 // = -1 user decision in guswim
331 // = 1 tracking performed with Runge Kutta
332 // = 2 tracking performed with helix
333 // = 3 constant magnetic field along z
336 gMC->Medium(kmed,name, (*fIdmate)[nmat], isvol, ifield, fieldm,
337 tmaxfd, stemax, deemax, epsil, stmin, ubuf, nbuf);
338 (*fIdtmed)[numed]=kmed;
341 //_______________________________________________________________________
342 void AliModule::AliMatrix(Int_t &nmat, Float_t theta1, Float_t phi1,
343 Float_t theta2, Float_t phi2, Float_t theta3,
347 // Define a rotation matrix. Angles are in degrees.
349 // nmat on output contains the number assigned to the rotation matrix
350 // theta1 polar angle for axis I
351 // phi1 azimuthal angle for axis I
352 // theta2 polar angle for axis II
353 // phi2 azimuthal angle for axis II
354 // theta3 polar angle for axis III
355 // phi3 azimuthal angle for axis III
357 gMC->Matrix(nmat, theta1, phi1, theta2, phi2, theta3, phi3);
360 //_______________________________________________________________________
361 Float_t AliModule::ZMin() const
366 //_______________________________________________________________________
367 Float_t AliModule::ZMax() const
372 //_______________________________________________________________________
373 void AliModule::SetEuclidFile(char* material, char* geometry)
376 // Sets the name of the Euclid file
378 fEuclidMaterial=material;
380 fEuclidGeometry=geometry;
382 char* name = new char[strlen(material)];
383 strcpy(name,material);
384 strcpy(&name[strlen(name)-4],".euc");
385 fEuclidGeometry=name;
390 //_______________________________________________________________________
391 void AliModule::ReadEuclid(const char* filnam, char* topvol)
394 // read in the geometry of the detector in euclid file format
396 // id_det : the detector identification (2=its,...)
397 // topvol : return parameter describing the name of the top
398 // volume of geometry.
403 // several changes have been made by miroslav helbich
404 // subroutine is rewrited to follow the new established way of memory
405 // booking for tracking medias and rotation matrices.
406 // all used tracking media have to be defined first, for this you can use
407 // subroutine greutmed.
408 // top volume is searched as only volume not positioned into another
411 Int_t i, nvol, iret, itmed, irot, numed, npar, ndiv, iaxe;
412 Int_t ndvmx, nr, flag;
413 char key[5], card[77], natmed[21];
414 char name[5], mother[5], shape[5], konly[5], volst[7000][5];
417 Float_t teta1, phi1, teta2, phi2, teta3, phi3, orig, step;
419 const Int_t kMaxRot=5000;
420 Int_t idrot[kMaxRot],istop[7000];
423 // *** The input filnam name will be with extension '.euc'
424 filtmp=gSystem->ExpandPathName(filnam);
425 lun=fopen(filtmp,"r");
428 AliError(Form("Could not open file %s",filnam));
431 //* --- definition of rotation matrix 0 ---
432 TArrayI &idtmed = *fIdtmed;
433 for(i=1; i<kMaxRot; ++i) idrot[i]=-99;
437 for(i=0;i<77;i++) card[i]=0;
438 iret=fscanf(lun,"%77[^\n]",card);
439 if(iret<=0) goto L20;
444 if (!strcmp(key,"TMED")) {
445 sscanf(&card[5],"%d '%[^']'",&itmed,natmed);
446 if( itmed<0 || itmed>=100 ) {
447 AliError(Form("TMED illegal medium number %d for %s",itmed,natmed));
450 //Pad the string with blanks
453 while(i<20) natmed[i++]=' ';
456 if( idtmed[itmed]<=0 ) {
457 AliError(Form("TMED undefined medium number %d for %s",itmed,natmed));
460 gMC->Gckmat(idtmed[itmed],natmed);
462 } else if (!strcmp(key,"ROTM")) {
463 sscanf(&card[4],"%d %f %f %f %f %f %f",&irot,&teta1,&phi1,&teta2,&phi2,&teta3,&phi3);
464 if( irot<=0 || irot>=kMaxRot ) {
465 AliError(Form("ROTM rotation matrix number %d illegal",irot));
468 AliMatrix(idrot[irot],teta1,phi1,teta2,phi2,teta3,phi3);
470 } else if (!strcmp(key,"VOLU")) {
471 sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, shape, &numed, &npar);
473 for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
476 gMC->Gsvolu( name, shape, idtmed[numed], par, npar);
477 //* save the defined volumes
478 strcpy(volst[++nvol],name);
481 } else if (!strcmp(key,"DIVN")) {
482 sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, mother, &ndiv, &iaxe);
483 gMC->Gsdvn ( name, mother, ndiv, iaxe );
485 } else if (!strcmp(key,"DVN2")) {
486 sscanf(&card[5],"'%[^']' '%[^']' %d %d %f %d",name, mother, &ndiv, &iaxe, &orig, &numed);
487 gMC->Gsdvn2( name, mother, ndiv, iaxe, orig,idtmed[numed]);
489 } else if (!strcmp(key,"DIVT")) {
490 sscanf(&card[5],"'%[^']' '%[^']' %f %d %d %d", name, mother, &step, &iaxe, &numed, &ndvmx);
491 gMC->Gsdvt ( name, mother, step, iaxe, idtmed[numed], ndvmx);
493 } else if (!strcmp(key,"DVT2")) {
494 sscanf(&card[5],"'%[^']' '%[^']' %f %d %f %d %d", name, mother, &step, &iaxe, &orig, &numed, &ndvmx);
495 gMC->Gsdvt2 ( name, mother, step, iaxe, orig, idtmed[numed], ndvmx );
497 } else if (!strcmp(key,"POSI")) {
498 sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']'", name, &nr, mother, &xo, &yo, &zo, &irot, konly);
499 if( irot<0 || irot>=kMaxRot ) {
500 Error("ReadEuclid","POSI %s#%d rotation matrix number %d illegal\n",name,nr,irot);
503 if( idrot[irot] == -99) {
504 Error("ReadEuclid","POSI %s#%d undefined matrix number %d\n",name,nr,irot);
507 //*** volume name cannot be the top volume
508 for(i=1;i<=nvol;i++) {
509 if (!strcmp(volst[i],name)) istop[i]=0;
512 gMC->Gspos ( name, nr, mother, xo, yo, zo, idrot[irot], konly );
514 } else if (!strcmp(key,"POSP")) {
515 sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']' %d", name, &nr, mother, &xo, &yo, &zo, &irot, konly, &npar);
516 if( irot<0 || irot>=kMaxRot ) {
517 Error("ReadEuclid","POSP %s#%d rotation matrix number %d illegal\n",name,nr,irot);
520 if( idrot[irot] == -99) {
521 Error("ReadEuclid","POSP %s#%d undefined matrix number %d\n",name,nr,irot);
525 for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
528 //*** volume name cannot be the top volume
529 for(i=1;i<=nvol;i++) {
530 if (!strcmp(volst[i],name)) istop[i]=0;
533 gMC->Gsposp ( name, nr, mother, xo,yo,zo, idrot[irot], konly, par, npar);
536 if (strcmp(key,"END")) goto L10;
537 //* find top volume in the geometry
539 for(i=1;i<=nvol;i++) {
540 if (istop[i] && flag) {
541 AliWarning(Form(" %s is another possible top volume",volst[i]));
543 if (istop[i] && !flag) {
544 strcpy(topvol,volst[i]);
545 AliDebug(2, Form("volume %s taken as a top volume",topvol));
550 AliWarning("top volume not found");
554 //* commented out only for the not cernlib version
555 AliDebug(1, Form("file: %s is now read in",filnam));
560 AliError("reading error or premature end of file");
563 //_______________________________________________________________________
564 void AliModule::ReadEuclidMedia(const char* filnam)
567 // read in the materials and tracking media for the detector
568 // in euclid file format
570 // filnam: name of the input file
571 // id_det: id_det is the detector identification (2=its,...)
573 // author : miroslav helbich
575 Float_t sxmgmx = gAlice->Field()->Max();
576 Int_t isxfld = gAlice->Field()->Integ();
577 Int_t end, i, iret, itmed;
578 char key[5], card[130], natmed[21], namate[21];
583 Int_t nwbuf, isvol, ifield, nmat;
584 Float_t a, z, dens, radl, absl, fieldm, tmaxfd, stemax, deemax, epsil, stmin;
587 for(i=0;i<end;i++) if(filnam[i]=='.') {
592 // *** The input filnam name will be with extension '.euc'
593 AliDebug(1, Form("The file name is %s",filnam)); //Debug
594 filtmp=gSystem->ExpandPathName(filnam);
595 lun=fopen(filtmp,"r");
598 AliWarning(Form("Could not open file %s",filnam));
602 // Retrieve Mag Field parameters
603 Int_t globField=gAlice->Field()->Integ();
604 Float_t globMaxField=gAlice->Field()->Max();
605 // TArrayI &idtmed = *fIdtmed;
608 for(i=0;i<130;i++) card[i]=0;
609 iret=fscanf(lun,"%4s %[^\n]",key,card);
610 if(iret<=0) goto L20;
614 if (!strcmp(key,"MATE")) {
615 sscanf(card,"%d '%[^']' %f %f %f %f %f %d",&imate,namate,&a,&z,&dens,&radl,&absl,&nwbuf);
616 if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
617 //Pad the string with blanks
620 while(i<20) namate[i++]=' ';
623 AliMaterial(imate,namate,a,z,dens,radl,absl,ubuf,nwbuf);
624 //* read tracking medium
625 } else if (!strcmp(key,"TMED")) {
626 sscanf(card,"%d '%[^']' %d %d %d %f %f %f %f %f %f %d",
627 &itmed,natmed,&nmat,&isvol,&ifield,&fieldm,&tmaxfd,
628 &stemax,&deemax,&epsil,&stmin,&nwbuf);
629 if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
630 if (ifield<0) ifield=isxfld;
631 if (fieldm<0) fieldm=sxmgmx;
632 //Pad the string with blanks
635 while(i<20) natmed[i++]=' ';
638 AliMedium(itmed,natmed,nmat,isvol,globField,globMaxField,tmaxfd,
639 stemax,deemax,epsil,stmin,ubuf,nwbuf);
640 // (*fImedia)[idtmed[itmed]-1]=id_det;
644 if (strcmp(key,"END")) goto L10;
647 //* commented out only for the not cernlib version
648 AliDebug(1, Form("file %s is now read in",filnam));
653 AliWarning("reading error or premature end of file");
656 //_______________________________________________________________________
657 void AliModule::RemapTrackReferencesIDs(Int_t *map)
660 // Remapping track reference
661 // Called at finish primary
663 if (!fTrackReferences) return;
664 for (Int_t i=0;i<fTrackReferences->GetEntries();i++){
665 AliTrackReference * ref = dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(i));
667 Int_t newID = map[ref->GetTrack()];
668 if (newID>=0) ref->SetTrack(newID);
671 ref->SetBit(kNotDeleted,kFALSE);
672 fTrackReferences->RemoveAt(i);
676 fTrackReferences->Compress();
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
693 if (fRunLoader == 0x0)
694 AliFatal("AliRunLoader not initialized. Can not proceed");
695 fRunLoader->GetAliRun()->GetMCApp()->ResetTrackReferences();
696 fRunLoader->TreeTR()->GetEvent(track);
699 fMaxIterTrackRef = fTrackReferences->GetEntriesFast();
700 fCurrentIterTrackRef = 0;
701 if(fMaxIterTrackRef) return dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(0));
705 //_______________________________________________________________________
706 AliTrackReference* AliModule::NextTrackReference()
709 // Return the next hit for the current track
711 if(fMaxIterTrackRef) {
712 if(++fCurrentIterTrackRef<fMaxIterTrackRef)
713 return dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(fCurrentIterTrackRef));
717 AliWarning("Iterator called without calling FistTrackReference before");
723 //_______________________________________________________________________
724 void AliModule::ResetTrackReferences()
727 // Reset number of hits and the hits array
729 fMaxIterTrackRef = 0;
730 if (fTrackReferences) fTrackReferences->Clear();
733 //_____________________________________________________________________________
735 AliLoader* AliModule::MakeLoader(const char* /*topfoldername*/)
740 //PH Merged with v3-09-08 |
742 //_____________________________________________________________________________
744 void AliModule::SetTreeAddress()
747 // Set branch address for track reference Tree
752 // Branch address for track reference tree
753 TTree *treeTR = TreeTR();
755 if (treeTR && fTrackReferences) {
756 branch = treeTR->GetBranch(GetName());
759 AliDebug(3, Form("(%s) Setting for TrackRefs",GetName()));
760 branch->SetAddress(&fTrackReferences);
764 //can be called before MakeBranch and than does not make sense to issue the warning
765 AliDebug(1, Form("(%s) Failed for Track References. Can not find branch in tree.",
771 //_____________________________________________________________________________
772 void AliModule::AddTrackReference(Int_t label){
774 // add a trackrefernce to the list
775 if (!fTrackReferences) {
776 AliError("Container trackrefernce not active");
779 Int_t nref = fTrackReferences->GetEntriesFast();
780 TClonesArray &lref = *fTrackReferences;
781 new(lref[nref]) AliTrackReference(label);
785 //_____________________________________________________________________________
786 void AliModule::MakeBranchTR(Option_t */*option*/)
789 // Makes branch in treeTR
791 AliDebug(2,Form("Making Track Refs. Branch for %s",GetName()));
792 TTree * tree = TreeTR();
793 if (fTrackReferences && tree)
795 TBranch *branch = tree->GetBranch(GetName());
798 AliDebug(2,Form("Branch %s is already in tree.",GetName()));
802 branch = tree->Branch(GetName(),&fTrackReferences);
806 AliDebug(2,Form("FAILED for %s: tree=%#x fTrackReferences=%#x",
807 GetName(),tree,fTrackReferences));
811 //_____________________________________________________________________________
812 TTree* AliModule::TreeTR()
815 // Return TR tree pointer
817 if ( fRunLoader == 0x0)
819 AliError("Can not get the run loader");
823 TTree* tree = fRunLoader->TreeTR();
828 //_____________________________________________________________________________
829 void AliModule::Digits2Raw()
831 // This is a dummy version that just copies the digits file contents
832 // to a raw data file.
834 AliWarning(Form("Dummy version called for %s", GetName()));
836 const Int_t kNDetectors = 17;
837 const char* kDetectors[kNDetectors] = {"TPC", "ITSSPD", "ITSSDD", "ITSSSD", "TRD", "TOF", "PHOS", "RICH", "EMCAL", "MUON", "MUTR", "ZDC", "PMD", "START", "VZERO", "CRT", "FMD"};
838 const Int_t kDetectorDDLs[kNDetectors] = {216, 20, 12, 16, 18, 72, 20, 20, 22, 20, 2, 1, 6, 1, 1, 1, 3};
841 for (Int_t i = 0; i < kNDetectors; i++) {
842 if (strcmp(GetName(), kDetectors[i]) == 0) {
843 nDDLs = kDetectorDDLs[i];
844 ddlOffset = 0x100 * i;
848 if (!GetLoader()) return;
849 fstream digitsFile(GetLoader()->GetDigitsFileName(), ios::in);
850 if (!digitsFile) return;
852 digitsFile.seekg(0, ios::end);
853 UInt_t size = digitsFile.tellg();
854 UInt_t ddlSize = 4 * (size / (4*nDDLs));
855 Char_t* buffer = new Char_t[ddlSize+1];
857 for (Int_t iDDL = 0; iDDL < nDDLs; iDDL++) {
859 sprintf(fileName, "%s_%d.ddl", GetName(), iDDL + ddlOffset);
860 fstream rawFile(fileName, ios::out);
861 if (!rawFile) return;
863 AliRawDataHeader header;
864 header.fSize = ddlSize + sizeof(header);
865 rawFile.write((char*) &header, sizeof(header));
867 digitsFile.read(buffer, ddlSize);
868 rawFile.write(buffer, ddlSize);
877 //_____________________________________________________________________________
878 Int_t AliModule::GetDebug() const
880 AliWarning("Don't use this method any more, use AliDebug instead");