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