Modifications needed by Geant4 (I.Hrivnacova)
[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 <TBRIK.h>
88cb7938 44#include <TCint.h>
5d12ce38 45#include <TDatabasePDG.h>
88cb7938 46#include <TGeometry.h>
88cb7938 47#include <TNode.h>
88cb7938 48#include <TROOT.h>
88cb7938 49#include <TRandom3.h>
50#include <TSystem.h>
88cb7938 51#include <TVirtualMC.h>
5d12ce38 52//
98490ea9 53#include "AliDetector.h"
fe4da5cc 54#include "AliDisplay.h"
98490ea9 55#include "AliHeader.h"
dffd31ef 56#include "AliLego.h"
98490ea9 57#include "AliLegoGenerator.h"
5d12ce38 58#include "AliMC.h"
aee8290b 59#include "AliMagFC.h"
60#include "AliMagFCM.h"
61#include "AliMagFDM.h"
141f90e3 62#include "AliPDG.h"
98490ea9 63#include "AliRun.h"
64#include "AliStack.h"
fe4da5cc 65
fe4da5cc 66AliRun *gAlice;
67
5d12ce38 68
fe4da5cc 69ClassImp(AliRun)
70
e2afb3b6 71//_______________________________________________________________________
72AliRun::AliRun():
73 fRun(0),
74 fEvent(0),
75 fEventNrInRun(0),
76 fEventsPerRun(0),
77 fDebug(0),
e2afb3b6 78 fModules(0),
79 fGeometry(0),
5d12ce38 80 fMCApp(0),
e2afb3b6 81 fDisplay(0),
e2afb3b6 82 fField(0),
83 fMC(0),
e2afb3b6 84 fNdets(0),
e2afb3b6 85 fInitDone(kFALSE),
86 fLego(0),
87 fPDGDB(0), //Particle factory object
e2afb3b6 88 fConfigFunction("\0"),
89 fRandom(0),
5d12ce38 90 fRunLoader(0x0)
fe4da5cc 91{
92 //
93 // Default constructor for AliRun
94 //
88cb7938 95 AliConfig::Instance();//skowron 29 Feb 2002
96 //ensures that the folder structure is build
e2afb3b6 97}
98
99//_______________________________________________________________________
100AliRun::AliRun(const AliRun& arun):
5d12ce38 101 TNamed(arun),
e2afb3b6 102 fRun(0),
103 fEvent(0),
104 fEventNrInRun(0),
105 fEventsPerRun(0),
106 fDebug(0),
e2afb3b6 107 fModules(0),
108 fGeometry(0),
5d12ce38 109 fMCApp(0),
e2afb3b6 110 fDisplay(0),
e2afb3b6 111 fField(0),
112 fMC(0),
e2afb3b6 113 fNdets(0),
e2afb3b6 114 fInitDone(kFALSE),
115 fLego(0),
116 fPDGDB(0), //Particle factory object
e2afb3b6 117 fConfigFunction("\0"),
118 fRandom(0),
88cb7938 119 fRunLoader(0x0)
e2afb3b6 120{
121 //
122 // Copy constructor for AliRun
123 //
124 arun.Copy(*this);
fe4da5cc 125}
126
127//_____________________________________________________________________________
e2afb3b6 128AliRun::AliRun(const char *name, const char *title):
5d12ce38 129 TNamed(name,title),
e2afb3b6 130 fRun(0),
131 fEvent(0),
132 fEventNrInRun(0),
133 fEventsPerRun(0),
134 fDebug(0),
e2afb3b6 135 fModules(new TObjArray(77)), // Support list for the Detectors
136 fGeometry(0),
5d12ce38 137 fMCApp(0),
e2afb3b6 138 fDisplay(0),
e2afb3b6 139 fField(0),
140 fMC(gMC),
e2afb3b6 141 fNdets(0),
e2afb3b6 142 fInitDone(kFALSE),
143 fLego(0),
144 fPDGDB(TDatabasePDG::Instance()), //Particle factory object!
e2afb3b6 145 fConfigFunction("Config();"),
146 fRandom(new TRandom3()),
88cb7938 147 fRunLoader(0x0)
fe4da5cc 148{
149 //
150 // Constructor for the main processor.
151 // Creates the geometry
152 // Creates the list of Detectors.
153 // Creates the list of particles.
154 //
e2afb3b6 155
fe4da5cc 156 gAlice = this;
65fb704d 157
158 // Set random number generator
e2afb3b6 159 gRandom = fRandom;
2ab0c725 160
161 if (gSystem->Getenv("CONFIG_SEED")) {
e2afb3b6 162 gRandom->SetSeed(static_cast<UInt_t>(atoi(gSystem->Getenv("CONFIG_SEED"))));
2ab0c725 163 }
e2afb3b6 164
165 // Add to list of browsable
fe4da5cc 166 gROOT->GetListOfBrowsables()->Add(this,name);
fe4da5cc 167 // Create the TNode geometry for the event display
fe4da5cc 168 BuildSimpleGeometry();
169
fe4da5cc 170 // Create default mag field
171 SetField();
e2afb3b6 172
e2afb3b6 173 // Add particle list to configuration
9e1a0ddb 174 AliConfig::Instance()->Add(fPDGDB);
e2afb3b6 175
fe4da5cc 176}
177
aee8290b 178
e2afb3b6 179//_______________________________________________________________________
fe4da5cc 180AliRun::~AliRun()
181{
182 //
2ab0c725 183 // Default AliRun destructor
fe4da5cc 184 //
88cb7938 185 gROOT->GetListOfBrowsables()->Remove(this);
186
187 if (fRunLoader)
188 {
189 TFolder* evfold = fRunLoader->GetEventFolder();
190 TFolder* modfold = dynamic_cast<TFolder*>(evfold->FindObjectAny(AliConfig::GetModulesFolderName()));
191 TIter next(fModules);
192 AliModule *mod;
193 while((mod = (AliModule*)next()))
194 {
195 modfold->Remove(mod);
196 }
197 }
5d12ce38 198
199
fe4da5cc 200 delete fField;
5d12ce38 201 delete fMCApp;
f5bc1485 202 delete gMC; gMC=0;
fe4da5cc 203 delete fGeometry;
204 delete fDisplay;
fe4da5cc 205 delete fLego;
8494b010 206 if (fModules) {
207 fModules->Delete();
208 delete fModules;
fe4da5cc 209 }
88cb7938 210
c222d2b0 211 delete fPDGDB;
fe4da5cc 212}
213
e2afb3b6 214//_______________________________________________________________________
6c4904c2 215void AliRun::Copy(TObject &) const
e2afb3b6 216{
217 Fatal("Copy","Not implemented!\n");
218}
219
e2afb3b6 220//_______________________________________________________________________
fe4da5cc 221void AliRun::Build()
222{
223 //
224 // Initialize Alice geometry
225 // Dummy routine
226 //
227}
228
e2afb3b6 229//_______________________________________________________________________
fe4da5cc 230void AliRun::BuildSimpleGeometry()
231{
232 //
233 // Create a simple TNode geometry used by Root display engine
234 //
235 // Initialise geometry
236 //
237 fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
238 new TMaterial("void","Vacuum",0,0,0); //Everything is void
239 TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
240 brik->SetVisibility(0);
241 new TNode("alice","alice","S_alice");
242}
243
e2afb3b6 244//_______________________________________________________________________
fe4da5cc 245void AliRun::CleanDetectors()
246{
247 //
248 // Clean Detectors at the end of event
249 //
88cb7938 250 fRunLoader->CleanDetectors();
fe4da5cc 251}
252
e2afb3b6 253//_______________________________________________________________________
5d12ce38 254void AliRun::ResetHits()
fe4da5cc 255{
5d12ce38 256 fMCApp->ResetHits();
fe4da5cc 257}
258
e2afb3b6 259//_______________________________________________________________________
5d12ce38 260AliGenerator* AliRun::Generator() const
fe4da5cc 261{
5d12ce38 262 return fMCApp->Generator();
fe4da5cc 263}
264
e2afb3b6 265//_______________________________________________________________________
d8408e76 266void AliRun::SetField(AliMagF* magField)
267{
2057aecb 268 //
269 // Set Magnetic Field Map
270 //
271 fField = magField;
272 fField->ReadField();
d8408e76 273}
274
e2afb3b6 275//_______________________________________________________________________
fe4da5cc 276void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
07c4aae4 277 Float_t maxField, const char* filename)
fe4da5cc 278{
279 //
280 // Set magnetic field parameters
281 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
282 // version Magnetic field map version (only 1 active now)
283 // scale Scale factor for the magnetic field
284 // maxField Maximum value for the magnetic field
285
286 //
287 // --- Sanity check on mag field flags
fe4da5cc 288 if(fField) delete fField;
289 if(version==1) {
d8408e76 290 fField = new AliMagFC("Map1"," ",type,scale,maxField);
f1b9d7c3 291 } else if(version<=2) {
d8408e76 292 fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
fe4da5cc 293 fField->ReadField();
f1b9d7c3 294 } else if(version==3) {
d8408e76 295 fField = new AliMagFDM("Map4",filename,type,scale,maxField);
f1b9d7c3 296 fField->ReadField();
fe4da5cc 297 } else {
23370b7a 298 Warning("SetField","Invalid map %d\n",version);
fe4da5cc 299 }
300}
88cb7938 301
302//_____________________________________________________________________________
303
304void AliRun::InitLoaders()
305{
306 //creates list of getters
307 if (GetDebug()) Info("InitLoaders","");
308 TIter next(fModules);
309 AliModule *mod;
310 while((mod = (AliModule*)next()))
311 {
1d6fbf6e 312 mod->SetRunLoader(fRunLoader);
88cb7938 313 AliDetector *det = dynamic_cast<AliDetector*>(mod);
314 if (det)
315 {
316 if (GetDebug()) Info("InitLoaders"," Adding %s ",det->GetName());
317 fRunLoader->AddLoader(det);
318 }
319 }
320 if (GetDebug()) Info("InitLoaders","Done");
321}
322//_____________________________________________________________________________
323
fe4da5cc 324void AliRun::FinishRun()
325{
326 //
327 // Called at the end of the run.
328 //
88cb7938 329
88cb7938 330 if(fLego)
331 {
332 if (GetDebug()) Info("FinishRun"," Finish Lego");
333 fRunLoader->CdGAFile();
334 fLego->FinishRun();
335 }
8e70f139 336
fe4da5cc 337 // Clean detector information
8494b010 338 TIter next(fModules);
339 AliModule *detector;
e2afb3b6 340 while((detector = dynamic_cast<AliModule*>(next()))) {
88cb7938 341 if (GetDebug()) Info("FinishRun"," %s->FinishRun()",detector->GetName());
fe4da5cc 342 detector->FinishRun();
343 }
344
88cb7938 345 if (GetDebug()) Info("FinishRun"," fRunLoader->WriteHeader(OVERWRITE)");
346 fRunLoader->WriteHeader("OVERWRITE");
682a4a95 347
88cb7938 348 // Write AliRun info and all detectors parameters
349 fRunLoader->CdGAFile();
350 Write(0,TObject::kOverwrite);//write AliRun
351 fRunLoader->Write(0,TObject::kOverwrite);//write RunLoader itself
352
fe4da5cc 353 // Clean tree information
88cb7938 354 if (GetDebug()) Info("FinishRun"," fRunLoader->Stack()->FinishRun()");
355 fRunLoader->Stack()->FinishRun();
9e1a0ddb 356
5d12ce38 357 if(fMCApp) fMCApp->FinishRun();
fe4da5cc 358
5d12ce38 359 fRunLoader->Synchronize();
fe4da5cc 360}
361
eb1b8d29 362//_______________________________________________________________________
363void AliRun::Announce() const
364{
365 //
37d06d5b 366 // Announce the current version of AliRoot
367 //
368 printf("%70s",
369 "****************************************************************\n");
370 printf("%6s","*");printf("%64s","*\n");
371
372 printf("%6s","*");
88cb7938 373 printf(" You are running AliRoot version NewIO\n");
37d06d5b 374
375 printf("%6s","*");
376 printf(" The cvs tag for the current program is $Name$\n");
377
378 printf("%6s","*");printf("%64s","*\n");
379 printf("%70s",
380 "****************************************************************\n");
eb1b8d29 381}
382
e2afb3b6 383//_______________________________________________________________________
94de3818 384AliModule *AliRun::GetModule(const char *name) const
fe4da5cc 385{
386 //
387 // Return pointer to detector from name
388 //
e2afb3b6 389 return dynamic_cast<AliModule*>(fModules->FindObject(name));
fe4da5cc 390}
391
e2afb3b6 392//_______________________________________________________________________
94de3818 393AliDetector *AliRun::GetDetector(const char *name) const
a68348e9 394{
395 //
396 // Return pointer to detector from name
397 //
e2afb3b6 398 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
a68348e9 399}
400
e2afb3b6 401//_______________________________________________________________________
94de3818 402Int_t AliRun::GetModuleID(const char *name) const
fe4da5cc 403{
404 //
405 // Return galice internal detector identifier from name
406 //
23370b7a 407 Int_t i=-1;
408 TObject *mod=fModules->FindObject(name);
409 if(mod) i=fModules->IndexOf(mod);
410 return i;
fe4da5cc 411}
412
e2afb3b6 413//_______________________________________________________________________
fe4da5cc 414Int_t AliRun::GetEvent(Int_t event)
415{
88cb7938 416//
417// Reloads data containers in folders # event
418// Set branch addresses
419//
420 if (fRunLoader == 0x0)
421 {
422 Error("GetEvent","RunLoader is not set. Can not load data.");
423 return -1;
424 }
425/*****************************************/
426/**** P R E R E L O A D I N G ****/
427/*****************************************/
428// Reset existing structures
5d12ce38 429 fMCApp->ResetHits();
430 fMCApp->ResetTrackReferences();
fe4da5cc 431 ResetDigits();
2ab0c725 432 ResetSDigits();
50a6540a 433
88cb7938 434/*****************************************/
435/**** R E L O A D ****/
436/*****************************************/
aab9c8d5 437
88cb7938 438 fRunLoader->GetEvent(event);
b9d0a01d 439
88cb7938 440/*****************************************/
441/**** P O S T R E L O A D I N G ****/
442/*****************************************/
82711e7a 443
fe4da5cc 444 // Set Trees branch addresses
8494b010 445 TIter next(fModules);
446 AliModule *detector;
88cb7938 447 while((detector = dynamic_cast<AliModule*>(next())))
448 {
449 detector->SetTreeAddress();
450 }
451
452 return fRunLoader->GetHeader()->GetNtrack();
fe4da5cc 453}
454
e2afb3b6 455//_______________________________________________________________________
fe4da5cc 456TGeometry *AliRun::GetGeometry()
457{
458 //
459 // Import Alice geometry from current file
460 // Return pointer to geometry object
461 //
e2afb3b6 462 if (!fGeometry) fGeometry = dynamic_cast<TGeometry*>(gDirectory->Get("AliceGeom"));
fe4da5cc 463 //
464 // Unlink and relink nodes in detectors
465 // This is bad and there must be a better way...
466 //
fe4da5cc 467
8494b010 468 TIter next(fModules);
469 AliModule *detector;
e2afb3b6 470 while((detector = dynamic_cast<AliModule*>(next()))) {
fe4da5cc 471 TList *dnodes=detector->Nodes();
472 Int_t j;
473 TNode *node, *node1;
474 for ( j=0; j<dnodes->GetSize(); j++) {
e2afb3b6 475 node = dynamic_cast<TNode*>(dnodes->At(j));
52d0ab00 476 node1 = fGeometry->GetNode(node->GetName());
fe4da5cc 477 dnodes->Remove(node);
478 dnodes->AddAt(node1,j);
479 }
480 }
481 return fGeometry;
482}
483
e2afb3b6 484//_______________________________________________________________________
485void AliRun::SetBaseFile(const char *filename)
2ab0c725 486{
39de14fb 487 fBaseFileName = filename;
2ab0c725 488}
489
e2afb3b6 490//_______________________________________________________________________
b9d0a01d 491void AliRun::ResetDigits()
fe4da5cc 492{
493 //
b9d0a01d 494 // Reset all Detectors digits
fe4da5cc 495 //
b9d0a01d 496 TIter next(fModules);
497 AliModule *detector;
e2afb3b6 498 while((detector = dynamic_cast<AliModule*>(next()))) {
b9d0a01d 499 detector->ResetDigits();
500 }
501}
502
e2afb3b6 503//_______________________________________________________________________
b9d0a01d 504void AliRun::ResetSDigits()
505{
dffd31ef 506 //
b9d0a01d 507 // Reset all Detectors digits
9e1a0ddb 508 //
b9d0a01d 509 TIter next(fModules);
510 AliModule *detector;
e2afb3b6 511 while((detector = dynamic_cast<AliModule*>(next()))) {
b9d0a01d 512 detector->ResetSDigits();
513 }
514}
dffd31ef 515
2b22f272 516
e2afb3b6 517//_______________________________________________________________________
88cb7938 518
b9d0a01d 519void AliRun::ResetPoints()
fe4da5cc 520{
521 //
b9d0a01d 522 // Reset all Detectors points
fe4da5cc 523 //
8494b010 524 TIter next(fModules);
525 AliModule *detector;
e2afb3b6 526 while((detector = dynamic_cast<AliModule*>(next()))) {
b9d0a01d 527 detector->ResetPoints();
fe4da5cc 528 }
529}
e2afb3b6 530//_______________________________________________________________________
88cb7938 531
b9d0a01d 532void AliRun::InitMC(const char *setup)
2ab0c725 533{
534 //
5d12ce38 535 // Initialize ALICE Simulation run
2ab0c725 536 //
37d06d5b 537 Announce();
538
b9d0a01d 539 if(fInitDone) {
540 Warning("Init","Cannot initialise AliRun twice!\n");
541 return;
fe4da5cc 542 }
b9d0a01d 543
67d736ee 544 if (!fMCApp)
545 fMCApp=new AliMC(GetName(),GetTitle());
5d12ce38 546
b9d0a01d 547 gROOT->LoadMacro(setup);
548 gInterpreter->ProcessLine(fConfigFunction.Data());
fe4da5cc 549
88cb7938 550 fRunLoader->CdGAFile();
aab9c8d5 551
141f90e3 552 AliPDG::AddParticlesToPdgDataBase();
aab9c8d5 553
b9d0a01d 554 fNdets = fModules->GetLast()+1;
aab9c8d5 555
88cb7938 556 TIter next(fModules);
5d12ce38 557 for(Int_t i=0; i<fNdets; ++i)
88cb7938 558 {
5d12ce38 559 TObject *objfirst, *objlast;
560 AliModule *detector=dynamic_cast<AliModule*>(fModules->At(i));
88cb7938 561 objlast = gDirectory->GetList()->Last();
b9d0a01d 562
88cb7938 563 // Add Detector histograms in Detector list of histograms
564 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
565 else objfirst = gDirectory->GetList()->First();
566 while (objfirst)
567 {
b9d0a01d 568 detector->Histograms()->Add(objfirst);
569 objfirst = gDirectory->GetList()->After(objfirst);
570 }
571 }
b9d0a01d 572
5d12ce38 573 fMCApp->Init();
b9d0a01d 574
504b172d 575 //Must be here because some MCs (G4) adds detectors here and not in Config.C
576 InitLoaders();
577 fRunLoader->MakeTree("E");
578 if (fLego == 0x0)
579 {
580 fRunLoader->LoadKinematics("RECREATE");
581 fRunLoader->LoadTrackRefs("RECREATE");
582 fRunLoader->LoadHits("all","RECREATE");
583 }
b9d0a01d 584 fInitDone = kTRUE;
b9d0a01d 585 //
586 // Save stuff at the beginning of the file to avoid file corruption
504b172d 587 fRunLoader->CdGAFile();
b9d0a01d 588 Write();
88cb7938 589 fEventNrInRun = -1; //important - we start Begin event from increasing current number in run
fe4da5cc 590}
591
e2afb3b6 592//_______________________________________________________________________
88cb7938 593
875c717b 594void AliRun::RunMC(Int_t nevent, const char *setup)
fe4da5cc 595{
596 //
597 // Main function to be called to process a galice run
598 // example
599 // Root > gAlice.Run();
600 // a positive number of events will cause the finish routine
601 // to be called
602 //
88cb7938 603 fEventsPerRun = nevent;
fe4da5cc 604 // check if initialisation has been done
875c717b 605 if (!fInitDone) InitMC(setup);
fe4da5cc 606
fe4da5cc 607 // Create the Root Tree with one branch per detector
88cb7938 608 //Hits moved to begin event -> now we are crating separate tree for each event
fe4da5cc 609
875c717b 610 gMC->ProcessRun(nevent);
611
fe4da5cc 612 // End of this run, close files
80762cb1 613 if(nevent>0) FinishRun();
fe4da5cc 614}
615
e2afb3b6 616//_______________________________________________________________________
27f087a9 617void AliRun::RunReco(const char *selected, Int_t first, Int_t last)
d47c658f 618{
619 //
620 // Main function to be called to reconstruct Alice event
27f087a9 621 //
88cb7938 622 Int_t nev = fRunLoader->GetNumberOfEvents();
623 if (GetDebug()) Info("RunReco","Found %d events",nev);
27f087a9 624 Int_t nFirst = first;
88cb7938 625 Int_t nLast = (last < 0)? nev : last;
27f087a9 626
627 for (Int_t nevent = nFirst; nevent <= nLast; nevent++) {
88cb7938 628 if (GetDebug()) Info("RunReco","Processing event %d",nevent);
9e1a0ddb 629 GetEvent(nevent);
9e1a0ddb 630 Digits2Reco(selected);
631 }
d47c658f 632}
633
e2afb3b6 634//_______________________________________________________________________
2ab0c725 635
636void AliRun::Hits2Digits(const char *selected)
637{
9e1a0ddb 638
d47c658f 639 // Convert Hits to sumable digits
640 //
9e1a0ddb 641 for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
642 GetEvent(nevent);
9e1a0ddb 643 Hits2SDigits(selected);
644 SDigits2Digits(selected);
645 }
2ab0c725 646}
647
d47c658f 648
e2afb3b6 649//_______________________________________________________________________
2ab0c725 650
d47c658f 651void AliRun::Tree2Tree(Option_t *option, const char *selected)
2ab0c725 652{
653 //
d47c658f 654 // Function to transform the content of
655 //
656 // - TreeH to TreeS (option "S")
657 // - TreeS to TreeD (option "D")
658 // - TreeD to TreeR (option "R")
659 //
660 // If multiple options are specified ("SDR"), transformation will be done in sequence for
661 // selected detector and for all detectors if none is selected (detector string
662 // can contain blank separated list of detector names).
2ab0c725 663
2ab0c725 664
5cf7bbad 665 const char *oS = strstr(option,"S");
666 const char *oD = strstr(option,"D");
667 const char *oR = strstr(option,"R");
2ab0c725 668
9e1a0ddb 669 TObjArray *detectors = Detectors();
2ab0c725 670
671 TIter next(detectors);
672
d47c658f 673 AliDetector *detector = 0;
2ab0c725 674
88cb7938 675 while((detector = dynamic_cast<AliDetector*>(next()))) {
d47c658f 676 if (selected)
2ab0c725 677 if (strcmp(detector->GetName(),selected)) continue;
88cb7938 678 if (detector->IsActive())
679 {
680
681 AliLoader* loader = detector->GetLoader();
682 if (loader == 0x0) continue;
683
684 if (oS)
685 {
686 if (GetDebug()) Info("Tree2Tree","Processing Hits2SDigits for %s ...",detector->GetName());
687 loader->LoadHits("read");
688 if (loader->TreeS() == 0x0) loader->MakeTree("S");
689 detector->MakeBranch(option);
690 detector->SetTreeAddress();
691 detector->Hits2SDigits();
692 loader->UnloadHits();
693 loader->UnloadSDigits();
694 }
695 if (oD)
696 {
697 if (GetDebug()) Info("Tree2Tree","Processing SDigits2Digits for %s ...",detector->GetName());
698 loader->LoadSDigits("read");
699 if (loader->TreeD() == 0x0) loader->MakeTree("D");
700 detector->MakeBranch(option);
701 detector->SetTreeAddress();
702 detector->SDigits2Digits();
703 loader->UnloadSDigits();
704 loader->UnloadDigits();
705 }
706 if (oR)
707 {
708 if (GetDebug()) Info("Tree2Tree","Processing Digits2Reco for %s ...",detector->GetName());
709 loader->LoadDigits("read");
710 if (loader->TreeR() == 0x0) loader->MakeTree("R");
711 detector->MakeBranch(option);
712 detector->SetTreeAddress();
713 detector->Digits2Reco();
714 loader->UnloadDigits();
715 loader->UnloadRecPoints();
716
717 }
718 }
719 }
2ab0c725 720}
721
e2afb3b6 722//_______________________________________________________________________
0a520a66 723void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
724 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
725 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener)
fe4da5cc 726{
727 //
728 // Generates lego plots of:
729 // - radiation length map phi vs theta
730 // - radiation length map phi vs eta
731 // - interaction length map
732 // - g/cm2 length map
733 //
734 // ntheta bins in theta, eta
735 // themin minimum angle in theta (degrees)
736 // themax maximum angle in theta (degrees)
737 // nphi bins in phi
738 // phimin minimum angle in phi (degrees)
739 // phimax maximum angle in phi (degrees)
740 // rmin minimum radius
741 // rmax maximum radius
742 //
743 //
744 // The number of events generated = ntheta*nphi
745 // run input parameters in macro setup (default="Config.C")
746 //
747 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
748 //Begin_Html
749 /*
1439f98e 750 <img src="picts/AliRunLego1.gif">
fe4da5cc 751 */
752 //End_Html
753 //Begin_Html
754 /*
1439f98e 755 <img src="picts/AliRunLego2.gif">
fe4da5cc 756 */
757 //End_Html
758 //Begin_Html
759 /*
1439f98e 760 <img src="picts/AliRunLego3.gif">
fe4da5cc 761 */
762 //End_Html
763 //
764
765 // check if initialisation has been done
2cacde38 766 // If runloader has been initialized, set the number of events per file to nc1 * nc2
5d12ce38 767
0a520a66 768 // Set new generator
769 if (!gener) gener = new AliLegoGenerator();
0a520a66 770 //
771 // Configure Generator
772 gener->SetRadiusRange(rmin, rmax);
773 gener->SetZMax(zmax);
774 gener->SetCoor1Range(nc1, c1min, c1max);
775 gener->SetCoor2Range(nc2, c2min, c2max);
776
777
b13db077 778 //Create Lego object
0a520a66 779 fLego = new AliLego("lego",gener);
b13db077 780
504b172d 781 if (!fInitDone) InitMC(setup);
782 //Save current generator
783
784 AliGenerator *gen=fMCApp->Generator();
785 fMCApp->ResetGenerator(gener);
dffd31ef 786 //Prepare MC for Lego Run
787 gMC->InitLego();
788
b13db077 789 //Run Lego Object
0a520a66 790
504b172d 791 if (fRunLoader) fRunLoader->SetNumberOfEventsPerFile(nc1 * nc2);
b9d0a01d 792 //gMC->ProcessRun(nc1*nc2+1);
793 gMC->ProcessRun(nc1*nc2);
fe4da5cc 794
fe4da5cc 795 // End of this run, close files
80762cb1 796 FinishRun();
0a520a66 797 // Restore current generator
5d12ce38 798 fMCApp->ResetGenerator(gen);
838edcaf 799 // Delete Lego Object
800 delete fLego; fLego=0;
fe4da5cc 801}
802
e2afb3b6 803//_______________________________________________________________________
94de3818 804void AliRun::SetConfigFunction(const char * config)
805{
806 //
807 // Set the signature of the function contained in Config.C to configure
808 // the run
809 //
810 fConfigFunction=config;
811}
812
b9d0a01d 813//
814// MC Application
815//
816
e2afb3b6 817//_______________________________________________________________________
b9d0a01d 818void AliRun::Field(const Double_t* x, Double_t *b) const
819{
2057aecb 820 //
821 // Return the value of the magnetic field
822 //
823 Float_t xfloat[3];
824 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
825
826 if (Field()) {
827 Float_t bfloat[3];
828 Field()->Field(xfloat,bfloat);
829 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
830 }
831 else {
832 printf("No mag field defined!\n");
833 b[0]=b[1]=b[2]=0.;
834 }
b9d0a01d 835}
836
837//
838// End of MC Application
839//
840
e2afb3b6 841//_______________________________________________________________________
fe4da5cc 842void AliRun::Streamer(TBuffer &R__b)
843{
eef4b160 844 // Stream an object of class AliRun.
2ab0c725 845
7e90ff59 846 if (R__b.IsReading()) {
847 if (!gAlice) gAlice = this;
7e90ff59 848 AliRun::Class()->ReadBuffer(R__b, this);
7e90ff59 849 gROOT->GetListOfBrowsables()->Add(this,"Run");
eef4b160 850
7e90ff59 851 gRandom = fRandom;
852 } else {
eef4b160 853 AliRun::Class()->WriteBuffer(R__b, this);
854 }
2ab0c725 855}
e2afb3b6 856//_______________________________________________________________________
b9d0a01d 857
27f087a9 858void AliRun::SetGenEventHeader(AliGenEventHeader* header)
859{
88cb7938 860 fRunLoader->GetHeader()->SetGenEventHeader(header);
27f087a9 861}
504b172d 862//_______________________________________________________________________
0592d1ca 863
88cb7938 864Int_t AliRun::GetEvNumber() const
865{
866//Returns number of current event
867 if (fRunLoader == 0x0)
868 {
869 Error("GetEvent","RunLoader is not set. Can not load data.");
870 return -1;
871 }
0592d1ca 872
88cb7938 873 return fRunLoader->GetEventNumber();
0592d1ca 874}
504b172d 875//_______________________________________________________________________
b9d0a01d 876
88cb7938 877void AliRun::SetRunLoader(AliRunLoader* rloader)
0592d1ca 878{
2057aecb 879 //
880 // Set the loader of the run
881 //
88cb7938 882 fRunLoader = rloader;
883 if (fRunLoader == 0x0) return;
884
885 TString evfoldname;
886 TFolder* evfold = fRunLoader->GetEventFolder();
887 if (evfold) evfoldname = evfold->GetName();
888 else Warning("SetRunLoader","Did not get Event Folder from Run Loader");
889
890 if ( fRunLoader->GetAliRun() )
891 {//if alrun already exists in folder
892 if (fRunLoader->GetAliRun() != this )
893 {//and is different than this - crash
894 Fatal("AliRun","AliRun is already in Folder and it is not this object");
895 return;//pro forma
896 }//else do nothing
897 }
898 else
899 {
900 evfold->Add(this);//Post this AliRun to Folder
901 }
902
903 TIter next(fModules);
904 AliModule *module;
905 while((module = (AliModule*)next()))
906 {
907 if (evfold) AliConfig::Instance()->Add(module,evfoldname);
1d6fbf6e 908 module->SetRunLoader(fRunLoader);
88cb7938 909 AliDetector* detector = dynamic_cast<AliDetector*>(module);
910 if (detector)
911 {
912 AliLoader* loader = fRunLoader->GetLoader(detector);
913 if (loader == 0x0)
914 {
915 Error("SetRunLoader","Can not get loader for detector %s",detector->GetName());
916 }
917 else
918 {
919 if (GetDebug()) Info("SetRunLoader","Setting loader for detector %s",detector->GetName());
920 detector->SetLoader(loader);
921 }
0592d1ca 922 }
88cb7938 923 }
0592d1ca 924}
925
88cb7938 926void AliRun::AddModule(AliModule* mod)
7a16e9cc 927{
2057aecb 928 //
929 // Add a module to the module list
930 //
88cb7938 931 if (mod == 0x0) return;
932 if (strlen(mod->GetName()) == 0) return;
933 if (GetModuleID(mod->GetName()) >= 0) return;
934
935 if (GetDebug()) Info("AddModule","%s",mod->GetName());
936 if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
937 else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
7a16e9cc 938
88cb7938 939 Modules()->Add(mod);
67d736ee 940
941 fNdets++;
7a16e9cc 942}