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