Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-16 08:10:55

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 //
0027 //
0028 //
0029 // --------------------------------------------------------------
0030 //      GEANT 4 - examplePar04
0031 // --------------------------------------------------------------
0032 // Comments
0033 //
0034 // Example of a main program demonstrating inference in C++
0035 // for fast simulation in calorimeters.
0036 //
0037 //-------------------------------------------------------------------
0038 #include "FTFP_BERT.hh"  // for FTFP_BERT
0039 #include "Par04ActionInitialisation.hh"  // for Par04ActionInitialisation
0040 #include "Par04DetectorConstruction.hh"  // for Par04DetectorConstruction
0041 #include "Par04ParallelFastWorld.hh"
0042 #include "Par04ParallelFullWorld.hh"
0043 
0044 #include "G4EmParameters.hh"  // for G4EmParameters
0045 #include "G4Exception.hh"  // for G4Exception
0046 #include "G4ExceptionSeverity.hh"  // for FatalErrorInArgument
0047 #include "G4FastSimulationPhysics.hh"  // for G4FastSimulationPhysics
0048 #include "G4HadronicProcessStore.hh"  // for G4HadronicProcessStore
0049 #include "G4ParallelWorldPhysics.hh"
0050 #include "G4RunManager.hh"  // for G4RunManager
0051 #include "G4RunManagerFactory.hh"  // for G4RunManagerFactory, G4RunMa...
0052 #include "G4String.hh"  // for G4String
0053 #include "G4Types.hh"  // for G4bool, G4int
0054 #include "G4UIExecutive.hh"  // for G4UIExecutive
0055 #include "G4UImanager.hh"  // for G4UImanager
0056 #include "G4VisExecutive.hh"  // for G4VisExecutive
0057 #include "G4VisManager.hh"  // for G4VisManager
0058 #include "G4ios.hh"  // for G4cout, G4endl
0059 
0060 #include <ctime>  // for time
0061 #include <sstream>  // for char_traits, operator<<, bas...
0062 #include <string>  // for allocator, operator+, operat...
0063 
0064 int main(int argc, char** argv)
0065 {
0066   // Macro name from arguments
0067   G4String batchMacroName;
0068   G4bool useInteractiveMode = false;
0069   G4int numOfThreadsOrTasks = 8;
0070   G4int runManagerTypeInt = 0;
0071   G4RunManagerType runManagerType = G4RunManagerType::Serial;
0072   G4String helpMsg(
0073     "Usage: " + G4String(argv[0]) +
0074     " [option(s)] \n You need to specify the mode and the macro file.\nOptions:"
0075     "\n\t-h\t\tdisplay this help message\n\t-m MACRO\ttriggers a batch mode "
0076      "executing MACRO\n\t-i\t\truns interactive mode, use it together with <-m vis*mac> macros"
0077     "\n\t-r\t\trun manager type (0=serial,1=MT,2=tasking)"
0078     "\n\t-t\t\tnumber of threads for MT mode (no change for other modes)."
0079     );
0080   if (argc < 2) {
0081     G4Exception("main", "No arguments", FatalErrorInArgument,
0082                 ("No arguments passed to " + G4String(argv[0]) + "\n" + helpMsg).c_str());
0083   }
0084   for (G4int i = 1; i < argc; ++i) {
0085     G4String argument(argv[i]);
0086     if (argument == "-h" || argument == "--help") {
0087       G4cout << helpMsg << G4endl;
0088       return 0;
0089     }
0090     else if (argument == "-m") {
0091       batchMacroName = G4String(argv[i + 1]);
0092       ++i;
0093     }
0094     else if (argument == "-i") {
0095       useInteractiveMode = true;
0096     }
0097     else if (argument == "-r") {
0098       G4int tmp = atoi(argv[i + 1]);
0099       ++i;
0100       switch (tmp) {
0101         case 0:
0102           runManagerTypeInt = tmp;
0103           runManagerType = G4RunManagerType::Serial;
0104           break;
0105         case 1:
0106           runManagerTypeInt = tmp;
0107           runManagerType = G4RunManagerType::MTOnly;
0108           break;
0109         case 2:
0110           runManagerTypeInt = tmp;
0111           runManagerType = G4RunManagerType::Tasking;
0112           break;
0113         default:
0114           G4Exception("main", "Wrong Run Manager type", FatalErrorInArgument,
0115                       "Choose 0 (serial, default), 1 (MT), 2 (tasking)");
0116           break;
0117       }
0118     }
0119     else if (argument == "-t") {
0120       numOfThreadsOrTasks = atoi(argv[i + 1]);
0121       ++i;
0122     }
0123     else {
0124       G4Exception(
0125         "main", "Unknown argument", FatalErrorInArgument,
0126         ("Unknown argument passed to " + G4String(argv[0]) + " : " + argument + "\n" + helpMsg)
0127           .c_str());
0128     }
0129   }
0130 
0131   // choose the Random engine
0132   CLHEP::HepRandom::setTheEngine(new CLHEP::RanecuEngine());
0133   // set random seed with system time
0134   G4long seed = time(NULL);
0135   CLHEP::HepRandom::setTheSeed(seed);
0136 
0137   // Instantiate G4UIExecutive if interactive mode
0138   G4UIExecutive* ui = nullptr;
0139 
0140   if (useInteractiveMode) {
0141     ui = new G4UIExecutive(argc, argv);
0142     runManagerType = G4RunManagerType::Serial;
0143   }
0144 
0145   // Initialization of default Run manager
0146   auto* runManager =
0147     G4RunManagerFactory::CreateRunManager(runManagerType);
0148   if(runManagerTypeInt == 1 || runManagerTypeInt == 2) {
0149     runManager->SetNumberOfThreads(numOfThreadsOrTasks);
0150   }
0151 
0152   // Detector geometry:
0153   auto detector = new Par04DetectorConstruction();
0154   auto parallelWorldFull = new Par04ParallelFullWorld("parallelWorldFullSim", detector);
0155   auto parallelWorldFast =
0156     new Par04ParallelFastWorld("parallelWorldFastSim", detector, parallelWorldFull);
0157   detector->RegisterParallelWorld(parallelWorldFull);
0158   detector->RegisterParallelWorld(parallelWorldFast);
0159   runManager->SetUserInitialization(detector);
0160 
0161   // Physics list
0162   auto physicsList = new FTFP_BERT();
0163   // Add fast simulation physics
0164   auto fastSimulationPhysics = new G4FastSimulationPhysics();
0165   fastSimulationPhysics->BeVerbose();
0166   fastSimulationPhysics->ActivateFastSimulation("e-");
0167   fastSimulationPhysics->ActivateFastSimulation("e+");
0168   fastSimulationPhysics->ActivateFastSimulation("gamma");
0169   physicsList->RegisterPhysics(fastSimulationPhysics);
0170   // Add parallel world for readout
0171   physicsList->RegisterPhysics(new G4ParallelWorldPhysics("parallelWorldFullSim"));
0172   physicsList->RegisterPhysics(new G4ParallelWorldPhysics("parallelWorldFastSim"));
0173   // reduce verbosity of physics lists
0174   G4EmParameters::Instance()->SetVerbose(0);
0175   runManager->SetUserInitialization(physicsList);
0176 
0177   //-------------------------------
0178   // UserAction classes
0179   //-------------------------------
0180   runManager->SetUserInitialization(new Par04ActionInitialisation(detector, parallelWorldFull));
0181   G4UImanager* UImanager = G4UImanager::GetUIpointer();
0182 
0183   if (useInteractiveMode) {
0184     //----------------
0185     // Visualization:
0186     //----------------
0187     G4cout << "Instantiating Visualization Manager......." << G4endl;
0188     G4VisManager* visManager = new G4VisExecutive;
0189     visManager->Initialize();
0190 
0191     if (batchMacroName.empty()) {
0192       G4Exception("main", "Unknown macro name", FatalErrorInArgument,
0193                   ("No macro name passed to " + G4String(argv[0])).c_str());
0194     }
0195     G4String command = "/control/execute ";
0196     UImanager->ApplyCommand(command + batchMacroName);
0197     ui->SessionStart();
0198     delete visManager;
0199   }
0200   else {
0201     G4String command = "/control/execute ";
0202     UImanager->ApplyCommand(command + batchMacroName);
0203   }
0204 
0205   // Free the store: user actions, physics_list and detector_description are
0206   //                 owned and deleted by the run manager, so they should not
0207   //                 be deleted in the main() program !
0208   delete ui;
0209   delete runManager;
0210 
0211   return 0;
0212 }