FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairRunSim.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 
9 // ----- FairRunSim source file -----
10 // ----- Created 06/01/04 by M. Al-Turany -----
11 
12 #include "FairRunSim.h"
13 
14 #include "FairBaseParSet.h" // for FairBaseParSet
15 #include "FairField.h" // for FairField
16 #include "FairFileHeader.h" // for FairFileHeader
17 #include "FairGenericVMCConfig.h" // for FairGenericVMCConfig
18 #include "FairGeoInterface.h" // for FairGeoInterface
19 #include "FairGeoLoader.h" // for FairGeoLoader
20 #include "FairGeoParSet.h" // for FairGeoParSet
21 #include "FairLogger.h" // for FairLogger, MESSAGE_ORIGIN
22 #include "FairMCEventHeader.h" // for FairMCEventHeader
23 #include "FairMesh.h" // for FairMesh
24 #include "FairModule.h" // for FairModule
25 #include "FairParSet.h" // for FairParSet
26 #include "FairPrimaryGenerator.h" // for FairPrimaryGenerator
27 #include "FairRootManager.h" // for FairRootManager
28 #include "FairRunIdGenerator.h" // for FairRunIdGenerator
29 #include "FairRuntimeDb.h" // for FairRuntimeDb
30 #include "FairTask.h" // for FairTask
31 #include "FairTrajFilter.h" // for FairTrajFilter
32 
33 #include <TCollection.h> // for TIter
34 #include <TGeoManager.h> // for gGeoManager
35 #include <TList.h> // for TList
36 #include <TObjString.h> // for TObjString
37 #include <TObject.h> // for TObject
38 #include <TRandom.h> // for gRandom
39 #include <TSystem.h> // for TSystem, gSystem
40 #include <iostream> // for cout, endl, ostream
41 #include <stdlib.h> // for getenv, nullptr
42 #include <string.h> // for strcmp, strncmp
43 
44 using std::cout;
45 using std::endl;
46 
48 
49 FairRunSim::FairRunSim(Bool_t isMaster)
50  : FairRun(isMaster)
51  , count(0)
52  , fApp(nullptr)
53  , fBeamMom(0)
54  , fUseBeamMom(kFALSE)
55  , fGen(nullptr)
56  , fMCEvHead(nullptr)
57  , fField(nullptr)
58  , fMapName("")
59  , fIons(new TObjArray())
60  , fParticles(new TObjArray())
61  , ListOfModules(new TObjArray())
62  , MatFname("")
63  , fStoreTraj(kFALSE)
64  , fLoaderName(new TString("TGeo"))
65  , fPythiaDecayer(kFALSE)
66  , fPythiaDecayerConfig("")
67  , fUserDecay(kFALSE)
68  , fUserDecayConfig("")
69  , fRadLength(kFALSE)
70  , fRadMap(kFALSE)
71  , fRadGrid(kFALSE)
72  , fMeshList(new TObjArray())
73  , fUserConfig("")
74  , fUserCuts("SetCuts.C")
75  , fIsMT(kFALSE)
76  , fImportTGeoToVMC(kTRUE)
77  , fSimulationConfig(nullptr)
78 
79 {
80  if (fginstance) {
81  Fatal("FairRun", "Singleton instance already exists.");
82  return;
83  }
84  fginstance = this;
85  fRunId = 0;
86  fAna = kFALSE;
87 }
88 
90 {
91  LOG(debug) << "Enter Destructor of FairRunSim ";
92 
93  // delete fApp;
94 
99  LOG(debug) << "Start deleting all registered modules";
100  ListOfModules->Delete();
101  delete ListOfModules;
102  LOG(debug) << "Finish deleting all registered modules";
103 
104  fIons->Delete();
105  delete fIons;
106  fParticles->Delete();
107  delete fParticles;
108 
109  delete fApp;
110  // delete fField;
111  // Not owner of the field
112  delete fGen;
113  delete fMCEvHead;
114 }
115 
117 
119 {
120  ListOfModules->Add(Mod);
121  Mod->SetModId(count++);
122 }
123 
125 {
126  Mesh->print();
127  Mesh->calculate();
128  fMeshList->Add(Mesh);
129 }
130 
132 {
134  return fIons;
135 }
136 
138 {
140  return fParticles;
141 }
142 
144 {
147 
148  CheckFlukaExec();
149 
150  // fOutFile=fRootManager->OpenOutFile(fOutname);
151  LOG(info) << "============== FairRunSim: Initialising simulation run ==============";
152 
153  FairGeoLoader* loader = new FairGeoLoader(fLoaderName->Data(), "Geo Loader");
154  FairGeoInterface* GeoInterFace = loader->getGeoInterface();
155  GeoInterFace->SetNoOfSets(ListOfModules->GetEntries());
156  GeoInterFace->setMediaFile(MatFname.Data());
157  GeoInterFace->readMedia();
158 
159  // gSystem->cd(flout.Data());
160 
161  fApp = new FairMCApplication("Fair", "The Fair VMC App", ListOfModules, MatFname);
163 
164  // Add a Generated run ID to the FairRunTimeDb
165  if (fRunId == 0) {
166  FairRunIdGenerator genid;
167  // FairRuntimeDb *rtdb= GetRuntimeDb();
168  fRunId = genid.generateId();
169  }
170  fRtdb->addRun(fRunId);
171 
174  fApp->AddTask(fTask);
176  FairBaseParSet* par = dynamic_cast<FairBaseParSet*>(fRtdb->getContainer("FairBaseParSet"));
177  if (par) {
179  par->SetGen(GetPrimaryGenerator());
180  par->SetBeamMom(fBeamMom);
181  }
182 
184  FairGeoParSet* geopar = dynamic_cast<FairGeoParSet*>(fRtdb->getContainer("FairGeoParSet"));
185  if (geopar) {
186  geopar->SetGeometry(gGeoManager);
187  }
188 
189  // Set global Parameter Info
190 
191  if (fPythiaDecayer) {
193  if (fPythiaDecayerConfig) {
195  }
196  }
197  if (fUserDecay) {
199  if (fUserDecayConfig) {
201  }
202  }
203  // on/off visualisation
204  if (fStoreTraj) {
205  LOG(info) << "Create visualisation manager ";
206  new FairTrajFilter();
207  }
208  if (fRadLength) {
210  }
211  if (fRadMap) {
213  }
214  if (fRadGrid) {
216  }
217 
218  if (fField) {
219  fField->Init();
220  }
221  fApp->SetField(fField);
223 
224  TList* containerList = fRtdb->getListOfContainers();
225  TIter next(containerList);
226  FairParSet* cont;
227  TObjArray* ContList = new TObjArray();
228  while ((cont = dynamic_cast<FairParSet*>(next()))) {
229  ContList->Add(new TObjString(cont->GetName()));
230  }
231  if (par) {
232  par->SetContListStr(ContList);
233  par->SetRndSeed(gRandom->GetSeed());
234  par->setChanged();
235  par->setInputVersion(fRunId, 1);
236  }
237  if (geopar) {
238  geopar->setChanged();
239  geopar->setInputVersion(fRunId, 1);
240  }
241 
243  SetMCConfig();
245 }
246 
248 {
249  if (fField) {
251  }
252 }
253 
254 void FairRunSim::CheckFlukaExec()
255 {
257  TString work = getenv("VMCWORKDIR");
258  TString work_config = work + "/gconfig/";
259  work_config.ReplaceAll("//", "/");
260 
261  TString config_dir = getenv("CONFIG_DIR");
262  config_dir.ReplaceAll("//", "/");
263  if (!config_dir.EndsWith("/")) {
264  config_dir += "/";
265  }
266 
267  TString flout;
268  if (strcmp(GetName(), "TFluka") == 0) {
269  TString flexec = "run_fluka.sh";
270  if (TString(gSystem->FindFile(config_dir.Data(), flexec)) != TString("")) {
271  LOG(info) << "---User path for Configuration is used: " << config_dir.Data();
272  } else {
273  flexec = work_config + "run_fluka.sh";
274  }
275  gSystem->Exec(flexec.Data());
276  // More generic for different macro
277  // dir layout <D.B>
278  TString macrodir = getenv("PWD");
279  // cout << macrodir << endl;
280  flout = macrodir + "/fluka_out";
281  flout.ReplaceAll("//", "/");
282  gSystem->cd(flout.Data());
283  }
284 }
285 
286 void FairRunSim::SetMCConfig()
287 {
288  // Either setup the simulation with the provided user hook
289  if (fUseSimSetupFunction) {
290  fSimSetup();
291  } else {
292  if (fSimulationConfig == nullptr) // RKRKRK COMMENT
294  fSimulationConfig->Setup(GetName());
295  }
296 
297  fApp->InitMC("foo", "bar");
298 
299  if (fUseSimSetupFunction) {
301  } else {
302  fSimulationConfig->SetupPostInit(GetName());
303  }
304 }
305 
306 void FairRunSim::Run(Int_t NEvents, Int_t) { fApp->RunMC(NEvents); }
307 
308 void FairRunSim::SetField(FairField* field) { fField = field; }
309 
311 
312 void FairRunSim::SetMaterials(const char* MatFileName)
313 {
314  TString Mat = "";
315  TString work = getenv("GEOMPATH");
316  work.ReplaceAll("//", "/");
317  if (work.IsNull()) {
318  work = getenv("VMCWORKDIR");
319  Mat = work + "/geometry/";
320  Mat.ReplaceAll("//", "/");
321  } else {
322  Mat = work;
323  if (!Mat.EndsWith("/")) {
324  Mat += "/";
325  }
326  }
327  MatFname = Mat + MatFileName;
328  LOG(info) << "Media file used: " << MatFname.Data();
329 }
330 
331 void FairRunSim::SetGeoModel(char* name)
332 {
333  if (strncmp(fName, "TGeant3", 7) == 0) {
334  delete fLoaderName;
335  fLoaderName = new TString(name);
336  LOG(info) << "FairRun::SetGeoModel(): G3 native geometry model used ";
337  } else {
338  LOG(info) << "FairRun::SetGeoModel(): Geant3 MC engine only !";
339  LOG(info) << "FairRun::SetGeoModel(): Method skipped ... ";
340  }
341 }
342 
344 {
347  fPythiaDecayer = kTRUE;
348 }
349 
350 void FairRunSim::SetUserDecay(const TString& Config)
351 {
354  fUserDecay = kTRUE;
355 }
356 
358 {
359  if (nullptr == fMCEvHead) {
361  }
362  return fMCEvHead;
363 }
364 
365 TMCThreadLocal FairRunSim* FairRunSim::fginstance = 0;
void SetPythiaDecayerConfig(const TString decayerConf)
void AddMesh(FairMesh *Mesh)
Definition: FairRunSim.cxx:124
Bool_t fPythiaDecayer
/** Geometry Model (TGeo or G3)*/
Definition: FairRunSim.h:214
FairRunSim(Bool_t isMaster=kTRUE)
Definition: FairRunSim.cxx:49
virtual const char * GetName() const
Definition: FairParSet.h:38
Bool_t fAna
Definition: FairRun.h:203
std::function< void()> fSimSetupPostInit
Definition: FairRunSim.h:229
void SetModId(Int_t id)
Definition: FairModule.h:117
void SetBeamMom(Double_t BMom)
void calculate()
Definition: FairMesh.cxx:62
void SetPythiaDecayer(Bool_t decayer)
void print()
Definition: FairMesh.cxx:92
static TMCThreadLocal FairRunSim * fginstance
/** MC Event Header */
Definition: FairRunSim.h:205
virtual void Setup(const char *mcEngine)
void SetField(FairField *field)
Definition: FairRunSim.cxx:308
unsigned int generateId(void)
Bool_t fRadMap
/** flag for registring radiation length*/
Definition: FairRunSim.h:219
FairMCEventHeader * GetMCEventHeader()
Definition: FairRunSim.cxx:357
FairGeoInterface * getGeoInterface()
Definition: FairGeoLoader.h:34
friend class FairMCApplication
Definition: FairRun.h:39
void SetFieldContainer()
Definition: FairRunSim.cxx:247
FairRootManager * fRootManager
Definition: FairRun.h:193
FairMCEventHeader * fMCEvHead
/** Primary Event Generator */
Definition: FairRunSim.h:204
TString MatFname
/** Array of used modules */
Definition: FairRunSim.h:211
void setChanged(Bool_t flag=kTRUE)
Definition: FairParSet.h:72
void SetRunId(UInt_t runid)
FairTask * fTask
Definition: FairRun.h:191
void WriteFileHeader(FairFileHeader *f)
virtual void FillParContainer()
Definition: FairField.h:107
void SetRadiationMapReg(Bool_t RadMap)
static FairRunSim * Instance()
Definition: FairRunSim.cxx:116
void SetGenerator(FairPrimaryGenerator *Gen)
Definition: FairRunSim.cxx:310
ClassImp(FairEventBuilder)
virtual void SetupPostInit(const char *mcEngine)
FairMCApplication * fApp
/** Internal counter*/
Definition: FairRunSim.h:200
void SetNoOfSets(Int_t n)
Bool_t fRadLength
/** Macro for decay configuration*/
Definition: FairRunSim.h:218
void SetContListStr(TObjArray *list)
TList * getListOfContainers()
FairParSet * getContainer(const Text_t *)
void SetRadiationLengthReg(Bool_t RadLen)
TObjArray * ListOfModules
/** Array of user defined particles*/
Definition: FairRunSim.h:210
void AddMeshList(TObjArray *meshList)
FairPrimaryGenerator * GetPrimaryGenerator()
Definition: FairRunSim.h:121
void SetRndSeed(UInt_t RndSeed)
void SetDetList(TObjArray *array)
TString fPythiaDecayerConfig
/** flag for using Pythia decayer*/
Definition: FairRunSim.h:215
Int_t count
Definition: FairRunSim.h:199
TObjArray * fParticles
/** Array of user defined ions */
Definition: FairRunSim.h:209
TObjArray * fMeshList
Definition: FairRunSim.h:221
FairPrimaryGenerator * fGen
/** flag for use Beam Energy */
Definition: FairRunSim.h:203
TObjArray * GetUserDefIons()
Definition: FairRunSim.cxx:131
void SetField(FairField *field)
Bool_t fUserDecay
/** Macro for Pythia decay configuration*/
Definition: FairRunSim.h:216
void SetUserDecayConfig(const TString decayerConf)
void SetPythiaDecayer(Bool_t decayer)
Definition: FairRunSim.h:124
FairGenericVMCConfig * fSimulationConfig
Definition: FairRunSim.h:232
FairMQExParamsParOne * par
virtual ~FairRunSim()
Definition: FairRunSim.cxx:89
FairFileHeader * fFileHeader
Definition: FairRun.h:207
TObjArray * GetListOfModules()
Definition: FairRunSim.h:118
Bool_t fStoreTraj
/** Material file name */
Definition: FairRunSim.h:212
virtual void Init()
Definition: FairRunSim.cxx:143
void SetGeoModel(char *name)
Definition: FairRunSim.cxx:331
void setInputVersion(Int_t v=-1, Int_t i=0)
Definition: FairParSet.h:51
void SetMaterials(const char *MatFileName)
Definition: FairRunSim.cxx:312
TString * fLoaderName
/** Trajectory store flags */
Definition: FairRunSim.h:213
Bool_t fRadGrid
/** flag for RadiationMapManager
Definition: FairRunSim.h:220
FairRtdbRun * addRun(Int_t runId, Int_t refId=-1)
std::function< void()> fSimSetup
/** Allow importing TGeometry to VMC */
Definition: FairRunSim.h:226
UInt_t fRunId
Definition: FairRun.h:201
TString fUserDecayConfig
Definition: FairRunSim.h:217
void SetUserDecay(const TString &Config)
Definition: FairRunSim.cxx:350
void SetGen(FairPrimaryGenerator *gen)
virtual void Run(Int_t NEvents=0, Int_t NotUsed=0)
Definition: FairRunSim.cxx:306
void AddModule(FairModule *Mod)
Definition: FairRunSim.cxx:118
void AddTask(TTask *fTask)
FairRuntimeDb * fRtdb
Definition: FairRun.h:189
TObjArray * fIons
/** Input file name map*/
Definition: FairRunSim.h:208
FairField * fField
/** Singleton Instance */
Definition: FairRunSim.h:206
void InitMC(const char *setup, const char *cuts)
void SetGenerator(FairPrimaryGenerator *fxGenerator)
void SetGeometry(TGeoManager *Geom)
Definition: FairGeoParSet.h:63
Double_t fBeamMom
/** Main VMC application */
Definition: FairRunSim.h:201
void RunMC(Int_t nofEvents)
virtual void Init()
Definition: FairField.h:53
void Config()
Configuration macro for Geant3 VirtualMC.
Definition: flConfig.C:10
TObjArray * GetUserDefParticles()
Definition: FairRunSim.cxx:137
void SetUserDecay(Bool_t decay)
*bool fUseSimSetupFunction
Definition: FairRunSim.h:228