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
107 Int_t id = gAlice->GetModuleID(name);
109 // Module already added !
110 AliWarning(Form("Module: %s already present at %d",name,id));
114 // Add this Module to the list of Modules
116 gAlice->AddModule(this);
118 //PH SetMarkerColor(3);
120 // Clear space for tracking media and material indexes
122 for(Int_t i=0;i<100;i++) (*fIdmate)[i]=(*fIdtmed)[i]=0;
125 //_______________________________________________________________________
126 AliModule::~AliModule()
132 // Remove this Module from the list of Modules
134 TObjArray * modules = gAlice->Modules();
135 if (modules) modules->Remove(this);
137 // Delete ROOT geometry
145 fHistograms->Clear();
149 // Delete track references
150 if (fTrackReferences) {
151 fTrackReferences->Delete();
152 delete fTrackReferences;
153 fTrackReferences = 0;
155 // Delete TArray objects
161 //_______________________________________________________________________
162 void AliModule::Disable()
165 // Disable Module on viewer
171 // Loop through geometry to disable all
172 // nodes for this Module
173 while((node = dynamic_cast<TNode*>(next()))) {
174 node->SetVisibility(-1);
178 //_______________________________________________________________________
179 void AliModule::Enable()
182 // Enable Module on the viewver
188 // Loop through geometry to enable all
189 // nodes for this Module
190 while((node = dynamic_cast<TNode*>(next()))) {
191 node->SetVisibility(3);
195 //_______________________________________________________________________
196 void AliModule::AliMaterial(Int_t imat, const char* name, Float_t a,
197 Float_t z, Float_t dens, Float_t radl,
198 Float_t absl, Float_t *buf, Int_t nwbuf) const
201 // Store the parameters for a material
203 // imat the material index will be stored in (*fIdmate)[imat]
204 // name material name
208 // radl radiation length
209 // absl absorbtion length
210 // buf adress of an array user words
211 // nwbuf number of user words
214 //Build the string uniquename as "DET_materialname"
215 TString uniquename = GetName();
216 uniquename.Append("_");
217 uniquename.Append(name);
218 //if geometry loaded from file only fill fIdmate, else create material too
219 if(gAlice->IsRootGeometry()){
220 TGeoMaterial *mat = gGeoManager->GetMaterial(uniquename.Data());
221 kmat = mat->GetUniqueID();
222 (*fIdmate)[imat]=kmat;
224 gMC->Material(kmat, uniquename.Data(), a, z, dens, radl, absl, buf, nwbuf);
225 (*fIdmate)[imat]=kmat;
229 //_______________________________________________________________________
230 void AliModule::AliGetMaterial(Int_t imat, char* name, Float_t &a,
231 Float_t &z, Float_t &dens, Float_t &radl,
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
250 kmat=(*fIdmate)[imat];
251 gMC->Gfmate(kmat, name, a, z, dens, radl, absl, buf, nwbuf);
255 //_______________________________________________________________________
256 void AliModule::AliMixture(Int_t imat, const char *name, Float_t *a,
257 Float_t *z, Float_t dens, Int_t nlmat,
261 // Defines mixture or compound imat as composed by
262 // nlmat materials defined by arrays a, z and wmat
264 // If nlmat > 0 wmat contains the proportion by
265 // weights of each basic material in the mixture
267 // If nlmat < 0 wmat contains the number of atoms
268 // of eack kind in the molecule of the compound
269 // In this case, wmat is changed on output to the relative weigths.
271 // imat the material index will be stored in (*fIdmate)[imat]
272 // name material name
273 // a array of atomic masses
274 // z array of atomic numbers
276 // nlmat number of components
277 // wmat array of concentrations
280 //Build the string uniquename as "DET_mixturename"
281 TString uniquename = GetName();
282 uniquename.Append("_");
283 uniquename.Append(name);
284 //if geometry loaded from file only fill fIdmate, else create mixture too
285 if(gAlice->IsRootGeometry()){
286 TGeoMaterial *mat = gGeoManager->GetMaterial(uniquename.Data());
287 kmat = mat->GetUniqueID();
288 (*fIdmate)[imat]=kmat;
290 gMC->Mixture(kmat, uniquename.Data(), a, z, dens, nlmat, wmat);
291 (*fIdmate)[imat]=kmat;
295 //_______________________________________________________________________
296 void AliModule::AliMedium(Int_t numed, const char *name, Int_t nmat,
297 Int_t isvol, Int_t ifield, Float_t fieldm,
298 Float_t tmaxfd, Float_t stemax, Float_t deemax,
299 Float_t epsil, Float_t stmin, Float_t *ubuf,
303 // Store the parameters of a tracking medium
305 // numed the medium number is stored into (*fIdtmed)[numed]
307 // nmat the material number is stored into (*fIdmate)[nmat]
308 // isvol sensitive volume if isvol!=0
309 // ifield magnetic field flag (see below)
310 // fieldm maximum magnetic field
311 // tmaxfd maximum deflection angle due to magnetic field
312 // stemax maximum step allowed
313 // deemax maximum fractional energy loss in one step
314 // epsil tracking precision in cm
315 // stmin minimum step due to continuous processes
317 // ifield = 0 no magnetic field
318 // = -1 user decision in guswim
319 // = 1 tracking performed with Runge Kutta
320 // = 2 tracking performed with helix
321 // = 3 constant magnetic field along z
324 //Build the string uniquename as "DET_mediumname"
325 TString uniquename = GetName();
326 uniquename.Append("_");
327 uniquename.Append(name);
328 //if geometry loaded from file only fill fIdtmed, else create medium too
329 if(gAlice->IsRootGeometry()){
330 TGeoMedium *med = gGeoManager->GetMedium(uniquename.Data());
332 (*fIdtmed)[numed]=kmed;
334 gMC->Medium(kmed, uniquename.Data(), (*fIdmate)[nmat], isvol, ifield,
335 fieldm, tmaxfd, stemax, deemax, epsil, stmin, ubuf, nbuf);
336 (*fIdtmed)[numed]=kmed;
340 //_______________________________________________________________________
341 void AliModule::AliMatrix(Int_t &nmat, Float_t theta1, Float_t phi1,
342 Float_t theta2, Float_t phi2, Float_t theta3,
346 // Define a rotation matrix. Angles are in degrees.
348 // nmat on output contains the number assigned to the rotation matrix
349 // theta1 polar angle for axis I
350 // phi1 azimuthal angle for axis I
351 // theta2 polar angle for axis II
352 // phi2 azimuthal angle for axis II
353 // theta3 polar angle for axis III
354 // phi3 azimuthal angle for axis III
356 gMC->Matrix(nmat, theta1, phi1, theta2, phi2, theta3, phi3);
359 //_______________________________________________________________________
360 Float_t AliModule::ZMin() const
365 //_______________________________________________________________________
366 Float_t AliModule::ZMax() const
371 //_______________________________________________________________________
372 void AliModule::SetEuclidFile(char* material, char* geometry)
375 // Sets the name of the Euclid file
377 fEuclidMaterial=material;
379 fEuclidGeometry=geometry;
381 char* name = new char[strlen(material)];
382 strcpy(name,material);
383 strcpy(&name[strlen(name)-4],".euc");
384 fEuclidGeometry=name;
389 //_______________________________________________________________________
390 void AliModule::ReadEuclid(const char* filnam, char* topvol)
393 // read in the geometry of the detector in euclid file format
395 // id_det : the detector identification (2=its,...)
396 // topvol : return parameter describing the name of the top
397 // volume of geometry.
402 // several changes have been made by miroslav helbich
403 // subroutine is rewrited to follow the new established way of memory
404 // booking for tracking medias and rotation matrices.
405 // all used tracking media have to be defined first, for this you can use
406 // subroutine greutmed.
407 // top volume is searched as only volume not positioned into another
410 Int_t i, nvol, iret, itmed, irot, numed, npar, ndiv, iaxe;
411 Int_t ndvmx, nr, flag;
412 char key[5], card[77], natmed[21];
413 char name[5], mother[5], shape[5], konly[5], volst[7000][5];
416 Float_t teta1, phi1, teta2, phi2, teta3, phi3, orig, step;
418 const Int_t kMaxRot=5000;
419 Int_t idrot[kMaxRot],istop[7000];
422 // *** The input filnam name will be with extension '.euc'
423 filtmp=gSystem->ExpandPathName(filnam);
424 lun=fopen(filtmp,"r");
427 AliError(Form("Could not open file %s",filnam));
430 //* --- definition of rotation matrix 0 ---
431 TArrayI &idtmed = *fIdtmed;
432 for(i=1; i<kMaxRot; ++i) idrot[i]=-99;
436 for(i=0;i<77;i++) card[i]=0;
437 iret=fscanf(lun,"%77[^\n]",card);
438 if(iret<=0) goto L20;
443 if (!strcmp(key,"TMED")) {
444 sscanf(&card[5],"%d '%[^']'",&itmed,natmed);
445 if( itmed<0 || itmed>=100 ) {
446 AliError(Form("TMED illegal medium number %d for %s",itmed,natmed));
449 //Pad the string with blanks
452 while(i<20) natmed[i++]=' ';
455 if( idtmed[itmed]<=0 ) {
456 AliError(Form("TMED undefined medium number %d for %s",itmed,natmed));
459 gMC->Gckmat(idtmed[itmed],natmed);
461 } else if (!strcmp(key,"ROTM")) {
462 sscanf(&card[4],"%d %f %f %f %f %f %f",&irot,&teta1,&phi1,&teta2,&phi2,&teta3,&phi3);
463 if( irot<=0 || irot>=kMaxRot ) {
464 AliError(Form("ROTM rotation matrix number %d illegal",irot));
467 AliMatrix(idrot[irot],teta1,phi1,teta2,phi2,teta3,phi3);
469 } else if (!strcmp(key,"VOLU")) {
470 sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, shape, &numed, &npar);
472 for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
475 gMC->Gsvolu( name, shape, idtmed[numed], par, npar);
476 //* save the defined volumes
477 strcpy(volst[++nvol],name);
480 } else if (!strcmp(key,"DIVN")) {
481 sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, mother, &ndiv, &iaxe);
482 gMC->Gsdvn ( name, mother, ndiv, iaxe );
484 } else if (!strcmp(key,"DVN2")) {
485 sscanf(&card[5],"'%[^']' '%[^']' %d %d %f %d",name, mother, &ndiv, &iaxe, &orig, &numed);
486 gMC->Gsdvn2( name, mother, ndiv, iaxe, orig,idtmed[numed]);
488 } else if (!strcmp(key,"DIVT")) {
489 sscanf(&card[5],"'%[^']' '%[^']' %f %d %d %d", name, mother, &step, &iaxe, &numed, &ndvmx);
490 gMC->Gsdvt ( name, mother, step, iaxe, idtmed[numed], ndvmx);
492 } else if (!strcmp(key,"DVT2")) {
493 sscanf(&card[5],"'%[^']' '%[^']' %f %d %f %d %d", name, mother, &step, &iaxe, &orig, &numed, &ndvmx);
494 gMC->Gsdvt2 ( name, mother, step, iaxe, orig, idtmed[numed], ndvmx );
496 } else if (!strcmp(key,"POSI")) {
497 sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']'", name, &nr, mother, &xo, &yo, &zo, &irot, konly);
498 if( irot<0 || irot>=kMaxRot ) {
499 Error("ReadEuclid","POSI %s#%d rotation matrix number %d illegal\n",name,nr,irot);
502 if( idrot[irot] == -99) {
503 Error("ReadEuclid","POSI %s#%d undefined matrix number %d\n",name,nr,irot);
506 //*** volume name cannot be the top volume
507 for(i=1;i<=nvol;i++) {
508 if (!strcmp(volst[i],name)) istop[i]=0;
511 gMC->Gspos ( name, nr, mother, xo, yo, zo, idrot[irot], konly );
513 } else if (!strcmp(key,"POSP")) {
514 sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']' %d", name, &nr, mother, &xo, &yo, &zo, &irot, konly, &npar);
515 if( irot<0 || irot>=kMaxRot ) {
516 Error("ReadEuclid","POSP %s#%d rotation matrix number %d illegal\n",name,nr,irot);
519 if( idrot[irot] == -99) {
520 Error("ReadEuclid","POSP %s#%d undefined matrix number %d\n",name,nr,irot);
524 for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
527 //*** volume name cannot be the top volume
528 for(i=1;i<=nvol;i++) {
529 if (!strcmp(volst[i],name)) istop[i]=0;
532 gMC->Gsposp ( name, nr, mother, xo,yo,zo, idrot[irot], konly, par, npar);
535 if (strcmp(key,"END")) goto L10;
536 //* find top volume in the geometry
538 for(i=1;i<=nvol;i++) {
539 if (istop[i] && flag) {
540 AliWarning(Form(" %s is another possible top volume",volst[i]));
542 if (istop[i] && !flag) {
543 strcpy(topvol,volst[i]);
544 AliDebug(2, Form("volume %s taken as a top volume",topvol));
549 AliWarning("top volume not found");
553 //* commented out only for the not cernlib version
554 AliDebug(1, Form("file: %s is now read in",filnam));
559 AliError("reading error or premature end of file");
562 //_______________________________________________________________________
563 void AliModule::ReadEuclidMedia(const char* filnam)
566 // read in the materials and tracking media for the detector
567 // in euclid file format
569 // filnam: name of the input file
570 // id_det: id_det is the detector identification (2=its,...)
572 // author : miroslav helbich
574 Float_t sxmgmx = gAlice->Field()->Max();
575 Int_t isxfld = gAlice->Field()->Integ();
576 Int_t end, i, iret, itmed;
577 char key[5], card[130], natmed[21], namate[21];
582 Int_t nwbuf, isvol, ifield, nmat;
583 Float_t a, z, dens, radl, absl, fieldm, tmaxfd, stemax, deemax, epsil, stmin;
586 for(i=0;i<end;i++) if(filnam[i]=='.') {
591 // *** The input filnam name will be with extension '.euc'
592 AliDebug(1, Form("The file name is %s",filnam)); //Debug
593 filtmp=gSystem->ExpandPathName(filnam);
594 lun=fopen(filtmp,"r");
597 AliWarning(Form("Could not open file %s",filnam));
601 // Retrieve Mag Field parameters
602 Int_t globField=gAlice->Field()->Integ();
603 Float_t globMaxField=gAlice->Field()->Max();
604 // TArrayI &idtmed = *fIdtmed;
607 for(i=0;i<130;i++) card[i]=0;
608 iret=fscanf(lun,"%4s %[^\n]",key,card);
609 if(iret<=0) goto L20;
613 if (!strcmp(key,"MATE")) {
614 sscanf(card,"%d '%[^']' %f %f %f %f %f %d",&imate,namate,&a,&z,&dens,&radl,&absl,&nwbuf);
615 if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
616 //Pad the string with blanks
619 while(i<20) namate[i++]=' ';
622 AliMaterial(imate,namate,a,z,dens,radl,absl,ubuf,nwbuf);
623 //* read tracking medium
624 } else if (!strcmp(key,"TMED")) {
625 sscanf(card,"%d '%[^']' %d %d %d %f %f %f %f %f %f %d",
626 &itmed,natmed,&nmat,&isvol,&ifield,&fieldm,&tmaxfd,
627 &stemax,&deemax,&epsil,&stmin,&nwbuf);
628 if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
629 if (ifield<0) ifield=isxfld;
630 if (fieldm<0) fieldm=sxmgmx;
631 //Pad the string with blanks
634 while(i<20) natmed[i++]=' ';
637 AliMedium(itmed,natmed,nmat,isvol,globField,globMaxField,tmaxfd,
638 stemax,deemax,epsil,stmin,ubuf,nwbuf);
639 // (*fImedia)[idtmed[itmed]-1]=id_det;
643 if (strcmp(key,"END")) goto L10;
646 //* commented out only for the not cernlib version
647 AliDebug(1, Form("file %s is now read in",filnam));
652 AliWarning("reading error or premature end of file");
655 //_______________________________________________________________________
656 void AliModule::AddAlignableVolumes() const
660 AliWarning(Form(" %s still has to implement the AddAlignableVolumes method!",GetName()));
663 //_______________________________________________________________________
665 AliLoader* AliModule::MakeLoader(const char* /*topfoldername*/)
671 //_____________________________________________________________________________
672 AliTrackReference* AliModule::AddTrackReference(Int_t label, Int_t id){
674 // add a trackrefernce to the list
675 return (gAlice->GetMCApp()->AddTrackReference(label, id));
678 //_______________________________________________________________________
679 AliTrackReference* AliModule::FirstTrackReference(Int_t track)
682 // Initialise the hit iterator
683 // Return the address of the first hit for track
684 // If track>=0 the track is read from disk
685 // while if track0 the first hit of the current
690 if (fRunLoader == 0x0)
691 AliFatal("AliRunLoader not initialized. Can not proceed");
692 fRunLoader->GetAliRun()->GetMCApp()->ResetTrackReferences();
693 fRunLoader->TreeTR()->GetEvent(track);
696 fMaxIterTrackRef = fTrackReferences->GetEntriesFast();
697 fCurrentIterTrackRef = 0;
698 if(fMaxIterTrackRef) return dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(0));
702 //_______________________________________________________________________
703 AliTrackReference* AliModule::NextTrackReference()
706 // Return the next hit for the current track
708 if(fMaxIterTrackRef) {
709 if(++fCurrentIterTrackRef < fMaxIterTrackRef)
710 return dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(fCurrentIterTrackRef));
714 AliWarning("Iterator called without calling FistTrackReference before");
722 //_____________________________________________________________________________
723 TTree* AliModule::TreeTR()
726 // Return TR tree pointer
728 if ( fRunLoader == 0x0)
730 AliError("Can not get the run loader");
734 TTree* tree = fRunLoader->TreeTR();
739 //_____________________________________________________________________________
740 void AliModule::Digits2Raw()
742 // This is a dummy version that just copies the digits file contents
743 // to a raw data file.
745 AliWarning(Form("Dummy version called for %s", GetName()));
747 Int_t nDDLs = AliDAQ::NumberOfDdls(GetName());
749 if (!GetLoader()) return;
750 fstream digitsFile(GetLoader()->GetDigitsFileName(), ios::in);
751 if (!digitsFile) return;
753 digitsFile.seekg(0, ios::end);
754 UInt_t size = digitsFile.tellg();
755 UInt_t ddlSize = 4 * (size / (4*nDDLs));
756 Char_t* buffer = new Char_t[ddlSize+1];
758 for (Int_t iDDL = 0; iDDL < nDDLs; iDDL++) {
760 strcpy(fileName,AliDAQ::DdlFileName(GetName(),iDDL));
761 fstream rawFile(fileName, ios::out);
762 if (!rawFile) return;
764 AliRawDataHeader header;
765 header.fSize = ddlSize + sizeof(header);
766 rawFile.write((char*) &header, sizeof(header));
768 digitsFile.read(buffer, ddlSize);
769 rawFile.write(buffer, ddlSize);