Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-07 09:23:05

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // This example is provided by the Geant4-DNA collaboration
0027 // Any report or published results obtained using the Geant4-DNA software
0028 // shall cite the following Geant4-DNA collaboration publication:
0029 // Med. Phys. 37 (2010) 4692-4708
0030 // J. Comput. Phys. 274 (2014) 841-882
0031 // Phys. Med. Biol. 63(10) (2018) 105014-12pp
0032 // The Geant4-DNA web site is available at http://geant4-dna.org
0033 //
0034 //
0035 /// \file chem5.cc
0036 /// \brief Chem5 example
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  * WARNING : Geant4 was initially not intended for this kind of application
0051  * This code is delivered as a prototype
0052  * We will be happy to hear from you, do not hesitate to send your feedback and
0053  * communicate on the difficulties you may encounter
0054  * The user interface may change in the next releases since a reiteration of the
0055  * code has started
0056  */
0057 
0058 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0070 
0071 int main(int argc, char** argv)
0072 {
0073   // Parse options given in commandLine
0074   Parse(argc, argv);
0075   Command* commandLine(nullptr);
0076   SetSeed();
0077 
0078   // Construct the run manager according to whether MT is activated or not
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   // Set mandatory initialization classes
0102   runManager->SetUserInitialization(new PhysicsList());
0103   runManager->SetUserInitialization(new DetectorConstruction());
0104   runManager->SetUserInitialization(new ActionInitialization());
0105 
0106   // Initialize G4 kernel
0107   runManager->Initialize();
0108 
0109   // Initialize visualization
0110   G4VisManager* visManager = new G4VisExecutive();
0111   // G4VisExecutive can take a verbosity argument - see /vis/verbose guidance.
0112   // G4VisManager* visManager = new G4VisExecutive("Quiet");
0113   visManager->Initialize();
0114 
0115   // Get the pointer to the User Interface manager
0116   G4UImanager* UImanager = G4UImanager::GetUIpointer();
0117   G4UIExecutive* ui = nullptr;
0118 
0119   // interactive mode : define UI session
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       // visualization is used by default
0127       if ((commandLine = parser->GetCommandIfActive("-vis"))) {
0128         // select a visualization driver if needed (e.g. HepFile)
0129         UImanager->ApplyCommand(G4String("/vis/open ") + commandLine->GetOption());
0130       }
0131       else {
0132         // by default OGL is used
0133         UImanager->ApplyCommand("/vis/open OGL 800x600-0+0");
0134       }
0135       UImanager->ApplyCommand("/control/execute vis.mac");
0136     }
0137   }
0138   else {
0139     // to be use visualization file (= store the visualization into
0140     // an external file:
0141     // ASCIITree ;  DAWNFILE ; HepRepFile ; VRML(1,2)FILE ; gMocrenFile ...
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   // Job termination
0162   // Free the store: user actions, physics_list and detector_description are
0163   // owned and deleted by the run manager, so they should not be deleted
0164   // in the main() program !
0165   delete visManager;
0166   delete runManager;
0167   CommandLineParser::DeleteInstance();
0168   return 0;
0169 }
0170 
0171 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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)  // If no seed given in argument, setup the seed
0195   {
0196     long jobID_int = 0;
0197     long noice = 0;
0198 
0199     //____________________________________
0200     // In case on cluster
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     }  // end cluster
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   // Choose the Random engine
0228   // CLHEP::HepRandom::setTheEngine(new CLHEP::RanecuEngine);
0229 }
0230 
0231 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0232 
0233 unsigned int noise()
0234 {
0235 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
0236   // TODO: MS Win
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   }  // end if
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0260 
0261 void Parse(int& argc, char** argv)
0262 {
0263   //////////
0264   // Parse options given in commandLine
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   // If -h or --help is given in option : print help and exit
0295   //
0296   if (parser->Parse(argc, argv) != 0)  // help is being printed
0297   {
0298     // if you are using ROOT, create a TApplication in this condition in order
0299     // to print the help from ROOT as well
0300     CommandLineParser::DeleteInstance();
0301     std::exit(0);
0302   }
0303 
0304   ///////////
0305   // Kill application if wrong argument in command line
0306   //
0307   if (parser->CheckIfNotHandledOptionsExists(argc, argv)) {
0308     // if you are using ROOT, you should initialise your TApplication
0309     // before this condition
0310     std::exit(0);
0311   }
0312 }