]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliRun.cxx
Adaption to new fluka common blocks (E. Futo)
[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
94de3818 43#include <stdio.h>
98490ea9 44#include <stdlib.h>
94de3818 45#include <string.h>
46
98490ea9 47#include "Riostream.h"
48#include "TBRIK.h"
49#include "TBrowser.h"
50#include "TCint.h"
51#include "TFile.h"
52#include "TFolder.h"
53#include "TGeometry.h"
98490ea9 54#include "TNode.h"
1578254f 55#include "TParticle.h"
98490ea9 56#include "TRandom3.h"
57#include "TROOT.h"
58#include "TSystem.h"
59#include "TTree.h"
60
61#include "AliConfig.h"
62#include "AliDetector.h"
fe4da5cc 63#include "AliDisplay.h"
98490ea9 64#include "AliGenerator.h"
65#include "AliHeader.h"
dffd31ef 66#include "AliLego.h"
98490ea9 67#include "AliLegoGenerator.h"
68#include "AliMCQA.h"
aee8290b 69#include "AliMagFC.h"
70#include "AliMagFCM.h"
71#include "AliMagFDM.h"
141f90e3 72#include "AliPDG.h"
98490ea9 73#include "AliRun.h"
74#include "AliStack.h"
fe4da5cc 75
fe4da5cc 76AliRun *gAlice;
77
fe4da5cc 78ClassImp(AliRun)
79
e2afb3b6 80//_______________________________________________________________________
81AliRun::AliRun():
82 fRun(0),
83 fEvent(0),
84 fEventNrInRun(0),
85 fEventsPerRun(0),
86 fDebug(0),
87 fHeader(0),
88 fTreeD(0),
89 fTreeS(0),
90 fTreeH(0),
91 fTreeTR(0),
92 fTreeE(0),
93 fTreeR(0),
94 fModules(0),
95 fGeometry(0),
96 fDisplay(0),
97 fTimer(),
98 fField(0),
99 fMC(0),
100 fImedia(0),
101 fNdets(0),
102 fTrRmax(1.e10),
103 fTrZmax(1.e10),
104 fGenerator(0),
105 fInitDone(kFALSE),
106 fLego(0),
107 fPDGDB(0), //Particle factory object
108 fHitLists(0),
109 fEventEnergy(0),
110 fSummEnergy(0),
111 fSum2Energy(0),
112 fConfigFunction("\0"),
113 fRandom(0),
114 fMCQA(0),
115 fTransParName("\0"),
116 fBaseFileName("\0"),
117 fStack(0),
118 fTreeDFileName(""),
119 fTreeDFile(0),
120 fTreeSFileName(""),
121 fTreeSFile(0),
122 fTreeRFileName(""),
123 fTreeRFile(0)
fe4da5cc 124{
125 //
126 // Default constructor for AliRun
127 //
e2afb3b6 128}
129
130//_______________________________________________________________________
131AliRun::AliRun(const AliRun& arun):
132 TVirtualMCApplication(arun),
133 fRun(0),
134 fEvent(0),
135 fEventNrInRun(0),
136 fEventsPerRun(0),
137 fDebug(0),
138 fHeader(0),
139 fTreeD(0),
140 fTreeS(0),
141 fTreeH(0),
142 fTreeTR(0),
143 fTreeE(0),
144 fTreeR(0),
145 fModules(0),
146 fGeometry(0),
147 fDisplay(0),
148 fTimer(),
149 fField(0),
150 fMC(0),
151 fImedia(0),
152 fNdets(0),
153 fTrRmax(1.e10),
154 fTrZmax(1.e10),
155 fGenerator(0),
156 fInitDone(kFALSE),
157 fLego(0),
158 fPDGDB(0), //Particle factory object
159 fHitLists(0),
160 fEventEnergy(0),
161 fSummEnergy(0),
162 fSum2Energy(0),
163 fConfigFunction("\0"),
164 fRandom(0),
165 fMCQA(0),
166 fTransParName("\0"),
167 fBaseFileName("\0"),
168 fStack(0),
169 fTreeDFileName(""),
170 fTreeDFile(0),
171 fTreeSFileName(""),
172 fTreeSFile(0),
173 fTreeRFileName(""),
174 fTreeRFile(0)
175{
176 //
177 // Copy constructor for AliRun
178 //
179 arun.Copy(*this);
fe4da5cc 180}
181
182//_____________________________________________________________________________
e2afb3b6 183AliRun::AliRun(const char *name, const char *title):
184 TVirtualMCApplication(name,title),
185 fRun(0),
186 fEvent(0),
187 fEventNrInRun(0),
188 fEventsPerRun(0),
189 fDebug(0),
190 fHeader(new AliHeader()),
191 fTreeD(0),
192 fTreeS(0),
193 fTreeH(0),
194 fTreeTR(0),
195 fTreeE(0),
196 fTreeR(0),
197 fModules(new TObjArray(77)), // Support list for the Detectors
198 fGeometry(0),
199 fDisplay(0),
200 fTimer(),
201 fField(0),
202 fMC(gMC),
203 fImedia(new TArrayI(1000)),
204 fNdets(0),
205 fTrRmax(1.e10),
206 fTrZmax(1.e10),
207 fGenerator(0),
208 fInitDone(kFALSE),
209 fLego(0),
210 fPDGDB(TDatabasePDG::Instance()), //Particle factory object!
211 fHitLists(new TList()), // Create HitLists list
212 fEventEnergy(0),
213 fSummEnergy(0),
214 fSum2Energy(0),
215 fConfigFunction("Config();"),
216 fRandom(new TRandom3()),
217 fMCQA(0),
218 fTransParName("\0"),
219 fBaseFileName("\0"),
220 fStack(new AliStack(10000)), //Particle stack
221 fTreeDFileName(""),
222 fTreeDFile(0),
223 fTreeSFileName(""),
224 fTreeSFile(0),
225 fTreeRFileName(""),
226 fTreeRFile(0)
fe4da5cc 227{
228 //
229 // Constructor for the main processor.
230 // Creates the geometry
231 // Creates the list of Detectors.
232 // Creates the list of particles.
233 //
e2afb3b6 234
fe4da5cc 235 gAlice = this;
65fb704d 236
237 // Set random number generator
e2afb3b6 238 gRandom = fRandom;
2ab0c725 239
240 if (gSystem->Getenv("CONFIG_SEED")) {
e2afb3b6 241 gRandom->SetSeed(static_cast<UInt_t>(atoi(gSystem->Getenv("CONFIG_SEED"))));
2ab0c725 242 }
e2afb3b6 243
244 // Add to list of browsable
fe4da5cc 245 gROOT->GetListOfBrowsables()->Add(this,name);
e2afb3b6 246
fe4da5cc 247 // Create the TNode geometry for the event display
fe4da5cc 248 BuildSimpleGeometry();
249
fe4da5cc 250 // Create default mag field
251 SetField();
e2afb3b6 252
fe4da5cc 253 // Prepare the tracking medium lists
e2afb3b6 254 for(Int_t i=0;i<1000;i++) (*fImedia)[i]=-99;
9e1a0ddb 255
e2afb3b6 256 // Add particle list to configuration
9e1a0ddb 257 AliConfig::Instance()->Add(fPDGDB);
e2afb3b6 258
259 // Set transport parameters
65fb704d 260 SetTransPar();
fe4da5cc 261}
262
aee8290b 263
e2afb3b6 264//_______________________________________________________________________
fe4da5cc 265AliRun::~AliRun()
266{
267 //
2ab0c725 268 // Default AliRun destructor
fe4da5cc 269 //
7a16e9cc 270 TFile *curfil =0;
271 if(fTreeE)curfil=fTreeE->GetCurrentFile();
fe4da5cc 272 delete fImedia;
273 delete fField;
f5bc1485 274 // delete fMC;
275 delete gMC; gMC=0;
fe4da5cc 276 delete fGeometry;
277 delete fDisplay;
278 delete fGenerator;
279 delete fLego;
280 delete fTreeD;
fe4da5cc 281 delete fTreeH;
aab9c8d5 282 delete fTreeTR;
fe4da5cc 283 delete fTreeE;
284 delete fTreeR;
2ab0c725 285 delete fTreeS;
8494b010 286 if (fModules) {
287 fModules->Delete();
288 delete fModules;
fe4da5cc 289 }
9e1a0ddb 290 delete fStack;
1cedd08a 291 delete fHitLists;
c222d2b0 292 delete fPDGDB;
e460afec 293 delete fMCQA;
9e1a0ddb 294 delete fHeader;
7a16e9cc 295 // avoid to delete TFile objects not owned by this object
296 // avoid multiple deletions
297 if(curfil == fTreeDFile) fTreeDFile=0;
298 if(curfil == fTreeSFile) fTreeSFile=0;
299 if(curfil == fTreeRFile) fTreeRFile=0;
300 if(fTreeSFile == fTreeDFile) fTreeSFile=0;
301 if(fTreeRFile == fTreeDFile) fTreeRFile=0;
302 if(fTreeRFile == fTreeSFile) fTreeRFile=0;
303 if(fTreeDFile){
304 if(fTreeDFile->IsOpen())fTreeDFile->Close();
305 delete fTreeDFile;
306 }
307 if(fTreeSFile){
308 if(fTreeSFile->IsOpen())fTreeSFile->Close();
309 delete fTreeSFile;
310 }
311 if(fTreeRFile){
312 if(fTreeRFile->IsOpen())fTreeRFile->Close();
313 delete fTreeRFile;
314 }
315 if (gROOT->GetListOfBrowsables())
316 gROOT->GetListOfBrowsables()->Remove(this);
b9d0a01d 317
318 gAlice=0;
fe4da5cc 319}
320
e2afb3b6 321//_______________________________________________________________________
322void AliRun::Copy(AliRun &) const
323{
37d06d5b 324 //
325 // Copy method ... not implemented
326 //
e2afb3b6 327 Fatal("Copy","Not implemented!\n");
328}
329
330//_______________________________________________________________________
fe4da5cc 331void AliRun::AddHit(Int_t id, Int_t track, Int_t *vol, Float_t *hits) const
332{
333 //
334 // Add a hit to detector id
335 //
8494b010 336 TObjArray &dets = *fModules;
e2afb3b6 337 if(dets[id]) dynamic_cast<AliModule*>(dets[id])->AddHit(track,vol,hits);
fe4da5cc 338}
339
e2afb3b6 340//_______________________________________________________________________
fe4da5cc 341void AliRun::AddDigit(Int_t id, Int_t *tracks, Int_t *digits) const
342{
343 //
344 // Add digit to detector id
345 //
8494b010 346 TObjArray &dets = *fModules;
e2afb3b6 347 if(dets[id]) dynamic_cast<AliModule*>(dets[id])->AddDigit(tracks,digits);
fe4da5cc 348}
349
e2afb3b6 350//_______________________________________________________________________
fe4da5cc 351void AliRun::Browse(TBrowser *b)
352{
353 //
354 // Called when the item "Run" is clicked on the left pane
355 // of the Root browser.
356 // It displays the Root Trees and all detectors.
357 //
9e1a0ddb 358 if(!fStack) fStack=fHeader->Stack();
359 TTree* pTreeK = fStack->TreeK();
360
361 if (pTreeK) b->Add(pTreeK,pTreeK->GetName());
fe4da5cc 362 if (fTreeH) b->Add(fTreeH,fTreeH->GetName());
aab9c8d5 363 if (fTreeTR) b->Add(fTreeTR,fTreeH->GetName());
fe4da5cc 364 if (fTreeD) b->Add(fTreeD,fTreeD->GetName());
365 if (fTreeE) b->Add(fTreeE,fTreeE->GetName());
366 if (fTreeR) b->Add(fTreeR,fTreeR->GetName());
2ab0c725 367 if (fTreeS) b->Add(fTreeS,fTreeS->GetName());
fe4da5cc 368
8494b010 369 TIter next(fModules);
370 AliModule *detector;
e2afb3b6 371 while((detector = dynamic_cast<AliModule*>(next()))) {
fe4da5cc 372 b->Add(detector,detector->GetName());
373 }
65fb704d 374 b->Add(fMCQA,"AliMCQA");
fe4da5cc 375}
376
e2afb3b6 377//_______________________________________________________________________
fe4da5cc 378void AliRun::Build()
379{
380 //
381 // Initialize Alice geometry
382 // Dummy routine
383 //
384}
385
e2afb3b6 386//_______________________________________________________________________
fe4da5cc 387void AliRun::BuildSimpleGeometry()
388{
389 //
390 // Create a simple TNode geometry used by Root display engine
391 //
392 // Initialise geometry
393 //
394 fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
395 new TMaterial("void","Vacuum",0,0,0); //Everything is void
396 TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
397 brik->SetVisibility(0);
398 new TNode("alice","alice","S_alice");
399}
400
e2afb3b6 401//_______________________________________________________________________
fe4da5cc 402void AliRun::CleanDetectors()
403{
404 //
405 // Clean Detectors at the end of event
406 //
8494b010 407 TIter next(fModules);
408 AliModule *detector;
e2afb3b6 409 while((detector = dynamic_cast<AliModule*>(next()))) {
fe4da5cc 410 detector->FinishEvent();
411 }
412}
413
e2afb3b6 414//_______________________________________________________________________
116cbefd 415Int_t AliRun::DistancetoPrimitive(Int_t, Int_t) const
fe4da5cc 416{
417 //
418 // Return the distance from the mouse to the AliRun object
419 // Dummy routine
420 //
421 return 9999;
422}
423
e2afb3b6 424//_______________________________________________________________________
94de3818 425void AliRun::DumpPart (Int_t i) const
fe4da5cc 426{
427 //
428 // Dumps particle i in the stack
429 //
9e1a0ddb 430 fStack->DumpPart(i);
fe4da5cc 431}
432
e2afb3b6 433//_______________________________________________________________________
94de3818 434void AliRun::DumpPStack () const
fe4da5cc 435{
436 //
437 // Dumps the particle stack
438 //
9e1a0ddb 439 fStack->DumpPStack();
fe4da5cc 440}
441
e2afb3b6 442//_______________________________________________________________________
d8408e76 443void AliRun::SetField(AliMagF* magField)
444{
445 // Set Magnetic Field Map
446 fField = magField;
447 fField->ReadField();
448}
449
e2afb3b6 450//_______________________________________________________________________
fe4da5cc 451void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
452 Float_t maxField, char* filename)
453{
454 //
455 // Set magnetic field parameters
456 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
457 // version Magnetic field map version (only 1 active now)
458 // scale Scale factor for the magnetic field
459 // maxField Maximum value for the magnetic field
460
461 //
462 // --- Sanity check on mag field flags
fe4da5cc 463 if(fField) delete fField;
464 if(version==1) {
d8408e76 465 fField = new AliMagFC("Map1"," ",type,scale,maxField);
f1b9d7c3 466 } else if(version<=2) {
d8408e76 467 fField = new AliMagFCM("Map2-3",filename,type,scale,maxField);
fe4da5cc 468 fField->ReadField();
f1b9d7c3 469 } else if(version==3) {
d8408e76 470 fField = new AliMagFDM("Map4",filename,type,scale,maxField);
f1b9d7c3 471 fField->ReadField();
fe4da5cc 472 } else {
23370b7a 473 Warning("SetField","Invalid map %d\n",version);
fe4da5cc 474 }
475}
fe4da5cc 476
e2afb3b6 477//_______________________________________________________________________
fe4da5cc 478void AliRun::FinishRun()
479{
480 //
481 // Called at the end of the run.
482 //
483
dffd31ef 484 //
485 if(fLego) fLego->FinishRun();
8e70f139 486
fe4da5cc 487 // Clean detector information
8494b010 488 TIter next(fModules);
489 AliModule *detector;
e2afb3b6 490 while((detector = dynamic_cast<AliModule*>(next()))) {
fe4da5cc 491 detector->FinishRun();
492 }
493
494 //Output energy summary tables
495 EnergySummary();
2ab0c725 496
497 TFile *file = fTreeE->GetCurrentFile();
498
aee8290b 499 file->cd();
2ab0c725 500
51e0e89d 501 fTreeE->Write(0,TObject::kOverwrite);
fe4da5cc 502
2ab0c725 503 // Write AliRun info and all detectors parameters
d47c658f 504 Write(0,TObject::kOverwrite);
682a4a95 505
fe4da5cc 506 // Clean tree information
9e1a0ddb 507
508 fStack->FinishRun();
509
d2ecd553 510 if (fTreeH) {
511 delete fTreeH; fTreeH = 0;
512 }
aab9c8d5 513 if (fTreeTR) {
514 delete fTreeTR; fTreeTR = 0;
515 }
d2ecd553 516 if (fTreeD) {
517 delete fTreeD; fTreeD = 0;
518 }
519 if (fTreeR) {
520 delete fTreeR; fTreeR = 0;
521 }
7e90ff59 522// if (fTreeE) {
523// delete fTreeE; fTreeE = 0;
524// }
9e1a0ddb 525 if (fTreeS) {
526 delete fTreeS; fTreeS = 0;
527 }
6df200c3 528 fGenerator->FinishRun();
529
fe4da5cc 530 // Close output file
aee8290b 531 file->Write();
fe4da5cc 532}
533
e2afb3b6 534//_______________________________________________________________________
fe4da5cc 535void AliRun::FlagTrack(Int_t track)
536{
9e1a0ddb 537 // Delegate to stack
fe4da5cc 538 //
9e1a0ddb 539 fStack->FlagTrack(track);
fe4da5cc 540}
541
e2afb3b6 542//_______________________________________________________________________
fe4da5cc 543void AliRun::EnergySummary()
544{
545 //
546 // Print summary of deposited energy
547 //
548
fe4da5cc 549 Int_t ndep=0;
550 Float_t edtot=0;
551 Float_t ed, ed2;
552 Int_t kn, i, left, j, id;
aee8290b 553 const Float_t kzero=0;
9e1a0ddb 554 Int_t ievent=fHeader->GetEvent()+1;
fe4da5cc 555 //
556 // Energy loss information
557 if(ievent) {
558 printf("***************** Energy Loss Information per event (GEV) *****************\n");
875c717b 559 for(kn=1;kn<fEventEnergy.GetSize();kn++) {
560 ed=fSummEnergy[kn];
fe4da5cc 561 if(ed>0) {
875c717b 562 fEventEnergy[ndep]=kn;
fe4da5cc 563 if(ievent>1) {
564 ed=ed/ievent;
875c717b 565 ed2=fSum2Energy[kn];
fe4da5cc 566 ed2=ed2/ievent;
aee8290b 567 ed2=100*TMath::Sqrt(TMath::Max(ed2-ed*ed,kzero))/ed;
fe4da5cc 568 } else
569 ed2=99;
875c717b 570 fSummEnergy[ndep]=ed;
e2afb3b6 571 fSum2Energy[ndep]=TMath::Min(static_cast<Float_t>(99.),TMath::Max(ed2,kzero));
fe4da5cc 572 edtot+=ed;
573 ndep++;
574 }
575 }
576 for(kn=0;kn<(ndep-1)/3+1;kn++) {
577 left=ndep-kn*3;
578 for(i=0;i<(3<left?3:left);i++) {
579 j=kn*3+i;
875c717b 580 id=Int_t (fEventEnergy[j]+0.1);
581 printf(" %s %10.3f +- %10.3f%%;",gMC->VolName(id),fSummEnergy[j],fSum2Energy[j]);
fe4da5cc 582 }
583 printf("\n");
584 }
585 //
586 // Relative energy loss in different detectors
587 printf("******************** Relative Energy Loss per event ********************\n");
588 printf("Total energy loss per event %10.3f GeV\n",edtot);
589 for(kn=0;kn<(ndep-1)/5+1;kn++) {
590 left=ndep-kn*5;
591 for(i=0;i<(5<left?5:left);i++) {
592 j=kn*5+i;
875c717b 593 id=Int_t (fEventEnergy[j]+0.1);
594 printf(" %s %10.3f%%;",gMC->VolName(id),100*fSummEnergy[j]/edtot);
fe4da5cc 595 }
596 printf("\n");
597 }
598 for(kn=0;kn<75;kn++) printf("*");
599 printf("\n");
600 }
601 //
602 // Reset the TArray's
875c717b 603 // fEventEnergy.Set(0);
604 // fSummEnergy.Set(0);
605 // fSum2Energy.Set(0);
fe4da5cc 606}
607
eb1b8d29 608//_______________________________________________________________________
609void AliRun::Announce() const
610{
611 //
37d06d5b 612 // Announce the current version of AliRoot
613 //
614 printf("%70s",
615 "****************************************************************\n");
616 printf("%6s","*");printf("%64s","*\n");
617
618 printf("%6s","*");
3bddc65e 619 printf(" You are running AliRoot version v3-09-07\n");
37d06d5b 620
621 printf("%6s","*");
622 printf(" The cvs tag for the current program is $Name$\n");
623
624 printf("%6s","*");printf("%64s","*\n");
625 printf("%70s",
626 "****************************************************************\n");
eb1b8d29 627}
628
e2afb3b6 629//_______________________________________________________________________
94de3818 630AliModule *AliRun::GetModule(const char *name) const
fe4da5cc 631{
632 //
633 // Return pointer to detector from name
634 //
e2afb3b6 635 return dynamic_cast<AliModule*>(fModules->FindObject(name));
fe4da5cc 636}
637
e2afb3b6 638//_______________________________________________________________________
94de3818 639AliDetector *AliRun::GetDetector(const char *name) const
a68348e9 640{
641 //
642 // Return pointer to detector from name
643 //
e2afb3b6 644 return dynamic_cast<AliDetector*>(fModules->FindObject(name));
a68348e9 645}
646
e2afb3b6 647//_______________________________________________________________________
94de3818 648Int_t AliRun::GetModuleID(const char *name) const
fe4da5cc 649{
650 //
651 // Return galice internal detector identifier from name
652 //
23370b7a 653 Int_t i=-1;
654 TObject *mod=fModules->FindObject(name);
655 if(mod) i=fModules->IndexOf(mod);
656 return i;
fe4da5cc 657}
658
e2afb3b6 659//_______________________________________________________________________
fe4da5cc 660Int_t AliRun::GetEvent(Int_t event)
661{
662 //
663 // Connect the Trees Kinematics and Hits for event # event
664 // Set branch addresses
665 //
fe4da5cc 666
667 // Reset existing structures
fe4da5cc 668 ResetHits();
aab9c8d5 669 ResetTrackReferences();
fe4da5cc 670 ResetDigits();
2ab0c725 671 ResetSDigits();
fe4da5cc 672
673 // Delete Trees already connected
ccf7a81f 674 if (fTreeH) { delete fTreeH; fTreeH = 0;}
aab9c8d5 675 if (fTreeTR) { delete fTreeTR; fTreeTR = 0;}
ccf7a81f 676 if (fTreeD) { delete fTreeD; fTreeD = 0;}
677 if (fTreeR) { delete fTreeR; fTreeR = 0;}
678 if (fTreeS) { delete fTreeS; fTreeS = 0;}
59fe9bd2 679
9e1a0ddb 680 // Create the particle stack
681 if (fHeader) delete fHeader;
682 fHeader = 0;
683
59fe9bd2 684 // Get header from file
9e1a0ddb 685 if(fTreeE) {
686 fTreeE->SetBranchAddress("Header", &fHeader);
ccf7a81f 687
688 if (!fTreeE->GetEntry(event)) {
689 Error("GetEvent","Cannot find event:%d\n",event);
690 return -1;
691 }
9e1a0ddb 692 }
ccf7a81f 693 else {
9e1a0ddb 694 Error("GetEvent","Cannot find Header Tree (TE)\n");
ccf7a81f 695 return -1;
696 }
9e1a0ddb 697
ccf7a81f 698 // Get the stack from the header, set fStack to 0 if it
699 // fails to get event
9e1a0ddb 700 //
2ab0c725 701 TFile *file = fTreeE->GetCurrentFile();
fe4da5cc 702 char treeName[20];
6df200c3 703
2ab0c725 704 file->cd();
705
50a6540a 706 if (fStack) delete fStack;
707 fStack = fHeader->Stack();
708 if (fStack) {
709 if (!fStack->GetEvent(event)) fStack = 0;
710 }
711
fe4da5cc 712 // Get Hits Tree header from file
713 sprintf(treeName,"TreeH%d",event);
e2afb3b6 714 fTreeH = dynamic_cast<TTree*>(gDirectory->Get(treeName));
2057d00a 715 if (!fTreeH) {
b9d0a01d 716 Warning("GetEvent","cannot find Hits Tree for event:%d\n",event);
2057d00a 717 }
718
aab9c8d5 719 // Get TracReferences Tree header from file
720 sprintf(treeName,"TreeTR%d",event);
e2afb3b6 721 fTreeTR = dynamic_cast<TTree*>(gDirectory->Get(treeName));
aab9c8d5 722 if (!fTreeTR) {
3d8c4988 723 Warning("GetEvent","cannot find TrackReferences Tree for event:%d\n",event);
aab9c8d5 724 }
725
7a16e9cc 726 // get current file name and compare with names containing trees S,D,R
e2afb3b6 727 TString curfilname=static_cast<TString>(fTreeE->GetCurrentFile()->GetName());
7a16e9cc 728 if(fTreeDFileName==curfilname)fTreeDFileName="";
729 if(fTreeSFileName==curfilname)fTreeSFileName="";
730 if(fTreeRFileName==curfilname)fTreeRFileName="";
b9d0a01d 731
fe4da5cc 732 // Get Digits Tree header from file
733 sprintf(treeName,"TreeD%d",event);
b9d0a01d 734
7a16e9cc 735 if (!fTreeDFile && fTreeDFileName != "") {
736 InitTreeFile("D",fTreeDFileName);
737 }
738 if (fTreeDFile) {
e2afb3b6 739 fTreeD = dynamic_cast<TTree*>(fTreeDFile->Get(treeName));
7a16e9cc 740 } else {
e2afb3b6 741 fTreeD = dynamic_cast<TTree*>(file->Get(treeName));
7a16e9cc 742 }
fe4da5cc 743 if (!fTreeD) {
2ab0c725 744 // Warning("GetEvent","cannot find Digits Tree for event:%d\n",event);
fe4da5cc 745 }
7a16e9cc 746 if(fTreeDFileName != ""){
747 if(fTreeDFileName==fTreeSFileName) {
748 fTreeSFileName = "";
749 fTreeSFile = fTreeDFile;
750 }
751 if(fTreeDFileName==fTreeRFileName) {
752 fTreeRFileName = "";
753 fTreeRFile = fTreeDFile;
754 }
755 }
82711e7a 756
757 file->cd();
758
759 // Get SDigits Tree header from file
760 sprintf(treeName,"TreeS%d",event);
7a16e9cc 761 if (!fTreeSFile && fTreeSFileName != "") {
762 InitTreeFile("S",fTreeSFileName);
763 }
764 if (fTreeSFile) {
e2afb3b6 765 fTreeS = dynamic_cast<TTree*>(fTreeSFile->Get(treeName));
7a16e9cc 766 } else {
e2afb3b6 767 fTreeS = dynamic_cast<TTree*>(gDirectory->Get(treeName));
7a16e9cc 768 }
82711e7a 769 if (!fTreeS) {
770 // Warning("GetEvent","cannot find SDigits Tree for event:%d\n",event);
771 }
772
7a16e9cc 773 if(fTreeSFileName != ""){
774 if(fTreeSFileName==fTreeRFileName){
775 fTreeRFileName = "";
776 fTreeRFile = fTreeSFile;
777 }
778 }
779
2ab0c725 780 file->cd();
fe4da5cc 781
782 // Get Reconstruct Tree header from file
783 sprintf(treeName,"TreeR%d",event);
7a16e9cc 784 if (!fTreeRFile && fTreeRFileName != "") {
785 InitTreeFile("R",fTreeRFileName);
786 }
787 if(fTreeRFile) {
e2afb3b6 788 fTreeR = dynamic_cast<TTree*>(fTreeRFile->Get(treeName));
7a16e9cc 789 } else {
e2afb3b6 790 fTreeR = dynamic_cast<TTree*>(gDirectory->Get(treeName));
7a16e9cc 791 }
fe4da5cc 792 if (!fTreeR) {
793 // printf("WARNING: cannot find Reconstructed Tree for event:%d\n",event);
794 }
2ab0c725 795
796 file->cd();
fe4da5cc 797
798 // Set Trees branch addresses
8494b010 799 TIter next(fModules);
800 AliModule *detector;
e2afb3b6 801 while((detector = dynamic_cast<AliModule*>(next()))) {
fe4da5cc 802 detector->SetTreeAddress();
803 }
9e1a0ddb 804
fa85c972 805 fEvent=event; //MI change
806
eef4b160 807 return fHeader->GetNtrack();
fe4da5cc 808}
809
e2afb3b6 810//_______________________________________________________________________
fe4da5cc 811TGeometry *AliRun::GetGeometry()
812{
813 //
814 // Import Alice geometry from current file
815 // Return pointer to geometry object
816 //
e2afb3b6 817 if (!fGeometry) fGeometry = dynamic_cast<TGeometry*>(gDirectory->Get("AliceGeom"));
fe4da5cc 818 //
819 // Unlink and relink nodes in detectors
820 // This is bad and there must be a better way...
821 //
fe4da5cc 822
8494b010 823 TIter next(fModules);
824 AliModule *detector;
e2afb3b6 825 while((detector = dynamic_cast<AliModule*>(next()))) {
fe4da5cc 826 TList *dnodes=detector->Nodes();
827 Int_t j;
828 TNode *node, *node1;
829 for ( j=0; j<dnodes->GetSize(); j++) {
e2afb3b6 830 node = dynamic_cast<TNode*>(dnodes->At(j));
52d0ab00 831 node1 = fGeometry->GetNode(node->GetName());
fe4da5cc 832 dnodes->Remove(node);
833 dnodes->AddAt(node1,j);
834 }
835 }
836 return fGeometry;
837}
838
e2afb3b6 839//_______________________________________________________________________
9e1a0ddb 840Int_t AliRun::GetPrimary(Int_t track) const
fe4da5cc 841{
842 //
843 // return number of primary that has generated track
844 //
9e1a0ddb 845 return fStack->GetPrimary(track);
fe4da5cc 846}
847
e2afb3b6 848//_______________________________________________________________________
fe4da5cc 849void AliRun::MediaTable()
850{
851 //
852 // Built media table to get from the media number to
853 // the detector id
854 //
ad51aeb0 855 Int_t kz, nz, idt, lz, i, k, ind;
856 // Int_t ibeg;
fe4da5cc 857 TObjArray &dets = *gAlice->Detectors();
8494b010 858 AliModule *det;
fe4da5cc 859 //
860 // For all detectors
861 for (kz=0;kz<fNdets;kz++) {
862 // If detector is defined
e2afb3b6 863 if((det=dynamic_cast<AliModule*>(dets[kz]))) {
ad51aeb0 864 TArrayI &idtmed = *(det->GetIdtmed());
865 for(nz=0;nz<100;nz++) {
fe4da5cc 866 // Find max and min material number
ad51aeb0 867 if((idt=idtmed[nz])) {
fe4da5cc 868 det->LoMedium() = det->LoMedium() < idt ? det->LoMedium() : idt;
869 det->HiMedium() = det->HiMedium() > idt ? det->HiMedium() : idt;
870 }
871 }
872 if(det->LoMedium() > det->HiMedium()) {
873 det->LoMedium() = 0;
874 det->HiMedium() = 0;
875 } else {
876 if(det->HiMedium() > fImedia->GetSize()) {
ad51aeb0 877 Error("MediaTable","Increase fImedia from %d to %d",
878 fImedia->GetSize(),det->HiMedium());
fe4da5cc 879 return;
880 }
881 // Tag all materials in rage as belonging to detector kz
882 for(lz=det->LoMedium(); lz<= det->HiMedium(); lz++) {
883 (*fImedia)[lz]=kz;
884 }
885 }
886 }
887 }
888 //
889 // Print summary table
890 printf(" Traking media ranges:\n");
891 for(i=0;i<(fNdets-1)/6+1;i++) {
892 for(k=0;k< (6<fNdets-i*6?6:fNdets-i*6);k++) {
893 ind=i*6+k;
e2afb3b6 894 det=dynamic_cast<AliModule*>(dets[ind]);
fe4da5cc 895 if(det)
896 printf(" %6s: %3d -> %3d;",det->GetName(),det->LoMedium(),
897 det->HiMedium());
898 else
899 printf(" %6s: %3d -> %3d;","NULL",0,0);
900 }
901 printf("\n");
902 }
903}
904
e2afb3b6 905//_______________________________________________________________________
fe4da5cc 906void AliRun::SetGenerator(AliGenerator *generator)
ee1dd322 907{
908 //
909 // Load the event generator
910 //
911 if(!fGenerator) fGenerator = generator;
912}
913
e2afb3b6 914//_______________________________________________________________________
ee1dd322 915void AliRun::ResetGenerator(AliGenerator *generator)
fe4da5cc 916{
917 //
918 // Load the event generator
919 //
b13db077 920 if(fGenerator)
838edcaf 921 if(generator)
922 Warning("ResetGenerator","Replacing generator %s with %s\n",
923 fGenerator->GetName(),generator->GetName());
924 else
925 Warning("ResetGenerator","Replacing generator %s with NULL\n",
926 fGenerator->GetName());
b13db077 927 fGenerator = generator;
fe4da5cc 928}
929
e2afb3b6 930//_______________________________________________________________________
931void AliRun::SetTransPar(const char *filename)
65fb704d 932{
37d06d5b 933 //
934 // Sets the file name for transport parameters
935 //
65fb704d 936 fTransParName = filename;
937}
938
e2afb3b6 939//_______________________________________________________________________
940void AliRun::SetBaseFile(const char *filename)
2ab0c725 941{
39de14fb 942 fBaseFileName = filename;
2ab0c725 943}
944
e2afb3b6 945//_______________________________________________________________________
65fb704d 946void AliRun::ReadTransPar()
fe4da5cc 947{
948 //
949 // Read filename to set the transport parameters
950 //
951
fe4da5cc 952
aee8290b 953 const Int_t kncuts=10;
954 const Int_t knflags=11;
955 const Int_t knpars=kncuts+knflags;
956 const char kpars[knpars][7] = {"CUTGAM" ,"CUTELE","CUTNEU","CUTHAD","CUTMUO",
fe4da5cc 957 "BCUTE","BCUTM","DCUTE","DCUTM","PPCUTM","ANNI",
958 "BREM","COMP","DCAY","DRAY","HADR","LOSS",
959 "MULS","PAIR","PHOT","RAYL"};
960 char line[256];
ad51aeb0 961 char detName[7];
fe4da5cc 962 char* filtmp;
aee8290b 963 Float_t cut[kncuts];
964 Int_t flag[knflags];
fe4da5cc 965 Int_t i, itmed, iret, ktmed, kz;
966 FILE *lun;
967 //
968 // See whether the file is there
65fb704d 969 filtmp=gSystem->ExpandPathName(fTransParName.Data());
fe4da5cc 970 lun=fopen(filtmp,"r");
971 delete [] filtmp;
972 if(!lun) {
65fb704d 973 Warning("ReadTransPar","File %s does not exist!\n",fTransParName.Data());
fe4da5cc 974 return;
975 }
976 //
9e1a0ddb 977 if(fDebug) {
978 printf(" "); for(i=0;i<60;i++) printf("*"); printf("\n");
979 printf(" *%59s\n","*");
980 printf(" * Please check carefully what you are doing!%10s\n","*");
981 printf(" *%59s\n","*");
982 }
fe4da5cc 983 //
984 while(1) {
985 // Initialise cuts and flags
aee8290b 986 for(i=0;i<kncuts;i++) cut[i]=-99;
987 for(i=0;i<knflags;i++) flag[i]=-99;
fe4da5cc 988 itmed=0;
989 for(i=0;i<256;i++) line[i]='\0';
990 // Read up to the end of line excluded
991 iret=fscanf(lun,"%[^\n]",line);
992 if(iret<0) {
993 //End of file
994 fclose(lun);
9e1a0ddb 995 if(fDebug){
996 printf(" *%59s\n","*");
997 printf(" "); for(i=0;i<60;i++) printf("*"); printf("\n");
998 }
fe4da5cc 999 return;
1000 }
1001 // Read the end of line
1002 fscanf(lun,"%*c");
1003 if(!iret) continue;
1004 if(line[0]=='*') continue;
1005 // Read the numbers
ad51aeb0 1006 iret=sscanf(line,"%s %d %f %f %f %f %f %f %f %f %f %f %d %d %d %d %d %d %d %d %d %d %d",
1007 detName,&itmed,&cut[0],&cut[1],&cut[2],&cut[3],&cut[4],&cut[5],&cut[6],&cut[7],&cut[8],
1008 &cut[9],&flag[0],&flag[1],&flag[2],&flag[3],&flag[4],&flag[5],&flag[6],&flag[7],
1009 &flag[8],&flag[9],&flag[10]);
fe4da5cc 1010 if(!iret) continue;
1011 if(iret<0) {
1012 //reading error
65fb704d 1013 Warning("ReadTransPar","Error reading file %s\n",fTransParName.Data());
fe4da5cc 1014 continue;
1015 }
ad51aeb0 1016 // Check that the module exist
1017 AliModule *mod = GetModule(detName);
1018 if(mod) {
1019 // Get the array of media numbers
1020 TArrayI &idtmed = *mod->GetIdtmed();
1021 // Check that the tracking medium code is valid
1022 if(0<=itmed && itmed < 100) {
1023 ktmed=idtmed[itmed];
1024 if(!ktmed) {
65fb704d 1025 Warning("ReadTransPar","Invalid tracking medium code %d for %s\n",itmed,mod->GetName());
ad51aeb0 1026 continue;
fe4da5cc 1027 }
ad51aeb0 1028 // Set energy thresholds
aee8290b 1029 for(kz=0;kz<kncuts;kz++) {
ad51aeb0 1030 if(cut[kz]>=0) {
9e1a0ddb 1031 if(fDebug) printf(" * %-6s set to %10.3E for tracking medium code %4d for %s\n",
aee8290b 1032 kpars[kz],cut[kz],itmed,mod->GetName());
1033 gMC->Gstpar(ktmed,kpars[kz],cut[kz]);
ad51aeb0 1034 }
fe4da5cc 1035 }
ad51aeb0 1036 // Set transport mechanisms
aee8290b 1037 for(kz=0;kz<knflags;kz++) {
ad51aeb0 1038 if(flag[kz]>=0) {
9e1a0ddb 1039 if(fDebug) printf(" * %-6s set to %10d for tracking medium code %4d for %s\n",
aee8290b 1040 kpars[kncuts+kz],flag[kz],itmed,mod->GetName());
1041 gMC->Gstpar(ktmed,kpars[kncuts+kz],Float_t(flag[kz]));
ad51aeb0 1042 }
1043 }
1044 } else {
65fb704d 1045 Warning("ReadTransPar","Invalid medium code %d *\n",itmed);
ad51aeb0 1046 continue;
fe4da5cc 1047 }
1048 } else {
9e1a0ddb 1049 if(fDebug) printf("%s::ReadTransParModule: %s not present\n",ClassName(),detName);
fe4da5cc 1050 continue;
1051 }
1052 }
1053}
1054
2ab0c725 1055
e2afb3b6 1056//_______________________________________________________________________
9e1a0ddb 1057void AliRun::MakeTree(Option_t *option, const char *file)
fe4da5cc 1058{
1059 //
1060 // Create the ROOT trees
1061 // Loop on all detectors to create the Root branch (if any)
1062 //
1063
b13db077 1064 char hname[30];
fe4da5cc 1065 //
1066 // Analyse options
5cf7bbad 1067 const char *oK = strstr(option,"K");
1068 const char *oH = strstr(option,"H");
aab9c8d5 1069 const char *oTR = strstr(option,"T");
5cf7bbad 1070 const char *oE = strstr(option,"E");
1071 const char *oD = strstr(option,"D");
1072 const char *oR = strstr(option,"R");
1073 const char *oS = strstr(option,"S");
fe4da5cc 1074 //
9e1a0ddb 1075
1076 TDirectory *cwd = gDirectory;
1077
1078 TBranch *branch = 0;
2ab0c725 1079
9e1a0ddb 1080 if (oK) fStack->MakeTree(fEvent, file);
1081
1082 if (oE && !fTreeE) {
1083 fTreeE = new TTree("TE","Header");
62016442 1084 // branch = fTreeE->Branch("Header", "AliHeader", &fHeader, 4000, 0);
21e74529 1085 branch = fTreeE->Branch("Header", "AliHeader", &fHeader, 4000, 0);
9e1a0ddb 1086 branch->SetAutoDelete(kFALSE);
e2afb3b6 1087 TFolder *folder = dynamic_cast<TFolder *>(gROOT->FindObjectAny("/Folders/RunMC/Event/Header"));
9e1a0ddb 1088 if (folder) folder->Add(fHeader);
1089// branch = fTreeE->Branch("Stack","AliStack", &fStack, 4000, 0);
1090// branch->SetAutoDelete(kFALSE);
1091// if (folder) folder->Add(fStack);
1092 fTreeE->Write(0,TObject::kOverwrite);
b13db077 1093 }
9e1a0ddb 1094
1095 if (file && branch) {
1096 char * outFile = new char[strlen(gAlice->GetBaseFile())+strlen(file)+2];
1097 sprintf(outFile,"%s/%s",GetBaseFile(),file);
1098 branch->SetFile(outFile);
1099 TIter next( branch->GetListOfBranches());
e2afb3b6 1100 while ((branch=dynamic_cast<TBranch*>(next()))) {
9e1a0ddb 1101 branch->SetFile(outFile);
1102 }
1103 if (GetDebug()>1)
1104 printf("* MakeBranch * Diverting Branch %s to file %s\n", branch->GetName(),file);
1105 cwd->cd();
1106 delete outFile;
1107 }
1108
aee8290b 1109 if (oH && !fTreeH) {
b13db077 1110 sprintf(hname,"TreeH%d",fEvent);
1111 fTreeH = new TTree(hname,"Hits");
1112 fTreeH->SetAutoSave(1000000000); //no autosave
9e1a0ddb 1113 fTreeH->Write(0,TObject::kOverwrite);
b13db077 1114 }
aab9c8d5 1115
1116 if (oTR && !fTreeTR) {
1117 sprintf(hname,"TreeTR%d",fEvent);
1118 fTreeTR = new TTree(hname,"TrackReferences");
1119 fTreeTR->SetAutoSave(1000000000); //no autosave
1120 fTreeTR->Write(0,TObject::kOverwrite);
1121 }
1122
aee8290b 1123 if (oD && !fTreeD) {
b13db077 1124 sprintf(hname,"TreeD%d",fEvent);
1125 fTreeD = new TTree(hname,"Digits");
9e1a0ddb 1126 fTreeD->Write(0,TObject::kOverwrite);
b13db077 1127 }
2ab0c725 1128 if (oS && !fTreeS) {
1129 sprintf(hname,"TreeS%d",fEvent);
1130 fTreeS = new TTree(hname,"SDigits");
9e1a0ddb 1131 fTreeS->Write(0,TObject::kOverwrite);
2ab0c725 1132 }
aee8290b 1133 if (oR && !fTreeR) {
b13db077 1134 sprintf(hname,"TreeR%d",fEvent);
1135 fTreeR = new TTree(hname,"Reconstruction");
9e1a0ddb 1136 fTreeR->Write(0,TObject::kOverwrite);
b13db077 1137 }
9e1a0ddb 1138
fe4da5cc 1139 //
1140 // Create a branch for hits/digits for each detector
1141 // Each branch is a TClonesArray. Each data member of the Hits classes
1142 // will be in turn a subbranch of the detector master branch
8494b010 1143 TIter next(fModules);
1144 AliModule *detector;
e2afb3b6 1145 while((detector = dynamic_cast<AliModule*>(next()))) {
b9d0a01d 1146 if (oH) detector->MakeBranch(option,file);
aab9c8d5 1147 if (oTR) detector->MakeBranchTR(option,file);
fe4da5cc 1148 }
fe4da5cc 1149}
1150
e2afb3b6 1151//_______________________________________________________________________
37d06d5b 1152TParticle* AliRun::Particle(Int_t i) const
2ab0c725 1153{
37d06d5b 1154 //
1155 // Returns particle i on the simulation stack
1156 //
1157 return fStack->Particle(i);
fe4da5cc 1158}
1159
e2afb3b6 1160//_______________________________________________________________________
b9d0a01d 1161void AliRun::ResetDigits()
fe4da5cc 1162{
1163 //
b9d0a01d 1164 // Reset all Detectors digits
fe4da5cc 1165 //
b9d0a01d 1166 TIter next(fModules);
1167 AliModule *detector;
e2afb3b6 1168 while((detector = dynamic_cast<AliModule*>(next()))) {
b9d0a01d 1169 detector->ResetDigits();
1170 }
1171}
1172
e2afb3b6 1173//_______________________________________________________________________
b9d0a01d 1174void AliRun::ResetSDigits()
1175{
dffd31ef 1176 //
b9d0a01d 1177 // Reset all Detectors digits
9e1a0ddb 1178 //
b9d0a01d 1179 TIter next(fModules);
1180 AliModule *detector;
e2afb3b6 1181 while((detector = dynamic_cast<AliModule*>(next()))) {
b9d0a01d 1182 detector->ResetSDigits();
1183 }
1184}
dffd31ef 1185
e2afb3b6 1186//_______________________________________________________________________
b9d0a01d 1187void AliRun::ResetHits()
1188{
dffd31ef 1189 //
b9d0a01d 1190 // Reset all Detectors hits
1191 //
1192 TIter next(fModules);
1193 AliModule *detector;
e2afb3b6 1194 while((detector = dynamic_cast<AliModule*>(next()))) {
b9d0a01d 1195 detector->ResetHits();
dffd31ef 1196 }
b9d0a01d 1197}
dffd31ef 1198
e2afb3b6 1199//_______________________________________________________________________
b9d0a01d 1200void AliRun::ResetTrackReferences()
1201{
59fe9bd2 1202 //
b9d0a01d 1203 // Reset all Detectors hits
1204 //
1205 TIter next(fModules);
1206 AliModule *detector;
e2afb3b6 1207 while((detector = dynamic_cast<AliModule*>(next()))) {
b9d0a01d 1208 detector->ResetTrackReferences();
1209 }
1210}
9e1a0ddb 1211
e2afb3b6 1212//_______________________________________________________________________
b9d0a01d 1213void AliRun::ResetPoints()
fe4da5cc 1214{
1215 //
b9d0a01d 1216 // Reset all Detectors points
fe4da5cc 1217 //
8494b010 1218 TIter next(fModules);
1219 AliModule *detector;
e2afb3b6 1220 while((detector = dynamic_cast<AliModule*>(next()))) {
b9d0a01d 1221 detector->ResetPoints();
fe4da5cc 1222 }
1223}
1224
e2afb3b6 1225//_______________________________________________________________________
b9d0a01d 1226void AliRun::InitMC(const char *setup)
2ab0c725 1227{
1228 //
b9d0a01d 1229 // Initialize the Alice setup
2ab0c725 1230 //
2ab0c725 1231
37d06d5b 1232 Announce();
1233
b9d0a01d 1234 if(fInitDone) {
1235 Warning("Init","Cannot initialise AliRun twice!\n");
1236 return;
fe4da5cc 1237 }
b9d0a01d 1238
1239 gROOT->LoadMacro(setup);
1240 gInterpreter->ProcessLine(fConfigFunction.Data());
fe4da5cc 1241
b9d0a01d 1242 // Register MC in configuration
1243 AliConfig::Instance()->Add(gMC);
1244 gMC->SetStack(fStack);
aab9c8d5 1245
b9d0a01d 1246 gMC->DefineParticles(); //Create standard MC particles
141f90e3 1247 AliPDG::AddParticlesToPdgDataBase();
aab9c8d5 1248
b9d0a01d 1249 TObject *objfirst, *objlast;
1250
1251 fNdets = fModules->GetLast()+1;
aab9c8d5 1252
fe4da5cc 1253 //
b9d0a01d 1254 //=================Create Materials and geometry
1255 gMC->Init();
1256
1257 // Added also after in case of interactive initialisation of modules
1258 fNdets = fModules->GetLast()+1;
1259
1260 TIter next(fModules);
1261 AliModule *detector;
e2afb3b6 1262 while((detector = dynamic_cast<AliModule*>(next()))) {
b9d0a01d 1263 detector->SetTreeAddress();
1264 objlast = gDirectory->GetList()->Last();
1265
1266 // Add Detector histograms in Detector list of histograms
1267 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
1268 else objfirst = gDirectory->GetList()->First();
1269 while (objfirst) {
1270 detector->Histograms()->Add(objfirst);
1271 objfirst = gDirectory->GetList()->After(objfirst);
1272 }
1273 }
1274 ReadTransPar(); //Read the cuts for all materials
1275
1276 MediaTable(); //Build the special IMEDIA table
1277
1278 //Initialise geometry deposition table
1279 fEventEnergy.Set(gMC->NofVolumes()+1);
1280 fSummEnergy.Set(gMC->NofVolumes()+1);
1281 fSum2Energy.Set(gMC->NofVolumes()+1);
1282
1283 //Compute cross-sections
1284 gMC->BuildPhysics();
1285
1286 //Write Geometry object to current file.
1287 fGeometry->Write();
1288
1289 fInitDone = kTRUE;
1290
1291 fMCQA = new AliMCQA(fNdets);
1292
1293 AliConfig::Instance();
1294 //
1295 // Save stuff at the beginning of the file to avoid file corruption
1296 Write();
fe4da5cc 1297}
1298
e2afb3b6 1299//_______________________________________________________________________
875c717b 1300void AliRun::RunMC(Int_t nevent, const char *setup)
fe4da5cc 1301{
1302 //
1303 // Main function to be called to process a galice run
1304 // example
1305 // Root > gAlice.Run();
1306 // a positive number of events will cause the finish routine
1307 // to be called
1308 //
6df200c3 1309 fEventsPerRun = nevent;
fe4da5cc 1310 // check if initialisation has been done
875c717b 1311 if (!fInitDone) InitMC(setup);
fe4da5cc 1312
fe4da5cc 1313 // Create the Root Tree with one branch per detector
2ab0c725 1314
b9d0a01d 1315 MakeTree("ESDRT");
d47c658f 1316
2ab0c725 1317 if (gSystem->Getenv("CONFIG_SPLIT_FILE")) {
39de14fb 1318 MakeTree("K","Kine.root");
1319 MakeTree("H","Hits.root");
2ab0c725 1320 } else {
d47c658f 1321 MakeTree("KH");
2ab0c725 1322 }
fe4da5cc 1323
875c717b 1324 gMC->ProcessRun(nevent);
1325
fe4da5cc 1326 // End of this run, close files
80762cb1 1327 if(nevent>0) FinishRun();
fe4da5cc 1328}
1329
e2afb3b6 1330//_______________________________________________________________________
27f087a9 1331void AliRun::RunReco(const char *selected, Int_t first, Int_t last)
d47c658f 1332{
1333 //
1334 // Main function to be called to reconstruct Alice event
27f087a9 1335 //
1336 cout << "Found "<< gAlice->TreeE()->GetEntries() << "events" << endl;
1337 Int_t nFirst = first;
e2afb3b6 1338 Int_t nLast = (last < 0)? static_cast<Int_t>(gAlice->TreeE()->GetEntries()) : last;
27f087a9 1339
1340 for (Int_t nevent = nFirst; nevent <= nLast; nevent++) {
1341 cout << "Processing event "<< nevent << endl;
9e1a0ddb 1342 GetEvent(nevent);
1343 // MakeTree("R");
1344 Digits2Reco(selected);
1345 }
d47c658f 1346}
1347
e2afb3b6 1348//_______________________________________________________________________
2ab0c725 1349
1350void AliRun::Hits2Digits(const char *selected)
1351{
9e1a0ddb 1352
d47c658f 1353 // Convert Hits to sumable digits
1354 //
9e1a0ddb 1355 for (Int_t nevent=0; nevent<gAlice->TreeE()->GetEntries(); nevent++) {
1356 GetEvent(nevent);
1357 // MakeTree("D");
1358 Hits2SDigits(selected);
1359 SDigits2Digits(selected);
1360 }
2ab0c725 1361}
1362
d47c658f 1363
e2afb3b6 1364//_______________________________________________________________________
2ab0c725 1365
d47c658f 1366void AliRun::Tree2Tree(Option_t *option, const char *selected)
2ab0c725 1367{
1368 //
d47c658f 1369 // Function to transform the content of
1370 //
1371 // - TreeH to TreeS (option "S")
1372 // - TreeS to TreeD (option "D")
1373 // - TreeD to TreeR (option "R")
1374 //
1375 // If multiple options are specified ("SDR"), transformation will be done in sequence for
1376 // selected detector and for all detectors if none is selected (detector string
1377 // can contain blank separated list of detector names).
2ab0c725 1378
2ab0c725 1379
5cf7bbad 1380 const char *oS = strstr(option,"S");
1381 const char *oD = strstr(option,"D");
1382 const char *oR = strstr(option,"R");
2ab0c725 1383
9e1a0ddb 1384 TObjArray *detectors = Detectors();
2ab0c725 1385
1386 TIter next(detectors);
1387
d47c658f 1388 AliDetector *detector = 0;
2ab0c725 1389
1390 TDirectory *cwd = gDirectory;
1391
3e4186da 1392 TObject *obj;
1393
d47c658f 1394 char outFile[32];
2ab0c725 1395
3e4186da 1396 while((obj = next())) {
1397 if (!dynamic_cast<AliModule*>(obj))
1398 Fatal("Tree2Tree","Wrong type in fModules array\n");
1399 if (!(detector = dynamic_cast<AliDetector*>(obj))) continue;
d47c658f 1400 if (selected)
2ab0c725 1401 if (strcmp(detector->GetName(),selected)) continue;
3e4186da 1402 if (!detector->IsActive()) continue;
1403 if (gSystem->Getenv("CONFIG_SPLIT_FILE")) {
9e1a0ddb 1404 if (oS) {
3e4186da 1405 sprintf(outFile,"SDigits.%s.root",detector->GetName());
1406 detector->MakeBranch("S",outFile);
1407 }
9e1a0ddb 1408 if (oD) {
3e4186da 1409 sprintf(outFile,"Digits.%s.root",detector->GetName());
1410 detector->MakeBranch("D",outFile);
1411 }
9e1a0ddb 1412 if (oR) {
3e4186da 1413 sprintf(outFile,"Reco.%s.root",detector->GetName());
1414 detector->MakeBranch("R",outFile);
1415 }
1416 } else {
1417 detector->MakeBranch(option);
1418 }
1419
1420 cwd->cd();
1421
1422 if (oS) {
1423 cout << "Hits2SDigits: Processing " << detector->GetName() << "..." << endl;
1424 detector->Hits2SDigits();
1425 }
1426 if (oD) {
1427 cout << "SDigits2Digits: Processing " << detector->GetName() << "..." << endl;
1428 detector->SDigits2Digits();
1429 }
1430 if (oR) {
1431 cout << "Digits2Reco: Processing " << detector->GetName() << "..." << endl;
1432 detector->Digits2Reco();
1433 }
1434
1435 cwd->cd();
1436 }
2ab0c725 1437}
1438
e2afb3b6 1439//_______________________________________________________________________
0a520a66 1440void AliRun::RunLego(const char *setup, Int_t nc1, Float_t c1min,
1441 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
1442 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener)
fe4da5cc 1443{
1444 //
1445 // Generates lego plots of:
1446 // - radiation length map phi vs theta
1447 // - radiation length map phi vs eta
1448 // - interaction length map
1449 // - g/cm2 length map
1450 //
1451 // ntheta bins in theta, eta
1452 // themin minimum angle in theta (degrees)
1453 // themax maximum angle in theta (degrees)
1454 // nphi bins in phi
1455 // phimin minimum angle in phi (degrees)
1456 // phimax maximum angle in phi (degrees)
1457 // rmin minimum radius
1458 // rmax maximum radius
1459 //
1460 //
1461 // The number of events generated = ntheta*nphi
1462 // run input parameters in macro setup (default="Config.C")
1463 //
1464 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
1465 //Begin_Html
1466 /*
1439f98e 1467 <img src="picts/AliRunLego1.gif">
fe4da5cc 1468 */
1469 //End_Html
1470 //Begin_Html
1471 /*
1439f98e 1472 <img src="picts/AliRunLego2.gif">
fe4da5cc 1473 */
1474 //End_Html
1475 //Begin_Html
1476 /*
1439f98e 1477 <img src="picts/AliRunLego3.gif">
fe4da5cc 1478 */
1479 //End_Html
1480 //
1481
1482 // check if initialisation has been done
875c717b 1483 if (!fInitDone) InitMC(setup);
838edcaf 1484 //Save current generator
1485 AliGenerator *gen=Generator();
1486
0a520a66 1487 // Set new generator
1488 if (!gener) gener = new AliLegoGenerator();
1489 ResetGenerator(gener);
1490 //
1491 // Configure Generator
1492 gener->SetRadiusRange(rmin, rmax);
1493 gener->SetZMax(zmax);
1494 gener->SetCoor1Range(nc1, c1min, c1max);
1495 gener->SetCoor2Range(nc2, c2min, c2max);
1496
1497
b13db077 1498 //Create Lego object
0a520a66 1499 fLego = new AliLego("lego",gener);
b13db077 1500
dffd31ef 1501 //Prepare MC for Lego Run
1502 gMC->InitLego();
1503
b13db077 1504 //Run Lego Object
0a520a66 1505
b9d0a01d 1506 //gMC->ProcessRun(nc1*nc2+1);
1507 gMC->ProcessRun(nc1*nc2);
fe4da5cc 1508
1509 // Create only the Root event Tree
80762cb1 1510 MakeTree("E");
fe4da5cc 1511
1512 // End of this run, close files
80762cb1 1513 FinishRun();
0a520a66 1514 // Restore current generator
1515 ResetGenerator(gen);
838edcaf 1516 // Delete Lego Object
1517 delete fLego; fLego=0;
fe4da5cc 1518}
1519
e2afb3b6 1520//_______________________________________________________________________
94de3818 1521void AliRun::SetConfigFunction(const char * config)
1522{
1523 //
1524 // Set the signature of the function contained in Config.C to configure
1525 // the run
1526 //
1527 fConfigFunction=config;
1528}
1529
e2afb3b6 1530//_______________________________________________________________________
fe4da5cc 1531void AliRun::SetCurrentTrack(Int_t track)
1532{
1533 //
1534 // Set current track number
1535 //
9e1a0ddb 1536 fStack->SetCurrentTrack(track);
fe4da5cc 1537}
1538
e2afb3b6 1539//_______________________________________________________________________
1578254f 1540void AliRun::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
fe4da5cc 1541 Float_t *vpos, Float_t *polar, Float_t tof,
98490ea9 1542 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
fe4da5cc 1543{
9e1a0ddb 1544// Delegate to stack
1545//
b9d0a01d 1546
1547 fStack->SetTrack(done, parent, pdg, pmom, vpos, polar, tof,
1548 mech, ntr, weight, is);
fe4da5cc 1549}
1550
e2afb3b6 1551//_______________________________________________________________________
89bbad6f 1552void AliRun::SetTrack(Int_t done, Int_t parent, Int_t pdg,
1553 Double_t px, Double_t py, Double_t pz, Double_t e,
1554 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
1555 Double_t polx, Double_t poly, Double_t polz,
98490ea9 1556 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
89bbad6f 1557{
9e1a0ddb 1558 // Delegate to stack
89bbad6f 1559 //
9e1a0ddb 1560 fStack->SetTrack(done, parent, pdg, px, py, pz, e, vx, vy, vz, tof,
47c8bcbe 1561 polx, poly, polz, mech, ntr, weight, is);
9e1a0ddb 1562
89bbad6f 1563}
1564
e2afb3b6 1565//_______________________________________________________________________
4d69d91e 1566void AliRun::SetHighWaterMark(const Int_t nt)
1567{
1568 //
1569 // Set high water mark for last track in event
9e1a0ddb 1570 fStack->SetHighWaterMark(nt);
4d69d91e 1571}
1572
e2afb3b6 1573//_______________________________________________________________________
fe4da5cc 1574void AliRun::KeepTrack(const Int_t track)
1575{
1576 //
9e1a0ddb 1577 // Delegate to stack
fe4da5cc 1578 //
9e1a0ddb 1579 fStack->KeepTrack(track);
fe4da5cc 1580}
1581
b9d0a01d 1582//
1583// MC Application
1584//
1585
e2afb3b6 1586//_______________________________________________________________________
b9d0a01d 1587void AliRun::ConstructGeometry()
1588{
1589 //
1590 // Create modules, materials, geometry
1591 //
1592
1593 TStopwatch stw;
1594 TIter next(fModules);
1595 AliModule *detector;
1596 printf("Geometry creation:\n");
e2afb3b6 1597 while((detector = dynamic_cast<AliModule*>(next()))) {
b9d0a01d 1598 stw.Start();
1599 // Initialise detector materials and geometry
1600 detector->CreateMaterials();
1601 detector->CreateGeometry();
1602 printf("%10s R:%.2fs C:%.2fs\n",
1603 detector->GetName(),stw.RealTime(),stw.CpuTime());
1604 }
1605}
1606
e2afb3b6 1607//_______________________________________________________________________
b9d0a01d 1608void AliRun::InitGeometry()
1609{
1610 //
1611 // Initialize detectors and display geometry
1612 //
1613
1614 printf("Initialisation:\n");
1615 TStopwatch stw;
1616 TIter next(fModules);
1617 AliModule *detector;
e2afb3b6 1618 while((detector = dynamic_cast<AliModule*>(next()))) {
b9d0a01d 1619 stw.Start();
1620 // Initialise detector and display geometry
1621 detector->Init();
1622 detector->BuildGeometry();
1623 printf("%10s R:%.2fs C:%.2fs\n",
1624 detector->GetName(),stw.RealTime(),stw.CpuTime());
1625 }
1626
1627}
1628
e2afb3b6 1629//_______________________________________________________________________
b9d0a01d 1630void AliRun::GeneratePrimaries()
1631{
1632 //
1633 // Generate primary particles and fill them in the stack.
1634 //
1635
1636 Generator()->Generate();
1637}
1638
e2afb3b6 1639//_______________________________________________________________________
b9d0a01d 1640void AliRun::BeginEvent()
1641{
1642 // Clean-up previous event
1643 // Energy scores
1644 fEventEnergy.Reset();
1645 // Clean detector information
1646 CleanDetectors();
1647 // Reset stack info
1648 fStack->Reset();
1649
1650
1651 //
1652 // Reset all Detectors & kinematics & trees
1653 //
1654 char hname[30];
1655 //
1656 // Initialise event header
1657 fHeader->Reset(fRun,fEvent,fEventNrInRun);
1658 //
1659 fStack->BeginEvent(fEvent);
1660
1661 //
1662 if(fLego) {
1663 fLego->BeginEvent();
1664 return;
1665 }
1666
1667 //
1668
1669 ResetHits();
1670 ResetTrackReferences();
1671 ResetDigits();
1672 ResetSDigits();
1673
1674
1675 if(fTreeH) {
1676 fTreeH->Reset();
1677 sprintf(hname,"TreeH%d",fEvent);
1678 fTreeH->SetName(hname);
1679 }
1680
1681 if(fTreeTR) {
1682 fTreeTR->Reset();
1683 sprintf(hname,"TreeTR%d",fEvent);
1684 fTreeTR->SetName(hname);
1685 }
1686
1687 if(fTreeD) {
1688 fTreeD->Reset();
1689 sprintf(hname,"TreeD%d",fEvent);
1690 fTreeD->SetName(hname);
1691 fTreeD->Write(0,TObject::kOverwrite);
1692 }
1693 if(fTreeS) {
1694 fTreeS->Reset();
1695 sprintf(hname,"TreeS%d",fEvent);
1696 fTreeS->SetName(hname);
1697 fTreeS->Write(0,TObject::kOverwrite);
1698 }
1699 if(fTreeR) {
1700 fTreeR->Reset();
1701 sprintf(hname,"TreeR%d",fEvent);
1702 fTreeR->SetName(hname);
1703 fTreeR->Write(0,TObject::kOverwrite);
1704 }
1705}
1706
e2afb3b6 1707//_______________________________________________________________________
b9d0a01d 1708void AliRun::BeginPrimary()
1709{
1710 //
1711 // Called at the beginning of each primary track
1712 //
1713
1714 // Reset Hits info
1715 gAlice->ResetHits();
1716 gAlice->ResetTrackReferences();
1717
1718}
1719
e2afb3b6 1720//_______________________________________________________________________
b9d0a01d 1721void AliRun::PreTrack()
1722{
116cbefd 1723 //
1724 // Method called before each track
1725 //
1726 TObjArray &dets = *fModules;
1727 AliModule *module;
1728
1729 for(Int_t i=0; i<=fNdets; i++)
1730 if((module = dynamic_cast<AliModule*>(dets[i])))
1731 module->PreTrack();
1732
1733 fMCQA->PreTrack();
b9d0a01d 1734}
1735
e2afb3b6 1736//_______________________________________________________________________
b9d0a01d 1737void AliRun::Stepping()
fe4da5cc 1738{
1739 //
1740 // Called at every step during transport
1741 //
1742
b9d0a01d 1743 Int_t id = DetFromMate(gMC->GetMedium());
1744 if (id < 0) return;
1745
fe4da5cc 1746 //
1747 // --- If lego option, do it and leave
b13db077 1748 if (fLego)
fe4da5cc 1749 fLego->StepManager();
b13db077 1750 else {
1751 Int_t copy;
1752 //Update energy deposition tables
875c717b 1753 AddEnergyDeposit(gMC->CurrentVolID(copy),gMC->Edep());
fe4da5cc 1754
b13db077 1755 //Call the appropriate stepping routine;
e2afb3b6 1756 AliModule *det = dynamic_cast<AliModule*>(fModules->At(id));
3f754d83 1757 if(det && det->StepManagerIsEnabled()) {
65fb704d 1758 fMCQA->StepManager(id);
1759 det->StepManager();
1760 }
fe4da5cc 1761 }
fe4da5cc 1762}
1763
e2afb3b6 1764//_______________________________________________________________________
b9d0a01d 1765void AliRun::PostTrack()
1766{
116cbefd 1767 //
1768 // Called after a track has been trasported
1769 //
1770 TObjArray &dets = *fModules;
1771 AliModule *module;
1772
1773 for(Int_t i=0; i<=fNdets; i++)
1774 if((module = dynamic_cast<AliModule*>(dets[i])))
1775 module->PostTrack();
b9d0a01d 1776}
1777
e2afb3b6 1778//_______________________________________________________________________
b9d0a01d 1779void AliRun::FinishPrimary()
1780{
1781 //
1782 // Called at the end of each primary track
1783 //
1784
1785 // static Int_t count=0;
1786 // const Int_t times=10;
1787 // This primary is finished, purify stack
1788 fStack->PurifyKine();
1789
1790 TIter next(fModules);
1791 AliModule *detector;
e2afb3b6 1792 while((detector = dynamic_cast<AliModule*>(next()))) {
b9d0a01d 1793 detector->FinishPrimary();
1794 }
1795
1796 // Write out hits if any
1797 if (gAlice->TreeH()) {
1798 gAlice->TreeH()->Fill();
1799 }
1800
1801 // Write out hits if any
1802 if (gAlice->TreeTR()) {
1803 gAlice->TreeTR()->Fill();
1804 }
1805
1806 //
1807 // if(++count%times==1) gObjectTable->Print();
1808}
1809
e2afb3b6 1810//_______________________________________________________________________
b9d0a01d 1811void AliRun::FinishEvent()
1812{
1813 //
1814 // Called at the end of the event.
1815 //
1816
1817 //
1818 if(fLego) fLego->FinishEvent();
1819
1820 //Update the energy deposit tables
1821 Int_t i;
1822 for(i=0;i<fEventEnergy.GetSize();i++) {
1823 fSummEnergy[i]+=fEventEnergy[i];
1824 fSum2Energy[i]+=fEventEnergy[i]*fEventEnergy[i];
1825 }
1826
1827
1828
1829 // Update Header information
1830
1831 fHeader->SetNprimary(fStack->GetNprimary());
1832 fHeader->SetNtrack(fStack->GetNtrack());
1833
1834
1835 // Write out the kinematics
1836 fStack->FinishEvent();
1837
1838 // Write out the event Header information
1839 if (fTreeE) {
1840 fHeader->SetStack(fStack);
1841 fTreeE->Fill();
1842 }
1843
1844
1845 // Write Tree headers
1846 TTree* pTreeK = fStack->TreeK();
1847 if (pTreeK) pTreeK->Write(0,TObject::kOverwrite);
1848 if (fTreeH) fTreeH->Write(0,TObject::kOverwrite);
1849 if (fTreeTR) fTreeTR->Write(0,TObject::kOverwrite);
1850
1851 ++fEvent;
1852 ++fEventNrInRun;
1853}
1854
e2afb3b6 1855//_______________________________________________________________________
b9d0a01d 1856void AliRun::Field(const Double_t* x, Double_t *b) const
1857{
116cbefd 1858 //
1859 // Returns the magnetic field at point x[3]
1860 // Units are kGauss
1861 //
1862 Float_t xfloat[3];
1863 for (Int_t i=0; i<3; i++) xfloat[i] = x[i];
1864
1865 if (Field()) {
1866 Float_t bfloat[3];
1867 Field()->Field(xfloat,bfloat);
1868 for (Int_t j=0; j<3; j++) b[j] = bfloat[j];
1869 }
1870 else {
1871 printf("No mag field defined!\n");
1872 b[0]=b[1]=b[2]=0.;
1873 }
b9d0a01d 1874
1875}
1876
1877//
1878// End of MC Application
1879//
1880
e2afb3b6 1881//_______________________________________________________________________
fe4da5cc 1882void AliRun::Streamer(TBuffer &R__b)
1883{
eef4b160 1884 // Stream an object of class AliRun.
2ab0c725 1885
7e90ff59 1886 if (R__b.IsReading()) {
1887 if (!gAlice) gAlice = this;
eef4b160 1888
7e90ff59 1889 AliRun::Class()->ReadBuffer(R__b, this);
1890 //
1891 gROOT->GetListOfBrowsables()->Add(this,"Run");
eef4b160 1892
e2afb3b6 1893 fTreeE = dynamic_cast<TTree*>(gDirectory->Get("TE"));
7e90ff59 1894 if (fTreeE) {
eef4b160 1895 fTreeE->SetBranchAddress("Header", &fHeader);
b9d0a01d 1896 }
7e90ff59 1897 else Error("Streamer","cannot find Header Tree\n");
b9d0a01d 1898
7e90ff59 1899 fTreeE->GetEntry(0);
7e90ff59 1900 gRandom = fRandom;
1901 } else {
eef4b160 1902 AliRun::Class()->WriteBuffer(R__b, this);
1903 }
2ab0c725 1904}
1905
9e1a0ddb 1906
e2afb3b6 1907//_______________________________________________________________________
9e1a0ddb 1908Int_t AliRun::CurrentTrack() const {
1909 //
1910 // Returns current track
1911 //
1912 return fStack->CurrentTrack();
1913}
1914
e2afb3b6 1915//_______________________________________________________________________
9e1a0ddb 1916Int_t AliRun::GetNtrack() const {
1917 //
1918 // Returns number of tracks in stack
1919 //
1920 return fStack->GetNtrack();
1921}
1922
e2afb3b6 1923//_______________________________________________________________________
37d06d5b 1924TObjArray* AliRun::Particles() const {
9e1a0ddb 1925 //
1926 // Returns pointer to Particles array
1927 //
1928 return fStack->Particles();
1929}
1930
e2afb3b6 1931//_______________________________________________________________________
37d06d5b 1932TTree* AliRun::TreeK() const {
9e1a0ddb 1933 //
1934 // Returns pointer to the TreeK array
1935 //
1936 return fStack->TreeK();
1937}
27f087a9 1938
b9d0a01d 1939
e2afb3b6 1940//_______________________________________________________________________
27f087a9 1941void AliRun::SetGenEventHeader(AliGenEventHeader* header)
1942{
1943 fHeader->SetGenEventHeader(header);
1944}
b9d0a01d 1945
e2afb3b6 1946//_______________________________________________________________________
7a16e9cc 1947TFile* AliRun::InitFile(TString fileName)
1948{
1949//
1950// create the file where the whole tree will be saved
1951//
1952 TDirectory *wd = gDirectory;
1953 TFile* file = TFile::Open(fileName,"update");
1954 gDirectory = wd;
1955 if (!file->IsOpen()) {
1956 Error("Cannot open file, %s\n",fileName);
1957 return 0;
1958 }
1959 return file;
1960}
1961
e2afb3b6 1962//_______________________________________________________________________
7a16e9cc 1963TFile* AliRun::InitTreeFile(Option_t *option, TString fileName)
1964{
1965 //
1966 // create the file where one of the following trees will be saved
1967 // trees: S,D,R
0592d1ca 1968 // WARNING: by default these trees are saved on the file on which
1969 // hits are stored. If you divert one of these trees, you cannot restore
1970 // it to the original file (usually galice.root) in the same aliroot session
7a16e9cc 1971 Bool_t oS = (strstr(option,"S")!=0);
1972 Bool_t oR = (strstr(option,"R")!=0);
1973 Bool_t oD = (strstr(option,"D")!=0);
0592d1ca 1974 Int_t choice[3];
1975 for (Int_t i=0; i<3; i++) choice[i] = 0;
1976 if(oS)choice[0] = 1;
1977 if(oD)choice[1] = 1;
1978 if(oR)choice[2] = 1;
1979
7a16e9cc 1980 TFile *ptr=0;
0592d1ca 1981
1982 if(!(oS || oR || oD))return ptr;
1983
1984 Int_t active[3];
1985 for (Int_t i=0; i<3; i++) active[i] = 0;
1986 if(fTreeSFileName != "") active[0] = 1;
1987 if(fTreeDFileName != "") active[1] = 1;
1988 if(fTreeDFileName != "") active[2] = 1;
1989
1990 Bool_t alreadyopen1 = kFALSE;
1991 Bool_t alreadyopen2 = kFALSE;
1992
1993 if(oS){
1994 // if already active and same name with non-null ptr
1995 if(active[0]==1 && fileName == fTreeSFileName && fTreeSFile){
1996 Warning("InitTreeFile","File %s already opened",fTreeSFileName.Data());
7a16e9cc 1997 ptr = fTreeSFile;
1998 }
0592d1ca 1999 else {
2000 // if already active with different name with non-null ptr
2001 if(active[0]==1 && fileName != fTreeSFileName && fTreeSFile){
2002 // close the active files and also the other possible files in option
2003 CloseTreeFile(option);
2004 }
2005 fTreeSFileName = fileName;
2006 alreadyopen1 =
2007 (active[1] == 1 && fTreeDFileName == fTreeSFileName && fTreeDFile);
2008 alreadyopen2 =
2009 (active[2] == 1 && fTreeRFileName == fTreeSFileName && fTreeRFile);
2010 if(!(alreadyopen1 || alreadyopen2)){
2011 ptr = InitFile(fileName);
2012 fTreeSFile = ptr;
7a16e9cc 2013 }
0592d1ca 2014 else {
2015 if(alreadyopen1){fTreeSFile = fTreeDFile; ptr = fTreeSFile;}
2016 if(alreadyopen2){fTreeSFile = fTreeRFile; ptr = fTreeSFile;}
2017 }
2018 if(choice[1] == 1) { fTreeDFileName = fileName; fTreeDFile = ptr;}
2019 if(choice[2] == 1) { fTreeRFileName = fileName; fTreeRFile = ptr;}
2020 }
2021 return ptr;
2022 }
2023
2024 if(oD){
2025 // if already active and same name with non-null ptr
2026 if(active[1]==1 && fileName == fTreeDFileName && fTreeDFile){
2027 Warning("InitTreeFile","File %s already opened",fTreeDFileName.Data());
7a16e9cc 2028 ptr = fTreeDFile;
2029 }
0592d1ca 2030 else {
2031 // if already active with different name with non-null ptr
2032 if(active[1]==1 && fileName != fTreeDFileName && fTreeDFile){
2033 // close the active files and also the other possible files in option
2034 CloseTreeFile(option);
2035 }
2036 fTreeDFileName = fileName;
2037 alreadyopen1 =
2038 (active[0] == 1 && fTreeSFileName == fTreeDFileName && fTreeSFile);
2039 alreadyopen2 =
2040 (active[2] == 1 && fTreeRFileName == fTreeDFileName && fTreeRFile);
2041 if(!(alreadyopen1 || alreadyopen2)){
2042 ptr = InitFile(fileName);
2043 fTreeDFile = ptr;
2044 }
2045 else {
2046 if(alreadyopen1){fTreeDFile = fTreeSFile; ptr = fTreeDFile;}
2047 if(alreadyopen2){fTreeDFile = fTreeRFile; ptr = fTreeDFile;}
7a16e9cc 2048 }
0592d1ca 2049 if(choice[2] == 1) { fTreeRFileName = fileName; fTreeRFile = ptr;}
2050 }
2051 return ptr;
2052 }
2053
2054 if(oR){
2055 // if already active and same name with non-null ptr
2056 if(active[2]==1 && fileName == fTreeRFileName && fTreeRFile){
2057 Warning("InitTreeFile","File %s already opened",fTreeRFileName.Data());
7a16e9cc 2058 ptr = fTreeRFile;
2059 }
0592d1ca 2060 else {
2061 // if already active with different name with non-null ptr
2062 if(active[2]==1 && fileName != fTreeRFileName && fTreeRFile){
2063 // close the active files and also the other possible files in option
2064 CloseTreeFile(option);
2065 }
2066 fTreeRFileName = fileName;
2067 alreadyopen1 =
2068 (active[1] == 1 && fTreeDFileName == fTreeRFileName && fTreeDFile);
2069 alreadyopen2 =
2070 (active[0]== 1 && fTreeSFileName == fTreeRFileName && fTreeSFile);
2071 if(!(alreadyopen1 || alreadyopen2)){
2072 ptr = InitFile(fileName);
2073 fTreeRFile = ptr;
2074 }
2075 else {
2076 if(alreadyopen1){fTreeRFile = fTreeDFile; ptr = fTreeRFile;}
2077 if(alreadyopen2){fTreeRFile = fTreeSFile; ptr = fTreeRFile;}
2078 }
2079 }
2080 return ptr;
7a16e9cc 2081 }
b16a1b1e 2082 return 0;
0592d1ca 2083}
b9d0a01d 2084
e2afb3b6 2085//_______________________________________________________________________
b9d0a01d 2086void AliRun::PrintTreeFile()
2087{
2088 //
2089 // prints the file names and pointer associated to S,D,R trees
2090 //
2091 cout<<"===================================================\n";
2092 TFile *file = fTreeE->GetCurrentFile();
2093 TString curfilname="";
e2afb3b6 2094 if(file)curfilname=static_cast<TString>(file->GetName());
b9d0a01d 2095 cout<<" Current tree file name: "<<curfilname<<endl;
2096 cout<<"Pointer: "<<file<<endl;
2097 cout<<" Tree S File name: "<<fTreeSFileName<<endl;
2098 cout<<"Pointer: "<<fTreeSFile<<endl<<endl;
2099 cout<<" Tree D File name: "<<fTreeDFileName<<endl;
2100 cout<<"Pointer: "<<fTreeDFile<<endl<<endl;
2101 cout<<" Tree R File name: "<<fTreeRFileName<<endl;
2102 cout<<"Pointer: "<<fTreeRFile<<endl<<endl;
2103 cout<<"===================================================\n";
2104}
e2afb3b6 2105//_______________________________________________________________________
0592d1ca 2106void AliRun::CloseTreeFile(Option_t *option)
2107{
2108 //
2109 // closes the file containing the tree specified in option
2110 // (S,D,R)
2111 //
2112 Bool_t oS = (strstr(option,"S")!=0);
2113 Bool_t oR = (strstr(option,"R")!=0);
2114 Bool_t oD = (strstr(option,"D")!=0);
2115 Bool_t none = !(oS || oR || oD);
2116 if(none)return;
2117 if(oS){
2118 fTreeSFileName = "";
2119 if(fTreeSFile){
2120 if(!((fTreeSFile == fTreeDFile) || (fTreeSFile == fTreeRFile)) &&
2121 fTreeSFile->IsOpen()){
2122 fTreeSFile->Close();
2123 delete fTreeSFile;
2124 }
2125 }
2126 fTreeSFile = 0;
2127 }
2128 if(oD){
2129 fTreeDFileName = "";
2130 if(fTreeDFile){
2131 if(!((fTreeDFile == fTreeRFile) || (fTreeDFile == fTreeSFile)) &&
2132 fTreeDFile->IsOpen()){
2133 fTreeDFile->Close();
2134 delete fTreeDFile;
2135 }
2136 }
2137 fTreeDFile = 0;
2138 }
2139 if(oR){
2140 fTreeRFileName = "";
2141 if(fTreeRFile){
2142 if(!((fTreeRFile == fTreeSFile) || (fTreeRFile == fTreeDFile)) &&
2143 fTreeRFile->IsOpen()){
2144 fTreeRFile->Close();
2145 delete fTreeRFile;
2146 }
2147 }
2148 fTreeRFile = 0;
2149 }
2150}
2151
e2afb3b6 2152//_______________________________________________________________________
7a16e9cc 2153void AliRun::MakeTree(Option_t *option, TFile *file)
2154{
2155 //
2156 // Create some trees in the separate file
2157 //
2158 const char *oD = strstr(option,"D");
2159 const char *oR = strstr(option,"R");
2160 const char *oS = strstr(option,"S");
2161
2162 TDirectory *cwd = gDirectory;
2163 char hname[30];
2164
2165 if (oD) {
2166 delete fTreeD;
2167 sprintf(hname,"TreeD%d",fEvent);
2168 file->cd();
2169 fTreeD = static_cast<TTree*>(file->Get("hname"));
2170 if (!fTreeD) {
2171 fTreeD = new TTree(hname,"Digits");
2172 fTreeD->Write(0,TObject::kOverwrite);
2173 }
2174 cwd->cd();
2175 }
2176 if (oS) {
2177 delete fTreeS;
2178 sprintf(hname,"TreeS%d",fEvent);
2179 file->cd();
2180 fTreeS = static_cast<TTree*>(file->Get("hname"));
2181 if (!fTreeS) {
2182 fTreeS = new TTree(hname,"SDigits");
2183 fTreeS->Write(0,TObject::kOverwrite);
2184 }
2185 cwd->cd();
2186 }
2187
2188 if (oR) {
2189 delete fTreeR;
2190 sprintf(hname,"TreeR%d",fEvent);
2191 file->cd();
2192 fTreeR = static_cast<TTree*>(file->Get("hname"));
2193 if (!fTreeR) {
2194 fTreeR = new TTree(hname,"RecPoint");
2195 fTreeR->Write(0,TObject::kOverwrite);
2196 }
2197 cwd->cd();
2198 }
2199}