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