DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Geant4Action.h
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation for LCD
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 #ifndef DD4HEP_DDG4_GEANT4ACTION_H
15 #define DD4HEP_DDG4_GEANT4ACTION_H
16 
17 // Framework include files
18 #include "DD4hep/Printout.h"
20 #include "DDG4/Geant4Context.h"
21 #include "DDG4/Geant4Callback.h"
22 
23 // Geant4 forward declarations
24 class G4Run;
25 class G4Event;
26 class G4Step;
27 class G4Track;
28 class G4TrackStack;
29 class G4EventGenerator;
30 class G4VTrajectory;
31 class G4TrackingManager;
32 class G4UIdirectory;
33 
34 // C/C++ include files
35 #include <string>
36 #include <cstdarg>
37 
39 namespace DD4hep {
40 
42  namespace Simulation {
43 
44  // Forward declarations
45  class Geant4UIMessenger;
46 
48  template <typename TO, typename FROM> TO fast_cast(FROM from) {
49 #ifdef USE_FASTCAST
50  return static_cast<TO>(from);
51 #else
52  return dynamic_cast<TO>(from);
53 #endif
54  }
55 
57 
62  class TypeName : public std::pair<std::string, std::string> {
63  public:
65  TypeName() = default;
67  TypeName(const TypeName& copy) = default;
69  TypeName(const std::pair<std::string, std::string>& c)
70  : std::pair<std::string, std::string>(c) { }
72  TypeName(const std::string& typ, const std::string& nam)
73  : std::pair<std::string, std::string>(typ, nam) { }
75  TypeName& operator=(const TypeName& copy) = default;
77  static TypeName split(const std::string& type_name);
79  static TypeName split(const std::string& type_name, const std::string& delim);
80  };
81 
83 
91  class Geant4Action {
92  protected:
97 
99  int m_outputLevel = 3;
101  bool m_needsControl = false;
103  std::string m_name;
107  long m_refCount = 1;
108 
109  public:
111 
116  class ContextSwap {
120  public:
123  context = action->context();
124  action->updateContext(c);
125  }
129  }
130  };
131 
132  protected:
133 
135  struct FindByName {
136  std::string _n;
137  FindByName(const std::string& n) : _n(n) {}
138  bool operator()(const Geant4Action* a) { return a->name() == _n; }
139  };
141 
146  template <typename T> class Actors {
147  public:
148  typedef typename std::vector<T*> _V;
150  Actors() = default;
151  ~Actors() = default;
152  void clear() { m_v.clear(); }
153  void add(T* obj) { m_v.push_back(obj); }
154  void add_front(T* obj) { m_v.insert(m_v.begin(), obj); }
155  operator const _V&() const { return m_v; }
156  operator _V&() { return m_v; }
157  const _V* operator->() const { return &m_v; }
158  _V* operator->() { return &m_v; }
159  typename _V::iterator begin() { return m_v.begin(); }
160  typename _V::iterator end() { return m_v.end(); }
161  typename _V::const_iterator begin() const { return m_v.begin(); }
162  typename _V::const_iterator end() const { return m_v.end(); }
163 
166  (*this)(&T::updateContext,ctxt);
167  }
169  template <typename F> typename _V::value_type get(const F& f) const {
170  if (!m_v.empty()) {
171  typename _V::const_iterator i=std::find_if(m_v.begin(),m_v.end(),f);
172  return i==m_v.end() ? 0 : (*i);
173  }
174  return 0;
175  }
177  template <typename R, typename Q> void operator()(R (Q::*pmf)()) {
178  if (m_v.empty())
179  return;
180  for (typename _V::iterator i = m_v.begin(); i != m_v.end(); ++i)
181  ((*i)->*pmf)();
182  }
183  template <typename R, typename Q, typename A0> void operator()(R (Q::*pmf)(A0), A0 a0) {
184  if (m_v.empty())
185  return;
186  for (typename _V::iterator i = m_v.begin(); i != m_v.end(); ++i)
187  ((*i)->*pmf)(a0);
188  }
189  template <typename R, typename Q, typename A0, typename A1> void operator()(R (Q::*pmf)(A0, A1), A0 a0, A1 a1) {
190  if (m_v.empty())
191  return;
192  for (typename _V::iterator i = m_v.begin(); i != m_v.end(); ++i)
193  ((*i)->*pmf)(a0, a1);
194  }
196  template <typename R, typename Q> void operator()(R (Q::*pmf)() const) const {
197  if (m_v.empty())
198  return;
199  for (typename _V::const_iterator i = m_v.begin(); i != m_v.end(); ++i)
200  ((*i)->*pmf)();
201  }
202  template <typename R, typename Q, typename A0> void operator()(R (Q::*pmf)(A0) const, A0 a0) const {
203  if (m_v.empty())
204  return;
205  for (typename _V::const_iterator i = m_v.begin(); i != m_v.end(); ++i)
206  ((*i)->*pmf)(a0);
207  }
208  template <typename R, typename Q, typename A0, typename A1> void operator()(R (Q::*pmf)(A0, A1) const, A0 a0,
209  A1 a1) const {
210  if (m_v.empty())
211  return;
212  for (typename _V::const_iterator i = m_v.begin(); i != m_v.end(); ++i)
213  ((*i)->*pmf)(a0, a1);
214  }
216  template <typename Q> bool filter(bool (Q::*pmf)() const) const {
217  if (!m_v.empty())
218  return true;
219  for (typename _V::const_iterator i = m_v.begin(); i != m_v.end(); ++i)
220  if (!((*i)->*pmf)())
221  return false;
222  return true;
223  }
224  template <typename Q, typename A0> bool filter(bool (Q::*pmf)(A0) const, A0 a0) const {
225  if (m_v.empty())
226  return true;
227  for (typename _V::const_iterator i = m_v.begin(); i != m_v.end(); ++i)
228  if (!((*i)->*pmf)(a0))
229  return false;
230  return true;
231  }
232  template <typename Q, typename A0, typename A1> bool filter(bool (Q::*pmf)(A0, A1) const, A0 a0, A1 a1) const {
233  if (m_v.empty())
234  return true;
235  for (typename _V::const_iterator i = m_v.begin(); i != m_v.end(); ++i)
236  if (!((*i)->*pmf)(a0, a1))
237  return false;
238  return true;
239  }
240  };
241 
242  protected:
244  Geant4Action() = default;
246  Geant4Action(const Geant4Action& copy) = delete;
248  Geant4Action& operator=(const Geant4Action& copy) = delete;
249 
251  virtual ~Geant4Action();
252 
253  public:
255  Geant4Action(Geant4Context* context, const std::string& nam);
257  long addRef();
259  long release();
262  return m_context;
263  }
265  virtual void updateContext(Geant4Context* ctxt) {
266  m_context = ctxt;
267  }
269  virtual void configureFiber(Geant4Context* thread_context);
271  const std::string& name() const {
272  return m_name;
273  }
275  const char* c_name() const {
276  return m_name.c_str();
277  }
279  void setName(const std::string& new_name) {
280  m_name = new_name;
281  }
284  return m_properties;
285  }
288  return (PrintLevel)m_outputLevel;
289  }
293  Geant4UIMessenger* control() const;
295  virtual void enableUI();
297  template <typename T> Geant4Action& declareProperty(const std::string& nam, T& val);
299  template <typename T> Geant4Action& declareProperty(const char* nam, T& val);
301  bool hasProperty(const std::string& name) const;
303  Property& property(const std::string& name);
306 
308  virtual void installMessengers();
310  virtual void installCommandMessenger();
312  virtual void installPropertyMessenger();
313 
315  void print(const char* fmt, ...) const;
317  void printM1(const char* fmt, ...) const;
319  void printM2(const char* fmt, ...) const;
321  void printP1(const char* fmt, ...) const;
323  void printP2(const char* fmt, ...) const;
324 
326  void debug(const char* fmt, ...) const;
328  void info(const char* fmt, ...) const;
330  void warning(const char* fmt, ...) const;
332  void error(const char* fmt, ...) const;
334  bool return_error(bool return_value, const char* fmt, ...) const;
336  void fatal(const char* fmt, ...) const;
338  void except(const char* fmt, ...) const;
339 
341  void abortRun(const std::string& exception, const char* fmt, ...) const;
342 
355  };
356 
358  template <typename T> Geant4Action& Geant4Action::declareProperty(const std::string& nam, T& val) {
359  m_properties.add(nam, val);
360  return *this;
361  }
362 
364  template <typename T> Geant4Action& Geant4Action::declareProperty(const char* nam, T& val) {
365  m_properties.add(nam, val);
366  return *this;
367  }
368  } // End namespace Simulation
369 } // End namespace DD4hep
370 
371 #endif // DD4HEP_DDG4_GEANT4ACTION_H
Functor to access elements by name.
Definition: Geant4Action.h:135
void add(const std::string &name, const Property &property)
Add a new property.
virtual void enableUI()
Enable and install UI messenger.
PrintLevel outputLevel() const
Access the output level.
Definition: Geant4Action.h:287
long m_refCount
Reference count. Initial value: 1.
Definition: Geant4Action.h:107
The property class to assign options to actions.
void printM1(const char *fmt,...) const
Support for messages with variable output level using output level-1.
Concrete implementation of the Geant4 stepping action sequence.
long release()
Decrease reference count. Implicit destruction.
bool filter(bool(Q::*pmf)(A0) const, A0 a0) const
Definition: Geant4Action.h:224
_V::const_iterator end() const
Definition: Geant4Action.h:162
Geant4TrackingActionSequence & trackingAction() const
Access to the main tracking action sequence from the kernel object.
TypeName(const std::pair< std::string, std::string > &c)
Copy constructor from pair.
Definition: Geant4Action.h:69
void operator()(R(Q::*pmf)(A0, A1), A0 a0, A1 a1)
Definition: Geant4Action.h:189
Geant4Context * context
reference to the context;
Definition: Geant4Action.h:118
void operator()(R(Q::*pmf)(A0) const, A0 a0) const
Definition: Geant4Action.h:202
Helper class to handle strings of the format "type/name".
Definition: Geant4Action.h:62
Geant4GeneratorActionSequence & generatorAction() const
Access to the main generator action sequence from the kernel object.
virtual void installPropertyMessenger()
Install property control messenger if wanted.
Geant4UIMessenger * control() const
Access to the UI messenger.
void copy(Alignment from, Alignment to)
Copy alignment object from source object.
void warning(const char *fmt,...) const
Support of warning messages.
Concrete implementation of the Geant4 event action sequence.
ContextSwap(Geant4Action *a, Geant4Context *c)
Constructor.
Definition: Geant4Action.h:122
void error(const char *fmt,...) const
Support of error messages.
PrintLevel
Definition: Printout.h:47
const char * c_name() const
Access name of the action.
Definition: Geant4Action.h:275
Geant4Context * context() const
Access the context.
Definition: Geant4Action.h:261
Concrete implementation of the Geant4 tracking action sequence.
TO fast_cast(FROM from)
Cast operator.
Definition: Geant4Action.h:48
void printP2(const char *fmt,...) const
Support for messages with variable output level using output level+2.
const std::string & name() const
Access name of the action.
Definition: Geant4Action.h:271
Geant4Context * m_context
Reference to the Geant4 context.
Definition: Geant4Action.h:94
void print(const char *fmt,...) const
Support for messages with variable output level using output level.
void except(const char *fmt,...) const
Support of exceptions: Print fatal message and throw runtime_error.
int m_outputLevel
Default property: Output level.
Definition: Geant4Action.h:99
Concrete implementation of the Geant4 stacking action sequence.
PropertyManager & properties()
Access to the properties of the object.
Definition: Geant4Action.h:283
Geant4Action & setProperties(PropertyConfigurator &setup)
Set object properties.
bool hasProperty(const std::string &name) const
Check property for existence.
void setName(const std::string &new_name)
Set the object name.
Definition: Geant4Action.h:279
virtual void updateContext(Geant4Context *ctxt)
Set or update client context.
Definition: Geant4Action.h:265
virtual void configureFiber(Geant4Context *thread_context)
Set or update client for the use in a new thread fiber.
long addRef()
Increase reference count.
Property & property(const std::string &name)
Access single property.
Geant4StackingActionSequence & stackingAction() const
Access to the main stacking action sequence from the kernel object.
Geant4SteppingActionSequence & steppingAction() const
Access to the main stepping action sequence from the kernel object.
bool return_error(bool return_value, const char *fmt,...) const
Action to support error messages.
virtual void installCommandMessenger()
Install command control messenger if wanted.
virtual ~Geant4Action()
Default destructor.
bool operator()(const Geant4Action *a)
Definition: Geant4Action.h:138
Concrete implementation of the Geant4 generator action sequence.
bool m_needsControl
Default property: Flag to create control instance.
Definition: Geant4Action.h:101
Concrete basic implementation of the Geant4 run action sequencer.
Geant4Action & operator=(const Geant4Action &copy)=delete
Inhibit assignment operator.
Heler class to configure properties.
Functor to update the context of a Geant4Action object.
Definition: Geant4Action.h:116
PrintLevel setOutputLevel(PrintLevel new_level)
Set the output level; returns previous value.
static TypeName split(const std::string &type_name)
Split string pair according to default delimiter ('/')
Geant4Action()=default
Inhibit default constructor.
virtual void installMessengers()
Install property control messenger if wanted.
Geant4RunActionSequence & runAction() const
Access to the main run action sequence from the kernel object.
void operator()(R(Q::*pmf)() const) const
CONST actions.
Definition: Geant4Action.h:196
TypeName & operator=(const TypeName &copy)=default
Assignment operator.
void fatal(const char *fmt,...) const
Support of fatal messages. Throws exception.
Generic context to extend user, run and event information.
TypeName(const std::string &typ, const std::string &nam)
Initializing constructor.
Definition: Geant4Action.h:72
PropertyManager m_properties
Property pool.
Definition: Geant4Action.h:105
std::string m_name
Action name.
Definition: Geant4Action.h:103
void abortRun(const std::string &exception, const char *fmt,...) const
Abort Geant4 Run by throwing a G4Exception with type RunMustBeAborted.
void info(const char *fmt,...) const
Support of info messages.
void operator()(R(Q::*pmf)(A0), A0 a0)
Definition: Geant4Action.h:183
Geant4EventActionSequence & eventAction() const
Access to the main event action sequence from the kernel object.
void operator()(R(Q::*pmf)())
NON-CONST actions.
Definition: Geant4Action.h:177
Generic implementation to export properties and actions to the Geant4 command prompt.
void operator()(R(Q::*pmf)(A0, A1) const, A0 a0, A1 a1) const
Definition: Geant4Action.h:208
bool filter(bool(Q::*pmf)(A0, A1) const, A0 a0, A1 a1) const
Definition: Geant4Action.h:232
void updateContext(Geant4Context *ctxt)
Context updates.
Definition: Geant4Action.h:165
Geant4Action & declareProperty(const std::string &nam, T &val)
Declare property.
Definition: Geant4Action.h:358
Manager to ease the handling of groups of properties.
_V::const_iterator begin() const
Definition: Geant4Action.h:161
Default base class for all Geant 4 actions and derivates thereof.
Definition: Geant4Action.h:91
void printP1(const char *fmt,...) const
Support for messages with variable output level using output level+1.
void debug(const char *fmt,...) const
Support of debug messages.
bool filter(bool(Q::*pmf)() const) const
CONST filters.
Definition: Geant4Action.h:216
Geant4UIMessenger * m_control
Control directory of this action.
Definition: Geant4Action.h:96
TypeName()=default
Default constructor.
void printM2(const char *fmt,...) const
Support for messages with variable output level using output level-2.
Actor class to manipulate action groups.
Definition: Geant4Action.h:146