VMC Examples Version 6.6
Loading...
Searching...
No Matches
testA01.cxx
Go to the documentation of this file.
1//------------------------------------------------
2// The Virtual Monte Carlo examples
3// Copyright (C) 2014 Ivana Hrivnacova
4// All rights reserved.
5//
6// For the licensing terms see geant4_vmc/LICENSE.
7// Contact: root-vmc@cern.ch
8//-------------------------------------------------
9
10/// \file testA01.cxx
11/// \brief The Geant4 VMC example A01 test application
12///
13/// The Geant4 VMC test application
14/// with explicitely instantiated TGeant3 or TGeant4 and linked
15/// with all libraries.
16///
17/// Usage:
18/// testA01
19/// [-g4g, --g4-geometry]: Geant4 VMC geometry option
20/// [-g4pl, --g4-physics-list]: Geant4 physics list selection
21/// [-g4sp, --g4-special-physics]: Geant4 special physics selection
22/// [-g4m, --g4-macro]: Geant4 macro
23/// [-g4vm, --g4-vis-macro]: Geant4 visualization macro
24/// [-g3g, --g3-geometry]: Geant3 geometry option
25/// (TGeant3,TGeant3TGeo)
26/// [-rm, --root-macro]: Root macro
27/// [-v, --verbose]: verbose option (yes,no)
28///
29/// Note that the g4* and g3* options are available only when built
30/// with the corresponding VMC_WITH_Geant4 or VMC_WITH_Geant3 option.
31/// Root macro with arguments should be passed within '', eg.
32/// --root-macro 'test_A01.C("",kFALSE)'
33///
34/// \date 26/02/2014
35/// \author I. Hrivnacova; IPN, Orsay
36
37#include "A01MCApplication.h"
38#include "A01PrimaryGenerator.h"
39
40#ifdef USE_GEANT4
41#include "A01RunConfiguration.h"
42#include "TG4RunConfiguration.h"
43#include "TGeant4.h"
44#endif
45
46#ifdef USE_GEANT3
47#include "TGeant3TGeo.h"
48#endif
49
50#include "TInterpreter.h"
51#include "TROOT.h"
52#include "TThread.h"
53
54#include <iostream>
55#include <string>
56
57namespace
58{
59
60/// Prints usage on error output stream
61void PrintUsage(std::string programName)
62{
63 std::cerr << " Usage: " << std::endl;
64 std::cerr << " " << programName << std::endl;
65#ifdef USE_GEANT4
66 std::cerr << " [-g4g, --g4-geometry]: Geant4 VMC geometry option"
67 << std::endl;
68 std::cerr << " [-g4pl, --g4-physics-list]: Geant4 physics list selection"
69 << std::endl;
70 std::cerr
71 << " [-g4sp, --g4-special-physics]: Geant4 special physics selection"
72 << std::endl;
73 std::cerr << " [-g4m, --g4-macro]: Geant4 macro" << std::endl;
74 std::cerr << " [-g4vm, --g4-vis-macro]: Geant4 visualization macro"
75 << std::endl;
76 std::cerr << " [-g4uc, --g4-user-class]: Geant4 user class "
77 << " (geometry)" << std::endl;
78 std::cerr << " [-g4lf, --g4-local-field]: Geant4 local field option "
79 << std::endl;
80#endif
81#ifdef USE_GEANT3
82 std::cerr << " [-g3g, --g3-geometry]: Geant3 geometry option "
83 "(TGeant3,TGeant3TGeo)"
84 << std::endl;
85#endif
86 std::cerr << " [-rm, --root-macro]: Root macro" << std::endl;
87 std::cerr << " [-v, --verbose]: verbose option (yes,no)"
88 << std::endl;
89}
90
91#ifdef USE_GEANT4
92/// Prints selected configuration on output stream (Geant4)
93void PrintG4Configuration(const std::string& programName,
94 const std::string& g4Geometry, const std::string& g4PhysicsList,
95 const std::string& g4SpecialPhysics, const std::string& g4Macro,
96 const std::string& g4VisMacro, const std::string& g4Session,
97 const std::string& g4UserClass, const std::string& g4LocalField,
98 const std::string& rootMacro)
99{
100 std::cout << " Running " << programName << " with options:" << std::endl;
101 std::cout << " --g4-geometry: " << g4Geometry << std::endl;
102 std::cout << " --g4-physics-list: " << g4PhysicsList << std::endl;
103 if (g4SpecialPhysics.size()) {
104 std::cout << " --g4-special-physics: " << g4SpecialPhysics << std::endl;
105 }
106 if (g4Macro.size()) {
107 std::cout << " --g4-macro: " << g4Macro << std::endl;
108 }
109 if (g4VisMacro.size()) {
110 std::cout << " --g4-vis-macro: " << g4VisMacro << std::endl;
111 }
112 if (g4Session.size()) {
113 std::cout << " --g4-session: " << g4Session << std::endl;
114 }
115 if (g4UserClass.size()) {
116 std::cout << " --g4-user-class: " << g4UserClass << std::endl;
117 }
118 if (g4LocalField.size()) {
119 std::cout << " --g4-local-field: " << g4LocalField << std::endl;
120 }
121 if (rootMacro.size()) {
122 std::cout << " --root-macro: " << rootMacro << std::endl;
123 }
124}
125#endif
126
127#ifdef USE_GEANT3
128/// Prints selected configuration on output stream (Geant3)
129void PrintG3Configuration(const std::string& programName,
130 const std::string& g3Geometry, const std::string& rootMacro)
131{
132 std::cout << " Running: " << std::endl;
133 std::cout << " " << programName << std::endl;
134 std::cout << " --g3-geometry: " << g3Geometry << std::endl;
135 if (rootMacro.size()) {
136 std::cout << " --root-macro]: " << rootMacro << std::endl;
137 }
138}
139#endif
140
141} // namespace
142
143/// Application main program
144int main(int argc, char** argv)
145{
146 // Initialize Root threading.
147 // (Multi-threading is triggered automatically if Geant4 was built
148 // in MT mode.)
149#ifdef G4MULTITHREADED
150 ROOT::EnableThreadSafety();
151#endif
152
153 // Process arguments
154 // This code is generic with the exception of the start values and
155 // the program name
156#ifdef USE_GEANT4
157 std::string g4Geometry = "geomRootToGeant4";
158 std::string g4PhysicsList = "FTFP_BERT";
159 std::string g4SpecialPhysics = "stepLimiter";
160 std::string g4Macro = "g4config.in";
161 std::string g4VisMacro = "g4vis.in";
162 std::string g4Session = "";
163 std::string g4UserClass = "";
164 std::string g4LocalField = "";
165#endif
166#ifdef USE_GEANT3
167 std::string g3Geometry = "TGeant3TGeo";
168#endif
169 std::string rootMacro = "";
170 std::string verbose = "yes";
171
172 for (Int_t i = 1; i < argc; i = i + 2) {
173 std::cout << "processing " << argv[i] << " with " << argv[i + 1]
174 << std::endl;
175#ifdef USE_GEANT4
176 if (std::string(argv[i]) == "--g4-geometry" ||
177 std::string(argv[i]) == "-g4g")
178 g4Geometry = argv[i + 1];
179 else if (std::string(argv[i]) == "--g4-physics-list" ||
180 std::string(argv[i]) == "-g4pl")
181 g4PhysicsList = argv[i + 1];
182 else if (std::string(argv[i]) == "--g4-special-physics" ||
183 std::string(argv[i]) == "-g4sp")
184 g4SpecialPhysics = argv[i + 1];
185 else if (std::string(argv[i]) == "--g4-macro" ||
186 std::string(argv[i]) == "-g4m")
187 g4Macro = argv[i + 1];
188 else if (std::string(argv[i]) == "--g4-vis-macro" ||
189 std::string(argv[i]) == "-g4vm")
190 g4VisMacro = argv[i + 1];
191 else if (std::string(argv[i]) == "--g4-session" ||
192 std::string(argv[i]) == "-g4s")
193 g4Session = argv[i + 1];
194 // the following option is specific to use of Geant4 dependent classes
195 else if (std::string(argv[i]) == "--g4-user-class" ||
196 std::string(argv[i]) == "-g4uc")
197 g4UserClass = argv[i + 1];
198 else if (std::string(argv[i]) == "--g4-local-field" ||
199 std::string(argv[i]) == "-g4lf")
200 g4LocalField = argv[i + 1];
201#endif
202#ifdef USE_GEANT3
203 if (std::string(argv[i]) == "--g3-geometry" ||
204 std::string(argv[i]) == "-g3g")
205 g3Geometry = argv[i + 1];
206#endif
207 else if (std::string(argv[i]) == "--root-macro" ||
208 std::string(argv[i]) == "-rm")
209 rootMacro = argv[i + 1];
210 else if (std::string(argv[i]) == "--verbose" ||
211 std::string(argv[i]) == "-v")
212 verbose = argv[i + 1];
213 else {
214 PrintUsage("testA01");
215 return 1;
216 }
217 }
218
219 if (verbose == "yes") {
220#ifdef USE_GEANT4
221 PrintG4Configuration("testA01", g4Geometry, g4PhysicsList, g4SpecialPhysics,
222 g4Macro, g4VisMacro, g4Session, g4UserClass, g4LocalField, rootMacro);
223#endif
224#ifdef USE_GEANT3
225 PrintG3Configuration("testA01", g3Geometry, rootMacro);
226#endif
227 }
228 //
229 // end of code to process arguments
230
231 // Create MC application (thread local)
232 A01MCApplication* appl =
233 new A01MCApplication("ExampleA01", "The exampleA01 MC application");
234#ifdef USE_GEANT4
235 // Local magnetic field option
236 if (g4LocalField.size()) {
237 G4cout << "setting local field to true" << G4endl;
238 appl->SetUseLocalMagField(true);
239 }
240
241 // RunConfiguration for Geant4
242 TG4RunConfiguration* runConfiguration = 0;
243 if (!g4UserClass.size()) {
244 runConfiguration =
245 new TG4RunConfiguration(g4Geometry, g4PhysicsList, g4SpecialPhysics);
246 }
247 else if (g4UserClass == "geometry") {
248 runConfiguration = new A01RunConfiguration(g4PhysicsList, g4SpecialPhysics);
249 // Local magnetic field option
250 if (g4LocalField.size()) {
251 static_cast<A01RunConfiguration*>(runConfiguration)
252 ->SetUseLocalMagField(true);
253 }
254 }
255 else {
256 PrintUsage("testA01");
257 return 1;
258 }
259
260 // TGeant4
261 // TO DO: pass g4session here
262 TGeant4* geant4 = new TGeant4(
263 "TGeant4", "The Geant4 Monte Carlo", runConfiguration, argc, argv);
264
265 // Customise Geant4 setting
266 // (verbose level, global range cut, ..)
267 if (g4Macro.size()) {
268 geant4->ProcessGeantMacro(g4Macro.data());
269 }
270#endif
271
272#ifdef USE_GEANT3
273 if (g3Geometry == "TGeant3") {
274 new TGeant3("C++ Interface to Geant3");
275 }
276 else if (g3Geometry == "TGeant3TGeo") {
277 new TGeant3TGeo("C++ Interface to Geant3");
278 }
279 else {
280 PrintUsage("testA01");
281 return 1;
282 }
283 gMC->SetProcess("DRAY", 1);
284 gMC->SetProcess("LOSS", 1);
285 gMC->SetProcess("HADR", 1);
286#endif
287
288 // Run example
289 if (!rootMacro.size()) {
290 // Run from this main
291 appl->GetPrimaryGenerator()->SetRandomize(false);
292 appl->SetWriteStack(true);
293 appl->SetWriteHits(true);
294 appl->InitMC("");
295#ifdef USE_GEANT4
296 // Setting Geant4 visualization
297 if (g4VisMacro.size()) {
298 geant4->ProcessGeantMacro(g4VisMacro.data());
299 }
300#endif
301 appl->RunMC(5);
302 }
303 else {
304 // Run from Root macro
305 gROOT->Macro(rootMacro.data());
306 }
307
308 delete appl;
309}
Definition of the A01MCApplication class.
Definition of the A01PrimaryGenerator class.
Definition of the A01RunConfiguration class.
Implementation of the TVirtualMCApplication.
void SetWriteHits(Bool_t writeHits)
void RunMC(Int_t nofEvents)
void SetWriteStack(Bool_t writeStack)
A01PrimaryGenerator * GetPrimaryGenerator() const
void InitMC(const char *setup)
void SetUseLocalMagField(Bool_t localMagField)
void SetRandomize(Bool_t val)
User Geant4 VMC run configuration.
int main(int argc, char **argv)
Application main program.
Definition testA01.cxx:144
std::string verbose
Definition testE03c.cxx:75
std::string rootMacro
Definition testE03c.cxx:74