File indexing completed on 2026-04-09 07:49:12
0001 #include <string>
0002
0003 #include <argparse/argparse.hpp>
0004
0005 #include "FTFP_BERT.hh"
0006 #include "G4OpticalPhysics.hh"
0007 #include "G4VModularPhysicsList.hh"
0008
0009 #include "G4UIExecutive.hh"
0010 #include "G4UImanager.hh"
0011 #include "G4VisExecutive.hh"
0012
0013 #include "sysrap/OPTICKS_LOG.hh"
0014
0015 #include "GPURaytrace.h"
0016
0017 #include "G4RunManager.hh"
0018 #include "G4RunManagerFactory.hh"
0019 #include "G4VUserActionInitialization.hh"
0020
0021 using namespace std;
0022
0023 struct ActionInitialization : public G4VUserActionInitialization
0024 {
0025 private:
0026 G4App *fG4App;
0027
0028 public:
0029
0030 ActionInitialization(G4App *app) : G4VUserActionInitialization(), fG4App(app)
0031 {
0032 }
0033
0034 virtual void BuildForMaster() const override
0035 {
0036 SetUserAction(fG4App->run_act_);
0037 }
0038
0039 virtual void Build() const override
0040 {
0041 SetUserAction(fG4App->prim_gen_);
0042 SetUserAction(fG4App->run_act_);
0043 SetUserAction(fG4App->event_act_);
0044 SetUserAction(fG4App->tracking_);
0045 SetUserAction(fG4App->stepping_);
0046 }
0047 };
0048
0049 int main(int argc, char **argv)
0050 {
0051
0052 OPTICKS_LOG(argc, argv);
0053
0054 argparse::ArgumentParser program("GPURaytrace", "0.0.0");
0055
0056 string gdml_file, macro_name;
0057 bool interactive;
0058
0059 program.add_argument("-g", "--gdml")
0060 .help("path to GDML file")
0061 .default_value(string("geom.gdml"))
0062 .nargs(1)
0063 .store_into(gdml_file);
0064
0065 program.add_argument("-m", "--macro")
0066 .help("path to G4 macro")
0067 .default_value(string("run.mac"))
0068 .nargs(1)
0069 .store_into(macro_name);
0070
0071 program.add_argument("-i", "--interactive")
0072 .help("whether to open an interactive window with a viewer")
0073 .flag()
0074 .store_into(interactive);
0075
0076 program.add_argument("-s", "--seed").help("fixed random seed (default: time-based)").scan<'i', long>();
0077
0078 try
0079 {
0080 program.parse_args(argc, argv);
0081 }
0082 catch (const exception &err)
0083 {
0084 cerr << err.what() << endl;
0085 cerr << program;
0086 exit(EXIT_FAILURE);
0087 }
0088
0089 long seed;
0090 if (program.is_used("--seed"))
0091 {
0092 seed = program.get<long>("--seed");
0093 }
0094 else
0095 {
0096 seed = static_cast<long>(time(nullptr));
0097 }
0098 CLHEP::HepRandom::setTheSeed(seed);
0099 G4cout << "Random seed set to: " << seed << G4endl;
0100
0101
0102
0103 G4VModularPhysicsList *physics = new FTFP_BERT;
0104 physics->RegisterPhysics(new G4OpticalPhysics);
0105
0106 auto *run_mgr = G4RunManagerFactory::CreateRunManager();
0107 run_mgr->SetUserInitialization(physics);
0108
0109 G4App *g4app = new G4App(gdml_file);
0110
0111 ActionInitialization *actionInit = new ActionInitialization(g4app);
0112 run_mgr->SetUserInitialization(actionInit);
0113 run_mgr->SetUserInitialization(g4app->det_cons_);
0114
0115 G4UIExecutive *uix = nullptr;
0116 G4VisManager *vis = nullptr;
0117
0118 if (interactive)
0119 {
0120 uix = new G4UIExecutive(argc, argv);
0121 vis = new G4VisExecutive;
0122 vis->Initialize();
0123 }
0124
0125 G4UImanager *ui = G4UImanager::GetUIpointer();
0126 ui->ApplyCommand("/control/execute " + macro_name);
0127
0128 if (interactive)
0129 {
0130 uix->SessionStart();
0131 }
0132
0133 delete uix;
0134
0135 return EXIT_SUCCESS;
0136 }