File indexing completed on 2026-05-21 07:53:32
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 #include "ActionInitialization.hh"
0040 #include "CommandLineParser.hh"
0041 #include "DetectorConstruction.hh"
0042 #include "PhysicsList.hh"
0043
0044 #include "G4DNAChemistryManager.hh"
0045 #include "G4RunManagerFactory.hh"
0046 #include "G4UIExecutive.hh"
0047 #include "G4UImanager.hh"
0048 #include "G4VisExecutive.hh"
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061 using namespace G4DNAPARSER;
0062 CommandLineParser* parser(nullptr);
0063 long seed = 0;
0064
0065 unsigned int noise();
0066 void SetSeed();
0067
0068 void Parse(int& argc, char** argv);
0069
0070
0071
0072 int main(int argc, char** argv)
0073 {
0074
0075 Parse(argc, argv);
0076 Command* commandLine(nullptr);
0077 SetSeed();
0078
0079
0080
0081 auto* runManager = G4RunManagerFactory::CreateRunManager();
0082
0083 if ((commandLine = parser->GetCommandIfActive("-mt"))) {
0084 G4int nThreads;
0085 if (commandLine->GetOption() == "NMAX") {
0086 nThreads = G4Threading::G4GetNumberOfCores();
0087 G4cout<<"nThreads : "<<nThreads<<G4endl;
0088 }
0089 else {
0090 nThreads = G4UIcommand::ConvertToInt(commandLine->GetOption());
0091 }
0092
0093 runManager->SetNumberOfThreads(nThreads);
0094 }
0095
0096 G4cout << "**************************************************************"
0097 << "******\n===== Chem5 is started with " << runManager->GetNumberOfThreads() << " of "
0098 << G4Threading::G4GetNumberOfCores()
0099 << " available threads =====\n\n*************************************"
0100 << "*******************************" << G4endl;
0101
0102
0103 runManager->SetUserInitialization(new PhysicsList());
0104 runManager->SetUserInitialization(new DetectorConstruction());
0105 runManager->SetUserInitialization(new ActionInitialization());
0106
0107
0108 runManager->Initialize();
0109
0110
0111 G4VisManager* visManager = new G4VisExecutive();
0112
0113
0114 visManager->Initialize();
0115
0116
0117 G4UImanager* UImanager = G4UImanager::GetUIpointer();
0118 G4UIExecutive* ui = nullptr;
0119
0120
0121 if ((commandLine = parser->GetCommandIfActive("-gui"))) {
0122 ui = new G4UIExecutive(argc, argv, commandLine->GetOption());
0123
0124 if (ui->IsGUI()) UImanager->ApplyCommand("/control/execute gui.mac");
0125
0126 if (parser->GetCommandIfActive("-novis") == nullptr) {
0127
0128 if ((commandLine = parser->GetCommandIfActive("-vis"))) {
0129
0130 UImanager->ApplyCommand(G4String("/vis/open ") + commandLine->GetOption());
0131 }
0132 else {
0133
0134 UImanager->ApplyCommand("/vis/open OGL 800x600-0+0");
0135 }
0136 UImanager->ApplyCommand("/control/execute vis.mac");
0137 }
0138 }
0139 else {
0140
0141
0142
0143 if ((commandLine = parser->GetCommandIfActive("-vis"))) {
0144 UImanager->ApplyCommand(G4String("/vis/open ") + commandLine->GetOption());
0145 UImanager->ApplyCommand("/control/execute vis.mac");
0146 }
0147 }
0148
0149 if ((commandLine = parser->GetCommandIfActive("-mac"))) {
0150 G4String command = "/control/execute ";
0151 UImanager->ApplyCommand(command + commandLine->GetOption());
0152 }
0153 else {
0154 UImanager->ApplyCommand("/control/execute beam.in");
0155 }
0156
0157 if ((commandLine = parser->GetCommandIfActive("-gui"))) {
0158 ui->SessionStart();
0159 delete ui;
0160 }
0161
0162
0163
0164
0165
0166 delete visManager;
0167 delete runManager;
0168 CommandLineParser::DeleteInstance();
0169 return 0;
0170 }
0171
0172
0173
0174 bool IsBracket(char c)
0175 {
0176 switch (c) {
0177 case '[':
0178 case ']':
0179 return true;
0180 default:
0181 return false;
0182 }
0183 }
0184
0185
0186
0187 void SetSeed()
0188 {
0189 Command* commandLine(nullptr);
0190
0191 if ((commandLine = parser->GetCommandIfActive("-seed"))) {
0192 seed = atoi(commandLine->GetOption().c_str());
0193 }
0194
0195 if (seed == 0)
0196 {
0197 long jobID_int = 0;
0198 long noice = 0;
0199
0200
0201
0202 if ((commandLine = parser->GetCommandIfActive("-cluster"))) {
0203 noice = labs((long)noise());
0204
0205 const char* env = std::getenv("PBS_JOBID");
0206
0207 if (env) {
0208 G4String buffer(env);
0209 G4String jobID_string = buffer.substr(0, buffer.find("."));
0210 jobID_string.erase(std::remove_if(jobID_string.begin(), jobID_string.end(), &IsBracket),
0211 jobID_string.end());
0212 jobID_int = atoi(jobID_string.c_str());
0213 }
0214 else {
0215 env = std::getenv("SGE_TASK_ID");
0216 if (env) jobID_int = atoi(env);
0217 }
0218 }
0219
0220
0221 seed = ((long)time(NULL)) + jobID_int + noice;
0222 }
0223
0224 G4cout << "Seed used : " << seed << G4endl;
0225 G4Random::setTheEngine(new CLHEP::MixMaxRng());
0226 G4Random::setTheSeed(seed);
0227
0228
0229
0230 }
0231
0232
0233
0234 unsigned int noise()
0235 {
0236 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
0237
0238 return std::time(0);
0239 #else
0240 unsigned int random_seed, random_seed_a, random_seed_b;
0241 std::ifstream file("/dev/urandom", std::ios::binary);
0242 if (file.is_open()) {
0243 char* memblock;
0244 int size = sizeof(int);
0245 memblock = new char[size];
0246 file.read(memblock, size);
0247 file.close();
0248 random_seed_a = *reinterpret_cast<int*>(memblock);
0249 delete[] memblock;
0250 }
0251 else {
0252 random_seed_a = 0;
0253 }
0254 random_seed_b = std::time(nullptr);
0255 random_seed = random_seed_a xor random_seed_b;
0256 return random_seed;
0257 #endif
0258 }
0259
0260
0261
0262 void Parse(int& argc, char** argv)
0263 {
0264
0265
0266
0267 parser = CommandLineParser::GetParser();
0268
0269 parser->AddCommand("-gui", Command::OptionNotCompulsory,
0270 "Select geant4 UI or just launch a geant4 terminal"
0271 "session",
0272 "qt");
0273
0274 parser->AddCommand("-mac", Command::WithOption, "Give a mac file to execute", "macFile.mac");
0275
0276 parser->AddCommand("-seed", Command::WithOption, "Give a seed value in argument to be tested",
0277 "seed");
0278
0279 parser->AddCommand("-mt", Command::WithOption,
0280 "Launch in MT mode (events computed in parallel,"
0281 " NOT RECOMMANDED WITH CHEMISTRY)",
0282 "2");
0283
0284 parser->AddCommand("-chemOFF", Command::WithoutOption, "Deactivate chemistry");
0285
0286 parser->AddCommand("-vis", Command::WithOption, "Select a visualization driver",
0287 "OGL 600x600-0+0");
0288
0289 parser->AddCommand("-novis", Command::WithoutOption, "Deactivate visualization when using GUI");
0290
0291 parser->AddCommand("-cluster", Command::WithoutOption,
0292 "Launch the code on a cluster, avoid dupplicated seeds");
0293
0294
0295
0296
0297 if (parser->Parse(argc, argv) != 0)
0298 {
0299
0300
0301 CommandLineParser::DeleteInstance();
0302 std::exit(0);
0303 }
0304
0305
0306
0307
0308 if (parser->CheckIfNotHandledOptionsExists(argc, argv)) {
0309
0310
0311 std::exit(0);
0312 }
0313 }