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