Back to home page

EIC code displayed by LXR

 
 

    


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

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 // The Geant4-DNA web site is available at http://geant4-dna.org
0032 //
0033 //
0034 /// \file chem4.cc
0035 /// \brief Chem4 example
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  * WARNING : Geant4 was initially not intended for this kind of application
0050  * This code is delivered as a prototype
0051  * We will be happy to hear from you, do not hesitate to send your feedback and
0052  * communicate on the difficulties you may encounter
0053  * The user interface may change in the next releases since a reiteration of the
0054  * code has started
0055  */
0056 
0057 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0069 
0070 int main(int argc, char** argv)
0071 {
0072   // Parse options given in commandLine
0073   Parse(argc, argv);
0074   Command* commandLine(0);
0075   SetSeed();
0076 
0077   // Construct the run manager according to whether MT is activated or not
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   // Set mandatory initialization classes
0100   runManager->SetUserInitialization(new PhysicsList());
0101   runManager->SetUserInitialization(new DetectorConstruction());
0102   runManager->SetUserInitialization(new ActionInitialization());
0103 
0104   // Initialize visualization
0105   G4VisManager* visManager = new G4VisExecutive();
0106   // G4VisExecutive can take a verbosity argument - see /vis/verbose guidance.
0107   // G4VisManager* visManager = new G4VisExecutive("Quiet");
0108   visManager->Initialize();
0109 
0110   // Get the pointer to the User Interface manager
0111   G4UImanager* UImanager = G4UImanager::GetUIpointer();
0112   G4UIExecutive* ui(0);
0113 
0114   // interactive mode : define UI session
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       // visualization is used by default
0122       if ((commandLine = parser->GetCommandIfActive("-vis"))) {
0123         // select a visualization driver if needed (e.g. HepFile)
0124         UImanager->ApplyCommand(G4String("/vis/open ") + commandLine->GetOption());
0125       }
0126       else {
0127         // by default OGL is used
0128         UImanager->ApplyCommand("/vis/open OGL 800x600-0+0");
0129       }
0130       UImanager->ApplyCommand("/control/execute vis.mac");
0131     }
0132   }
0133   else {
0134     // to be use visualization file (= store the visualization into
0135     // an external file:
0136     // ASCIITree ;  DAWNFILE ; HepRepFile ; VRML(1,2)FILE ; gMocrenFile ...
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   // Job termination
0157   // Free the store: user actions, physics_list and detector_description are
0158   // owned and deleted by the run manager, so they should not be deleted
0159   // in the main() program !
0160   delete visManager;
0161   delete runManager;
0162   CommandLineParser::DeleteInstance();
0163   return 0;
0164 }
0165 
0166 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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)  // If no seed given in argument, setup the seed
0190   {
0191     long jobID_int = 0;
0192     long noice = 0;
0193 
0194     //____________________________________
0195     // In case on cluster
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     }  // end cluster
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   // Choose the Random engine
0223   // CLHEP::HepRandom::setTheEngine(new CLHEP::RanecuEngine);
0224 }
0225 
0226 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0227 
0228 unsigned int noise()
0229 {
0230 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
0231   // TODO: MS Win
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   }  // end if
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0255 
0256 void Parse(int& argc, char** argv)
0257 {
0258   //////////
0259   // Parse options given in commandLine
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   // If -h or --help is given in option : print help and exit
0290   //
0291   if (parser->Parse(argc, argv) != 0)  // help is being printed
0292   {
0293     // if you are using ROOT, create a TApplication in this condition in order
0294     // to print the help from ROOT as well
0295     CommandLineParser::DeleteInstance();
0296     std::exit(0);
0297   }
0298 
0299   ///////////
0300   // Kill application if wrong argument in command line
0301   //
0302   if (parser->CheckIfNotHandledOptionsExists(argc, argv)) {
0303     // if you are using ROOT, you should initialise your TApplication
0304     // before this condition
0305     std::exit(0);
0306   }
0307 }