-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMiniScatter.cc
1251 lines (1088 loc) · 59.7 KB
/
MiniScatter.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* MiniScatter.cc:
* Helga Holmestad 2015-2019
* Kyrre Sjobak 2015-
* Eric Fackelman 2022-
*
* This file is part of MiniScatter.
*
* MiniScatter is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MiniScatter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MiniScatter. If not, see <https://www.gnu.org/licenses/>.
*/
#include "G4RunManager.hh"
#include "G4UImanager.hh"
#include "DetectorConstruction.hh"
#include "MagnetSensorWorldConstruction.hh"
#include "VirtualTrackerWorldConstruction.hh"
#include "PrimaryGeneratorAction.hh"
#include "RunAction.hh"
#include "EventAction.hh"
#include "G4PhysListFactory.hh"
#include "G4ParallelWorldPhysics.hh"
#include "EMonlyPhysicsList.hh"
#include "G4Version.hh"
#include "G4Exception.hh"
#if G4VERSION_NUMBER >= 1060
//These macros were removed in version 10.6;
// from what I can see it looks like they are are no longer needed
#define G4VIS_USE
#define G4UI_USE
#endif
#include "MiniScatterVersion.hh"
#include "RootFileWriter.hh"
#include "G4SystemOfUnits.hh"
#include "G4String.hh"
#include <string> //C++11 std::stoi
#include "TROOT.h"
#ifdef G4VIS_USE
#include "G4VisExecutive.hh"
#endif
#ifdef G4UI_USE
#include "G4UIExecutive.hh"
#endif
//#include <unistd.h> //getopt()
#include <getopt.h> // Long options to getopt (GNU extension)
void printHelp(G4double target_thick,
G4String target_material,
G4String background_material,
std::vector<G4double>* detector_distances,
G4double detector_angle,
G4double target_angle,
G4double world_size,
G4String physListName,
G4double physCutoffDist,
G4int numEvents,
G4double beam_energy,
G4double beam_eFlat_min,
G4double beam_eFlat_max,
G4String beam_type,
G4double beam_offset,
G4double beam_zpos,
G4double beam_angle,
G4String covarianceString,
G4double beam_rCut,
G4bool doBacktrack,
G4String beam_loadfile,
G4int rngSeed,
G4String filename_out,
G4String foldername_out,
G4bool quickmode,
G4bool anaScatterTest,
G4bool miniROOTfile,
G4double cutoff_energyFraction,
G4double cutoff_radius,
G4double edep_dens_dz,
G4int engNbins,
G4double histPosLim,
G4double histAngLim,
std::vector<G4String> &magnetDefinitions);
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
int main(int argc,char** argv) {
// Avoid problem (segfault)
// "Error in <UnknownClass::InitInterpreter()>: LLVM SYMBOLS ARE EXPOSED TO CLING! This will cause problems; please hide them or dlopen() them after the call to TROOT::InitInterpreter()!"
// when running with GUI.
gROOT->Reset();
G4cout << "**********************************************************" << G4endl
<< "**********************************************************" << G4endl
<< G4endl;
G4cout << " Welcome to MiniScatter, release version " << miniscatter_version << "!" << G4endl
<< " Release dated: " << miniscatter_date << G4endl
<< G4endl;
G4cout << " MiniScatter is controlled with command line arguments." << G4endl
<< " Run MiniScatter --help to see all possible arguments,"
<< " a description of each of them, and their default/current values." << G4endl
<< G4endl;
G4cout << " If publishing results obtained with this code, " << G4endl
<< " please cite K.Sjobak and H.Holmestad, 'MINISCATTER, A SIMPLE GEANT4 WRAPPER'," << G4endl
<< " https://dx.doi.org/10.18429/JACoW-IPAC2019-WEPTS025" << G4endl
<< G4endl;
G4cout << "**********************************************************" << G4endl
<< "**********************************************************" << G4endl
<< G4endl;
//Parse command line arguments
int getopt_char;
int getopt_idx;
G4double target_thick = 1.0; // Target thickness [mm]; 0.0 for no target slab (only magnets)
G4String target_material = "G4_Al"; // Name of target material to use
G4double target_angle = 0.0; // Target angle around y-axis [deg]
G4String background_material = "G4_Galactic"; // Background material
std::vector<G4double> detector_distances; // Detector distances at x=y=0 [mm]
detector_distances.push_back(50.0);
G4double detector_angle = 0.0; // Detectector angle around y-axis [deg]
G4double world_size = 500.0; // World full size X/Y [mm]
// (not half-size as taken as argument by G4Box etc.)
G4double beam_energy = 200; // Beam kinetic energy [MeV]
G4double beam_eFlat_min = -1.0; // For flat-spectrum energy distribution,
G4double beam_eFlat_max = -1.0; // set these to min/max, both > 0.0.
G4String beam_type = "e-"; // Beam particle type
G4double beam_offset = 0.0; // Beam offset (x) [mm]
G4double beam_zpos = 0.0; // Beam offset (z) [mm]
G4bool doBacktrack = false; // Backtrack to the z-position?
G4double beam_angle = 0.0; // Beam angle [deg]
G4String covarianceString = ""; // Beam covariance matrix parameters
G4double beam_rCut = 0.0; // Beam distribution radial cutoff
G4String beam_loadFile = ""; // Filename to load beam distribution from
G4String physListName = "QGSP_FTFP_BERT"; // Name of physics list to use
G4double physCutoffDist = 0.1; // Default physics cutoff distance [mm]
G4int numEvents = 0; // Number of events to generate
G4bool useGUI = false; // GUI on/off
G4bool quickmode = false; // Don't make slow plots
G4bool anaScatterTest = false; // Compute analytical multiple scattering,
// for comparison with simulation output.
G4String filename_out = "output"; // Output filename
G4String foldername_out = "plots"; // Output foldername
G4bool miniROOTfile = false; // Write small root file
// (only analysis output, no TTrees)
G4int rngSeed = 0; // RNG seed
G4double cutoff_energyFraction = 0.95; // [fraction]
G4double cutoff_radius = world_size; // [mm]
G4double edep_dens_dz = 0.0; // Z bin width for energy deposit histograms [mm]
G4int engNbins = 0; // Number of bins for the 1D energy histograms
G4double histPosLim = 10.0; // Position histogram Limit, default 10
G4double histAngLim = 5.0; // Angle histogram Limit, default 5
std::vector<G4String> magnetDefinitions;
static struct option long_options[] = {
{"thick", required_argument, NULL, 't' },
{"mat", required_argument, NULL, 'm' },
{"backgroundMaterial", required_argument, NULL, 1600 },
{"dist", required_argument, NULL, 'd' },
{"ang", required_argument, NULL, 'a' },
{"targetAngle", required_argument, NULL, 'A' },
{"worldsize", required_argument, NULL, 'w' },
{"ang", required_argument, NULL, 'a' },
{"phys", required_argument, NULL, 'p' },
{"physCutoffDist", required_argument, NULL, 1400 },
{"numEvents", required_argument, NULL, 'n' },
{"energy", required_argument, NULL, 'e' },
{"energyDistFlat", required_argument, NULL, 1300 },
{"beam", required_argument, NULL, 'b' },
{"xoffset", required_argument, NULL, 'x' },
{"zoffset", required_argument, NULL, 'z' },
{"beamAngle", required_argument, NULL, 1500 },
{"covar", required_argument, NULL, 'c' },
{"beamRcut", required_argument, NULL, 1200 },
{"beamFile", required_argument, NULL, 1700 },
{"outname", required_argument, NULL, 'f' },
{"outfolder", required_argument, NULL, 'o' },
{"seed", required_argument, NULL, 's' },
{"help", no_argument, NULL, 'h' },
{"gui", no_argument, NULL, 'g' },
{"quickmode", no_argument, NULL, 'q' },
{"anaScatterTest", no_argument, NULL, 1004 },
{"miniroot", no_argument, NULL, 'r' },
{"cutoffEnergyFraction", required_argument, NULL, 1000 },
{"cutoffRadius", required_argument, NULL, 1001 },
{"edepDZ", required_argument, NULL, 1002 },
{"engNbins", required_argument, NULL, 1003 },
{"histPosLim", required_argument, NULL, 1005 },
{"histAngLim", required_argument, NULL, 1006 },
{"magnet", required_argument, NULL, 1100 },
{"object", required_argument, NULL, 1100 }, //synonymous with --magnet
{0,0,0,0}
};
while ( (getopt_char = getopt_long(argc,argv, "t:m:d:a:A:w:p:n:e:b:x:z:c:f:o:s:hgqr", long_options, &getopt_idx)) != -1) {
switch(getopt_char) {
case 'h': //--help ; Print help
printHelp(target_thick,
target_material,
background_material,
&detector_distances,
detector_angle,
target_angle,
world_size,
physListName,
physCutoffDist,
numEvents,
beam_energy,
beam_eFlat_min,
beam_eFlat_max,
beam_type,
beam_offset,
beam_zpos,
beam_angle,
covarianceString,
beam_rCut,
doBacktrack,
beam_loadFile,
rngSeed,
filename_out,
foldername_out,
quickmode,
anaScatterTest,
miniROOTfile,
cutoff_energyFraction,
cutoff_radius,
edep_dens_dz,
engNbins,
histPosLim,
histAngLim,
magnetDefinitions);
exit(1);
break;
case 'g': //--gui ; Use the GUI
useGUI = true;
break;
case 't': //--thick ; Target thickness
try {
target_thick = std::stod(string(optarg));
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading target thickness" << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected a floating point number! (exponential notation is accepted)" << G4endl;
exit(1);
}
break;
case 'd': //--dist ; Detector distance(s)
{ //Scope to avoid spilling temp variables
detector_distances.clear();
std::string detectorString = std::string(optarg);
if (detectorString == "NONE") break; //Just clear it
//Split by :
size_t startPos = 0;
size_t endPos = 0;
do {
endPos = detectorString.find(":",startPos);
std::string detStr = detectorString.substr(startPos,endPos-startPos);
G4double dist;
try {
dist = std::stod(detStr);
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading detector distance" << G4endl
<< "Got: '" << detStr << "'" << G4endl
<< "Expected a floating point number! (exponential notation is accepted)" << G4endl;
exit(1);
}
detector_distances.push_back(dist);
startPos = endPos+1;
} while (endPos != std::string::npos);
}
break;
case 'a': //--angle ; Detector angle
try {
detector_angle = std::stod(string(optarg));
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading detector angle" << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected a floating point number! (exponential notation is accepted)" << G4endl;
exit(1);
}
break;
case 'A': //--targetAngle ; Target angle
try {
target_angle = std::stod(string(optarg));
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading target angle" << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected a floating point number! (exponential notation is accepted)" << G4endl;
exit(1);
}
break;
case 'w': //--worldsize ; World size
try {
world_size = std::stod(string(optarg));
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading world size" << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected a floating point number! (exponential notation is accepted)" << G4endl;
exit(1);
}
break;
case 'p': //--phys ; Named physics list
physListName = G4String(optarg);
break;
case 1400: //--physCutoffDist ; Physics cutoff distance (--physCutoffDist)
try {
physCutoffDist = std::stod(string(optarg));
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading physCutoffDist" << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected a floating point number! (exponential notation is accepted)" << G4endl;
exit(1);
}
break;
case 'm': //--mat ; Target material
target_material = G4String(optarg);
break;
case 1600: //--backgroundMaterial ; Background material
background_material = G4String(optarg);
break;
case 'n': //--numEvents ; Number of events
try {
numEvents = std::stoi(string(optarg));
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading number of events" << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected an integer!" << G4endl;
exit(1);
}
break;
case 'e': //--energy ; The (reference) beam kinetic energy
try {
beam_energy = std::stod(string(optarg));
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading beam kinetic energy" << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected a floating point number! (exponential notation is accepted)" << G4endl;
exit(1);
}
break;
case 1300: {//--energyDistFlat ; beam energy (flat distribution)
std::string edist_str = std::string(optarg);
size_t startPos = 0;
size_t endPos = edist_str.find(':',startPos);
if (endPos == std::string::npos) {
G4cout << " Error while searching for ':' in edist_str = '"
<< edist_str << "', did not find?" << G4endl;
exit(1);
}
try {
beam_eFlat_min = std::stod(string(edist_str.substr(0,endPos)));
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading minimum energy '" << edist_str.substr(0,endPos) << "' for eFlat." << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected a floating point number! (exponential notation is accepted)" << G4endl;
exit(1);
}
startPos = endPos+1;
endPos = edist_str.find(":",startPos);
if (endPos != std::string::npos) {
G4cout << " Error while searching for ':' in edist_str = "
<< edist_str << "', found a second one?" << G4endl;
exit(1);
}
try {
beam_eFlat_max = std::stod(string(edist_str.substr(startPos,endPos-startPos)));
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading maximum energy '" << edist_str.substr(startPos,endPos-startPos) << "' for eFlat." << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected a floating point number! (exponential notation is accepted)" << G4endl;
exit(1);
}
if (beam_eFlat_min < 0 or beam_eFlat_max <= 0 or beam_eFlat_min >= beam_eFlat_max) {
G4cout << "Invalid eFlat_min/_max" << G4endl;
exit(1);
}
}
break;
case 'b': //--beam ; Beam particle type
beam_type = G4String(optarg);
break;
case 'x': //--xoffset / Beam offset (x)
try {
beam_offset = std::stod(string(optarg));
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading beam offset (x)" << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected a floating point number! (exponential notation is accepted)" << G4endl;
exit(1);
}
break;
case 'z': //--zoffset ; Beam offset (z)
if (strlen(optarg) > 0 && optarg[0] == '*') {
doBacktrack = true;
}
try {
if (doBacktrack) {
beam_zpos = std::stod(string(optarg).substr(1));
}
else {
beam_zpos = std::stod(string(optarg));
}
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading beam offset (z)" << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected a floating point number! (exponential notation and prepended '*' is accepted)" << G4endl;
exit(1);
}
break;
case 1500: //--beamAngle ; Initial angle of beam particles
try {
beam_angle = std::stod(string(optarg));
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading beamAngle" << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected a floating point number! (exponential notation is accepted)" << G4endl;
exit(1);
}
break;
case 'c': //--covar ; Beam covariance matrix from Twiss parameters
//-c epsN[um]:beta[m]:alpha(::epsN_Y[um]:betaY[m]:alphaY)
covarianceString = G4String(optarg);
break;
case 1200: //--beamRcut ; Beam radial cutoff [mm]
try {
beam_rCut = std::stod(string(optarg));
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading beam_rCut" << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected a floating point number! (exponential notation is accepted)" << G4endl;
exit(1);
}
break;
case 1700: //--beamFile ; Input filename to load beam from
beam_loadFile = G4String(optarg);
break;
case 'f': //--outname ; Output filename
filename_out = G4String(optarg);
break;
case 'o': //--outname ; Output foldername
foldername_out = G4String(optarg);
break;
case 'r': //--miniroot ; Mini ROOT file
miniROOTfile = true;
break;
case 's': //--seed ; RNG seed
try {
rngSeed = std::stoi(string(optarg));
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading rngSeed" << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected an integer!" << G4endl;
exit(1);
}
break;
case 'q': //--quickmode ; Quick mode (skip most plots)
quickmode = true;
break;
case 1004: //--anaScatterTest ; Make analytical plots for scattering
anaScatterTest = true;
break;
case 1000: //--cutoffEnergyFraction ; Cutoff energy fraction
try {
cutoff_energyFraction = std::stod(string(optarg));
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading cutoff_energyFraction" << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected a floating point number! (exponential notation is accepted)" << G4endl;
exit(1);
}
if (cutoff_energyFraction > 1.0 || cutoff_energyFraction < 0) {
G4ExceptionDescription errormessage;
errormessage << "Expected an energy cut off fraction between 0.0 and 1.0, but " << cutoff_energyFraction << " was given!";
G4Exception("MiniScatter.cc Flag Check: --cutoff_energyFraction","MSFlagCheck1000",FatalException,errormessage);
}
break;
case 1001: //--cutoffRadius ; Cutoff radius [mm]
try {
cutoff_radius = std::stod(string(optarg));
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading cutoff_radius" << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected a floating point number! (exponential notation is accepted)" << G4endl;
exit(1);
}
break;
case 1002: //--edepDZ ; Z bin width for energy deposit histograms [mm]
try {
edep_dens_dz = std::stod(string(optarg));
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading edep_dens_dz" << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected a floating point number! (exponential notation is accepted)" << G4endl;
exit(1);
}
break;
case 1003: //--engNbins ; Number of bins for the energy 1D histograms
if (engNbins != 0) {
G4cout << "Can only set engNbins once." << G4endl;
}
try {
engNbins = std::stoi(string(optarg));
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading engNbins" << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected an integer!" << G4endl;
exit(1);
}
if (engNbins < 0){
G4cout << "engNbins must be >= 0" << G4endl;
}
break;
case 1005: //--histPosLim ; Position Histogram Limit change
try {
histPosLim = std::stod(string(optarg));
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading histPosLim" << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected a floating point number!" << G4endl;
exit(1);
}
break;
case 1006: //--histAngLim ; Angle Histogram Limit change
try {
histAngLim = std::stod(string(optarg));
}
catch (const std::invalid_argument& ia) {
G4cout << "Invalid argument when reading histAngLim" << G4endl
<< "Got: '" << optarg << "'" << G4endl
<< "Expected a floating point number!" << G4endl;
exit(1);
}
break;
case 1100: //--object/--magnet ; Definition of an Object, also known as a Magnet
magnetDefinitions.push_back(string(optarg));
break;
default: // WTF?
G4cout << "Got an unknown getopt_char '" << char(getopt_char) << "' ("<< getopt_char<<")"
<< " when parsing command line arguments." << G4endl;
exit(1);
}
}
//Copy remaining arguments to array that is passed to Geant4
int argc_effective = argc-optind+1;
char** argv_effective = new char*[argc_effective];
argv_effective[0] = argv[0]; //First arg is always executable name
for (int i = optind; i < argc;i++){
argv_effective[i+1-optind] = argv[i];
}
//Print the gotten/default arguments
printHelp(target_thick,
target_material,
background_material,
&detector_distances,
detector_angle,
target_angle,
world_size,
physListName,
physCutoffDist,
numEvents,
beam_energy,
beam_eFlat_min,
beam_eFlat_max,
beam_type,
beam_offset,
beam_zpos,
beam_angle,
covarianceString,
beam_rCut,
doBacktrack,
beam_loadFile,
rngSeed,
filename_out,
foldername_out,
quickmode,
anaScatterTest,
miniROOTfile,
cutoff_energyFraction,
cutoff_radius,
edep_dens_dz,
engNbins,
histPosLim,
histAngLim,
magnetDefinitions);
G4cout << "Status of other arguments:" << G4endl
<< "numEvents = " << numEvents << G4endl
<< "useGUI = " << (useGUI==true ? "yes" : "no") << G4endl;
if (covarianceString != "") {
G4cout << "Covariance string = '" << covarianceString << "'" << G4endl;
}
G4cout << "Arguments which are passed on to Geant4:" << G4endl;
for (int i = 0; i < argc_effective; i++) {
G4cout << i << " '" << argv_effective[i] << "'" << G4endl;
}
G4cout << G4endl;
G4cout << "Starting Geant4..." << G4endl << G4endl;
G4RunManager* runManager = new G4RunManager;
//Set the initial seed
if (rngSeed == 0) {
G4Random::setTheSeed(time(NULL));
}
else {
G4Random::setTheSeed(rngSeed);
}
// ** Set mandatory initialization classes **
// Physics
G4int verbose=0;
G4VModularPhysicsList* physlist = NULL;
if (physListName.substr(0,7) == "EMonly_") {
G4String EMlistName = physListName.substr(7,physListName.length()-7);
physlist = new EMonlyPhysicsList(EMlistName);
}
else {
G4PhysListFactory plFactory;
physlist = plFactory.GetReferencePhysList(physListName);
if (physlist==NULL) {
G4cerr << "Bad physics list!" << G4endl;
G4cerr << G4endl;
G4cerr << "Possiblities:" << G4endl;
const std::vector<G4String>& listnames_hadr = plFactory.AvailablePhysLists();
for (auto l : listnames_hadr) {
G4cerr << "'" << l << "'" << G4endl;
}
G4cerr << G4endl;
G4cerr << "EM options:" << G4endl;
const std::vector<G4String>& listnames_em = plFactory.AvailablePhysListsEM();
for (auto l : listnames_em) {
G4cerr << "'" << l << "'" << G4endl;
}
G4cerr << G4endl;
G4String errormessage = "Physics list '" + physListName +"' not found.";
G4Exception("MiniScatter.cc physics list selection", "MSPhysList1000", FatalException, errormessage);
}
}
physlist->SetVerboseLevel(verbose);
runManager->SetUserInitialization(physlist);
//physlist->SetDefaultCutValue( 0.00001*mm);
physlist->SetDefaultCutValue( physCutoffDist*mm);
// Geometry and detectors
G4double world_min_length_detectors =
VirtualTrackerWorldConstruction::ComputeMaxAbsZ(&detector_distances, detector_angle, world_size) * 2.0;
G4double world_min_length_beam = fabs(PrimaryGeneratorAction::GetDefaultZpos(target_thick)) * 2.0;
if (beam_zpos != 0.0) {
world_min_length_beam = fabs(beam_zpos) * 2.0;
}
G4double world_min_length = world_min_length_detectors;
if (world_min_length_beam > world_min_length) {
world_min_length = world_min_length_beam;
}
DetectorConstruction* physWorld = new DetectorConstruction(target_thick,
target_material,
target_angle,
background_material,
world_size,
world_min_length,
magnetDefinitions);
MagnetSensorWorldConstruction* magnetSensorWorld =
new MagnetSensorWorldConstruction("MagnetSensorWorld",physWorld);
physWorld->RegisterParallelWorld(magnetSensorWorld);
physlist->RegisterPhysics(new G4ParallelWorldPhysics("MagnetSensorWorld"));
VirtualTrackerWorldConstruction* virtualTrackerWorld =
new VirtualTrackerWorldConstruction("VirtualTrackerWorld",physWorld, &detector_distances, detector_angle);
physWorld->RegisterParallelWorld(virtualTrackerWorld);
physlist->RegisterPhysics(new G4ParallelWorldPhysics("VirtualTrackerWorld"));
runManager->SetUserInitialization(physWorld);
// ** Set user action classes **
PrimaryGeneratorAction* gen_action = new PrimaryGeneratorAction(physWorld,
beam_energy,
beam_type,
beam_offset,
beam_zpos,
doBacktrack,
beam_angle,
covarianceString,
beam_rCut,
rngSeed,
beam_eFlat_min,
beam_eFlat_max,
beam_loadFile,
numEvents);
runManager->SetUserAction(gen_action);
//
RunAction* run_action = new RunAction;
runManager->SetUserAction(run_action);
//
EventAction* event_action = new EventAction(run_action);
runManager->SetUserAction(event_action);
// ** Final initializations **
//Initialize G4 kernel
runManager->Initialize();
//Initialize magnetic fields
physWorld->PostInitialize();
//Configure ROOT output
RootFileWriter::GetInstance()->setFilename(filename_out);
RootFileWriter::GetInstance()->setFoldername(foldername_out);
RootFileWriter::GetInstance()->setQuickmode(quickmode);
RootFileWriter::GetInstance()->setanaScatterTest(anaScatterTest);
RootFileWriter::GetInstance()->setMiniFile(miniROOTfile);
RootFileWriter::GetInstance()->setBeamEnergyCutoff(cutoff_energyFraction);
RootFileWriter::GetInstance()->setPositionCutoffR(cutoff_radius);
RootFileWriter::GetInstance()->setEdepDensDZ(edep_dens_dz);
RootFileWriter::GetInstance()->setEngNbins(engNbins); // 0 = auto
RootFileWriter::GetInstance()->setNumEvents(numEvents); // May be 0
RootFileWriter::GetInstance()->setRNGseed(rngSeed);
RootFileWriter::GetInstance()->setHistPosLim(histPosLim);
RootFileWriter::GetInstance()->setHistAngLim(histAngLim);
#ifdef G4VIS_USE
// Initialize visualization
G4VisManager* visManager = new G4VisExecutive;
// G4VisExecutive can take a verbosity argument - see /vis/verbose guidance.
// G4VisManager* visManager = new G4VisExecutive("Quiet");
visManager->Initialize();
#endif
// Get the pointer to the User Interface manager
G4UImanager* UImanager = G4UImanager::GetUIpointer();
// ** Start the run **
if (argc_effective != 1) { // batch mode
if (useGUI) {
G4cout << "UseGUI is not compatible with batch mode!" << G4endl;
exit(1);
}
G4String command = "/control/execute ";
G4String fileName = argv_effective[1];
UImanager->ApplyCommand(command+fileName);
}
else if (useGUI == true) { // interactive mode : define UI session
#ifdef G4UI_USE
G4UIExecutive* ui = new G4UIExecutive(argc_effective,argv_effective);
#ifdef G4VIS_USE
UImanager->ApplyCommand("/control/execute vis.mac");
#endif
if (numEvents > 0) {
G4cout << G4String("'/run/beamOn ") + std::to_string(numEvents) << "'" << G4endl;
UImanager->ApplyCommand(G4String("/run/beamOn ") + std::to_string(numEvents));
}
if (ui->IsGUI())
ui->SessionStart(); //Returns when GUI is closed.
delete ui;
#else
G4cout << "ERROR in initialization: GUI is turned on (-g), but G4UI_USE is not set!" << G4endl;
exit(1);
#endif
}
//Run given number of events
if (useGUI==false and numEvents > 0) {
G4cout << G4String("'/run/beamOn ") + std::to_string(numEvents) << "'" << G4endl;
UImanager->ApplyCommand(G4String("/run/beamOn ") + std::to_string(numEvents));
}
G4cout <<"Done." << G4endl;
// ** Job termination and cleanup **
// 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
delete runManager;
G4cout << "runManager deleted" << G4endl;
//Cleanup memory
delete[] argv_effective;
argv_effective = NULL;
delete magnetSensorWorld;
magnetSensorWorld = NULL;
G4cout << "MiniScatter is finished" << G4endl;
return 0;
}
//--------------------------------------------------------------------------------
void printHelp(G4double target_thick,
G4String target_material,
G4String background_material,
std::vector<G4double>* detector_distances,
G4double detector_angle,
G4double target_angle,
G4double world_size,
G4String physListName,
G4double physCutoffDist,
G4int numEvents,
G4double beam_energy,
G4double beam_eFlat_min,
G4double beam_eFlat_max,
G4String beam_type,
G4double beam_offset,
G4double beam_zpos,
G4double beam_angle,
G4String covarianceString,
G4double beam_rCut,
G4bool doBacktrack,
G4String beam_loadFile,
G4int rngSeed,
G4String filename_out,
G4String foldername_out,
G4bool quickmode,
G4bool anaScatterTest,
G4bool miniROOTfile,
G4double cutoff_energyFraction,
G4double cutoff_radius,
G4double edep_dens_dz,
G4int engNbins,
G4double histPosLim,
G4double histAngLim,
std::vector<G4String> &magnetDefinitions) {
G4cout << "Usage/options, long and short forms:" << G4endl<<G4endl;
G4cout << " --thick/-t <double>" << G4endl
<< "\t Target thickness [mm]"
<< "\t Set thickness to 0.0 for no target (only objects)" << G4endl
<< "\t Default/current value = " << target_thick << G4endl <<G4endl;
G4cout << " --mat/-m <string>" << G4endl
<< "\t Target material name" << G4endl
<< "\t Valid choices: 'G4_Al', 'G4_Au','G4_C', 'G4_Cu', 'G4_Pb', 'G4_Ti', 'G4_Si', 'G4_W', 'G4_U', 'G4_Fe'," << G4endl
<< "\t 'G4_MYLAR', 'G4_KAPTON', 'G4_STAINLESS-STEEL', 'G4_WATER', 'G4_SODIUM_IODIDE', 'G4_POLYPROPYLENE'" << G4endl
<< "\t 'G4_Galactic', 'G4_AIR'," << G4endl
<< "\t 'Sapphire', 'ChromoxPure', 'ChromoxScreen'." << G4endl
<< "\t Also possible: 'gas::pressure' " << G4endl
<< "\t where 'gas' is 'H_2', 'He', 'N_2', 'Ne', or 'Ar'," << G4endl
<< "\t and pressure is given in mbar (T=300K is assumed)." << G4endl
<< "\t Default/current value = '"
<< target_material << "'" << G4endl << G4endl;
G4cout << " --backgroundMaterial <string>" << G4endl
<< "\t The name of the background material (world volume) to use." << G4endl
<< "\t Default/current value = '"
<< background_material << "'" << G4endl << G4endl;
G4cout << " --dist/-d <double>(:<double>:<double>:...) or NONE" << G4endl
<< "\t Detector distance(s) [mm]" << G4endl
<< "\t Default/current value = ";
if (detector_distances->size() == 0) {
G4cout << "NONE" << G4endl<<G4endl;
}
else {
for (int i = 0; i < ((int)detector_distances->size())-1; i++) {
G4cout << detector_distances->at(i) << ":";
}
G4cout << detector_distances->back() << G4endl<<G4endl;
}
G4cout << " --ang/-a <double>" << G4endl
<< "\t Detector angle [deg]" << G4endl
<< "\t Default/current value = "
<< detector_angle << G4endl << G4endl;
G4cout << " --targetAngle/-A <double>" << G4endl
<< "\t Target angle [deg]" << G4endl
<< "\t Default/current value = "
<< target_angle << G4endl << G4endl;
G4cout << " --worldsize/-w <double>" << G4endl
<< "\t World size X/Y [mm]" << G4endl
<< "\t Default/current value = "
<< world_size << G4endl << G4endl;
G4cout << " --phys/-p <string>" << G4endl
<< "\t Physics list name" << G4endl
<< "\t Run with an invalid value to see options. To disable hadronic physics," << G4endl
<< "\t use the \"base\" physics list 'EMonly_' followed by one of the usual EM options ('EMY' etc)," << G4endl