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