]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliRun.cxx
New code from Piergiorgio added
[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
16/*
17$Log$
ef42d733 18Revision 1.40 2000/07/12 08:56:25 fca
19Coding convention correction and warning removal
20
8918e700 21Revision 1.39 2000/07/11 18:24:59 fca
22Coding convention corrections + few minor bug fixes
23
aee8290b 24Revision 1.38 2000/06/20 13:05:45 fca
25Writing down the TREE headers before job starts
26
51e0e89d 27Revision 1.37 2000/06/09 20:05:11 morsch
28Introduce possibility to chose magnetic field version 3: AliMagFDM + field02.dat
29
f1b9d7c3 30Revision 1.36 2000/06/08 14:03:58 hristov
31Only one initializer for a default argument
32
d33c0226 33Revision 1.35 2000/06/07 10:13:14 hristov
34Delete only existent objects.
35
d2ecd553 36Revision 1.34 2000/05/18 10:45:38 fca
37Delete Particle Factory properly
38
c222d2b0 39Revision 1.33 2000/05/16 13:10:40 fca
40New method IsNewTrack and fix for a problem in Father-Daughter relations
41
a01a8b12 42Revision 1.32 2000/04/27 10:38:21 fca
43Correct termination of Lego Run and introduce Lego getter in AliRun
44
838edcaf 45Revision 1.31 2000/04/26 10:17:32 fca
46Changes in Lego for G4 compatibility
47
dffd31ef 48Revision 1.30 2000/04/18 19:11:40 fca
49Introduce variable Config.C function signature
50
45189757 51Revision 1.29 2000/04/07 11:12:34 fca
52G4 compatibility changes
53
875c717b 54Revision 1.28 2000/04/05 06:51:06 fca
55Workaround for an HP compiler problem
56
5eb58812 57Revision 1.27 2000/03/22 18:08:07 fca
58Rationalisation of the virtual MC interfaces
59
80762cb1 60Revision 1.26 2000/03/22 13:42:26 fca
61SetGenerator does not replace an existing generator, ResetGenerator does
62
ee1dd322 63Revision 1.25 2000/02/23 16:25:22 fca
64AliVMC and AliGeant3 classes introduced
65ReadEuclid moved from AliRun to AliModule
66
b13db077 67Revision 1.24 2000/01/19 17:17:20 fca
68Introducing a list of lists of hits -- more hits allowed for detector now
69
1cedd08a 70Revision 1.23 1999/12/03 11:14:31 fca
71Fixing previous wrong checking
72
00719c1b 73Revision 1.21 1999/11/25 10:40:08 fca
74Fixing daughters information also in primary tracks
75
ae23d366 76Revision 1.20 1999/10/04 18:08:49 fca
77Adding protection against inconsistent Euclid files
78
3fcc96a1 79Revision 1.19 1999/09/29 07:50:40 fca
80Introduction of the Copyright and cvs Log
81
99d554c8 82*/
83
fe4da5cc 84///////////////////////////////////////////////////////////////////////////////
85// //
86// Control class for Alice C++ //
87// Only one single instance of this class exists. //
88// The object is created in main program aliroot //
89// and is pointed by the global gAlice. //
90// //
8494b010 91// -Supports the list of all Alice Detectors (fModules). //
fe4da5cc 92// -Supports the list of particles (fParticles). //
93// -Supports the Trees. //
94// -Supports the geometry. //
95// -Supports the event display. //
96//Begin_Html
97/*
1439f98e 98<img src="picts/AliRunClass.gif">
fe4da5cc 99*/
100//End_Html
101//Begin_Html
102/*
1439f98e 103<img src="picts/alirun.gif">
fe4da5cc 104*/
105//End_Html
106// //
107///////////////////////////////////////////////////////////////////////////////
108
109#include <TFile.h>
110#include <TRandom.h>
111#include <TBRIK.h>
112#include <TNode.h>
fe4da5cc 113#include <TCint.h>
114#include <TSystem.h>
a8f1fb7c 115#include <TObjectTable.h>
fe4da5cc 116
1578254f 117#include "TParticle.h"
fe4da5cc 118#include "AliRun.h"
fe4da5cc 119#include "AliDisplay.h"
875c717b 120#include "AliMC.h"
dffd31ef 121#include "AliLego.h"
aee8290b 122#include "AliMagFC.h"
123#include "AliMagFCM.h"
124#include "AliMagFDM.h"
fe4da5cc 125
fe4da5cc 126#include <stdlib.h>
127#include <stdio.h>
128#include <string.h>
129
130AliRun *gAlice;
131
aee8290b 132static AliHeader *gAliHeader;
fe4da5cc 133
fe4da5cc 134ClassImp(AliRun)
135
136//_____________________________________________________________________________
137AliRun::AliRun()
138{
139 //
140 // Default constructor for AliRun
141 //
aee8290b 142 gAliHeader=&fHeader;
fe4da5cc 143 fRun = 0;
144 fEvent = 0;
145 fCurrent = -1;
8494b010 146 fModules = 0;
fe4da5cc 147 fGenerator = 0;
148 fTreeD = 0;
149 fTreeK = 0;
150 fTreeH = 0;
151 fTreeE = 0;
152 fTreeR = 0;
153 fParticles = 0;
154 fGeometry = 0;
155 fDisplay = 0;
156 fField = 0;
875c717b 157 fMC = 0;
fe4da5cc 158 fNdets = 0;
159 fImedia = 0;
160 fTrRmax = 1.e10;
161 fTrZmax = 1.e10;
fe4da5cc 162 fInitDone = kFALSE;
163 fLego = 0;
1578254f 164 fPDGDB = 0; //Particle factory object!
1cedd08a 165 fHitLists = 0;
45189757 166 fConfigFunction = "\0";
fe4da5cc 167}
168
169//_____________________________________________________________________________
170AliRun::AliRun(const char *name, const char *title)
171 : TNamed(name,title)
172{
173 //
174 // Constructor for the main processor.
175 // Creates the geometry
176 // Creates the list of Detectors.
177 // Creates the list of particles.
178 //
179 Int_t i;
180
181 gAlice = this;
182 fTreeD = 0;
183 fTreeK = 0;
184 fTreeH = 0;
185 fTreeE = 0;
186 fTreeR = 0;
187 fTrRmax = 1.e10;
188 fTrZmax = 1.e10;
1141f8e4 189 fGenerator = 0;
fe4da5cc 190 fInitDone = kFALSE;
191 fLego = 0;
192 fField = 0;
45189757 193 fConfigFunction = "Config();";
fe4da5cc 194
195 gROOT->GetListOfBrowsables()->Add(this,name);
196 //
197 // create the support list for the various Detectors
8494b010 198 fModules = new TObjArray(77);
fe4da5cc 199 //
200 // Create the TNode geometry for the event display
201
202 BuildSimpleGeometry();
203
204
205 fNtrack=0;
206 fHgwmk=0;
207 fCurrent=-1;
aee8290b 208 gAliHeader=&fHeader;
fe4da5cc 209 fRun = 0;
210 fEvent = 0;
211 //
212 // Create the particle stack
1578254f 213 fParticles = new TClonesArray("TParticle",100);
fe4da5cc 214
215 fDisplay = 0;
216 //
217 // Create default mag field
218 SetField();
219 //
875c717b 220 fMC = gMC;
fe4da5cc 221 //
222 // Prepare the tracking medium lists
223 fImedia = new TArrayI(1000);
224 for(i=0;i<1000;i++) (*fImedia)[i]=-99;
1578254f 225 //
226 // Make particles
227 fPDGDB = TDatabasePDG::Instance(); //Particle factory object!
1cedd08a 228 //
229 // Create HitLists list
230 fHitLists = new TList();
fe4da5cc 231}
232
aee8290b 233
fe4da5cc 234//_____________________________________________________________________________
235AliRun::~AliRun()
236{
237 //
238 // Defaullt AliRun destructor
239 //
fe4da5cc 240 delete fImedia;
241 delete fField;
875c717b 242 delete fMC;
fe4da5cc 243 delete fGeometry;
244 delete fDisplay;
245 delete fGenerator;
246 delete fLego;
247 delete fTreeD;
248 delete fTreeK;
249 delete fTreeH;
250 delete fTreeE;
251 delete fTreeR;
8494b010 252 if (fModules) {
253 fModules->Delete();
254 delete fModules;
fe4da5cc 255 }
256 if (fParticles) {
257 fParticles->Delete();
258 delete fParticles;
259 }
1cedd08a 260 delete fHitLists;
c222d2b0 261 delete fPDGDB;
fe4da5cc 262}
263
264//_____________________________________________________________________________
265void AliRun::AddHit(Int_t id, Int_t track, Int_t *vol, Float_t *hits) const
266{
267 //
268 // Add a hit to detector id
269 //
8494b010 270 TObjArray &dets = *fModules;
271 if(dets[id]) ((AliModule*) dets[id])->AddHit(track,vol,hits);
fe4da5cc 272}
273
274//_____________________________________________________________________________
275void AliRun::AddDigit(Int_t id, Int_t *tracks, Int_t *digits) const
276{
277 //
278 // Add digit to detector id
279 //
8494b010 280 TObjArray &dets = *fModules;
281 if(dets[id]) ((AliModule*) dets[id])->AddDigit(tracks,digits);
fe4da5cc 282}
283
284//_____________________________________________________________________________
285void AliRun::Browse(TBrowser *b)
286{
287 //
288 // Called when the item "Run" is clicked on the left pane
289 // of the Root browser.
290 // It displays the Root Trees and all detectors.
291 //
292 if (fTreeK) b->Add(fTreeK,fTreeK->GetName());
293 if (fTreeH) b->Add(fTreeH,fTreeH->GetName());
294 if (fTreeD) b->Add(fTreeD,fTreeD->GetName());
295 if (fTreeE) b->Add(fTreeE,fTreeE->GetName());
296 if (fTreeR) b->Add(fTreeR,fTreeR->GetName());
297
8494b010 298 TIter next(fModules);
299 AliModule *detector;
300 while((detector = (AliModule*)next())) {
fe4da5cc 301 b->Add(detector,detector->GetName());
302 }
303}
304
305//_____________________________________________________________________________
306void AliRun::Build()
307{
308 //
309 // Initialize Alice geometry
310 // Dummy routine
311 //
312}
313
314//_____________________________________________________________________________
315void AliRun::BuildSimpleGeometry()
316{
317 //
318 // Create a simple TNode geometry used by Root display engine
319 //
320 // Initialise geometry
321 //
322 fGeometry = new TGeometry("AliceGeom","Galice Geometry for Hits");
323 new TMaterial("void","Vacuum",0,0,0); //Everything is void
324 TBRIK *brik = new TBRIK("S_alice","alice volume","void",2000,2000,3000);
325 brik->SetVisibility(0);
326 new TNode("alice","alice","S_alice");
327}
328
329//_____________________________________________________________________________
330void AliRun::CleanDetectors()
331{
332 //
333 // Clean Detectors at the end of event
334 //
8494b010 335 TIter next(fModules);
336 AliModule *detector;
337 while((detector = (AliModule*)next())) {
fe4da5cc 338 detector->FinishEvent();
339 }
340}
341
342//_____________________________________________________________________________
343void AliRun::CleanParents()
344{
345 //
346 // Clean Particles stack.
1578254f 347 // Set parent/daughter relations
fe4da5cc 348 //
349 TClonesArray &particles = *(gAlice->Particles());
1578254f 350 TParticle *part;
fe4da5cc 351 int i;
352 for(i=0; i<fNtrack; i++) {
1578254f 353 part = (TParticle *)particles.UncheckedAt(i);
aee8290b 354 if(!part->TestBit(kDaughtersBit)) {
1578254f 355 part->SetFirstDaughter(-1);
356 part->SetLastDaughter(-1);
fe4da5cc 357 }
358 }
359}
360
361//_____________________________________________________________________________
362Int_t AliRun::DistancetoPrimitive(Int_t, Int_t)
363{
364 //
365 // Return the distance from the mouse to the AliRun object
366 // Dummy routine
367 //
368 return 9999;
369}
370
371//_____________________________________________________________________________
372void AliRun::DumpPart (Int_t i)
373{
374 //
375 // Dumps particle i in the stack
376 //
377 TClonesArray &particles = *fParticles;
1578254f 378 ((TParticle*) particles[i])->Print();
fe4da5cc 379}
380
381//_____________________________________________________________________________
382void AliRun::DumpPStack ()
383{
384 //
385 // Dumps the particle stack
386 //
387 TClonesArray &particles = *fParticles;
388 printf(
389 "\n\n=======================================================================\n");
390 for (Int_t i=0;i<fNtrack;i++)
391 {
1578254f 392 printf("-> %d ",i); ((TParticle*) particles[i])->Print();
fe4da5cc 393 printf("--------------------------------------------------------------\n");
394 }
395 printf(
396 "\n=======================================================================\n\n");
397}
398
399//_____________________________________________________________________________
400void AliRun::SetField(Int_t type, Int_t version, Float_t scale,
401 Float_t maxField, char* filename)
402{
403 //
404 // Set magnetic field parameters
405 // type Magnetic field transport flag 0=no field, 2=helix, 3=Runge Kutta
406 // version Magnetic field map version (only 1 active now)
407 // scale Scale factor for the magnetic field
408 // maxField Maximum value for the magnetic field
409
410 //
411 // --- Sanity check on mag field flags
412 if(type<0 || type > 2) {
23370b7a 413 Warning("SetField",
414 "Invalid magnetic field flag: %5d; Helix tracking chosen instead\n"
fe4da5cc 415 ,type);
416 type=2;
417 }
418 if(fField) delete fField;
419 if(version==1) {
420 fField = new AliMagFC("Map1"," ",type,version,scale,maxField);
f1b9d7c3 421 } else if(version<=2) {
fe4da5cc 422 fField = new AliMagFCM("Map2-3",filename,type,version,scale,maxField);
423 fField->ReadField();
f1b9d7c3 424 } else if(version==3) {
425 fField = new AliMagFDM("Map4",filename,type,version,scale,maxField);
426 fField->ReadField();
fe4da5cc 427 } else {
23370b7a 428 Warning("SetField","Invalid map %d\n",version);
fe4da5cc 429 }
430}
431
432//_____________________________________________________________________________
433void AliRun::FillTree()
434{
435 //
436 // Fills all AliRun TTrees
437 //
438 if (fTreeK) fTreeK->Fill();
439 if (fTreeH) fTreeH->Fill();
440 if (fTreeD) fTreeD->Fill();
441 if (fTreeR) fTreeR->Fill();
442}
443
444//_____________________________________________________________________________
445void AliRun::FinishPrimary()
446{
447 //
448 // Called at the end of each primary track
449 //
450
6c9704e6 451 // static Int_t count=0;
452 // const Int_t times=10;
fe4da5cc 453 // This primary is finished, purify stack
80762cb1 454 PurifyKine();
fe4da5cc 455
456 // Write out hits if any
457 if (gAlice->TreeH()) {
458 gAlice->TreeH()->Fill();
459 }
460
461 // Reset Hits info
462 gAlice->ResetHits();
a8f1fb7c 463
464 //
465 // if(++count%times==1) gObjectTable->Print();
fe4da5cc 466}
467
468//_____________________________________________________________________________
469void AliRun::FinishEvent()
470{
471 //
472 // Called at the end of the event.
473 //
7fb01480 474
dffd31ef 475 //
476 if(fLego) fLego->FinishEvent();
477
fe4da5cc 478 //Update the energy deposit tables
479 Int_t i;
875c717b 480 for(i=0;i<fEventEnergy.GetSize();i++) {
481 fSummEnergy[i]+=fEventEnergy[i];
482 fSum2Energy[i]+=fEventEnergy[i]*fEventEnergy[i];
fe4da5cc 483 }
875c717b 484 fEventEnergy.Reset();
fe4da5cc 485
486 // Clean detector information
487 CleanDetectors();
488
489 // Write out the kinematics
490 if (fTreeK) {
491 CleanParents();
492 fTreeK->Fill();
493 }
494
495 // Write out the digits
496 if (fTreeD) {
497 fTreeD->Fill();
498 ResetDigits();
499 }
500
501 // Write out reconstructed clusters
502 if (fTreeR) {
503 fTreeR->Fill();
504 }
505
506 // Write out the event Header information
507 if (fTreeE) fTreeE->Fill();
508
509 // Reset stack info
510 ResetStack();
511
512 // Write Tree headers
59fe9bd2 513 // Int_t ievent = fHeader.GetEvent();
514 // char hname[30];
515 // sprintf(hname,"TreeK%d",ievent);
51e0e89d 516 if (fTreeK) fTreeK->Write(0,TObject::kOverwrite);
59fe9bd2 517 // sprintf(hname,"TreeH%d",ievent);
51e0e89d 518 if (fTreeH) fTreeH->Write(0,TObject::kOverwrite);
59fe9bd2 519 // sprintf(hname,"TreeD%d",ievent);
51e0e89d 520 if (fTreeD) fTreeD->Write(0,TObject::kOverwrite);
59fe9bd2 521 // sprintf(hname,"TreeR%d",ievent);
51e0e89d 522 if (fTreeR) fTreeR->Write(0,TObject::kOverwrite);
875c717b 523
524 ++fEvent;
fe4da5cc 525}
526
527//_____________________________________________________________________________
528void AliRun::FinishRun()
529{
530 //
531 // Called at the end of the run.
532 //
533
dffd31ef 534 //
535 if(fLego) fLego->FinishRun();
536
fe4da5cc 537 // Clean detector information
8494b010 538 TIter next(fModules);
539 AliModule *detector;
540 while((detector = (AliModule*)next())) {
fe4da5cc 541 detector->FinishRun();
542 }
543
544 //Output energy summary tables
545 EnergySummary();
546
547 // file is retrieved from whatever tree
aee8290b 548 TFile *file = 0;
549 if (fTreeK) file = fTreeK->GetCurrentFile();
550 if ((!file) && (fTreeH)) file = fTreeH->GetCurrentFile();
551 if ((!file) && (fTreeD)) file = fTreeD->GetCurrentFile();
552 if ((!file) && (fTreeE)) file = fTreeE->GetCurrentFile();
553 if( NULL==file ) {
fe4da5cc 554 Error("FinishRun","There isn't root file!");
555 exit(1);
556 }
aee8290b 557 file->cd();
51e0e89d 558 fTreeE->Write(0,TObject::kOverwrite);
fe4da5cc 559
560 // Clean tree information
d2ecd553 561 if (fTreeK) {
562 delete fTreeK; fTreeK = 0;
563 }
564 if (fTreeH) {
565 delete fTreeH; fTreeH = 0;
566 }
567 if (fTreeD) {
568 delete fTreeD; fTreeD = 0;
569 }
570 if (fTreeR) {
571 delete fTreeR; fTreeR = 0;
572 }
573 if (fTreeE) {
574 delete fTreeE; fTreeE = 0;
575 }
fe4da5cc 576
577 // Write AliRun info and all detectors parameters
578 Write();
579
580 // Close output file
aee8290b 581 file->Write();
fe4da5cc 582}
583
584//_____________________________________________________________________________
585void AliRun::FlagTrack(Int_t track)
586{
587 //
588 // Flags a track and all its family tree to be kept
589 //
590 int curr;
1578254f 591 TParticle *particle;
fe4da5cc 592
593 curr=track;
594 while(1) {
1578254f 595 particle=(TParticle*)fParticles->UncheckedAt(curr);
fe4da5cc 596
597 // If the particle is flagged the three from here upward is saved already
aee8290b 598 if(particle->TestBit(kKeepBit)) return;
fe4da5cc 599
600 // Save this particle
aee8290b 601 particle->SetBit(kKeepBit);
fe4da5cc 602
603 // Move to father if any
1578254f 604 if((curr=particle->GetFirstMother())==-1) return;
fe4da5cc 605 }
606}
607
608//_____________________________________________________________________________
609void AliRun::EnergySummary()
610{
611 //
612 // Print summary of deposited energy
613 //
614
fe4da5cc 615 Int_t ndep=0;
616 Float_t edtot=0;
617 Float_t ed, ed2;
618 Int_t kn, i, left, j, id;
aee8290b 619 const Float_t kzero=0;
fe4da5cc 620 Int_t ievent=fHeader.GetEvent()+1;
621 //
622 // Energy loss information
623 if(ievent) {
624 printf("***************** Energy Loss Information per event (GEV) *****************\n");
875c717b 625 for(kn=1;kn<fEventEnergy.GetSize();kn++) {
626 ed=fSummEnergy[kn];
fe4da5cc 627 if(ed>0) {
875c717b 628 fEventEnergy[ndep]=kn;
fe4da5cc 629 if(ievent>1) {
630 ed=ed/ievent;
875c717b 631 ed2=fSum2Energy[kn];
fe4da5cc 632 ed2=ed2/ievent;
aee8290b 633 ed2=100*TMath::Sqrt(TMath::Max(ed2-ed*ed,kzero))/ed;
fe4da5cc 634 } else
635 ed2=99;
875c717b 636 fSummEnergy[ndep]=ed;
aee8290b 637 fSum2Energy[ndep]=TMath::Min((Float_t) 99.,TMath::Max(ed2,kzero));
fe4da5cc 638 edtot+=ed;
639 ndep++;
640 }
641 }
642 for(kn=0;kn<(ndep-1)/3+1;kn++) {
643 left=ndep-kn*3;
644 for(i=0;i<(3<left?3:left);i++) {
645 j=kn*3+i;
875c717b 646 id=Int_t (fEventEnergy[j]+0.1);
647 printf(" %s %10.3f +- %10.3f%%;",gMC->VolName(id),fSummEnergy[j],fSum2Energy[j]);
fe4da5cc 648 }
649 printf("\n");
650 }
651 //
652 // Relative energy loss in different detectors
653 printf("******************** Relative Energy Loss per event ********************\n");
654 printf("Total energy loss per event %10.3f GeV\n",edtot);
655 for(kn=0;kn<(ndep-1)/5+1;kn++) {
656 left=ndep-kn*5;
657 for(i=0;i<(5<left?5:left);i++) {
658 j=kn*5+i;
875c717b 659 id=Int_t (fEventEnergy[j]+0.1);
660 printf(" %s %10.3f%%;",gMC->VolName(id),100*fSummEnergy[j]/edtot);
fe4da5cc 661 }
662 printf("\n");
663 }
664 for(kn=0;kn<75;kn++) printf("*");
665 printf("\n");
666 }
667 //
668 // Reset the TArray's
875c717b 669 // fEventEnergy.Set(0);
670 // fSummEnergy.Set(0);
671 // fSum2Energy.Set(0);
fe4da5cc 672}
673
674//_____________________________________________________________________________
8494b010 675AliModule *AliRun::GetModule(const char *name)
fe4da5cc 676{
677 //
678 // Return pointer to detector from name
679 //
8494b010 680 return (AliModule*)fModules->FindObject(name);
fe4da5cc 681}
682
a68348e9 683//_____________________________________________________________________________
684AliDetector *AliRun::GetDetector(const char *name)
685{
686 //
687 // Return pointer to detector from name
688 //
689 return (AliDetector*)fModules->FindObject(name);
690}
691
fe4da5cc 692//_____________________________________________________________________________
8494b010 693Int_t AliRun::GetModuleID(const char *name)
fe4da5cc 694{
695 //
696 // Return galice internal detector identifier from name
697 //
23370b7a 698 Int_t i=-1;
699 TObject *mod=fModules->FindObject(name);
700 if(mod) i=fModules->IndexOf(mod);
701 return i;
fe4da5cc 702}
703
704//_____________________________________________________________________________
705Int_t AliRun::GetEvent(Int_t event)
706{
707 //
708 // Connect the Trees Kinematics and Hits for event # event
709 // Set branch addresses
710 //
fe4da5cc 711
712 // Reset existing structures
713 ResetStack();
714 ResetHits();
715 ResetDigits();
716
717 // Delete Trees already connected
718 if (fTreeK) delete fTreeK;
719 if (fTreeH) delete fTreeH;
720 if (fTreeD) delete fTreeD;
721 if (fTreeR) delete fTreeR;
59fe9bd2 722
723 // Get header from file
724 if(fTreeE) fTreeE->GetEntry(event);
725 else Error("GetEvent","Cannot file Header Tree\n");
fe4da5cc 726
727 // Get Kine Tree from file
728 char treeName[20];
729 sprintf(treeName,"TreeK%d",event);
730 fTreeK = (TTree*)gDirectory->Get(treeName);
731 if (fTreeK) fTreeK->SetBranchAddress("Particles", &fParticles);
23370b7a 732 else Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
fe4da5cc 733
734 // Get Hits Tree header from file
735 sprintf(treeName,"TreeH%d",event);
736 fTreeH = (TTree*)gDirectory->Get(treeName);
737 if (!fTreeH) {
23370b7a 738 Error("GetEvent","cannot find Hits Tree for event:%d\n",event);
fe4da5cc 739 }
740
741 // Get Digits Tree header from file
742 sprintf(treeName,"TreeD%d",event);
743 fTreeD = (TTree*)gDirectory->Get(treeName);
744 if (!fTreeD) {
07a68c1d 745 Warning("GetEvent","cannot find Digits Tree for event:%d\n",event);
fe4da5cc 746 }
747
748
749 // Get Reconstruct Tree header from file
750 sprintf(treeName,"TreeR%d",event);
751 fTreeR = (TTree*)gDirectory->Get(treeName);
752 if (!fTreeR) {
753 // printf("WARNING: cannot find Reconstructed Tree for event:%d\n",event);
754 }
755
756 // Set Trees branch addresses
8494b010 757 TIter next(fModules);
758 AliModule *detector;
759 while((detector = (AliModule*)next())) {
fe4da5cc 760 detector->SetTreeAddress();
761 }
762
763 if (fTreeK) fTreeK->GetEvent(0);
764 fNtrack = Int_t (fParticles->GetEntries());
765 return fNtrack;
766}
767
768//_____________________________________________________________________________
769TGeometry *AliRun::GetGeometry()
770{
771 //
772 // Import Alice geometry from current file
773 // Return pointer to geometry object
774 //
775 if (!fGeometry) fGeometry = (TGeometry*)gDirectory->Get("AliceGeom");
776 //
777 // Unlink and relink nodes in detectors
778 // This is bad and there must be a better way...
779 //
fe4da5cc 780
8494b010 781 TIter next(fModules);
782 AliModule *detector;
783 while((detector = (AliModule*)next())) {
fe4da5cc 784 detector->SetTreeAddress();
785 TList *dnodes=detector->Nodes();
786 Int_t j;
787 TNode *node, *node1;
788 for ( j=0; j<dnodes->GetSize(); j++) {
789 node = (TNode*) dnodes->At(j);
52d0ab00 790 node1 = fGeometry->GetNode(node->GetName());
fe4da5cc 791 dnodes->Remove(node);
792 dnodes->AddAt(node1,j);
793 }
794 }
795 return fGeometry;
796}
797
798//_____________________________________________________________________________
799void AliRun::GetNextTrack(Int_t &mtrack, Int_t &ipart, Float_t *pmom,
800 Float_t &e, Float_t *vpos, Float_t *polar,
801 Float_t &tof)
802{
803 //
804 // Return next track from stack of particles
805 //
a8f1fb7c 806 TVector3 pol;
fe4da5cc 807 fCurrent=-1;
1578254f 808 TParticle *track;
fe4da5cc 809 for(Int_t i=fNtrack-1; i>=0; i--) {
1578254f 810 track=(TParticle*) fParticles->UncheckedAt(i);
aee8290b 811 if(!track->TestBit(kDoneBit)) {
fe4da5cc 812 //
813 // The track has not yet been processed
814 fCurrent=i;
1578254f 815 ipart=track->GetPdgCode();
816 pmom[0]=track->Px();
817 pmom[1]=track->Py();
818 pmom[2]=track->Pz();
819 e =track->Energy();
820 vpos[0]=track->Vx();
821 vpos[1]=track->Vy();
822 vpos[2]=track->Vz();
a8f1fb7c 823 track->GetPolarisation(pol);
824 polar[0]=pol.X();
825 polar[1]=pol.Y();
826 polar[2]=pol.Z();
1578254f 827 tof=track->T();
aee8290b 828 track->SetBit(kDoneBit);
fe4da5cc 829 break;
830 }
831 }
832 mtrack=fCurrent;
833 //
834 // stop and start timer when we start a primary track
835 Int_t nprimaries = fHeader.GetNprimary();
836 if (fCurrent >= nprimaries) return;
837 if (fCurrent < nprimaries-1) {
838 fTimer.Stop();
1578254f 839 track=(TParticle*) fParticles->UncheckedAt(fCurrent+1);
840 // track->SetProcessTime(fTimer.CpuTime());
fe4da5cc 841 }
842 fTimer.Start();
843}
844
845//_____________________________________________________________________________
846Int_t AliRun::GetPrimary(Int_t track)
847{
848 //
849 // return number of primary that has generated track
850 //
851 int current, parent;
1578254f 852 TParticle *part;
fe4da5cc 853 //
854 parent=track;
855 while (1) {
856 current=parent;
1578254f 857 part = (TParticle *)fParticles->UncheckedAt(current);
858 parent=part->GetFirstMother();
fe4da5cc 859 if(parent<0) return current;
860 }
861}
862
863//_____________________________________________________________________________
875c717b 864void AliRun::InitMC(const char *setup)
fe4da5cc 865{
866 //
867 // Initialize the Alice setup
868 //
869
51e0e89d 870 if(fInitDone) {
871 Warning("Init","Cannot initialise AliRun twice!\n");
872 return;
873 }
874
fe4da5cc 875 gROOT->LoadMacro(setup);
45189757 876 gInterpreter->ProcessLine(fConfigFunction.Data());
fe4da5cc 877
cfce8870 878 gMC->DefineParticles(); //Create standard MC particles
fe4da5cc 879
880 TObject *objfirst, *objlast;
881
23370b7a 882 fNdets = fModules->GetLast()+1;
883
fe4da5cc 884 //
875c717b 885 //=================Create Materials and geometry
886 gMC->Init();
887
8494b010 888 TIter next(fModules);
889 AliModule *detector;
890 while((detector = (AliModule*)next())) {
fe4da5cc 891 detector->SetTreeAddress();
892 objlast = gDirectory->GetList()->Last();
893
fe4da5cc 894 // Add Detector histograms in Detector list of histograms
895 if (objlast) objfirst = gDirectory->GetList()->After(objlast);
896 else objfirst = gDirectory->GetList()->First();
897 while (objfirst) {
898 detector->Histograms()->Add(objfirst);
899 objfirst = gDirectory->GetList()->After(objfirst);
900 }
901 }
902 SetTransPar(); //Read the cuts for all materials
903
904 MediaTable(); //Build the special IMEDIA table
905
fe4da5cc 906 //Initialise geometry deposition table
875c717b 907 fEventEnergy.Set(gMC->NofVolumes()+1);
908 fSummEnergy.Set(gMC->NofVolumes()+1);
909 fSum2Energy.Set(gMC->NofVolumes()+1);
fe4da5cc 910
fe4da5cc 911 //Compute cross-sections
875c717b 912 gMC->BuildPhysics();
fe4da5cc 913
914 //Write Geometry object to current file.
915 fGeometry->Write();
916
917 fInitDone = kTRUE;
51e0e89d 918
919 //
920 // Save stuff at the beginning of the file to avoid file corruption
921 Write();
fe4da5cc 922}
923
924//_____________________________________________________________________________
925void AliRun::MediaTable()
926{
927 //
928 // Built media table to get from the media number to
929 // the detector id
930 //
ad51aeb0 931 Int_t kz, nz, idt, lz, i, k, ind;
932 // Int_t ibeg;
fe4da5cc 933 TObjArray &dets = *gAlice->Detectors();
8494b010 934 AliModule *det;
fe4da5cc 935 //
936 // For all detectors
937 for (kz=0;kz<fNdets;kz++) {
938 // If detector is defined
8494b010 939 if((det=(AliModule*) dets[kz])) {
ad51aeb0 940 TArrayI &idtmed = *(det->GetIdtmed());
941 for(nz=0;nz<100;nz++) {
fe4da5cc 942 // Find max and min material number
ad51aeb0 943 if((idt=idtmed[nz])) {
fe4da5cc 944 det->LoMedium() = det->LoMedium() < idt ? det->LoMedium() : idt;
945 det->HiMedium() = det->HiMedium() > idt ? det->HiMedium() : idt;
946 }
947 }
948 if(det->LoMedium() > det->HiMedium()) {
949 det->LoMedium() = 0;
950 det->HiMedium() = 0;
951 } else {
952 if(det->HiMedium() > fImedia->GetSize()) {
ad51aeb0 953 Error("MediaTable","Increase fImedia from %d to %d",
954 fImedia->GetSize(),det->HiMedium());
fe4da5cc 955 return;
956 }
957 // Tag all materials in rage as belonging to detector kz
958 for(lz=det->LoMedium(); lz<= det->HiMedium(); lz++) {
959 (*fImedia)[lz]=kz;
960 }
961 }
962 }
963 }
964 //
965 // Print summary table
966 printf(" Traking media ranges:\n");
967 for(i=0;i<(fNdets-1)/6+1;i++) {
968 for(k=0;k< (6<fNdets-i*6?6:fNdets-i*6);k++) {
969 ind=i*6+k;
8494b010 970 det=(AliModule*)dets[ind];
fe4da5cc 971 if(det)
972 printf(" %6s: %3d -> %3d;",det->GetName(),det->LoMedium(),
973 det->HiMedium());
974 else
975 printf(" %6s: %3d -> %3d;","NULL",0,0);
976 }
977 printf("\n");
978 }
979}
980
981//____________________________________________________________________________
982void AliRun::SetGenerator(AliGenerator *generator)
ee1dd322 983{
984 //
985 // Load the event generator
986 //
987 if(!fGenerator) fGenerator = generator;
988}
989
990//____________________________________________________________________________
991void AliRun::ResetGenerator(AliGenerator *generator)
fe4da5cc 992{
993 //
994 // Load the event generator
995 //
b13db077 996 if(fGenerator)
838edcaf 997 if(generator)
998 Warning("ResetGenerator","Replacing generator %s with %s\n",
999 fGenerator->GetName(),generator->GetName());
1000 else
1001 Warning("ResetGenerator","Replacing generator %s with NULL\n",
1002 fGenerator->GetName());
b13db077 1003 fGenerator = generator;
fe4da5cc 1004}
1005
1006//____________________________________________________________________________
1007void AliRun::SetTransPar(char* filename)
1008{
1009 //
1010 // Read filename to set the transport parameters
1011 //
1012
fe4da5cc 1013
aee8290b 1014 const Int_t kncuts=10;
1015 const Int_t knflags=11;
1016 const Int_t knpars=kncuts+knflags;
1017 const char kpars[knpars][7] = {"CUTGAM" ,"CUTELE","CUTNEU","CUTHAD","CUTMUO",
fe4da5cc 1018 "BCUTE","BCUTM","DCUTE","DCUTM","PPCUTM","ANNI",
1019 "BREM","COMP","DCAY","DRAY","HADR","LOSS",
1020 "MULS","PAIR","PHOT","RAYL"};
1021 char line[256];
ad51aeb0 1022 char detName[7];
fe4da5cc 1023 char* filtmp;
aee8290b 1024 Float_t cut[kncuts];
1025 Int_t flag[knflags];
fe4da5cc 1026 Int_t i, itmed, iret, ktmed, kz;
1027 FILE *lun;
1028 //
1029 // See whether the file is there
1030 filtmp=gSystem->ExpandPathName(filename);
1031 lun=fopen(filtmp,"r");
1032 delete [] filtmp;
1033 if(!lun) {
ad51aeb0 1034 Warning("SetTransPar","File %s does not exist!\n",filename);
fe4da5cc 1035 return;
1036 }
1037 //
1038 printf(" "); for(i=0;i<60;i++) printf("*"); printf("\n");
1039 printf(" *%59s\n","*");
1040 printf(" * Please check carefully what you are doing!%10s\n","*");
1041 printf(" *%59s\n","*");
1042 //
1043 while(1) {
1044 // Initialise cuts and flags
aee8290b 1045 for(i=0;i<kncuts;i++) cut[i]=-99;
1046 for(i=0;i<knflags;i++) flag[i]=-99;
fe4da5cc 1047 itmed=0;
1048 for(i=0;i<256;i++) line[i]='\0';
1049 // Read up to the end of line excluded
1050 iret=fscanf(lun,"%[^\n]",line);
1051 if(iret<0) {
1052 //End of file
1053 fclose(lun);
1054 printf(" *%59s\n","*");
1055 printf(" "); for(i=0;i<60;i++) printf("*"); printf("\n");
1056 return;
1057 }
1058 // Read the end of line
1059 fscanf(lun,"%*c");
1060 if(!iret) continue;
1061 if(line[0]=='*') continue;
1062 // Read the numbers
ad51aeb0 1063 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",
1064 detName,&itmed,&cut[0],&cut[1],&cut[2],&cut[3],&cut[4],&cut[5],&cut[6],&cut[7],&cut[8],
1065 &cut[9],&flag[0],&flag[1],&flag[2],&flag[3],&flag[4],&flag[5],&flag[6],&flag[7],
1066 &flag[8],&flag[9],&flag[10]);
fe4da5cc 1067 if(!iret) continue;
1068 if(iret<0) {
1069 //reading error
ad51aeb0 1070 Warning("SetTransPar","Error reading file %s\n",filename);
fe4da5cc 1071 continue;
1072 }
ad51aeb0 1073 // Check that the module exist
1074 AliModule *mod = GetModule(detName);
1075 if(mod) {
1076 // Get the array of media numbers
1077 TArrayI &idtmed = *mod->GetIdtmed();
1078 // Check that the tracking medium code is valid
1079 if(0<=itmed && itmed < 100) {
1080 ktmed=idtmed[itmed];
1081 if(!ktmed) {
1082 Warning("SetTransPar","Invalid tracking medium code %d for %s\n",itmed,mod->GetName());
1083 continue;
fe4da5cc 1084 }
ad51aeb0 1085 // Set energy thresholds
aee8290b 1086 for(kz=0;kz<kncuts;kz++) {
ad51aeb0 1087 if(cut[kz]>=0) {
23370b7a 1088 printf(" * %-6s set to %10.3E for tracking medium code %4d for %s\n",
aee8290b 1089 kpars[kz],cut[kz],itmed,mod->GetName());
1090 gMC->Gstpar(ktmed,kpars[kz],cut[kz]);
ad51aeb0 1091 }
fe4da5cc 1092 }
ad51aeb0 1093 // Set transport mechanisms
aee8290b 1094 for(kz=0;kz<knflags;kz++) {
ad51aeb0 1095 if(flag[kz]>=0) {
1096 printf(" * %-6s set to %10d for tracking medium code %4d for %s\n",
aee8290b 1097 kpars[kncuts+kz],flag[kz],itmed,mod->GetName());
1098 gMC->Gstpar(ktmed,kpars[kncuts+kz],Float_t(flag[kz]));
ad51aeb0 1099 }
1100 }
1101 } else {
1102 Warning("SetTransPar","Invalid medium code %d *\n",itmed);
1103 continue;
fe4da5cc 1104 }
1105 } else {
ad51aeb0 1106 Warning("SetTransPar","Module %s not present\n",detName);
fe4da5cc 1107 continue;
1108 }
1109 }
1110}
1111
1112//_____________________________________________________________________________
1113void AliRun::MakeTree(Option_t *option)
1114{
1115 //
1116 // Create the ROOT trees
1117 // Loop on all detectors to create the Root branch (if any)
1118 //
1119
b13db077 1120 char hname[30];
fe4da5cc 1121 //
1122 // Analyse options
aee8290b 1123 char *oK = strstr(option,"K");
1124 char *oH = strstr(option,"H");
1125 char *oE = strstr(option,"E");
1126 char *oD = strstr(option,"D");
1127 char *oR = strstr(option,"R");
fe4da5cc 1128 //
aee8290b 1129 if (oK && !fTreeK) {
b13db077 1130 sprintf(hname,"TreeK%d",fEvent);
1131 fTreeK = new TTree(hname,"Kinematics");
1132 // Create a branch for particles
1133 fTreeK->Branch("Particles",&fParticles,4000);
51e0e89d 1134 fTreeK->Write();
b13db077 1135 }
aee8290b 1136 if (oH && !fTreeH) {
b13db077 1137 sprintf(hname,"TreeH%d",fEvent);
1138 fTreeH = new TTree(hname,"Hits");
1139 fTreeH->SetAutoSave(1000000000); //no autosave
51e0e89d 1140 fTreeH->Write();
b13db077 1141 }
aee8290b 1142 if (oD && !fTreeD) {
b13db077 1143 sprintf(hname,"TreeD%d",fEvent);
1144 fTreeD = new TTree(hname,"Digits");
51e0e89d 1145 fTreeD->Write();
b13db077 1146 }
aee8290b 1147 if (oR && !fTreeR) {
b13db077 1148 sprintf(hname,"TreeR%d",fEvent);
1149 fTreeR = new TTree(hname,"Reconstruction");
51e0e89d 1150 fTreeR->Write();
b13db077 1151 }
aee8290b 1152 if (oE && !fTreeE) {
b13db077 1153 fTreeE = new TTree("TE","Header");
1154 // Create a branch for Header
aee8290b 1155 fTreeE->Branch("Header","AliHeader",&gAliHeader,4000);
51e0e89d 1156 fTreeE->Write();
b13db077 1157 }
fe4da5cc 1158 //
1159 // Create a branch for hits/digits for each detector
1160 // Each branch is a TClonesArray. Each data member of the Hits classes
1161 // will be in turn a subbranch of the detector master branch
8494b010 1162 TIter next(fModules);
1163 AliModule *detector;
1164 while((detector = (AliModule*)next())) {
aee8290b 1165 if (oH || oD || oR) detector->MakeBranch(option);
fe4da5cc 1166 }
fe4da5cc 1167}
1168
1169//_____________________________________________________________________________
1170Int_t AliRun::PurifyKine(Int_t lastSavedTrack, Int_t nofTracks)
1171{
1172 //
1173 // PurifyKine with external parameters
1174 //
1175 fHgwmk = lastSavedTrack;
1176 fNtrack = nofTracks;
1177 PurifyKine();
1178 return fHgwmk;
1179}
1180
1181//_____________________________________________________________________________
1182void AliRun::PurifyKine()
1183{
1184 //
1185 // Compress kinematic tree keeping only flagged particles
1186 // and renaming the particle id's in all the hits
1187 //
1188 TClonesArray &particles = *fParticles;
1189 int nkeep=fHgwmk+1, parent, i;
1578254f 1190 TParticle *part, *partnew, *father;
fe4da5cc 1191 int *map = new int[particles.GetEntries()];
1192
1193 // Save in Header total number of tracks before compression
1194 fHeader.SetNtrack(fHeader.GetNtrack()+fNtrack-fHgwmk);
1195
a01a8b12 1196 // First pass, invalid Daughter information
fe4da5cc 1197 for(i=0; i<fNtrack; i++) {
a01a8b12 1198 // Preset map, to be removed later
1199 if(i<=fHgwmk) map[i]=i ; else map[i] = -99;
aee8290b 1200 ((TParticle *)particles.UncheckedAt(i))->ResetBit(kDaughtersBit);
a01a8b12 1201 }
fe4da5cc 1202 // Second pass, build map between old and new numbering
1203 for(i=fHgwmk+1; i<fNtrack; i++) {
1578254f 1204 part = (TParticle *)particles.UncheckedAt(i);
aee8290b 1205 if(part->TestBit(kKeepBit)) {
fe4da5cc 1206
1207 // This particle has to be kept
1208 map[i]=nkeep;
1209 if(i!=nkeep) {
1210
1211 // Old and new are different, have to copy
1578254f 1212 partnew = (TParticle *)particles.UncheckedAt(nkeep);
5eb58812 1213 // Change due to a bug in the HP compiler
1214 // *partnew = *part;
1215 memcpy(partnew,part,sizeof(TParticle));
fe4da5cc 1216 } else partnew = part;
1217
1218 // as the parent is always *before*, it must be already
1219 // in place. This is what we are checking anyway!
1578254f 1220 if((parent=partnew->GetFirstMother())>fHgwmk) {
fe4da5cc 1221 if(map[parent]==-99) printf("map[%d] = -99!\n",parent);
1578254f 1222 partnew->SetFirstMother(map[parent]);
fe4da5cc 1223 }
1224 nkeep++;
1225 }
1226 }
1227 fNtrack=nkeep;
1228
1578254f 1229 // Fix daughters information
ae23d366 1230 for (i=0; i<fNtrack; i++) {
1578254f 1231 part = (TParticle *)particles.UncheckedAt(i);
1232 parent = part->GetFirstMother();
ae23d366 1233 if(parent>=0) {
1234 father = (TParticle *)particles.UncheckedAt(parent);
aee8290b 1235 if(father->TestBit(kDaughtersBit)) {
fe4da5cc 1236
ae23d366 1237 if(i<father->GetFirstDaughter()) father->SetFirstDaughter(i);
1238 if(i>father->GetLastDaughter()) father->SetLastDaughter(i);
1239 } else {
1240 // Iitialise daughters info for first pass
1241 father->SetFirstDaughter(i);
1242 father->SetLastDaughter(i);
aee8290b 1243 father->SetBit(kDaughtersBit);
ae23d366 1244 }
fe4da5cc 1245 }
1246 }
1247
1cedd08a 1248#ifdef old
fe4da5cc 1249 // Now loop on all detectors and reset the hits
b13db077 1250 AliHit *OneHit;
8494b010 1251 TIter next(fModules);
1252 AliModule *detector;
1253 while((detector = (AliModule*)next())) {
fe4da5cc 1254 if (!detector->Hits()) continue;
1255 TClonesArray &vHits=*(detector->Hits());
1256 if(vHits.GetEntries() != detector->GetNhits())
1257 printf("vHits.GetEntries()!=detector->GetNhits(): %d != %d\n",
1258 vHits.GetEntries(),detector->GetNhits());
1259 for (i=0; i<detector->GetNhits(); i++) {
1260 OneHit = (AliHit *)vHits.UncheckedAt(i);
1261 OneHit->SetTrack(map[OneHit->GetTrack()]);
1262 }
1263 }
1cedd08a 1264#else
1265
1266 // Now loop on all registered hit lists
1267 TIter next(fHitLists);
1268 TCollection *hitList;
1269 while((hitList = (TCollection*)next())) {
1270 TIter nexthit(hitList);
1271 AliHit *hit;
1272 while((hit = (AliHit*)nexthit())) {
1273 hit->SetTrack(map[hit->GetTrack()]);
1274 }
1275 }
1276#endif
fe4da5cc 1277
1278 fHgwmk=nkeep-1;
1279 particles.SetLast(fHgwmk);
1280 delete [] map;
1281}
1282
1283//_____________________________________________________________________________
dffd31ef 1284void AliRun::BeginEvent()
fe4da5cc 1285{
1286 //
1287 // Reset all Detectors & kinematics & trees
1288 //
59fe9bd2 1289 char hname[30];
dffd31ef 1290 //
1291
1292 //
1293 if(fLego) {
1294 fLego->BeginEvent();
1295 return;
1296 }
1297
59fe9bd2 1298 //
fe4da5cc 1299 ResetStack();
1300 ResetHits();
1301 ResetDigits();
1302
1303 // Initialise event header
875c717b 1304 fHeader.Reset(fRun,fEvent);
fe4da5cc 1305
59fe9bd2 1306 if(fTreeK) {
1307 fTreeK->Reset();
875c717b 1308 sprintf(hname,"TreeK%d",fEvent);
59fe9bd2 1309 fTreeK->SetName(hname);
1310 }
1311 if(fTreeH) {
1312 fTreeH->Reset();
875c717b 1313 sprintf(hname,"TreeH%d",fEvent);
59fe9bd2 1314 fTreeH->SetName(hname);
1315 }
1316 if(fTreeD) {
1317 fTreeD->Reset();
875c717b 1318 sprintf(hname,"TreeD%d",fEvent);
59fe9bd2 1319 fTreeD->SetName(hname);
1320 }
1321 if(fTreeR) {
1322 fTreeR->Reset();
875c717b 1323 sprintf(hname,"TreeR%d",fEvent);
59fe9bd2 1324 fTreeR->SetName(hname);
1325 }
fe4da5cc 1326}
1327
1328//_____________________________________________________________________________
1329void AliRun::ResetDigits()
1330{
1331 //
1332 // Reset all Detectors digits
1333 //
8494b010 1334 TIter next(fModules);
1335 AliModule *detector;
1336 while((detector = (AliModule*)next())) {
fe4da5cc 1337 detector->ResetDigits();
1338 }
1339}
1340
1341//_____________________________________________________________________________
1342void AliRun::ResetHits()
1343{
1344 //
1345 // Reset all Detectors hits
1346 //
8494b010 1347 TIter next(fModules);
1348 AliModule *detector;
1349 while((detector = (AliModule*)next())) {
fe4da5cc 1350 detector->ResetHits();
1351 }
1352}
1353
1354//_____________________________________________________________________________
1355void AliRun::ResetPoints()
1356{
1357 //
1358 // Reset all Detectors points
1359 //
8494b010 1360 TIter next(fModules);
1361 AliModule *detector;
1362 while((detector = (AliModule*)next())) {
fe4da5cc 1363 detector->ResetPoints();
1364 }
1365}
1366
1367//_____________________________________________________________________________
875c717b 1368void AliRun::RunMC(Int_t nevent, const char *setup)
fe4da5cc 1369{
1370 //
1371 // Main function to be called to process a galice run
1372 // example
1373 // Root > gAlice.Run();
1374 // a positive number of events will cause the finish routine
1375 // to be called
1376 //
1377
fe4da5cc 1378 // check if initialisation has been done
875c717b 1379 if (!fInitDone) InitMC(setup);
fe4da5cc 1380
fe4da5cc 1381 // Create the Root Tree with one branch per detector
80762cb1 1382 MakeTree("KHDER");
fe4da5cc 1383
875c717b 1384 gMC->ProcessRun(nevent);
1385
fe4da5cc 1386 // End of this run, close files
80762cb1 1387 if(nevent>0) FinishRun();
fe4da5cc 1388}
1389
1390//_____________________________________________________________________________
1391void AliRun::RunLego(const char *setup,Int_t ntheta,Float_t themin,
1392 Float_t themax,Int_t nphi,Float_t phimin,Float_t phimax,
1393 Float_t rmin,Float_t rmax,Float_t zmax)
1394{
1395 //
1396 // Generates lego plots of:
1397 // - radiation length map phi vs theta
1398 // - radiation length map phi vs eta
1399 // - interaction length map
1400 // - g/cm2 length map
1401 //
1402 // ntheta bins in theta, eta
1403 // themin minimum angle in theta (degrees)
1404 // themax maximum angle in theta (degrees)
1405 // nphi bins in phi
1406 // phimin minimum angle in phi (degrees)
1407 // phimax maximum angle in phi (degrees)
1408 // rmin minimum radius
1409 // rmax maximum radius
1410 //
1411 //
1412 // The number of events generated = ntheta*nphi
1413 // run input parameters in macro setup (default="Config.C")
1414 //
1415 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
1416 //Begin_Html
1417 /*
1439f98e 1418 <img src="picts/AliRunLego1.gif">
fe4da5cc 1419 */
1420 //End_Html
1421 //Begin_Html
1422 /*
1439f98e 1423 <img src="picts/AliRunLego2.gif">
fe4da5cc 1424 */
1425 //End_Html
1426 //Begin_Html
1427 /*
1439f98e 1428 <img src="picts/AliRunLego3.gif">
fe4da5cc 1429 */
1430 //End_Html
1431 //
1432
1433 // check if initialisation has been done
875c717b 1434 if (!fInitDone) InitMC(setup);
b13db077 1435
838edcaf 1436 //Save current generator
1437 AliGenerator *gen=Generator();
1438
b13db077 1439 //Create Lego object
1440 fLego = new AliLego("lego",ntheta,themin,themax,nphi,phimin,phimax,rmin,rmax,zmax);
1441
dffd31ef 1442 //Prepare MC for Lego Run
1443 gMC->InitLego();
1444
b13db077 1445 //Run Lego Object
dffd31ef 1446 gMC->ProcessRun(ntheta*nphi+1);
fe4da5cc 1447
1448 // Create only the Root event Tree
80762cb1 1449 MakeTree("E");
fe4da5cc 1450
1451 // End of this run, close files
80762cb1 1452 FinishRun();
838edcaf 1453
1454 // Delete Lego Object
1455 delete fLego; fLego=0;
1456
1457 // Restore current generator
1458 SetGenerator(gen);
fe4da5cc 1459}
1460
1461//_____________________________________________________________________________
1462void AliRun::SetCurrentTrack(Int_t track)
1463{
1464 //
1465 // Set current track number
1466 //
1467 fCurrent = track;
1468}
1469
1470//_____________________________________________________________________________
1578254f 1471void AliRun::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
fe4da5cc 1472 Float_t *vpos, Float_t *polar, Float_t tof,
8918e700 1473 const char* /* mecha */, Int_t &ntr, Float_t weight)
fe4da5cc 1474{
1475 //
1476 // Load a track on the stack
1477 //
1478 // done 0 if the track has to be transported
1479 // 1 if not
1480 // parent identifier of the parent track. -1 for a primary
1578254f 1481 // pdg particle code
fe4da5cc 1482 // pmom momentum GeV/c
1483 // vpos position
1484 // polar polarisation
1485 // tof time of flight in seconds
1486 // mecha production mechanism
1487 // ntr on output the number of the track stored
1488 //
1489 TClonesArray &particles = *fParticles;
1578254f 1490 TParticle *particle;
fe4da5cc 1491 Float_t mass;
aee8290b 1492 const Int_t kfirstdaughter=-1;
1493 const Int_t klastdaughter=-1;
1494 const Int_t kS=0;
1578254f 1495 // const Float_t tlife=0;
fe4da5cc 1496
1578254f 1497 //
1498 // Here we get the static mass
1499 // For MC is ok, but a more sophisticated method could be necessary
1500 // if the calculated mass is required
1501 // also, this method is potentially dangerous if the mass
1502 // used in the MC is not the same of the PDG database
1503 //
1504 mass = TDatabasePDG::Instance()->GetParticle(pdg)->Mass();
fe4da5cc 1505 Float_t e=TMath::Sqrt(mass*mass+pmom[0]*pmom[0]+
1506 pmom[1]*pmom[1]+pmom[2]*pmom[2]);
1507
aee8290b 1508 //printf("Loading particle %s mass %f ene %f No %d ip %d pos %f %f %f mom %f %f %f kS %d m %s\n",
1509 //pname,mass,e,fNtrack,pdg,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS,mecha);
fe4da5cc 1510
aee8290b 1511 particle=new(particles[fNtrack]) TParticle(pdg,kS,parent,-1,kfirstdaughter,
1512 klastdaughter,pmom[0],pmom[1],pmom[2],
1578254f 1513 e,vpos[0],vpos[1],vpos[2],tof);
1514 // polar[0],polar[1],polar[2],tof,
1515 // mecha,weight);
1516 ((TParticle*)particles[fNtrack])->SetPolarisation(TVector3(polar[0],polar[1],polar[2]));
1517 ((TParticle*)particles[fNtrack])->SetWeight(weight);
aee8290b 1518 if(!done) particle->SetBit(kDoneBit);
a01a8b12 1519 //Declare that the daughter information is valid
aee8290b 1520 ((TParticle*)particles[fNtrack])->SetBit(kDaughtersBit);
fe4da5cc 1521
1522 if(parent>=0) {
1578254f 1523 particle=(TParticle*) fParticles->UncheckedAt(parent);
1524 particle->SetLastDaughter(fNtrack);
1525 if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
fe4da5cc 1526 } else {
1527 //
1528 // This is a primary track. Set high water mark for this event
1529 fHgwmk=fNtrack;
1530 //
1531 // Set also number if primary tracks
1532 fHeader.SetNprimary(fHgwmk+1);
1533 fHeader.SetNtrack(fHgwmk+1);
1534 }
1535 ntr = fNtrack++;
1536}
1537
1538//_____________________________________________________________________________
1539void AliRun::KeepTrack(const Int_t track)
1540{
1541 //
1542 // flags a track to be kept
1543 //
1544 TClonesArray &particles = *fParticles;
aee8290b 1545 ((TParticle*)particles[track])->SetBit(kKeepBit);
fe4da5cc 1546}
1547
1548//_____________________________________________________________________________
875c717b 1549void AliRun::StepManager(Int_t id)
fe4da5cc 1550{
1551 //
1552 // Called at every step during transport
1553 //
1554
fe4da5cc 1555 //
1556 // --- If lego option, do it and leave
b13db077 1557 if (fLego)
fe4da5cc 1558 fLego->StepManager();
b13db077 1559 else {
1560 Int_t copy;
1561 //Update energy deposition tables
875c717b 1562 AddEnergyDeposit(gMC->CurrentVolID(copy),gMC->Edep());
fe4da5cc 1563
b13db077 1564 //Call the appropriate stepping routine;
1565 AliModule *det = (AliModule*)fModules->At(id);
1566 if(det) det->StepManager();
fe4da5cc 1567 }
fe4da5cc 1568}
1569
fe4da5cc 1570//_____________________________________________________________________________
1571void AliRun::Streamer(TBuffer &R__b)
1572{
1573 //
1574 // Stream an object of class AliRun.
1575 //
1576 if (R__b.IsReading()) {
1577 Version_t R__v = R__b.ReadVersion(); if (R__v) { }
1578 TNamed::Streamer(R__b);
1579 if (!gAlice) gAlice = this;
1580 gROOT->GetListOfBrowsables()->Add(this,"Run");
59fe9bd2 1581 fTreeE = (TTree*)gDirectory->Get("TE");
aee8290b 1582 if (fTreeE) fTreeE->SetBranchAddress("Header", &gAliHeader);
59fe9bd2 1583 else Error("Streamer","cannot find Header Tree\n");
fe4da5cc 1584 R__b >> fNtrack;
1585 R__b >> fHgwmk;
1586 R__b >> fDebug;
1587 fHeader.Streamer(R__b);
8494b010 1588 R__b >> fModules;
fe4da5cc 1589 R__b >> fParticles;
1590 R__b >> fField;
875c717b 1591 // R__b >> fMC;
fe4da5cc 1592 R__b >> fNdets;
1593 R__b >> fTrRmax;
1594 R__b >> fTrZmax;
1595 R__b >> fGenerator;
59fe9bd2 1596 if(R__v>1) {
1597 R__b >> fPDGDB; //Particle factory object!
1598 fTreeE->GetEntry(0);
1599 } else {
1600 fHeader.SetEvent(0);
1601 fPDGDB = TDatabasePDG::Instance(); //Particle factory object!
1602 }
45189757 1603 if(R__v>2) {
1604 fConfigFunction.Streamer(R__b);
1605 } else {
1606 fConfigFunction="Config();";
1607 }
fe4da5cc 1608 } else {
1609 R__b.WriteVersion(AliRun::IsA());
1610 TNamed::Streamer(R__b);
1611 R__b << fNtrack;
1612 R__b << fHgwmk;
1613 R__b << fDebug;
1614 fHeader.Streamer(R__b);
8494b010 1615 R__b << fModules;
fe4da5cc 1616 R__b << fParticles;
1617 R__b << fField;
875c717b 1618 // R__b << fMC;
fe4da5cc 1619 R__b << fNdets;
1620 R__b << fTrRmax;
1621 R__b << fTrZmax;
1622 R__b << fGenerator;
1578254f 1623 R__b << fPDGDB; //Particle factory object!
45189757 1624 fConfigFunction.Streamer(R__b);
fe4da5cc 1625 }
1626}