]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliModule.cxx
New ITS code replacing the old structure and simulations code.
[u/mrichter/AliRoot.git] / STEER / AliModule.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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  **************************************************************************/
15
16 /*
17 $Log$
18 Revision 1.8  2000/02/23 16:25:22  fca
19 AliVMC and AliGeant3 classes introduced
20 ReadEuclid moved from AliRun to AliModule
21
22 Revision 1.7  1999/09/29 09:24:29  fca
23 Introduction of the Copyright and cvs Log
24
25 */
26
27 ///////////////////////////////////////////////////////////////////////////////
28 //                                                                           //
29 // Base class for ALICE modules. Both sensitive modules (Modules) and      //
30 // non-sensitive ones are described by this base class. This class           //
31 // supports the hit and digit trees produced by the simulation and also      //
32 // the objects produced by the reconstruction.                               //
33 //                                                                           //
34 // This class is also responsible for building the geometry of the           //
35 // Modules.                                                                //
36 //                                                                           //
37 //Begin_Html
38 /*
39 <img src="picts/AliModuleClass.gif">
40 */
41 //End_Html
42 //                                                                           //
43 ///////////////////////////////////////////////////////////////////////////////
44 #include "AliModule.h"
45 #include "AliRun.h"
46 #include "AliHit.h"
47 #include "AliPoints.h"
48 #include <TClass.h>
49 #include <TNode.h>
50 #include <TRandom.h>
51
52 ClassImp(AliModule)
53  
54 //_____________________________________________________________________________
55 AliModule::AliModule()
56 {
57   //
58   // Default constructor for the AliModule class
59   //
60   fHistograms = 0;
61   fNodes      = 0;
62 }
63  
64 //_____________________________________________________________________________
65 AliModule::AliModule(const char* name,const char *title):TNamed(name,title)
66 {
67   //
68   // Normal constructor invoked by all Modules.
69   // Create the list for Module specific histograms
70   // Add this Module to the global list of Modules in Run.
71   //
72   //
73   // Initialises the histogram list
74   fHistograms = new TList();
75   //
76   // Initialises the list of ROOT TNodes
77   fNodes      = new TList();
78   //  
79   // Get the Module numeric ID
80   Int_t id = gAlice->GetModuleID(name);
81   if (id>=0) {
82     // Module already added !
83      Warning("Ctor","Module: %s already present at %d\n",name,id);
84      return;
85   }
86   //
87   // Add this Module to the list of Modules
88   gAlice->Modules()->Add(this);
89   //
90   //
91   SetMarkerColor(3);
92   //
93   // Allocate space for tracking media and material indexes
94   fIdtmed = new TArrayI(100);
95   fIdmate  = new TArrayI(100);
96   for(Int_t i=0;i<100;i++) (*fIdmate)[i]=(*fIdtmed)[i]=0;
97   //
98   // Prepare to find the tracking media range
99   fLoMedium = 65536;
100   fHiMedium = 0;
101 }
102  
103 //_____________________________________________________________________________
104 AliModule::~AliModule()
105 {
106   //
107   // Destructor
108   //
109   fHistograms = 0;
110   //
111   // Delete ROOT geometry
112   if(fNodes) {
113     fNodes->Clear();
114     delete fNodes;
115   }
116   //
117   // Delete TArray objects
118   delete fIdtmed;
119   delete fIdmate;
120 }
121  
122 //_____________________________________________________________________________
123 void AliModule::Disable()
124 {
125   //
126   // Disable Module on viewer
127   //
128   fActive = kFALSE;
129   TIter next(fNodes);
130   TNode *node;
131   //
132   // Loop through geometry to disable all
133   // nodes for this Module
134   while((node = (TNode*)next())) {
135     node->SetVisibility(0);
136   }   
137 }
138
139 //_____________________________________________________________________________
140 Int_t AliModule::DistancetoPrimitive(Int_t, Int_t)
141 {
142   //
143   // Return distance from mouse pointer to object
144   // Dummy routine for the moment
145   //
146   return 9999;
147 }
148
149 //_____________________________________________________________________________
150 void AliModule::Enable()
151 {
152   //
153   // Enable Module on the viewver
154   //
155   fActive = kTRUE;
156   TIter next(fNodes);
157   TNode *node;
158   //
159   // Loop through geometry to enable all
160   // nodes for this Module
161   while((node = (TNode*)next())) {
162     node->SetVisibility(1);
163   }   
164 }
165
166 //_____________________________________________________________________________
167 void AliModule::AliMaterial(Int_t imat, const char* name, Float_t a, 
168                               Float_t z, Float_t dens, Float_t radl,
169                               Float_t absl, Float_t *buf, Int_t nwbuf) const
170 {
171   //
172   // Store the parameters for a material
173   //
174   // imat        the material index will be stored in (*fIdmate)[imat]
175   // name        material name
176   // a           atomic mass
177   // z           atomic number
178   // dens        density
179   // radl        radiation length
180   // absl        absorbtion length
181   // buf         adress of an array user words
182   // nwbuf       number of user words
183   //
184   Int_t kmat;
185   gMC->Material(kmat, name, a, z, dens, radl, absl, buf, nwbuf);
186   (*fIdmate)[imat]=kmat;
187 }
188   
189 //_____________________________________________________________________________
190 void AliModule::AliGetMaterial(Int_t imat, char* name, Float_t &a, 
191                               Float_t &z, Float_t &dens, Float_t &radl,
192                               Float_t &absl)
193 {
194   //
195   // Store the parameters for a material
196   //
197   // imat        the material index will be stored in (*fIdmate)[imat]
198   // name        material name
199   // a           atomic mass
200   // z           atomic number
201   // dens        density
202   // radl        radiation length
203   // absl        absorbtion length
204   // buf         adress of an array user words
205   // nwbuf       number of user words
206   //
207
208   Float_t buf[10];
209   Int_t nwbuf, kmat;
210   kmat=(*fIdmate)[imat];
211   gMC->Gfmate(kmat, name, a, z, dens, radl, absl, buf, nwbuf);
212 }
213   
214
215 //_____________________________________________________________________________
216 void AliModule::AliMixture(Int_t imat, const char *name, Float_t *a,
217                              Float_t *z, Float_t dens, Int_t nlmat,
218                              Float_t *wmat) const
219
220   //
221   // Defines mixture or compound imat as composed by 
222   // nlmat materials defined by arrays a, z and wmat
223   // 
224   // If nlmat > 0 wmat contains the proportion by
225   // weights of each basic material in the mixture  
226   // 
227   // If nlmat < 0 wmat contains the number of atoms 
228   // of eack kind in the molecule of the compound
229   // In this case, wmat is changed on output to the relative weigths.
230   //
231   // imat        the material index will be stored in (*fIdmate)[imat]
232   // name        material name
233   // a           array of atomic masses
234   // z           array of atomic numbers
235   // dens        density
236   // nlmat       number of components
237   // wmat        array of concentrations
238   //
239   Int_t kmat;
240   gMC->Mixture(kmat, name, a, z, dens, nlmat, wmat);
241   (*fIdmate)[imat]=kmat;
242
243  
244 //_____________________________________________________________________________
245 void AliModule::AliMedium(Int_t numed, const char *name, Int_t nmat,
246                             Int_t isvol, Int_t ifield, Float_t fieldm,
247                             Float_t tmaxfd, Float_t stemax, Float_t deemax,
248                             Float_t epsil, Float_t stmin, Float_t *ubuf,
249                             Int_t nbuf) const
250
251   //
252   // Store the parameters of a tracking medium
253   //
254   // numed       the medium number is stored into (*fIdtmed)[numed]
255   // name        medium name
256   // nmat        the material number is stored into (*fIdmate)[nmat]
257   // isvol       sensitive volume if isvol!=0
258   // ifield      magnetic field flag (see below)
259   // fieldm      maximum magnetic field
260   // tmaxfd      maximum deflection angle due to magnetic field
261   // stemax      maximum step allowed
262   // deemax      maximum fractional energy loss in one step
263   // epsil       tracking precision in cm
264   // stmin       minimum step due to continuous processes
265   //
266   // ifield =  0       no magnetic field
267   //        = -1       user decision in guswim
268   //        =  1       tracking performed with Runge Kutta
269   //        =  2       tracking performed with helix
270   //        =  3       constant magnetic field along z
271   //  
272   Int_t kmed;
273   gMC->Medium(kmed,name, (*fIdmate)[nmat], isvol, ifield, fieldm,
274                          tmaxfd, stemax, deemax, epsil, stmin, ubuf, nbuf); 
275   (*fIdtmed)[numed]=kmed;
276
277  
278 //_____________________________________________________________________________
279 void AliModule::AliMatrix(Int_t &nmat, Float_t theta1, Float_t phi1,
280                             Float_t theta2, Float_t phi2, Float_t theta3,
281                             Float_t phi3) const
282 {
283   // 
284   // Define a rotation matrix. Angles are in degrees.
285   //
286   // nmat        on output contains the number assigned to the rotation matrix
287   // theta1      polar angle for axis I
288   // phi1        azimuthal angle for axis I
289   // theta2      polar angle for axis II
290   // phi2        azimuthal angle for axis II
291   // theta3      polar angle for axis III
292   // phi3        azimuthal angle for axis III
293   //
294   gMC->Matrix(nmat, theta1, phi1, theta2, phi2, theta3, phi3); 
295
296
297 //_____________________________________________________________________________
298 void AliModule::SetEuclidFile(char* material, char* geometry)
299 {
300   //
301   // Sets the name of the Euclid file
302   //
303   fEuclidMaterial=material;
304   if(geometry) {
305     fEuclidGeometry=geometry;
306   } else {
307     char* name = new char[strlen(material)];
308     strcpy(name,material);
309     strcpy(&name[strlen(name)-4],".euc");
310     fEuclidGeometry=name;
311     delete [] name;
312   }
313 }
314  
315 //_____________________________________________________________________________
316 void AliModule::ReadEuclid(const char* filnam, char* topvol)
317 {
318   //                                                                     
319   //       read in the geometry of the detector in euclid file format    
320   //                                                                     
321   //        id_det : the detector identification (2=its,...)            
322   //        topvol : return parameter describing the name of the top    
323   //        volume of geometry.                                          
324   //                                                                     
325   //            author : m. maire                                        
326   //                                                                     
327   //     28.07.98
328   //     several changes have been made by miroslav helbich
329   //     subroutine is rewrited to follow the new established way of memory
330   //     booking for tracking medias and rotation matrices.
331   //     all used tracking media have to be defined first, for this you can use
332   //     subroutine  greutmed.
333   //     top volume is searched as only volume not positioned into another 
334   //
335
336   Int_t i, nvol, iret, itmed, irot, numed, npar, ndiv, iaxe;
337   Int_t ndvmx, nr, flag;
338   char key[5], card[77], natmed[21];
339   char name[5], mother[5], shape[5], konly[5], volst[7000][5];
340   char *filtmp;
341   Float_t par[50];
342   Float_t teta1, phi1, teta2, phi2, teta3, phi3, orig, step;
343   Float_t xo, yo, zo;
344   const Int_t maxrot=5000;
345   Int_t idrot[maxrot],istop[7000];
346   FILE *lun;
347   //
348   // *** The input filnam name will be with extension '.euc'
349   filtmp=gSystem->ExpandPathName(filnam);
350   lun=fopen(filtmp,"r");
351   delete [] filtmp;
352   if(!lun) {
353     Error("ReadEuclid","Could not open file %s\n",filnam);
354     return;
355   }
356   //* --- definition of rotation matrix 0 ---  
357   TArrayI &idtmed = *fIdtmed;
358   for(i=1; i<maxrot; ++i) idrot[i]=-99;
359   idrot[0]=0;
360   nvol=0;
361  L10:
362   for(i=0;i<77;i++) card[i]=0;
363   iret=fscanf(lun,"%77[^\n]",card);
364   if(iret<=0) goto L20;
365   fscanf(lun,"%*c");
366   //*
367   strncpy(key,card,4);
368   key[4]='\0';
369   if (!strcmp(key,"TMED")) {
370     sscanf(&card[5],"%d '%[^']'",&itmed,natmed);
371     if( itmed<0 || itmed>=100 ) {
372       Error("ReadEuclid","TMED illegal medium number %d for %s\n",itmed,natmed);
373       exit(1);
374     }
375     //Pad the string with blanks
376     i=-1;
377     while(natmed[++i]);
378     while(i<20) natmed[i++]=' ';
379     natmed[i]='\0';
380     //
381     if( idtmed[itmed]<=0 ) {
382       Error("ReadEuclid","TMED undefined medium number %d for %s\n",itmed,natmed);
383       exit(1);
384     }
385     gMC->Gckmat(idtmed[itmed],natmed);
386     //*
387   } else if (!strcmp(key,"ROTM")) {
388     sscanf(&card[4],"%d %f %f %f %f %f %f",&irot,&teta1,&phi1,&teta2,&phi2,&teta3,&phi3);
389     if( irot<=0 || irot>=maxrot ) {
390       Error("ReadEuclid","ROTM rotation matrix number %d illegal\n",irot);
391       exit(1);
392     }
393     AliMatrix(idrot[irot],teta1,phi1,teta2,phi2,teta3,phi3);
394     //*
395   } else if (!strcmp(key,"VOLU")) {
396     sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, shape, &numed, &npar);
397     if (npar>0) {
398       for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
399       fscanf(lun,"%*c");
400     }
401     gMC->Gsvolu( name, shape, idtmed[numed], par, npar);
402     //*     save the defined volumes
403     strcpy(volst[++nvol],name);
404     istop[nvol]=1;
405     //*
406   } else if (!strcmp(key,"DIVN")) {
407     sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, mother, &ndiv, &iaxe);
408     gMC->Gsdvn  ( name, mother, ndiv, iaxe );
409     //*
410   } else if (!strcmp(key,"DVN2")) {
411     sscanf(&card[5],"'%[^']' '%[^']' %d %d %f %d",name, mother, &ndiv, &iaxe, &orig, &numed);
412     gMC->Gsdvn2( name, mother, ndiv, iaxe, orig,idtmed[numed]);
413     //*
414   } else if (!strcmp(key,"DIVT")) {
415     sscanf(&card[5],"'%[^']' '%[^']' %f %d %d %d", name, mother, &step, &iaxe, &numed, &ndvmx);
416     gMC->Gsdvt ( name, mother, step, iaxe, idtmed[numed], ndvmx);
417     //*
418   } else if (!strcmp(key,"DVT2")) {
419     sscanf(&card[5],"'%[^']' '%[^']' %f %d %f %d %d", name, mother, &step, &iaxe, &orig, &numed, &ndvmx);
420     gMC->Gsdvt2 ( name, mother, step, iaxe, orig, idtmed[numed], ndvmx );
421     //*
422   } else if (!strcmp(key,"POSI")) {
423     sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']'", name, &nr, mother, &xo, &yo, &zo, &irot, konly);
424     if( irot<0 || irot>=maxrot ) {
425       Error("ReadEuclid","POSI %s#%d rotation matrix number %d illegal\n",name,nr,irot);
426       exit(1);
427     }
428     if( idrot[irot] == -99) {
429       Error("ReadEuclid","POSI %s#%d undefined matrix number %d\n",name,nr,irot);
430       exit(1);
431     }
432     //*** volume name cannot be the top volume
433     for(i=1;i<=nvol;i++) {
434       if (!strcmp(volst[i],name)) istop[i]=0;
435     }
436     //*
437     gMC->Gspos  ( name, nr, mother, xo, yo, zo, idrot[irot], konly );
438     //*
439   } else if (!strcmp(key,"POSP")) {
440     sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']' %d", name, &nr, mother, &xo, &yo, &zo, &irot, konly, &npar);
441     if( irot<0 || irot>=maxrot ) {
442       Error("ReadEuclid","POSP %s#%d rotation matrix number %d illegal\n",name,nr,irot);
443       exit(1);
444     }
445     if( idrot[irot] == -99) {
446       Error("ReadEuclid","POSP %s#%d undefined matrix number %d\n",name,nr,irot);
447       exit(1);
448     }
449     if (npar > 0) {
450       for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
451       fscanf(lun,"%*c");
452     }
453     //*** volume name cannot be the top volume
454     for(i=1;i<=nvol;i++) {
455       if (!strcmp(volst[i],name)) istop[i]=0;
456     }
457     //*
458     gMC->Gsposp ( name, nr, mother, xo,yo,zo, idrot[irot], konly, par, npar);
459   }
460   //*
461   if (strcmp(key,"END")) goto L10;
462   //* find top volume in the geometry
463   flag=0;
464   for(i=1;i<=nvol;i++) {
465     if (istop[i] && flag) {
466       Warning("ReadEuclid"," %s is another possible top volume\n",volst[i]);
467     }
468     if (istop[i] && !flag) {
469       strcpy(topvol,volst[i]);
470       printf(" *** GREUCL *** volume %s taken as a top volume\n",topvol);
471       flag=1;
472     }
473   }
474   if (!flag) {
475     Warning("ReadEuclid","top volume not found\n");
476   }
477   fclose (lun);
478   //*
479   //*     commented out only for the not cernlib version
480   printf(" *** GREUCL *** file: %s is now read in\n",filnam);
481   //
482   return;
483   //*
484   L20:
485   Error("ReadEuclid","reading error or premature end of file\n");
486 }
487
488 //_____________________________________________________________________________
489 void AliModule::ReadEuclidMedia(const char* filnam)
490 {
491   //                                                                     
492   //       read in the materials and tracking media for the detector     
493   //                   in euclid file format                             
494   //                                                                     
495   //       filnam: name of the input file                                
496   //       id_det: id_det is the detector identification (2=its,...)     
497   //                                                                     
498   //            author : miroslav helbich                                
499   //
500   Float_t sxmgmx = gAlice->Field()->Max();
501   Int_t   isxfld = gAlice->Field()->Integ();
502   Int_t end, i, iret, itmed;
503   char key[5], card[130], natmed[21], namate[21];
504   Float_t ubuf[50];
505   char* filtmp;
506   FILE *lun;
507   Int_t imate;
508   Int_t nwbuf, isvol, ifield, nmat;
509   Float_t a, z, dens, radl, absl, fieldm, tmaxfd, stemax, deemax, epsil, stmin;
510   //
511   end=strlen(filnam);
512   for(i=0;i<end;i++) if(filnam[i]=='.') {
513     end=i;
514     break;
515   }
516   //
517   // *** The input filnam name will be with extension '.euc'
518   printf("The file name is %s\n",filnam); //Debug
519   filtmp=gSystem->ExpandPathName(filnam);
520   lun=fopen(filtmp,"r");
521   delete [] filtmp;
522   if(!lun) {
523     Warning("ReadEuclidMedia","Could not open file %s\n",filnam);
524     return;
525   }
526   //
527   // Retrieve Mag Field parameters
528   Int_t ISXFLD=gAlice->Field()->Integ();
529   Float_t SXMGMX=gAlice->Field()->Max();
530   //  TArrayI &idtmed = *fIdtmed;
531   //
532  L10:
533   for(i=0;i<130;i++) card[i]=0;
534   iret=fscanf(lun,"%4s %[^\n]",key,card);
535   if(iret<=0) goto L20;
536   fscanf(lun,"%*c");
537   //*
538   //* read material
539   if (!strcmp(key,"MATE")) {
540     sscanf(card,"%d '%[^']' %f %f %f %f %f %d",&imate,namate,&a,&z,&dens,&radl,&absl,&nwbuf);
541     if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
542     //Pad the string with blanks
543     i=-1;
544     while(namate[++i]);
545     while(i<20) namate[i++]=' ';
546     namate[i]='\0';
547     //
548     AliMaterial(imate,namate,a,z,dens,radl,absl,ubuf,nwbuf);
549     //* read tracking medium
550   } else if (!strcmp(key,"TMED")) {
551     sscanf(card,"%d '%[^']' %d %d %d %f %f %f %f %f %f %d",
552            &itmed,natmed,&nmat,&isvol,&ifield,&fieldm,&tmaxfd,
553            &stemax,&deemax,&epsil,&stmin,&nwbuf);
554     if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
555     if (ifield<0) ifield=isxfld;
556     if (fieldm<0) fieldm=sxmgmx;
557     //Pad the string with blanks
558     i=-1;
559     while(natmed[++i]);
560     while(i<20) natmed[i++]=' ';
561     natmed[i]='\0';
562     //
563     AliMedium(itmed,natmed,nmat,isvol,ISXFLD,SXMGMX,tmaxfd,
564                    stemax,deemax,epsil,stmin,ubuf,nwbuf);
565     //    (*fImedia)[idtmed[itmed]-1]=id_det;
566     //*
567   }
568   //*
569   if (strcmp(key,"END")) goto L10;
570   fclose (lun);
571   //*
572   //*     commented out only for the not cernlib version
573   Warning("ReadEuclidMedia","file: %s is now read in\n",filnam);
574   //*
575   return;
576   //*
577  L20:
578   Warning("ReadEuclidMedia","reading error or premature end of file\n");
579
580  
581 //_____________________________________________________________________________
582 void AliModule::Streamer(TBuffer &R__b)
583 {
584   //
585   // Stream an object of class Module.
586   //
587   if (R__b.IsReading()) {
588     Version_t R__v = R__b.ReadVersion(); if (R__v) { }
589     TNamed::Streamer(R__b);
590     TAttLine::Streamer(R__b);
591     TAttMarker::Streamer(R__b);
592     fEuclidMaterial.Streamer(R__b);
593     fEuclidGeometry.Streamer(R__b);
594     R__b >> fActive;
595     R__b >> fHistograms;
596     //
597     // Stream the pointers but not the TClonesArrays
598     R__b >> fNodes; // diff
599   } else {
600     R__b.WriteVersion(AliModule::IsA());
601     TNamed::Streamer(R__b);
602     TAttLine::Streamer(R__b);
603     TAttMarker::Streamer(R__b);
604     fEuclidMaterial.Streamer(R__b);
605     fEuclidGeometry.Streamer(R__b);
606     R__b << fActive;
607     R__b << fHistograms;
608     //
609     // Stream the pointers but not the TClonesArrays
610     R__b << fNodes; // diff
611   }
612 }
613