]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliModule.cxx
Access to run loader
[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 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
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.                               //
24 //                                                                           //
25 // This class is also responsible for building the geometry of the           //
26 // Modules.                                                                //
27 //                                                                           //
28 //Begin_Html
29 /*
30 <img src="picts/AliModuleClass.gif">
31 */
32 //End_Html
33 //                                                                           //
34 ///////////////////////////////////////////////////////////////////////////////
35
36 #include <TNode.h>
37 #include <TObjArray.h>
38 #include <TClonesArray.h>
39 #include <TTree.h>
40 #include <TSystem.h>
41 #include <TDirectory.h>
42 #include <TVirtualMC.h>
43
44 #include "AliConfig.h"
45 #include "AliLoader.h"
46 #include "AliMagF.h"
47 #include "AliModule.h"
48 #include "AliRun.h"
49 #include "AliTrackReference.h"
50 #include "AliMC.h"
51
52 ClassImp(AliModule)
53  
54 //_______________________________________________________________________
55 AliModule::AliModule():
56   fEuclidMaterial(""),
57   fEuclidGeometry(""),
58   fIdtmed(0),
59   fIdmate(0),
60   fLoMedium(0),
61   fHiMedium(0),
62   fActive(0),
63   fHistograms(0),
64   fNodes(0),
65   fDebug(0),
66   fEnable(1),
67   fTrackReferences(0),
68   fMaxIterTrackRef(0),
69   fCurrentIterTrackRef(0),
70   fRunLoader(0)
71 {
72   //
73   // Default constructor for the AliModule class
74   //
75 }
76  
77 //_______________________________________________________________________
78 AliModule::AliModule(const char* name,const char *title):
79   TNamed(name,title),
80   fEuclidMaterial(""),
81   fEuclidGeometry(""),
82   fIdtmed(new TArrayI(100)),
83   fIdmate(new TArrayI(100)),
84   fLoMedium(65536),
85   fHiMedium(0),
86   fActive(0),
87   fHistograms(new TList()),
88   fNodes(new TList()),
89   fDebug(0),
90   fEnable(1),
91   fTrackReferences(new TClonesArray("AliTrackReference", 100)),
92   fMaxIterTrackRef(0),
93   fCurrentIterTrackRef(0),
94   fRunLoader(0)
95 {
96   //
97   // Normal constructor invoked by all Modules.
98   // Create the list for Module specific histograms
99   // Add this Module to the global list of Modules in Run.
100   //
101   // Get the Module numeric ID
102   Int_t id = gAlice->GetModuleID(name);
103   if (id>=0) {
104     // Module already added !
105      Warning("Ctor","Module: %s already present at %d\n",name,id);
106      return;
107   }
108   //
109   // Add this Module to the list of Modules
110
111   gAlice->AddModule(this);
112
113   SetMarkerColor(3);
114   //
115   // Clear space for tracking media and material indexes
116
117   for(Int_t i=0;i<100;i++) (*fIdmate)[i]=(*fIdtmed)[i]=0;
118
119     
120   SetDebug(gAlice->GetDebug());
121 }
122  
123 //_______________________________________________________________________
124 AliModule::AliModule(const AliModule &mod):
125   TNamed(mod),
126   TAttLine(mod),
127   TAttMarker(mod),
128   AliRndm(mod),
129   fEuclidMaterial(""),
130   fEuclidGeometry(""),
131   fIdtmed(0),
132   fIdmate(0),
133   fLoMedium(0),
134   fHiMedium(0),
135   fActive(0),
136   fHistograms(0),
137   fNodes(0),
138   fDebug(0),
139   fEnable(0),
140   fTrackReferences(0),
141   fMaxIterTrackRef(0),
142   fCurrentIterTrackRef(0),
143   fRunLoader(0)
144 {
145   //
146   // Copy constructor
147   //
148   mod.Copy(*this);
149 }
150
151 //_______________________________________________________________________
152 AliModule::~AliModule()
153 {
154   //
155   // Destructor
156   //
157
158   // Remove this Module from the list of Modules
159   if (gAlice) {
160     TObjArray * modules = gAlice->Modules();
161     if (modules) modules->Remove(this);
162   }
163   // Delete ROOT geometry
164   if(fNodes) {
165     fNodes->Clear();
166     delete fNodes;
167     fNodes = 0;
168   }
169   // Delete histograms
170   if(fHistograms) {
171     fHistograms->Clear();
172     delete fHistograms;
173     fHistograms = 0;
174   }
175   // Delete track references
176   if (fTrackReferences) {
177     fTrackReferences->Delete();
178     delete fTrackReferences;
179     fTrackReferences     = 0;
180   }
181   // Delete TArray objects
182   delete fIdtmed;
183   delete fIdmate;
184
185 }
186  
187 //_______________________________________________________________________
188 void AliModule::Copy(TObject & /* mod */) const
189 {
190   //
191   // Copy *this onto mod, not implemented for AliModule
192   //
193   Fatal("Copy","Not implemented!\n");
194 }
195
196 //_______________________________________________________________________
197 void AliModule::Disable()
198 {
199   //
200   // Disable Module on viewer
201   //
202   fActive = kFALSE;
203   TIter next(fNodes);
204   TNode *node;
205   //
206   // Loop through geometry to disable all
207   // nodes for this Module
208   while((node = dynamic_cast<TNode*>(next()))) {
209     node->SetVisibility(-1);
210   }   
211 }
212
213 //_______________________________________________________________________
214 void AliModule::Enable()
215 {
216   //
217   // Enable Module on the viewver
218   //
219   fActive = kTRUE;
220   TIter next(fNodes);
221   TNode *node;
222   //
223   // Loop through geometry to enable all
224   // nodes for this Module
225   while((node = dynamic_cast<TNode*>(next()))) {
226     node->SetVisibility(3);
227   }   
228 }
229
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
234 {
235   //
236   // Store the parameters for a material
237   //
238   // imat        the material index will be stored in (*fIdmate)[imat]
239   // name        material name
240   // a           atomic mass
241   // z           atomic number
242   // dens        density
243   // radl        radiation length
244   // absl        absorbtion length
245   // buf         adress of an array user words
246   // nwbuf       number of user words
247   //
248   Int_t kmat;
249   gMC->Material(kmat, name, a, z, dens, radl, absl, buf, nwbuf);
250   (*fIdmate)[imat]=kmat;
251 }
252   
253 //_______________________________________________________________________
254 void AliModule::AliGetMaterial(Int_t imat, char* name, Float_t &a, 
255                                Float_t &z, Float_t &dens, Float_t &radl,
256                                Float_t &absl) const
257 {
258   //
259   // Store the parameters for a material
260   //
261   // imat        the material index will be stored in (*fIdmate)[imat]
262   // name        material name
263   // a           atomic mass
264   // z           atomic number
265   // dens        density
266   // radl        radiation length
267   // absl        absorbtion length
268   // buf         adress of an array user words
269   // nwbuf       number of user words
270   //
271
272   Float_t buf[10];
273   Int_t nwbuf, kmat;
274   kmat=(*fIdmate)[imat];
275   gMC->Gfmate(kmat, name, a, z, dens, radl, absl, buf, nwbuf);
276 }
277   
278
279 //_______________________________________________________________________
280 void AliModule::AliMixture(Int_t imat, const char *name, Float_t *a,
281                            Float_t *z, Float_t dens, Int_t nlmat,
282                            Float_t *wmat) const
283
284   //
285   // Defines mixture or compound imat as composed by 
286   // nlmat materials defined by arrays a, z and wmat
287   // 
288   // If nlmat > 0 wmat contains the proportion by
289   // weights of each basic material in the mixture  
290   // 
291   // If nlmat < 0 wmat contains the number of atoms 
292   // of eack kind in the molecule of the compound
293   // In this case, wmat is changed on output to the relative weigths.
294   //
295   // imat        the material index will be stored in (*fIdmate)[imat]
296   // name        material name
297   // a           array of atomic masses
298   // z           array of atomic numbers
299   // dens        density
300   // nlmat       number of components
301   // wmat        array of concentrations
302   //
303   Int_t kmat;
304   gMC->Mixture(kmat, name, a, z, dens, nlmat, wmat);
305   (*fIdmate)[imat]=kmat;
306
307  
308 //_______________________________________________________________________
309 void AliModule::AliMedium(Int_t numed, const char *name, Int_t nmat,
310                           Int_t isvol, Int_t ifield, Float_t fieldm,
311                           Float_t tmaxfd, Float_t stemax, Float_t deemax,
312                           Float_t epsil, Float_t stmin, Float_t *ubuf,
313                           Int_t nbuf) const
314
315   //
316   // Store the parameters of a tracking medium
317   //
318   // numed       the medium number is stored into (*fIdtmed)[numed]
319   // name        medium name
320   // nmat        the material number is stored into (*fIdmate)[nmat]
321   // isvol       sensitive volume if isvol!=0
322   // ifield      magnetic field flag (see below)
323   // fieldm      maximum magnetic field
324   // tmaxfd      maximum deflection angle due to magnetic field
325   // stemax      maximum step allowed
326   // deemax      maximum fractional energy loss in one step
327   // epsil       tracking precision in cm
328   // stmin       minimum step due to continuous processes
329   //
330   // ifield =  0       no magnetic field
331   //        = -1       user decision in guswim
332   //        =  1       tracking performed with Runge Kutta
333   //        =  2       tracking performed with helix
334   //        =  3       constant magnetic field along z
335   //  
336   Int_t kmed;
337   gMC->Medium(kmed,name, (*fIdmate)[nmat], isvol, ifield, fieldm,
338                          tmaxfd, stemax, deemax, epsil, stmin, ubuf, nbuf); 
339   (*fIdtmed)[numed]=kmed;
340
341  
342 //_______________________________________________________________________
343 void AliModule::AliMatrix(Int_t &nmat, Float_t theta1, Float_t phi1,
344                           Float_t theta2, Float_t phi2, Float_t theta3,
345                           Float_t phi3) const
346 {
347   // 
348   // Define a rotation matrix. Angles are in degrees.
349   //
350   // nmat        on output contains the number assigned to the rotation matrix
351   // theta1      polar angle for axis I
352   // phi1        azimuthal angle for axis I
353   // theta2      polar angle for axis II
354   // phi2        azimuthal angle for axis II
355   // theta3      polar angle for axis III
356   // phi3        azimuthal angle for axis III
357   //
358   gMC->Matrix(nmat, theta1, phi1, theta2, phi2, theta3, phi3); 
359
360
361 //_______________________________________________________________________
362 Float_t AliModule::ZMin() const
363 {
364   return -500;
365 }
366
367 //_______________________________________________________________________
368 Float_t AliModule::ZMax() const
369 {
370   return 500;
371 }
372
373 //_______________________________________________________________________
374 void AliModule::SetEuclidFile(char* material, char* geometry)
375 {
376   //
377   // Sets the name of the Euclid file
378   //
379   fEuclidMaterial=material;
380   if(geometry) {
381     fEuclidGeometry=geometry;
382   } else {
383     char* name = new char[strlen(material)];
384     strcpy(name,material);
385     strcpy(&name[strlen(name)-4],".euc");
386     fEuclidGeometry=name;
387     delete [] name;
388   }
389 }
390  
391 //_______________________________________________________________________
392 void AliModule::ReadEuclid(const char* filnam, char* topvol)
393 {
394   //                                                                     
395   //       read in the geometry of the detector in euclid file format    
396   //                                                                     
397   //        id_det : the detector identification (2=its,...)            
398   //        topvol : return parameter describing the name of the top    
399   //        volume of geometry.                                          
400   //                                                                     
401   //            author : m. maire                                        
402   //                                                                     
403   //     28.07.98
404   //     several changes have been made by miroslav helbich
405   //     subroutine is rewrited to follow the new established way of memory
406   //     booking for tracking medias and rotation matrices.
407   //     all used tracking media have to be defined first, for this you can use
408   //     subroutine  greutmed.
409   //     top volume is searched as only volume not positioned into another 
410   //
411
412   Int_t i, nvol, iret, itmed, irot, numed, npar, ndiv, iaxe;
413   Int_t ndvmx, nr, flag;
414   char key[5], card[77], natmed[21];
415   char name[5], mother[5], shape[5], konly[5], volst[7000][5];
416   char *filtmp;
417   Float_t par[100];
418   Float_t teta1, phi1, teta2, phi2, teta3, phi3, orig, step;
419   Float_t xo, yo, zo;
420   const Int_t kMaxRot=5000;
421   Int_t idrot[kMaxRot],istop[7000];
422   FILE *lun;
423   //
424   // *** The input filnam name will be with extension '.euc'
425   filtmp=gSystem->ExpandPathName(filnam);
426   lun=fopen(filtmp,"r");
427   delete [] filtmp;
428   if(!lun) {
429     Error("ReadEuclid","Could not open file %s\n",filnam);
430     return;
431   }
432   //* --- definition of rotation matrix 0 ---  
433   TArrayI &idtmed = *fIdtmed;
434   for(i=1; i<kMaxRot; ++i) idrot[i]=-99;
435   idrot[0]=0;
436   nvol=0;
437  L10:
438   for(i=0;i<77;i++) card[i]=0;
439   iret=fscanf(lun,"%77[^\n]",card);
440   if(iret<=0) goto L20;
441   fscanf(lun,"%*c");
442   //*
443   strncpy(key,card,4);
444   key[4]='\0';
445   if (!strcmp(key,"TMED")) {
446     sscanf(&card[5],"%d '%[^']'",&itmed,natmed);
447     if( itmed<0 || itmed>=100 ) {
448       Error("ReadEuclid","TMED illegal medium number %d for %s\n",itmed,natmed);
449       exit(1);
450     }
451     //Pad the string with blanks
452     i=-1;
453     while(natmed[++i]);
454     while(i<20) natmed[i++]=' ';
455     natmed[i]='\0';
456     //
457     if( idtmed[itmed]<=0 ) {
458       Error("ReadEuclid","TMED undefined medium number %d for %s\n",itmed,natmed);
459       exit(1);
460     }
461     gMC->Gckmat(idtmed[itmed],natmed);
462     //*
463   } else if (!strcmp(key,"ROTM")) {
464     sscanf(&card[4],"%d %f %f %f %f %f %f",&irot,&teta1,&phi1,&teta2,&phi2,&teta3,&phi3);
465     if( irot<=0 || irot>=kMaxRot ) {
466       Error("ReadEuclid","ROTM rotation matrix number %d illegal\n",irot);
467       exit(1);
468     }
469     AliMatrix(idrot[irot],teta1,phi1,teta2,phi2,teta3,phi3);
470     //*
471   } else if (!strcmp(key,"VOLU")) {
472     sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, shape, &numed, &npar);
473     if (npar>0) {
474       for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
475       fscanf(lun,"%*c");
476     }
477     gMC->Gsvolu( name, shape, idtmed[numed], par, npar);
478     //*     save the defined volumes
479     strcpy(volst[++nvol],name);
480     istop[nvol]=1;
481     //*
482   } else if (!strcmp(key,"DIVN")) {
483     sscanf(&card[5],"'%[^']' '%[^']' %d %d", name, mother, &ndiv, &iaxe);
484     gMC->Gsdvn  ( name, mother, ndiv, iaxe );
485     //*
486   } else if (!strcmp(key,"DVN2")) {
487     sscanf(&card[5],"'%[^']' '%[^']' %d %d %f %d",name, mother, &ndiv, &iaxe, &orig, &numed);
488     gMC->Gsdvn2( name, mother, ndiv, iaxe, orig,idtmed[numed]);
489     //*
490   } else if (!strcmp(key,"DIVT")) {
491     sscanf(&card[5],"'%[^']' '%[^']' %f %d %d %d", name, mother, &step, &iaxe, &numed, &ndvmx);
492     gMC->Gsdvt ( name, mother, step, iaxe, idtmed[numed], ndvmx);
493     //*
494   } else if (!strcmp(key,"DVT2")) {
495     sscanf(&card[5],"'%[^']' '%[^']' %f %d %f %d %d", name, mother, &step, &iaxe, &orig, &numed, &ndvmx);
496     gMC->Gsdvt2 ( name, mother, step, iaxe, orig, idtmed[numed], ndvmx );
497     //*
498   } else if (!strcmp(key,"POSI")) {
499     sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']'", name, &nr, mother, &xo, &yo, &zo, &irot, konly);
500     if( irot<0 || irot>=kMaxRot ) {
501       Error("ReadEuclid","POSI %s#%d rotation matrix number %d illegal\n",name,nr,irot);
502       exit(1);
503     }
504     if( idrot[irot] == -99) {
505       Error("ReadEuclid","POSI %s#%d undefined matrix number %d\n",name,nr,irot);
506       exit(1);
507     }
508     //*** volume name cannot be the top volume
509     for(i=1;i<=nvol;i++) {
510       if (!strcmp(volst[i],name)) istop[i]=0;
511     }
512     //*
513     gMC->Gspos  ( name, nr, mother, xo, yo, zo, idrot[irot], konly );
514     //*
515   } else if (!strcmp(key,"POSP")) {
516     sscanf(&card[5],"'%[^']' %d '%[^']' %f %f %f %d '%[^']' %d", name, &nr, mother, &xo, &yo, &zo, &irot, konly, &npar);
517     if( irot<0 || irot>=kMaxRot ) {
518       Error("ReadEuclid","POSP %s#%d rotation matrix number %d illegal\n",name,nr,irot);
519       exit(1);
520     }
521     if( idrot[irot] == -99) {
522       Error("ReadEuclid","POSP %s#%d undefined matrix number %d\n",name,nr,irot);
523       exit(1);
524     }
525     if (npar > 0) {
526       for(i=0;i<npar;i++) fscanf(lun,"%f",&par[i]);
527       fscanf(lun,"%*c");
528     }
529     //*** volume name cannot be the top volume
530     for(i=1;i<=nvol;i++) {
531       if (!strcmp(volst[i],name)) istop[i]=0;
532     }
533     //*
534     gMC->Gsposp ( name, nr, mother, xo,yo,zo, idrot[irot], konly, par, npar);
535   }
536   //*
537   if (strcmp(key,"END")) goto L10;
538   //* find top volume in the geometry
539   flag=0;
540   for(i=1;i<=nvol;i++) {
541     if (istop[i] && flag) {
542       Warning("ReadEuclid"," %s is another possible top volume\n",volst[i]);
543     }
544     if (istop[i] && !flag) {
545       strcpy(topvol,volst[i]);
546       if(fDebug) printf("%s::ReadEuclid: volume %s taken as a top volume\n",ClassName(),topvol);
547       flag=1;
548     }
549   }
550   if (!flag) {
551     Warning("ReadEuclid","top volume not found\n");
552   }
553   fclose (lun);
554   //*
555   //*     commented out only for the not cernlib version
556   if(fDebug) printf("%s::ReadEuclid: file: %s is now read in\n",ClassName(),filnam);
557   //
558   return;
559   //*
560   L20:
561   Error("ReadEuclid","reading error or premature end of file\n");
562 }
563
564 //_______________________________________________________________________
565 void AliModule::ReadEuclidMedia(const char* filnam)
566 {
567   //                                                                     
568   //       read in the materials and tracking media for the detector     
569   //                   in euclid file format                             
570   //                                                                     
571   //       filnam: name of the input file                                
572   //       id_det: id_det is the detector identification (2=its,...)     
573   //                                                                     
574   //            author : miroslav helbich                                
575   //
576   Float_t sxmgmx = gAlice->Field()->Max();
577   Int_t   isxfld = gAlice->Field()->Integ();
578   Int_t end, i, iret, itmed;
579   char key[5], card[130], natmed[21], namate[21];
580   Float_t ubuf[50];
581   char* filtmp;
582   FILE *lun;
583   Int_t imate;
584   Int_t nwbuf, isvol, ifield, nmat;
585   Float_t a, z, dens, radl, absl, fieldm, tmaxfd, stemax, deemax, epsil, stmin;
586   //
587   end=strlen(filnam);
588   for(i=0;i<end;i++) if(filnam[i]=='.') {
589     end=i;
590     break;
591   }
592   //
593   // *** The input filnam name will be with extension '.euc'
594   if(fDebug) printf("%s::ReadEuclid: The file name is %s\n",ClassName(),filnam); //Debug
595   filtmp=gSystem->ExpandPathName(filnam);
596   lun=fopen(filtmp,"r");
597   delete [] filtmp;
598   if(!lun) {
599     Warning("ReadEuclidMedia","Could not open file %s\n",filnam);
600     return;
601   }
602   //
603   // Retrieve Mag Field parameters
604   Int_t globField=gAlice->Field()->Integ();
605   Float_t globMaxField=gAlice->Field()->Max();
606   //  TArrayI &idtmed = *fIdtmed;
607   //
608  L10:
609   for(i=0;i<130;i++) card[i]=0;
610   iret=fscanf(lun,"%4s %[^\n]",key,card);
611   if(iret<=0) goto L20;
612   fscanf(lun,"%*c");
613   //*
614   //* read material
615   if (!strcmp(key,"MATE")) {
616     sscanf(card,"%d '%[^']' %f %f %f %f %f %d",&imate,namate,&a,&z,&dens,&radl,&absl,&nwbuf);
617     if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
618     //Pad the string with blanks
619     i=-1;
620     while(namate[++i]);
621     while(i<20) namate[i++]=' ';
622     namate[i]='\0';
623     //
624     AliMaterial(imate,namate,a,z,dens,radl,absl,ubuf,nwbuf);
625     //* read tracking medium
626   } else if (!strcmp(key,"TMED")) {
627     sscanf(card,"%d '%[^']' %d %d %d %f %f %f %f %f %f %d",
628            &itmed,natmed,&nmat,&isvol,&ifield,&fieldm,&tmaxfd,
629            &stemax,&deemax,&epsil,&stmin,&nwbuf);
630     if (nwbuf>0) for(i=0;i<nwbuf;i++) fscanf(lun,"%f",&ubuf[i]);
631     if (ifield<0) ifield=isxfld;
632     if (fieldm<0) fieldm=sxmgmx;
633     //Pad the string with blanks
634     i=-1;
635     while(natmed[++i]);
636     while(i<20) natmed[i++]=' ';
637     natmed[i]='\0';
638     //
639     AliMedium(itmed,natmed,nmat,isvol,globField,globMaxField,tmaxfd,
640                    stemax,deemax,epsil,stmin,ubuf,nwbuf);
641     //    (*fImedia)[idtmed[itmed]-1]=id_det;
642     //*
643   }
644   //*
645   if (strcmp(key,"END")) goto L10;
646   fclose (lun);
647   //*
648   //*     commented out only for the not cernlib version
649   if(fDebug) printf("%s::ReadEuclidMedia: file %s is now read in\n",
650         ClassName(),filnam);
651   //*
652   return;
653   //*
654  L20:
655   Warning("ReadEuclidMedia","reading error or premature end of file\n");
656
657
658 //_______________________________________________________________________
659 void AliModule::RemapTrackReferencesIDs(Int_t *map)
660 {
661   // 
662   // Remapping track reference
663   // Called at finish primary
664   //
665   if (!fTrackReferences) return;
666   for (Int_t i=0;i<fTrackReferences->GetEntries();i++){
667     AliTrackReference * ref = dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(i));
668     if (ref) {
669       Int_t newID = map[ref->GetTrack()];
670       if (newID>=0) ref->SetTrack(newID);
671       else {
672         //ref->SetTrack(-1);
673         ref->SetBit(kNotDeleted,kFALSE);
674         fTrackReferences->RemoveAt(i);  
675       }      
676     }
677   }
678   fTrackReferences->Compress();
679
680 }
681
682
683 //_______________________________________________________________________
684 AliTrackReference* AliModule::FirstTrackReference(Int_t track)
685 {
686   //
687   // Initialise the hit iterator
688   // Return the address of the first hit for track
689   // If track>=0 the track is read from disk
690   // while if track<0 the first hit of the current
691   // track is returned
692   // 
693   if(track>=0) 
694    {
695      if (fRunLoader == 0x0)
696        Fatal("FirstTrackReference","AliRunLoader not initialized. Can not proceed");
697      fRunLoader->GetAliRun()->GetMCApp()->ResetTrackReferences();
698      fRunLoader->TreeTR()->GetEvent(track);
699    }
700   //
701   fMaxIterTrackRef     = fTrackReferences->GetEntriesFast();
702   fCurrentIterTrackRef = 0;
703   if(fMaxIterTrackRef) return dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(0));
704   else            return 0;
705 }
706
707 //_______________________________________________________________________
708 AliTrackReference* AliModule::NextTrackReference()
709 {
710   //
711   // Return the next hit for the current track
712   //
713   if(fMaxIterTrackRef) {
714     if(++fCurrentIterTrackRef<fMaxIterTrackRef) 
715       return dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(fCurrentIterTrackRef));
716     else        
717       return 0;
718   } else {
719     printf("* AliModule::NextTrackReference * TrackReference  Iterator called without calling FistTrackReference before\n");
720     return 0;
721   }
722 }
723
724
725 //_______________________________________________________________________
726 void AliModule::ResetTrackReferences()
727 {
728   //
729   // Reset number of hits and the hits array
730   //
731   fMaxIterTrackRef   = 0;
732   if (fTrackReferences)   fTrackReferences->Clear();
733 }
734  
735 //_____________________________________________________________________________
736
737 AliLoader*  AliModule::MakeLoader(const char* /*topfoldername*/) 
738  {
739    return 0x0;
740  }//skowron   
741  
742 //PH Merged with v3-09-08 |
743 //                        V
744 //_____________________________________________________________________________
745
746 void AliModule::SetTreeAddress()
747 {
748   //
749   // Set branch address for track reference Tree
750   //
751
752   TBranch *branch;
753
754   // Branch address for track reference tree
755   TTree *treeTR = TreeTR();
756
757   if (treeTR && fTrackReferences) {
758      branch = treeTR->GetBranch(GetName());
759     if (branch) 
760      {
761        if(GetDebug()) 
762          Info("SetTreeAddress","(%s) Setting for TrackRefs",GetName());
763        branch->SetAddress(&fTrackReferences);
764      }
765     else
766      { 
767      //can be called before MakeBranch and than does not make sense to issue the warning
768        if(GetDebug()) 
769          Warning("SetTreeAddress",
770                  "(%s) Failed for Track References. Can not find branch in tree.",
771                  GetName());
772      }
773   }
774 }
775
776 //_____________________________________________________________________________
777 void  AliModule::AddTrackReference(Int_t label){
778   //
779   // add a trackrefernce to the list
780   if (!fTrackReferences) {
781     cerr<<"Container trackrefernce not active\n";
782     return;
783   }
784   Int_t nref = fTrackReferences->GetEntriesFast();
785   TClonesArray &lref = *fTrackReferences;
786   new(lref[nref]) AliTrackReference(label);
787 }
788
789
790 //_____________________________________________________________________________
791 void AliModule::MakeBranchTR(Option_t */*option*/)
792
793     //
794     // Makes branch in treeTR
795     //  
796   if(GetDebug()) Info("MakeBranchTR","Making Track Refs. Branch for %s",GetName());
797   TTree * tree = TreeTR();
798   if (fTrackReferences && tree) 
799    {
800       TBranch *branch = tree->GetBranch(GetName());
801      if (branch) 
802        {  
803          if(GetDebug()) Info("MakeBranch","Branch %s is already in tree.",GetName());
804          return;
805        }
806   
807      branch = tree->Branch(GetName(),&fTrackReferences);
808    }
809   else
810     {
811       if(GetDebug()) 
812         Info("MakeBranchTR","FAILED for %s: tree=%#x fTrackReferences=%#x",
813              GetName(),tree,fTrackReferences);
814     }
815 }
816
817 //_____________________________________________________________________________
818 TTree* AliModule::TreeTR()
819 {
820   if ( fRunLoader == 0x0)
821    {
822      Error("TreeTR","Can not get the run loader");
823      return 0x0;
824    }
825
826   TTree* tree = fRunLoader->TreeTR();
827   return tree;
828 }