]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliRun.cxx
Removed compilation warning in AliConfig initialisation.
[u/mrichter/AliRoot.git] / STEER / AliRun.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.66  2001/05/22 14:32:40  hristov
19 Weird inline removed
20
21 Revision 1.65  2001/05/21 17:22:51  buncic
22 Fixed problem with missing AliConfig while reading galice.root
23
24 Revision 1.64  2001/05/16 14:57:22  alibrary
25 New files for folders and Stack
26
27 Revision 1.62  2001/04/06 11:12:33  morsch
28 Clear fParticles after each event. (Ivana Hrivnacova)
29
30 Revision 1.61  2001/03/30 07:04:10  morsch
31 Call fGenerator->FinishRun() for final print-outs, cross-section and weight calculations.
32
33 Revision 1.60  2001/03/21 18:22:30  hristov
34 fParticleFileMap fix (I.Hrivnacova)
35
36 Revision 1.59  2001/03/12 17:47:03  hristov
37 Changes needed on Sun with CC 5.0
38
39 Revision 1.58  2001/03/09 14:27:26  morsch
40 Fix for multiple events per file: inhibit decrease of size of  fParticleFileMap.
41
42 Revision 1.57  2001/02/23 17:40:23  buncic
43 All trees needed for simulation created in RunMC(). TreeR and its branches
44 are now created in new RunReco() method.
45
46 Revision 1.56  2001/02/14 15:45:20  hristov
47 Algorithmic way of getting entry index in fParticleMap. Protection of fParticleFileMap (I.Hrivnacova)
48
49 Revision 1.55  2001/02/12 15:52:54  buncic
50 Removed OpenBaseFile().
51
52 Revision 1.54  2001/02/07 10:39:05  hristov
53 Remove default value for argument
54
55 Revision 1.53  2001/02/06 11:02:26  hristov
56 New SetTrack interface added, added check for unfilled particles in FinishEvent (I.Hrivnacova)
57
58 Revision 1.52  2001/02/05 16:22:25  buncic
59 Added TreeS to GetEvent().
60
61 Revision 1.51  2001/02/02 15:16:20  morsch
62 SetHighWaterMark method added to mark last particle in event.
63
64 Revision 1.50  2001/01/27 10:32:00  hristov
65 Leave the loop when primaries are filled (I.Hrivnacova)
66
67 Revision 1.49  2001/01/26 19:58:48  hristov
68 Major upgrade of AliRoot code
69
70 Revision 1.48  2001/01/17 10:50:50  hristov
71 Corrections to destructors
72
73 Revision 1.47  2000/12/18 10:44:01  morsch
74 Possibility to set field map by passing pointer to objet of type AliMagF via
75 SetField().
76 Example:
77 gAlice->SetField(new AliMagFCM("Map2", "$(ALICE_ROOT)/data/field01.dat",2,1.,10.));
78
79 Revision 1.46  2000/12/14 19:29:27  fca
80 galice.cuts was not read any more
81
82 Revision 1.45  2000/11/30 07:12:49  alibrary
83 Introducing new Rndm and QA classes
84
85 Revision 1.44  2000/10/26 13:58:59  morsch
86 Add possibility to choose the lego generator (of type AliGeneratorLego or derived) when running
87 RunLego(). Default is the base class AliGeneratorLego.
88
89 Revision 1.43  2000/10/09 09:43:17  fca
90 Special remapping of hits for TPC and TRD. End-of-primary action introduced
91
92 Revision 1.42  2000/10/02 21:28:14  fca
93 Removal of useless dependecies via forward declarations
94
95 Revision 1.41  2000/07/13 16:19:09  fca
96 Mainly coding conventions + some small bug fixes
97
98 Revision 1.40  2000/07/12 08:56:25  fca
99 Coding convention correction and warning removal
100
101 Revision 1.39  2000/07/11 18:24:59  fca
102 Coding convention corrections + few minor bug fixes
103
104 Revision 1.38  2000/06/20 13:05:45  fca
105 Writing down the TREE headers before job starts
106
107 Revision 1.37  2000/06/09 20:05:11  morsch
108 Introduce possibility to chose magnetic field version 3: AliMagFDM + field02.dat
109
110 Revision 1.36  2000/06/08 14:03:58  hristov
111 Only one initializer for a default argument
112
113 Revision 1.35  2000/06/07 10:13:14  hristov
114 Delete only existent objects.
115
116 Revision 1.34  2000/05/18 10:45:38  fca
117 Delete Particle Factory properly
118
119 Revision 1.33  2000/05/16 13:10:40  fca
120 New method IsNewTrack and fix for a problem in Father-Daughter relations
121
122 Revision 1.32  2000/04/27 10:38:21  fca
123 Correct termination of Lego Run and introduce Lego getter in AliRun
124
125 Revision 1.31  2000/04/26 10:17:32  fca
126 Changes in Lego for G4 compatibility
127
128 Revision 1.30  2000/04/18 19:11:40  fca
129 Introduce variable Config.C function signature
130
131 Revision 1.29  2000/04/07 11:12:34  fca
132 G4 compatibility changes
133
134 Revision 1.28  2000/04/05 06:51:06  fca
135 Workaround for an HP compiler problem
136
137 Revision 1.27  2000/03/22 18:08:07  fca
138 Rationalisation of the virtual MC interfaces
139
140 Revision 1.26  2000/03/22 13:42:26  fca
141 SetGenerator does not replace an existing generator, ResetGenerator does
142
143 Revision 1.25  2000/02/23 16:25:22  fca
144 AliVMC and AliGeant3 classes introduced
145 ReadEuclid moved from AliRun to AliModule
146
147 Revision 1.24  2000/01/19 17:17:20  fca
148 Introducing a list of lists of hits -- more hits allowed for detector now
149
150 Revision 1.23  1999/12/03 11:14:31  fca
151 Fixing previous wrong checking
152
153 Revision 1.21  1999/11/25 10:40:08  fca
154 Fixing daughters information also in primary tracks
155
156 Revision 1.20  1999/10/04 18:08:49  fca
157 Adding protection against inconsistent Euclid files
158
159 Revision 1.19  1999/09/29 07:50:40  fca
160 Introduction of the Copyright and cvs Log
161
162 */
163
164 ///////////////////////////////////////////////////////////////////////////////
165 //                                                                           //
166 //  Control class for Alice C++                                              //
167 //  Only one single instance of this class exists.                           //
168 //  The object is created in main program aliroot                            //
169 //  and is pointed by the global gAlice.                                     //
170 //                                                                           //
171 //   -Supports the list of all Alice Detectors (fModules).                 //
172 //   -Supports the list of particles (fParticles).                           //
173 //   -Supports the Trees.                                                    //
174 //   -Supports the geometry.                                                 //
175 //   -Supports the event display.                                            //
176 //Begin_Html
177 /*
178 <img src="picts/AliRunClass.gif">
179 */
180 //End_Html
181 //Begin_Html
182 /*
183 <img src="picts/alirun.gif">
184 */
185 //End_Html
186 //                                                                           //
187 ///////////////////////////////////////////////////////////////////////////////
188
189 #include <stdlib.h>
190 #include <stdio.h>
191 #include <string.h>
192 #include <iostream.h>
193  
194 #include <TFile.h>
195 #include <TRandom.h>
196 #include <TBRIK.h> 
197 #include <TNode.h> 
198 #include <TCint.h> 
199 #include <TSystem.h>
200 #include <TObjectTable.h>
201 #include <TTree.h>
202 #include <TGeometry.h>
203 #include <TROOT.h>
204 #include <TBrowser.h>
205 #include <TFolder.h>
206
207 #include "TParticle.h"
208 #include "AliRun.h"
209 #include "AliDisplay.h"
210 #include "AliMC.h"
211 #include "AliLego.h"
212 #include "AliMagFC.h"
213 #include "AliMagFCM.h"
214 #include "AliMagFDM.h"
215 #include "AliHit.h"
216 #include "TRandom3.h"
217 #include "AliMCQA.h"
218 #include "AliGenerator.h"
219 #include "AliLegoGenerator.h"
220 #include "AliConfig.h"
221 #include "AliStack.h"
222 #include "AliGenEventHeader.h"
223 #include "AliHeader.h"
224
225 #include "AliDetector.h"
226
227 AliRun *gAlice;
228
229 ClassImp(AliRun)
230
231 //_____________________________________________________________________________
232 AliRun::AliRun()
233 {
234   //
235   // Default constructor for AliRun
236   //
237   fHeader    = 0;
238   fRun       = 0;
239   fEvent     = 0;
240   fStack     = 0;
241   fModules   = 0;
242   fGenerator = 0;
243   fTreeD     = 0;
244   fTreeH     = 0;
245   fTreeE     = 0;
246   fTreeR     = 0;
247   fTreeS     = 0;
248   fGeometry  = 0;
249   fDisplay   = 0;
250   fField     = 0;
251   fMC        = 0;
252   fNdets     = 0;
253   fImedia    = 0;
254   fTrRmax    = 1.e10;
255   fTrZmax    = 1.e10;
256   fInitDone  = kFALSE;
257   fLego      = 0;
258   fPDGDB     = 0;        //Particle factory object!
259   fHitLists  = 0;
260   fConfigFunction    = "\0";
261   fRandom = 0;
262   fMCQA = 0;
263   fTransParName = "\0";
264   fBaseFileName = ".\0";
265   fDebug        = 0;
266 }
267
268 //_____________________________________________________________________________
269 AliRun::AliRun(const char *name, const char *title)
270   : TNamed(name,title)
271 {
272   //
273   //  Constructor for the main processor.
274   //  Creates the geometry
275   //  Creates the list of Detectors.
276   //  Creates the list of particles.
277   //
278   Int_t i;
279   
280   gAlice     = this;
281   fTreeD     = 0;
282   fTreeH     = 0;
283   fTreeE     = 0;
284   fTreeR     = 0;
285   fTreeS     = 0;
286   fTrRmax    = 1.e10;
287   fTrZmax    = 1.e10;
288   fGenerator = 0;
289   fInitDone  = kFALSE;
290   fLego      = 0;
291   fField     = 0;
292   fConfigFunction    = "Config();";
293
294   // Set random number generator
295   gRandom = fRandom = new TRandom3();
296
297   if (gSystem->Getenv("CONFIG_SEED")) {
298      gRandom->SetSeed((UInt_t)atoi(gSystem->Getenv("CONFIG_SEED")));
299   }
300   
301   gROOT->GetListOfBrowsables()->Add(this,name);
302   //
303   // Particle stack
304   fStack = new AliStack(10000);
305   // create the support list for the various Detectors
306   fModules = new TObjArray(77);
307   //
308   // Create the TNode geometry for the event display
309   
310   BuildSimpleGeometry();
311   
312   fHeader    = new AliHeader();
313   fRun       = 0;
314   fEvent     = 0;
315   //
316   fDisplay = 0;
317   //
318   // Create default mag field
319   SetField();
320   //
321   fMC      = gMC;
322   //
323   // Prepare the tracking medium lists
324   fImedia = new TArrayI(1000);
325   for(i=0;i<1000;i++) (*fImedia)[i]=-99;
326   //
327   // Make particles
328   fPDGDB     = TDatabasePDG::Instance();        //Particle factory object!
329
330   AliConfig::Instance()->Add(fPDGDB); 
331   //
332   // Create HitLists list
333   fHitLists  = new TList();
334   //
335   SetTransPar();
336   fBaseFileName = ".\0";
337   //
338   fDebug        = 0;
339 }
340
341
342 //_____________________________________________________________________________
343 AliRun::~AliRun()
344 {
345   //
346   // Default AliRun destructor
347   //
348   delete fImedia;
349   delete fField;
350   delete fMC;
351   delete fGeometry;
352   delete fDisplay;
353   delete fGenerator;
354   delete fLego;
355   delete fTreeD;
356   delete fTreeH;
357   delete fTreeE;
358   delete fTreeR;
359   delete fTreeS;
360   if (fModules) {
361     fModules->Delete();
362     delete fModules;
363   }
364   delete fStack;
365   delete fHitLists;
366   delete fPDGDB;
367   delete fMCQA;
368   delete fHeader;
369 }
370
371 //_____________________________________________________________________________
372 void AliRun::AddHit(Int_t id, Int_t track, Int_t *vol, Float_t *hits) const
373 {
374   //
375   //  Add a hit to detector id
376   //
377   TObjArray &dets = *fModules;
378   if(dets[id]) ((AliModule*) dets[id])->AddHit(track,vol,hits);
379 }
380
381 //_____________________________________________________________________________
382 void AliRun::AddDigit(Int_t id, Int_t *tracks, Int_t *digits) const
383 {
384   //
385   // Add digit to detector id
386   //
387   TObjArray &dets = *fModules;
388   if(dets[id]) ((AliModule*) dets[id])->AddDigit(tracks,digits);
389 }
390
391 //_____________________________________________________________________________
392 void AliRun::Browse(TBrowser *b)
393 {
394   //
395   // Called when the item "Run" is clicked on the left pane
396   // of the Root browser.
397   // It displays the Root Trees and all detectors.
398   //
399   if(!fStack) fStack=fHeader->Stack();
400   TTree*  pTreeK = fStack->TreeK();
401     
402   if (pTreeK) b->Add(pTreeK,pTreeK->GetName());
403   if (fTreeH) b->Add(fTreeH,fTreeH->GetName());
404   if (fTreeD) b->Add(fTreeD,fTreeD->GetName());
405   if (fTreeE) b->Add(fTreeE,fTreeE->GetName());
406   if (fTreeR) b->Add(fTreeR,fTreeR->GetName());
407   if (fTreeS) b->Add(fTreeS,fTreeS->GetName());
408   
409   TIter next(fModules);
410   AliModule *detector;
411   while((detector = (AliModule*)next())) {
412     b->Add(detector,detector->GetName());
413   }
414   b->Add(fMCQA,"AliMCQA");
415 }
416
417 //_____________________________________________________________________________
418 void AliRun::Build()
419 {
420   //
421   // Initialize Alice geometry
422   // Dummy routine
423   //
424 }
425  
426 //_____________________________________________________________________________
427 void AliRun::BuildSimpleGeometry()
428 {
429   //
430   // Create a simple TNode geometry used by Root display engine
431   //
432   // Initialise geometry
433   //
434   fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
435   new TMaterial("void","Vacuum",0,0,0);  //Everything is void
436   TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
437   brik->SetVisibility(0);
438   new TNode("alice","alice","S_alice");
439 }
440
441 //_____________________________________________________________________________
442 void AliRun::CleanDetectors()
443 {
444   //
445   // Clean Detectors at the end of event
446   //
447   TIter next(fModules);
448   AliModule *detector;
449   while((detector = (AliModule*)next())) {
450     detector->FinishEvent();
451   }
452 }
453
454 //_____________________________________________________________________________
455 Int_t AliRun::DistancetoPrimitive(Int_t, Int_t)
456 {
457   //
458   // Return the distance from the mouse to the AliRun object
459   // Dummy routine
460   //
461   return 9999;
462 }
463
464 //_____________________________________________________________________________
465 void AliRun::DumpPart (Int_t i) const
466 {
467   //
468   // Dumps particle i in the stack
469   //
470     fStack->DumpPart(i);
471 }
472
473 //_____________________________________________________________________________
474 void AliRun::DumpPStack () const
475 {
476   //
477   // Dumps the particle stack
478   //
479     fStack->DumpPStack();
480 }
481
482 //_____________________________________________________________________________
483 void  AliRun::SetField(AliMagF* magField)
484 {
485     // Set Magnetic Field Map
486     fField = magField;
487     fField->ReadField();
488 }
489
490 //_____________________________________________________________________________
491 void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
492                       Float_t maxField, char* filename)
493 {
494   //
495   //  Set magnetic field parameters
496   //  type      Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
497   //  version   Magnetic field map version (only 1 active now)
498   //  scale     Scale factor for the magnetic field
499   //  maxField  Maximum value for the magnetic field
500
501   //
502   // --- Sanity check on mag field flags
503   if(fField) delete fField;
504   if(version==1) {
505     fField = new AliMagFC("Map1"," ",type,scale,maxField);
506   } else if(version<=2) {
507     fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
508     fField->ReadField();
509   } else if(version==3) {
510     fField = new AliMagFDM("Map4",filename,type,scale,maxField);
511     fField->ReadField();
512   } else {
513     Warning("SetField","Invalid map %d\n",version);
514   }
515 }
516  
517 //_____________________________________________________________________________
518 void AliRun::PreTrack()
519 {
520      TObjArray &dets = *fModules;
521      AliModule *module;
522
523      for(Int_t i=0; i<=fNdets; i++)
524        if((module = (AliModule*)dets[i]))
525          module->PreTrack();
526
527      fMCQA->PreTrack();
528 }
529
530 //_____________________________________________________________________________
531 void AliRun::PostTrack()
532 {
533      TObjArray &dets = *fModules;
534      AliModule *module;
535
536      for(Int_t i=0; i<=fNdets; i++)
537        if((module = (AliModule*)dets[i]))
538          module->PostTrack();
539 }
540
541 //_____________________________________________________________________________
542 void AliRun::FinishPrimary()
543 {
544   //
545   // Called  at the end of each primary track
546   //
547   
548   //  static Int_t count=0;
549   //  const Int_t times=10;
550   // This primary is finished, purify stack
551   fStack->PurifyKine();
552
553   TIter next(fModules);
554   AliModule *detector;
555   while((detector = (AliModule*)next())) {
556     detector->FinishPrimary();
557   }
558
559   // Write out hits if any
560   if (gAlice->TreeH()) {
561     gAlice->TreeH()->Fill();
562   }
563   
564   //
565   //  if(++count%times==1) gObjectTable->Print();
566 }
567
568 //_____________________________________________________________________________
569 void AliRun::BeginPrimary()
570 {
571   //
572   // Called  at the beginning of each primary track
573   //
574   
575   // Reset Hits info
576   gAlice->ResetHits();
577
578 }
579
580 //_____________________________________________________________________________
581 void AliRun::FinishEvent()
582 {
583   //
584   // Called at the end of the event.
585   //
586   
587   //
588   if(fLego) fLego->FinishEvent();
589
590   //Update the energy deposit tables
591   Int_t i;
592   for(i=0;i<fEventEnergy.GetSize();i++) {
593     fSummEnergy[i]+=fEventEnergy[i];
594     fSum2Energy[i]+=fEventEnergy[i]*fEventEnergy[i];
595   }
596
597
598   
599   // Update Header information 
600
601   fHeader->SetNprimary(fStack->GetNprimary());
602   fHeader->SetNtrack(fStack->GetNtrack());  
603
604   
605   // Write out the kinematics
606   fStack->FinishEvent();
607    
608   // Write out the event Header information
609   if (fTreeE) {
610       fHeader->SetStack(fStack);
611       fTreeE->Fill();
612   }
613   
614   
615   // Write Tree headers
616   TTree* pTreeK = fStack->TreeK();
617   if (pTreeK) pTreeK->Write(0,TObject::kOverwrite);
618   if (fTreeH) fTreeH->Write(0,TObject::kOverwrite);
619   
620   ++fEvent;
621 }
622
623 //_____________________________________________________________________________
624 void AliRun::FinishRun()
625 {
626   //
627   // Called at the end of the run.
628   //
629
630   //
631   if(fLego) fLego->FinishRun();
632   
633   // Clean detector information
634   TIter next(fModules);
635   AliModule *detector;
636   while((detector = (AliModule*)next())) {
637     detector->FinishRun();
638   }
639   
640   //Output energy summary tables
641   EnergySummary();
642
643   TFile *file = fTreeE->GetCurrentFile();
644
645   file->cd();
646   
647   fTreeE->Write(0,TObject::kOverwrite);
648   
649   // Write AliRun info and all detectors parameters
650   Write(0,TObject::kOverwrite);
651
652   // Clean tree information
653
654   fStack->FinishRun();
655   
656   if (fTreeH) {
657     delete fTreeH; fTreeH = 0;
658   }
659   if (fTreeD) {
660     delete fTreeD; fTreeD = 0;
661   }
662   if (fTreeR) {
663     delete fTreeR; fTreeR = 0;
664   }
665   if (fTreeE) {
666     delete fTreeE; fTreeE = 0;
667   }
668   if (fTreeS) {
669     delete fTreeS; fTreeS = 0;
670   }
671     
672   // Close output file
673   file->Write();
674 }
675
676 //_____________________________________________________________________________
677 void AliRun::FlagTrack(Int_t track)
678 {
679   // Delegate to stack
680   //
681     fStack->FlagTrack(track);
682 }
683  
684 //_____________________________________________________________________________
685 void AliRun::EnergySummary()
686 {
687   //
688   // Print summary of deposited energy
689   //
690
691   Int_t ndep=0;
692   Float_t edtot=0;
693   Float_t ed, ed2;
694   Int_t kn, i, left, j, id;
695   const Float_t kzero=0;
696   Int_t ievent=fHeader->GetEvent()+1;
697   //
698   // Energy loss information
699   if(ievent) {
700     printf("***************** Energy Loss Information per event (GEV) *****************\n");
701     for(kn=1;kn<fEventEnergy.GetSize();kn++) {
702       ed=fSummEnergy[kn];
703       if(ed>0) {
704         fEventEnergy[ndep]=kn;
705         if(ievent>1) {
706           ed=ed/ievent;
707           ed2=fSum2Energy[kn];
708           ed2=ed2/ievent;
709           ed2=100*TMath::Sqrt(TMath::Max(ed2-ed*ed,kzero))/ed;
710         } else 
711           ed2=99;
712         fSummEnergy[ndep]=ed;
713         fSum2Energy[ndep]=TMath::Min((Float_t) 99.,TMath::Max(ed2,kzero));
714         edtot+=ed;
715         ndep++;
716       }
717     }
718     for(kn=0;kn<(ndep-1)/3+1;kn++) {
719       left=ndep-kn*3;
720       for(i=0;i<(3<left?3:left);i++) {
721         j=kn*3+i;
722         id=Int_t (fEventEnergy[j]+0.1);
723         printf(" %s %10.3f +- %10.3f%%;",gMC->VolName(id),fSummEnergy[j],fSum2Energy[j]);
724       }
725       printf("\n");
726     }
727     //
728     // Relative energy loss in different detectors
729     printf("******************** Relative Energy Loss per event ********************\n");
730     printf("Total energy loss per event %10.3f GeV\n",edtot);
731     for(kn=0;kn<(ndep-1)/5+1;kn++) {
732       left=ndep-kn*5;
733       for(i=0;i<(5<left?5:left);i++) {
734         j=kn*5+i;
735         id=Int_t (fEventEnergy[j]+0.1);
736         printf(" %s %10.3f%%;",gMC->VolName(id),100*fSummEnergy[j]/edtot);
737       }
738       printf("\n");
739     }
740     for(kn=0;kn<75;kn++) printf("*"); 
741     printf("\n");
742   }
743   //
744   // Reset the TArray's
745   //  fEventEnergy.Set(0);
746   //  fSummEnergy.Set(0);
747   //  fSum2Energy.Set(0);
748 }
749
750 //_____________________________________________________________________________
751 AliModule *AliRun::GetModule(const char *name) const
752 {
753   //
754   // Return pointer to detector from name
755   //
756   return (AliModule*)fModules->FindObject(name);
757 }
758  
759 //_____________________________________________________________________________
760 AliDetector *AliRun::GetDetector(const char *name) const
761 {
762   //
763   // Return pointer to detector from name
764   //
765   return (AliDetector*)fModules->FindObject(name);
766 }
767  
768 //_____________________________________________________________________________
769 Int_t AliRun::GetModuleID(const char *name) const
770 {
771   //
772   // Return galice internal detector identifier from name
773   //
774   Int_t i=-1;
775   TObject *mod=fModules->FindObject(name);
776   if(mod) i=fModules->IndexOf(mod);
777   return i;
778 }
779  
780 //_____________________________________________________________________________
781 Int_t AliRun::GetEvent(Int_t event)
782 {
783   //
784   // Connect the Trees Kinematics and Hits for event # event
785   // Set branch addresses
786   //
787
788   // Reset existing structures
789   ResetHits();
790   ResetDigits();
791   ResetSDigits();
792   
793   // Delete Trees already connected
794   if (fTreeH) delete fTreeH;
795   if (fTreeD) delete fTreeD;
796   if (fTreeR) delete fTreeR;
797   if (fTreeS) delete fTreeS;
798
799  // Create the particle stack
800   if (fHeader) delete fHeader; 
801   fHeader = 0;
802   
803   // Get header from file
804   if(fTreeE) {
805       fTreeE->SetBranchAddress("Header", &fHeader);
806       fTreeE->GetEntry(event);
807   }  
808   else 
809       Error("GetEvent","Cannot find Header Tree (TE)\n");
810
811   // Get the stack from the header
812   if (fStack) delete fStack;
813   fStack = fHeader->Stack();
814   fStack->GetEvent(event);
815   //
816   TFile *file = fTreeE->GetCurrentFile();
817   char treeName[20];
818   file->cd();
819
820   // Get Hits Tree header from file
821   sprintf(treeName,"TreeH%d",event);
822   fTreeH = (TTree*)gDirectory->Get(treeName);
823   if (!fTreeH) {
824     Error("GetEvent","cannot find Hits Tree for event:%d\n",event);
825   }
826   
827   // Get Digits Tree header from file
828   sprintf(treeName,"TreeD%d",event);
829   fTreeD = (TTree*)gDirectory->Get(treeName);
830   if (!fTreeD) {
831     // Warning("GetEvent","cannot find Digits Tree for event:%d\n",event);
832   }
833
834   file->cd();
835
836   // Get SDigits Tree header from file
837   sprintf(treeName,"TreeS%d",event);
838   fTreeS = (TTree*)gDirectory->Get(treeName);
839   if (!fTreeS) {
840     // Warning("GetEvent","cannot find SDigits Tree for event:%d\n",event);
841   }
842
843   file->cd();
844   
845   // Get Reconstruct Tree header from file
846   sprintf(treeName,"TreeR%d",event);
847   fTreeR = (TTree*)gDirectory->Get(treeName);
848   if (!fTreeR) {
849     //    printf("WARNING: cannot find Reconstructed Tree for event:%d\n",event);
850   }
851
852   file->cd();
853  
854   // Set Trees branch addresses
855   TIter next(fModules);
856   AliModule *detector;
857   while((detector = (AliModule*)next())) {
858     detector->SetTreeAddress();
859   }
860
861   return fStack->GetNtrack();
862 }
863
864 //_____________________________________________________________________________
865 TGeometry *AliRun::GetGeometry()
866 {
867   //
868   // Import Alice geometry from current file
869   // Return pointer to geometry object
870   //
871   if (!fGeometry) fGeometry = (TGeometry*)gDirectory->Get("AliceGeom");
872   //
873   // Unlink and relink nodes in detectors
874   // This is bad and there must be a better way...
875   //
876   
877   TIter next(fModules);
878   AliModule *detector;
879   while((detector = (AliModule*)next())) {
880     TList *dnodes=detector->Nodes();
881     Int_t j;
882     TNode *node, *node1;
883     for ( j=0; j<dnodes->GetSize(); j++) {
884       node = (TNode*) dnodes->At(j);
885       node1 = fGeometry->GetNode(node->GetName());
886       dnodes->Remove(node);
887       dnodes->AddAt(node1,j);
888     }
889   }
890   return fGeometry;
891 }
892
893 //_____________________________________________________________________________
894 void AliRun::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
895                           Float_t &e, Float_t *vpos, Float_t *polar,
896                           Float_t &tof)
897 {
898   // Delegate to stack
899   //
900     fStack->GetNextTrack(mtrack, ipart, pmom, e, vpos, polar, tof);  
901 }
902
903 //_____________________________________________________________________________
904 Int_t AliRun::GetPrimary(Int_t track) const
905 {
906   //
907   // return number of primary that has generated track
908   //
909     return fStack->GetPrimary(track);
910 }
911  
912 //_____________________________________________________________________________
913 void AliRun::InitMC(const char *setup)
914 {
915   //
916   // Initialize the Alice setup
917   //
918
919   if(fInitDone) {
920     Warning("Init","Cannot initialise AliRun twice!\n");
921     return;
922   }
923     
924   gROOT->LoadMacro(setup);
925   gInterpreter->ProcessLine(fConfigFunction.Data());
926
927
928   gMC->DefineParticles();  //Create standard MC particles
929
930   TObject *objfirst, *objlast;
931
932   fNdets = fModules->GetLast()+1;
933
934   //
935   //=================Create Materials and geometry
936   gMC->Init();
937
938   // Added also after in case of interactive initialisation of modules
939   fNdets = fModules->GetLast()+1;
940
941    TIter next(fModules);
942    AliModule *detector;
943    while((detector = (AliModule*)next())) {
944       detector->SetTreeAddress();
945       objlast = gDirectory->GetList()->Last();
946       
947       // Add Detector histograms in Detector list of histograms
948       if (objlast) objfirst = gDirectory->GetList()->After(objlast);
949       else         objfirst = gDirectory->GetList()->First();
950       while (objfirst) {
951         detector->Histograms()->Add(objfirst);
952         objfirst = gDirectory->GetList()->After(objfirst);
953       }
954    }
955    ReadTransPar(); //Read the cuts for all materials
956    
957    MediaTable(); //Build the special IMEDIA table
958    
959    //Initialise geometry deposition table
960    fEventEnergy.Set(gMC->NofVolumes()+1);
961    fSummEnergy.Set(gMC->NofVolumes()+1);
962    fSum2Energy.Set(gMC->NofVolumes()+1);
963    
964    //Compute cross-sections
965    gMC->BuildPhysics();
966    
967    //Write Geometry object to current file.
968    fGeometry->Write();
969    
970    fInitDone = kTRUE;
971
972    fMCQA = new AliMCQA(fNdets);
973
974    AliConfig::Instance();
975    //
976    // Save stuff at the beginning of the file to avoid file corruption
977    Write();
978 }
979
980 //_____________________________________________________________________________
981 void AliRun::MediaTable()
982 {
983   //
984   // Built media table to get from the media number to
985   // the detector id
986   //
987   Int_t kz, nz, idt, lz, i, k, ind;
988   //  Int_t ibeg;
989   TObjArray &dets = *gAlice->Detectors();
990   AliModule *det;
991   //
992   // For all detectors
993   for (kz=0;kz<fNdets;kz++) {
994     // If detector is defined
995     if((det=(AliModule*) dets[kz])) {
996         TArrayI &idtmed = *(det->GetIdtmed()); 
997         for(nz=0;nz<100;nz++) {
998         // Find max and min material number
999         if((idt=idtmed[nz])) {
1000           det->LoMedium() = det->LoMedium() < idt ? det->LoMedium() : idt;
1001           det->HiMedium() = det->HiMedium() > idt ? det->HiMedium() : idt;
1002         }
1003       }
1004       if(det->LoMedium() > det->HiMedium()) {
1005         det->LoMedium() = 0;
1006         det->HiMedium() = 0;
1007       } else {
1008         if(det->HiMedium() > fImedia->GetSize()) {
1009           Error("MediaTable","Increase fImedia from %d to %d",
1010                 fImedia->GetSize(),det->HiMedium());
1011           return;
1012         }
1013         // Tag all materials in rage as belonging to detector kz
1014         for(lz=det->LoMedium(); lz<= det->HiMedium(); lz++) {
1015           (*fImedia)[lz]=kz;
1016         }
1017       }
1018     }
1019   }
1020   //
1021   // Print summary table
1022   printf(" Traking media ranges:\n");
1023   for(i=0;i<(fNdets-1)/6+1;i++) {
1024     for(k=0;k< (6<fNdets-i*6?6:fNdets-i*6);k++) {
1025       ind=i*6+k;
1026       det=(AliModule*)dets[ind];
1027       if(det)
1028         printf(" %6s: %3d -> %3d;",det->GetName(),det->LoMedium(),
1029                det->HiMedium());
1030       else
1031         printf(" %6s: %3d -> %3d;","NULL",0,0);
1032     }
1033     printf("\n");
1034   }
1035 }
1036
1037 //____________________________________________________________________________
1038 void AliRun::SetGenerator(AliGenerator *generator)
1039 {
1040   //
1041   // Load the event generator
1042   //
1043   if(!fGenerator) fGenerator = generator;
1044 }
1045
1046 //____________________________________________________________________________
1047 void AliRun::ResetGenerator(AliGenerator *generator)
1048 {
1049   //
1050   // Load the event generator
1051   //
1052   if(fGenerator)
1053     if(generator)
1054       Warning("ResetGenerator","Replacing generator %s with %s\n",
1055               fGenerator->GetName(),generator->GetName());
1056     else
1057       Warning("ResetGenerator","Replacing generator %s with NULL\n",
1058               fGenerator->GetName());
1059   fGenerator = generator;
1060 }
1061
1062 //____________________________________________________________________________
1063 void AliRun::SetTransPar(char *filename)
1064 {
1065   fTransParName = filename;
1066 }
1067
1068 //____________________________________________________________________________
1069 void AliRun::SetBaseFile(char *filename)
1070 {
1071   fBaseFileName = filename;
1072 }
1073
1074 //____________________________________________________________________________
1075 void AliRun::ReadTransPar()
1076 {
1077   //
1078   // Read filename to set the transport parameters
1079   //
1080
1081
1082   const Int_t kncuts=10;
1083   const Int_t knflags=11;
1084   const Int_t knpars=kncuts+knflags;
1085   const char kpars[knpars][7] = {"CUTGAM" ,"CUTELE","CUTNEU","CUTHAD","CUTMUO",
1086                                "BCUTE","BCUTM","DCUTE","DCUTM","PPCUTM","ANNI",
1087                                "BREM","COMP","DCAY","DRAY","HADR","LOSS",
1088                                "MULS","PAIR","PHOT","RAYL"};
1089   char line[256];
1090   char detName[7];
1091   char* filtmp;
1092   Float_t cut[kncuts];
1093   Int_t flag[knflags];
1094   Int_t i, itmed, iret, ktmed, kz;
1095   FILE *lun;
1096   //
1097   // See whether the file is there
1098   filtmp=gSystem->ExpandPathName(fTransParName.Data());
1099   lun=fopen(filtmp,"r");
1100   delete [] filtmp;
1101   if(!lun) {
1102     Warning("ReadTransPar","File %s does not exist!\n",fTransParName.Data());
1103     return;
1104   }
1105   //
1106   if(fDebug) {
1107     printf(" "); for(i=0;i<60;i++) printf("*"); printf("\n");
1108     printf(" *%59s\n","*");
1109     printf(" *       Please check carefully what you are doing!%10s\n","*");
1110     printf(" *%59s\n","*");
1111   }
1112   //
1113   while(1) {
1114     // Initialise cuts and flags
1115     for(i=0;i<kncuts;i++) cut[i]=-99;
1116     for(i=0;i<knflags;i++) flag[i]=-99;
1117     itmed=0;
1118     for(i=0;i<256;i++) line[i]='\0';
1119     // Read up to the end of line excluded
1120     iret=fscanf(lun,"%[^\n]",line);
1121     if(iret<0) {
1122       //End of file
1123       fclose(lun);
1124       if(fDebug){
1125         printf(" *%59s\n","*");
1126         printf(" "); for(i=0;i<60;i++) printf("*"); printf("\n");
1127       }
1128       return;
1129     }
1130     // Read the end of line
1131     fscanf(lun,"%*c");
1132     if(!iret) continue;
1133     if(line[0]=='*') continue;
1134     // Read the numbers
1135     iret=sscanf(line,"%s %d %f %f %f %f %f %f %f %f %f %f %d %d %d %d %d %d %d %d %d %d %d",
1136                 detName,&itmed,&cut[0],&cut[1],&cut[2],&cut[3],&cut[4],&cut[5],&cut[6],&cut[7],&cut[8],
1137                 &cut[9],&flag[0],&flag[1],&flag[2],&flag[3],&flag[4],&flag[5],&flag[6],&flag[7],
1138                 &flag[8],&flag[9],&flag[10]);
1139     if(!iret) continue;
1140     if(iret<0) {
1141       //reading error
1142       Warning("ReadTransPar","Error reading file %s\n",fTransParName.Data());
1143       continue;
1144     }
1145     // Check that the module exist
1146     AliModule *mod = GetModule(detName);
1147     if(mod) {
1148       // Get the array of media numbers
1149       TArrayI &idtmed = *mod->GetIdtmed();
1150       // Check that the tracking medium code is valid
1151       if(0<=itmed && itmed < 100) {
1152         ktmed=idtmed[itmed];
1153         if(!ktmed) {
1154           Warning("ReadTransPar","Invalid tracking medium code %d for %s\n",itmed,mod->GetName());
1155           continue;
1156         }
1157         // Set energy thresholds
1158         for(kz=0;kz<kncuts;kz++) {
1159           if(cut[kz]>=0) {
1160             if(fDebug) printf(" *  %-6s set to %10.3E for tracking medium code %4d for %s\n",
1161                    kpars[kz],cut[kz],itmed,mod->GetName());
1162             gMC->Gstpar(ktmed,kpars[kz],cut[kz]);
1163           }
1164         }
1165         // Set transport mechanisms
1166         for(kz=0;kz<knflags;kz++) {
1167           if(flag[kz]>=0) {
1168             if(fDebug) printf(" *  %-6s set to %10d for tracking medium code %4d for %s\n",
1169                    kpars[kncuts+kz],flag[kz],itmed,mod->GetName());
1170             gMC->Gstpar(ktmed,kpars[kncuts+kz],Float_t(flag[kz]));
1171           }
1172         }
1173       } else {
1174         Warning("ReadTransPar","Invalid medium code %d *\n",itmed);
1175         continue;
1176       }
1177     } else {
1178       if(fDebug) printf("%s::ReadTransParModule: %s not present\n",ClassName(),detName);
1179       continue;
1180     }
1181   }
1182 }
1183
1184
1185 //_____________________________________________________________________________
1186 void AliRun::MakeTree(Option_t *option, const char *file)
1187 {
1188   //
1189   //  Create the ROOT trees
1190   //  Loop on all detectors to create the Root branch (if any)
1191   //
1192
1193   char hname[30];
1194   //
1195   // Analyse options
1196   const char *oK = strstr(option,"K");
1197   const char *oH = strstr(option,"H");
1198   const char *oE = strstr(option,"E");
1199   const char *oD = strstr(option,"D");
1200   const char *oR = strstr(option,"R");
1201   const char *oS = strstr(option,"S");
1202   //
1203
1204   TDirectory *cwd = gDirectory;
1205
1206   TBranch *branch = 0;
1207   
1208   if (oK) fStack->MakeTree(fEvent, file);
1209
1210   if (oE && !fTreeE) {
1211     fTreeE = new TTree("TE","Header");
1212     branch = fTreeE->Branch("Header", "AliHeader", &fHeader, 4000, 0);         
1213     branch->SetAutoDelete(kFALSE);           
1214     TFolder *folder = (TFolder *)gROOT->FindObjectAny("/Folders/RunMC/Event/Header");
1215     if (folder) folder->Add(fHeader);
1216 //    branch = fTreeE->Branch("Stack","AliStack", &fStack, 4000, 0);
1217 //    branch->SetAutoDelete(kFALSE);         
1218 //    if (folder) folder->Add(fStack);
1219     fTreeE->Write(0,TObject::kOverwrite);
1220   }
1221   
1222   if (file && branch) {
1223     char * outFile = new char[strlen(gAlice->GetBaseFile())+strlen(file)+2];
1224     sprintf(outFile,"%s/%s",GetBaseFile(),file);
1225     branch->SetFile(outFile);
1226     TIter next( branch->GetListOfBranches());
1227     while ((branch=(TBranch*)next())) {
1228        branch->SetFile(outFile);
1229     } 
1230     if (GetDebug()>1)
1231         printf("* MakeBranch * Diverting Branch %s to file %s\n", branch->GetName(),file);
1232     cwd->cd();
1233     delete outFile;
1234   }
1235   
1236   if (oH && !fTreeH) {
1237     sprintf(hname,"TreeH%d",fEvent);
1238     fTreeH = new TTree(hname,"Hits");
1239     fTreeH->SetAutoSave(1000000000); //no autosave
1240     fTreeH->Write(0,TObject::kOverwrite);
1241   }
1242   if (oD && !fTreeD) {
1243     sprintf(hname,"TreeD%d",fEvent);
1244     fTreeD = new TTree(hname,"Digits");
1245     fTreeD->Write(0,TObject::kOverwrite);
1246   }
1247   if (oS && !fTreeS) {
1248     sprintf(hname,"TreeS%d",fEvent);
1249     fTreeS = new TTree(hname,"SDigits");
1250     fTreeS->Write(0,TObject::kOverwrite);
1251   }
1252   if (oR && !fTreeR) {
1253     sprintf(hname,"TreeR%d",fEvent);
1254     fTreeR = new TTree(hname,"Reconstruction");
1255     fTreeR->Write(0,TObject::kOverwrite);
1256   }
1257
1258   //
1259   // Create a branch for hits/digits for each detector
1260   // Each branch is a TClonesArray. Each data member of the Hits classes
1261   // will be in turn a subbranch of the detector master branch
1262   TIter next(fModules);
1263   AliModule *detector;
1264   while((detector = (AliModule*)next())) {
1265      if (oH) detector->MakeBranch(option,file);
1266   }
1267 }
1268
1269 //_____________________________________________________________________________
1270 TParticle* AliRun::Particle(Int_t i)
1271 {
1272     return fStack->Particle(i);
1273 }
1274
1275 //_____________________________________________________________________________
1276 void AliRun::BeginEvent()
1277 {
1278     // Clean-up previous event
1279     // Energy scores  
1280     fEventEnergy.Reset();  
1281     // Clean detector information
1282     CleanDetectors();
1283     // Reset stack info
1284     fStack->Reset();
1285
1286  
1287   //
1288   //  Reset all Detectors & kinematics & trees
1289   //
1290   char hname[30];
1291   //
1292   // Initialise event header
1293   fHeader->Reset(fRun,fEvent);
1294   //
1295   fStack->BeginEvent(fEvent);
1296
1297   //
1298   if(fLego) {
1299     fLego->BeginEvent();
1300     return;
1301   }
1302
1303   //
1304
1305   ResetHits();
1306   ResetDigits();
1307   ResetSDigits();
1308
1309
1310   if(fTreeH) {
1311     fTreeH->Reset();
1312     sprintf(hname,"TreeH%d",fEvent);
1313     fTreeH->SetName(hname);
1314   }
1315   if(fTreeD) {
1316     fTreeD->Reset();
1317     sprintf(hname,"TreeD%d",fEvent);
1318     fTreeD->SetName(hname);
1319     fTreeD->Write(0,TObject::kOverwrite);
1320   }
1321   if(fTreeS) {
1322     fTreeS->Reset();
1323     sprintf(hname,"TreeS%d",fEvent);
1324     fTreeS->SetName(hname);
1325     fTreeS->Write(0,TObject::kOverwrite);
1326   }
1327   if(fTreeR) {
1328     fTreeR->Reset();
1329     sprintf(hname,"TreeR%d",fEvent);
1330     fTreeR->SetName(hname);
1331     fTreeR->Write(0,TObject::kOverwrite);
1332   }
1333 }
1334 //_____________________________________________________________________________
1335 void AliRun::ResetDigits()
1336 {
1337   //
1338   //  Reset all Detectors digits
1339   //
1340   TIter next(fModules);
1341   AliModule *detector;
1342   while((detector = (AliModule*)next())) {
1343      detector->ResetDigits();
1344   }
1345 }
1346
1347 //_____________________________________________________________________________
1348 void AliRun::ResetSDigits()
1349 {
1350   //
1351   //  Reset all Detectors digits
1352   //
1353   TIter next(fModules);
1354   AliModule *detector;
1355   while((detector = (AliModule*)next())) {
1356      detector->ResetSDigits();
1357   }
1358 }
1359
1360 //_____________________________________________________________________________
1361 void AliRun::ResetHits()
1362 {
1363   //
1364   //  Reset all Detectors hits
1365   //
1366   TIter next(fModules);
1367   AliModule *detector;
1368   while((detector = (AliModule*)next())) {
1369      detector->ResetHits();
1370   }
1371 }
1372
1373 //_____________________________________________________________________________
1374 void AliRun::ResetPoints()
1375 {
1376   //
1377   // Reset all Detectors points
1378   //
1379   TIter next(fModules);
1380   AliModule *detector;
1381   while((detector = (AliModule*)next())) {
1382      detector->ResetPoints();
1383   }
1384 }
1385
1386 //_____________________________________________________________________________
1387 void AliRun::RunMC(Int_t nevent, const char *setup)
1388 {
1389   //
1390   // Main function to be called to process a galice run
1391   // example
1392   //    Root > gAlice.Run(); 
1393   // a positive number of events will cause the finish routine
1394   // to be called
1395   //
1396
1397   // check if initialisation has been done
1398   if (!fInitDone) InitMC(setup);
1399   
1400   // Create the Root Tree with one branch per detector
1401
1402    MakeTree("ESDR");
1403
1404   if (gSystem->Getenv("CONFIG_SPLIT_FILE")) {
1405      MakeTree("K","Kine.root");
1406      MakeTree("H","Hits.root");
1407   } else {
1408      MakeTree("KH");
1409   }
1410
1411   gMC->ProcessRun(nevent);
1412
1413   // End of this run, close files
1414   if(nevent>0) FinishRun();
1415 }
1416
1417 //_____________________________________________________________________________
1418 void AliRun::RunReco(const char *selected)
1419 {
1420   //
1421   // Main function to be called to reconstruct Alice event
1422   // 
1423    cout << "Found "<< gAlice->TreeE()->GetEntries() << "events" << endl; 
1424    for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
1425      cout << "Processing event "<< nevent << endl;
1426      GetEvent(nevent);
1427      // MakeTree("R");
1428      Digits2Reco(selected);
1429    }
1430 }
1431
1432 //_____________________________________________________________________________
1433
1434 void AliRun::Hits2Digits(const char *selected)
1435 {
1436
1437    // Convert Hits to sumable digits
1438    // 
1439    for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
1440      GetEvent(nevent);
1441     // MakeTree("D");
1442      Hits2SDigits(selected);
1443      SDigits2Digits(selected);
1444    }  
1445 }
1446
1447
1448 //_____________________________________________________________________________
1449
1450 void AliRun::Tree2Tree(Option_t *option, const char *selected)
1451 {
1452   //
1453   // Function to transform the content of
1454   //  
1455   // - TreeH to TreeS (option "S")
1456   // - TreeS to TreeD (option "D")
1457   // - TreeD to TreeR (option "R")
1458   // 
1459   // If multiple options are specified ("SDR"), transformation will be done in sequence for
1460   // selected detector and for all detectors if none is selected (detector string 
1461   // can contain blank separated list of detector names). 
1462
1463
1464    const char *oS = strstr(option,"S");
1465    const char *oD = strstr(option,"D");
1466    const char *oR = strstr(option,"R");
1467    
1468    TObjArray *detectors = Detectors();
1469
1470    TIter next(detectors);
1471
1472    AliDetector *detector = 0;
1473
1474    TDirectory *cwd = gDirectory;
1475
1476    char outFile[32];
1477    
1478    while((detector = (AliDetector*)next())) {
1479      if (selected) 
1480        if (strcmp(detector->GetName(),selected)) continue;
1481      if (detector->IsActive()){ 
1482        if (gSystem->Getenv("CONFIG_SPLIT_FILE")) {          
1483           if (oS) {
1484             sprintf(outFile,"SDigits.%s.root",detector->GetName());
1485             detector->MakeBranch("S",outFile);
1486           }    
1487           if (oD) {
1488             sprintf(outFile,"Digits.%s.root",detector->GetName());
1489             detector->MakeBranch("D",outFile);
1490           }    
1491           if (oR) {
1492             sprintf(outFile,"Reco.%s.root",detector->GetName());
1493             detector->MakeBranch("R",outFile);
1494           }    
1495        } else {
1496           detector->MakeBranch(option);
1497        }
1498        
1499        cwd->cd(); 
1500
1501        if (oS) {
1502           cout << "Hits2SDigits: Processing " << detector->GetName() << "..." << endl;
1503           detector->Hits2SDigits(); 
1504        }  
1505        if (oD) {
1506           cout << "SDigits2Digits: Processing " << detector->GetName() << "..." << endl;
1507           detector->SDigits2Digits();
1508        } 
1509        if (oR) {
1510           cout << "Digits2Reco: Processing " << detector->GetName() << "..." << endl;
1511           detector->Digits2Reco(); 
1512        }
1513        
1514        cwd->cd();        
1515      }   
1516    }
1517 }
1518
1519
1520 //_____________________________________________________________________________
1521 void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
1522                      Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
1523                      Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener)
1524 {
1525   //
1526   // Generates lego plots of:
1527   //    - radiation length map phi vs theta
1528   //    - radiation length map phi vs eta
1529   //    - interaction length map
1530   //    - g/cm2 length map
1531   //
1532   //  ntheta    bins in theta, eta
1533   //  themin    minimum angle in theta (degrees)
1534   //  themax    maximum angle in theta (degrees)
1535   //  nphi      bins in phi
1536   //  phimin    minimum angle in phi (degrees)
1537   //  phimax    maximum angle in phi (degrees)
1538   //  rmin      minimum radius
1539   //  rmax      maximum radius
1540   //  
1541   //
1542   //  The number of events generated = ntheta*nphi
1543   //  run input parameters in macro setup (default="Config.C")
1544   //
1545   //  Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
1546   //Begin_Html
1547   /*
1548     <img src="picts/AliRunLego1.gif">
1549   */
1550   //End_Html
1551   //Begin_Html
1552   /*
1553     <img src="picts/AliRunLego2.gif">
1554   */
1555   //End_Html
1556   //Begin_Html
1557   /*
1558     <img src="picts/AliRunLego3.gif">
1559   */
1560   //End_Html
1561   //
1562
1563   // check if initialisation has been done
1564   if (!fInitDone) InitMC(setup);
1565   //Save current generator
1566   AliGenerator *gen=Generator();
1567
1568   // Set new generator
1569   if (!gener) gener  = new AliLegoGenerator();
1570   ResetGenerator(gener);
1571   //
1572   // Configure Generator
1573   gener->SetRadiusRange(rmin, rmax);
1574   gener->SetZMax(zmax);
1575   gener->SetCoor1Range(nc1, c1min, c1max);
1576   gener->SetCoor2Range(nc2, c2min, c2max);
1577   
1578   
1579   //Create Lego object  
1580   fLego = new AliLego("lego",gener);
1581
1582   //Prepare MC for Lego Run
1583   gMC->InitLego();
1584   
1585   //Run Lego Object
1586
1587   gMC->ProcessRun(nc1*nc2+1);
1588   
1589   // Create only the Root event Tree
1590   MakeTree("E");
1591   
1592   // End of this run, close files
1593   FinishRun();
1594   // Restore current generator
1595   ResetGenerator(gen);
1596   // Delete Lego Object
1597   delete fLego; fLego=0;
1598 }
1599
1600 //_____________________________________________________________________________
1601 void AliRun::SetConfigFunction(const char * config) 
1602 {
1603   //
1604   // Set the signature of the function contained in Config.C to configure
1605   // the run
1606   //
1607   fConfigFunction=config;
1608 }
1609
1610 //_____________________________________________________________________________
1611 void AliRun::SetCurrentTrack(Int_t track)
1612
1613   //
1614   // Set current track number
1615   //
1616     fStack->SetCurrentTrack(track); 
1617 }
1618  
1619 //_____________________________________________________________________________
1620 void AliRun::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
1621                       Float_t *vpos, Float_t *polar, Float_t tof,
1622                       AliMCProcess mech, Int_t &ntr, Float_t weight)
1623
1624 // Delegate to stack
1625 //
1626     fStack->SetTrack(done, parent, pdg, pmom, vpos, polar, tof,
1627                      mech, ntr, weight);
1628 }
1629
1630 //_____________________________________________________________________________
1631 void AliRun::SetTrack(Int_t done, Int_t parent, Int_t pdg,
1632                       Double_t px, Double_t py, Double_t pz, Double_t e,
1633                       Double_t vx, Double_t vy, Double_t vz, Double_t tof,
1634                       Double_t polx, Double_t poly, Double_t polz,
1635                       AliMCProcess mech, Int_t &ntr, Float_t weight)
1636
1637   // Delegate to stack
1638   //
1639     fStack->SetTrack(done, parent, pdg, px, py, pz, e, vx, vy, vz, tof,
1640                    polx, poly, polz, mech, ntr, weight);
1641     
1642 }
1643
1644 //_____________________________________________________________________________
1645 void AliRun::SetHighWaterMark(const Int_t nt)
1646 {
1647     //
1648     // Set high water mark for last track in event
1649     fStack->SetHighWaterMark(nt);
1650 }
1651
1652 //_____________________________________________________________________________
1653 void AliRun::KeepTrack(const Int_t track)
1654
1655   //
1656   // Delegate to stack
1657   //
1658     fStack->KeepTrack(track);
1659 }
1660  
1661 //_____________________________________________________________________________
1662 void AliRun::StepManager(Int_t id) 
1663 {
1664   //
1665   // Called at every step during transport
1666   //
1667
1668   //
1669   // --- If lego option, do it and leave 
1670   if (fLego)
1671     fLego->StepManager();
1672   else {
1673     Int_t copy;
1674     //Update energy deposition tables
1675     AddEnergyDeposit(gMC->CurrentVolID(copy),gMC->Edep());
1676   
1677     //Call the appropriate stepping routine;
1678     AliModule *det = (AliModule*)fModules->At(id);
1679     if(det) {
1680       fMCQA->StepManager(id);
1681       det->StepManager();
1682     }
1683   }
1684 }
1685
1686 //_____________________________________________________________________________
1687 void AliRun::Streamer(TBuffer &R__b)
1688 {
1689    // Stream an object of class AliRun.
1690
1691    if (R__b.IsReading()) {
1692       if (!gAlice) gAlice = this;
1693
1694       AliRun::Class()->ReadBuffer(R__b, this);
1695       //
1696       gROOT->GetListOfBrowsables()->Add(this,"Run");
1697
1698       fTreeE = (TTree*)gDirectory->Get("TE");
1699       if (fTreeE) {
1700           fTreeE->SetBranchAddress("Header", &fHeader);
1701       }
1702       
1703       else    Error("Streamer","cannot find Header Tree\n");
1704       fTreeE->GetEntry(0);
1705
1706       gRandom = fRandom;
1707    } else {
1708       AliRun::Class()->WriteBuffer(R__b, this);
1709    }
1710 }
1711
1712
1713 //___________________________________________________________________________
1714 Int_t AliRun::CurrentTrack() const {
1715   //
1716   // Returns current track
1717   //
1718   return fStack->CurrentTrack();
1719 }
1720
1721 //___________________________________________________________________________
1722 Int_t AliRun::GetNtrack() const {
1723   //
1724   // Returns number of tracks in stack
1725   //
1726   return fStack->GetNtrack();
1727 }
1728
1729 //___________________________________________________________________________
1730 TObjArray* AliRun::Particles() {
1731   //
1732   // Returns pointer to Particles array
1733   //
1734   return fStack->Particles();
1735 }
1736
1737 //___________________________________________________________________________
1738 TTree* AliRun::TreeK() {
1739   //
1740   // Returns pointer to the TreeK array
1741   //
1742   return fStack->TreeK();
1743 }