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>
43 #include <TGeoManager.h>
47 #include "AliConfig.h"
48 #include "AliLoader.h"
50 #include "AliModule.h"
52 #include "AliTrackReference.h"
54 #include "AliRawDataHeader.h"
56 #include "AliDAQConfig.h"
60 //_______________________________________________________________________
61 AliModule::AliModule():
74 fCurrentIterTrackRef(0),
78 // Default constructor for the AliModule class
82 //_______________________________________________________________________
83 AliModule::AliModule(const char* name,const char *title):
87 fIdtmed(new TArrayI(100)),
88 fIdmate(new TArrayI(100)),
92 fHistograms(new TList()),
95 fTrackReferences(new TClonesArray("AliTrackReference", 100)),
97 fCurrentIterTrackRef(0),
101 // Normal constructor invoked by all Modules.
102 // Create the list for Module specific histograms
103 // Add this Module to the global list of Modules in Run.
105 // Get the Module numeric ID
106 Int_t id = gAlice->GetModuleID(name);
108 // Module already added !
109 AliWarning(Form("Module: %s already present at %d",name,id));
113 // Add this Module to the list of Modules
115 gAlice->AddModule(this);
119 // Clear space for tracking media and material indexes
121 for(Int_t i=0;i<100;i++) (*fIdmate)[i]=(*fIdtmed)[i]=0;
124 //_______________________________________________________________________
125 AliModule::AliModule(const AliModule &mod):
142 fCurrentIterTrackRef(0),
151 //_______________________________________________________________________
152 AliModule::~AliModule()
158 // Remove this Module from the list of Modules
160 TObjArray * modules = gAlice->Modules();
161 if (modules) modules->Remove(this);
163 // Delete ROOT geometry
171 fHistograms->Clear();
175 // Delete track references
176 if (fTrackReferences) {
177 fTrackReferences->Delete();
178 delete fTrackReferences;
179 fTrackReferences = 0;
181 // Delete TArray objects
187 //_______________________________________________________________________
188 void AliModule::Copy(TObject & /* mod */) const
191 // Copy *this onto mod, not implemented for AliModule
193 AliFatal("Not implemented!");
196 //_______________________________________________________________________
197 void AliModule::Disable()
200 // Disable Module on viewer
206 // Loop through geometry to disable all
207 // nodes for this Module
208 while((node = dynamic_cast<TNode*>(next()))) {
209 node->SetVisibility(-1);
213 //_______________________________________________________________________
214 void AliModule::Enable()
217 // Enable Module on the viewver
223 // Loop through geometry to enable all
224 // nodes for this Module
225 while((node = dynamic_cast<TNode*>(next()))) {
226 node->SetVisibility(3);
230 //_______________________________________________________________________
231 void AliModule::AliMaterial(Int_t imat, const char* name, Float_t a,
232 Float_t z, Float_t dens, Float_t radl,
233 Float_t absl, Float_t *buf, Int_t nwbuf) const
236 // Store the parameters for a material
238 // imat the material index will be stored in (*fIdmate)[imat]
239 // name material name
243 // radl radiation length
244 // absl absorbtion length
245 // buf adress of an array user words
246 // nwbuf number of user words
249 //Build the string uniquename as "DET_materialname"
250 TString uniquename = GetName();
251 uniquename.Append("_");
252 uniquename.Append(name);
253 //if geometry loaded from file only fill fIdmate, else create material too
254 if(gAlice->IsRootGeometry()){
255 TGeoMaterial *mat = gGeoManager->GetMaterial(uniquename.Data());
256 kmat = mat->GetUniqueID();
257 (*fIdmate)[imat]=kmat;
259 gMC->Material(kmat, uniquename.Data(), a, z, dens, radl, absl, buf, nwbuf);
260 (*fIdmate)[imat]=kmat;
264 //_______________________________________________________________________
265 void AliModule::AliGetMaterial(Int_t imat, char* name, Float_t &a,
266 Float_t &z, Float_t &dens, Float_t &radl,
270 // Store the parameters for a material
272 // imat the material index will be stored in (*fIdmate)[imat]
273 // name material name
277 // radl radiation length
278 // absl absorbtion length
279 // buf adress of an array user words
280 // nwbuf number of user words
285 kmat=(*fIdmate)[imat];
286 gMC->Gfmate(kmat, name, a, z, dens, radl, absl, buf, nwbuf);
290 //_______________________________________________________________________
291 void AliModule::AliMixture(Int_t imat, const char *name, Float_t *a,
292 Float_t *z, Float_t dens, Int_t nlmat,
296 // Defines mixture or compound imat as composed by
297 // nlmat materials defined by arrays a, z and wmat
299 // If nlmat > 0 wmat contains the proportion by
300 // weights of each basic material in the mixture
302 // If nlmat < 0 wmat contains the number of atoms
303 // of eack kind in the molecule of the compound
304 // In this case, wmat is changed on output to the relative weigths.
306 // imat the material index will be stored in (*fIdmate)[imat]
307 // name material name
308 // a array of atomic masses
309 // z array of atomic numbers
311 // nlmat number of components
312 // wmat array of concentrations
315 //Build the string uniquename as "DET_mixturename"
316 TString uniquename = GetName();
317 uniquename.Append("_");
318 uniquename.Append(name);
319 //if geometry loaded from file only fill fIdmate, else create mixture too
320 if(gAlice->IsRootGeometry()){
321 TGeoMaterial *mat = gGeoManager->GetMaterial(uniquename.Data());
322 kmat = mat->GetUniqueID();
323 (*fIdmate)[imat]=kmat;
325 gMC->Mixture(kmat, uniquename.Data(), a, z, dens, nlmat, wmat);
326 (*fIdmate)[imat]=kmat;
330 //_______________________________________________________________________
331 void AliModule::AliMedium(Int_t numed, const char *name, Int_t nmat,
332 Int_t isvol, Int_t ifield, Float_t fieldm,
333 Float_t tmaxfd, Float_t stemax, Float_t deemax,
334 Float_t epsil, Float_t stmin, Float_t *ubuf,
338 // Store the parameters of a tracking medium
340 // numed the medium number is stored into (*fIdtmed)[numed]
342 // nmat the material number is stored into (*fIdmate)[nmat]
343 // isvol sensitive volume if isvol!=0
344 // ifield magnetic field flag (see below)
345 // fieldm maximum magnetic field
346 // tmaxfd maximum deflection angle due to magnetic field
347 // stemax maximum step allowed
348 // deemax maximum fractional energy loss in one step
349 // epsil tracking precision in cm
350 // stmin minimum step due to continuous processes
352 // ifield = 0 no magnetic field
353 // = -1 user decision in guswim
354 // = 1 tracking performed with Runge Kutta
355 // = 2 tracking performed with helix
356 // = 3 constant magnetic field along z
359 //Build the string uniquename as "DET_mediumname"
360 TString uniquename = GetName();
361 uniquename.Append("_");
362 uniquename.Append(name);
363 //if geometry loaded from file only fill fIdtmed, else create medium too
364 if(gAlice->IsRootGeometry()){
365 TGeoMedium *med = gGeoManager->GetMedium(uniquename.Data());
367 (*fIdtmed)[numed]=kmed;
369 gMC->Medium(kmed, uniquename.Data(), (*fIdmate)[nmat], isvol, ifield,
370 fieldm, tmaxfd, stemax, deemax, epsil, stmin, ubuf, nbuf);
371 (*fIdtmed)[numed]=kmed;
375 //_______________________________________________________________________
376 void AliModule::AliMatrix(Int_t &nmat, Float_t theta1, Float_t phi1,
377 Float_t theta2, Float_t phi2, Float_t theta3,
381 // Define a rotation matrix. Angles are in degrees.
383 // nmat on output contains the number assigned to the rotation matrix
384 // theta1 polar angle for axis I
385 // phi1 azimuthal angle for axis I
386 // theta2 polar angle for axis II
387 // phi2 azimuthal angle for axis II
388 // theta3 polar angle for axis III
389 // phi3 azimuthal angle for axis III
391 gMC->Matrix(nmat, theta1, phi1, theta2, phi2, theta3, phi3);
394 //_______________________________________________________________________
395 Float_t AliModule::ZMin() const
400 //_______________________________________________________________________
401 Float_t AliModule::ZMax() const
406 //_______________________________________________________________________
407 void AliModule::SetEuclidFile(char* material, char* geometry)
410 // Sets the name of the Euclid file
412 fEuclidMaterial=material;
414 fEuclidGeometry=geometry;
416 char* name = new char[strlen(material)];
417 strcpy(name,material);
418 strcpy(&name[strlen(name)-4],".euc");
419 fEuclidGeometry=name;
424 //_______________________________________________________________________
425 void AliModule::ReadEuclid(const char* filnam, char* topvol)
428 // read in the geometry of the detector in euclid file format
430 // id_det : the detector identification (2=its,...)
431 // topvol : return parameter describing the name of the top
432 // volume of geometry.
437 // several changes have been made by miroslav helbich
438 // subroutine is rewrited to follow the new established way of memory
439 // booking for tracking medias and rotation matrices.
440 // all used tracking media have to be defined first, for this you can use
441 // subroutine greutmed.
442 // top volume is searched as only volume not positioned into another
445 Int_t i, nvol, iret, itmed, irot, numed, npar, ndiv, iaxe;
446 Int_t ndvmx, nr, flag;
447 char key[5], card[77], natmed[21];
448 char name[5], mother[5], shape[5], konly[5], volst[7000][5];
451 Float_t teta1, phi1, teta2, phi2, teta3, phi3, orig, step;
453 const Int_t kMaxRot=5000;
454 Int_t idrot[kMaxRot],istop[7000];
457 // *** The input filnam name will be with extension '.euc'
458 filtmp=gSystem->ExpandPathName(filnam);
459 lun=fopen(filtmp,"r");
462 AliError(Form("Could not open file %s",filnam));
465 //* --- definition of rotation matrix 0 ---
466 TArrayI &idtmed = *fIdtmed;
467 for(i=1; i<kMaxRot; ++i) idrot[i]=-99;
471 for(i=0;i<77;i++) card[i]=0;
472 iret=fscanf(lun,"%77[^\n]",card);
473 if(iret<=0) goto L20;
478 if (!strcmp(key,"TMED")) {
479 sscanf(&card[5],"%d '%[^']'",&itmed,natmed);
480 if( itmed<0 || itmed>=100 ) {
481 AliError(Form("TMED illegal medium number %d for %s",itmed,natmed));
484 //Pad the string with blanks
487 while(i<20) natmed[i++]=' ';
490 if( idtmed[itmed]<=0 ) {
491 AliError(Form("TMED undefined medium number %d for %s",itmed,natmed));
494 gMC->Gckmat(idtmed[itmed],natmed);
496 } else if (!strcmp(key,"ROTM")) {
497 sscanf(&card[4],"%d %f %f %f %f %f %f",&irot,&teta1,&phi1,&teta2,&phi2,&teta3,&phi3);
498 if( irot<=0 || irot>=kMaxRot ) {
499 AliError(Form("ROTM rotation matrix number %d illegal",irot));
502 AliMatrix(idrot[irot],teta1,phi1,teta2,phi2,teta3,phi3);
504 } else if (!strcmp(key,"VOLU")) {
505 sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, shape, &numed, &npar);
507 for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
510 gMC->Gsvolu( name, shape, idtmed[numed], par, npar);
511 //* save the defined volumes
512 strcpy(volst[++nvol],name);
515 } else if (!strcmp(key,"DIVN")) {
516 sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, mother, &ndiv, &iaxe);
517 gMC->Gsdvn ( name, mother, ndiv, iaxe );
519 } else if (!strcmp(key,"DVN2")) {
520 sscanf(&card[5],"'%[^']' '%[^']' %d %d %f %d",name, mother, &ndiv, &iaxe, &orig, &numed);
521 gMC->Gsdvn2( name, mother, ndiv, iaxe, orig,idtmed[numed]);
523 } else if (!strcmp(key,"DIVT")) {
524 sscanf(&card[5],"'%[^']' '%[^']' %f %d %d %d", name, mother, &step, &iaxe, &numed, &ndvmx);
525 gMC->Gsdvt ( name, mother, step, iaxe, idtmed[numed], ndvmx);
527 } else if (!strcmp(key,"DVT2")) {
528 sscanf(&card[5],"'%[^']' '%[^']' %f %d %f %d %d", name, mother, &step, &iaxe, &orig, &numed, &ndvmx);
529 gMC->Gsdvt2 ( name, mother, step, iaxe, orig, idtmed[numed], ndvmx );
531 } else if (!strcmp(key,"POSI")) {
532 sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']'", name, &nr, mother, &xo, &yo, &zo, &irot, konly);
533 if( irot<0 || irot>=kMaxRot ) {
534 Error("ReadEuclid","POSI %s#%d rotation matrix number %d illegal\n",name,nr,irot);
537 if( idrot[irot] == -99) {
538 Error("ReadEuclid","POSI %s#%d undefined matrix number %d\n",name,nr,irot);
541 //*** volume name cannot be the top volume
542 for(i=1;i<=nvol;i++) {
543 if (!strcmp(volst[i],name)) istop[i]=0;
546 gMC->Gspos ( name, nr, mother, xo, yo, zo, idrot[irot], konly );
548 } else if (!strcmp(key,"POSP")) {
549 sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']' %d", name, &nr, mother, &xo, &yo, &zo, &irot, konly, &npar);
550 if( irot<0 || irot>=kMaxRot ) {
551 Error("ReadEuclid","POSP %s#%d rotation matrix number %d illegal\n",name,nr,irot);
554 if( idrot[irot] == -99) {
555 Error("ReadEuclid","POSP %s#%d undefined matrix number %d\n",name,nr,irot);
559 for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
562 //*** volume name cannot be the top volume
563 for(i=1;i<=nvol;i++) {
564 if (!strcmp(volst[i],name)) istop[i]=0;
567 gMC->Gsposp ( name, nr, mother, xo,yo,zo, idrot[irot], konly, par, npar);
570 if (strcmp(key,"END")) goto L10;
571 //* find top volume in the geometry
573 for(i=1;i<=nvol;i++) {
574 if (istop[i] && flag) {
575 AliWarning(Form(" %s is another possible top volume",volst[i]));
577 if (istop[i] && !flag) {
578 strcpy(topvol,volst[i]);
579 AliDebug(2, Form("volume %s taken as a top volume",topvol));
584 AliWarning("top volume not found");
588 //* commented out only for the not cernlib version
589 AliDebug(1, Form("file: %s is now read in",filnam));
594 AliError("reading error or premature end of file");
597 //_______________________________________________________________________
598 void AliModule::ReadEuclidMedia(const char* filnam)
601 // read in the materials and tracking media for the detector
602 // in euclid file format
604 // filnam: name of the input file
605 // id_det: id_det is the detector identification (2=its,...)
607 // author : miroslav helbich
609 Float_t sxmgmx = gAlice->Field()->Max();
610 Int_t isxfld = gAlice->Field()->Integ();
611 Int_t end, i, iret, itmed;
612 char key[5], card[130], natmed[21], namate[21];
617 Int_t nwbuf, isvol, ifield, nmat;
618 Float_t a, z, dens, radl, absl, fieldm, tmaxfd, stemax, deemax, epsil, stmin;
621 for(i=0;i<end;i++) if(filnam[i]=='.') {
626 // *** The input filnam name will be with extension '.euc'
627 AliDebug(1, Form("The file name is %s",filnam)); //Debug
628 filtmp=gSystem->ExpandPathName(filnam);
629 lun=fopen(filtmp,"r");
632 AliWarning(Form("Could not open file %s",filnam));
636 // Retrieve Mag Field parameters
637 Int_t globField=gAlice->Field()->Integ();
638 Float_t globMaxField=gAlice->Field()->Max();
639 // TArrayI &idtmed = *fIdtmed;
642 for(i=0;i<130;i++) card[i]=0;
643 iret=fscanf(lun,"%4s %[^\n]",key,card);
644 if(iret<=0) goto L20;
648 if (!strcmp(key,"MATE")) {
649 sscanf(card,"%d '%[^']' %f %f %f %f %f %d",&imate,namate,&a,&z,&dens,&radl,&absl,&nwbuf);
650 if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
651 //Pad the string with blanks
654 while(i<20) namate[i++]=' ';
657 AliMaterial(imate,namate,a,z,dens,radl,absl,ubuf,nwbuf);
658 //* read tracking medium
659 } else if (!strcmp(key,"TMED")) {
660 sscanf(card,"%d '%[^']' %d %d %d %f %f %f %f %f %f %d",
661 &itmed,natmed,&nmat,&isvol,&ifield,&fieldm,&tmaxfd,
662 &stemax,&deemax,&epsil,&stmin,&nwbuf);
663 if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
664 if (ifield<0) ifield=isxfld;
665 if (fieldm<0) fieldm=sxmgmx;
666 //Pad the string with blanks
669 while(i<20) natmed[i++]=' ';
672 AliMedium(itmed,natmed,nmat,isvol,globField,globMaxField,tmaxfd,
673 stemax,deemax,epsil,stmin,ubuf,nwbuf);
674 // (*fImedia)[idtmed[itmed]-1]=id_det;
678 if (strcmp(key,"END")) goto L10;
681 //* commented out only for the not cernlib version
682 AliDebug(1, Form("file %s is now read in",filnam));
687 AliWarning("reading error or premature end of file");
690 //_______________________________________________________________________
691 void AliModule::RemapTrackReferencesIDs(Int_t *map)
694 // Remapping track reference
695 // Called at finish primary
697 if (!fTrackReferences) return;
698 for (Int_t i=0;i<fTrackReferences->GetEntries();i++){
699 AliTrackReference * ref = dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(i));
701 Int_t newID = map[ref->GetTrack()];
702 if (newID>=0) ref->SetTrack(newID);
705 ref->SetBit(kNotDeleted,kFALSE);
706 fTrackReferences->RemoveAt(i);
710 fTrackReferences->Compress();
715 //_______________________________________________________________________
716 AliTrackReference* AliModule::FirstTrackReference(Int_t track)
719 // Initialise the hit iterator
720 // Return the address of the first hit for track
721 // If track>=0 the track is read from disk
722 // while if track<0 the first hit of the current
727 if (fRunLoader == 0x0)
728 AliFatal("AliRunLoader not initialized. Can not proceed");
729 fRunLoader->GetAliRun()->GetMCApp()->ResetTrackReferences();
730 fRunLoader->TreeTR()->GetEvent(track);
733 fMaxIterTrackRef = fTrackReferences->GetEntriesFast();
734 fCurrentIterTrackRef = 0;
735 if(fMaxIterTrackRef) return dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(0));
739 //_______________________________________________________________________
740 AliTrackReference* AliModule::NextTrackReference()
743 // Return the next hit for the current track
745 if(fMaxIterTrackRef) {
746 if(++fCurrentIterTrackRef<fMaxIterTrackRef)
747 return dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(fCurrentIterTrackRef));
751 AliWarning("Iterator called without calling FistTrackReference before");
757 //_______________________________________________________________________
758 void AliModule::ResetTrackReferences()
761 // Reset number of hits and the hits array
763 fMaxIterTrackRef = 0;
764 if (fTrackReferences) fTrackReferences->Clear();
767 //_____________________________________________________________________________
769 AliLoader* AliModule::MakeLoader(const char* /*topfoldername*/)
774 //PH Merged with v3-09-08 |
776 //_____________________________________________________________________________
778 void AliModule::SetTreeAddress()
781 // Set branch address for track reference Tree
786 // Branch address for track reference tree
787 TTree *treeTR = TreeTR();
789 if (treeTR && fTrackReferences) {
790 branch = treeTR->GetBranch(GetName());
793 AliDebug(3, Form("(%s) Setting for TrackRefs",GetName()));
794 branch->SetAddress(&fTrackReferences);
798 //can be called before MakeBranch and than does not make sense to issue the warning
799 AliDebug(1, Form("(%s) Failed for Track References. Can not find branch in tree.",
805 //_____________________________________________________________________________
806 void AliModule::AddTrackReference(Int_t label){
808 // add a trackrefernce to the list
809 if (!fTrackReferences) {
810 AliError("Container trackrefernce not active");
813 Int_t nref = fTrackReferences->GetEntriesFast();
814 TClonesArray &lref = *fTrackReferences;
815 new(lref[nref]) AliTrackReference(label);
819 //_____________________________________________________________________________
820 void AliModule::MakeBranchTR(Option_t */*option*/)
823 // Makes branch in treeTR
825 AliDebug(2,Form("Making Track Refs. Branch for %s",GetName()));
826 TTree * tree = TreeTR();
827 if (fTrackReferences && tree)
829 TBranch *branch = tree->GetBranch(GetName());
832 AliDebug(2,Form("Branch %s is already in tree.",GetName()));
836 branch = tree->Branch(GetName(),&fTrackReferences);
840 AliDebug(2,Form("FAILED for %s: tree=%#x fTrackReferences=%#x",
841 GetName(),tree,fTrackReferences));
845 //_____________________________________________________________________________
846 TTree* AliModule::TreeTR()
849 // Return TR tree pointer
851 if ( fRunLoader == 0x0)
853 AliError("Can not get the run loader");
857 TTree* tree = fRunLoader->TreeTR();
862 //_____________________________________________________________________________
863 void AliModule::Digits2Raw()
865 // This is a dummy version that just copies the digits file contents
866 // to a raw data file.
868 AliWarning(Form("Dummy version called for %s", GetName()));
872 for (Int_t i = 0; i < kNDetectors; i++) {
873 if (strcmp(GetName(), kDetectors[i]) == 0) {
874 nDDLs = kDetectorDDLs[i];
875 ddlOffset = 0x100 * i;
879 if (!GetLoader()) return;
880 fstream digitsFile(GetLoader()->GetDigitsFileName(), ios::in);
881 if (!digitsFile) return;
883 digitsFile.seekg(0, ios::end);
884 UInt_t size = digitsFile.tellg();
885 UInt_t ddlSize = 4 * (size / (4*nDDLs));
886 Char_t* buffer = new Char_t[ddlSize+1];
888 for (Int_t iDDL = 0; iDDL < nDDLs; iDDL++) {
890 sprintf(fileName, "%s_%d.ddl", GetName(), iDDL + ddlOffset);
891 fstream rawFile(fileName, ios::out);
892 if (!rawFile) return;
894 AliRawDataHeader header;
895 header.fSize = ddlSize + sizeof(header);
896 rawFile.write((char*) &header, sizeof(header));
898 digitsFile.read(buffer, ddlSize);
899 rawFile.write(buffer, ddlSize);