Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-23 09:21:50

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(0);
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(0);
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     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   // Set mandatory initialization classes
0101   runManager->SetUserInitialization(new PhysicsList());
0102   runManager->SetUserInitialization(new DetectorConstruction());
0103   runManager->SetUserInitialization(new ActionInitialization());
0104 
0105   // Initialize G4 kernel
0106   runManager->Initialize();
0107 
0108   // Initialize visualization
0109   G4VisManager* visManager = new G4VisExecutive();
0110   // G4VisExecutive can take a verbosity argument - see /vis/verbose guidance.
0111   // G4VisManager* visManager = new G4VisExecutive("Quiet");
0112   visManager->Initialize();
0113 
0114   // Get the pointer to the User Interface manager
0115   G4UImanager* UImanager = G4UImanager::GetUIpointer();
0116   G4UIExecutive* ui(0);
0117 
0118   // interactive mode : define UI session
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       // visualization is used by default
0126       if ((commandLine = parser->GetCommandIfActive("-vis"))) {
0127         // select a visualization driver if needed (e.g. HepFile)
0128         UImanager->ApplyCommand(G4String("/vis/open ") + commandLine->GetOption());
0129       }
0130       else {
0131         // by default OGL is used
0132         UImanager->ApplyCommand("/vis/open OGL 800x600-0+0");
0133       }
0134       UImanager->ApplyCommand("/control/execute vis.mac");
0135     }
0136   }
0137   else {
0138     // to be use visualization file (= store the visualization into
0139     // an external file:
0140     // ASCIITree ;  DAWNFILE ; HepRepFile ; VRML(1,2)FILE ; gMocrenFile ...
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   // Job termination
0161   // Free the store: user actions, physics_list and detector_description are
0162   // owned and deleted by the run manager, so they should not be deleted
0163   // in the main() program !
0164   delete visManager;
0165   delete runManager;
0166   CommandLineParser::DeleteInstance();
0167   return 0;
0168 }
0169 
0170 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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)  // If no seed given in argument, setup the seed
0194   {
0195     long jobID_int = 0;
0196     long noice = 0;
0197 
0198     //____________________________________
0199     // In case on cluster
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     }  // end cluster
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   // Choose the Random engine
0227   // CLHEP::HepRandom::setTheEngine(new CLHEP::RanecuEngine);
0228 }
0229 
0230 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0231 
0232 unsigned int noise()
0233 {
0234 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
0235   // TODO: MS Win
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   }  // end if
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0259 
0260 void Parse(int& argc, char** argv)
0261 {
0262   //////////
0263   // Parse options given in commandLine
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   // If -h or --help is given in option : print help and exit
0294   //
0295   if (parser->Parse(argc, argv) != 0)  // help is being printed
0296   {
0297     // if you are using ROOT, create a TApplication in this condition in order
0298     // to print the help from ROOT as well
0299     CommandLineParser::DeleteInstance();
0300     std::exit(0);
0301   }
0302 
0303   ///////////
0304   // Kill application if wrong argument in command line
0305   //
0306   if (parser->CheckIfNotHandledOptionsExists(argc, argv)) {
0307     // if you are using ROOT, you should initialise your TApplication
0308     // before this condition
0309     std::exit(0);
0310   }
0311 }