This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble_chamber.cc
440 lines (378 loc) · 16 KB
/
bubble_chamber.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#include "BubbleChamberSim.h"
#include "B1DetectorConstruction.hh"
#include "B1ActionInitialization.hh"
#include "G4SystemOfUnits.hh"
#include <cstdio>
#include <iostream>
#include <unistd.h>
#include <tuple>
#include "clipp.h"
#ifdef G4MULTITHREADED
#include "G4MTRunManager.hh"
#else
#include "G4RunManager.hh"
#endif
#include "G4UImanager.hh"
#include "QBBC.hh"
#include "G4UIQt.hh"
#include "G4UIterminal.hh"
#include <qmainwindow.h>
#include "G4VisExecutive.hh"
#include "G4UIExecutive.hh"
#include "G4PhysListFactory.hh"
#include "G4StepLimiterPhysics.hh"
#include "Randomize.hh"
#include "B1ParallelWorldConstruction.hh"
#include "G4ParallelWorldPhysics.hh"
#include "GPSConfig.hh"
#include "G4Run.hh"
template<class C>
void print_help(C cli)
{
std::cout << make_man_page(cli, "bubble_chamber") << "\n";
}
//______________________________________________________________________________
//______________________________________________________________________________
bool fexists(const std::string& filename) {
std::ifstream ifile(filename.c_str());
if( ifile ) return true;
return false;
}
//______________________________________________________________________________
std::string exec(const char* cmd) {
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while (!feof(pipe.get())) {
if (fgets(buffer, 128, pipe.get()) != NULL)
result += buffer;
}
return result;
}
//______________________________________________________________________________
void copy_files(const std::vector<std::string>& files)
{
for(const auto& f: files) {
std::string cmd = std::string("cp ") + f + " .";
if( fexists(f) ) {
std::cout << "copying " << f << std::endl;
exec(cmd.c_str());
}
}
}
//______________________________________________________________________________
template<typename T>
void print_usage(T cli, const char* argv0 )
{
//used default formatting
std::cout << "Usage:\n" << usage_lines(cli, argv0)
<< "\nOptions:\n" << documentation(cli) << '\n';
}
//______________________________________________________________________________
template<typename T>
void print_man_page(T cli, const char* argv0 ){
//all formatting options (with their default values)
auto fmt = clipp::doc_formatting{}
.start_column(8) //column where usage lines and documentation starts
.doc_column(30) //parameter docstring start col
.indent_size(4) //indent of documentation lines for children of a documented group
.line_spacing(0) //number of empty lines after single documentation lines
.paragraph_spacing(1) //number of empty lines before and after paragraphs
.flag_separator(", ") //between flags of the same parameter
.param_separator(" ") //between parameters
.group_separator(" ") //between groups (in usage)
.alternative_param_separator("|") //between alternative flags
.alternative_group_separator(" | ") //between alternative groups
.surround_group("(", ")") //surround groups with these
.surround_alternatives("(", ")") //surround group of alternatives with these
.surround_alternative_flags("", "") //surround alternative flags with these
.surround_joinable("(", ")") //surround group of joinable flags with these
.surround_optional("[", "]") //surround optional parameters with these
.surround_repeat("", "..."); //surround repeatable parameters with these
//.surround_value("<", ">") //surround values with these
//.empty_label("") //used if parameter has no flags and no label
//.max_alternative_flags_in_usage(1) //max. # of flags per parameter in usage
//.max_alternative_flags_in_doc(2) //max. # of flags per parameter in detailed documentation
//.split_alternatives(true) //split usage into several lines for large alternatives
//.alternatives_min_split_size(3) //min. # of parameters for separate usage line
//.merge_alternative_flags_with_common_prefix(false) //-ab(cdxy|xy) instead of -abcdxy|-abxy
//.merge_joinable_flags_with_common_prefix(true); //-abc instead of -a -b -c
auto mp = make_man_page(cli, argv0, fmt);
mp.prepend_section("DESCRIPTION", "Bubble chamber simulation");
mp.append_section("EXAMPLES", " $ bubble_chamber -h ");
std::cout << mp << "\n";
}
//______________________________________________________________________________
//auto define_cli()
//{
// using namespace clipp;
// using std::cout;
// using CopyMode = Settings::CopyMode;
// using Mode = Settings::Mode;
// return std::move(std::make_tuple(S, cli));
//}
int main(int argc,char** argv)
{
using namespace clipp;
using std::cout;
using CopyMode = Settings::CopyMode;
using Mode = Settings::Mode;
Settings S;
GPSConfig& gps_conf = S.gps_conf;
auto cli_basics = (
( option("-r", "--run") & value("run_number",S.run_number) ) % "set the run number",
( option("-E", "--energy") & number("energy",gps_conf.energy) ) % "set beam energy [MeV]",
( option("-e", "--delta") & number("delta",S.delta_E) ) % "beam energy spread[MeV]",
(option("-V","--vertex").set(S.vertex_set) & value("x", S.vertex[0]) & value("y", S.vertex[1]) & value("z", S.vertex[2])) % "vertex position [mm]",
(option("-R","--vertexRMS").set(S.vertex_set) & value("x", S.vertex_rms[0]) & value("y", S.vertex_rms[1]) & value("z", S.vertex_rms[2])) % "vertex position RMS [mm]",
( option("-m", "--macro") & value("macro",S.macroFile).set(S.has_macro_file,true) ) % "set the run number",
option("-b", "--batch")([&](){S.use_gui=false; S.use_vis=false; S.is_interactive=false;}) % "run in batch mode (no gui or vis)",
(option("-g", "--gui") & integer("gui")([&](auto v){S.use_gui=v;})) % "use GUI ",
option("-i", "--interactive")([&](){S.is_interactive=true;}) % "run in interactivemode ",
(option("-v", "--vis") & integer("vis")([&](auto v){S.use_vis=v;})) % "use vis ",
option("-h", "--help").set(S.selected,Mode::help)
);
auto help_mode = command("help").set(S.selected, Mode::help);
auto copy_mode = (command("copy").set(S.selected, Mode::copy) % "Copy example/stock files to the current directory.",
(command("macro") % "selec macro to copy",
option("-v","--vis").set(S.copy_sel,CopyMode::vis) % "all vis macro.",
option("-v1","--vis1").set(S.copy_sel,CopyMode::v1) % "only vis.mac is copied."
"This macro visualizes the detector using the qt gui and runs 1000",
option("-v2","--vis2").set(S.copy_sel,CopyMode::v2) % "vis2 mac only")
);
auto gps_build_energy = (
"Mono energetic GPS distributated like I∝δ(E−E0) with one parameter" % (
command("Mono") % "type name" &
value("E0") % "E0 fixed energy parameter" &
( option("MeV")|option("GeV") ) % "Units [default:MeV]" )
|
"Gaussian distributed distribution with two parameters. I=(2πσ)−12exp[−(E/E0)2/σ2] Mean energy E0, std deviation σ" % (
command("Gauss") % "type name" &
value("E0") % "mean energy" &
( option("MeV")|option("GeV") ) % "Units [default:MeV]" &
value("sigma") % "sigma: std deviation " &
( option("MeV")|option("GeV") ) % "Units [default:MeV]" )
|
"Exp - exponential I∝exp(−E/E0) Energy scale-height E0" % (
command("Exp") % " typename " &
value("E0") % "E0 fixed energy parameter" &
( option("MeV")|option("GeV") ) % "Units [default:MeV]" )
|
"Lin - linear I∝I0+m×E Intercept I0 , slope m" % (
command("Lin") % "type name" &
value("I0") % "I0 intercept" &
value("m") % "slope" &
( option("MeV")|option("GeV") ) % "inverse Units [default:MeV]" )
|
"Pow - power-law I∝Eα Spectral index α" % (
command("Pow") % " typename" &
value("alpha") % "alpha parameter" &
( option("MeV")|option("GeV") ) % "Units [default:MeV]")
//command("brem") % "Brem - bremsstrahlung I=∫2E2[h2c2(exp(−E/kT)−1)]−1 Temperature T",
//command("Bbody") % "Bbody- black body I∝(kT)12Eexp(−E/kT) Temperature T",
//command("cdg") % "Cdg - cosmic diffuse gamma ray I∝[(E/Eb)α1+(E/Eb)α2]−1 Energy range Emin to Emax; Eb and indices α1 and α2 are fixed"
) ;
auto gps_mode = "gps mode:" % (
command("gps") &
"GPS energy confugration" % (
command("energy") % "Energy Dist Type. See G4 docs for details (https://tinyurl.com/ydgsc5qj)" & gps_build_energy
)
//|
//command("angle") % "Angular Dist Type " &
//"List of Angular Distributions:" % (command("iso") % "iso"| command("flat") % "flat" )
);
auto cli = ( help_mode % "print help this help"
|copy_mode % "Copy mode"
|gps_mode
|cli_basics % "typical style commands");
assert( cli.flags_are_prefix_free() );
auto result = parse(argc, argv, cli);
auto doc_label = [](const parameter& p) {
if(!p.flags().empty()) return p.flags().front();
if(!p.label().empty()) return p.label();
return doc_string{"<?>"};
};
cout << "args -> parameter mapping:\n";
;
for(const auto& m0 : result) {
std::cout << "#" << m0.index() << " " << m0.arg() << " -> ";
auto p = m0.param();
if(p) {
std::cout << doc_label(*p) << " \t";
if(m0.repeat() > 0) {
std::cout << (m0.bad_repeat() ? "[bad repeat " : "[repeat ")
<< m0.repeat() << "]";
}
if(m0.blocked()) std::cout << " [blocked]";
if(m0.conflict()) std::cout << " [conflict]";
std::cout << '\n';
}
else {
std::cout << " [unmapped]\n";
}
}
cout << "missing parameters:\n";
for(const auto& m0 : result.missing()) {
auto p = m0.param();
if(p) {
std::cout << doc_label(*p) << " \t";
std::cout << " [missing after " << m0.after_index() << "]\n";
}
}
if(!result) {
print_man_page(cli, argv[0]);
return 0;
}
switch(S.selected) {
case Mode::help :
print_man_page<decltype(cli)>(cli,argv[0]);
return 0;
break;
case Mode::copy :
if(S.copy_sel == CopyMode::vis) {
copy_files( S.copymode_file_names[S.copy_sel] );
}
return 0;
break;
case Mode::none :
break;
}
// here we assume the last argument is a macro file
if( optind < argc ) {
S.has_macro_file = true;
}
for (int i = optind; i < argc; i++) {
S.theRest += argv[i];
}
// Get the piped commands
std::vector<std::string> piped_commands;
if(!isatty(STDIN_FILENO)) {
std::cout << "Reading piped commands...\n";
std::string lineInput;
while(std::getline(std::cin,lineInput)) {
piped_commands.push_back(lineInput);
}
}
//std::cout << " the rest of the arguments: " << S.theRest << std::endl;
//std::cout << "output : " << S.output_file_name << std::endl;
//std::cout << " tree : " << S.output_tree_name << std::endl;
//---------------------------------------------------------------------------
// Detect interactive mode (if no arguments) and define UI session
// Note third argument of G4UIExecutive can be ("qt", "xm", "win32", "gag", "tcsh", "csh")
G4UIExecutive* ui = 0;
if( S.is_interactive || S.use_gui ) {
if( S.use_gui ) {
ui = new G4UIExecutive(argc, argv, "qt");
} else {
ui = new G4UIExecutive(argc, argv, "tcsh");
}
}
// Choose the Random engine
G4Random::setTheEngine(new CLHEP::RanecuEngine);
G4long seed = time(NULL);
CLHEP::HepRandom::setTheSeed(seed);
// Construct the default run manager
#ifdef G4MULTITHREADED
G4MTRunManager* runManager = new G4MTRunManager;
#else
G4RunManager* runManager = new G4RunManager;
#endif
// Set mandatory initialization classes
//
// Detector construction with parallel world
G4String paraWorldName = "ParallelWorld";
B1DetectorConstruction * realWorld = new B1DetectorConstruction();
B1ParallelWorldConstruction * parallelWorld = new B1ParallelWorldConstruction(paraWorldName);
realWorld->RegisterParallelWorld(parallelWorld);
runManager->SetUserInitialization(realWorld);
// Physics list
G4PhysListFactory factory;
auto avail_phy_lists = factory.AvailablePhysLists();
std::cout << " AvailablePhysLists\n";
for(const auto& l : avail_phy_lists) {
std::cout << l << std::endl;
}
std::cout << " AvailablePhysListsEM\n";
auto avail_phy_listsEM = factory.AvailablePhysListsEM();
for(const auto& l : avail_phy_listsEM) {
std::cout << l << std::endl;
}
//QGSP_BIC_EMY,
//QGSP_BERT_HP_PEN
//QGSP_BIC_LIV
//FTFP_BERT_HP
G4VModularPhysicsList * physicsList = factory.GetReferencePhysList("Shielding_EMZ");
// This is needed to make use of the G4UserLimits applied to logical volumes.
physicsList->RegisterPhysics(new G4StepLimiterPhysics());
// This connects the phyics to the parallel world (and sensitive detectors)
physicsList->RegisterPhysics(new G4ParallelWorldPhysics(paraWorldName,/*layered_mass=*/true));
//physicsList->ReplacePhysics(new G4IonQMDPhysics());
//physicsList->SetDefaultCutValue(0.005*um);
//G4VModularPhysicsList* physicsList = new QBBC;
physicsList->SetVerboseLevel(1);
runManager->SetUserInitialization(physicsList);
// User action initialization
auto user_action_init = new B1ActionInitialization(S);
user_action_init->SetBeamEnergy(gps_conf.energy);
runManager->SetUserInitialization(user_action_init);
// Initialize G4 kernel
runManager->Initialize();
// Initialize visualization
//
G4VisManager* visManager = new G4VisExecutive;
// G4VisExecutive can take a verbosity argument - see /vis/verbose guidance.
// G4VisManager* visManager = new G4VisExecutive("Quiet");
visManager->Initialize();
// Get the pointer to the User Interface manager
G4UImanager* UImanager = G4UImanager::GetUIpointer();
G4UIQt* qui = static_cast<G4UIQt*> (UImanager->GetG4UIWindow());
if (qui) {
qui->GetMainWindow()->setVisible(true);
}
// Process macro or start UI session
if( S.has_macro_file ) {
G4String command = "/control/execute ";
G4String fileName = S.macroFile;
UImanager->ApplyCommand(command+fileName);
} else {
// run init
// interactive mode
G4String command = "/control/macroPath ";
G4String mac_dir = BUBBLESIM_MACRO_DIR;
G4String fileName = "init_default.mac";
std::cout << " executing " << command+mac_dir << std::endl;
UImanager->ApplyCommand(command+mac_dir);
command = "/control/execute ";
if( S.use_vis ) {
std::cout << " executing " << command+fileName << std::endl;
UImanager->ApplyCommand(command+fileName);
}
}
// run the piped commands
for(auto cmd : piped_commands){
std::cout << cmd << std::endl;
UImanager->ApplyCommand(G4String(cmd));
}
// run the set number of events
if( S.number_of_events > 0) {
G4String command = "/run/beamOn " + std::to_string(S.number_of_events);
UImanager->ApplyCommand( command );
}
if( S.is_interactive ) {
ui->SessionStart();
delete ui;
}
// get number of events simulated
if (S.number_of_events < 0) {
S.number_of_events = runManager->GetCurrentRun()->GetNumberOfEvent();
}
PrintSettings(S);
// Job termination
// 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 !
delete visManager;
delete runManager;
}
//______________________________________________________________________________