Bug fix. Removed delete statement
[u/mrichter/AliRoot.git] / STEER / AliRun.cxx
CommitLineData
99d554c8 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
acd84897 16/* $Id$ */
99d554c8 17
fe4da5cc 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// //
8494b010 25// -Supports the list of all Alice Detectors (fModules). //
fe4da5cc 26// -Supports the list of particles (fParticles). //
27// -Supports the Trees. //
28// -Supports the geometry. //
29// -Supports the event display. //
30//Begin_Html
31/*
1439f98e 32<img src="picts/AliRunClass.gif">
fe4da5cc 33*/
34//End_Html
35//Begin_Html
36/*
1439f98e 37<img src="picts/alirun.gif">
fe4da5cc 38*/
39//End_Html
40// //
41///////////////////////////////////////////////////////////////////////////////
42
88cb7938 43#include <TCint.h>
88cb7938 44#include <TROOT.h>
88cb7938 45#include <TRandom3.h>
46#include <TSystem.h>
88cb7938 47#include <TVirtualMC.h>
178a256c 48#include <TGeoManager.h>
5d12ce38 49//
21bf7095 50#include "AliLog.h"
98490ea9 51#include "AliDetector.h"
98490ea9 52#include "AliHeader.h"
5d12ce38 53#include "AliMC.h"
141f90e3 54#include "AliPDG.h"
98490ea9 55#include "AliRun.h"
56#include "AliStack.h"
b3276c04 57#include "AliCDBManager.h"
995ad051 58#include "AliAlignObj.h"
3e2e3ece 59#include "AliSimulation.h"
60#include "AliLego.h"
fe4da5cc 61
fe4da5cc 62AliRun *gAlice;
63
fe4da5cc 64ClassImp(AliRun)
65
e2afb3b6 66//_______________________________________________________________________
67AliRun::AliRun():
33c3c91a 68// fRun(-1),
3e2e3ece 69 fEventNrInRun(-1),
e2afb3b6 70 fModules(0),
5d12ce38 71 fMCApp(0),
e2afb3b6 72 fNdets(0),
4539539b 73 fConfigFunction(""),
e2afb3b6 74 fRandom(0),
fe12e09c 75 fBaseFileName(""),
4a9de4af 76 fIsRootGeometry(kFALSE),
ced249e6 77 fGeometryFromCDB(kFALSE),
4539539b 78 fGeometryFileName(""),
79 fTriggerDescriptor(""),
5d12ce38 80 fRunLoader(0x0)
fe4da5cc 81{
82 //
83 // Default constructor for AliRun
84 //
88cb7938 85 AliConfig::Instance();//skowron 29 Feb 2002
86 //ensures that the folder structure is build
3d45e923 87
e2afb3b6 88}
89
fe4da5cc 90//_____________________________________________________________________________
e2afb3b6 91AliRun::AliRun(const char *name, const char *title):
5d12ce38 92 TNamed(name,title),
3e2e3ece 93 fEventNrInRun(-1),
e2afb3b6 94 fModules(new TObjArray(77)), // Support list for the Detectors
3e2e3ece 95 fMCApp(new AliMC(GetName(),GetTitle())),
e2afb3b6 96 fNdets(0),
e2afb3b6 97 fConfigFunction("Config();"),
98 fRandom(new TRandom3()),
fe12e09c 99 fBaseFileName(""),
4a9de4af 100 fIsRootGeometry(kFALSE),
ced249e6 101 fGeometryFromCDB(kFALSE),
4539539b 102 fGeometryFileName(""),
103 fTriggerDescriptor(""),
b60e0f5e 104 fRunLoader(0x0)
fe4da5cc 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 //
e2afb3b6 112
fe4da5cc 113 gAlice = this;
65fb704d 114
115 // Set random number generator
e2afb3b6 116 gRandom = fRandom;
2ab0c725 117
118 if (gSystem->Getenv("CONFIG_SEED")) {
e2afb3b6 119 gRandom->SetSeed(static_cast<UInt_t>(atoi(gSystem->Getenv("CONFIG_SEED"))));
2ab0c725 120 }
e2afb3b6 121
122 // Add to list of browsable
fe4da5cc 123 gROOT->GetListOfBrowsables()->Add(this,name);
fe4da5cc 124
fe4da5cc 125}
126
aee8290b 127
e2afb3b6 128//_______________________________________________________________________
fe4da5cc 129AliRun::~AliRun()
130{
131 //
2ab0c725 132 // Default AliRun destructor
fe4da5cc 133 //
88cb7938 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 }
3e2e3ece 147
5d12ce38 148 delete fMCApp;
f5bc1485 149 delete gMC; gMC=0;
8494b010 150 if (fModules) {
151 fModules->Delete();
152 delete fModules;
fe4da5cc 153 }
88cb7938 154
fe4da5cc 155}
156
d8408e76 157
e2afb3b6 158//_______________________________________________________________________
4a9de4af 159void AliRun::SetRootGeometry(Bool_t flag)
160{
161// Instruct application that the geometry is to be retreived from a root file.
162 fIsRootGeometry = flag;
e8572077 163 if (flag && gMC) gMC->SetRootGeometry();
4a9de4af 164}
ced249e6 165
166//_______________________________________________________________________
167void 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
3d45e923 171 if(AliCDBManager::Instance()->IsDefaultStorageSet() &&
172 AliCDBManager::Instance()->GetRun() >= 0){
ced249e6 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
88cb7938 182//_____________________________________________________________________________
183
184void AliRun::InitLoaders()
185{
186 //creates list of getters
21bf7095 187 AliDebug(1, "");
88cb7938 188 TIter next(fModules);
189 AliModule *mod;
190 while((mod = (AliModule*)next()))
191 {
1d6fbf6e 192 mod->SetRunLoader(fRunLoader);
88cb7938 193 AliDetector *det = dynamic_cast<AliDetector*>(mod);
194 if (det)
195 {
21bf7095 196 AliDebug(2, Form("Adding %s", det->GetName()));
88cb7938 197 fRunLoader->AddLoader(det);
198 }
199 }
21bf7095 200 AliDebug(1, "Done");
88cb7938 201}
fe4da5cc 202
e2afb3b6 203//_______________________________________________________________________
eb1b8d29 204void AliRun::Announce() const
205{
206 //
37d06d5b 207 // Announce the current version of AliRoot
208 //
209 printf("%70s",
210 "****************************************************************\n");
211 printf("%6s","*");printf("%64s","*\n");
212
213 printf("%6s","*");
88cb7938 214 printf(" You are running AliRoot version NewIO\n");
37d06d5b 215
216 printf("%6s","*");
5960f18d 217 printf(" The SVN version for the current program is $Id$\n");
37d06d5b 218
219 printf("%6s","*");printf("%64s","*\n");
220 printf("%70s",
221 "****************************************************************\n");
eb1b8d29 222}
223
224//_______________________________________________________________________
94de3818 225AliModule *AliRun::GetModule(const char *name) const
fe4da5cc 226{
227 //
228 // Return pointer to detector from name
229 //
e2afb3b6 230 return dynamic_cast<AliModule*>(fModules->FindObject(name));
fe4da5cc 231}
232
e2afb3b6 233//_______________________________________________________________________
94de3818 234AliDetector *AliRun::GetDetector(const char *name) const
a68348e9 235{
236 //
237 // Return pointer to detector from name
238 //
e2afb3b6 239 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
a68348e9 240}
241
e2afb3b6 242//_______________________________________________________________________
94de3818 243Int_t AliRun::GetModuleID(const char *name) const
fe4da5cc 244{
245 //
246 // Return galice internal detector identifier from name
247 //
23370b7a 248 Int_t i=-1;
249 TObject *mod=fModules->FindObject(name);
250 if(mod) i=fModules->IndexOf(mod);
251 return i;
fe4da5cc 252}
253
e2afb3b6 254//_______________________________________________________________________
fe4da5cc 255Int_t AliRun::GetEvent(Int_t event)
256{
88cb7938 257//
258// Reloads data containers in folders # event
259// Set branch addresses
260//
261 if (fRunLoader == 0x0)
262 {
21bf7095 263 AliError("RunLoader is not set. Can not load data.");
88cb7938 264 return -1;
265 }
266/*****************************************/
267/**** P R E R E L O A D I N G ****/
268/*****************************************/
269// Reset existing structures
5d12ce38 270 fMCApp->ResetHits();
271 fMCApp->ResetTrackReferences();
3e2e3ece 272 fMCApp->ResetDigits();
273 fMCApp->ResetSDigits();
50a6540a 274
88cb7938 275/*****************************************/
276/**** R E L O A D ****/
277/*****************************************/
aab9c8d5 278
33c3c91a 279 AliRunLoader::Instance()->GetEvent(event);
b9d0a01d 280
88cb7938 281/*****************************************/
282/**** P O S T R E L O A D I N G ****/
283/*****************************************/
82711e7a 284
fe4da5cc 285 // Set Trees branch addresses
8494b010 286 TIter next(fModules);
07d55def 287 AliDetector *detector;
288 while((detector = dynamic_cast<AliDetector*>(next())))
88cb7938 289 {
290 detector->SetTreeAddress();
291 }
292
33c3c91a 293 return AliRunLoader::Instance()->GetHeader()->GetNtrack();
fe4da5cc 294}
295
e2afb3b6 296//_______________________________________________________________________
e2afb3b6 297void AliRun::SetBaseFile(const char *filename)
2ab0c725 298{
39de14fb 299 fBaseFileName = filename;
2ab0c725 300}
301
b9d0a01d 302
e2afb3b6 303//_______________________________________________________________________
2ab0c725 304void AliRun::Hits2Digits(const char *selected)
305{
9e1a0ddb 306
d47c658f 307 // Convert Hits to sumable digits
308 //
33c3c91a 309 for (Int_t nevent=0; nevent<AliRunLoader::Instance()->TreeE()->GetEntries(); nevent++) {
9e1a0ddb 310 GetEvent(nevent);
9e1a0ddb 311 Hits2SDigits(selected);
312 SDigits2Digits(selected);
313 }
2ab0c725 314}
315
d47c658f 316
e2afb3b6 317//_______________________________________________________________________
2ab0c725 318
d47c658f 319void AliRun::Tree2Tree(Option_t *option, const char *selected)
2ab0c725 320{
321 //
d47c658f 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).
2ab0c725 331
2ab0c725 332
5cf7bbad 333 const char *oS = strstr(option,"S");
334 const char *oD = strstr(option,"D");
335 const char *oR = strstr(option,"R");
2ab0c725 336
9e1a0ddb 337 TObjArray *detectors = Detectors();
2ab0c725 338
339 TIter next(detectors);
340
d47c658f 341 AliDetector *detector = 0;
2ab0c725 342
88cb7938 343 while((detector = dynamic_cast<AliDetector*>(next()))) {
d47c658f 344 if (selected)
2ab0c725 345 if (strcmp(detector->GetName(),selected)) continue;
88cb7938 346 if (detector->IsActive())
347 {
348
349 AliLoader* loader = detector->GetLoader();
350 if (loader == 0x0) continue;
351
352 if (oS)
353 {
21bf7095 354 AliDebug(1, Form("Processing Hits2SDigits for %s ...", detector->GetName()));
88cb7938 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 {
21bf7095 365 AliDebug(1, Form("Processing SDigits2Digits for %s ...", detector->GetName()));
88cb7938 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 {
21bf7095 376 AliDebug(1, Form("Processing Digits2Reco for %s ...", detector->GetName()));
88cb7938 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 }
2ab0c725 388}
389
b9d0a01d 390
e2afb3b6 391//_______________________________________________________________________
fe4da5cc 392void AliRun::Streamer(TBuffer &R__b)
393{
eef4b160 394 // Stream an object of class AliRun.
2ab0c725 395
7e90ff59 396 if (R__b.IsReading()) {
397 if (!gAlice) gAlice = this;
7e90ff59 398 AliRun::Class()->ReadBuffer(R__b, this);
7e90ff59 399 gROOT->GetListOfBrowsables()->Add(this,"Run");
eef4b160 400
7e90ff59 401 gRandom = fRandom;
402 } else {
eef4b160 403 AliRun::Class()->WriteBuffer(R__b, this);
404 }
2ab0c725 405}
e2afb3b6 406//_______________________________________________________________________
b9d0a01d 407
27f087a9 408void AliRun::SetGenEventHeader(AliGenEventHeader* header)
409{
33c3c91a 410 AliRunLoader::Instance()->GetHeader()->SetGenEventHeader(header);
27f087a9 411}
7dd2cbe4 412
413
504b172d 414//_______________________________________________________________________
0592d1ca 415
88cb7938 416Int_t AliRun::GetEvNumber() const
417{
418//Returns number of current event
419 if (fRunLoader == 0x0)
420 {
21bf7095 421 AliError("RunLoader is not set. Can not load data.");
88cb7938 422 return -1;
423 }
0592d1ca 424
88cb7938 425 return fRunLoader->GetEventNumber();
0592d1ca 426}
504b172d 427//_______________________________________________________________________
b9d0a01d 428
88cb7938 429void AliRun::SetRunLoader(AliRunLoader* rloader)
0592d1ca 430{
2057aecb 431 //
432 // Set the loader of the run
433 //
88cb7938 434 fRunLoader = rloader;
435 if (fRunLoader == 0x0) return;
436
437 TString evfoldname;
438 TFolder* evfold = fRunLoader->GetEventFolder();
439 if (evfold) evfoldname = evfold->GetName();
21bf7095 440 else AliWarning("Did not get Event Folder from Run Loader");
88cb7938 441
442 if ( fRunLoader->GetAliRun() )
443 {//if alrun already exists in folder
444 if (fRunLoader->GetAliRun() != this )
445 {//and is different than this - crash
21bf7095 446 AliFatal("AliRun is already in Folder and it is not this object");
88cb7938 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);
1d6fbf6e 460 module->SetRunLoader(fRunLoader);
88cb7938 461 AliDetector* detector = dynamic_cast<AliDetector*>(module);
462 if (detector)
463 {
464 AliLoader* loader = fRunLoader->GetLoader(detector);
465 if (loader == 0x0)
466 {
21bf7095 467 AliError(Form("Can not get loader for detector %s", detector->GetName()));
88cb7938 468 }
469 else
470 {
21bf7095 471 AliDebug(1, Form("Setting loader for detector %s", detector->GetName()));
88cb7938 472 detector->SetLoader(loader);
473 }
0592d1ca 474 }
88cb7938 475 }
0592d1ca 476}
477
88cb7938 478void AliRun::AddModule(AliModule* mod)
7a16e9cc 479{
2057aecb 480 //
481 // Add a module to the module list
482 //
88cb7938 483 if (mod == 0x0) return;
484 if (strlen(mod->GetName()) == 0) return;
485 if (GetModuleID(mod->GetName()) >= 0) return;
486
21bf7095 487 AliDebug(1, mod->GetName());
88cb7938 488 if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
489 else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
7a16e9cc 490
88cb7938 491 Modules()->Add(mod);
67d736ee 492
493 fNdets++;
7a16e9cc 494}
21bf7095 495