41 #include <TDatabasePDG.h>
43 #include <TGeoManager.h>
44 #include <TGeoMedium.h>
46 #include <TGeoPhysicalNode.h>
47 #include <TGeoTrack.h>
48 #include <TGeoVolume.h>
50 #include <THashList.h>
51 #include <TInterpreter.h>
52 #include <TIterator.h>
54 #include <TObjArray.h>
56 #include <TParticlePDG.h>
58 #include <TRefArray.h>
60 #include <TVirtualMC.h>
61 #include <TVirtualMCStack.h>
76 : TVirtualMCApplication(name, title)
77 , fActiveDetectors(nullptr)
78 , fFairTaskList(nullptr)
84 , fPythiaDecayer(kFALSE)
85 , fPythiaDecayerConfig(
"")
87 , fRootManager(nullptr)
88 , fSenVolumes(nullptr)
92 , fTrajFilter(nullptr)
93 , fTrajAccepted(kFALSE)
95 , fUserDecayConfig(
"")
103 , fTrkPos(TLorentzVector(0, 0, 0, 0))
105 , fRadLenMan(nullptr)
107 , fRadMapMan(nullptr)
108 , fRadGridMan(nullptr)
109 , fEventHeader(nullptr)
110 , fMCEventHeader(nullptr)
111 , listActiveDetectors()
115 , fSaveCurrentEvent(kTRUE)
118 , fGeometryIsInitialized(kFALSE)
124 LOG(debug) <<
"FairMCApplication-ctor " <<
this;
145 }
else if (!dynamic_cast<FairModule*>(obj)) {
146 LOG(error) <<
"Dynamic cast fails. Object neither FairDetector nor FairModule in module list";
161 fgMasterInstance =
this;
166 : TVirtualMCApplication(rhs.GetName(), rhs.GetTitle())
167 , fActiveDetectors(nullptr)
168 , fFairTaskList(nullptr)
169 , fDetectors(nullptr)
174 , fPythiaDecayer(kFALSE)
175 , fPythiaDecayerConfig(rhs.fPythiaDecayerConfig)
177 , fRootManager(nullptr)
178 , fSenVolumes(nullptr)
179 , fxField(rhs.fxField)
181 , fMcVersion(rhs.fMcVersion)
182 , fTrajFilter(nullptr)
183 , fTrajAccepted(kFALSE)
185 , fUserDecayConfig(rhs.fUserDecayConfig)
193 , fTrkPos(rhs.fTrkPos)
195 , fRadLenMan(nullptr)
197 , fRadMapMan(nullptr)
198 , fRadGridMan(nullptr)
199 , fEventHeader(nullptr)
200 , fMCEventHeader(nullptr)
201 , listActiveDetectors()
205 , fSaveCurrentEvent(kTRUE)
208 , fGeometryIsInitialized(kFALSE)
213 LOG(debug) <<
"FairMCApplication-copy-ctor " <<
this;
221 while ((obj = rhs.
fModIter->Next())) {
222 LOG(debug) <<
"cloning " << (
static_cast<FairModule*
>(obj))->GetName();
223 fModules->Add(static_cast<FairModule*>(obj)->CloneModule());
232 if (obj->InheritsFrom(
"FairDetector")) {
242 LOG(error) <<
"Dynamic cast fails.";
259 : TVirtualMCApplication()
260 , fActiveDetectors(0)
267 , fPythiaDecayer(kFALSE)
268 , fPythiaDecayerConfig(
"")
275 , fTrajFilter(nullptr)
276 , fTrajAccepted(kFALSE)
278 , fUserDecayConfig(
"")
286 , fTrkPos(TLorentzVector(0, 0, 0, 0))
288 , fRadLenMan(nullptr)
290 , fRadMapMan(nullptr)
291 , fRadGridMan(nullptr)
292 , fEventHeader(nullptr)
293 , fMCEventHeader(nullptr)
294 , listActiveDetectors()
298 , fSaveCurrentEvent(kTRUE)
301 , fGeometryIsInitialized(kFALSE)
328 TVirtualMCApplication::operator=(rhs);
360 fGeometryIsInitialized = kFALSE;
369 while ((obj = rhs.
fModIter->Next())) {
370 fModules->Add(static_cast<FairModule*>(obj)->CloneModule());
379 if (obj->InheritsFrom(
"FairDetector")) {
389 LOG(error) <<
"Dynamic cast fails.";
418 fMC = TVirtualMC::GetMC();
421 LOG(fatal) <<
"No MC engine defined";
426 LOG(fatal) <<
"No Stack defined.";
435 TString MCName =
fMC->GetName();
436 if (MCName ==
"TGeant3" || MCName ==
"TGeant3TGeo") {
438 }
else if (MCName ==
"TGeant4") {
440 }
else if (MCName ==
"TFluka") {
447 LOG(info) <<
"Monte Carlo Engine Initialisation with: " << MCName.Data();
461 fMC->ProcessRun(nofEvents);
472 LOG(debug) <<
"FairMCMCApplication::FinishRun() start";
475 detectorPtr->FinishRun();
487 TObjArray* meshlist =
nullptr;
498 LOG(info) <<
"=======================================================";
500 LOG(info) <<
"=======================================================";
503 radGridFile->mkdir(
"Dosimetry");
504 radGridFile->cd(
"Dosimetry");
506 for (Int_t i = 0; i < meshlist->GetEntriesFast(); i++) {
509 aMesh->
Scale(1. / nprimary);
518 radGridFile->Write();
519 radGridFile->Close();
534 UndoGeometryModifications();
537 LOG(debug) <<
"Done FairMCMCApplication::FinishRun()";
547 detectorPtr->BeginEvent();
557 detectorPtr->BeginPrimary();
568 detectorPtr->PreTrack();
574 TParticle* particle =
fStack->GetCurrentTrack();
593 LOG(info) <<
"FairMCApplication::CloneForWorker ";
598 workerRun->SetName(
fRun->GetName());
613 return workerApplication;
640 LOG(info) <<
"Monte Carlo Engine Worker Initialisation with: " <<
fMC->GetName();
646 LOG(debug) <<
"FairMCApplication::FinishRunOnWorker: ";
659 static Int_t TrackId = 0;
660 if (
fMcVersion == 2 &&
fMC->GetStack()->GetCurrentTrackNumber() != TrackId) {
662 TrackId =
fMC->GetStack()->GetCurrentTrackNumber();
675 Int_t
id =
fMC->CurrentVolID(copyNo);
676 Bool_t InMap = kFALSE;
688 if (copyNo == fCopyNo) {
712 fVolMap.insert(pair<Int_t, FairVolume*>(
id, fNewV));
740 id =
fMC->CurrentVolID(copyNo);
745 id =
fMC->CurrentVolID(copyNo);
760 detectorPtr->PostTrack();
770 detectorPtr->FinishPrimary();
785 LOG(warn) <<
"StopRun() exiting not safetly oopps !!!@@@!!!";
803 << gMC->CurrentEvent() <<
")";
822 detectorPtr->FinishEvent();
831 for (
auto detectorPtr : listActiveDetectors) {
832 detectorPtr->EndOfEvent();
840 gGeoManager->GetListOfTracks()->Delete();
878 TListIter iter(MediaList);
882 while ((medium = dynamic_cast<FairGeoMedium*>(iter.Next()))) {
887 if (gGeoManager && (Med = gGeoManager->GetMedium(medium->GetName()))) {
895 Double_t ppckov[NK], absco[NK], effic[NK], rindex[NK];
896 for (Int_t i = 0; i < NK; i++) {
898 ppckov[i] = p[0] * 1E-9;
903 TVirtualMC::GetMC()->SetCerenkov(Mid, NK, ppckov, absco, effic, rindex);
908 while ((Mod = dynamic_cast<FairModule*>(
fModIter->Next()))) {
920 LOG(fatal) <<
"gGeoManager not initialized at FairMCApplication::ConstructGeometry\n";
927 Int_t NoOfVolumes = 0;
928 Int_t NoOfVolumesBefore = 0;
931 TObjArray* tgeovolumelist = gGeoManager->GetListOfVolumes();
933 while ((Mod = dynamic_cast<FairModule*>(
fModIter->Next()))) {
934 NoOfVolumesBefore = tgeovolumelist->GetEntriesFast();
938 NoOfVolumes = tgeovolumelist->GetEntriesFast();
939 for (Int_t n = NoOfVolumesBefore; n < NoOfVolumes; n++) {
940 TGeoVolume* v = (TGeoVolume*)tgeovolumelist->At(n);
941 fModVolMap.insert(pair<Int_t, Int_t>(v->GetNumber(), ModId));
946 int NoOfVolumesBeforeClose = tgeovolumelist->GetEntries();
947 gGeoManager->CloseGeometry();
948 int NoOfVolumesAfterClose = tgeovolumelist->GetEntries();
953 if (NoOfVolumesBeforeClose != NoOfVolumesAfterClose) {
954 LOG(error) <<
"TGeoManager::CloseGeometry() modified the volume list from " << NoOfVolumesBeforeClose <<
" to "
955 << NoOfVolumesAfterClose <<
"\n"
956 <<
"This almost certainly means inconsistent lookup structures used in simulation/stepping.\n";
960 TVirtualMC::GetMC()->SetRootGeometry();
961 LOG(info) <<
"TGeometry will be imported to VMC"
964 LOG(info) <<
"TGeometry will not be imported to VMC"
968 TDatabasePDG* pdgDatabase = TDatabasePDG::Instance();
969 const THashList* list = pdgDatabase->ParticleList();
971 pdgDatabase->ReadPDGTable();
972 list = pdgDatabase->ParticleList();
974 TIterator* particleIter = list->MakeIterator();
975 TParticlePDG* Particle = 0;
976 while ((Particle = dynamic_cast<TParticlePDG*>(particleIter->Next())) && (Counter <= 256)) {
977 TString Name = gGeoManager->GetPdgName(Particle->PdgCode());
980 gGeoManager->SetPdgName(Particle->PdgCode(), Particle->GetName());
986 while ((Mod = dynamic_cast<FairModule*>(
fModIter->Next()))) {
993 gGeoManager->RefreshPhysicalNodes(kFALSE);
1016 if (fGeometryIsInitialized)
1030 LOG(warn) <<
"Stack is not registered ";
1068 LOG(info) <<
"Simulation RunID: " << runId;
1110 LOG(error) <<
"No FairVolume in fSenVolumes at position " << i;
1116 TGeoVolume* v = gGeoManager->GetVolume(fv->GetName());
1119 fNs = v->GetNodes();
1122 for (Int_t k = 0; k < fNs->GetEntriesFast(); k++) {
1123 fN =
dynamic_cast<TGeoNode*
>(fNs->At(k));
1125 LOG(error) <<
"No TGeoNode in fNs at position " << k;
1133 fVolMap.insert(pair<Int_t, FairVolume*>(
id, fNewV));
1141 fVolMap.insert(pair<Int_t, FairVolume*>(
id, fNewV));
1144 fVolMap.insert(pair<Int_t, FairVolume*>(
id, fv));
1148 fGeometryIsInitialized = kTRUE;
1158 LOG(debug) <<
"FairMCApplication::GeneratePrimaries: " <<
fEvGen;
1175 TDatabasePDG* pdgDatabase = TDatabasePDG::Instance();
1177 TIterator* Iter = NewIons->MakeIterator();
1181 while ((obj = Iter->Next())) {
1182 ion =
dynamic_cast<FairIon*
>(obj);
1190 Int_t ionPdg = GetIonPdg(ion->
GetZ(), ion->
GetA());
1191 if (!pdgDatabase->GetParticle(ionPdg)) {
1196 ion->SetName(pdgDatabase->GetParticle(ionPdg)->GetName());
1200 gGeoManager->SetPdgName(pdgDatabase->GetParticle(ion->GetName())->PdgCode(), ion->GetName());
1202 LOG(info) <<
"Add Ion: " << ion->GetName() <<
" with PDG "
1203 << pdgDatabase->GetParticle(ion->GetName())->PdgCode();
1216 TIterator* parIter = NewPart->MakeIterator();
1220 TVirtualMC* curMC = TVirtualMC::GetMC();
1222 LOG(fatal) <<
"No MC engine was defined before AddParticles()";
1226 while ((obj = parIter->Next())) {
1230 LOG(info) <<
"Add Particle: " << particle->
GetName() <<
" with PDG " << particle->
GetPDG() <<
"\n"
1231 << particle->
GetName() <<
" // const TString& name \n"
1232 << particle->
GetMCType() <<
" // TMCParticleType mcType \n"
1233 << particle->
GetMass() <<
" // Double_t mass \n"
1234 << particle->
GetCharge() <<
" // Double_t charge \n"
1235 << particle->
GetDecayTime() <<
" // Double_t lifetime \n"
1236 << particle->
GetPType() <<
" // const TString& pType, \n"
1237 << particle->
GetWidth() <<
" // Double_t width \n"
1238 << particle->
GetSpin() <<
" // Int_t iSpin \n"
1239 << particle->
GetiParity() <<
" // Int_t iParity \n"
1241 << particle->
GetIsospin() <<
" // Int_t iIsospin \n"
1242 << particle->
GetIsospinZ() <<
" // Int_t iIsospinZ \n"
1243 << particle->
GetgParity() <<
" // Int_t gParity \n"
1244 << particle->
GetLepton() <<
" // Int_t lepton \n"
1245 << particle->
GetBaryon() <<
" // Int_t baryon \n"
1246 << particle->
IsStable() <<
" // Bool_t stable \n";
1248 curMC->DefineParticle(particle->
GetPDG(),
1269 gGeoManager->SetPdgName(particle->
GetPDG(), particle->
GetName());
1280 TString work = getenv(
"VMCWORKDIR");
1281 TString work_config = work +
"/gconfig/";
1282 work_config.ReplaceAll(
"//",
"/");
1284 TString config_dir = getenv(
"CONFIG_DIR");
1285 config_dir.ReplaceAll(
"//",
"/");
1287 Bool_t AbsPath = kFALSE;
1289 if (!config_dir.EndsWith(
"/")) {
1295 TString decayConfig;
1297 decayConfig =
"DecayConfig.C";
1306 if (!AbsPath && TString(gSystem->FindFile(config_dir.Data(), decayConfig)) != TString(
"")) {
1307 LOG(info) <<
"---User path for Configuration (DecayConfig.C) is used : " << config_dir.Data();
1316 LOG(info) <<
"External Decay Modes with script \n " << decayConfig.Data();
1318 Int_t pyt = gROOT->LoadMacro(decayConfig.Data());
1320 gInterpreter->ProcessLine(
"DecayConfig()");
1328 Userdecay =
"UserDecay.C";
1337 if (!AbsPath && TString(gSystem->FindFile(config_dir.Data(), Userdecay)) != TString(
"")) {
1338 LOG(info) <<
"---User path for Configuration (UserDecay.C) is used : " << config_dir.Data();
1346 LOG(info) <<
"User Decay Modes with script \n " << Userdecay.Data();
1347 Int_t dec = gROOT->LoadMacro(Userdecay.Data());
1349 gInterpreter->ProcessLine(
"UserDecayConfig()");
1386 while ((Mod = dynamic_cast<FairModule*>(
fModIter->Next()))) {
1398 LOG(info) <<
"Initialize Tasks--------------------------";
1409 LOG(warn) <<
"The function is not available in MT mode";
1442 Int_t FairMCApplication::GetIonPdg(Int_t z, Int_t a)
const
1447 return 1000000000 + 10 * 1000 * z + 10 * a;
1451 void FairMCApplication::UndoGeometryModifications()
1463 TObjArray* physNodes = gGeoManager->GetListOfPhysicalNodes();
1464 Int_t numPhysNodes = physNodes->GetEntriesFast();
1466 if (0 == numPhysNodes)
1470 LOG(info) <<
"Undo all misalignment";
1472 TGeoPhysicalNode* node =
nullptr;
1473 TGeoHMatrix* ng3 =
nullptr;
1474 for (Int_t k = 0; k < numPhysNodes; k++) {
1475 node =
static_cast<TGeoPhysicalNode*
>(physNodes->At(k));
1476 ng3 = node->GetOriginalMatrix();
1480 gGeoManager->ClearPhysicalNodes(kFALSE);
virtual void FinishRunOnWorker()
FairMCApplicationState fState
virtual FairGenericStack * CloneStack() const
virtual const char * GetName() const
void AlignGeometry() const
virtual void ConstructGeometry()
list of container factories
FairGeoMedia * getMedia()
TRefArray * fActiveDetectors
virtual void AddParticles()
TString fPythiaDecayerConfig
static thread_local TRefArray * svList
virtual void ExecuteTask(Option_t *option="0")
virtual void ConstructOpGeometry()
TString GetOutputFileName()
virtual ~FairMCApplication()
virtual void FinishEvent()
virtual void RegisterAlignmentMatrices()
virtual void Register()=0
const TString & GetPType()
virtual void SetParContainers()
virtual void SetSpecialPhysicsCuts()
FairEventHeader * fEventHeader
std::map< Int_t, Int_t > fModVolMap
FairRootManager * fRootManager
virtual Double_t TrackingRmax() const
FairMCEventHeader * GetMCEventHeader()
FairGeoInterface * getGeoInterface()
virtual Double_t TrackingZmax() const
void SetDetArrayList(TRefArray *detArray)
static FairGeoLoader * Instance()
static FairRun * Instance()
TObjArray * GetMeshList()
FairGeoNode * getGeoNode()
virtual void InitGeometry()
virtual void UpdateTrackIndex(TRefArray *)
void SetRadiationMapReg(Bool_t RadMap)
Double_t GetStepSizeCut() const
void getCerenkovPar(Int_t, Double_t *)
virtual Bool_t MisalignGeometry()
Bool_t IsImportTGeoToVMC() const
std::map< Int_t, Int_t >::iterator fModVolIter
static FairRootManager * Instance()
static FairRunSim * Instance()
Bool_t IsAccepted(const TParticle *p) const
ClassImp(FairEventBuilder)
FairGenericStack * GetStack()
FairRadGridManager * fRadGridMan
virtual void FinishPrimary()
std::list< FairDetector * > listActiveDetectors
void SetSink(FairSink *tempSink)
FairDetector * GetDetector(const char *DetName)
virtual void ConstructGeometry()
FairRadLenManager * fRadLenMan
std::multimap< Int_t, FairVolume * > fVolMap
void SetRadiationLengthReg(Bool_t RadLen)
void SetModule(FairModule *mod)
Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
void AddMeshList(TObjArray *meshList)
virtual void Initialize()
Bool_t initContainers(Int_t runId, Int_t refId=-1, const Text_t *fileName="")
virtual TVirtualMCApplication * CloneForWorker() const
FairPrimaryGenerator * GetPrimaryGenerator()
TGeoTrack * GetCurrentTrk()
TObjArray * GetUserDefIons()
void SetField(FairField *field)
virtual void FillTrackArray()
virtual FairPrimaryGenerator * ClonePrimaryGenerator() const
TGeoTrack * CheckAddTrack(Int_t trackId, TParticle *p)
virtual void ConstructOpGeometry()
std::list< FairDetector * > listDetectors
virtual void FinishTask()
virtual void GeneratePrimaries()
virtual void AddDecayModes()
FairMCEventHeader * fMCEventHeader
FairPrimaryGenerator * GetGenerator()
static FairTrajFilter * Instance()
FairDetector * GetDetector()
Int_t GetInstanceId() const
virtual Sink_Type GetSinkType()=0
virtual void InitOnWorker()
virtual void InitParContainers()
virtual Bool_t GenerateEvent(FairGenericStack *pStack)
FairRuntimeDb * GetRuntimeDb(void)
void SetEvent(FairMCEventHeader *event)
void AddPoint(Int_t &ModuleId)
Bool_t GetStoreTraj() const
virtual FairSink * CloneSink()=0
FairPrimaryGenerator * fEvGen
FairGenericStack * fStack
TMCParticleType GetMCType()
virtual void BeginPrimary()
virtual Bool_t ProcessHits(FairVolume *v=0)=0
virtual void FinishPrimary()
virtual void BeginEvent()
void AddTask(TTask *fTask)
std::multimap< Int_t, FairVolume * >::iterator fVolIter
virtual void FinishEvent()
void AddPoint(Int_t &ModuleId)
void InitMC(const char *setup, const char *cuts)
void SetGenerator(FairPrimaryGenerator *fxGenerator)
FairTrajFilter * fTrajFilter
void Init(TString brName="GeoTracks", TString folderName="MCGeoTrack")
void RunMC(Int_t nofEvents)
void AddMeshList(TObjArray *list)
FairRadMapManager * fRadMapMan
TObjArray * GetUserDefParticles()
Double_t GetExcEnergy() const