DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4UIManager.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
15 #include <DDG4/Geant4UIManager.h>
16 #include <DDG4/Geant4Kernel.h>
17 #include <DDG4/Geant4UIMessenger.h>
18 #include <DD4hep/Primitives.h>
19 
21 #include <G4Version.hh>
22 #include <G4VisExecutive.hh>
23 #include <G4UImanager.hh>
24 #include <G4UIsession.hh>
25 #include <G4VisExecutive.hh>
26 #include <G4UIExecutive.hh>
27 #include <G4RunManager.hh>
28 
30 #include <cstdlib>
31 #include <functional>
32 
33 using namespace dd4hep::sim;
34 
35 namespace {
36  std::string make_cmd(const std::string& cmd) {
37  return std::string( "/control/execute "+cmd);
38  }
39 }
40 
42 Geant4UIManager::Geant4UIManager(Geant4Context* ctxt, const std::string& nam)
43  : Geant4Action(ctxt,nam), m_vis(0), m_ui(0)
44 {
45  declareProperty("SetupUI", m_uiSetup="");
46  declareProperty("SetupVIS", m_visSetup="");
47  declareProperty("SessionType", m_sessionType="tcsh");
48  declareProperty("Macros", m_macros);
49  declareProperty("ConfigureCommands", m_configureCommands);
50  declareProperty("InitializeCommands", m_initializeCommands);
51  declareProperty("TerminateCommands", m_terminateCommands);
52  declareProperty("Commands", m_preRunCommands);
53  declareProperty("PreRunCommands", m_preRunCommands);
54  declareProperty("PostRunCommands", m_postRunCommands);
55  declareProperty("HaveVIS", m_haveVis=false);
56  declareProperty("HaveUI", m_haveUI=true);
57  declareProperty("Prompt", m_prompt);
61  enableUI();
62 }
63 
66 }
67 
71  G4UImanager* mgr = G4UImanager::GetUIpointer();
73  if ( m_haveUI ) {
74  m_ui = startUI();
75  }
77  for(const auto& c : m_configureCommands) {
78  info("++ Executing configure command: %s",c.c_str());
79  G4int ret = mgr->ApplyCommand(c.c_str());
80  if ( ret != 0 ) {
81  except("Failed to execute command: %s",c.c_str());
82  }
83  }
84 }
85 
89  G4UImanager* mgr = G4UImanager::GetUIpointer();
91  for(const auto& c : m_initializeCommands) {
92  info("++ Executing initialization command: %s",c.c_str());
93  G4int ret = mgr->ApplyCommand(c.c_str());
94  if ( ret != 0 ) {
95  except("Failed to execute command: %s",c.c_str());
96  }
97  }
98 }
99 
103  G4UImanager* mgr = G4UImanager::GetUIpointer();
105  for(const auto& c : m_terminateCommands) {
106  info("++ Executing finalization command: %s",c.c_str());
107  G4int ret = mgr->ApplyCommand(c.c_str());
108  if ( ret != 0 ) {
109  except("Failed to execute command: %s",c.c_str());
110  }
111  }
112 }
113 
115 void Geant4UIManager::applyCommand(const std::string& command) {
117  G4UImanager* mgr = G4UImanager::GetUIpointer();
118  if ( mgr ) {
119  info("++ Executing G4 command: %s",command.c_str());
120  G4int ret = mgr->ApplyCommand(command.c_str());
121  if ( ret == 0 ) {
122  return;
123  }
124  except("Failed to execute command: %s",command.c_str());
125  }
126  except("No UI reference present. Too early to interact with Geant4!");
127 }
128 
131  m_control->addCall("exit", "Force exiting this process",
132  Callback(this).make(&Geant4UIManager::forceExit),0);
133  m_control->addCall("terminate", "Regular exit this process",
134  Callback(this).make(&Geant4UIManager::regularExit),0);
135 }
136 
139  std::_Exit(0);
140 }
141 
144  info("++ End of processing requested.");
145  context()->kernel().terminate();
146  forceExit();
147 }
148 
150 G4VisManager* Geant4UIManager::startVis() {
152  info("+++ Starting G4VisExecutive ....");
153  G4VisManager* vis = new G4VisExecutive();
154  vis->Initialize();
155  return vis;
156 }
157 
159 G4UIExecutive* Geant4UIManager::startUI() {
160  G4UIExecutive* ui = 0;
161  const char* args[] = {"DDG4","",""};
162  info("+++ Starting G4UIExecutive '%s' of type %s....", args[0], m_sessionType.c_str());
163 #if (G4VERSION_NUMBER >= 960)
164  ui = new G4UIExecutive(1,(char**)args,m_sessionType.c_str());
165 #else
166  ui = new G4UIExecutive(1,(char**)args );
167 #endif
168  if ( !m_prompt.empty() ) {
169  ui->SetPrompt(m_prompt);
170  }
171  return ui;
172 }
173 
176  start();
177  stop();
178 }
179 
183  G4UImanager* mgr = G4UImanager::GetUIpointer();
184  bool executed_statements = false;
185 
187  if ( m_haveVis || !m_visSetup.empty() ) {
188  m_vis = startVis();
189  m_haveVis = true;
190  m_haveUI = true;
191  }
193  if ( !m_visSetup.empty() ) {
194  info("++ Executing visualization setup: %s",m_visSetup.c_str());
195  mgr->ApplyCommand(make_cmd(m_visSetup).c_str());
196  }
198  if ( !m_uiSetup.empty() ) {
199  info("++ Executing UI setup: %s",m_uiSetup.c_str());
200  mgr->ApplyCommand(make_cmd(m_uiSetup).c_str());
201  executed_statements = true;
202  }
204  for(const auto& m : m_macros) {
205  info("++ Executing Macro file: %s",m.c_str());
206  mgr->ApplyCommand(make_cmd(m.c_str()));
207  executed_statements = true;
208  }
210  for(const auto& c : m_preRunCommands) {
211  info("++ Executing pre-run statement: %s",c.c_str());
212  G4int ret = mgr->ApplyCommand(c.c_str());
213  if ( ret != 0 ) {
214  except("Failed to execute command: %s",c.c_str());
215  }
216  executed_statements = true;
217  }
219  if ( m_haveUI && m_ui ) {
220  m_ui->SessionStart();
222  for(const auto& c : m_postRunCommands) {
223  info("++ Executing post-run statement: %s",c.c_str());
224  G4int ret = mgr->ApplyCommand(c.c_str());
225  if ( ret != 0 ) {
226  except("Failed to execute command: %s",c.c_str());
227  }
228  executed_statements = true;
229  }
230  return;
231  }
232  else if ( m_haveUI ) {
233  warning("++ No UI manager found. Exit.");
234  return;
235  }
236  else if ( executed_statements ) {
238  for(const auto& c : m_postRunCommands) {
239  info("++ Executing post-run statement: %s",c.c_str());
240  G4int ret = mgr->ApplyCommand(c.c_str());
241  if ( ret != 0 ) {
242  except("Failed to execute command: %s",c.c_str());
243  }
244  }
245  return;
246  }
247 
249  long numEvent = context()->kernel().property("NumEvents").value<long>();
250  if(numEvent < 0) numEvent = std::numeric_limits<int>::max();
251  info("++ Start run with %d events.",numEvent);
252  try {
253  context()->kernel().runManager().BeamOn(numEvent);
254  }
255  catch (DD4hep_End_Of_File& e) {
256  info("++ End of file reached, ending run...");
257  context()->kernel().runManager().RunTermination();
258  }
259 }
260 
263  detail::deletePtr(m_vis);
264  detail::deletePtr(m_ui);
265 }
dd4hep::sim::Geant4UIManager::terminate
void terminate()
Callback on terminate. Callback registered to the Geant4Kernel.
Definition: Geant4UIManager.cpp:101
dd4hep::sim::Geant4UIManager::m_haveUI
bool m_haveUI
Property: Flag to instantiate UI (default=true)
Definition: Geant4UIManager.h:78
dd4hep::sim::Geant4UIManager::m_initializeCommands
std::vector< std::string > m_initializeCommands
Property: List of commands to be executed when the Geant4Kernel gets initialized.
Definition: Geant4UIManager.h:64
dd4hep::sim::Geant4UIManager::~Geant4UIManager
virtual ~Geant4UIManager()
Default destructor.
Definition: Geant4UIManager.cpp:65
dd4hep::sim::Geant4UIManager::m_vis
G4VisManager * m_vis
Reference to Geant4 visualtion manager.
Definition: Geant4UIManager.h:80
dd4hep::sim::Geant4UIManager::start
void start()
Start manager & session.
Definition: Geant4UIManager.cpp:181
dd4hep::sim::Geant4UIManager::m_macros
std::vector< std::string > m_macros
Property: Array of commands to be chained.
Definition: Geant4UIManager.h:72
dd4hep::sim::Geant4UIManager::m_ui
G4UIExecutive * m_ui
Reference to Geant4 UI manager.
Definition: Geant4UIManager.h:82
dd4hep::sim::Geant4Kernel::property
Property & property(const std::string &name)
Access single property.
Definition: Geant4Kernel.cpp:298
dd4hep::sim::Geant4UIManager::forceExit
void forceExit()
Force exiting this process without calling atexit handlers.
Definition: Geant4UIManager.cpp:138
dd4hep::sim::Geant4UIManager::m_terminateCommands
std::vector< std::string > m_terminateCommands
Property: List of commands to be executed when the Geant4Kernel gets terminated.
Definition: Geant4UIManager.h:66
dd4hep::sim::Geant4UIManager::m_visSetup
std::string m_visSetup
Property: Name of the visualization macro file.
Definition: Geant4UIManager.h:60
dd4hep::sim::Geant4Action::warning
void warning(const char *fmt,...) const
Support of warning messages.
Definition: Geant4Action.cpp:223
dd4hep::sim::Geant4UIManager::initialize
void initialize()
Initialize the object. Callback registered to the Geant4Kernel.
Definition: Geant4UIManager.cpp:87
dd4hep::sim::Geant4Kernel::register_initialize
void register_initialize(const std::function< void()> &callback)
Register initialize callback. Signature: (function)()
Definition: Geant4Kernel.cpp:375
dd4hep::sim::Geant4UIManager::startUI
G4UIExecutive * startUI()
Start UI.
Definition: Geant4UIManager.cpp:159
dd4hep::sim::Geant4UIManager::m_haveVis
bool m_haveVis
Property: Flag to instantiate Vis manager (default=false, unless m_visSetup set)
Definition: Geant4UIManager.h:76
dd4hep::sim::Geant4UIManager::stop
void stop()
Stop and release resources.
Definition: Geant4UIManager.cpp:262
dd4hep::sim::Geant4Action::info
void info(const char *fmt,...) const
Support of info messages.
Definition: Geant4Action.cpp:215
dd4hep::sim::Geant4Action::except
void except(const char *fmt,...) const
Support of exceptions: Print fatal message and throw runtime_error.
Definition: Geant4Action.cpp:256
Geant4UIMessenger.h
dd4hep::sim::Geant4Kernel::register_terminate
void register_terminate(const std::function< void()> &callback)
Register terminate callback. Signature: (function)()
Definition: Geant4Kernel.cpp:380
dd4hep::sim::Geant4UIManager::m_uiSetup
std::string m_uiSetup
Property: Name of the UI macro file.
Definition: Geant4UIManager.h:58
dd4hep::Callback
Definition of the generic callback structure for member functions.
Definition: Callback.h:38
dd4hep::sim::Geant4UIManager::installCommandMessenger
void installCommandMessenger()
Install command control messenger to write GDML file from command prompt.
Definition: Geant4UIManager.cpp:130
dd4hep::sim::Geant4Action::declareProperty
Geant4Action & declareProperty(const std::string &nam, T &val)
Declare property.
Definition: Geant4Action.h:366
dd4hep::sim::Geant4UIManager::regularExit
void regularExit()
Regularly exiting this process without calling atexit handlers.
Definition: Geant4UIManager.cpp:143
dd4hep::sim::Geant4Action
Default base class for all Geant 4 actions and derivates thereof.
Definition: Geant4Action.h:113
dd4hep::sim::Geant4UIManager::applyCommand
void applyCommand(const std::string &command)
Apply single command.
Definition: Geant4UIManager.cpp:115
dd4hep::sim::Geant4UIManager::configure
void configure()
Configure the object. Callback registered to the Geant4Kernel.
Definition: Geant4UIManager.cpp:69
dd4hep::sim::Geant4Kernel::register_configure
void register_configure(const std::function< void()> &callback)
Register configure callback. Signature: (function)()
Definition: Geant4Kernel.cpp:370
dd4hep::Property::value
TYPE value() const
Retrieve value.
Definition: ComponentProperties.h:126
Primitives.h
dd4hep::sim::Geant4Action::m_control
Geant4UIMessenger * m_control
Control directory of this action.
Definition: Geant4Action.h:118
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: Geant4Output2EDM4hep.cpp:49
dd4hep::sim::Geant4Context::kernel
Geant4Kernel & kernel() const
Access to the kernel object.
Definition: Geant4Context.h:233
dd4hep::sim::DD4hep_End_Of_File
Helper class to indicate the end of the input file.
Definition: Geant4Kernel.h:43
dd4hep::sim::Geant4UIManager::startVis
G4VisManager * startVis()
Start visualization.
Definition: Geant4UIManager.cpp:150
dd4hep::sim::Geant4UIManager::operator()
virtual void operator()(void *param)
Run UI.
Definition: Geant4UIManager.cpp:175
dd4hep::sim::Geant4UIMessenger::addCall
void addCall(const std::string &name, const std::string &description, const Callback &cb, size_t npar=0)
Add a new callback structure.
Definition: Geant4UIMessenger.cpp:60
dd4hep::sim::Geant4UIManager::m_sessionType
std::string m_sessionType
Name of the default session type (="cmd")
Definition: Geant4UIManager.h:56
dd4hep::sim::Geant4UIManager::m_preRunCommands
std::vector< std::string > m_preRunCommands
Property: List of commands to be executed BEFORE running.
Definition: Geant4UIManager.h:68
Geant4Kernel.h
dd4hep::sim::Geant4UIManager::m_configureCommands
std::vector< std::string > m_configureCommands
Property: List of commands to be executed when the Geant4Kernel gets configured.
Definition: Geant4UIManager.h:62
dd4hep::sim::Geant4Action::enableUI
virtual void enableUI()
Enable and install UI messenger.
Definition: Geant4Action.cpp:134
dd4hep::sim::Geant4Kernel::runManager
G4RunManager & runManager()
Access to the Geant4 run manager.
Definition: Geant4Kernel.cpp:322
dd4hep::sim::Geant4UIManager::m_postRunCommands
std::vector< std::string > m_postRunCommands
Property: List of commands to be executed AFTER running.
Definition: Geant4UIManager.h:70
dd4hep::sim::Geant4UIManager::m_prompt
std::string m_prompt
Property: New prompt if the user wants to change it. (Default is do nothing)
Definition: Geant4UIManager.h:74
Geant4UIManager.h
dd4hep::sim::Geant4UIManager::Geant4UIManager
Geant4UIManager(Geant4Context *context, const std::string &name)
Initializing constructor.
Definition: Geant4UIManager.cpp:42
dd4hep::sim::Geant4Context
Generic context to extend user, run and event information.
Definition: Geant4Context.h:201
dd4hep::sim::Geant4Action::context
Geant4Context * context() const
Access the context.
Definition: Geant4Action.h:270
dd4hep::sim::Geant4Kernel::terminate
virtual int terminate() override
Run the simulation: Terminate Geant4.
Definition: Geant4Kernel.cpp:428