Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-14 08:30:46

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 extensibleFactory.cc
0027 /// \brief Main program of the physicslists/extensibleFactory example
0028 
0029 // -------------------------------------------------------------
0030 //      extensibleFactory
0031 //
0032 //  Application demonstrating the extensible physics list factory
0033 //
0034 //  Author of hadronic/Hadr00/Hadr00.cc
0035 //     V.Ivanchenko, 20 June 2008  (as hadronic/Hadr00/Hadr00.cc)
0036 //  Author of examples/extended/physicslists/factory/factory.cc
0037 //      I. Hrivnacova, 2017-09-26
0038 
0039 //  Modified from factory.cc
0040 //      R.Hatcher 2017-10-31
0041 //        copied from examples/extended/physicslists/factory
0042 //        modified to use alternative extensible physics list factory
0043 //
0044 // -------------------------------------------------------------
0045 //
0046 //
0047 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0048 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0049 
0050 #include "ActionInitialization.hh"
0051 #include "DetectorConstruction.hh"
0052 #include "PrimaryGeneratorAction.hh"
0053 
0054 #include "G4RunManagerFactory.hh"
0055 
0056 /////////////////////////////////////////////////////////////////////////////
0057 // The following change is the _only_ required changed to move from
0058 // the non-extensible factory to the exensible factory.  All other changes
0059 // relative to the "factory" example are there to demonstrate new features.
0060 /////////////////////////////////////////////////////////////////////////////
0061 // non-extensible:  #include "G4PhysListFactory.hh"
0062 #include "G4PhysListFactoryAlt.hh"
0063 // use this for drop-in replacement:  using namespace g4alt;
0064 
0065 /////////////////////////////////////////////////////////////////////////////
0066 // headers needed to demonstrate new featues
0067 /////////////////////////////////////////////////////////////////////////////
0068 
0069 // allow ourselves to extend the short names for physics ctor addition/replace
0070 // along the same lines as EMX, EMY, etc
0071 #include "G4PhysListRegistry.hh"
0072 
0073 // allow ourselves to give the user extra info about available physics ctors
0074 #include "G4PhysicsConstructorFactory.hh"
0075 
0076 // pull in a user defined physics list definition into the main program
0077 // and register it with the factory (doesn't have to be the main program
0078 // but the .o containing the declaration _must_ get linked/loaded)
0079 #include "MySpecialPhysList.hh"
0080 
0081 #include "G4PhysListStamper.hh"  // defines macro for factory registration
0082 G4_DECLARE_PHYSLIST_FACTORY(MySpecialPhysList);
0083 
0084 /////////////////////////////////////////////////////////////////////////////
0085 
0086 #include "G4UIExecutive.hh"
0087 #include "G4UImanager.hh"
0088 #include "G4VModularPhysicsList.hh"
0089 #include "G4VisExecutive.hh"
0090 #include "Randomize.hh"
0091 
0092 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0093 
0094 namespace
0095 {
0096 
0097 void PrintAvailable(G4int verbosity)
0098 {
0099   G4cout << G4endl;
0100   G4cout << "extensibleFactory: here are the available physics lists:" << G4endl;
0101   g4alt::G4PhysListFactory factory;
0102   factory.PrintAvailablePhysLists();
0103 
0104   // if user asked for extra verbosity then print physics ctors as well
0105   if (verbosity > 1) {
0106     G4cout << G4endl;
0107     G4cout << "extensibleFactory: "
0108            << "here are the available physics ctors that can be added:" << G4endl;
0109     G4PhysicsConstructorRegistry* g4pctorFactory = G4PhysicsConstructorRegistry::Instance();
0110     g4pctorFactory->PrintAvailablePhysicsConstructors();
0111   }
0112 }
0113 
0114 void PrintUsage(G4int verbosity)
0115 {
0116   G4cerr << " Usage: " << G4endl;
0117   G4cerr << " extensibleFactory [-m macro ] [-p physList ]"
0118          << " [-u UIsession] [-t nThreads]" << G4endl << " [-v | --verbose] [-h | --help]"
0119          << G4endl;
0120   G4cerr << "   note: -t option is available only for multi-threaded mode." << G4endl;
0121   G4cerr << "   note: -v can be repeated to increase verbosity." << G4endl;
0122   G4cerr << G4endl;
0123 
0124   if (verbosity > 0) PrintAvailable(verbosity);
0125 }
0126 
0127 }  // namespace
0128 
0129 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0130 
0131 int main(int argc, char** argv)
0132 {
0133   // Evaluate arguments
0134   //
0135   if (argc > 13) {
0136     PrintUsage(0);
0137     return 1;
0138   }
0139 
0140   G4String macro;
0141   G4String session;
0142   G4String physListName;
0143   char* physListNameEnv = nullptr;
0144   G4String gdmlFileName;
0145 #ifdef G4MULTITHREADED
0146   G4int nofThreads = 0;
0147 #endif
0148   G4int verbosity = 0;
0149 
0150   for (G4int i = 1; i < argc; i = i + 2) {
0151     G4String g4argv(argv[i]);  // convert only once
0152     if (g4argv == "-m")
0153       macro = argv[i + 1];
0154     else if (g4argv == "-u")
0155       session = argv[i + 1];
0156     else if (g4argv == "-p")
0157       physListName = argv[i + 1];
0158 #ifdef G4MULTITHREADED
0159     else if (g4argv == "-t") {
0160       nofThreads = G4UIcommand::ConvertToInt(argv[i + 1]);
0161     }
0162 #endif
0163     else if (g4argv == "-v" || g4argv == "--verbose") {
0164       ++verbosity;  // verbose flag doesn't take an argument
0165       --i;  // don't increment argc by two, just the one
0166     }
0167     else if (g4argv == "-h" || g4argv == "--help") {
0168       PrintUsage(verbosity + 1);
0169       return 1;
0170     }
0171     else {
0172       PrintUsage(0);
0173       return 1;
0174     }
0175   }
0176 
0177   // Detect interactive mode (if no arguments) and define UI session
0178   //
0179   G4UIExecutive* ui = nullptr;
0180   if (!macro.size()) {
0181     ui = new G4UIExecutive(argc, argv, session);
0182   }
0183 
0184   // Choose the Random engine  //choose the Random engine
0185   G4Random::setTheEngine(new CLHEP::RanecuEngine());
0186 
0187   // Construct the run manager
0188   auto* runManager = G4RunManagerFactory::CreateRunManager();
0189 #ifdef G4MULTITHREADED
0190   if (nofThreads > 0) {
0191     runManager->SetNumberOfThreads(nofThreads);
0192   }
0193 #endif
0194 
0195   // g4alt::G4PhysListFactoryAlt is the extensible factory
0196   // including the G4PhysListFactoryAlt.hh header and the line:
0197   //    using namespace g4alt;
0198   // would make this a drop-in replacement, but we'll list the explicit
0199   // namespace here just for clarity
0200   g4alt::G4PhysListFactory factory;
0201   G4VModularPhysicsList* physList = nullptr;
0202 
0203   // Show how an alternative default list could be set
0204   // but set the default to the normal default FTFP_BERT.
0205   // This is what is used when no -p flag is given and $PHYSLIST
0206   // is not defined in the environment.
0207   G4String defaultPhysListName = "FTFP_BERT";
0208   if (verbosity > 0) {
0209     G4cout << "extensibleFactory: SetDefaultReferencePhysList to '" << defaultPhysListName
0210            << "' ('' = system default)" << G4endl << G4endl;
0211   }
0212   factory.SetDefaultReferencePhysList(defaultPhysListName);
0213 
0214   // set a short name for G4RadioactiveDecayPhysics
0215   G4PhysListRegistry* plreg = G4PhysListRegistry::Instance();
0216   plreg->AddPhysicsExtension("RADIO", "G4RadioactiveDecayPhysics");
0217   plreg->AddPhysicsExtension("MYPHYSICS", "MyG4PhysicsPhysics");
0218   if (verbosity > 0) {
0219     G4cout << "extensibleFactory: adding extensions" << G4endl
0220            << "   RADIO     ===> G4RadioactiveDecayPhysics" << G4endl
0221            << "   MYPHYSICS ===> MyG4PhysicsPhysics" << G4endl << G4endl;
0222   }
0223 
0224   // Get Reference PhysicsList via its name, or if none given
0225   //    from environment varialb e$PHYSLIST, with fall back to a default
0226   if (physListName.size()) {
0227     if (verbosity > 0) {
0228       G4cout << "extensibleFactory: explicitly using '" << physListName << "'" << G4endl;
0229     }
0230     physList = factory.GetReferencePhysList(physListName);
0231   }
0232   else {
0233     if (verbosity > 0) {
0234       G4cout << "extensibleFactory: no -p flag;"
0235              << " using ReferencePhysList() ($PHYSLIST or default)" << G4endl;
0236     }
0237     physList = factory.ReferencePhysList();
0238 
0239     if (!physList) {
0240       // failed?  get what the user set, but we couldn't find
0241       physListNameEnv = std::getenv("PHYSLIST");
0242       if (physListNameEnv) {
0243         G4cout << "extensibleFactory: $PHYSLIST=" << physListNameEnv << G4endl;
0244       }
0245     }
0246   }
0247 
0248   // deal with failure to get what the user wanted
0249   // print what they _could_ use
0250   if (!physList) {
0251     G4cerr << "extensibleFactory: PhysicsList '"
0252            << (physListNameEnv ? physListNameEnv : physListName)
0253            << "' was not available in g4alt::PhysListFactory." << G4endl;
0254     PrintAvailable(verbosity);
0255 
0256     // if we can't get what the user asked for...
0257     //    don't go on to use something else, that's confusing
0258     G4ExceptionDescription ED;
0259     ED << "The factory for the physicslist [" << (physListNameEnv ? physListNameEnv : physListName)
0260        << "] does not exist!" << G4endl;
0261     G4Exception("extensibleFactory", "extensibleFactory001", FatalException, ED);
0262     exit(42);
0263   }
0264 
0265   // Set mandatory initialization classes
0266   runManager->SetUserInitialization(new DetectorConstruction());
0267   runManager->SetUserInitialization(physList);
0268 
0269   // set user action classes
0270   auto actinit = new ActionInitialization("extensibleFactory");
0271   runManager->SetUserInitialization(actinit);
0272 
0273   // Initialize visualization
0274   G4VisManager* visManager = new G4VisExecutive;
0275   // G4VisExecutive can take a verbosity argument - see /vis/verbose guidance.
0276   // G4VisManager* visManager = new G4VisExecutive("Quiet");
0277   visManager->Initialize();
0278 
0279   // Get the pointer to the User Interface manager
0280   G4UImanager* UImanager = G4UImanager::GetUIpointer();
0281 
0282   if (macro.size()) {
0283     // batch mode
0284     G4String command = "/control/execute ";
0285     UImanager->ApplyCommand(command + macro);
0286   }
0287   else {
0288     // interactive mode : define UI session
0289     UImanager->ApplyCommand("/control/execute init_vis.mac");
0290     ui->SessionStart();
0291     delete ui;
0292   }
0293 
0294   // Job termination
0295   // Free the store: user actions, physics_list and detector_description are
0296   // owned and deleted by the run manager, so they should not be deleted
0297   // in the main() program !
0298 
0299   delete visManager;
0300   delete runManager;
0301 }
0302 
0303 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......