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.cpp
Go to the documentation of this file.
1 // $Id: $
2 //==========================================================================
3 // AIDA Detector description implementation for LCD
4 //--------------------------------------------------------------------------
5 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
6 // All rights reserved.
7 //
8 // For the licensing terms see $DD4hepINSTALL/LICENSE.
9 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
10 //
11 // Author : M.Frank
12 //
13 //==========================================================================
14 
15 // Framework include files
16 #include "DD4hep/Printout.h"
17 #include "DD4hep/InstanceCount.h"
18 #include "DDG4/Geant4Context.h"
19 #include "DDG4/Geant4Action.h"
20 #include "DDG4/Geant4Kernel.h"
21 #include "DDG4/Geant4UIMessenger.h"
22 
23 // Geant4 include files
24 #include "G4UIdirectory.hh"
25 
26 // C/C++ include files
27 #include <algorithm>
28 
29 using namespace std;
30 using namespace DD4hep;
31 using namespace DD4hep::Simulation;
32 
33 TypeName TypeName::split(const string& type_name, const string& delim) {
34  size_t idx = type_name.find(delim);
35  string typ = type_name, nam = type_name;
36  if (idx != string::npos) {
37  typ = type_name.substr(0, idx);
38  nam = type_name.substr(idx + 1);
39  }
40  return TypeName(typ, nam);
41 }
42 
43 TypeName TypeName::split(const string& type_name) {
44  return split(type_name,"/");
45 }
46 #if 0
47 void Geant4Action::ContextUpdate::operator()(Geant4Action* action) const {
48  action->m_context = context;
49  if ( 0 == action->m_context ) {
50 
51  cout << "EERIOR" << endl;
52 
53  }
54 }
55 #endif
56 Geant4Action::Geant4Action(Geant4Context* ctxt, const string& nam)
58  : m_context(ctxt), m_control(0), m_outputLevel(INFO), m_needsControl(false), m_name(nam),
59  m_refCount(1)
60 {
62  m_outputLevel = ctxt ? ctxt->kernel().getOutputLevel(nam) : (printLevel()-1);
63  declareProperty("Name", m_name);
64  declareProperty("name", m_name);
65  declareProperty("OutputLevel", m_outputLevel);
66  declareProperty("Control", m_needsControl);
67 }
68 
72 }
73 
76  return ++m_refCount;
77 }
78 
81  long count = --m_refCount;
82  if (m_refCount <= 0) {
83  printM1("Geant4Action: Deleting object %s of type %s Pointer:%p",
84  m_name.c_str(),typeName(typeid(*this)).c_str(),(void*)this);
85  delete this;
86  }
87  return count;
88 }
89 
92  int old = m_outputLevel;
93  m_outputLevel = new_level;
94  return (PrintLevel)old;
95 }
96 
99  m_properties.set(m_name, setup);
100  return *this;
101 }
102 
104 bool Geant4Action::hasProperty(const string& nam) const {
105  return m_properties.exists(nam);
106 }
107 
109 Property& Geant4Action::property(const string& nam) {
110  return properties()[nam];
111 }
112 
115  //m_needsControl = true;
116  if (m_needsControl && !m_control) {
117  string path = context()->kernel().directoryName();
118  path += name() + "/";
119  m_control = new Geant4UIMessenger(name(), path);
122  }
123 }
124 
128 }
129 
132 }
133 
136  if ( m_control ) {
137  return m_control;
138  }
139  except("No control was installed for this action item.");
140  return 0;
141 }
142 
145  m_needsControl = true;
147 }
148 
150 void Geant4Action::configureFiber(Geant4Context* /* thread_context */) {
151 }
152 
154 void Geant4Action::print(const char* fmt, ...) const {
155  int level = max(int(outputLevel()),(int)VERBOSE);
156  if ( level >= printLevel() ) {
157  va_list args;
158  va_start(args, fmt);
159  DD4hep::printout((PrintLevel)level, m_name.c_str(), fmt, args);
160  va_end(args);
161  }
162 }
163 
165 void Geant4Action::printM1(const char* fmt, ...) const {
166  int level = max(outputLevel()-1,(int)VERBOSE);
167  if ( level >= printLevel() ) {
168  va_list args;
169  va_start(args, fmt);
170  DD4hep::printout((PrintLevel)level, m_name.c_str(), fmt, args);
171  va_end(args);
172  }
173 }
174 
176 void Geant4Action::printM2(const char* fmt, ...) const {
177  int level = max(outputLevel()-2,(int)VERBOSE);
178  if ( level >= printLevel() ) {
179  va_list args;
180  va_start(args, fmt);
181  DD4hep::printout((PrintLevel)level, m_name.c_str(), fmt, args);
182  va_end(args);
183  }
184 }
185 
187 void Geant4Action::printP1(const char* fmt, ...) const {
188  int level = min(outputLevel()+1,(int)FATAL);
189  if ( level >= printLevel() ) {
190  va_list args;
191  va_start(args, fmt);
192  DD4hep::printout((PrintLevel)level, m_name.c_str(), fmt, args);
193  va_end(args);
194  }
195 }
196 
198 void Geant4Action::printP2(const char* fmt, ...) const {
199  int level = min(outputLevel()+2,(int)FATAL);
200  if ( level >= printLevel() ) {
201  va_list args;
202  va_start(args, fmt);
203  DD4hep::printout((PrintLevel)level, m_name.c_str(), fmt, args);
204  va_end(args);
205  }
206 }
207 
209 void Geant4Action::debug(const char* fmt, ...) const {
210  va_list args;
211  va_start(args, fmt);
213  va_end(args);
214 }
215 
217 void Geant4Action::info(const char* fmt, ...) const {
218  va_list args;
219  va_start(args, fmt);
220  DD4hep::printout(DD4hep::INFO, m_name, fmt, args);
221  va_end(args);
222 }
223 
225 void Geant4Action::warning(const char* fmt, ...) const {
226  va_list args;
227  va_start(args, fmt);
229  va_end(args);
230 }
231 
233 void Geant4Action::error(const char* fmt, ...) const {
234  va_list args;
235  va_start(args, fmt);
237  va_end(args);
238 }
239 
241 bool Geant4Action::return_error(bool return_value, const char* fmt, ...) const {
242  va_list args;
243  va_start(args, fmt);
245  va_end(args);
246  return return_value;
247 }
248 
250 void Geant4Action::fatal(const char* fmt, ...) const {
251  va_list args;
252  va_start(args, fmt);
254  va_end(args);
255 }
256 
258 void Geant4Action::except(const char* fmt, ...) const {
259  va_list args;
260  va_start(args, fmt);
262  string err = DD4hep::format(m_name, fmt, args);
263  va_end(args);
264  throw runtime_error(err);
265 }
266 
268 void Geant4Action::abortRun(const string& exception, const char* fmt, ...) const {
269  string desc, typ = typeName(typeid(*this));
270  string issuer = name()+" ["+typ+"]";
271  va_list args;
272  va_start(args, fmt);
273  desc = DD4hep::format("*** Geant4Action:", fmt, args);
274  va_end(args);
275  G4Exception(issuer.c_str(),exception.c_str(),RunMustBeAborted,desc.c_str());
276  //throw runtime_error(issuer+"> "+desc);
277 }
278 
281  return m_context->kernel().runAction();
282 }
283 
286  return m_context->kernel().eventAction();
287 }
288 
291  return m_context->kernel().steppingAction();
292 }
293 
296  return m_context->kernel().trackingAction();
297 }
298 
301  return m_context->kernel().stackingAction();
302 }
303 
306  return m_context->kernel().generatorAction();
307 }
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.
Geant4TrackingActionSequence & trackingAction() const
Access to the main tracking action sequence from the kernel object.
Geant4Kernel & kernel() const
Access to the kernel object.
Geant4SteppingActionSequence * steppingAction(bool create)
Access stepping action sequence.
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.
Geant4TrackingActionSequence * trackingAction(bool create)
Access tracking action sequence.
static void decrement(T *)
Decrement count according to type information.
void exportProperties(PropertyManager &mgr)
Export all properties to the Geant4 UI.
virtual void installPropertyMessenger()
Install property control messenger if wanted.
std::string typeName(const std::type_info &type)
ABI information about type names.
Definition: Primitives.cpp:186
Geant4UIMessenger * control() const
Access to the UI messenger.
Geant4StackingActionSequence * stackingAction(bool create)
Access stacking action sequence.
void warning(const char *fmt,...) const
Support of warning messages.
const std::string & directoryName() const
Access the command directory.
Definition: Geant4Kernel.h:159
Concrete implementation of the Geant4 event action sequence.
PrintLevel printLevel()
Access the current printer level.
Definition: Printout.cpp:323
void error(const char *fmt,...) const
Support of error messages.
void set(const std::string &component_name, PropertyConfigurator &setup)
Bulk set of all properties.
PrintLevel
Definition: Printout.h:47
Geant4Context * context() const
Access the context.
Definition: Geant4Action.h:261
Concrete implementation of the Geant4 tracking action sequence.
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.
std::string format(const std::string &src, const std::string &fmt,...)
Build formatted string.
Definition: Printout.cpp:267
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.
virtual void configureFiber(Geant4Context *thread_context)
Set or update client for the use in a new thread fiber.
Geant4EventActionSequence * eventAction(bool create)
Access run action sequence.
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.
Geant4GeneratorActionSequence * generatorAction(bool create)
Access generator action sequence.
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.
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
Heler class to configure properties.
bool exists(const std::string &name) const
Check for existence.
PrintLevel setOutputLevel(PrintLevel new_level)
Set the output level; returns previous value.
virtual void installMessengers()
Install property control messenger if wanted.
Geant4RunActionSequence & runAction() const
Access to the main run action sequence from the kernel object.
void fatal(const char *fmt,...) const
Support of fatal messages. Throws exception.
Generic context to extend user, run and event information.
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.
Geant4EventActionSequence & eventAction() const
Access to the main event action sequence from the kernel object.
Generic implementation to export properties and actions to the Geant4 command prompt.
int printout(PrintLevel severity, const char *src, const char *fmt,...)
Calls the display action with a given severity level.
Definition: Printout.cpp:111
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.
Geant4RunActionSequence * runAction(bool create)
Access run action sequence.
void debug(const char *fmt,...) const
Support of debug messages.
Geant4UIMessenger * m_control
Control directory of this action.
Definition: Geant4Action.h:96
void printM2(const char *fmt,...) const
Support for messages with variable output level using output level-2.
PrintLevel getOutputLevel(const std::string object) const
Retrieve the global output level of a named object.