Effective c++, committed on behalf of ´┐Żystein Djuvsland
[u/mrichter/AliRoot.git] / STEER / AliMC.cxx
CommitLineData
5d12ce38 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/* $Id$ */
17
d0d4a6b3 18// This class is extracted from the AliRun class
19// and contains all the MC-related functionality
20// The number of dependencies has to be reduced...
21// Author: F.Carminati
22// Federico.Carminati@cern.ch
23
c75cfb51 24#include <RVersion.h>
5d12ce38 25#include <TBrowser.h>
7ca4655f 26#include <TClonesArray.h>
27#include <TGeoManager.h>
5d12ce38 28#include <TStopwatch.h>
29#include <TSystem.h>
30#include <TVirtualMC.h>
b60e0f5e 31
21bf7095 32#include "AliLog.h"
5d12ce38 33#include "AliDetector.h"
34#include "AliGenerator.h"
35#include "AliHeader.h"
36#include "AliLego.h"
37#include "AliMC.h"
38#include "AliMCQA.h"
39#include "AliRun.h"
40#include "AliStack.h"
0561efeb 41#include "AliMagF.h"
5d12ce38 42#include "AliTrackReference.h"
39b3f8ba 43#include "AliSimulation.h"
392808e8 44#include "AliGeomManager.h"
5d12ce38 45
46
47ClassImp(AliMC)
48
49//_______________________________________________________________________
50AliMC::AliMC() :
51 fGenerator(0),
52 fEventEnergy(0),
53 fSummEnergy(0),
54 fSum2Energy(0),
55 fTrRmax(1.e10),
56 fTrZmax(1.e10),
90e48c0c 57 fRDecayMax(1.e10),
38ca2ad6 58 fRDecayMin(-1.),
90e48c0c 59 fDecayPdg(0),
5d12ce38 60 fImedia(0),
61 fTransParName("\0"),
62 fMCQA(0),
63 fHitLists(0),
64 fTrackReferences(0)
65
66{
d0d4a6b3 67 //default constructor
0561efeb 68 DecayLimits();
5d12ce38 69}
70
71//_______________________________________________________________________
72AliMC::AliMC(const char *name, const char *title) :
73 TVirtualMCApplication(name, title),
74 fGenerator(0),
75 fEventEnergy(0),
76 fSummEnergy(0),
77 fSum2Energy(0),
78 fTrRmax(1.e10),
79 fTrZmax(1.e10),
90e48c0c 80 fRDecayMax(1.e10),
38ca2ad6 81 fRDecayMin(-1.),
90e48c0c 82 fDecayPdg(0),
5d12ce38 83 fImedia(new TArrayI(1000)),
84 fTransParName("\0"),
85 fMCQA(0),
86 fHitLists(new TList()),
87 fTrackReferences(new TClonesArray("AliTrackReference", 100))
88{
d0d4a6b3 89 //constructor
5d12ce38 90 // Set transport parameters
91 SetTransPar();
0561efeb 92 DecayLimits();
5d12ce38 93 // Prepare the tracking medium lists
94 for(Int_t i=0;i<1000;i++) (*fImedia)[i]=-99;
5d12ce38 95}
96
97//_______________________________________________________________________
98AliMC::AliMC(const AliMC &mc) :
99 TVirtualMCApplication(mc),
100 fGenerator(0),
101 fEventEnergy(0),
102 fSummEnergy(0),
103 fSum2Energy(0),
104 fTrRmax(1.e10),
105 fTrZmax(1.e10),
90e48c0c 106 fRDecayMax(1.e10),
38ca2ad6 107 fRDecayMin(-1.),
90e48c0c 108 fDecayPdg(0),
5d12ce38 109 fImedia(0),
110 fTransParName("\0"),
111 fMCQA(0),
112 fHitLists(0),
113 fTrackReferences(0)
114{
115 //
90e48c0c 116 // Copy constructor for AliMC
5d12ce38 117 //
118 mc.Copy(*this);
119}
120
121//_______________________________________________________________________
122AliMC::~AliMC()
123{
d0d4a6b3 124 //destructor
5d12ce38 125 delete fGenerator;
126 delete fImedia;
127 delete fMCQA;
128 delete fHitLists;
129 // Delete track references
130 if (fTrackReferences) {
131 fTrackReferences->Delete();
132 delete fTrackReferences;
133 fTrackReferences = 0;
134 }
135
136}
137
138//_______________________________________________________________________
6c4904c2 139void AliMC::Copy(TObject &) const
5d12ce38 140{
d0d4a6b3 141 //dummy Copy function
21bf7095 142 AliFatal("Not implemented!");
5d12ce38 143}
144
145//_______________________________________________________________________
146void AliMC::ConstructGeometry()
147{
148 //
4a9de4af 149 // Either load geometry from file or create it through usual
150 // loop on detectors. In the first case the method
151 // AliModule::CreateMaterials() only builds fIdtmed and is postponed
152 // at InitGeometry().
5d12ce38 153 //
154
4a9de4af 155 if(gAlice->IsRootGeometry()){
156 // Load geometry
157 const char *geomfilename = gAlice->GetGeometryFileName();
158 if(gSystem->ExpandPathName(geomfilename)){
159 AliInfo(Form("Loading geometry from file:\n %40s\n\n",geomfilename));
160 TGeoManager::Import(geomfilename);
161 }else{
162 AliInfo(Form("Geometry file %40s not found!\n",geomfilename));
163 return;
164 }
165 }else{
166 // Create modules, materials, geometry
5d12ce38 167 TStopwatch stw;
168 TIter next(gAlice->Modules());
169 AliModule *detector;
21bf7095 170 AliDebug(1, "Geometry creation:");
5d12ce38 171 while((detector = dynamic_cast<AliModule*>(next()))) {
172 stw.Start();
173 // Initialise detector materials and geometry
174 detector->CreateMaterials();
175 detector->CreateGeometry();
21bf7095 176 AliInfo(Form("%10s R:%.2fs C:%.2fs",
177 detector->GetName(),stw.RealTime(),stw.CpuTime()));
5d12ce38 178 }
4a9de4af 179 }
180
5d12ce38 181}
182
183//_______________________________________________________________________
39b3f8ba 184Bool_t AliMC::MisalignGeometry()
185{
186// Call misalignment code if AliSimulation object was defined.
0717eed2 187
188 //Set alignable volumes for the whole geometry
189 SetAllAlignableVolumes();
190 // Misalign geometry via AliSimulation instance
39b3f8ba 191 if (!AliSimulation::GetInstance()) return kFALSE;
392808e8 192 AliGeomManager::SetGeometry(gGeoManager);
39b3f8ba 193 return AliSimulation::GetInstance()->MisalignGeometry(gAlice->GetRunLoader());
194}
195
196//_______________________________________________________________________
661663fa 197void AliMC::ConstructOpGeometry()
198{
199 //
200 // Loop all detector modules and call DefineOpticalProperties() method
201 //
202
203 TIter next(gAlice->Modules());
204 AliModule *detector;
205 AliInfo("Optical properties definition");
206 while((detector = dynamic_cast<AliModule*>(next()))) {
207 // Initialise detector optical properties
208 detector->DefineOpticalProperties();
209 }
210}
211
212//_______________________________________________________________________
5d12ce38 213void AliMC::InitGeometry()
214{
215 //
27cf3cdc 216 // Initialize detectors
5d12ce38 217 //
218
4a9de4af 219 AliInfo("Initialisation:");
220 TStopwatch stw;
221 TIter next(gAlice->Modules());
222 AliModule *detector;
223 while((detector = dynamic_cast<AliModule*>(next()))) {
224 stw.Start();
27cf3cdc 225 // Initialise detector geometry
4a9de4af 226 if(gAlice->IsRootGeometry()) detector->CreateMaterials();
227 detector->Init();
4a9de4af 228 AliInfo(Form("%10s R:%.2fs C:%.2fs",
229 detector->GetName(),stw.RealTime(),stw.CpuTime()));
230 }
4787b401 231}
232
233//_______________________________________________________________________
234void AliMC::SetAllAlignableVolumes()
235{
236 //
237 // Add alignable volumes (TGeoPNEntries) looping on all
238 // active modules
239 //
240
241 AliInfo(Form("Setting entries for all alignable volumes of active detectors"));
242 AliModule *detector;
243 TIter next(gAlice->Modules());
244 while((detector = dynamic_cast<AliModule*>(next()))) {
245 detector->AddAlignableVolumes();
246 }
5d12ce38 247}
248
249//_______________________________________________________________________
2057aecb 250void AliMC::GeneratePrimaries()
5d12ce38 251{
252 //
253 // Generate primary particles and fill them in the stack.
254 //
255
256 Generator()->Generate();
257}
258
259//_______________________________________________________________________
260void AliMC::SetGenerator(AliGenerator *generator)
261{
262 //
263 // Load the event generator
264 //
265 if(!fGenerator) fGenerator = generator;
266}
267
268//_______________________________________________________________________
269void AliMC::ResetGenerator(AliGenerator *generator)
270{
271 //
272 // Load the event generator
273 //
9a213abc 274 if(fGenerator) {
275 if(generator) {
21bf7095 276 AliWarning(Form("Replacing generator %s with %s",
9a213abc 277 fGenerator->GetName(),generator->GetName()));
278 }
279 else {
21bf7095 280 AliWarning(Form("Replacing generator %s with NULL",
281 fGenerator->GetName()));
9a213abc 282 }
283 }
5d12ce38 284 fGenerator = generator;
285}
286
287//_______________________________________________________________________
288void AliMC::FinishRun()
289{
290 // Clean generator information
21bf7095 291 AliDebug(1, "fGenerator->FinishRun()");
5d12ce38 292 fGenerator->FinishRun();
293
294 //Output energy summary tables
21bf7095 295 AliDebug(1, "EnergySummary()");
296 ToAliDebug(1, EnergySummary());
5d12ce38 297}
298
299//_______________________________________________________________________
300void AliMC::BeginPrimary()
301{
302 //
303 // Called at the beginning of each primary track
304 //
305
306 // Reset Hits info
307 ResetHits();
308 ResetTrackReferences();
309
310}
311
312//_______________________________________________________________________
313void AliMC::PreTrack()
314{
d0d4a6b3 315 // Actions before the track's transport
5d12ce38 316 TObjArray &dets = *gAlice->Modules();
317 AliModule *module;
318
319 for(Int_t i=0; i<=gAlice->GetNdets(); i++)
320 if((module = dynamic_cast<AliModule*>(dets[i])))
321 module->PreTrack();
322
323 fMCQA->PreTrack();
324}
325
326//_______________________________________________________________________
327void AliMC::Stepping()
328{
329 //
330 // Called at every step during transport
331 //
0561efeb 332
38ca2ad6 333 Int_t id = DetFromMate(gMC->CurrentMedium());
5d12ce38 334 if (id < 0) return;
335
0561efeb 336
337 if ( gMC->IsNewTrack() &&
338 gMC->TrackTime() == 0. &&
38ca2ad6 339 fRDecayMin >= 0. &&
0561efeb 340 fRDecayMax > fRDecayMin &&
341 gMC->TrackPid() == fDecayPdg )
342 {
343 FixParticleDecaytime();
344 }
345
346
347
5d12ce38 348 //
349 // --- If lego option, do it and leave
350 if (gAlice->Lego())
351 gAlice->Lego()->StepManager();
352 else {
353 Int_t copy;
354 //Update energy deposition tables
355 AddEnergyDeposit(gMC->CurrentVolID(copy),gMC->Edep());
ab03c084 356 //
357 // write tracke reference for track which is dissapearing - MI
b60e0f5e 358 if (gMC->IsTrackDisappeared()) {
359 if (gMC->Etot()>0.05) AddTrackReference(GetCurrentTrackNumber());
360 }
5d12ce38 361
362 //Call the appropriate stepping routine;
363 AliModule *det = dynamic_cast<AliModule*>(gAlice->Modules()->At(id));
364 if(det && det->StepManagerIsEnabled()) {
e569033b 365 if(AliLog::GetGlobalDebugLevel()>0) fMCQA->StepManager(id);
5d12ce38 366 det->StepManager();
367 }
368 }
369}
370
371//_______________________________________________________________________
372void AliMC::EnergySummary()
373{
374 //
375 // Print summary of deposited energy
376 //
377
378 Int_t ndep=0;
379 Float_t edtot=0;
380 Float_t ed, ed2;
381 Int_t kn, i, left, j, id;
382 const Float_t kzero=0;
383 Int_t ievent=gAlice->GetRunLoader()->GetHeader()->GetEvent()+1;
384 //
385 // Energy loss information
386 if(ievent) {
387 printf("***************** Energy Loss Information per event (GEV) *****************\n");
388 for(kn=1;kn<fEventEnergy.GetSize();kn++) {
389 ed=fSummEnergy[kn];
390 if(ed>0) {
391 fEventEnergy[ndep]=kn;
392 if(ievent>1) {
393 ed=ed/ievent;
394 ed2=fSum2Energy[kn];
395 ed2=ed2/ievent;
396 ed2=100*TMath::Sqrt(TMath::Max(ed2-ed*ed,kzero))/ed;
397 } else
398 ed2=99;
399 fSummEnergy[ndep]=ed;
400 fSum2Energy[ndep]=TMath::Min(static_cast<Float_t>(99.),TMath::Max(ed2,kzero));
401 edtot+=ed;
402 ndep++;
403 }
404 }
405 for(kn=0;kn<(ndep-1)/3+1;kn++) {
406 left=ndep-kn*3;
407 for(i=0;i<(3<left?3:left);i++) {
408 j=kn*3+i;
409 id=Int_t (fEventEnergy[j]+0.1);
410 printf(" %s %10.3f +- %10.3f%%;",gMC->VolName(id),fSummEnergy[j],fSum2Energy[j]);
411 }
412 printf("\n");
413 }
414 //
415 // Relative energy loss in different detectors
416 printf("******************** Relative Energy Loss per event ********************\n");
417 printf("Total energy loss per event %10.3f GeV\n",edtot);
418 for(kn=0;kn<(ndep-1)/5+1;kn++) {
419 left=ndep-kn*5;
420 for(i=0;i<(5<left?5:left);i++) {
421 j=kn*5+i;
422 id=Int_t (fEventEnergy[j]+0.1);
423 printf(" %s %10.3f%%;",gMC->VolName(id),100*fSummEnergy[j]/edtot);
424 }
425 printf("\n");
426 }
427 for(kn=0;kn<75;kn++) printf("*");
428 printf("\n");
429 }
430 //
431 // Reset the TArray's
432 // fEventEnergy.Set(0);
433 // fSummEnergy.Set(0);
434 // fSum2Energy.Set(0);
435}
436
437//_____________________________________________________________________________
438void AliMC::BeginEvent()
439{
440 //
441 // Clean-up previous event
442 // Energy scores
21bf7095 443 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
444 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
445 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
446 AliDebug(1, " BEGINNING EVENT ");
447 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
448 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
449 AliDebug(1, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
5d12ce38 450
451 AliRunLoader *runloader=gAlice->GetRunLoader();
452
453 /*******************************/
454 /* Clean after eventual */
455 /* previous event */
456 /*******************************/
457
458
459 //Set the next event in Run Loader -> Cleans trees (TreeK and all trees in detectors),
460 gAlice->SetEventNrInRun(gAlice->GetEventNrInRun()+1);
461 runloader->SetEventNumber(gAlice->GetEventNrInRun());// sets new files, cleans the previous event stuff, if necessary, etc.,
21bf7095 462 AliDebug(1, Form("EventNr is %d",gAlice->GetEventNrInRun()));
5d12ce38 463
464 fEventEnergy.Reset();
465 // Clean detector information
466
467 if (runloader->Stack())
468 runloader->Stack()->Reset();//clean stack -> tree is unloaded
469 else
470 runloader->MakeStack();//or make a new one
504b172d 471
472 if(gAlice->Lego() == 0x0)
473 {
21bf7095 474 AliDebug(1, "fRunLoader->MakeTree(K)");
504b172d 475 runloader->MakeTree("K");
476 }
477
21bf7095 478 AliDebug(1, "gMC->SetStack(fRunLoader->Stack())");
5d12ce38 479 gMC->SetStack(gAlice->GetRunLoader()->Stack());//Was in InitMC - but was moved here
480 //because we don't have guarantee that
481 //stack pointer is not going to change from event to event
482 //since it bellobgs to header and is obtained via RunLoader
483 //
484 // Reset all Detectors & kinematics & make/reset trees
485 //
486
487 runloader->GetHeader()->Reset(gAlice->GetRunNumber(),gAlice->GetEvNumber(),
488 gAlice->GetEventNrInRun());
489// fRunLoader->WriteKinematics("OVERWRITE"); is there any reason to rewrite here since MakeTree does so
490
504b172d 491 if(gAlice->Lego())
492 {
493 gAlice->Lego()->BeginEvent();
494 return;
495 }
496
5d12ce38 497
21bf7095 498 AliDebug(1, "ResetHits()");
5d12ce38 499 ResetHits();
504b172d 500
21bf7095 501 AliDebug(1, "fRunLoader->MakeTree(H)");
5d12ce38 502 runloader->MakeTree("H");
504b172d 503
21bf7095 504 AliDebug(1, "fRunLoader->MakeTrackRefsContainer()");
504b172d 505 runloader->MakeTrackRefsContainer();//for insurance
5d12ce38 506
5d12ce38 507
508 //create new branches and SetAdresses
509 TIter next(gAlice->Modules());
510 AliModule *detector;
511 while((detector = (AliModule*)next()))
512 {
21bf7095 513 AliDebug(2, Form("%s->MakeBranch(H)",detector->GetName()));
5d12ce38 514 detector->MakeBranch("H");
21bf7095 515 AliDebug(2, Form("%s->MakeBranchTR()",detector->GetName()));
5d12ce38 516 detector->MakeBranchTR();
21bf7095 517 AliDebug(2, Form("%s->SetTreeAddress()",detector->GetName()));
5d12ce38 518 detector->SetTreeAddress();
519 }
ab03c084 520 // make branch for AliRun track References
521 TTree * treeTR = runloader->TreeTR();
522 if (treeTR){
523 // make branch for central track references
524 if (!fTrackReferences) fTrackReferences = new TClonesArray("AliTrackReference",0);
525 TBranch *branch;
526 branch = treeTR->Branch("AliRun",&fTrackReferences);
527 branch->SetAddress(&fTrackReferences);
528 }
529 //
5d12ce38 530}
531
532//_______________________________________________________________________
533void AliMC::ResetHits()
534{
535 //
536 // Reset all Detectors hits
537 //
538 TIter next(gAlice->Modules());
539 AliModule *detector;
540 while((detector = dynamic_cast<AliModule*>(next()))) {
541 detector->ResetHits();
542 }
543}
544
545//_______________________________________________________________________
546void AliMC::PostTrack()
547{
d0d4a6b3 548 // Posts tracks for each module
4a9de4af 549 TObjArray &dets = *gAlice->Modules();
550 AliModule *module;
551
552 for(Int_t i=0; i<=gAlice->GetNdets(); i++)
553 if((module = dynamic_cast<AliModule*>(dets[i])))
554 module->PostTrack();
5d12ce38 555}
556
557//_______________________________________________________________________
558void AliMC::FinishPrimary()
559{
560 //
561 // Called at the end of each primary track
562 //
563 AliRunLoader *runloader=gAlice->GetRunLoader();
564 // static Int_t count=0;
565 // const Int_t times=10;
566 // This primary is finished, purify stack
c75cfb51 567#if ROOT_VERSION_CODE > 262152
05c7517d 568 if (!(gMC->SecondariesAreOrdered()))
569 runloader->Stack()->ReorderKine();
c75cfb51 570#endif
5d12ce38 571 runloader->Stack()->PurifyKine();
504b172d 572
5d12ce38 573 TIter next(gAlice->Modules());
574 AliModule *detector;
575 while((detector = dynamic_cast<AliModule*>(next()))) {
576 detector->FinishPrimary();
504b172d 577 AliLoader* loader = detector->GetLoader();
578 if(loader)
579 {
580 TTree* treeH = loader->TreeH();
581 if (treeH) treeH->Fill(); //can be Lego run and treeH can not exist
582 }
5d12ce38 583 }
584
585 // Write out track references if any
586 if (runloader->TreeTR()) runloader->TreeTR()->Fill();
587}
588
589//_______________________________________________________________________
590void AliMC::FinishEvent()
591{
592 //
593 // Called at the end of the event.
594 //
595
596 //
38ca2ad6 597
5d12ce38 598 if(gAlice->Lego()) gAlice->Lego()->FinishEvent();
599
600 TIter next(gAlice->Modules());
601 AliModule *detector;
602 while((detector = dynamic_cast<AliModule*>(next()))) {
603 detector->FinishEvent();
604 }
605
606 //Update the energy deposit tables
607 Int_t i;
608 for(i=0;i<fEventEnergy.GetSize();i++)
609 {
610 fSummEnergy[i]+=fEventEnergy[i];
611 fSum2Energy[i]+=fEventEnergy[i]*fEventEnergy[i];
612 }
613
614 AliRunLoader *runloader=gAlice->GetRunLoader();
615
616 AliHeader* header = runloader->GetHeader();
617 AliStack* stack = runloader->Stack();
618 if ( (header == 0x0) || (stack == 0x0) )
619 {//check if we got header and stack. If not cry and exit aliroot
21bf7095 620 AliFatal("Can not get the stack or header from LOADER");
5d12ce38 621 return;//never reached
622 }
623 // Update Header information
624 header->SetNprimary(stack->GetNprimary());
625 header->SetNtrack(stack->GetNtrack());
626
627
628 // Write out the kinematics
38ca2ad6 629 if (!gAlice->Lego()) stack->FinishEvent();
5d12ce38 630
631 // Write out the event Header information
632 TTree* treeE = runloader->TreeE();
633 if (treeE)
634 {
635 header->SetStack(stack);
636 treeE->Fill();
637 }
638 else
639 {
21bf7095 640 AliError("Can not get TreeE from RL");
5d12ce38 641 }
642
504b172d 643 if(gAlice->Lego() == 0x0)
644 {
645 runloader->WriteKinematics("OVERWRITE");
646 runloader->WriteTrackRefs("OVERWRITE");
647 runloader->WriteHits("OVERWRITE");
648 }
649
21bf7095 650 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
651 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
652 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
653 AliDebug(1, " FINISHING EVENT ");
654 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
655 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
656 AliDebug(1, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
5d12ce38 657}
658
659//_______________________________________________________________________
660void AliMC::Field(const Double_t* x, Double_t* b) const
661{
d0d4a6b3 662 // Calculates field "b" at point "x"
5d12ce38 663 gAlice->Field(x,b);
664}
665
666//_______________________________________________________________________
667void AliMC::Init()
668{
d0d4a6b3 669 // MC initialization
5d12ce38 670
671 //=================Create Materials and geometry
672 gMC->Init();
4e889ff4 673 // Set alignable volumes for the whole geometry (with old root)
674#if ROOT_VERSION_CODE < 331527
675 SetAllAlignableVolumes();
676#endif
5d12ce38 677 //Read the cuts for all materials
678 ReadTransPar();
679 //Build the special IMEDIA table
680 MediaTable();
681
682 //Compute cross-sections
683 gMC->BuildPhysics();
684
5d12ce38 685 //Initialise geometry deposition table
686 fEventEnergy.Set(gMC->NofVolumes()+1);
687 fSummEnergy.Set(gMC->NofVolumes()+1);
688 fSum2Energy.Set(gMC->NofVolumes()+1);
689
690 //
691 fMCQA = new AliMCQA(gAlice->GetNdets());
692
693 // Register MC in configuration
694 AliConfig::Instance()->Add(gMC);
695
696}
697
698//_______________________________________________________________________
699void AliMC::MediaTable()
700{
701 //
702 // Built media table to get from the media number to
703 // the detector id
704 //
705
706 Int_t kz, nz, idt, lz, i, k, ind;
707 // Int_t ibeg;
708 TObjArray &dets = *gAlice->Detectors();
709 AliModule *det;
710 Int_t ndets=gAlice->GetNdets();
711 //
712 // For all detectors
713 for (kz=0;kz<ndets;kz++) {
714 // If detector is defined
715 if((det=dynamic_cast<AliModule*>(dets[kz]))) {
716 TArrayI &idtmed = *(det->GetIdtmed());
717 for(nz=0;nz<100;nz++) {
0561efeb 718
5d12ce38 719 // Find max and min material number
720 if((idt=idtmed[nz])) {
721 det->LoMedium() = det->LoMedium() < idt ? det->LoMedium() : idt;
722 det->HiMedium() = det->HiMedium() > idt ? det->HiMedium() : idt;
723 }
724 }
725 if(det->LoMedium() > det->HiMedium()) {
726 det->LoMedium() = 0;
727 det->HiMedium() = 0;
728 } else {
729 if(det->HiMedium() > fImedia->GetSize()) {
21bf7095 730 AliError(Form("Increase fImedia from %d to %d",
731 fImedia->GetSize(),det->HiMedium()));
5d12ce38 732 return;
733 }
734 // Tag all materials in rage as belonging to detector kz
735 for(lz=det->LoMedium(); lz<= det->HiMedium(); lz++) {
736 (*fImedia)[lz]=kz;
737 }
738 }
739 }
740 }
741 //
742 // Print summary table
21bf7095 743 AliInfo("Tracking media ranges:");
744 ToAliInfo(
5d12ce38 745 for(i=0;i<(ndets-1)/6+1;i++) {
746 for(k=0;k< (6<ndets-i*6?6:ndets-i*6);k++) {
747 ind=i*6+k;
748 det=dynamic_cast<AliModule*>(dets[ind]);
749 if(det)
750 printf(" %6s: %3d -> %3d;",det->GetName(),det->LoMedium(),
751 det->HiMedium());
752 else
753 printf(" %6s: %3d -> %3d;","NULL",0,0);
754 }
755 printf("\n");
756 }
21bf7095 757 )
5d12ce38 758}
759
760//_______________________________________________________________________
761void AliMC::ReadTransPar()
762{
763 //
764 // Read filename to set the transport parameters
765 //
766
767
768 const Int_t kncuts=10;
769 const Int_t knflags=11;
770 const Int_t knpars=kncuts+knflags;
771 const char kpars[knpars][7] = {"CUTGAM" ,"CUTELE","CUTNEU","CUTHAD","CUTMUO",
772 "BCUTE","BCUTM","DCUTE","DCUTM","PPCUTM","ANNI",
773 "BREM","COMP","DCAY","DRAY","HADR","LOSS",
774 "MULS","PAIR","PHOT","RAYL"};
775 char line[256];
776 char detName[7];
777 char* filtmp;
778 Float_t cut[kncuts];
779 Int_t flag[knflags];
780 Int_t i, itmed, iret, ktmed, kz;
781 FILE *lun;
782 //
783 // See whether the file is there
784 filtmp=gSystem->ExpandPathName(fTransParName.Data());
785 lun=fopen(filtmp,"r");
786 delete [] filtmp;
787 if(!lun) {
21bf7095 788 AliWarning(Form("File %s does not exist!",fTransParName.Data()));
5d12ce38 789 return;
790 }
791 //
5d12ce38 792 while(1) {
793 // Initialise cuts and flags
794 for(i=0;i<kncuts;i++) cut[i]=-99;
795 for(i=0;i<knflags;i++) flag[i]=-99;
796 itmed=0;
797 for(i=0;i<256;i++) line[i]='\0';
798 // Read up to the end of line excluded
799 iret=fscanf(lun,"%[^\n]",line);
800 if(iret<0) {
801 //End of file
802 fclose(lun);
5d12ce38 803 return;
804 }
805 // Read the end of line
806 fscanf(lun,"%*c");
807 if(!iret) continue;
808 if(line[0]=='*') continue;
809 // Read the numbers
810 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",
811 detName,&itmed,&cut[0],&cut[1],&cut[2],&cut[3],&cut[4],&cut[5],&cut[6],&cut[7],&cut[8],
812 &cut[9],&flag[0],&flag[1],&flag[2],&flag[3],&flag[4],&flag[5],&flag[6],&flag[7],
813 &flag[8],&flag[9],&flag[10]);
814 if(!iret) continue;
815 if(iret<0) {
816 //reading error
21bf7095 817 AliWarning(Form("Error reading file %s",fTransParName.Data()));
5d12ce38 818 continue;
819 }
820 // Check that the module exist
821 AliModule *mod = gAlice->GetModule(detName);
822 if(mod) {
823 // Get the array of media numbers
824 TArrayI &idtmed = *mod->GetIdtmed();
825 // Check that the tracking medium code is valid
826 if(0<=itmed && itmed < 100) {
827 ktmed=idtmed[itmed];
828 if(!ktmed) {
21bf7095 829 AliWarning(Form("Invalid tracking medium code %d for %s",itmed,mod->GetName()));
5d12ce38 830 continue;
831 }
832 // Set energy thresholds
833 for(kz=0;kz<kncuts;kz++) {
834 if(cut[kz]>=0) {
21bf7095 835 AliDebug(2, Form("%-6s set to %10.3E for tracking medium code %4d for %s",
836 kpars[kz],cut[kz],itmed,mod->GetName()));
5d12ce38 837 gMC->Gstpar(ktmed,kpars[kz],cut[kz]);
838 }
839 }
840 // Set transport mechanisms
841 for(kz=0;kz<knflags;kz++) {
842 if(flag[kz]>=0) {
21bf7095 843 AliDebug(2, Form("%-6s set to %10d for tracking medium code %4d for %s",
844 kpars[kncuts+kz],flag[kz],itmed,mod->GetName()));
5d12ce38 845 gMC->Gstpar(ktmed,kpars[kncuts+kz],Float_t(flag[kz]));
846 }
847 }
848 } else {
21bf7095 849 AliWarning(Form("Invalid medium code %d",itmed));
5d12ce38 850 continue;
851 }
852 } else {
21bf7095 853 AliDebug(1, Form("%s not present",detName));
5d12ce38 854 continue;
855 }
856 }
857}
858
859//_______________________________________________________________________
860void AliMC::SetTransPar(const char *filename)
861{
862 //
863 // Sets the file name for transport parameters
864 //
865 fTransParName = filename;
866}
867
868//_______________________________________________________________________
0054628d 869void AliMC::Browse(TBrowser *b)
5d12ce38 870{
871 //
872 // Called when the item "Run" is clicked on the left pane
873 // of the Root browser.
874 // It displays the Root Trees and all detectors.
875 //
876 //detectors are in folders anyway
877 b->Add(fMCQA,"AliMCQA");
878}
879
5d12ce38 880//_______________________________________________________________________
881void AliMC::AddHit(Int_t id, Int_t track, Int_t *vol, Float_t *hits) const
882{
883 //
884 // Add a hit to detector id
885 //
886 TObjArray &dets = *gAlice->Modules();
887 if(dets[id]) dynamic_cast<AliModule*>(dets[id])->AddHit(track,vol,hits);
888}
889
890//_______________________________________________________________________
891void AliMC::AddDigit(Int_t id, Int_t *tracks, Int_t *digits) const
892{
893 //
894 // Add digit to detector id
895 //
896 TObjArray &dets = *gAlice->Modules();
897 if(dets[id]) dynamic_cast<AliModule*>(dets[id])->AddDigit(tracks,digits);
898}
899
900//_______________________________________________________________________
901Int_t AliMC::GetCurrentTrackNumber() const {
902 //
903 // Returns current track
904 //
905 return gAlice->GetRunLoader()->Stack()->GetCurrentTrackNumber();
906}
907
908//_______________________________________________________________________
909void AliMC::DumpPart (Int_t i) const
910{
911 //
912 // Dumps particle i in the stack
913 //
914 AliRunLoader * runloader = gAlice->GetRunLoader();
915 if (runloader->Stack())
916 runloader->Stack()->DumpPart(i);
917}
918
919//_______________________________________________________________________
920void AliMC::DumpPStack () const
921{
922 //
923 // Dumps the particle stack
924 //
925 AliRunLoader * runloader = gAlice->GetRunLoader();
926 if (runloader->Stack())
927 runloader->Stack()->DumpPStack();
928}
929
930//_______________________________________________________________________
931Int_t AliMC::GetNtrack() const {
932 //
933 // Returns number of tracks in stack
934 //
935 Int_t ntracks = -1;
936 AliRunLoader * runloader = gAlice->GetRunLoader();
937 if (runloader->Stack())
938 ntracks = runloader->Stack()->GetNtrack();
939 return ntracks;
940}
941
942//_______________________________________________________________________
943Int_t AliMC::GetPrimary(Int_t track) const
944{
945 //
946 // return number of primary that has generated track
947 //
948 Int_t nprimary = -999;
949 AliRunLoader * runloader = gAlice->GetRunLoader();
950 if (runloader->Stack())
951 nprimary = runloader->Stack()->GetPrimary(track);
952 return nprimary;
953}
954
955//_______________________________________________________________________
956TParticle* AliMC::Particle(Int_t i) const
957{
d0d4a6b3 958 // Returns the i-th particle from the stack taking into account
959 // the remaping done by PurifyKine
5d12ce38 960 AliRunLoader * runloader = gAlice->GetRunLoader();
961 if (runloader)
962 if (runloader->Stack())
963 return runloader->Stack()->Particle(i);
964 return 0x0;
965}
966
967//_______________________________________________________________________
968TObjArray* AliMC::Particles() const {
969 //
970 // Returns pointer to Particles array
971 //
972 AliRunLoader * runloader = gAlice->GetRunLoader();
973 if (runloader)
974 if (runloader->Stack())
975 return runloader->Stack()->Particles();
976 return 0x0;
977}
978
979//_______________________________________________________________________
980void AliMC::PushTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
981 Float_t *vpos, Float_t *polar, Float_t tof,
2057aecb 982 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is) const
5d12ce38 983{
984// Delegate to stack
985//
986 AliRunLoader * runloader = gAlice->GetRunLoader();
987 if (runloader)
988 if (runloader->Stack())
989 runloader->Stack()->PushTrack(done, parent, pdg, pmom, vpos, polar, tof,
990 mech, ntr, weight, is);
991}
992
993//_______________________________________________________________________
994void AliMC::PushTrack(Int_t done, Int_t parent, Int_t pdg,
995 Double_t px, Double_t py, Double_t pz, Double_t e,
996 Double_t vx, Double_t vy, Double_t vz, Double_t tof,
997 Double_t polx, Double_t poly, Double_t polz,
2057aecb 998 TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is) const
5d12ce38 999{
1000 // Delegate to stack
1001 //
1002 AliRunLoader * runloader = gAlice->GetRunLoader();
1003 if (runloader)
1004 if (runloader->Stack())
1005 runloader->Stack()->PushTrack(done, parent, pdg, px, py, pz, e, vx, vy, vz, tof,
1006 polx, poly, polz, mech, ntr, weight, is);
1007}
1008
1009//_______________________________________________________________________
2057aecb 1010void AliMC::SetHighWaterMark(Int_t nt) const
5d12ce38 1011{
1012 //
1013 // Set high water mark for last track in event
1014 AliRunLoader * runloader = gAlice->GetRunLoader();
1015 if (runloader)
1016 if (runloader->Stack())
1017 runloader->Stack()->SetHighWaterMark(nt);
1018}
1019
1020//_______________________________________________________________________
2057aecb 1021void AliMC::KeepTrack(Int_t track) const
5d12ce38 1022{
1023 //
1024 // Delegate to stack
1025 //
1026 AliRunLoader * runloader = gAlice->GetRunLoader();
1027 if (runloader)
1028 if (runloader->Stack())
1029 runloader->Stack()->KeepTrack(track);
1030}
1031
1032//_______________________________________________________________________
2057aecb 1033void AliMC::FlagTrack(Int_t track) const
5d12ce38 1034{
1035 // Delegate to stack
1036 //
1037 AliRunLoader * runloader = gAlice->GetRunLoader();
1038 if (runloader)
1039 if (runloader->Stack())
1040 runloader->Stack()->FlagTrack(track);
1041}
1042
1043//_______________________________________________________________________
2057aecb 1044void AliMC::SetCurrentTrack(Int_t track) const
5d12ce38 1045{
1046 //
1047 // Set current track number
1048 //
1049 AliRunLoader * runloader = gAlice->GetRunLoader();
1050 if (runloader)
1051 if (runloader->Stack())
1052 runloader->Stack()->SetCurrentTrack(track);
1053}
1054
1055//_______________________________________________________________________
1056void AliMC::AddTrackReference(Int_t label){
1057 //
1058 // add a trackrefernce to the list
1059 if (!fTrackReferences) {
21bf7095 1060 AliError("Container trackrefernce not active");
5d12ce38 1061 return;
1062 }
1063 Int_t nref = fTrackReferences->GetEntriesFast();
1064 TClonesArray &lref = *fTrackReferences;
1065 new(lref[nref]) AliTrackReference(label);
1066}
1067
1068
1069
1070//_______________________________________________________________________
1071void AliMC::ResetTrackReferences()
1072{
1073 //
1074 // Reset all references
1075 //
1076 if (fTrackReferences) fTrackReferences->Clear();
1077
1078 TIter next(gAlice->Modules());
1079 AliModule *detector;
1080 while((detector = dynamic_cast<AliModule*>(next()))) {
1081 detector->ResetTrackReferences();
1082 }
1083}
1084
1085void AliMC::RemapTrackReferencesIDs(Int_t *map)
1086{
1087 //
1088 // Remapping track reference
1089 // Called at finish primary
1090 //
1091 if (!fTrackReferences) return;
4e490a96 1092 Int_t nEntries = fTrackReferences->GetEntries();
1093 for (Int_t i=0; i < nEntries; i++){
1094 AliTrackReference * ref = dynamic_cast<AliTrackReference*>(fTrackReferences->UncheckedAt(i));
1095 if (ref) {
1096 Int_t newID = map[ref->GetTrack()];
1097 if (newID>=0) ref->SetTrack(newID);
1098 else {
1099 ref->SetBit(kNotDeleted,kFALSE);
1100 fTrackReferences->RemoveAt(i);
1101 }
1102 } // if ref
5d12ce38 1103 }
1104 fTrackReferences->Compress();
1105}
0561efeb 1106
1107void AliMC::FixParticleDecaytime()
1108{
1109 //
1110 // Fix the particle decay time according to rmin and rmax for decays
1111 //
1112
1113 TLorentzVector p;
1114 gMC->TrackMomentum(p);
1115 Double_t tmin, tmax;
1116 Double_t b;
1117
1118 // Transverse velocity
1119 Double_t vt = p.Pt() / p.E();
1120
1121 if ((b = gAlice->Field()->SolenoidField()) > 0.) { // [kG]
1122
1123 // Radius of helix
1124
1125 Double_t rho = p.Pt() / 0.0003 / b; // [cm]
1126
1127 // Revolution frequency
1128
1129 Double_t omega = vt / rho;
1130
1131 // Maximum and minimum decay time
1132 //
1133 // Check for curlers first
1134 if (fRDecayMax * fRDecayMax / rho / rho / 2. > 1.) return;
1135
1136 //
1137
1138 tmax = TMath::ACos(1. - fRDecayMax * fRDecayMax / rho / rho / 2.) / omega; // [ct]
1139 tmin = TMath::ACos(1. - fRDecayMin * fRDecayMin / rho / rho / 2.) / omega; // [ct]
1140 } else {
1141 tmax = fRDecayMax / vt; // [ct]
1142 tmin = fRDecayMin / vt; // [ct]
1143 }
1144
1145 //
1146 // Dial t using the two limits
1147 Double_t t = tmin + (tmax - tmin) * gRandom->Rndm(); // [ct]
1148 //
1149 //
1150 // Force decay time in transport code
1151 //
fa7ca4da 1152 gMC->ForceDecayTime(t / 2.99792458e10);
0561efeb 1153}