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