]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/STEER/AliRun.cxx
made d-tor virtual
[u/mrichter/AliRoot.git] / STEER / 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 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 //  Control class for Alice C++                                              //
21 //  Only one single instance of this class exists.                           //
22 //  The object is created in main program aliroot                            //
23 //  and is pointed by the global gAlice.                                     //
24 //                                                                           //
25 //   -Supports the list of all Alice Detectors (fModules).                 //
26 //   -Supports the list of particles (fParticles).                           //
27 //   -Supports the Trees.                                                    //
28 //   -Supports the geometry.                                                 //
29 //   -Supports the event display.                                            //
30 //Begin_Html
31 /*
32 <img src="picts/AliRunClass.gif">
33 */
34 //End_Html
35 //Begin_Html
36 /*
37 <img src="picts/alirun.gif">
38 */
39 //End_Html
40 //                                                                           //
41 ///////////////////////////////////////////////////////////////////////////////
42
43 #include <TCint.h> 
44 #include <TROOT.h>
45 #include <TRandom3.h>
46 #include <TSystem.h>
47 #include <TVirtualMC.h>
48 #include <TGeoManager.h>
49 #include <TTree.h>
50 // 
51 #include "AliLog.h"
52 #include "AliDetector.h"
53 #include "AliHeader.h"
54 #include "AliMC.h"
55 #include "AliPDG.h"
56 #include "AliRun.h"
57 #include "AliStack.h"
58 #include "AliCDBManager.h"
59 #include "AliAlignObj.h"
60 #include "AliSimulation.h"
61 #include "AliLego.h"
62
63 AliRun *gAlice;
64
65 ClassImp(AliRun)
66
67 //_______________________________________________________________________
68 AliRun::AliRun():
69 //  fRun(-1),
70   fEventNrInRun(-1),
71   fModules(0),
72   fMCApp(0),
73   fNdets(0),
74   fConfigFunction(""),
75   fBaseFileName(""),
76   fRunLoader(0x0)
77 {
78   //
79   // Default constructor for AliRun
80   //
81   AliConfig::Instance();//skowron 29 Feb 2002
82                         //ensures that the folder structure is build
83
84 }
85
86 //_____________________________________________________________________________
87 AliRun::AliRun(const char *name, const char *title):
88   TNamed(name,title),
89   fEventNrInRun(-1),
90   fModules(new TObjArray(77)), // Support list for the Detectors
91   fMCApp(new AliMC(GetName(),GetTitle())),
92   fNdets(0),
93   fConfigFunction("Config();"),
94   fBaseFileName(""),
95   fRunLoader(0x0)
96 {
97   //
98   //  Constructor for the main processor.
99   //  Creates the geometry
100   //  Creates the list of Detectors.
101   //  Creates the list of particles.
102   //
103
104   gAlice     = this;
105
106   // Set random number generator
107   gRandom = new TRandom3();
108
109   if (gSystem->Getenv("CONFIG_SEED")) {
110      gRandom->SetSeed(static_cast<UInt_t>(atoi(gSystem->Getenv("CONFIG_SEED"))));
111   }
112
113   // Add to list of browsable  
114   gROOT->GetListOfBrowsables()->Add(this,name);
115   
116 }
117
118 //_______________________________________________________________________
119 AliRun::~AliRun()
120 {
121   //
122   // Default AliRun destructor
123   //
124   gROOT->GetListOfBrowsables()->Remove(this);
125
126   if (fRunLoader)
127    {
128     TFolder* evfold = fRunLoader->GetEventFolder();
129     TFolder* modfold = dynamic_cast<TFolder*>(evfold->FindObjectAny(AliConfig::GetModulesFolderName()));
130     if(!modfold) AliFatal(Form("Folder %s not found\n",AliConfig::GetModulesFolderName().Data()));
131     TIter next(fModules);
132     AliModule *mod;
133     while((mod = (AliModule*)next()))
134      { 
135        modfold->Remove(mod);
136      }
137    }
138     
139   delete fMCApp;
140   delete gMC; gMC=0;
141   if (fModules) {
142     fModules->Delete();
143     delete fModules;
144   }
145   
146 }
147
148 //_____________________________________________________________________________
149 void AliRun::InitLoaders()
150 {
151   //creates list of getters
152   AliDebug(1, "");
153   TIter next(fModules);
154   AliModule *mod;
155   while((mod = (AliModule*)next()))
156    { 
157      mod->SetRunLoader(fRunLoader);
158      AliDetector *det = dynamic_cast<AliDetector*>(mod);
159      if (det) 
160       {
161         AliDebug(2, Form("Adding %s", det->GetName()));
162         fRunLoader->AddLoader(det);
163       }
164    }
165   AliDebug(1, "Done");
166 }
167
168 //_______________________________________________________________________
169 void AliRun::Announce() const
170 {
171   //
172   // Announce the current version of AliRoot
173   //
174   printf("%70s",
175          "****************************************************************\n");
176   printf("%6s","*");printf("%64s","*\n");
177
178   printf("%6s","*");
179   printf("    You are running AliRoot version NewIO\n");
180
181   printf("%6s","*");
182   printf("    The SVN version for the current program is $Id$\n");
183
184   printf("%6s","*");printf("%64s","*\n");
185   printf("%70s",
186          "****************************************************************\n");
187 }
188
189 //_______________________________________________________________________
190 AliModule *AliRun::GetModule(const char *name) const
191 {
192   //
193   // Return pointer to detector from name
194   //
195   return dynamic_cast<AliModule*>(fModules->FindObject(name));
196 }
197  
198 //_______________________________________________________________________
199 AliDetector *AliRun::GetDetector(const char *name) const
200 {
201   //
202   // Return pointer to detector from name
203   //
204   return dynamic_cast<AliDetector*>(fModules->FindObject(name));
205 }
206  
207 //_______________________________________________________________________
208 Int_t AliRun::GetModuleID(const char *name) const
209 {
210   //
211   // Return galice internal detector identifier from name
212   //
213   Int_t i=-1;
214   TObject *mod=fModules->FindObject(name);
215   if(mod) i=fModules->IndexOf(mod);
216   return i;
217 }
218  
219 //_______________________________________________________________________
220 Int_t AliRun::GetEvent(Int_t event)
221 {
222 //
223 // Reloads data containers in folders # event
224 // Set branch addresses
225 //
226   if (fRunLoader == 0x0)
227    {
228      AliError("RunLoader is not set. Can not load data.");
229      return -1;
230    }
231 /*****************************************/ 
232 /****   P R E    R E L O A D I N G    ****/
233 /*****************************************/ 
234 // Reset existing structures
235   fMCApp->ResetHits();
236   fMCApp->ResetTrackReferences();
237   fMCApp->ResetDigits();
238   fMCApp->ResetSDigits();
239
240 /*****************************************/ 
241 /****       R  E  L  O  A  D          ****/
242 /*****************************************/
243
244   AliRunLoader::Instance()->GetEvent(event);
245
246 /*****************************************/ 
247 /****  P O S T    R E L O A D I N G   ****/
248 /*****************************************/ 
249
250   // Set Trees branch addresses
251   TIter next(fModules);
252   AliDetector *detector;
253   while((detector = dynamic_cast<AliDetector*>(next()))) 
254    {
255      detector->SetTreeAddress();
256    }
257  
258   return AliRunLoader::Instance()->GetHeader()->GetNtrack();
259 }
260
261 //_______________________________________________________________________
262 void AliRun::SetBaseFile(const char *filename)
263 {
264   fBaseFileName = filename;
265 }
266
267
268 //_______________________________________________________________________
269 void AliRun::Hits2Digits(const char *selected)
270 {
271
272    // Convert Hits to sumable digits
273    // 
274    for (Int_t nevent=0; nevent<AliRunLoader::Instance()->TreeE()->GetEntries(); nevent++) {
275      GetEvent(nevent);
276      Hits2SDigits(selected);
277      SDigits2Digits(selected);
278    }  
279 }
280
281
282 //_______________________________________________________________________
283 void AliRun::Tree2Tree(Option_t *option, const char *selected)
284 {
285   //
286   // Function to transform the content of
287   //  
288   // - TreeH to TreeS (option "S")
289   // - TreeS to TreeD (option "D")
290   // - TreeD to TreeR (option "R")
291   // 
292   // If multiple options are specified ("SDR"), transformation will be done in sequence for
293   // selected detector and for all detectors if none is selected (detector string 
294   // can contain blank separated list of detector names). 
295
296
297    const char *oS = strstr(option,"S");
298    const char *oD = strstr(option,"D");
299    const char *oR = strstr(option,"R");
300    
301    TObjArray *detectors = Detectors();
302
303    TIter next(detectors);
304
305    AliDetector *detector = 0;
306
307    while((detector = dynamic_cast<AliDetector*>(next()))) {
308      if (selected) 
309        if (strcmp(detector->GetName(),selected)) continue;
310      if (detector->IsActive())
311       { 
312        
313        AliLoader* loader = detector->GetLoader();
314        if (loader == 0x0) continue;
315        
316        if (oS) 
317         {
318           AliDebug(1, Form("Processing Hits2SDigits for %s ...", detector->GetName()));
319           loader->LoadHits("read");
320           if (loader->TreeS() == 0x0) loader->MakeTree("S");
321           detector->MakeBranch(option);
322           detector->SetTreeAddress();
323           detector->Hits2SDigits();
324           loader->UnloadHits();
325           loader->UnloadSDigits();
326         }  
327        if (oD) 
328         {
329           AliDebug(1, Form("Processing SDigits2Digits for %s ...", detector->GetName()));
330           loader->LoadSDigits("read");
331           if (loader->TreeD() == 0x0) loader->MakeTree("D");
332           detector->MakeBranch(option);
333           detector->SetTreeAddress();
334           detector->SDigits2Digits();
335           loader->UnloadSDigits();
336           loader->UnloadDigits();
337         } 
338        if (oR) 
339         {
340           AliDebug(1, Form("Processing Digits2Reco for %s ...", detector->GetName()));
341           loader->LoadDigits("read");
342           if (loader->TreeR() == 0x0) loader->MakeTree("R");
343           detector->MakeBranch(option);
344           detector->SetTreeAddress();
345           detector->Digits2Reco(); 
346           loader->UnloadDigits();
347           loader->UnloadRecPoints();
348
349         }
350      }   
351    }
352 }
353
354
355 //_______________________________________________________________________
356 void AliRun::Streamer(TBuffer &R__b)
357 {
358   // Stream an object of class AliRun.
359
360   if (R__b.IsReading()) {
361     if (!gAlice) gAlice = this;
362     AliRun::Class()->ReadBuffer(R__b, this);
363     gROOT->GetListOfBrowsables()->Add(this,"Run");
364     gRandom = new TRandom3();
365   } else {
366     AliRun::Class()->WriteBuffer(R__b, this);
367   }
368 }
369
370 //_______________________________________________________________________
371 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
372 {
373   AliRunLoader::Instance()->GetHeader()->SetGenEventHeader(header);
374 }
375
376
377 //_______________________________________________________________________
378 Int_t AliRun::GetEvNumber() const
379
380 //Returns number of current event  
381   if (fRunLoader == 0x0)
382    {
383      AliError("RunLoader is not set. Can not load data.");
384      return -1;
385    }
386
387   return fRunLoader->GetEventNumber();
388 }
389
390 //_______________________________________________________________________
391 void AliRun::SetRunLoader(AliRunLoader* rloader)
392 {
393   //
394   // Set the loader of the run
395   //
396   fRunLoader = rloader;
397   if (fRunLoader == 0x0) return;
398   
399   TString evfoldname;
400   TFolder* evfold = fRunLoader->GetEventFolder();
401   if (evfold) evfoldname = evfold->GetName();
402   else AliFatal("Did not get Event Folder from Run Loader");
403   
404   if ( fRunLoader->GetAliRun() )
405    {//if alrun already exists in folder
406     if (fRunLoader->GetAliRun() != this )
407      {//and is different than this - crash
408        AliFatal("AliRun is already in Folder and it is not this object");
409        return;//pro forma
410      }//else do nothing
411    }
412   else
413    {
414      evfold->Add(this);//Post this AliRun to Folder
415    }
416   
417   TIter next(fModules);
418   AliModule *module;
419   while((module = (AliModule*)next())) 
420    {
421      if (evfold) AliConfig::Instance()->Add(module,evfoldname);
422      module->SetRunLoader(fRunLoader);
423      AliDetector* detector = dynamic_cast<AliDetector*>(module);
424      if (detector)
425       {
426         AliLoader* loader = fRunLoader->GetLoader(detector);
427         if (loader == 0x0)
428          {
429            AliError(Form("Can not get loader for detector %s", detector->GetName()));
430          }
431         else
432          {
433            AliDebug(1, Form("Setting loader for detector %s", detector->GetName()));
434            detector->SetLoader(loader);
435          }
436       }
437    }
438 }
439
440 //_______________________________________________________________________
441 void AliRun::AddModule(AliModule* mod)
442 {
443   //
444   // Add a module to the module list
445   //
446   if (mod == 0x0) return;
447   if (strlen(mod->GetName()) == 0) return;
448   if (GetModuleID(mod->GetName()) >= 0) return;
449   
450   AliDebug(1, mod->GetName());
451   if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
452   else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
453
454   Modules()->Add(mod);
455   
456   fNdets++;
457 }
458