DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4Action.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 
14 // Framework include files
15 #include <DD4hep/Printout.h>
16 #include <DD4hep/InstanceCount.h>
17 #include <DDG4/Geant4Context.h>
18 #include <DDG4/Geant4Action.h>
19 #include <DDG4/Geant4Kernel.h>
20 #include <DDG4/Geant4UIMessenger.h>
21 
22 // Geant4 include files
23 #include <G4UIdirectory.hh>
24 
25 // C/C++ include files
26 #include <algorithm>
27 
28 using namespace dd4hep::sim;
29 
30 TypeName TypeName::split(const std::string& type_name, const std::string& delim) {
31  size_t idx = type_name.find(delim);
32  std::string typ = type_name, nam = type_name;
33  if (idx != std::string::npos) {
34  typ = type_name.substr(0, idx);
35  nam = type_name.substr(idx + 1);
36  }
37  return TypeName(typ, nam);
38 }
39 
40 TypeName TypeName::split(const std::string& type_name) {
41  return split(type_name,"/");
42 }
43 #if 0
44 void Geant4Action::ContextUpdate::operator()(Geant4Action* action) const {
45  action->m_context = context;
46  if ( 0 == action->m_context ) {
47 
48  cout << "ERROR" << endl;
49 
50  }
51 }
52 #endif
53 Geant4Action::Geant4Action(Geant4Context* ctxt, const std::string& nam)
55  : m_context(ctxt), m_outputLevel(INFO), m_name(nam)
56 {
58  m_outputLevel = ctxt ? ctxt->kernel().getOutputLevel(nam) : (printLevel()-1);
59  declareProperty("Name", m_name);
60  declareProperty("name", m_name);
61  declareProperty("OutputLevel", m_outputLevel);
62  declareProperty("Control", m_needsControl);
63 }
64 
68 }
69 
72  return ++m_refCount;
73 }
74 
77  long count = --m_refCount;
78  if (m_refCount <= 0) {
79  printM1("Geant4Action: Deleting object %s of type %s Pointer:%p",
80  m_name.c_str(),typeName(typeid(*this)).c_str(),(void*)this);
81  delete this;
82  }
83  return count;
84 }
85 
87 dd4hep::PrintLevel Geant4Action::setOutputLevel(PrintLevel new_level) {
88  int old = m_outputLevel;
89  m_outputLevel = new_level;
90  return (PrintLevel)old;
91 }
92 
94 bool Geant4Action::hasProperty(const std::string& nam) const {
95  return m_properties.exists(nam);
96 }
97 
99 dd4hep::Property& Geant4Action::property(const std::string& nam) {
100  return properties()[nam];
101 }
102 
105  //m_needsControl = true;
106  if (m_needsControl && !m_control) {
107  std::string path = context()->kernel().directoryName();
108  path += name() + "/";
109  m_control = new Geant4UIMessenger(name(), path);
112  }
113 }
114 
118 }
119 
122 }
123 
126  if ( m_control ) {
127  return m_control;
128  }
129  except("No control was installed for this action item.");
130  return 0;
131 }
132 
135  m_needsControl = true;
137 }
138 
140 void Geant4Action::configureFiber(Geant4Context* /* thread_context */) {
141 }
142 
144 void Geant4Action::print(const char* fmt, ...) const {
145  int level = std::max(int(outputLevel()),(int)VERBOSE);
146  if ( level >= printLevel() ) {
147  va_list args;
148  va_start(args, fmt);
149  dd4hep::printout((PrintLevel)level, m_name.c_str(), fmt, args);
150  va_end(args);
151  }
152 }
153 
155 void Geant4Action::printM1(const char* fmt, ...) const {
156  int level = std::max(outputLevel()-1,(int)VERBOSE);
157  if ( level >= printLevel() ) {
158  va_list args;
159  va_start(args, fmt);
160  dd4hep::printout((PrintLevel)level, m_name.c_str(), fmt, args);
161  va_end(args);
162  }
163 }
164 
166 void Geant4Action::printM2(const char* fmt, ...) const {
167  int level = std::max(outputLevel()-2,(int)VERBOSE);
168  if ( level >= printLevel() ) {
169  va_list args;
170  va_start(args, fmt);
171  dd4hep::printout((PrintLevel)level, m_name.c_str(), fmt, args);
172  va_end(args);
173  }
174 }
175 
177 void Geant4Action::printP1(const char* fmt, ...) const {
178  int level = std::min(outputLevel()+1,(int)FATAL);
179  if ( level >= printLevel() ) {
180  va_list args;
181  va_start(args, fmt);
182  dd4hep::printout((PrintLevel)level, m_name.c_str(), fmt, args);
183  va_end(args);
184  }
185 }
186 
188 void Geant4Action::printP2(const char* fmt, ...) const {
189  int level = std::min(outputLevel()+2,(int)FATAL);
190  if ( level >= printLevel() ) {
191  va_list args;
192  va_start(args, fmt);
193  dd4hep::printout((PrintLevel)level, m_name.c_str(), fmt, args);
194  va_end(args);
195  }
196 }
197 
199 void Geant4Action::always(const char* fmt, ...) const {
200  va_list args;
201  va_start(args, fmt);
202  dd4hep::printout(dd4hep::ALWAYS, m_name, fmt, args);
203  va_end(args);
204 }
205 
207 void Geant4Action::debug(const char* fmt, ...) const {
208  va_list args;
209  va_start(args, fmt);
210  dd4hep::printout(dd4hep::DEBUG, m_name, fmt, args);
211  va_end(args);
212 }
213 
215 void Geant4Action::info(const char* fmt, ...) const {
216  va_list args;
217  va_start(args, fmt);
218  dd4hep::printout(dd4hep::INFO, m_name, fmt, args);
219  va_end(args);
220 }
221 
223 void Geant4Action::warning(const char* fmt, ...) const {
224  va_list args;
225  va_start(args, fmt);
226  dd4hep::printout(dd4hep::WARNING, m_name, fmt, args);
227  va_end(args);
228 }
229 
231 void Geant4Action::error(const char* fmt, ...) const {
232  va_list args;
233  va_start(args, fmt);
234  dd4hep::printout(dd4hep::ERROR, m_name, fmt, args);
235  va_end(args);
236 }
237 
239 bool Geant4Action::return_error(bool return_value, const char* fmt, ...) const {
240  va_list args;
241  va_start(args, fmt);
242  dd4hep::printout(dd4hep::ERROR, m_name, fmt, args);
243  va_end(args);
244  return return_value;
245 }
246 
248 void Geant4Action::fatal(const char* fmt, ...) const {
249  va_list args;
250  va_start(args, fmt);
251  dd4hep::printout(dd4hep::FATAL, m_name, fmt, args);
252  va_end(args);
253 }
254 
256 void Geant4Action::except(const char* fmt, ...) const {
257  va_list args;
258  va_start(args, fmt);
259  std::string err = dd4hep::format(m_name, fmt, args);
260  dd4hep::printout(dd4hep::FATAL, m_name, err.c_str());
261  va_end(args);
262  throw std::runtime_error(err);
263 }
264 
266 void Geant4Action::abortRun(const std::string& exception, const char* fmt, ...) const {
267  std::string desc, typ = typeName(typeid(*this));
268  std::string issuer = name()+" ["+typ+"]";
269  va_list args;
270  va_start(args, fmt);
271  desc = dd4hep::format("*** Geant4Action:", fmt, args);
272  va_end(args);
273  G4Exception(issuer.c_str(),exception.c_str(),RunMustBeAborted,desc.c_str());
274  //throw std::runtime_error(issuer+"> "+desc);
275 }
276 
279  return m_context->kernel().runAction();
280 }
281 
284  return m_context->kernel().eventAction();
285 }
286 
289  return m_context->kernel().steppingAction();
290 }
291 
294  return m_context->kernel().trackingAction();
295 }
296 
299  return m_context->kernel().stackingAction();
300 }
301 
304  return m_context->kernel().generatorAction();
305 }
dd4hep::sim::Geant4Kernel::directoryName
const std::string & directoryName() const
Access the command directory.
Definition: Geant4Kernel.h:207
dd4hep::sim::Geant4Action::printP1
void printP1(const char *fmt,...) const
Support for messages with variable output level using output level+1.
Definition: Geant4Action.cpp:177
dd4hep::sim::Geant4Action::printP2
void printP2(const char *fmt,...) const
Support for messages with variable output level using output level+2.
Definition: Geant4Action.cpp:188
dd4hep::sim::Geant4Action::m_needsControl
bool m_needsControl
Default property: Flag to create control instance.
Definition: Geant4Action.h:123
dd4hep::Property
The property class to assign options to actions.
Definition: ComponentProperties.h:48
dd4hep::sim::Geant4Action::Geant4Action
Geant4Action(Geant4Context *context, const std::string &nam)
Standard constructor.
Definition: Geant4Action.cpp:54
dd4hep::sim::Geant4UIMessenger::exportProperties
void exportProperties(PropertyManager &mgr)
Export all properties to the Geant4 UI.
Definition: Geant4UIMessenger.cpp:78
dd4hep::sim::Geant4Action::control
Geant4UIMessenger * control() const
Access to the UI messenger.
Definition: Geant4Action.cpp:125
dd4hep::sim::Geant4StackingActionSequence
Concrete implementation of the Geant4 stacking action sequence.
Definition: Geant4StackingAction.h:128
dd4hep::sim::Geant4Action::installPropertyMessenger
virtual void installPropertyMessenger()
Install property control messenger if wanted.
Definition: Geant4Action.cpp:116
dd4hep::sim::Geant4ActionContainer::runAction
Geant4RunActionSequence * runAction(bool create)
Access run action sequence.
Definition: Geant4ActionContainer.cpp:92
dd4hep::sim::Geant4Action::configureFiber
virtual void configureFiber(Geant4Context *thread_context)
Set or update client for the use in a new thread fiber.
Definition: Geant4Action.cpp:140
dd4hep::sim::Geant4Action::setOutputLevel
PrintLevel setOutputLevel(PrintLevel new_level)
Set the output level; returns previous value.
Definition: Geant4Action.cpp:87
dd4hep::exception
void exception(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:69
dd4hep::sim::Geant4Action::generatorAction
Geant4GeneratorActionSequence & generatorAction() const
Access to the main generator action sequence from the kernel object.
Definition: Geant4Action.cpp:303
dd4hep::sim::Geant4Action::return_error
bool return_error(bool return_value, const char *fmt,...) const
Action to support error messages.
Definition: Geant4Action.cpp:239
dd4hep::sim::Geant4Action::m_context
Geant4Context * m_context
Reference to the Geant4 context.
Definition: Geant4Action.h:116
dd4hep::sim::Geant4ActionContainer::stackingAction
Geant4StackingActionSequence * stackingAction(bool create)
Access stacking action sequence.
Definition: Geant4ActionContainer.cpp:120
dd4hep::sim::Geant4ActionContainer::trackingAction
Geant4TrackingActionSequence * trackingAction(bool create)
Access tracking action sequence.
Definition: Geant4ActionContainer.cpp:113
dd4hep::sim::Geant4Action::property
Property & property(const std::string &name)
Access single property.
Definition: Geant4Action.cpp:99
dd4hep::sim::Geant4Action::abortRun
void abortRun(const std::string &exception, const char *fmt,...) const
Abort Geant4 Run by throwing a G4Exception with type RunMustBeAborted.
Definition: Geant4Action.cpp:266
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
dd4hep::sim::Geant4Action::m_name
std::string m_name
Action name.
Definition: Geant4Action.h:125
dd4hep::sim::Geant4Action::m_outputLevel
int m_outputLevel
Default property: Output level.
Definition: Geant4Action.h:121
dd4hep::sim::Geant4Action::warning
void warning(const char *fmt,...) const
Support of warning messages.
Definition: Geant4Action.cpp:223
dd4hep::sim::Geant4Action::m_properties
PropertyManager m_properties
Property pool.
Definition: Geant4Action.h:127
dd4hep::sim::Geant4Action::fatal
void fatal(const char *fmt,...) const
Support of fatal messages. Throws exception.
Definition: Geant4Action.cpp:248
dd4hep::PropertyManager::exists
bool exists(const std::string &name) const
Check for existence.
Definition: ComponentProperties.cpp:102
dd4hep::sim::Geant4TrackingActionSequence
Concrete implementation of the Geant4 tracking action sequence.
Definition: Geant4TrackingAction.h:114
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::getOutputLevel
PrintLevel getOutputLevel(const std::string object) const
Retrieve the global output level of a named object.
Definition: Geant4Kernel.cpp:308
dd4hep::sim::Geant4Action::eventAction
Geant4EventActionSequence & eventAction() const
Access to the main event action sequence from the kernel object.
Definition: Geant4Action.cpp:283
dd4hep::sim::Geant4GeneratorActionSequence
Concrete implementation of the Geant4 generator action sequence.
Definition: Geant4GeneratorAction.h:115
dd4hep::sim::Geant4Action::error
void error(const char *fmt,...) const
Support of error messages.
Definition: Geant4Action.cpp:231
dd4hep::sim::Geant4Action::m_refCount
long m_refCount
Reference count. Initial value: 1.
Definition: Geant4Action.h:129
dd4hep::sim::Geant4ActionContainer::eventAction
Geant4EventActionSequence * eventAction(bool create)
Access run action sequence.
Definition: Geant4ActionContainer.cpp:99
dd4hep::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
dd4hep::sim::Geant4RunActionSequence
Concrete basic implementation of the Geant4 run action sequencer.
Definition: Geant4RunAction.h:117
dd4hep::sim::Geant4Action::printM1
void printM1(const char *fmt,...) const
Support for messages with variable output level using output level-1.
Definition: Geant4Action.cpp:155
dd4hep::sim::Geant4Action
Default base class for all Geant 4 actions and derivates thereof.
Definition: Geant4Action.h:113
dd4hep::sim::Geant4Action::release
long release()
Decrease reference count. Implicit destruction.
Definition: Geant4Action.cpp:76
dd4hep::sim::Geant4Action::name
const std::string & name() const
Access name of the action.
Definition: Geant4Action.h:280
dd4hep::sim::Geant4Action::outputLevel
PrintLevel outputLevel() const
Access the output level.
Definition: Geant4Action.h:296
dd4hep::sim::TypeName::split
static TypeName split(const std::string &type_name)
Split string pair according to default delimiter ('/')
Definition: Geant4Action.cpp:40
dd4hep::sim::Geant4Action::print
void print(const char *fmt,...) const
Support for messages with variable output level using output level.
Definition: Geant4Action.cpp:144
dd4hep::sim::Geant4Action::properties
PropertyManager & properties()
Access to the properties of the object.
Definition: Geant4Action.h:292
dd4hep::sim::Geant4Action::runAction
Geant4RunActionSequence & runAction() const
Access to the main run action sequence from the kernel object.
Definition: Geant4Action.cpp:278
dd4hep::sim::Geant4Action::m_control
Geant4UIMessenger * m_control
Control directory of this action.
Definition: Geant4Action.h:118
dd4hep::sim::Geant4Action::addRef
long addRef()
Increase reference count.
Definition: Geant4Action.cpp:71
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::Geant4ActionContainer::steppingAction
Geant4SteppingActionSequence * steppingAction(bool create)
Access stepping action sequence.
Definition: Geant4ActionContainer.cpp:106
dd4hep::sim::Geant4Action::installCommandMessenger
virtual void installCommandMessenger()
Install command control messenger if wanted.
Definition: Geant4Action.cpp:121
dd4hep::sim::TypeName::TypeName
TypeName()=default
Default constructor.
dd4hep::sim::Geant4Action::debug
void debug(const char *fmt,...) const
Support of debug messages.
Definition: Geant4Action.cpp:207
dd4hep::sim::TypeName
Helper class to handle strings of the format "type/name".
Definition: Geant4Action.h:82
dd4hep::sim::Geant4Action::always
void always(const char *fmt,...) const
Support of always printed messages.
Definition: Geant4Action.cpp:199
dd4hep::sim::Geant4EventActionSequence
Concrete implementation of the Geant4 event action sequence.
Definition: Geant4EventAction.h:124
Geant4Kernel.h
dd4hep::sim::Geant4Action::enableUI
virtual void enableUI()
Enable and install UI messenger.
Definition: Geant4Action.cpp:134
dd4hep::sim::Geant4Action::hasProperty
bool hasProperty(const std::string &name) const
Check property for existence.
Definition: Geant4Action.cpp:94
InstanceCount.h
dd4hep::sim::Geant4SteppingActionSequence
Concrete implementation of the Geant4 stepping action sequence.
Definition: Geant4SteppingAction.h:104
dd4hep::sim::Geant4Action::~Geant4Action
virtual ~Geant4Action()
Default destructor.
Definition: Geant4Action.cpp:66
dd4hep::sim::Geant4Action::stackingAction
Geant4StackingActionSequence & stackingAction() const
Access to the main stacking action sequence from the kernel object.
Definition: Geant4Action.cpp:298
dd4hep::sim::Geant4Action::installMessengers
virtual void installMessengers()
Install property control messenger if wanted.
Definition: Geant4Action.cpp:104
Printout.h
Geant4Context.h
Geant4Action.h
dd4hep::sim::Geant4UIMessenger
Generic implementation to export properties and actions to the Geant4 command prompt.
Definition: Geant4UIMessenger.h:35
dd4hep::sim::Geant4Action::printM2
void printM2(const char *fmt,...) const
Support for messages with variable output level using output level-2.
Definition: Geant4Action.cpp:166
dd4hep::sim::Geant4Action::steppingAction
Geant4SteppingActionSequence & steppingAction() const
Access to the main stepping action sequence from the kernel object.
Definition: Geant4Action.cpp:288
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::Geant4ActionContainer::generatorAction
Geant4GeneratorActionSequence * generatorAction(bool create)
Access generator action sequence.
Definition: Geant4ActionContainer.cpp:85
dd4hep::sim::Geant4Action::trackingAction
Geant4TrackingActionSequence & trackingAction() const
Access to the main tracking action sequence from the kernel object.
Definition: Geant4Action.cpp:293