Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:17:35

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 
0014 /// Framework include files
0015 #include <DDG4/Geant4UIManager.h>
0016 #include <DDG4/Geant4Kernel.h>
0017 #include <DDG4/Geant4UIMessenger.h>
0018 #include <DD4hep/Primitives.h>
0019 
0020 /// Geant4 include files
0021 #include <G4Version.hh>
0022 #include <G4VisExecutive.hh>
0023 #include <G4UImanager.hh>
0024 #include <G4UIsession.hh>
0025 #include <G4VisExecutive.hh>
0026 #include <G4UIExecutive.hh>
0027 #include <G4RunManager.hh>
0028 
0029 /// C/C++ include files
0030 #include <cstdlib>
0031 #include <functional>
0032 
0033 using namespace dd4hep::sim;
0034 
0035 namespace   {
0036   std::string make_cmd(const std::string& cmd)  {
0037     return std::string( "/control/execute "+cmd);
0038   }
0039 }
0040 
0041 /// Initializing constructor
0042 Geant4UIManager::Geant4UIManager(Geant4Context* ctxt, const std::string& nam)
0043   : Geant4Action(ctxt,nam), m_vis(0), m_ui(0)
0044 {
0045   declareProperty("SetupUI",            m_uiSetup="");
0046   declareProperty("SetupVIS",           m_visSetup="");
0047   declareProperty("SessionType",        m_sessionType="tcsh");
0048   declareProperty("Macros",             m_macros);
0049   declareProperty("ConfigureCommands",  m_configureCommands);
0050   declareProperty("InitializeCommands", m_initializeCommands);
0051   declareProperty("TerminateCommands",  m_terminateCommands);
0052   declareProperty("Commands",           m_preRunCommands);
0053   declareProperty("PreRunCommands",     m_preRunCommands);
0054   declareProperty("PostRunCommands",    m_postRunCommands);
0055   declareProperty("HaveVIS",            m_haveVis=false);
0056   declareProperty("HaveUI",             m_haveUI=true);
0057   declareProperty("Prompt",             m_prompt);
0058   context()->kernel().register_configure(std::bind(&Geant4UIManager::configure,this));
0059   context()->kernel().register_initialize(std::bind(&Geant4UIManager::initialize,this));
0060   context()->kernel().register_terminate(std::bind(&Geant4UIManager::terminate,this));
0061   enableUI();
0062 }
0063 
0064 /// Default destructor
0065 Geant4UIManager::~Geant4UIManager()   {
0066 }
0067 
0068 /// Configure the object
0069 void Geant4UIManager::configure()   {
0070   /// Get the pointer to the User Interface manager
0071   G4UImanager* mgr = G4UImanager::GetUIpointer();
0072   /// Start UI instance
0073   if ( m_haveUI ) {
0074     m_ui = startUI();
0075   }
0076   /// Execute the chained command statements
0077   for(const auto& c : m_configureCommands)  {
0078     info("++ Executing configure command: %s",c.c_str());
0079     G4int ret = mgr->ApplyCommand(c.c_str());
0080     if ( ret != 0 )  {
0081       except("Failed to execute command: %s",c.c_str());
0082     }
0083   }
0084 }
0085 
0086 /// Initialize the object
0087 void Geant4UIManager::initialize()   {
0088   /// Get the pointer to the User Interface manager
0089   G4UImanager* mgr = G4UImanager::GetUIpointer();
0090   /// Execute the chained command statements
0091   for(const auto& c : m_initializeCommands)  {
0092     info("++ Executing initialization command: %s",c.c_str());
0093     G4int ret = mgr->ApplyCommand(c.c_str());
0094     if ( ret != 0 )  {
0095       except("Failed to execute command: %s",c.c_str());
0096     }
0097   }
0098 }
0099 
0100 /// Callback on terminate
0101 void Geant4UIManager::terminate() {
0102   /// Get the pointer to the User Interface manager
0103   G4UImanager* mgr = G4UImanager::GetUIpointer();
0104   /// Execute the chained command statements
0105   for(const auto& c : m_terminateCommands)  {
0106     info("++ Executing finalization command: %s",c.c_str());
0107     G4int ret = mgr->ApplyCommand(c.c_str());
0108     if ( ret != 0 )  {
0109       except("Failed to execute command: %s",c.c_str());
0110     }
0111   }
0112 }
0113 
0114 /// Apply single command
0115 void Geant4UIManager::applyCommand(const std::string& command)   {
0116   /// Get the pointer to the User Interface manager
0117   G4UImanager* mgr = G4UImanager::GetUIpointer();
0118   if ( mgr )    {
0119     info("++ Executing G4 command: %s",command.c_str());
0120     G4int ret = mgr->ApplyCommand(command.c_str());
0121     if ( ret == 0 )  {
0122       return;
0123     }
0124     except("Failed to execute command: %s",command.c_str());
0125   }
0126   except("No UI reference present. Too early to interact with Geant4!");
0127 }
0128 
0129 /// Install command control messenger to write GDML file from command prompt.
0130 void Geant4UIManager::installCommandMessenger()   {
0131   m_control->addCall("exit", "Force exiting this process",
0132                      Callback(this).make(&Geant4UIManager::forceExit),0);
0133   m_control->addCall("terminate", "Regular exit this process",
0134                      Callback(this).make(&Geant4UIManager::regularExit),0);
0135 }
0136 
0137 /// Force exiting this process without calling atexit handlers
0138 void Geant4UIManager::forceExit()   {
0139   std::_Exit(0);
0140 }
0141 
0142 /// Regularly exiting this process without calling atexit handlers
0143 void Geant4UIManager::regularExit()   {
0144   info("++ End of processing requested.");
0145   context()->kernel().terminate();
0146   forceExit();
0147 }
0148 
0149 /// Start visualization
0150 G4VisManager* Geant4UIManager::startVis()  {
0151   /// Initialize visualization
0152   info("+++ Starting G4VisExecutive ....");
0153   G4VisManager* vis = new G4VisExecutive();
0154   vis->Initialize();
0155   return vis;
0156 }
0157 
0158 /// Start UI
0159 G4UIExecutive* Geant4UIManager::startUI()   {
0160   G4UIExecutive* ui = 0;
0161   const char* args[] = {"DDG4","",""};
0162   info("+++ Starting G4UIExecutive '%s' of type %s....", args[0], m_sessionType.c_str());
0163 #if (G4VERSION_NUMBER >= 960)
0164   ui = new G4UIExecutive(1,(char**)args,m_sessionType.c_str());
0165 #else
0166   ui = new G4UIExecutive(1,(char**)args );
0167 #endif
0168   if ( !m_prompt.empty() )  {
0169     ui->SetPrompt(m_prompt);
0170   }
0171   return ui;
0172 }
0173 
0174 /// Run UI
0175 void Geant4UIManager::operator()(void* )   {
0176   start();
0177   stop();
0178 }
0179 
0180 /// Start manager & session
0181 void Geant4UIManager::start() {
0182   /// Get the pointer to the User Interface manager
0183   G4UImanager* mgr = G4UImanager::GetUIpointer();
0184   bool executed_statements = false;
0185 
0186   /// Start visualization
0187   if ( m_haveVis || !m_visSetup.empty() ) {
0188     m_vis = startVis();
0189     m_haveVis = true;   /// If graphics setup, vis is always true
0190     m_haveUI = true;    /// No graphics without UI!
0191   }
0192   /// Configure visualization instance
0193   if ( !m_visSetup.empty() ) {
0194     info("++ Executing visualization setup: %s",m_visSetup.c_str());
0195     mgr->ApplyCommand(make_cmd(m_visSetup).c_str());
0196   }
0197   /// Configure UI instance
0198   if ( !m_uiSetup.empty() )   {
0199     info("++ Executing UI setup: %s",m_uiSetup.c_str());
0200     mgr->ApplyCommand(make_cmd(m_uiSetup).c_str());
0201     executed_statements = true;
0202   }
0203   /// Execute the chained macro files
0204   for(const auto& m : m_macros)  {
0205     info("++ Executing Macro file: %s",m.c_str());
0206     mgr->ApplyCommand(make_cmd(m.c_str()));
0207     executed_statements = true;
0208   }
0209   /// Execute the chained pre-run command statements
0210   for(const auto& c : m_preRunCommands)  {
0211     info("++ Executing pre-run statement: %s",c.c_str());
0212     G4int ret = mgr->ApplyCommand(c.c_str());
0213     if ( ret != 0 )  {
0214       except("Failed to execute command: %s",c.c_str());
0215     }
0216     executed_statements = true;
0217   }
0218   /// Start UI session if present
0219   if ( m_haveUI && m_ui )   {
0220     m_ui->SessionStart();
0221     /// Execute the chained post-run command statements
0222     for(const auto& c : m_postRunCommands)  {
0223       info("++ Executing post-run statement: %s",c.c_str());
0224       G4int ret = mgr->ApplyCommand(c.c_str());
0225       if ( ret != 0 )  {
0226         except("Failed to execute command: %s",c.c_str());
0227       }
0228       executed_statements = true;
0229     }
0230     return;
0231   }
0232   else if ( m_haveUI )   {
0233     warning("++ No UI manager found. Exit.");
0234     return;
0235   }
0236   else if ( executed_statements )  {
0237     /// Execute the chained post-run command statements
0238     for(const auto& c : m_postRunCommands)  {
0239       info("++ Executing post-run statement: %s",c.c_str());
0240       G4int ret = mgr->ApplyCommand(c.c_str());
0241       if ( ret != 0 )  {
0242         except("Failed to execute command: %s",c.c_str());
0243       }
0244     }
0245     return;
0246   }
0247 
0248   /// No UI. Pure batch mode: Simply execute requested number of events
0249   long numEvent = context()->kernel().property("NumEvents").value<long>();
0250   if(numEvent < 0) numEvent = std::numeric_limits<int>::max();
0251   info("++ Start run with %d events.",numEvent);
0252   try {
0253     context()->kernel().runManager().BeamOn(numEvent);
0254   }
0255   catch (DD4hep_End_Of_File& e) {
0256     info("++ End of file reached, ending run...");
0257     context()->kernel().runManager().RunTermination();
0258   }
0259 }
0260 
0261 /// Stop and release resources
0262 void Geant4UIManager::stop() {
0263   detail::deletePtr(m_vis);
0264   detail::deletePtr(m_ui);
0265 }