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