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"
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);
117 //PH SetMarkerColor(3);
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()
131 // Remove this Module from the list of Modules
133 TObjArray * modules = gAlice->Modules();
134 if (modules) modules->Remove(this);
136 // Delete ROOT geometry
144 fHistograms->Clear();
148 // Delete track references
149 if (fTrackReferences) {
150 fTrackReferences->Delete();
151 delete fTrackReferences;
152 fTrackReferences = 0;
154 // Delete TArray objects
160 //_______________________________________________________________________
161 void AliModule::Disable()
164 // Disable Module on viewer
170 // Loop through geometry to disable all
171 // nodes for this Module
172 while((node = dynamic_cast<TNode*>(next()))) {
173 node->SetVisibility(-1);
177 //_______________________________________________________________________
178 void AliModule::Enable()
181 // Enable Module on the viewver
187 // Loop through geometry to enable all
188 // nodes for this Module
189 while((node = dynamic_cast<TNode*>(next()))) {
190 node->SetVisibility(3);
194 //_______________________________________________________________________
195 void AliModule::AliMaterial(Int_t imat, const char* name, Float_t a,
196 Float_t z, Float_t dens, Float_t radl,
197 Float_t absl, Float_t *buf, Int_t nwbuf) const
200 // Store the parameters for a material
202 // imat the material index will be stored in (*fIdmate)[imat]
203 // name material name
207 // radl radiation length
208 // absl absorbtion length
209 // buf adress of an array user words
210 // nwbuf number of user words
213 //Build the string uniquename as "DET_materialname"
214 TString uniquename = GetName();
215 uniquename.Append("_");
216 uniquename.Append(name);
217 //if geometry loaded from file only fill fIdmate, else create material too
218 if(gAlice->IsRootGeometry()){
219 TGeoMaterial *mat = gGeoManager->GetMaterial(uniquename.Data());
220 kmat = mat->GetUniqueID();
221 (*fIdmate)[imat]=kmat;
223 gMC->Material(kmat, uniquename.Data(), a, z, dens, radl, absl, buf, nwbuf);
224 (*fIdmate)[imat]=kmat;
228 //_______________________________________________________________________
229 void AliModule::AliGetMaterial(Int_t imat, char* name, Float_t &a,
230 Float_t &z, Float_t &dens, Float_t &radl,
234 // Store the parameters for a material
236 // imat the material index will be stored in (*fIdmate)[imat]
237 // name material name
241 // radl radiation length
242 // absl absorbtion length
243 // buf adress of an array user words
244 // nwbuf number of user words
249 kmat=(*fIdmate)[imat];
250 gMC->Gfmate(kmat, name, a, z, dens, radl, absl, buf, nwbuf);
254 //_______________________________________________________________________
255 void AliModule::AliMixture(Int_t imat, const char *name, Float_t *a,
256 Float_t *z, Float_t dens, Int_t nlmat,
260 // Defines mixture or compound imat as composed by
261 // nlmat materials defined by arrays a, z and wmat
263 // If nlmat > 0 wmat contains the proportion by
264 // weights of each basic material in the mixture
266 // If nlmat < 0 wmat contains the number of atoms
267 // of eack kind in the molecule of the compound
268 // In this case, wmat is changed on output to the relative weigths.
270 // imat the material index will be stored in (*fIdmate)[imat]
271 // name material name
272 // a array of atomic masses
273 // z array of atomic numbers
275 // nlmat number of components
276 // wmat array of concentrations
279 //Build the string uniquename as "DET_mixturename"
280 TString uniquename = GetName();
281 uniquename.Append("_");
282 uniquename.Append(name);
283 //if geometry loaded from file only fill fIdmate, else create mixture too
284 if(gAlice->IsRootGeometry()){
285 TGeoMaterial *mat = gGeoManager->GetMaterial(uniquename.Data());
286 kmat = mat->GetUniqueID();
287 (*fIdmate)[imat]=kmat;
289 gMC->Mixture(kmat, uniquename.Data(), a, z, dens, nlmat, wmat);
290 (*fIdmate)[imat]=kmat;
294 //_______________________________________________________________________
295 void AliModule::AliMedium(Int_t numed, const char *name, Int_t nmat,
296 Int_t isvol, Int_t ifield, Float_t fieldm,
297 Float_t tmaxfd, Float_t stemax, Float_t deemax,
298 Float_t epsil, Float_t stmin, Float_t *ubuf,
302 // Store the parameters of a tracking medium
304 // numed the medium number is stored into (*fIdtmed)[numed]
306 // nmat the material number is stored into (*fIdmate)[nmat]
307 // isvol sensitive volume if isvol!=0
308 // ifield magnetic field flag (see below)
309 // fieldm maximum magnetic field
310 // tmaxfd maximum deflection angle due to magnetic field
311 // stemax maximum step allowed
312 // deemax maximum fractional energy loss in one step
313 // epsil tracking precision in cm
314 // stmin minimum step due to continuous processes
316 // ifield = 0 no magnetic field
317 // = -1 user decision in guswim
318 // = 1 tracking performed with Runge Kutta
319 // = 2 tracking performed with helix
320 // = 3 constant magnetic field along z
323 //Build the string uniquename as "DET_mediumname"
324 TString uniquename = GetName();
325 uniquename.Append("_");
326 uniquename.Append(name);
327 //if geometry loaded from file only fill fIdtmed, else create medium too
328 if(gAlice->IsRootGeometry()){
329 TGeoMedium *med = gGeoManager->GetMedium(uniquename.Data());
331 (*fIdtmed)[numed]=kmed;
333 gMC->Medium(kmed, uniquename.Data(), (*fIdmate)[nmat], isvol, ifield,
334 fieldm, tmaxfd, stemax, deemax, epsil, stmin, ubuf, nbuf);
335 (*fIdtmed)[numed]=kmed;
339 //_______________________________________________________________________
340 void AliModule::AliMatrix(Int_t &nmat, Float_t theta1, Float_t phi1,
341 Float_t theta2, Float_t phi2, Float_t theta3,
345 // Define a rotation matrix. Angles are in degrees.
347 // nmat on output contains the number assigned to the rotation matrix
348 // theta1 polar angle for axis I
349 // phi1 azimuthal angle for axis I
350 // theta2 polar angle for axis II
351 // phi2 azimuthal angle for axis II
352 // theta3 polar angle for axis III
353 // phi3 azimuthal angle for axis III
355 gMC->Matrix(nmat, theta1, phi1, theta2, phi2, theta3, phi3);
358 //_______________________________________________________________________
359 Float_t AliModule::ZMin() const
364 //_______________________________________________________________________
365 Float_t AliModule::ZMax() const
370 //_______________________________________________________________________
371 void AliModule::SetEuclidFile(char* material, char* geometry)
374 // Sets the name of the Euclid file
376 fEuclidMaterial=material;
378 fEuclidGeometry=geometry;
380 char* name = new char[strlen(material)];
381 strcpy(name,material);
382 strcpy(&name[strlen(name)-4],".euc");
383 fEuclidGeometry=name;
388 //_______________________________________________________________________
389 void AliModule::ReadEuclid(const char* filnam, char* topvol)
392 // read in the geometry of the detector in euclid file format
394 // id_det : the detector identification (2=its,...)
395 // topvol : return parameter describing the name of the top
396 // volume of geometry.
401 // several changes have been made by miroslav helbich
402 // subroutine is rewrited to follow the new established way of memory
403 // booking for tracking medias and rotation matrices.
404 // all used tracking media have to be defined first, for this you can use
405 // subroutine greutmed.
406 // top volume is searched as only volume not positioned into another
409 Int_t i, nvol, iret, itmed, irot, numed, npar, ndiv, iaxe;
410 Int_t ndvmx, nr, flag;
411 char key[5], card[77], natmed[21];
412 char name[5], mother[5], shape[5], konly[5], volst[7000][5];
415 Float_t teta1, phi1, teta2, phi2, teta3, phi3, orig, step;
417 const Int_t kMaxRot=5000;
418 Int_t idrot[kMaxRot],istop[7000];
421 // *** The input filnam name will be with extension '.euc'
422 filtmp=gSystem->ExpandPathName(filnam);
423 lun=fopen(filtmp,"r");
426 AliError(Form("Could not open file %s",filnam));
429 //* --- definition of rotation matrix 0 ---
430 TArrayI &idtmed = *fIdtmed;
431 for(i=1; i<kMaxRot; ++i) idrot[i]=-99;
435 for(i=0;i<77;i++) card[i]=0;
436 iret=fscanf(lun,"%77[^\n]",card);
437 if(iret<=0) goto L20;
442 if (!strcmp(key,"TMED")) {
443 sscanf(&card[5],"%d '%[^']'",&itmed,natmed);
444 if( itmed<0 || itmed>=100 ) {
445 AliError(Form("TMED illegal medium number %d for %s",itmed,natmed));
448 //Pad the string with blanks
451 while(i<20) natmed[i++]=' ';
454 if( idtmed[itmed]<=0 ) {
455 AliError(Form("TMED undefined medium number %d for %s",itmed,natmed));
458 gMC->Gckmat(idtmed[itmed],natmed);
460 } else if (!strcmp(key,"ROTM")) {
461 sscanf(&card[4],"%d %f %f %f %f %f %f",&irot,&teta1,&phi1,&teta2,&phi2,&teta3,&phi3);
462 if( irot<=0 || irot>=kMaxRot ) {
463 AliError(Form("ROTM rotation matrix number %d illegal",irot));
466 AliMatrix(idrot[irot],teta1,phi1,teta2,phi2,teta3,phi3);
468 } else if (!strcmp(key,"VOLU")) {
469 sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, shape, &numed, &npar);
471 for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
474 gMC->Gsvolu( name, shape, idtmed[numed], par, npar);
475 //* save the defined volumes
476 strcpy(volst[++nvol],name);
479 } else if (!strcmp(key,"DIVN")) {
480 sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, mother, &ndiv, &iaxe);
481 gMC->Gsdvn ( name, mother, ndiv, iaxe );
483 } else if (!strcmp(key,"DVN2")) {
484 sscanf(&card[5],"'%[^']' '%[^']' %d %d %f %d",name, mother, &ndiv, &iaxe, &orig, &numed);
485 gMC->Gsdvn2( name, mother, ndiv, iaxe, orig,idtmed[numed]);
487 } else if (!strcmp(key,"DIVT")) {
488 sscanf(&card[5],"'%[^']' '%[^']' %f %d %d %d", name, mother, &step, &iaxe, &numed, &ndvmx);
489 gMC->Gsdvt ( name, mother, step, iaxe, idtmed[numed], ndvmx);
491 } else if (!strcmp(key,"DVT2")) {
492 sscanf(&card[5],"'%[^']' '%[^']' %f %d %f %d %d", name, mother, &step, &iaxe, &orig, &numed, &ndvmx);
493 gMC->Gsdvt2 ( name, mother, step, iaxe, orig, idtmed[numed], ndvmx );
495 } else if (!strcmp(key,"POSI")) {
496 sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']'", name, &nr, mother, &xo, &yo, &zo, &irot, konly);
497 if( irot<0 || irot>=kMaxRot ) {
498 Error("ReadEuclid","POSI %s#%d rotation matrix number %d illegal\n",name,nr,irot);
501 if( idrot[irot] == -99) {
502 Error("ReadEuclid","POSI %s#%d undefined matrix number %d\n",name,nr,irot);
505 //*** volume name cannot be the top volume
506 for(i=1;i<=nvol;i++) {
507 if (!strcmp(volst[i],name)) istop[i]=0;
510 gMC->Gspos ( name, nr, mother, xo, yo, zo, idrot[irot], konly );
512 } else if (!strcmp(key,"POSP")) {
513 sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']' %d", name, &nr, mother, &xo, &yo, &zo, &irot, konly, &npar);
514 if( irot<0 || irot>=kMaxRot ) {
515 Error("ReadEuclid","POSP %s#%d rotation matrix number %d illegal\n",name,nr,irot);
518 if( idrot[irot] == -99) {
519 Error("ReadEuclid","POSP %s#%d undefined matrix number %d\n",name,nr,irot);
523 for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
526 //*** volume name cannot be the top volume
527 for(i=1;i<=nvol;i++) {
528 if (!strcmp(volst[i],name)) istop[i]=0;
531 gMC->Gsposp ( name, nr, mother, xo,yo,zo, idrot[irot], konly, par, npar);
534 if (strcmp(key,"END")) goto L10;
535 //* find top volume in the geometry
537 for(i=1;i<=nvol;i++) {
538 if (istop[i] && flag) {
539 AliWarning(Form(" %s is another possible top volume",volst[i]));
541 if (istop[i] && !flag) {
542 strcpy(topvol,volst[i]);
543 AliDebug(2, Form("volume %s taken as a top volume",topvol));
548 AliWarning("top volume not found");
552 //* commented out only for the not cernlib version
553 AliDebug(1, Form("file: %s is now read in",filnam));
558 AliError("reading error or premature end of file");
561 //_______________________________________________________________________
562 void AliModule::ReadEuclidMedia(const char* filnam)
565 // read in the materials and tracking media for the detector
566 // in euclid file format
568 // filnam: name of the input file
569 // id_det: id_det is the detector identification (2=its,...)
571 // author : miroslav helbich
573 Float_t sxmgmx = gAlice->Field()->Max();
574 Int_t isxfld = gAlice->Field()->Integ();
575 Int_t end, i, iret, itmed;
576 char key[5], card[130], natmed[21], namate[21];
581 Int_t nwbuf, isvol, ifield, nmat;
582 Float_t a, z, dens, radl, absl, fieldm, tmaxfd, stemax, deemax, epsil, stmin;
585 for(i=0;i<end;i++) if(filnam[i]=='.') {
590 // *** The input filnam name will be with extension '.euc'
591 AliDebug(1, Form("The file name is %s",filnam)); //Debug
592 filtmp=gSystem->ExpandPathName(filnam);
593 lun=fopen(filtmp,"r");
596 AliWarning(Form("Could not open file %s",filnam));
600 // Retrieve Mag Field parameters
601 Int_t globField=gAlice->Field()->Integ();
602 Float_t globMaxField=gAlice->Field()->Max();
603 // TArrayI &idtmed = *fIdtmed;
606 for(i=0;i<130;i++) card[i]=0;
607 iret=fscanf(lun,"%4s %[^\n]",key,card);
608 if(iret<=0) goto L20;
612 if (!strcmp(key,"MATE")) {
613 sscanf(card,"%d '%[^']' %f %f %f %f %f %d",&imate,namate,&a,&z,&dens,&radl,&absl,&nwbuf);
614 if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
615 //Pad the string with blanks
618 while(i<20) namate[i++]=' ';
621 AliMaterial(imate,namate,a,z,dens,radl,absl,ubuf,nwbuf);
622 //* read tracking medium
623 } else if (!strcmp(key,"TMED")) {
624 sscanf(card,"%d '%[^']' %d %d %d %f %f %f %f %f %f %d",
625 &itmed,natmed,&nmat,&isvol,&ifield,&fieldm,&tmaxfd,
626 &stemax,&deemax,&epsil,&stmin,&nwbuf);
627 if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
628 if (ifield<0) ifield=isxfld;
629 if (fieldm<0) fieldm=sxmgmx;
630 //Pad the string with blanks
633 while(i<20) natmed[i++]=' ';
636 AliMedium(itmed,natmed,nmat,isvol,globField,globMaxField,tmaxfd,
637 stemax,deemax,epsil,stmin,ubuf,nwbuf);
638 // (*fImedia)[idtmed[itmed]-1]=id_det;
642 if (strcmp(key,"END")) goto L10;
645 //* commented out only for the not cernlib version
646 AliDebug(1, Form("file %s is now read in",filnam));
651 AliWarning("reading error or premature end of file");
654 //_______________________________________________________________________
655 void AliModule::AddAlignableVolumes() const
659 AliWarning(Form(" %s still has to implement the AddAlignableVolumes method!",GetName()));
662 //_______________________________________________________________________
663 void AliModule::RemapTrackReferencesIDs(Int_t *map)
666 // Remapping track reference
667 // Called at finish primary
669 if (!fTrackReferences) return;
670 Int_t nEntries = fTrackReferences->GetEntries();
672 for (Int_t i=0;i<nEntries;i++){
673 AliTrackReference * ref = dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(i));
675 Int_t newID = map[ref->GetTrack()];
676 if (newID>=0) ref->SetTrack(newID);
678 ref->SetBit(kNotDeleted,kFALSE);
679 fTrackReferences->RemoveAt(i);
683 fTrackReferences->Compress();
688 //_______________________________________________________________________
689 AliTrackReference* AliModule::FirstTrackReference(Int_t track)
692 // Initialise the hit iterator
693 // Return the address of the first hit for track
694 // If track>=0 the track is read from disk
695 // while if track<0 the first hit of the current
700 if (fRunLoader == 0x0)
701 AliFatal("AliRunLoader not initialized. Can not proceed");
702 fRunLoader->GetAliRun()->GetMCApp()->ResetTrackReferences();
703 fRunLoader->TreeTR()->GetEvent(track);
706 fMaxIterTrackRef = fTrackReferences->GetEntriesFast();
707 fCurrentIterTrackRef = 0;
708 if(fMaxIterTrackRef) return dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(0));
712 //_______________________________________________________________________
713 AliTrackReference* AliModule::NextTrackReference()
716 // Return the next hit for the current track
718 if(fMaxIterTrackRef) {
719 if(++fCurrentIterTrackRef<fMaxIterTrackRef)
720 return dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(fCurrentIterTrackRef));
724 AliWarning("Iterator called without calling FistTrackReference before");
730 //_______________________________________________________________________
731 void AliModule::ResetTrackReferences()
734 // Reset number of hits and the hits array
736 fMaxIterTrackRef = 0;
737 if (fTrackReferences) fTrackReferences->Clear();
740 //_____________________________________________________________________________
742 AliLoader* AliModule::MakeLoader(const char* /*topfoldername*/)
747 //PH Merged with v3-09-08 |
749 //_____________________________________________________________________________
751 void AliModule::SetTreeAddress()
754 // Set branch address for track reference Tree
759 // Branch address for track reference tree
760 TTree *treeTR = TreeTR();
762 if (treeTR && fTrackReferences) {
763 branch = treeTR->GetBranch(GetName());
766 AliDebug(3, Form("(%s) Setting for TrackRefs",GetName()));
767 branch->SetAddress(&fTrackReferences);
771 //can be called before MakeBranch and than does not make sense to issue the warning
772 AliDebug(1, Form("(%s) Failed for Track References. Can not find branch in tree.",
778 //_____________________________________________________________________________
779 AliTrackReference* AliModule::AddTrackReference(Int_t label){
781 // add a trackrefernce to the list
782 if (!fTrackReferences) {
783 AliError("Container trackrefernce not active");
786 Int_t nref = fTrackReferences->GetEntriesFast();
787 TClonesArray &lref = *fTrackReferences;
788 return new(lref[nref]) AliTrackReference(label);
792 //_____________________________________________________________________________
793 void AliModule::MakeBranchTR(Option_t */*option*/)
796 // Makes branch in treeTR
798 AliDebug(2,Form("Making Track Refs. Branch for %s",GetName()));
799 TTree * tree = TreeTR();
800 if (fTrackReferences && tree)
802 TBranch *branch = tree->GetBranch(GetName());
805 AliDebug(2,Form("Branch %s is already in tree.",GetName()));
809 branch = tree->Branch(GetName(),&fTrackReferences);
813 AliDebug(2,Form("FAILED for %s: tree=%#x fTrackReferences=%#x",
814 GetName(),tree,fTrackReferences));
818 //_____________________________________________________________________________
819 TTree* AliModule::TreeTR()
822 // Return TR tree pointer
824 if ( fRunLoader == 0x0)
826 AliError("Can not get the run loader");
830 TTree* tree = fRunLoader->TreeTR();
835 //_____________________________________________________________________________
836 void AliModule::Digits2Raw()
838 // This is a dummy version that just copies the digits file contents
839 // to a raw data file.
841 AliWarning(Form("Dummy version called for %s", GetName()));
843 Int_t nDDLs = AliDAQ::NumberOfDdls(GetName());
845 if (!GetLoader()) return;
846 fstream digitsFile(GetLoader()->GetDigitsFileName(), ios::in);
847 if (!digitsFile) return;
849 digitsFile.seekg(0, ios::end);
850 UInt_t size = digitsFile.tellg();
851 UInt_t ddlSize = 4 * (size / (4*nDDLs));
852 Char_t* buffer = new Char_t[ddlSize+1];
854 for (Int_t iDDL = 0; iDDL < nDDLs; iDDL++) {
856 strcpy(fileName,AliDAQ::DdlFileName(GetName(),iDDL));
857 fstream rawFile(fileName, ios::out);
858 if (!rawFile) return;
860 AliRawDataHeader header;
861 header.fSize = ddlSize + sizeof(header);
862 rawFile.write((char*) &header, sizeof(header));
864 digitsFile.read(buffer, ddlSize);
865 rawFile.write(buffer, ddlSize);