Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-23 09:22:38

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