EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairMCApplication.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairMCApplication.cxx
1 // -------------------------------------------------------------------------
2 // ----- FairDetector source file -----
3 // ----- Created 06/01/04 by M. Al-Turany -----
4 // -------------------------------------------------------------------------
5 
6 
7 #include "FairMCApplication.h"
8 #include "FairGenericStack.h"
9 #include "FairField.h"
10 #include "FairModule.h"
11 #include "FairDetector.h"
12 #include "FairRunSim.h"
13 #include "FairIon.h"
14 #include "FairMCEventHeader.h"
15 #include "FairEventHeader.h"
16 #include "FairPrimaryGenerator.h"
17 #include "FairTrajFilter.h"
18 #include "FairRootManager.h"
19 #include "FairTask.h"
20 #include "FairVolume.h"
21 #include "FairGeoLoader.h"
22 #include "FairGeoInterface.h"
23 #include "FairGeoMedia.h"
24 #include "FairGeoMedium.h"
25 #include "FairRadLenManager.h"
26 #include "FairRadGridManager.h"
27 #include "FairRadMapManager.h"
28 #include "FairMesh.h"
29 #include "FairRuntimeDb.h"
30 #include "FairLogger.h"
31 #include "FairRunInfo.h"
32 
33 #include "TObjArray.h"
34 #include "TGeoTrack.h"
35 #include "TGeoVolume.h"
36 #include "TParticle.h"
37 #include "TGeoManager.h"
38 #include "TRefArray.h"
39 #include "TROOT.h"
40 #include "TInterpreter.h"
41 #include "TVirtualMC.h"
42 #include "TDatabasePDG.h"
43 #include "TGeoTrack.h"
44 #include "TGeoVolume.h"
45 #include "TParticle.h"
46 #include "TGeoManager.h"
47 #include "TParticlePDG.h"
48 #include "TMCParticleType.h"
49 #include "THashList.h"
50 #include "TSystem.h"
51 
52 #include <iostream>
53 
54 using std::cout;
55 using std::endl;
56 using std::pair;
57 //_____________________________________________________________________________
59  TObjArray* ModList, const char* MatName)
60  :TVirtualMCApplication(name,title),
61  fActDetIter(NULL),
62  fActiveDetectors(NULL),
63  fFairTaskList(NULL),
64  fDetIter(NULL),
65  fDetectors(NULL),
66  fDetMap(NULL),
67  fLogger(FairLogger::GetLogger()),
68  fModIter(NULL),
69  fModules(NULL),
70  fNoSenVolumes(0),
71  fPythiaDecayer(kFALSE),
72  fPythiaDecayerConfig(""),
73  fStack(NULL),
74  fRootManager(NULL),
75  fSenVolumes(NULL),
76  fxField(NULL),
77  fEvGen(NULL),
78  fMcVersion(-1),
79  fTrajFilter(NULL),
80  fTrajAccepted(kFALSE),
81  fUserDecay(kFALSE),
82  fUserDecayConfig(""),
83  fDebug(kFALSE),
84  fDisVol(NULL),
85  fDisDet(NULL),
86  fVolMap(),
87  //fVolIter(NULL),
88  fModVolMap(),
89  //fModVolIter(NULL),
90  fTrkPos(TLorentzVector(0,0,0,0)),
91  fRadLength(kFALSE),
92  fRadLenMan(NULL),
93  fRadMap(kFALSE),
94  fRadMapMan(NULL),
95  fRadGridMan(NULL),
96  fEventHeader(NULL),
97  fMCEventHeader(NULL),
98  fRunInfo()
99 {
100 // Standard Simulation constructor
101 // Check if the Fair root manager exist!
103 // Create an ObjArray of Modules and its iterator
104  fModules=ModList;
105  fModIter = fModules->MakeIterator();
106 // Create and fill a list of active detectors
107  fDetectors=new TRefArray;
108  fActiveDetectors=new TRefArray();
109  fModIter->Reset();
110  FairDetector* detector;
111  TObject* obj;
112  while((obj=fModIter->Next())) {
113  if(obj->InheritsFrom("FairDetector")) {
114  detector=(FairDetector*)obj;
115  fDetectors->Add(detector);
116  if(detector->IsActive()) {
117  fActiveDetectors->Add(detector);
118  }
119  }
120  }
121  fDetIter=fDetectors->MakeIterator();
122  fActDetIter=fActiveDetectors->MakeIterator();
123 // Create a Task list
124  fFairTaskList= new FairTask("Task List", 1);
125  gROOT->GetListOfBrowsables()->Add(fFairTaskList);
126  fMcVersion=-1;
127  // Initialise fTrajFilter pointer
128  fTrajFilter = NULL;
129  fDetMap=new TRefArray(1000);
130  fDisVol=0;
131  fDisDet=0;
132 
133 }
134 //_____________________________________________________________________________
136  :TVirtualMCApplication(),
137  fActDetIter(0),
138  fActiveDetectors(0),
139  fFairTaskList(0),
140  fDetIter(0),
141  fDetectors(0),
142  fDetMap(0),
143  fLogger(FairLogger::GetLogger()),
144  fModIter(0),
145  fModules(0),
146  fNoSenVolumes(0),
147  fPythiaDecayer(kFALSE),
148  fPythiaDecayerConfig(""),
149  fStack(0),
150  fRootManager(0),
151  fSenVolumes(0),
152  fxField(0),
153  fEvGen(0),
154  fMcVersion(-1),
155  fTrajFilter(NULL),
156  fTrajAccepted(kFALSE),
157  fUserDecay(kFALSE),
158  fUserDecayConfig(""),
159  fDebug(kFALSE),
160  fDisVol(0),
161  fDisDet(0),
162  fVolMap(),
163  //fVolIter(NULL),
164  fModVolMap(),
165  //fModVolIter(NULL),
166  fTrkPos(TLorentzVector(0,0,0,0)),
167  fRadLength(kFALSE),
168  fRadLenMan(NULL),
169  fRadMap(kFALSE),
170  fRadMapMan(NULL),
171  fRadGridMan(NULL),
172  fEventHeader(NULL),
173  fMCEventHeader(NULL),
174  fRunInfo()
175 {
176 // Default constructor
177 }
178 //_____________________________________________________________________________
180 {
181 // Destructor
182 // cout<<"Enter Destructor of FairMCApplication"<<endl;
183  delete fStack;
184  delete fActiveDetectors; // don't do fActiveDetectors->Delete() here
185  // the modules are already deleted in FairRunSim
186  delete fActDetIter;
187  delete fDetectors;
188  delete gMC;
189  //@W@gMC=0;
190  // cout<<"Leave Destructor of FairMCApplication"<<endl;
191 }
192 //_____________________________________________________________________________
194 {
195 // Registers stack in Root manager.
196 // cout << "FairMCApplication::RegisterStack() " << endl;
197  if(fEvGen) {
198  fStack->Register();
199  }
200 }
201 //_____________________________________________________________________________
202 void FairMCApplication::InitMC(const char* setup, const char* cuts)
203 {
204 // Initialize MC.
205 // ---
206  fStack = (FairGenericStack*) gMC->GetStack();
207 
208  gMC->SetMagField(fxField);
209 
210  gMC->Init();
211  gMC->BuildPhysics();
212  TString MCName=gMC->GetName();
213  if (MCName == "TGeant3" || MCName == "TGeant3TGeo") {
214  fMcVersion = 0 ;
215  } else if(MCName == "TGeant4") {
216  fMcVersion = 1;
217  } else if(MCName == "TFluka") {
218  fMcVersion = 2;
219  } else {
220  fMcVersion = 3; //Geane
221  }
223 
224  fLogger->Info(MESSAGE_ORIGIN, "Monte carlo Engine Initialisation with : %s ", MCName.Data());
225 }
226 //_____________________________________________________________________________
227 void FairMCApplication::RunMC(Int_t nofEvents)
228 {
229  // Reset the time for FairRunInfo. Otherwise the time of the
230  // first event will include the time needed for initilization.
231  fRunInfo.Reset();
232  // MC run.
233  gMC->ProcessRun(nofEvents);
234  // finish run
235  FinishRun();
236  // Save histograms with memory and runtime information in the output file
237  if (FairRunSim::Instance()->GetWriteRunInfoFile()) {
239  }
240 }
241 //____________________________________________________________________________
243 {
244 // Finish MC run.
245 // ---
246  if(fActDetIter) {
247  fActDetIter->Reset();
248  FairDetector* detector=NULL;
249  TObject* obj=0;
250  while((obj=fActDetIter->Next())) {
251  detector = dynamic_cast<FairDetector*>(obj);
252  if (detector) {
253  detector->FinishRun();
254  }
255  }
256  }
258  //fRootManager->Fill();
259 
261  //FairMCEventHeader* header = gen->GetEvent();
262  Int_t nprimary = gen->GetTotPrimary();
263  TObjArray* meshlist = NULL;
264 
265  if (fRadGridMan ) {
266  // cout << "-I FairMCApplication::FinishRun scaling ... " << endl;
267  meshlist = fRadGridMan->GetMeshList();
268  // cout << " entries " << meshlist->GetEntriesFast() << endl;
269  for(Int_t i=0; i<meshlist->GetEntriesFast(); i++ ) {
270  FairMesh* aMesh = (FairMesh*) meshlist->At(i);
271  aMesh->Scale(1./nprimary);
272  }
273  }
274  if (fRadGridMan) {
275  TH2D* tid = NULL;
276  TH2D* flu = NULL;
277  TH2D* seu = NULL;
278  // cout << "-I FairMCApplication::FinishRun saving ... " << endl;
279  // cout << " entries " << meshlist->GetEntriesFast() << endl;
280 
281  cout << endl << endl;
282  cout << "======================================================="
283  << endl;
284  cout << " Dosimetry histos saving " << endl << endl;
285  cout << "======================================================="
286  << endl;
287  cout << endl << endl;
288 
289  gDirectory->mkdir("Dosimetry");
290  gDirectory->cd("Dosimetry");
291  gDirectory->cd("");
292 
293 
294  for(Int_t i=0; i<meshlist->GetEntriesFast(); i++ ) {
295  FairMesh* aMesh = (FairMesh*) meshlist->At(i);
296  tid = aMesh->GetMeshTid();
297  flu = aMesh->GetMeshFlu();
298  seu = aMesh->GetMeshSEU();
299  //
300  // tid->Dump();
301  tid->Write();
302  flu->Write();
303  seu->Write();
304  }
305  }
306  gDirectory->cd("..");
307  if (!fRadGridMan) {
308  fRootManager->Write();
309  }
310 
311  // fRootManager->Write();
312 
313 }
314 //_____________________________________________________________________________
316 {
317 // User actions at beginning of event
318 // ---
319  if(fActDetIter) {
320  fActDetIter->Reset();
321  FairDetector* detector;
322  TObject* obj=0;
323  while((obj=fActDetIter->Next())) {
324  detector = dynamic_cast<FairDetector*>(obj);
325  if (detector) {
326  detector->BeginEvent();
327  }
328  }
329  }
330 
331 }
332 
333 //_____________________________________________________________________________
335 {
336 // User actions at beginning of a primary track
337 // ---
338  if(fActDetIter) {
339  fActDetIter->Reset();
340  FairDetector* detector=NULL;
341  TObject* obj=0;
342  while((obj=fActDetIter->Next())) {
343  detector = dynamic_cast<FairDetector*>(obj);
344  if( detector ) {
345  detector->BeginPrimary();
346  }
347  }
348  }
349 }
350 //_____________________________________________________________________________
352 {
353 
354 // User actions at beginning of each track
355 // ---
356 
357  if(fActDetIter) {
358  fActDetIter->Reset();
359  FairDetector* detector=NULL;
360  TObject* obj=0;
361  while((obj=fActDetIter->Next())) {
362  detector = dynamic_cast<FairDetector*>(obj);
363  if (detector) {
364  detector->PreTrack();
365  }
366  }
367  }
368  fTrajAccepted=kFALSE;
369  if(NULL != fTrajFilter) {
370  // Get the pointer to current track
371  TParticle* particle = fStack->GetCurrentTrack();
372 // cout << " FairMCApplication::PreTrack()" << particle << endl;
373  // Apply cuts
374  fTrajAccepted = fTrajFilter->IsAccepted(particle);
375  if(fTrajAccepted) {
376  // Add trajectory to geo manager
377  // Int_t trackId = fStack->GetCurrentTrackNumber();
378  TGeoTrack* fTrack=fTrajFilter->AddTrack(particle);
379  // TLorentzVector pos;
380  gMC->TrackPosition(fTrkPos);
381  fTrack->AddPoint(fTrkPos.X(), fTrkPos.Y(), fTrkPos.Z(), fTrkPos.T());
382  }
383  }
384 }
385 //_____________________________________________________________________________
387 {
388 // User actions at each step
389 // ---
390  // Work around for Fluka VMC, which does not call
391  // MCApplication::PreTrack()
392  static Int_t TrackId = 0;
393  if ( fMcVersion ==2 && gMC->GetStack()->GetCurrentTrackNumber() != TrackId ) {
394  PreTrack();
395  TrackId = gMC->GetStack()->GetCurrentTrackNumber();
396  }
397 
398 
399  Int_t copyNo;
400  Int_t id = gMC->CurrentVolID(copyNo);
401  //printf("Here %d!\n", id);
402  Bool_t InMap =kFALSE;
403  fDisVol=0;
404  fDisDet=0;
405  Int_t fCopyNo=0;
406  fVolIter =fVolMap.find(id);
407  if (fVolIter!=fVolMap.end()) {
408  do {
409  fDisVol=fVolIter->second;
410  //printf(" -->%\n", fDisVol->GetName());
411  fCopyNo=fDisVol->getCopyNo();
412  if(copyNo==fCopyNo) {
413  fDisDet=dynamic_cast<FairDetector*> (fDisVol->GetModule());
414  if (fDisDet) {
416  }
417  InMap=kTRUE;
418  break;
419  }
420  fVolIter++;
421  } while(fVolIter!=fVolMap.upper_bound(id));
422  if(fDisVol && !InMap) {
423  FairVolume* fNewV=new FairVolume( gMC->CurrentVolName(), id);
424  fNewV->setMCid(id);
425  fNewV->setModId(fDisVol->getModId());
426  fNewV->SetModule(fDisVol->GetModule());
427  fNewV->setCopyNo(copyNo);
428  fVolMap.insert(pair<Int_t, FairVolume* >(id, fNewV));
429  fDisDet=dynamic_cast<FairDetector*> (fDisVol->GetModule());
430  if ( fDisDet) {
431  fDisDet->ProcessHits(fNewV);
432  }
433  }
434  }
435  if(fTrajAccepted) {
436  if(gMC->TrackStep() > fTrajFilter->GetStepSizeCut()) {
437  gMC->TrackPosition(fTrkPos);
438  fTrajFilter->GetCurrentTrk()->AddPoint(fTrkPos.X(), fTrkPos.Y(), fTrkPos.Z(), fTrkPos.T());
439  }
440  }
441  if(fRadLenMan) {
442  id = gMC->CurrentVolID(copyNo);
443  fModVolIter =fModVolMap.find(id);
444  fRadLenMan->AddPoint(fModVolIter->second);
445  }
446  if(fRadMapMan) {
447  id = gMC->CurrentVolID(copyNo);
448  fModVolIter =fModVolMap.find(id);
449  fRadMapMan->AddPoint(fModVolIter->second);
450  }
451  if(fRadGridMan) {
453  }
454 
455 }
456 //_____________________________________________________________________________
458 {
459 // User actions after finishing of each track
460 // ---
461  fActDetIter->Reset();
462  FairDetector* detector=NULL;
463  TObject* obj=0;
464  while((obj=fActDetIter->Next())) {
465  detector = dynamic_cast<FairDetector*>(obj);
466  if (detector ) {
467  detector->PostTrack();
468  }
469  }
470 }
471 
472 //_____________________________________________________________________________
474 {
475 // User actions after finishing of a primary track
476 // ---
477  if(fActDetIter) {
478  fActDetIter->Reset();
479  FairDetector* detector=NULL;
480  TObject* obj=0;
481  while((obj=fActDetIter->Next())) {
482  detector = dynamic_cast<FairDetector*>(obj);
483  if (detector) {
484  detector->FinishPrimary();
485  }
486  }
487  }
488 }
489 
490 //_____________________________________________________________________________
491 
493 {
494  FinishEvent();
495  FinishRun();
496  // Save histograms with memory and runtime information in the output file
497  if (FairRunSim::Instance()->GetWriteRunInfoFile()) {
499  }
500  fRootManager->Write();
502  fLogger->Warning(MESSAGE_ORIGIN, "StopRun() exiting not safetly oopps !!!@@@!!!" );
503 
505  rtdb->closeOutput();
506  exit(0) ;
507 }
508 //_____________________________________________________________________________
510 {
511 // User actions after finishing of an event
512 // ---
513  // --> Fill the stack output array
515  // --> Update track indizes in MCTracks and MCPoints
517  // --> Screen output of stack
518  fFairTaskList->ExecuteTask("");
520  TObject* obj=NULL;
521  FairDetector* detector=NULL;
522  fActDetIter->Reset();
523 
524  while((obj=fActDetIter->Next())) {
525  detector = dynamic_cast<FairDetector*>(obj);
526  if (detector) {
527  detector->FinishEvent();
528  }
529  }
530 
531  fRootManager->Fill();
532  fActDetIter->Reset();
533  detector=NULL;
534  obj=NULL;
535  while((obj=fActDetIter->Next())) {
536  detector = dynamic_cast<FairDetector*>(obj);
537  if (detector) {
538  detector->EndOfEvent();
539  }
540  }
541  fStack->Reset();
542  if(NULL != fTrajFilter) {
543  fTrajFilter->Reset();
544  TObjArray* fListOfTracks=gGeoManager->GetListOfTracks();
545  fListOfTracks->Delete();
546  }
547  if(NULL !=fRadLenMan) {
548  fRadLenMan->Reset();
549  }
550  if(NULL !=fRadMapMan) {
551  fRadMapMan->Reset();
552  }
553 
554  // Store information about runtime for one event and memory consuption
555  // for later usage.
557 }
558 //_____________________________________________________________________________
560 {
561 // No limit
562 // ---
563  return DBL_MAX;
564 }
565 //_____________________________________________________________________________
567 {
568 // No limit
569 // ---
570  return DBL_MAX;
571 }
572 //_____________________________________________________________________________
573 void FairMCApplication::SetField(FairField* field)
574 {
575  fxField=field;
576 }
577 //_____________________________________________________________________________
579 {
581  FairGeoInterface* GeoInterface =loader->getGeoInterface();
582  FairGeoMedia* media= GeoInterface->getMedia();
583  TList* MediaList= media->getListOfMedia();
584  TListIter iter(MediaList);
585  FairGeoMedium* medium;
586  Int_t NK=0;
587  Double_t p[4];
588  while((medium=(FairGeoMedium*)iter.Next())) {
589  NK=medium->getNpckov();
590  if(NK>0) {
591  Int_t Mid=0;
592  TGeoMedium* Med = 0;
593  if ( gGeoManager && (Med = gGeoManager->GetMedium(medium->GetName())) ) {
594  Mid=Med->GetId();
595  } else {
596  Mid=medium->getMediumIndex();
597  if(Mid<=0) {
598  continue;
599  }
600  }
601  Double_t ppckov[NK], absco[NK], effic[NK],rindex[NK];
602  for (Int_t i=0; i<NK; i++) {
603  medium->getCerenkovPar(i, p);
604  ppckov[i]=p[0]*1E-9;
605  absco[i]=p[1];
606  effic[i]=p[2];
607  rindex[i]=p[3];
608  }
609  gMC->SetCerenkov(Mid, NK, ppckov,absco, effic, rindex);
610  }
611  }
612  fModIter->Reset();
613  FairModule* Mod=NULL;
614  while((Mod = dynamic_cast<FairModule*>(fModIter->Next()))) {
615  Mod->ConstructOpGeometry();
616  }
617 }
618 
619 //_____________________________________________________________________________
621 {
622 
623  fModIter->Reset();
624  FairModule* Mod=NULL;
625  Int_t NoOfVolumes=0;
626  Int_t NoOfVolumesBefore=0;
627  Int_t ModId=0;
628  while((Mod = dynamic_cast<FairModule*>(fModIter->Next()))) {
629  NoOfVolumesBefore=gGeoManager->GetListOfVolumes()->GetEntriesFast();
630  Mod->ConstructGeometry();
631  ModId=Mod->GetModId();
632  NoOfVolumes=gGeoManager->GetListOfVolumes()->GetEntriesFast();
633  for (Int_t n=NoOfVolumesBefore; n <= NoOfVolumes; n++) {
634  fModVolMap.insert(pair<Int_t, Int_t >(n,ModId));
635  }
636  }
638  if(fSenVolumes) {
639  fNoSenVolumes=fSenVolumes->GetEntries();
640  }
641  if (gGeoManager) {
642  // cout << "FairMCApplication::ConstructGeometry() : Now closing the geometry"<<endl;
643  gGeoManager->CloseGeometry(); // close geometry
644  gMC->SetRootGeometry(); // notify VMC about Root geometry
645  gGeoManager->SetPdgName(22, "gamma");
646  gGeoManager->SetPdgName(211, "pi+");
647  gGeoManager->SetPdgName(321, "K+");
648  gGeoManager->SetPdgName(2212, "proton");
649  gGeoManager->SetPdgName(-211, "pi-");
650  gGeoManager->SetPdgName(-321, "K-");
651  gGeoManager->SetPdgName(111, "pi0");
652  gGeoManager->SetPdgName(310, "K0");
653  gGeoManager->SetPdgName(130, "K0");
654  gGeoManager->SetPdgName(2112, "neutron");
655  gGeoManager->SetPdgName(11, "e-");
656  gGeoManager->SetPdgName(13, "mu-");
657  gGeoManager->SetPdgName(-11, "e+");
658  gGeoManager->SetPdgName(-13, "mu+");
659  gGeoManager->SetPdgName(3312, "Xsi");
660  gGeoManager->SetPdgName(3334, "Omega");
661  gGeoManager->SetPdgName(50000050, "Ckov");
662  gGeoManager->SetPdgName(-421, "D0bar");
663  gGeoManager->SetPdgName(421, "D0");
664  gGeoManager->SetPdgName(-411, "D-");
665  gGeoManager->SetPdgName(411, "D+");
666  gGeoManager->SetPdgName(-213, "rho-");
667  gGeoManager->SetPdgName(213, "rho+");
668  gGeoManager->SetPdgName(113, "rho0");
669  gGeoManager->SetPdgName(1000010020, "Deuteron");
670  gGeoManager->SetPdgName(1000010030, "Triton");
671  gGeoManager->SetPdgName(1000020030, "HE3");
672  gGeoManager->SetPdgName(1000020040, "Alpha");
673  }
674 }
675 //_____________________________________________________________________________
676 
678 {
680 
681  FairVolume* fv=0;
682  Int_t id=0;
683  fModIter->Reset();
684  FairDetector* detector=NULL;
685  if(fEvGen!=0 && fStack!=0) {
686  fStack->Register();
687  } else {
688  fLogger->Warning(MESSAGE_ORIGIN, "Stack is not registerd ");
689  }
691  // if(fEvGen)fEvGen->Init();
693  fActDetIter->Reset();
694  while((detector = dynamic_cast<FairDetector*>(fActDetIter->Next()))) {
695  detector->Initialize(); // initialize the detectors
696  detector->SetSpecialPhysicsCuts(); // set the detector specific detector cuts
697  detector->Register(); // add branches to tree
698  }
700 
701 
702  if (fFairTaskList) {
703  InitTasks();
704  }
705 
706  // store the EventHeader Info
707  // Get and register EventHeader
708  UInt_t runId = FairRunSim::Instance()->GetRunId();
709 
710  fLogger->Info(MESSAGE_ORIGIN, "Simulation RunID: %i ", runId);
711 
712  // Get and register the MCEventHeader
714  fMCEventHeader->SetRunID(runId);
716 
717  if(NULL !=fRadGridMan) {
718  fRadGridMan->Init();
719  }
720 
721  if(fEvGen) {
723  }
725  if(NULL != fTrajFilter ) {
726  fTrajFilter->Init();
727  }
728  if(NULL !=fRadLenMan) {
729  fRadLenMan->Init();
730  }
731  if(NULL !=fRadMapMan) {
732  fRadMapMan->Init();
733  }
734  if(NULL !=fRadGridMan) {
735  fRadGridMan->Init();
736  }
737 
738 
741  TTree* outTree =new TTree("cbmsim", "/cbmroot", 99);
742  fRootManager->TruncateBranchNames(outTree, "cbmroot");
743  fRootManager->SetOutTree(outTree);
744 
745  for ( Int_t i = 0 ; i < fNoSenVolumes ; i++ ) {
746  fv= (FairVolume*)fSenVolumes->At(i);
747  id=fv->getMCid();
748  if(fv->getGeoNode()==0) { //handel sensetive volumes created directly by user
749  TGeoNode* fN=0;
750  TGeoVolume* v=gGeoManager->GetVolume(fv->GetName());
751  TObjArray* fNs=0;
752  if(v) {
753  fNs=v->GetNodes();
754  }
755  if(fNs) {
756  for(Int_t k=0; k<fNs->GetEntriesFast(); k++) {
757  fN=(TGeoNode*)fNs->At(k);
758  FairVolume* fNewV=new FairVolume( fv->GetName(), id);
759  fNewV->setModId(fv->getModId());
760  fNewV->SetModule(fv->GetModule());
761  fNewV->setCopyNo(fN->GetNumber());
762  fNewV->setMCid(id);
763  fVolMap.insert(pair<Int_t, FairVolume* >(id, fNewV));
764  }
765  } else {
766  FairVolume* fNewV=new FairVolume( fv->GetName(), id);
767  fNewV->setModId(fv->getModId());
768  fNewV->SetModule(fv->GetModule());
769  fNewV->setCopyNo(1);
770  fNewV->setMCid(id);
771  fVolMap.insert(pair<Int_t, FairVolume* >(id, fNewV));
772  }
773  } else {
774  fVolMap.insert(pair<Int_t, FairVolume* >(id, fv));
775  }
776  }
777 
778 
779 }
780 
781 //_____________________________________________________________________________
783 {
784 // Fill the user stack (derived from TVirtualMCStack) with primary particles.
785 // ---
786  if(fEvGen) {
787 // cout << "FairMCApplication::GeneratePrimaries()" << endl;
788  if (!fEvGen->GenerateEvent( fStack) ) {
789  StopRun();
790  }
791  }
792 }
793 //_____________________________________________________________________________
795 {
796  return (FairDetector*)fModules->FindObject(DetName);
797 }
798 //_____________________________________________________________________________
800 {
801  TDatabasePDG* pdgDatabase = TDatabasePDG::Instance();
803  TObjArray* NewIons=fRun->GetUserDefIons();
804  TIterator* Iter=NewIons->MakeIterator();
805  Iter->Reset();
806  TObject* obj=0;
807  FairIon* ion=0;
808  while((obj=Iter->Next())) {
809  ion=dynamic_cast <FairIon*> (obj);
810  if(ion) {
811  // Check if an ion with the calculated pdg code already exists in the
812  // TDatabasePDG.
813  // If the ion already exist don't create a new one because this fails for
814  // Geant4. Instead modify the FairIon to use the already existing ion
815  // from the TDatabasePDG.
816  // The problem occured for example for Alphas which exist already.
817  Int_t ionPdg = GetIonPdg( ion->GetZ(), ion->GetA() );
818  if ( !pdgDatabase->GetParticle( ionPdg ) ) {
819  gMC->DefineIon(ion->GetName(), ion->GetZ(), ion->GetA(), ion->GetQ(),
820  ion->GetExcEnergy(),ion->GetMass());
821 
822  } else {
823  ion->SetName(pdgDatabase->GetParticle(ionPdg)->GetName());
824  }
825  //Add Ion to gGeoManager visualization
826  if(gGeoManager) {
827  gGeoManager->SetPdgName(pdgDatabase->GetParticle(ion->GetName())->PdgCode(),ion->GetName() );
828  }
829  fLogger->Info(MESSAGE_ORIGIN, "Add Ion: %s with PDG %i ", ion->GetName(), pdgDatabase->GetParticle(ion->GetName())->PdgCode());
830  }
831  }
832  delete Iter;
834  if(fEvGen) {
835  fEvGen->Init();
836  }
837 }
838 //_____________________________________________________________________________
840 {
841 
843  TObjArray* NewIons=fRun->GetUserDefIons();
844  TIterator* Iter=NewIons->MakeIterator();
845  Iter->Reset();
846  TObject* obj=0;
847  TObjArray* NewPart=fRun->GetUserDefParticles();
848  TIterator* parIter=NewPart->MakeIterator();
849  parIter->Reset();
850  obj=0;
852  while((obj=parIter->Next())) {
853  particle=dynamic_cast <FairParticle*> (obj);
854  if(particle) { // (Int_t pdg, const char* name, TMCParticleType type,
855  //Double_t mass, Double_t charge, Double_t lifetime);
856  cout << "Add Particle: " << particle->GetName() << " with PDG " << particle->GetPDG() << "\n"<<
857  particle->GetName() << " // const TString& name \n" <<
858  particle->GetMCType()<<" // TMCParticleType mcType \n" <<
859  particle->GetMass()<<" // Double_t mass \n" <<
860  particle->GetCharge()<<" // Double_t charge \n" <<
861  particle->GetDecayTime()<<" // Double_t lifetime \n" <<
862  particle->GetPType()<< " // const TString& pType, \n" <<
863  particle->GetWidth()<< " // Double_t width \n" <<
864  particle->GetSpin()<< " // Int_t iSpin \n" <<
865  particle->GetiParity()<< " // Int_t iParity \n" <<
866  particle->GetConjugation()<<" // Int_t iConjugation \n" <<
867  particle->GetIsospin()<< " // Int_t iIsospin \n" <<
868  particle->GetIsospinZ()<< " // Int_t iIsospinZ \n" <<
869  particle->GetgParity()<< " // Int_t gParity \n" <<
870  particle->GetLepton()<< " // Int_t lepton \n" <<
871  particle->GetBaryon()<< " // Int_t baryon \n" <<
872  particle->IsStable() << " // Bool_t stable \n" << endl;
873  gMC->DefineParticle(particle->GetPDG(), // Int_t pdg
874  particle->GetName(), // const TString& name
875  particle->GetMCType(), // TMCParticleType mcType
876  particle->GetMass(), // Double_t mass
877  particle->GetCharge(), // Double_t charge
878  particle->GetDecayTime(), // Double_t lifetime
879  particle->GetPType(), // const TString& pType,
880  particle->GetWidth(), // Double_t width
881  particle->GetSpin(), // Int_t iSpin
882  particle->GetiParity(), // Int_t iParity
883  particle->GetConjugation(), // Int_t iConjugation
884  particle->GetIsospin(), // Int_t iIsospin
885  particle->GetIsospinZ(), // Int_t iIsospinZ
886  particle->GetgParity(), // Int_t gParity
887  particle->GetLepton(), // Int_t lepton
888  particle->GetBaryon(), // Int_t baryon
889  particle->IsStable() // Bool_t stable
890  );
891  //Add Ion to gGeoManager visualization
892  if(gGeoManager) {
893  gGeoManager->SetPdgName(particle->GetPDG(),particle->GetName() );
894  }
895  }
896  }
897  delete parIter;
898  AddDecayModes();
899  delete Iter;
900 }
901 
902 //_____________________________________________________________________________
904 {
905  TString work = getenv("VMCWORKDIR");
906  TString work_config=work+"/gconfig/";
907  TString config_dir= getenv("CONFIG_DIR");
908  Bool_t AbsPath=kFALSE;
909 
910  if (!config_dir.EndsWith("/")) {
911  config_dir+="/";
912  }
913  // set Pythia as external decayer
914 
915  if(fPythiaDecayer) {
916  TString decayConfig;
917  if(fPythiaDecayerConfig.IsNull()) {
918  decayConfig="DecayConfig.C";
919  fPythiaDecayerConfig= decayConfig;
920  } else {
921  if (fPythiaDecayerConfig.Contains("/")) {
922  AbsPath=kTRUE;
923  }
924  decayConfig=fPythiaDecayerConfig;
925  }
926 
927  if (!AbsPath && TString(gSystem->FindFile(config_dir.Data(), decayConfig)) != TString("")) {
928  cout << "---User path for Configuration (DecayConfig.C) is used : " << config_dir.Data() << endl;
929  } else {
930  if(AbsPath) {
931  decayConfig= fPythiaDecayerConfig;
932  } else {
933  decayConfig=work_config+ fPythiaDecayerConfig ;
934  }
935  }
936  // Add decay modes using an external configuration script
937  cout << "External Decay Modes with script \n "<< decayConfig.Data() << endl;
938  // Load configuration script and execute it
939  Int_t pyt= gROOT->LoadMacro(decayConfig.Data());
940  if(pyt==0) {
941  gInterpreter->ProcessLine("DecayConfig()");
942  }
943  }
944  // set user defined phase space decay for particles (ions)
945  AbsPath=kFALSE;
946  if(fUserDecay) {
947  TString Userdecay;
948  if(fUserDecayConfig.IsNull()) {
949  Userdecay="UserDecay.C";
950  fUserDecayConfig =Userdecay;
951  } else {
952  if(fUserDecayConfig.Contains("/")) {
953  AbsPath=kTRUE;
954  }
955  Userdecay= fUserDecayConfig;
956  }
957 
958 
959  if (!AbsPath && TString(gSystem->FindFile(config_dir.Data(), Userdecay)) != TString("")) {
960  cout << "---User path for Configuration (UserDecay.C) is used : " << config_dir.Data() << endl;
961  } else {
962  if(AbsPath) {
963  Userdecay=fUserDecayConfig;
964  } else {
965  Userdecay=work_config+fUserDecayConfig;
966  }
967  }
968  cout << "User Decay Modes with script \n "<< Userdecay.Data() << endl;
969  Int_t dec= gROOT->LoadMacro(Userdecay.Data());
970  if(dec==0) {
971  gInterpreter->ProcessLine("UserDecayConfig()");
972  }
973  }
974 }
975 //_____________________________________________________________________________
977 {
978  return fEvGen;
979 }
980 //_____________________________________________________________________________
982 {
983  fEvGen=pGen;
984 }
985 //_____________________________________________________________________________
986 void FairMCApplication::AddTask(TTask* fTask)
987 {
988  if (! fFairTaskList ) {
989  fFairTaskList= new FairTask("Task List", 1);
990  gROOT->GetListOfBrowsables()->Add(fFairTaskList);
991  }
992  fFairTaskList->Add(fTask);
993  SetParTask();
994 }
995 //_____________________________________________________________________________
997 {
998  return fStack;
999 }
1000 //_____________________________________________________________________________
1002 {
1003  return fFairTaskList;
1004 }
1005 
1006 //_____________________________________________________________________________
1008 {
1009  // Only RTDB init when more than Main Task list
1010  if(FairRun::Instance()->GetNTasks() >= 1 ) {
1012  }
1013  fModIter->Reset();
1014  FairModule* Mod=NULL;
1015  while((Mod = dynamic_cast<FairModule*>(fModIter->Next()))) {
1016  Mod->SetParContainers();
1017  }
1019  fRTdb->initContainers(FairRunSim::Instance()->GetRunId());
1020 
1021 }
1022 //_____________________________________________________________________________
1024 {
1025 
1026  // Only RTDB init when more than Main Task list
1027  if(FairRun::Instance()->GetNTasks() >= 1 ) {
1028  fLogger->Info(MESSAGE_ORIGIN, "Initialize Tasks--------------------------");
1030 
1031  }
1032 
1033 
1034 }
1035 //_____________________________________________________________________________
1037 {
1038  return fRootManager->GetInChain();
1039 }
1040 //_____________________________________________________________________________
1041 
1043 {
1044  fRadLength=RadLen;
1045  if(fRadLength) {
1047  }
1048 }
1049 //_____________________________________________________________________________
1050 
1051 
1053 {
1054  fRadMap=RadMap;
1055  if(fRadMap) {
1057  }
1058 }
1059 //_____________________________________________________________________________
1060 
1061 
1062 void FairMCApplication::AddMeshList(TObjArray* meshList)
1063 {
1064  if (!fRadGridMan) {
1066  }
1067  fRadGridMan->AddMeshList (meshList);
1068 }
1069 //_____________________________________________________________________________
1070 
1071 Int_t FairMCApplication::GetIonPdg(Int_t z, Int_t a) const
1072 {
1073  // Acording to
1074  // http://pdg.lbl.gov/2012/reviews/rpp2012-rev-monte-carlo-numbering.pdf
1075 
1076  return 1000000000 + 10*1000*z + 10*a;
1077 }
1078 
1080 
1081