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