Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-21 07:53:32

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 /// \file chem5.cc
0027 /// \brief Main program of the dna/chem5 example
0028 
0029 // This example is provided by the Geant4-DNA collaboration
0030 // Any report or published results obtained using the Geant4-DNA software
0031 // shall cite the following Geant4-DNA collaboration publication:
0032 // Med. Phys. 37 (2010) 4692-4708
0033 // J. Comput. Phys. 274 (2014) 841-882
0034 // Phys. Med. Biol. 63(10) (2018) 105014-12pp
0035 // The Geant4-DNA web site is available at http://geant4-dna.org
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  * WARNING : Geant4 was initially not intended for this kind of application
0052  * This code is delivered as a prototype
0053  * We will be happy to hear from you, do not hesitate to send your feedback and
0054  * communicate on the difficulties you may encounter
0055  * The user interface may change in the next releases since a reiteration of the
0056  * code has started
0057  */
0058 
0059 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0071 
0072 int main(int argc, char** argv)
0073 {
0074   // Parse options given in commandLine
0075   Parse(argc, argv);
0076   Command* commandLine(nullptr);
0077   SetSeed();
0078 
0079   // Construct the run manager according to whether MT is activated or not
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   // Set mandatory initialization classes
0103   runManager->SetUserInitialization(new PhysicsList());
0104   runManager->SetUserInitialization(new DetectorConstruction());
0105   runManager->SetUserInitialization(new ActionInitialization());
0106 
0107   // Initialize G4 kernel
0108   runManager->Initialize();
0109 
0110   // Initialize visualization
0111   G4VisManager* visManager = new G4VisExecutive();
0112   // G4VisExecutive can take a verbosity argument - see /vis/verbose guidance.
0113   // G4VisManager* visManager = new G4VisExecutive("Quiet");
0114   visManager->Initialize();
0115 
0116   // Get the pointer to the User Interface manager
0117   G4UImanager* UImanager = G4UImanager::GetUIpointer();
0118   G4UIExecutive* ui = nullptr;
0119 
0120   // interactive mode : define UI session
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       // visualization is used by default
0128       if ((commandLine = parser->GetCommandIfActive("-vis"))) {
0129         // select a visualization driver if needed (e.g. HepFile)
0130         UImanager->ApplyCommand(G4String("/vis/open ") + commandLine->GetOption());
0131       }
0132       else {
0133         // by default OGL is used
0134         UImanager->ApplyCommand("/vis/open OGL 800x600-0+0");
0135       }
0136       UImanager->ApplyCommand("/control/execute vis.mac");
0137     }
0138   }
0139   else {
0140     // to be use visualization file (= store the visualization into
0141     // an external file:
0142     // ASCIITree ;  DAWNFILE ; HepRepFile ; VRML(1,2)FILE ; gMocrenFile ...
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   // Job termination
0163   // Free the store: user actions, physics_list and detector_description are
0164   // owned and deleted by the run manager, so they should not be deleted
0165   // in the main() program !
0166   delete visManager;
0167   delete runManager;
0168   CommandLineParser::DeleteInstance();
0169   return 0;
0170 }
0171 
0172 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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)  // If no seed given in argument, setup the seed
0196   {
0197     long jobID_int = 0;
0198     long noice = 0;
0199 
0200     //____________________________________
0201     // In case on cluster
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     }  // end cluster
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   // Choose the Random engine
0229   // CLHEP::HepRandom::setTheEngine(new CLHEP::RanecuEngine);
0230 }
0231 
0232 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0233 
0234 unsigned int noise()
0235 {
0236 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
0237   // TODO: MS Win
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   }  // end if
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0261 
0262 void Parse(int& argc, char** argv)
0263 {
0264   //////////
0265   // Parse options given in commandLine
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   // If -h or --help is given in option : print help and exit
0296   //
0297   if (parser->Parse(argc, argv) != 0)  // help is being printed
0298   {
0299     // if you are using ROOT, create a TApplication in this condition in order
0300     // to print the help from ROOT as well
0301     CommandLineParser::DeleteInstance();
0302     std::exit(0);
0303   }
0304 
0305   ///////////
0306   // Kill application if wrong argument in command line
0307   //
0308   if (parser->CheckIfNotHandledOptionsExists(argc, argv)) {
0309     // if you are using ROOT, you should initialise your TApplication
0310     // before this condition
0311     std::exit(0);
0312   }
0313 }