FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairYamlVMCConfig.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 // ----- FairYamlVMCConfigurce file -----
10 // ----- Created 2019.02.19 by R. Karabowicz -----
11 // -------------------------------------------------------------------------
12 #include "FairYamlVMCConfig.h"
13 
15 #include "FairLogger.h"
16 #include "FairRunSim.h"
17 #include "FairSink.h" // for FairSink
18 
19 #include <Rtypes.h>
20 #include <TGeant3.h>
21 #include <TGeant3TGeo.h>
22 #include <TGeant4.h>
23 #include <TObjString.h> // for TObjString
24 #include <TObject.h> // for TObject, TObject::kSingleKey
25 #include <TString.h>
26 #include <TSystem.h> // for TSystem, gSystem
27 #include <TVirtualMC.h> // for TVirtualMC
28 #include <cstdlib> // for getenv
29 #include <cstring> // for strcmp, strncmp
30 #include <ostream> // for operator<<, ostringstream
31 #include <string> // for string, basic_string, cha...
32 #include <vector> // for vector
33 
36  , fMCEngine("")
37 {
39 }
40 
41 void FairYamlVMCConfig::Setup(const char* mcEngine)
42 {
43  fMCEngine = mcEngine;
44  if (!((strcmp(mcEngine, "TGeant4") == 0) || (strcmp(mcEngine, "TGeant3") == 0))) {
45  LOG(fatal) << "FairYamlVMCConfig::Setup() Engine \"" << mcEngine << "\" unknown!";
46  }
47 
48  string yamlFileName = ObtainYamlFileName(mcEngine);
49  fYamlConfig = YAML::LoadFile(yamlFileName);
50 
51  if (strcmp(mcEngine, "TGeant4") == 0) {
52  SetupGeant4();
53  } else if (strcmp(mcEngine, "TGeant3") == 0) {
54  SetupGeant3();
55  }
56  SetupStack();
57  SetCuts();
58 
59  StoreYamlInfo();
60 }
61 
62 void FairYamlVMCConfig::SetupPostInit(const char* mcEngine)
63 {
64  if ( !fPostInitFlag ) {
65  LOG(info) << "FairYamlVMCConfig::SetupPostInit() OFF." << fPostInitName;
66  return;
67  }
68 
69  if (!((strcmp(mcEngine, "TGeant4") == 0))) {
70  LOG(fatal) << "FairYamlVMCConfig::SetupPostInit() only valid for TGeant4.";
71  }
72 
73  TString work = getenv("VMCWORKDIR");
74  TString work_config = work + "/gconfig/";
75  work_config.ReplaceAll("//", "/");
76 
77  TString config_dir = getenv("CONFIG_DIR");
78  config_dir.ReplaceAll("//", "/");
79 
80  Bool_t AbsPath = kFALSE;
81 
82  TString ConfigMacro;
83  TString g4Macro;
84  if (fPostInitName.empty()) {
85  g4Macro = "g4ConfigPostInit.C";
86  fPostInitName = g4Macro;
87  } else {
88  if (fPostInitName.find("/")!=std::string::npos) {
89  AbsPath = kTRUE;
90  }
91  g4Macro = fPostInitName;
92  LOG(info) << "---------------User config is used: " << g4Macro.Data();
93  }
94  if (!AbsPath && TString(gSystem->FindFile(config_dir.Data(), g4Macro)) != TString("")) {
95  LOG(info) << "---User path for Configuration (" << fPostInitName << ") is used: " << config_dir.Data();
96  ConfigMacro = g4Macro;
97  } else {
98  if (AbsPath) {
99  ConfigMacro = fPostInitName;
100  } else {
101  ConfigMacro = work_config + fPostInitName;
102  }
103  }
104 
105  fYamlConfigPostInit = YAML::LoadFile(ConfigMacro.Data());
106 
107  if (fYamlConfigPostInit["Geant4_PostInit_Commands"]) {
108  std::vector<std::string> g4commands = fYamlConfigPostInit["Geant4_PostInit_Commands"].as<std::vector<std::string>>();
109  for ( const auto& value: g4commands ) {
110  LOG(info) << " execute command \"" << value << "\"";
111  TGeant4* geant4 = dynamic_cast<TGeant4*>(TVirtualMC::GetMC());
112  geant4->ProcessGeantCommand(value.data());
113  }
114  }
115 
116  LOG(info) << "got info from " << fPostInitName;
117 }
118 
120 {
121  LOG(info) << "FairYamlVMCConfig::SetupGeant3() called";
123  TString* gModel = fRun->GetGeoModel();
124  TGeant3* geant3 = nullptr;
125  if (strncmp(gModel->Data(), "TGeo", 4) == 0) {
126  geant3 = new TGeant3TGeo("C++ Interface to Geant3");
127  } else {
128  geant3 = new TGeant3("C++ Interface to Geant3");
129  }
130 
131  if (fYamlConfig["G3_TRIG"])
132  geant3->SetTRIG(fYamlConfig["G3_TRIG"].as<int>());
133  if (fYamlConfig["G3_SWIT"]) {
134  std::vector<int> intVect = fYamlConfig["G3_SWIT"].as<std::vector<int>>();
135  if (intVect.size() != 2)
136  LOG(fatal) << "FairSetupGeant3: expecting 2 integers to setup G3_SWIT";
137  geant3->SetSWIT(intVect[0], intVect[1]);
138  }
139  if (fYamlConfig["G3_DEBU"]) {
140  std::vector<int> intVect = fYamlConfig["G3_DEBU"].as<std::vector<int>>();
141  if (intVect.size() != 3)
142  LOG(fatal) << "FairSetupGeant3: expecting 3 integers to setup G3_DEBU";
143  geant3->SetDEBU(intVect[0], intVect[1], intVect[2]);
144  }
145  if (fYamlConfig["G3_RAYL"])
146  geant3->SetRAYL(fYamlConfig["G3_RAYL"].as<int>());
147  if (fYamlConfig["G3_STRA"])
148  geant3->SetSTRA(fYamlConfig["G3_STRA"].as<int>());
149  if (fYamlConfig["G3_AUTO"])
150  geant3->SetAUTO(fYamlConfig["G3_AUTO"].as<int>());
151  if (fYamlConfig["G3_ABAN"])
152  geant3->SetABAN(fYamlConfig["G3_ABAN"].as<int>());
153  if (fYamlConfig["G3_OPTI"])
154  geant3->SetOPTI(fYamlConfig["G3_OPTI"].as<int>());
155  if (fYamlConfig["G3_ERAN"])
156  geant3->SetERAN(fYamlConfig["G3_ERAN"].as<double>());
157  if (fYamlConfig["G3_CKOV"])
158  geant3->SetCKOV(fYamlConfig["G3_CKOV"].as<int>());
159 }
160 
162 {
163  LOG(info) << "FairYamlVMCConfig::SetupGeant4() called";
164 
165  if (!fYamlConfig["Geant4_UserGeometry"]) {
166  LOG(fatal) << "User geometry not provided";
167  }
168  if (!fYamlConfig["Geant4_PhysicsList"]) {
169  LOG(fatal) << "Physics list not provided";
170  }
171  if (!fYamlConfig["Geant4_SpecialProcess"]) {
172  LOG(fatal) << "Special processy not provided";
173  }
174  bool specialStacking = false;
175  if (fYamlConfig["Geant4_SpecialStacking"]) {
176  // LOG(info) << "Special stacking used";
177  specialStacking = fYamlConfig["Geant4_SpecialStacking"].as<bool>();
178  }
179  bool mtMode = FairRunSim::Instance()->IsMT();
180 
181  if (fYamlConfig["Geant4_Multithreaded"]) {
182  mtMode = fYamlConfig["Geant4_Multithreaded"].as<bool>();
183  // LOG(info) << "Setting Geant4 multithreaded to " << (mtMode?"true":"false");
184  }
185 
186  FairFastSimRunConfiguration* runConfiguration =
187  new FairFastSimRunConfiguration(fYamlConfig["Geant4_UserGeometry"].as<std::string>(),
188  fYamlConfig["Geant4_PhysicsList"].as<std::string>(),
189  fYamlConfig["Geant4_SpecialProcess"].as<std::string>(),
190  specialStacking,
191  mtMode);
192 
193  TGeant4* geant4 = new TGeant4("TGeant4", "The Geant4 Monte Carlo", runConfiguration);
194 
195  if (fYamlConfig["Geant4_MaxNStep"]) {
196  LOG(info) << " execute SetMaxNStep (" << fYamlConfig["Geant4_MaxNStep"].as<int>() << ")";
197  geant4->SetMaxNStep(fYamlConfig["Geant4_MaxNStep"].as<int>());
198  }
199  if (fYamlConfig["Geant4_Commands"]) {
200  std::vector<std::string> g4commands = fYamlConfig["Geant4_Commands"].as<std::vector<std::string>>();
201  for ( const auto& value: g4commands ) {
202  LOG(info) << " execute command \"" << value << "\"";
203  geant4->ProcessGeantCommand(value.data());
204  }
205  }
206 
207  LOG(info) << geant4->GetName() << " MonteCarlo engine created!.";
208 }
209 
211 {
212  LOG(info) << "FairYamlVMCConfig::SetCuts() called";
213  if (fYamlConfig["MonteCarlo_Process"]) {
214  YAML::Node mcProcess = fYamlConfig["MonteCarlo_Process"];
215  TVirtualMC* MC = TVirtualMC::GetMC();
216  for (auto it = mcProcess.begin(); it != mcProcess.end(); ++it) {
217  // LOG(info) << "proc: " << it->first << " --> " << it->second;
218  MC->SetProcess(it->first.as<std::string>().c_str(), it->second.as<int>());
219  }
220  }
221 
222  if (fYamlConfig["MonteCarlo_Cut"]) {
223  YAML::Node mcProcess = fYamlConfig["MonteCarlo_Cut"];
224  TVirtualMC* MC = TVirtualMC::GetMC();
225  for (auto it = mcProcess.begin(); it != mcProcess.end(); ++it) {
226  // LOG(info) << "cut: " << it->first << " --> " << it->second;
227  MC->SetCut(it->first.as<std::string>().c_str(), it->second.as<double>());
228  }
229  }
230 }
231 
232 string FairYamlVMCConfig::ObtainYamlFileName(const char* mcEngine)
233 {
234  TString lUserConfig = FairRunSim::Instance()->GetUserConfig();
235 
236  TString work = getenv("VMCWORKDIR");
237  TString work_config = work + "/gconfig/";
238  work_config.ReplaceAll("//", "/");
239 
240  TString config_dir = getenv("CONFIG_DIR");
241  config_dir.ReplaceAll("//", "/");
242 
243  Bool_t AbsPath = kFALSE;
244 
245  TString configFileWithPath;
246 
247  TString configFile;
248  if (lUserConfig.IsNull()) {
249  if (strcmp(mcEngine, "TGeant4") == 0) {
250  configFile = "g4Config.yaml";
251  }
252  if (strcmp(mcEngine, "TGeant3") == 0) {
253  configFile = "g3Config.yaml";
254  }
255  lUserConfig = configFile;
256  } else {
257  if (lUserConfig.Contains("/")) {
258  AbsPath = kTRUE;
259  }
260  configFile = lUserConfig;
261  LOG(info) << "---------------User config is used: " << configFile.Data();
262  }
263  if (!AbsPath && TString(gSystem->FindFile(config_dir.Data(), configFile)) != TString("")) {
264  LOG(info) << "---------------CONFIG_DIR is used: " << config_dir.Data() << " to get \"" << configFile.Data()
265  << "\"";
266  configFileWithPath = configFile;
267  } else {
268  if (AbsPath) {
269  configFileWithPath = lUserConfig;
270  } else {
271  configFileWithPath = work_config + lUserConfig;
272  }
273  }
274  return configFileWithPath.Data();
275 }
276 
277 void FairYamlVMCConfig::StoreYamlInfo()
278 {
279  std::ostringstream nodestring;
280  nodestring << fMCEngine << "\n";
281  nodestring << fYamlConfig;
282  nodestring << "\n";
283  TObjString* configObject = new TObjString(nodestring.str().c_str());
284  FairRunSim::Instance()->GetSink()->WriteObject(configObject, "SimulationSetup", TObject::kSingleKey);
285 
286  LOG(info) << "FairYamlVMCConfig::StoreYamlInfo() done.";
287 }
288 
YAML::Node fYamlConfigPostInit
virtual void SetupStack()=0
TString * GetGeoModel()
Definition: FairRunSim.h:109
Bool_t IsMT() const
Definition: FairRunSim.h:185
virtual void Setup(const char *mcEngine)
TString GetUserConfig()
Definition: FairRunSim.h:151
static FairRunSim * Instance()
Definition: FairRunSim.cxx:116
FairSink * GetSink()
Definition: FairRun.h:93
ClassImp(FairEventBuilder)
virtual void UsePostInitConfig(bool useC=true, const char *stringC="g4ConfigPostInit.yaml")
virtual void WriteObject(TObject *f, const char *, Int_t option=0)=0
virtual void SetupPostInit(const char *mcEngine)