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 Float_t AliModule::fgDensityFactor = 1.0;
62 //_______________________________________________________________________
63 AliModule::AliModule():
76 fCurrentIterTrackRef(0),
80 // Default constructor for the AliModule class
84 //_______________________________________________________________________
85 AliModule::AliModule(const char* name,const char *title):
89 fIdtmed(new TArrayI(100)),
90 fIdmate(new TArrayI(100)),
94 fHistograms(new TList()),
97 fTrackReferences(new TClonesArray("AliTrackReference", 100)),
99 fCurrentIterTrackRef(0),
103 // Normal constructor invoked by all Modules.
104 // Create the list for Module specific histograms
105 // Add this Module to the global list of Modules in Run.
107 // Get the Module numeric ID
109 Int_t id = gAlice->GetModuleID(name);
111 // Module already added !
112 AliWarning(Form("Module: %s already present at %d",name,id));
116 // Add this Module to the list of Modules
118 gAlice->AddModule(this);
120 //PH SetMarkerColor(3);
122 // Clear space for tracking media and material indexes
124 for(Int_t i=0;i<100;i++) (*fIdmate)[i]=(*fIdtmed)[i]=0;
127 //_______________________________________________________________________
128 AliModule::~AliModule()
134 // Remove this Module from the list of Modules
136 TObjArray * modules = gAlice->Modules();
137 if (modules) modules->Remove(this);
139 // Delete ROOT geometry
147 fHistograms->Clear();
151 // Delete track references
152 if (fTrackReferences) {
153 fTrackReferences->Delete();
154 delete fTrackReferences;
155 fTrackReferences = 0;
157 // Delete TArray objects
163 //_______________________________________________________________________
164 void AliModule::Disable()
167 // Disable Module on viewer
173 // Loop through geometry to disable all
174 // nodes for this Module
175 while((node = dynamic_cast<TNode*>(next()))) {
176 node->SetVisibility(-1);
180 //_______________________________________________________________________
181 void AliModule::Enable()
184 // Enable Module on the viewver
190 // Loop through geometry to enable all
191 // nodes for this Module
192 while((node = dynamic_cast<TNode*>(next()))) {
193 node->SetVisibility(3);
197 //_______________________________________________________________________
198 void AliModule::AliMaterial(Int_t imat, const char* name, Float_t a,
199 Float_t z, Float_t dens, Float_t radl,
200 Float_t absl, Float_t *buf, Int_t nwbuf) const
203 // Store the parameters for a material
205 // imat the material index will be stored in (*fIdmate)[imat]
206 // name material name
210 // radl radiation length
211 // absl absorbtion length
212 // buf adress of an array user words
213 // nwbuf number of user words
216 //Build the string uniquename as "DET_materialname"
217 TString uniquename = GetName();
218 uniquename.Append("_");
219 uniquename.Append(name);
220 //if geometry loaded from file only fill fIdmate, else create material too
221 if(gAlice->IsRootGeometry()){
222 TGeoMaterial *mat = gGeoManager->GetMaterial(uniquename.Data());
223 kmat = mat->GetUniqueID();
224 (*fIdmate)[imat]=kmat;
226 if (fgDensityFactor != 1.0)
227 AliWarning(Form("Material density multiplied by %.2f!", fgDensityFactor));
228 gMC->Material(kmat, uniquename.Data(), a, z, dens * fgDensityFactor, radl, absl, buf, nwbuf);
229 (*fIdmate)[imat]=kmat;
233 //_______________________________________________________________________
234 void AliModule::AliGetMaterial(Int_t imat, char* name, Float_t &a,
235 Float_t &z, Float_t &dens, Float_t &radl,
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
254 kmat=(*fIdmate)[imat];
255 gMC->Gfmate(kmat, name, a, z, dens, radl, absl, buf, nwbuf);
259 //_______________________________________________________________________
260 void AliModule::AliMixture(Int_t imat, const char *name, Float_t *a,
261 Float_t *z, Float_t dens, Int_t nlmat,
265 // Defines mixture or compound imat as composed by
266 // nlmat materials defined by arrays a, z and wmat
268 // If nlmat > 0 wmat contains the proportion by
269 // weights of each basic material in the mixture
271 // If nlmat < 0 wmat contains the number of atoms
272 // of eack kind in the molecule of the compound
273 // In this case, wmat is changed on output to the relative weigths.
275 // imat the material index will be stored in (*fIdmate)[imat]
276 // name material name
277 // a array of atomic masses
278 // z array of atomic numbers
280 // nlmat number of components
281 // wmat array of concentrations
284 //Build the string uniquename as "DET_mixturename"
285 TString uniquename = GetName();
286 uniquename.Append("_");
287 uniquename.Append(name);
288 //if geometry loaded from file only fill fIdmate, else create mixture too
289 if(gAlice->IsRootGeometry()){
290 TGeoMaterial *mat = gGeoManager->GetMaterial(uniquename.Data());
291 kmat = mat->GetUniqueID();
292 (*fIdmate)[imat]=kmat;
294 if (fgDensityFactor != 1.0)
295 AliWarning(Form("Material density multiplied by %.2f!", fgDensityFactor));
296 gMC->Mixture(kmat, uniquename.Data(), a, z, dens * fgDensityFactor, nlmat, wmat);
297 (*fIdmate)[imat]=kmat;
301 //_______________________________________________________________________
302 void AliModule::AliMedium(Int_t numed, const char *name, Int_t nmat,
303 Int_t isvol, Int_t ifield, Float_t fieldm,
304 Float_t tmaxfd, Float_t stemax, Float_t deemax,
305 Float_t epsil, Float_t stmin, Float_t *ubuf,
309 // Store the parameters of a tracking medium
311 // numed the medium number is stored into (*fIdtmed)[numed]
313 // nmat the material number is stored into (*fIdmate)[nmat]
314 // isvol sensitive volume if isvol!=0
315 // ifield magnetic field flag (see below)
316 // fieldm maximum magnetic field
317 // tmaxfd maximum deflection angle due to magnetic field
318 // stemax maximum step allowed
319 // deemax maximum fractional energy loss in one step
320 // epsil tracking precision in cm
321 // stmin minimum step due to continuous processes
323 // ifield = 0 no magnetic field
324 // = -1 user decision in guswim
325 // = 1 tracking performed with Runge Kutta
326 // = 2 tracking performed with helix
327 // = 3 constant magnetic field along z
330 //Build the string uniquename as "DET_mediumname"
331 TString uniquename = GetName();
332 uniquename.Append("_");
333 uniquename.Append(name);
334 //if geometry loaded from file only fill fIdtmed, else create medium too
335 if(gAlice->IsRootGeometry()){
336 TGeoMedium *med = gGeoManager->GetMedium(uniquename.Data());
338 (*fIdtmed)[numed]=kmed;
340 gMC->Medium(kmed, uniquename.Data(), (*fIdmate)[nmat], isvol, ifield,
341 fieldm, tmaxfd, stemax, deemax, epsil, stmin, ubuf, nbuf);
342 (*fIdtmed)[numed]=kmed;
346 //_______________________________________________________________________
347 void AliModule::AliMatrix(Int_t &nmat, Float_t theta1, Float_t phi1,
348 Float_t theta2, Float_t phi2, Float_t theta3,
352 // Define a rotation matrix. Angles are in degrees.
354 // nmat on output contains the number assigned to the rotation matrix
355 // theta1 polar angle for axis I
356 // phi1 azimuthal angle for axis I
357 // theta2 polar angle for axis II
358 // phi2 azimuthal angle for axis II
359 // theta3 polar angle for axis III
360 // phi3 azimuthal angle for axis III
362 gMC->Matrix(nmat, theta1, phi1, theta2, phi2, theta3, phi3);
365 //_______________________________________________________________________
366 Float_t AliModule::ZMin() const
371 //_______________________________________________________________________
372 Float_t AliModule::ZMax() const
377 //_______________________________________________________________________
378 void AliModule::SetEuclidFile(char* material, char* geometry)
381 // Sets the name of the Euclid file
383 fEuclidMaterial=material;
385 fEuclidGeometry=geometry;
387 char* name = new char[strlen(material)];
388 strcpy(name,material);
389 strcpy(&name[strlen(name)-4],".euc");
390 fEuclidGeometry=name;
395 //_______________________________________________________________________
396 void AliModule::ReadEuclid(const char* filnam, char* topvol)
399 // read in the geometry of the detector in euclid file format
401 // id_det : the detector identification (2=its,...)
402 // topvol : return parameter describing the name of the top
403 // volume of geometry.
408 // several changes have been made by miroslav helbich
409 // subroutine is rewrited to follow the new established way of memory
410 // booking for tracking medias and rotation matrices.
411 // all used tracking media have to be defined first, for this you can use
412 // subroutine greutmed.
413 // top volume is searched as only volume not positioned into another
416 Int_t i, nvol, iret, itmed, irot, numed, npar, ndiv, iaxe;
417 Int_t ndvmx, nr, flag;
418 char key[5], card[77], natmed[21];
419 char name[5], mother[5], shape[5], konly[5], volst[7000][5];
422 Float_t teta1, phi1, teta2, phi2, teta3, phi3, orig, step;
424 const Int_t kMaxRot=5000;
425 Int_t idrot[kMaxRot],istop[7000];
428 // *** The input filnam name will be with extension '.euc'
429 filtmp=gSystem->ExpandPathName(filnam);
430 lun=fopen(filtmp,"r");
433 AliError(Form("Could not open file %s",filnam));
436 //* --- definition of rotation matrix 0 ---
437 TArrayI &idtmed = *fIdtmed;
438 for(i=1; i<kMaxRot; ++i) idrot[i]=-99;
442 for(i=0;i<77;i++) card[i]=0;
443 iret=fscanf(lun,"%77[^\n]",card);
444 if(iret<=0) goto L20;
449 if (!strcmp(key,"TMED")) {
450 sscanf(&card[5],"%d '%[^']'",&itmed,natmed);
451 if( itmed<0 || itmed>=100 ) {
452 AliError(Form("TMED illegal medium number %d for %s",itmed,natmed));
455 //Pad the string with blanks
458 while(i<20) natmed[i++]=' ';
461 if( idtmed[itmed]<=0 ) {
462 AliError(Form("TMED undefined medium number %d for %s",itmed,natmed));
465 gMC->Gckmat(idtmed[itmed],natmed);
467 } else if (!strcmp(key,"ROTM")) {
468 sscanf(&card[4],"%d %f %f %f %f %f %f",&irot,&teta1,&phi1,&teta2,&phi2,&teta3,&phi3);
469 if( irot<=0 || irot>=kMaxRot ) {
470 AliError(Form("ROTM rotation matrix number %d illegal",irot));
473 AliMatrix(idrot[irot],teta1,phi1,teta2,phi2,teta3,phi3);
475 } else if (!strcmp(key,"VOLU")) {
476 sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, shape, &numed, &npar);
478 for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
481 gMC->Gsvolu( name, shape, idtmed[numed], par, npar);
482 //* save the defined volumes
483 strcpy(volst[++nvol],name);
486 } else if (!strcmp(key,"DIVN")) {
487 sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, mother, &ndiv, &iaxe);
488 gMC->Gsdvn ( name, mother, ndiv, iaxe );
490 } else if (!strcmp(key,"DVN2")) {
491 sscanf(&card[5],"'%[^']' '%[^']' %d %d %f %d",name, mother, &ndiv, &iaxe, &orig, &numed);
492 gMC->Gsdvn2( name, mother, ndiv, iaxe, orig,idtmed[numed]);
494 } else if (!strcmp(key,"DIVT")) {
495 sscanf(&card[5],"'%[^']' '%[^']' %f %d %d %d", name, mother, &step, &iaxe, &numed, &ndvmx);
496 gMC->Gsdvt ( name, mother, step, iaxe, idtmed[numed], ndvmx);
498 } else if (!strcmp(key,"DVT2")) {
499 sscanf(&card[5],"'%[^']' '%[^']' %f %d %f %d %d", name, mother, &step, &iaxe, &orig, &numed, &ndvmx);
500 gMC->Gsdvt2 ( name, mother, step, iaxe, orig, idtmed[numed], ndvmx );
502 } else if (!strcmp(key,"POSI")) {
503 sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']'", name, &nr, mother, &xo, &yo, &zo, &irot, konly);
504 if( irot<0 || irot>=kMaxRot ) {
505 Error("ReadEuclid","POSI %s#%d rotation matrix number %d illegal\n",name,nr,irot);
508 if( idrot[irot] == -99) {
509 Error("ReadEuclid","POSI %s#%d undefined matrix number %d\n",name,nr,irot);
512 //*** volume name cannot be the top volume
513 for(i=1;i<=nvol;i++) {
514 if (!strcmp(volst[i],name)) istop[i]=0;
517 gMC->Gspos ( name, nr, mother, xo, yo, zo, idrot[irot], konly );
519 } else if (!strcmp(key,"POSP")) {
520 sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']' %d", name, &nr, mother, &xo, &yo, &zo, &irot, konly, &npar);
521 if( irot<0 || irot>=kMaxRot ) {
522 Error("ReadEuclid","POSP %s#%d rotation matrix number %d illegal\n",name,nr,irot);
525 if( idrot[irot] == -99) {
526 Error("ReadEuclid","POSP %s#%d undefined matrix number %d\n",name,nr,irot);
530 for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
533 //*** volume name cannot be the top volume
534 for(i=1;i<=nvol;i++) {
535 if (!strcmp(volst[i],name)) istop[i]=0;
538 gMC->Gsposp ( name, nr, mother, xo,yo,zo, idrot[irot], konly, par, npar);
541 if (strcmp(key,"END")) goto L10;
542 //* find top volume in the geometry
544 for(i=1;i<=nvol;i++) {
545 if (istop[i] && flag) {
546 AliWarning(Form(" %s is another possible top volume",volst[i]));
548 if (istop[i] && !flag) {
549 strcpy(topvol,volst[i]);
550 AliDebug(2, Form("volume %s taken as a top volume",topvol));
555 AliWarning("top volume not found");
559 //* commented out only for the not cernlib version
560 AliDebug(1, Form("file: %s is now read in",filnam));
565 AliError("reading error or premature end of file");
568 //_______________________________________________________________________
569 void AliModule::ReadEuclidMedia(const char* filnam)
572 // read in the materials and tracking media for the detector
573 // in euclid file format
575 // filnam: name of the input file
576 // id_det: id_det is the detector identification (2=its,...)
578 // author : miroslav helbich
580 Float_t sxmgmx = gAlice->Field()->Max();
581 Int_t isxfld = gAlice->Field()->Integ();
582 Int_t end, i, iret, itmed;
583 char key[5], card[130], natmed[21], namate[21];
588 Int_t nwbuf, isvol, ifield, nmat;
589 Float_t a, z, dens, radl, absl, fieldm, tmaxfd, stemax, deemax, epsil, stmin;
592 for(i=0;i<end;i++) if(filnam[i]=='.') {
597 // *** The input filnam name will be with extension '.euc'
598 AliDebug(1, Form("The file name is %s",filnam)); //Debug
599 filtmp=gSystem->ExpandPathName(filnam);
600 lun=fopen(filtmp,"r");
603 AliWarning(Form("Could not open file %s",filnam));
607 // Retrieve Mag Field parameters
608 Int_t globField=gAlice->Field()->Integ();
609 Float_t globMaxField=gAlice->Field()->Max();
610 // TArrayI &idtmed = *fIdtmed;
613 for(i=0;i<130;i++) card[i]=0;
614 iret=fscanf(lun,"%4s %[^\n]",key,card);
615 if(iret<=0) goto L20;
619 if (!strcmp(key,"MATE")) {
620 sscanf(card,"%d '%[^']' %f %f %f %f %f %d",&imate,namate,&a,&z,&dens,&radl,&absl,&nwbuf);
621 if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
622 //Pad the string with blanks
625 while(i<20) namate[i++]=' ';
628 AliMaterial(imate,namate,a,z,dens,radl,absl,ubuf,nwbuf);
629 //* read tracking medium
630 } else if (!strcmp(key,"TMED")) {
631 sscanf(card,"%d '%[^']' %d %d %d %f %f %f %f %f %f %d",
632 &itmed,natmed,&nmat,&isvol,&ifield,&fieldm,&tmaxfd,
633 &stemax,&deemax,&epsil,&stmin,&nwbuf);
634 if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
635 if (ifield<0) ifield=isxfld;
636 if (fieldm<0) fieldm=sxmgmx;
637 //Pad the string with blanks
640 while(i<20) natmed[i++]=' ';
643 AliMedium(itmed,natmed,nmat,isvol,globField,globMaxField,tmaxfd,
644 stemax,deemax,epsil,stmin,ubuf,nwbuf);
645 // (*fImedia)[idtmed[itmed]-1]=id_det;
649 if (strcmp(key,"END")) goto L10;
652 //* commented out only for the not cernlib version
653 AliDebug(1, Form("file %s is now read in",filnam));
658 AliWarning("reading error or premature end of file");
661 //_______________________________________________________________________
662 void AliModule::AddAlignableVolumes() const
666 AliWarning(Form(" %s still has to implement the AddAlignableVolumes method!",GetName()));
669 //_______________________________________________________________________
671 AliLoader* AliModule::MakeLoader(const char* /*topfoldername*/)
677 //_____________________________________________________________________________
678 AliTrackReference* AliModule::AddTrackReference(Int_t label, Int_t id){
680 // add a trackrefernce to the list
681 return (gAlice->GetMCApp()->AddTrackReference(label, id));
684 //_______________________________________________________________________
685 AliTrackReference* AliModule::FirstTrackReference(Int_t track)
688 // Initialise the hit iterator
689 // Return the address of the first hit for track
690 // If track>=0 the track is read from disk
691 // while if track0 the first hit of the current
696 if (fRunLoader == 0x0)
697 AliFatal("AliRunLoader not initialized. Can not proceed");
698 fRunLoader->GetAliRun()->GetMCApp()->ResetTrackReferences();
699 fRunLoader->TreeTR()->GetEvent(track);
702 fMaxIterTrackRef = fTrackReferences->GetEntriesFast();
703 fCurrentIterTrackRef = 0;
704 if(fMaxIterTrackRef) return dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(0));
708 //_______________________________________________________________________
709 AliTrackReference* AliModule::NextTrackReference()
712 // Return the next hit for the current track
714 if(fMaxIterTrackRef) {
715 if(++fCurrentIterTrackRef < fMaxIterTrackRef)
716 return dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(fCurrentIterTrackRef));
720 AliWarning("Iterator called without calling FistTrackReference before");
728 //_____________________________________________________________________________
729 TTree* AliModule::TreeTR()
732 // Return TR tree pointer
734 if ( fRunLoader == 0x0)
736 AliError("Can not get the run loader");
740 TTree* tree = fRunLoader->TreeTR();
745 //_____________________________________________________________________________
746 void AliModule::Digits2Raw()
748 // This is a dummy version that just copies the digits file contents
749 // to a raw data file.
751 AliWarning(Form("Dummy version called for %s", GetName()));
753 Int_t nDDLs = AliDAQ::NumberOfDdls(GetName());
755 if (!GetLoader()) return;
756 fstream digitsFile(GetLoader()->GetDigitsFileName(), ios::in);
757 if (!digitsFile) return;
759 digitsFile.seekg(0, ios::end);
760 UInt_t size = digitsFile.tellg();
761 UInt_t ddlSize = 4 * (size / (4*nDDLs));
762 Char_t* buffer = new Char_t[ddlSize+1];
764 for (Int_t iDDL = 0; iDDL < nDDLs; iDDL++) {
766 strcpy(fileName,AliDAQ::DdlFileName(GetName(),iDDL));
767 fstream rawFile(fileName, ios::out);
768 if (!rawFile) return;
770 AliRawDataHeader header;
771 header.fSize = ddlSize + sizeof(header);
772 rawFile.write((char*) &header, sizeof(header));
774 digitsFile.read(buffer, ddlSize);
775 rawFile.write(buffer, ddlSize);