]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliRun.cxx
This is a backward incompatible change in AliRoot. The following methods have been...
[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 /* $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 // 
50 #include "AliLog.h"
51 #include "AliDetector.h"
52 #include "AliHeader.h"
53 #include "AliMC.h"
54 #include "AliPDG.h"
55 #include "AliRun.h"
56 #include "AliStack.h"
57 #include "AliCDBManager.h"
58 #include "AliAlignObj.h"
59 #include "AliSimulation.h"
60 #include "AliLego.h"
61
62 AliRun *gAlice;
63
64 ClassImp(AliRun)
65
66 //_______________________________________________________________________
67 AliRun::AliRun():
68 //  fRun(-1),
69   fEventNrInRun(-1),
70   fModules(0),
71   fMCApp(0),
72   fNdets(0),
73   fConfigFunction(""),
74   fRandom(0),
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   fRandom(new TRandom3()),
95   fBaseFileName(""),
96   fRunLoader(0x0)
97 {
98   //
99   //  Constructor for the main processor.
100   //  Creates the geometry
101   //  Creates the list of Detectors.
102   //  Creates the list of particles.
103   //
104
105   gAlice     = this;
106
107   // Set random number generator
108   gRandom = fRandom;
109
110   if (gSystem->Getenv("CONFIG_SEED")) {
111      gRandom->SetSeed(static_cast<UInt_t>(atoi(gSystem->Getenv("CONFIG_SEED"))));
112   }
113
114   // Add to list of browsable  
115   gROOT->GetListOfBrowsables()->Add(this,name);
116   
117 }
118
119 //_______________________________________________________________________
120 AliRun::~AliRun()
121 {
122   //
123   // Default AliRun destructor
124   //
125   gROOT->GetListOfBrowsables()->Remove(this);
126
127   if (fRunLoader)
128    {
129     TFolder* evfold = fRunLoader->GetEventFolder();
130     TFolder* modfold = dynamic_cast<TFolder*>(evfold->FindObjectAny(AliConfig::GetModulesFolderName()));
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
365     gRandom = fRandom;
366   } else {
367     AliRun::Class()->WriteBuffer(R__b, this);
368   }
369 }
370
371 //_______________________________________________________________________
372 void AliRun::SetGenEventHeader(AliGenEventHeader* header)
373 {
374   AliRunLoader::Instance()->GetHeader()->SetGenEventHeader(header);
375 }
376
377
378 //_______________________________________________________________________
379 Int_t AliRun::GetEvNumber() const
380
381 //Returns number of current event  
382   if (fRunLoader == 0x0)
383    {
384      AliError("RunLoader is not set. Can not load data.");
385      return -1;
386    }
387
388   return fRunLoader->GetEventNumber();
389 }
390
391 //_______________________________________________________________________
392 void AliRun::SetRunLoader(AliRunLoader* rloader)
393 {
394   //
395   // Set the loader of the run
396   //
397   fRunLoader = rloader;
398   if (fRunLoader == 0x0) return;
399   
400   TString evfoldname;
401   TFolder* evfold = fRunLoader->GetEventFolder();
402   if (evfold) evfoldname = evfold->GetName();
403   else AliWarning("Did not get Event Folder from Run Loader");
404   
405   if ( fRunLoader->GetAliRun() )
406    {//if alrun already exists in folder
407     if (fRunLoader->GetAliRun() != this )
408      {//and is different than this - crash
409        AliFatal("AliRun is already in Folder and it is not this object");
410        return;//pro forma
411      }//else do nothing
412    }
413   else
414    {
415      evfold->Add(this);//Post this AliRun to Folder
416    }
417   
418   TIter next(fModules);
419   AliModule *module;
420   while((module = (AliModule*)next())) 
421    {
422      if (evfold) AliConfig::Instance()->Add(module,evfoldname);
423      module->SetRunLoader(fRunLoader);
424      AliDetector* detector = dynamic_cast<AliDetector*>(module);
425      if (detector)
426       {
427         AliLoader* loader = fRunLoader->GetLoader(detector);
428         if (loader == 0x0)
429          {
430            AliError(Form("Can not get loader for detector %s", detector->GetName()));
431          }
432         else
433          {
434            AliDebug(1, Form("Setting loader for detector %s", detector->GetName()));
435            detector->SetLoader(loader);
436          }
437       }
438    }
439 }
440
441 //_______________________________________________________________________
442 void AliRun::AddModule(AliModule* mod)
443 {
444   //
445   // Add a module to the module list
446   //
447   if (mod == 0x0) return;
448   if (strlen(mod->GetName()) == 0) return;
449   if (GetModuleID(mod->GetName()) >= 0) return;
450   
451   AliDebug(1, mod->GetName());
452   if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
453   else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
454
455   Modules()->Add(mod);
456   
457   fNdets++;
458 }
459