File indexing completed on 2025-01-30 09:17:35
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DDG4/Geant4UIManager.h>
0016 #include <DDG4/Geant4Kernel.h>
0017 #include <DDG4/Geant4UIMessenger.h>
0018 #include <DD4hep/Primitives.h>
0019
0020
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
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
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
0065 Geant4UIManager::~Geant4UIManager() {
0066 }
0067
0068
0069 void Geant4UIManager::configure() {
0070
0071 G4UImanager* mgr = G4UImanager::GetUIpointer();
0072
0073 if ( m_haveUI ) {
0074 m_ui = startUI();
0075 }
0076
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
0087 void Geant4UIManager::initialize() {
0088
0089 G4UImanager* mgr = G4UImanager::GetUIpointer();
0090
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
0101 void Geant4UIManager::terminate() {
0102
0103 G4UImanager* mgr = G4UImanager::GetUIpointer();
0104
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
0115 void Geant4UIManager::applyCommand(const std::string& command) {
0116
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
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
0138 void Geant4UIManager::forceExit() {
0139 std::_Exit(0);
0140 }
0141
0142
0143 void Geant4UIManager::regularExit() {
0144 info("++ End of processing requested.");
0145 context()->kernel().terminate();
0146 forceExit();
0147 }
0148
0149
0150 G4VisManager* Geant4UIManager::startVis() {
0151
0152 info("+++ Starting G4VisExecutive ....");
0153 G4VisManager* vis = new G4VisExecutive();
0154 vis->Initialize();
0155 return vis;
0156 }
0157
0158
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
0175 void Geant4UIManager::operator()(void* ) {
0176 start();
0177 stop();
0178 }
0179
0180
0181 void Geant4UIManager::start() {
0182
0183 G4UImanager* mgr = G4UImanager::GetUIpointer();
0184 bool executed_statements = false;
0185
0186
0187 if ( m_haveVis || !m_visSetup.empty() ) {
0188 m_vis = startVis();
0189 m_haveVis = true;
0190 m_haveUI = true;
0191 }
0192
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
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
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
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
0219 if ( m_haveUI && m_ui ) {
0220 m_ui->SessionStart();
0221
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
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
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
0262 void Geant4UIManager::stop() {
0263 detail::deletePtr(m_vis);
0264 detail::deletePtr(m_ui);
0265 }