-
Notifications
You must be signed in to change notification settings - Fork 42
/
g4sbs.cc
363 lines (291 loc) · 10.5 KB
/
g4sbs.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "TString.h" // These need to come first or the
#include "TBuffer.h" // compiler bitches about shadowed variables
#include "TMatrixTBase.h"
#include "THashTable.h"
#include "CLHEP/Random/Random.h"
#include "G4SBSRunAction.hh"
#include "G4SBSPrimaryGeneratorAction.hh"
#include "G4SBSEventAction.hh"
#include "G4SBSSteppingAction.hh"
#include "G4SBSTrackingAction.hh"
#include "G4SBSRun.hh"
#include "G4SBSRunData.hh"
#include "G4UIcommandStatus.hh"
//------------
// Geometries:
//------------
#include "G4SBSDetectorConstruction.hh"
#include "G4SBSIO.hh"
#include "G4SBSMessenger.hh"
//-----------------------------------
// G4SBSPhysicsList (makes use of the
// G4ParameterisationManagerProcess):
//-----------------------------------
#include "G4SBSPhysicsList.hh"
#include "G4UImanager.hh"
#include "G4RunManager.hh"
#include "G4RunManagerKernel.hh"
#include "G4UnitsTable.hh"
//#ifdef G4VIS_USE
#include "G4VisExecutive.hh"
//#endif
//#ifdef G4UI_USE
#include "G4UIExecutive.hh"
//#endif
#ifdef __APPLE__
#include "unistd.h"
#endif
bool parseArgument(std::string arg, std::string &name, std::string &value)
{
size_t length = arg.length();
// There are two possible types of parameters.
// 1) Parameters with a value (--name=value)
// 2) Parameters with no value (--name), also known as flags.
// Minimum size for the argument is 3, two dashes and at least one
// other character
if(length<3)
return false; // Not long enough to be a valid parameter
// Now, look for the double dash at the beginning that will signal
// that a parameter is being specified
size_t pos1 = arg.find_first_of("--");
if(pos1 != std::string::npos && pos1 == 0) { // Double dash found
// First, identify if it is parameter type 1 by looking for the equal sign
size_t pos2 = arg.find_first_of("=");
if(pos2 != std::string::npos && pos2 < length-1 && pos2>0) { // Param type 1
// Split name from value using on both sides of the equal sign
name = arg.substr(2,pos2-2);
value = arg.substr(pos2+1,length);
return true;
} else { // No equal sign means parameter type 2
name = arg.substr(2,length);
return true;
}
}
return false;
}
G4int executeMacro(G4String macro, G4UImanager *UImanager)
{
if(macro.length()>0) {
G4String command = "/control/execute ";
//G4UIcommandStatus success = fCommandSucceeded;
return UImanager->ApplyCommand(command+macro);
//return success;
} else {
return fCommandSucceeded;
}
}
bool getBool(std::string value, bool default_value)
{
if(value.compare("true") == 0) {
return true;
} else if(value.compare("false") == 0) {
return false;
} else {
return default_value;
}
}
int main(int argc, char** argv)
{
//Allow user to apply some (perhaps all?) commands in the pre-initialization phase:
//-------------------------------
// Initialize the CLHEP random engine used by
// "shoot" type functions
//-------------------------------
G4bool batch_mode = false; //Run in interactive mode by default
G4String preinit_macro = "";
G4String postinit_macro = "";
bool flag_gui = false; // Display GUI flag
//-------------------------------
// Parse command line arguments.
//
// In order to maintain backwards compatibility with the old method
// let's keep track of a flag. If any of the new paramethers are found
// we will require the new way. If none are found, we'll proceed with
// the old method.
//-------------------------------
bool paramsFound = false;
for(int i = 1; i < argc; i++) {
std::string paramName;
std::string paramValue;
bool validParam = parseArgument(argv[i],paramName,paramValue);
if(validParam) {
paramsFound = true;
if(paramName.compare("pre") == 0) {
preinit_macro = paramValue;
} else if (paramName.compare("post") == 0) {
postinit_macro = paramValue;
} else if (paramName.compare("gui") == 0) {
flag_gui = getBool(paramValue,true);
}
}
}
// If no valid parameters were found, revert to the old method, in which
// there are two possible parameters to specify the pre-init macro and
// post init-macro.
if(!paramsFound) {
if( argc == 2 ){ //if only one command line argument is given, assume all commands are to be applied post-initialization:
postinit_macro = argv[1];
} else if( argc > 2 ){ //assume first argument is pre-init commands, second argument is post-init commands:
preinit_macro = argv[1];
postinit_macro = argv[2];
}
}
bool use_gui = false;
// If no postinit macro specified, turn on the GUI
if(postinit_macro == "") {
use_gui = true;
}
// If the GUI flag/parameter is passed, always display GUI
if(flag_gui) {
use_gui = true;
}
CLHEP::HepRandom::createInstance();
unsigned int seed = time(0) + (int) getpid();
unsigned int devrandseed = 0;
FILE *fdrand = fopen("/dev/urandom", "r");
if( fdrand ){
fread(&devrandseed, sizeof(int), 1, fdrand);
seed += devrandseed;
fclose(fdrand);
}
CLHEP::HepRandom::setTheSeed(seed);
G4SBSRunData *rundata = G4SBSRun::GetRun()->GetData();
rundata->SetSeed(seed);
G4SBSIO *io = new G4SBSIO();
//-------------------------------
// Initialization of Run manager
//-------------------------------
G4cout << "RunManager construction starting...." << G4endl;
G4RunManager * runManager = new G4RunManager;
G4SBSMessenger *sbsmess = new G4SBSMessenger();
sbsmess->SetIO(io);
G4VModularPhysicsList *physicslist = new G4SBSPhysicsList;
runManager->SetUserInitialization(physicslist);
sbsmess->SetPhysList( (G4SBSPhysicsList*) physicslist );
// Detector/mass geometry and parallel geometry(ies):
G4SBSDetectorConstruction* detector = new G4SBSDetectorConstruction();
sbsmess->SetDetCon((G4SBSDetectorConstruction *) detector);
// This lets us output graphical field maps
io->SetGlobalField( ((G4SBSDetectorConstruction *) detector)->GetGlobalField() );
runManager->SetUserInitialization(detector);
//-------------------------------
// UserAction classes
//-------------------------------
G4UserRunAction* run_action = new G4SBSRunAction;
((G4SBSRunAction *) run_action)->SetIO(io);
runManager->SetUserAction(run_action);
//
G4VUserPrimaryGeneratorAction* gen_action = new G4SBSPrimaryGeneratorAction;
((G4SBSPrimaryGeneratorAction *) gen_action)->SetIO(io);
sbsmess->SetEvGen(((G4SBSPrimaryGeneratorAction *) gen_action)->GetEvGen());
sbsmess->SetPriGen((G4SBSPrimaryGeneratorAction *) gen_action);
( (G4SBSPrimaryGeneratorAction*) gen_action )->SetRunAction( (G4SBSRunAction*) run_action );
runManager->SetUserAction(gen_action);
//
G4UserEventAction* event_action = new G4SBSEventAction;
((G4SBSEventAction *) event_action)->SetIO(io);
((G4SBSEventAction *) event_action)->SetEvGen(((G4SBSPrimaryGeneratorAction *) gen_action)->GetEvGen());
runManager->SetUserAction(event_action);
sbsmess->SetEvAct((G4SBSEventAction *) event_action);
//
G4SBSSteppingAction* stepping_action = new G4SBSSteppingAction;
runManager->SetUserAction(stepping_action);
G4SBSTrackingAction *tracking_action = new G4SBSTrackingAction;
runManager->SetUserAction(tracking_action);
//Store pointer to tracking action in SBS UI messenger:
sbsmess->SetTrackingAction(tracking_action);
sbsmess->SetSteppingAction(stepping_action);
((G4SBSRunAction *) run_action)->SetTrackingAction(tracking_action);
((G4SBSRunAction *) run_action)->SetSteppingAction(stepping_action);
G4UImanager * UImanager = G4UImanager::GetUIpointer();
//Set GEANT4 macro search path based on environment variable G4SBS, if it is set:
G4String macropathcmd = "/control/macroPath ";
//
macropathcmd += ".:./scripts";
char *G4SBS_env_var = std::getenv("G4SBS");
if( G4SBS_env_var != NULL ){
macropathcmd += ":";
macropathcmd += G4SBS_env_var;
macropathcmd += "/scripts";
}
//Let GEANT4 search ./scripts and $G4SBS/scripts for macros:
UImanager->ApplyCommand( macropathcmd );
//#endif
// This line helps G4SBSRunData locate the pre-init, post-init macro files and
// all external macros called (if ANY), without forcing libg4sbsroot to depend
// on the GEANT4 libraries
rundata->SetMacroPath( UImanager->GetMacroSearchPath() );
if( preinit_macro != "" ){
rundata->SetPreInitMacroFile(preinit_macro.data());
G4int success = executeMacro(preinit_macro,UImanager);
if( success != fCommandSucceeded ){
G4cerr << "Problem executing macro, exiting..." << G4endl;
exit(-1);
}
}
if( postinit_macro != "") {
rundata->SetMacroFile(postinit_macro.data());
}
// Initialize Run manager
runManager->Initialize();
/*
UImanager->ApplyCommand("/Step/Verbose 0");
UImanager->ApplyCommand("/tracking/Verbose 1");
UImanager->ApplyCommand("/gun/particle e-");
UImanager->ApplyCommand("/gun/energy 100 MeV");
UImanager->ApplyCommand("/gun/direction 0 0 1");
UImanager->ApplyCommand("/gun/position 0 0 0");
UImanager->ApplyCommand("/gun/direction 0 .3 1.");
*/
if( use_gui ) {
//--------------------------
// Define (G)UI
//--------------------------
//----------------
// Visualization:
//----------------
//#ifdef G4VIS_USE
//#endif
// Setup commands
//
//----------------
// Visualization:
//----------------
//#ifdef G4VIS_USE
G4cout << "Instantiating Visualization Manager......." << G4endl;
G4VisManager* visManager = new G4VisExecutive;
visManager -> Initialize ();
//#endif
// Setup commands
//
//#ifdef G4UI_USE
G4UIExecutive * ui = new G4UIExecutive(argc,argv);
UImanager->SetSession( ui->GetSession() );
//#endif
G4int success = executeMacro(postinit_macro, UImanager);
if( success != fCommandSucceeded ){
G4cerr << "Problem executing macro, exiting..." << G4endl;
exit(-1);
}
//#ifdef G4UI_USE
ui->SessionStart();
delete ui;
delete visManager;
//#endif
} else {
// Run the postinit macro if one is specified
G4int success = executeMacro(postinit_macro, UImanager);
if( success != fCommandSucceeded ){
G4cerr << "Problem executing macro, exiting..." << G4endl;
exit(-1);
}
}
// Free the store: user actions, physics_list and detector_description are
// owned and deleted by the run manager, so they should not
// be deleted in the main() program !
//#ifdef G4VIS_USE
// delete visManager;
//#endif
delete runManager;
return 0;
}