EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairRunSim.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairRunSim.cxx
1 //_____________________________________________________________________________
2 // ----- FairRunSim source file -----
3 // ----- Created 06/01/04 by M. Al-Turany -----
4 //_____________________________________________________________________________
5 #include "FairRunSim.h"
6 #include "FairRootManager.h"
7 #include "FairTrajFilter.h"
8 #include "FairBaseParSet.h"
9 #include "FairRunIdGenerator.h"
10 #include "FairModule.h"
11 #include "FairField.h"
12 #include "FairRuntimeDb.h"
13 #include "FairGeoLoader.h"
14 #include "FairGeoInterface.h"
15 #include "FairMCEventHeader.h"
16 #include "FairPrimaryGenerator.h"
17 #include "FairMesh.h"
18 #include "TROOT.h"
19 #include "TSystem.h"
20 #include <iostream>
21 #include "FairFileHeader.h"
22 
23 using std::cout;
24 using std::endl;
25 
27 //_____________________________________________________________________________
29  :FairRun(),
30  count(0),
31  fApp(NULL),
32  fBeamMom(0),
33  fUseBeamMom(kFALSE),
34  fGen(NULL),
35  fMCEvHead(NULL),
36  fField(NULL),
37  fMapName(""),
38  fIons(new TObjArray()),
39  fParticles( new TObjArray()),
40  ListOfModules(new TObjArray()),
41  MatFname(""),
42  fStoreTraj( kFALSE),
43  fLoaderName( new TString("TGeo")),
44  fPythiaDecayer(kFALSE),
45  fPythiaDecayerConfig(""),
46  fUserDecay(kFALSE),
47  fUserDecayConfig(""),
48  fRadLength(kFALSE),
49  fRadMap(kFALSE),
50  fRadGrid(kFALSE),
51  fMeshList( new TObjArray() ),
52  fUserConfig(""),
53  fUserCuts("SetCuts.C")
54 
55 {
56  if (fginstance) {
57  Fatal("FairRun", "Singleton instance already exists.");
58  return;
59  }
60  fOutname="";
61  fginstance=this;
62  fRunId=0;
63  fAna=kFALSE;
64 }
65 //_____________________________________________________________________________
67 {
68  fLogger->Debug(MESSAGE_ORIGIN," Enter Destructor of FairRunSim ");
73  fLogger->Debug(MESSAGE_ORIGIN," Start deleting all registered modules");
74  ListOfModules->Delete();
75  delete ListOfModules;
76  fLogger->Debug(MESSAGE_ORIGIN," Finish deleting all registered modules");
77 
78  fIons->Delete();
79  delete fIons;
80  fParticles->Delete();
81  delete fParticles;
82 
83  delete fApp;
84  delete fField;
85  delete fGen;
86  delete fMCEvHead;
87 }
88 //_____________________________________________________________________________
90 {
91  return fginstance;
92 }
93 //_____________________________________________________________________________
95 {
96  ListOfModules->Add(Mod);
97  Mod->SetModId(count++);
98 }
99 //_____________________________________________________________________________
101 {
102  Mesh->print();
103  Mesh->calculate();
104  fMeshList->Add(Mesh);
105 }
106 //_____________________________________________________________________________
108 {
110  return fIons;
111 }
112 //_____________________________________________________________________________
114 {
116  return fParticles;
117 }
118 //_____________________________________________________________________________
120 {
123  CheckFlukaExec();
124 
125 // fOutFile=fRootManager->OpenOutFile(fOutname);
126  fLogger->Info(MESSAGE_ORIGIN,"============== FairRunSim: Initialising simulation run ==============");
127 
128  FairGeoLoader* loader=new FairGeoLoader(fLoaderName->Data(), "Geo Loader");
129  FairGeoInterface* GeoInterFace=loader->getGeoInterface();
130  GeoInterFace->SetNoOfSets(ListOfModules->GetEntries());
131  GeoInterFace->setMediaFile(MatFname.Data());
132  GeoInterFace->readMedia();
133 
134  // gSystem->cd(flout.Data());
135 
136  if (!fApp) fApp= new FairMCApplication("Fair","The Fair VMC App",ListOfModules, MatFname);
138 
139  // Add a Generated run ID to the FairRunTimeDb
140  FairRunIdGenerator genid;
141  // FairRuntimeDb *rtdb= GetRuntimeDb();
142  fRunId = genid.generateId();
143  fRtdb->addRun(fRunId);
144 
147  fApp->AddTask(fTask);
148 
149  FairBaseParSet* par=(FairBaseParSet*)(fRtdb->getContainer("FairBaseParSet"));
151  par->SetGen(GetPrimaryGenerator());
152  par->SetBeamMom(fBeamMom);
153  par->SetGeometry(gGeoManager);
154 
155  // Set global Parameter Info
156 
157  if(fPythiaDecayer) {
159  }
160  if(fUserDecay) {
162  }
163  // on/off visualisation
164  if( fStoreTraj ) {
165  fLogger->Info(MESSAGE_ORIGIN, "Create visualisation manager ");
166  new FairTrajFilter();
167  }
168  if(fRadLength) {
170  }
171  if(fRadMap) {
173  }
174  if(fRadGrid) {
176  }
177 
178  if(fField) { fField->Init(); }
179  fApp->SetField(fField);
181 
182  TList* containerList=fRtdb->getListOfContainers();
183  TIter next(containerList);
184  FairParSet* cont;
185  TObjArray* ContList= new TObjArray();
186  while ((cont=(FairParSet*)next())) {
187  ContList->Add(new TObjString(cont->GetName()));
188  }
189 
190  par->SetContListStr(ContList);
191  par->setChanged();
192  par->setInputVersion(fRunId,1);
193 
194 
196  SetMCConfig();
198 
199 }
200 //_____________________________________________________________________________
202 {
203  if (fField) {
204  fField->FillParContainer();
205  }
206 }
207 //_____________________________________________________________________________
209 {
211  TString work = getenv("VMCWORKDIR");
212  TString work_config=work+"/gconfig/";
213  TString config_dir= getenv("CONFIG_DIR");
214  if (!config_dir.EndsWith("/")) { config_dir+="/"; }
215  TString flout;
216  if(strcmp(GetName(),"TFluka") == 0 ) {
217  TString flexec="run_fluka.sh";
218  if (TString(gSystem->FindFile(config_dir.Data(),flexec)) != TString("")) {
219  fLogger->Info(MESSAGE_ORIGIN,"---User path for Configuration is used : %s ", config_dir.Data());
220  } else {
221  flexec=work_config+"run_fluka.sh";
222  }
223  gSystem->Exec(flexec.Data());
224  // More generic for different macro
225  // dir layout <D.B>
226  TString macrodir = getenv("PWD");
227  cout << macrodir << endl;
228  flout = macrodir + "/fluka_out" ;
229  gSystem->cd(flout.Data());
230  }
231 }
232 //_____________________________________________________________________________
234 {
235  //printf("FairRunSim::SetMCConfig() #1 ...\n");
238  TString work = getenv("VMCWORKDIR");
239  //TString Lib_config= getenv("GEANT4VMC_MACRO_DIR");
240  TString work_config=work+"/gconfig/";
241  TString config_dir= getenv("CONFIG_DIR");
242  Bool_t AbsPath=kFALSE;
243  if (!config_dir.EndsWith("/")) { config_dir+="/"; }
244  //if (!Lib_config.EndsWith("/")) { Lib_config+="/"; }
245 
246  //TString LibMacro;
247  //TString LibFunction;
248  TString ConfigMacro;
249  TString cuts=fUserCuts;
250 
251  //printf("FairRunSim::SetMCConfig() #2 ...\n");
252 
253  //----------------------------------------------Geant4 Config-----------------------------------------
254  if(strcmp(GetName(),"TGeant4") == 0 ) {
255  //TString g4LibMacro="g4libs.C";
256  TString g4Macro;
257  if(fUserConfig.IsNull()) {
258  g4Macro="g4Config.C";
259  fUserConfig = g4Macro;
260  } else {
261  if (fUserConfig.Contains("/")) { AbsPath=kTRUE; }
262  g4Macro = fUserConfig;
263  fLogger->Info(MESSAGE_ORIGIN,"---------------User config is used : %s ",g4Macro.Data());
264  }
265  //if (TString(gSystem->FindFile(config_dir.Data(),g4LibMacro)) != TString("")) { //be carfull after this call the string g4LibMacro is empty if not found!!!!
266  //fLogger->Info(MESSAGE_ORIGIN,"---User path for Configuration (g4libs.C) is used : %s" ,config_dir.Data());
267  //} else {
268  //g4LibMacro=Lib_config+"g4libs.C";
269  //}
270  //LibMacro=g4LibMacro;
271  //LibFunction="g4libs()";
272  if (!AbsPath && TString(gSystem->FindFile(config_dir.Data(),g4Macro)) != TString("")) {
273  fLogger->Info(MESSAGE_ORIGIN,"---User path for Configuration (g4Config.C) is used : %s" , config_dir.Data());
274  ConfigMacro=g4Macro;
275  } else {
276  if(AbsPath) { ConfigMacro = fUserConfig; }
277  else { ConfigMacro =work_config+fUserConfig; }
278  }
279  //----------------------------------------------Geant3 Config-----------------------------------------
280  } else if(strcmp(GetName(),"TGeant3") == 0 ) {
281  //printf("FairRunSim::SetMCConfig() #3 ...\n");
282  //TString g3LibMacro="g3libs.C";
283  TString g3Macro="g3Config.C";
284  if(fUserConfig.IsNull()) {
285  g3Macro="g3Config.C";
286  fUserConfig = g3Macro;
287  fLogger->Info(MESSAGE_ORIGIN,"-------------- Standard Config is called ------------------------------------");
288  } else {
289  if (fUserConfig.Contains("/")) { AbsPath=kTRUE; }
290  g3Macro = fUserConfig;
291  fLogger->Info(MESSAGE_ORIGIN,"---------------User config is used : ", g3Macro.Data());
292  }
293  //if (TString(gSystem->FindFile(config_dir.Data(),g3LibMacro)) != TString("")) {
294  // fLogger->Info(MESSAGE_ORIGIN,"---User path for Configuration (g3libs.C) is used : %s " , config_dir.Data());
295  //} else {
296  //g3LibMacro=work_config+"g3libs.C";
297  //}
298  //LibMacro=g3LibMacro;
299  //LibFunction="g3libs()";
300  if (!AbsPath && TString(gSystem->FindFile(config_dir.Data(),g3Macro)) != TString("")) {
301  fLogger->Info(MESSAGE_ORIGIN,"---User path for Configuration (g3Config.C) is used : %s", config_dir.Data());
302  ConfigMacro=g3Macro;
303  } else {
304  if(AbsPath) { ConfigMacro = fUserConfig; }
305  else { ConfigMacro =work_config+fUserConfig; }
306  }
307 
308  //printf("FairRunSim::SetMCConfig() #4 ...\n");
309 
310  //----------------------------------------------Fluka Config-----------------------------------------
311  } else if(strcmp(GetName(),"TFluka") == 0 ) {
312  //TString flLibMacro="fllibs.C";
313  TString flMacro="flConfig.C";
314  if(fUserConfig.IsNull()) {
315  flMacro="flConfig.C";
316  fUserConfig=flMacro;
317  } else {
318  if (fUserConfig.Contains("/")) { AbsPath=kTRUE; }
319  flMacro = fUserConfig;
320  fLogger->Info(MESSAGE_ORIGIN,"---------------User config is used : %s " , flMacro.Data());
321  }
322  //if (TString(gSystem->FindFile(config_dir.Data(), flLibMacro)) != TString("")) {
323  //fLogger->Info(MESSAGE_ORIGIN,"---User path for Configuration (fllibs.C) is used : %s", config_dir.Data());
324  //} else {
325  //flLibMacro=work_config+"fllibs.C";
326  //}
327  //LibMacro=flLibMacro;
328  //LibFunction="fllibs()";
329  if (!AbsPath && TString(gSystem->FindFile(config_dir.Data(),flMacro)) != TString("")) {
330  fLogger->Info(MESSAGE_ORIGIN, "---User path for Configuration (flConfig.C) is used : %s", config_dir.Data());
331  ConfigMacro=flMacro;
332  } else {
333  if(AbsPath) { ConfigMacro = fUserConfig; }
334  else { ConfigMacro =work_config+fUserConfig; }
335  }
336  }
337  //printf("FairRunSim::SetMCConfig() #5 ...\n");
338  //----------------------------------------------SetCuts------------------------------------------------
339  if (TString(gSystem->FindFile(config_dir.Data(),cuts)) != TString("")) {
340  fLogger->Info(MESSAGE_ORIGIN,"---User path for Cuts and Processes (SetCuts.C) is used : %s", config_dir.Data());
341  } else {
342  cuts =work_config+ fUserCuts;
343  }
344  //printf("FairRunSim::SetMCConfig() #6a ...\n");
345  //--------------------------------------Now load the Config and Cuts------------------------------------
346  //gROOT->LoadMacro(LibMacro.Data());
347  //printf("FairRunSim::SetMCConfig() #6b ...\n");
348  //gROOT->ProcessLine(LibFunction.Data());
349  //printf("FairRunSim::SetMCConfig() #6c ...\n");
350 
351  gROOT->LoadMacro(ConfigMacro.Data());
352  //printf("FairRunSim::SetMCConfig() #6d ...\n");
353  gROOT->ProcessLine("Config()");
354  //printf("FairRunSim::SetMCConfig() #6e ...\n");
355 
356  gROOT->LoadMacro(cuts);
357  //printf("FairRunSim::SetMCConfig() #6f ...\n");
358  gROOT->ProcessLine("SetCuts()");
359  //printf("FairRunSim::SetMCConfig() #7 ...\n");
360 
361  fApp->InitMC(ConfigMacro.Data(), cuts.Data());
362  //printf("FairRunSim::SetMCConfig() #8 ...\n");
363 }
364 
365 //_____________________________________________________________________________
366 void FairRunSim::Run(Int_t NEvents, Int_t NotUsed)
367 {
368  fApp->RunMC(NEvents);
369 
370  // Help tutorials/designer/calorimeter/simulation.C to call exit() at the end without
371  // crashing; this is related to how fEicMoCaPointCollection variable is declared for
372  // ROOT streaming; why the problem occurs in a first place is still a mistery;
373  if (GetOutputFile()) GetOutputFile()->Close();
374 }
375 //_____________________________________________________________________________
376 void FairRunSim::SetField(FairField* field)
377 {
378  fField=field;
379 }
380 //_____________________________________________________________________________
382 {
383  fGen=Gen;
384 }
385 //_____________________________________________________________________________
386 void FairRunSim::SetMaterials(const char* MatFileName)
387 {
388  TString Mat="";
389  TString work = getenv("GEOMPATH");
390  if (work.IsNull()) {
391  work = getenv("VMCWORKDIR");
392  Mat=work+"/geometry/";
393  } else {
394  Mat=work;
395  if (!Mat.EndsWith("/")) { Mat+="/"; }
396  }
397  MatFname=Mat+MatFileName;
398  fLogger->Info(MESSAGE_ORIGIN,"Media file used : %s ", MatFname.Data());
399 }
400 //_____________________________________________________________________________
402 {
403  if ( strncmp(fName,"TGeant3",7) == 0 ) {
404  if (fLoaderName) { delete fLoaderName; }
405  fLoaderName = new TString(name);
406  fLogger->Info(MESSAGE_ORIGIN,"FairRun::SetGeoModel(): G3 native geometry model used ");
407  } else {
408  fLogger->Info(MESSAGE_ORIGIN,"FairRun::SetGeoModel(): Geant3 MC engine only !");
409  fLogger->Info(MESSAGE_ORIGIN,"FairRun::SetGeoModel(): Method skipped ... ");
410  }
411 }
412 //_____________________________________________________________________________
414 {
417  fPythiaDecayer =kTRUE;
418 }
419 //_____________________________________________________________________________
420 
421 void FairRunSim::SetUserDecay(const TString& Config)
422 {
425  fUserDecay = kTRUE;
426 }
427 //_____________________________________________________________________________
429 {
430  if ( NULL == fMCEvHead ) { fMCEvHead = new FairMCEventHeader(); }
431  return fMCEvHead;
432 }
433 //_____________________________________________________________________________
434 
436 
437 
438