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