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
Geant4Kernel.h
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 #ifndef DD4HEP_DDG4_GEANT4KERNEL_H
15 #define DD4HEP_DDG4_GEANT4KERNEL_H
16 
17 // Framework include files
19 
20 // C/C++ include files
21 #include <map>
22 #include <typeinfo>
23 
24 // Forward declarations
25 class G4RunManager;
26 class G4UIdirectory;
27 
29 namespace DD4hep {
30 
32  namespace Simulation {
33 
34  // Forward declarations
35  class Geant4ActionPhase;
36 
38 
44  public:
46  typedef std::map<unsigned long, Geant4Kernel*> Workers;
47  typedef std::map<std::string, Geant4ActionPhase*> Phases;
48  typedef std::map<std::string, Geant4Action*> GlobalActions;
49  typedef std::map<std::string,int> ClientOutputLevels;
50 
51  protected:
53  G4RunManager* m_runManager;
55  G4UIdirectory* m_control;
57  G4TrackingManager* m_trackMgr;
62 
63 
73  std::string m_controlName;
75  std::string m_uiName;
77  long m_numEvent;
82 
84  //bool m_multiThreaded;
88  unsigned long m_id, m_ident;
93 
94  bool isMaster() const { return this == m_master; }
95  bool isWorker() const { return this != m_master; }
96 
97 #ifndef __CINT__
98  Geant4Kernel(Geant4Kernel* m, unsigned long identifier);
100 #endif
101 
102  public:
105 
107  Geant4Kernel& master() const { return *m_master; }
108 
110  Geant4Kernel& shared() const { return *m_shared; }
111 
112  //bool isMultiThreaded() const { return m_multiThreaded; }
113  bool isMultiThreaded() const { return m_numThreads > 0; }
114 
116  static unsigned long int thread_self();
117 
118  public:
119 
121 
127  public:
131  PhaseSelector(Geant4Kernel* kernel);
133  PhaseSelector(const PhaseSelector& c);
137  Geant4ActionPhase& operator[](const std::string& name) const;
138  } phase;
139 
140  enum State {
142  };
144  virtual ~Geant4Kernel();
145 
146 #ifndef __CINT__
147  static Geant4Kernel& instance(LCDD& lcdd);
149 #endif
150  const Phases& phases() const { return m_phases; }
153  LCDD& lcdd() const { return *m_lcdd; }
155  G4TrackingManager* trackMgr() const { return m_trackMgr; }
157  void setTrackMgr(G4TrackingManager* mgr) { m_trackMgr = mgr; }
159  const std::string& directoryName() const { return m_controlName; }
161  unsigned long id() const { return m_ident; }
163  G4RunManager& runManager();
164 
169  void printProperties() const;
171  template <typename T> Geant4Kernel& declareProperty(const std::string& nam, T& val);
173  template <typename T> Geant4Kernel& declareProperty(const char* nam, T& val);
175  bool hasProperty(const std::string& name) const;
177  Property& property(const std::string& name);
178 
180  PrintLevel outputLevel() const { return (PrintLevel)m_outputLevel; }
185  void setOutputLevel(const std::string object, PrintLevel new_level);
187  PrintLevel getOutputLevel(const std::string object) const;
188 
190 
194  Geant4Kernel& registerGlobalAction(Geant4Action* action);
196  Geant4Action* globalAction(const std::string& action_name, bool throw_if_not_present = true);
198 
202  Geant4Kernel& registerGlobalFilter(Geant4Action* filter);
204  Geant4Action* globalFilter(const std::string& filter_name, bool throw_if_not_present = true);
205 
207  Geant4ActionPhase* getPhase(const std::string& name);
208 
210  virtual Geant4ActionPhase* addSimplePhase(const std::string& name, bool throw_on_exist);
211 
213  virtual Geant4ActionPhase* addPhase(const std::string& name, const std::type_info& arg1, const std::type_info& arg2,
214  const std::type_info& arg3, bool throw_on_exist);
216  template <typename A0> Geant4ActionPhase* addPhase(const std::string& name, bool throw_on_exist = true) {
217  return addPhase(name, typeid(A0), typeid(void), typeid(void), throw_on_exist);
218  }
220  template <typename A0, typename A1>
221  Geant4ActionPhase* addPhase(const std::string& name, bool throw_on_exist = true) {
222  return addPhase(name, typeid(A0), typeid(A1), typeid(void), throw_on_exist);
223  }
225  template <typename A0, typename A1, typename A2>
226  Geant4ActionPhase* addPhase(const std::string& name, bool throw_on_exist = true) {
227  return addPhase(name, typeid(A0), typeid(A1), typeid(A2), throw_on_exist);
228  }
230  virtual bool removePhase(const std::string& name);
232  virtual void destroyPhases();
233 
235  virtual bool executePhase(const std::string& name, const void** args) const;
236 
238  virtual void loadGeometry(const std::string& compact_file);
240  virtual void loadXML(const char* fname);
241 
243  virtual Geant4Kernel& createWorker();
246  Geant4Kernel& worker(unsigned long thread_identifier, bool create_if=false);
248  int numWorkers() const;
249 
251  virtual int configure();
253  virtual int initialize();
255  virtual int run();
257  virtual int runEvents(int num_events);
259  virtual int terminate();
260  };
262  template <typename T> Geant4Kernel& Geant4Kernel::declareProperty(const std::string& nam, T& val) {
263  m_properties.add(nam, val);
264  return *this;
265  }
266 
268  template <typename T> Geant4Kernel& Geant4Kernel::declareProperty(const char* nam, T& val) {
269  m_properties.add(nam, val);
270  return *this;
271  }
272 
274  class Geant4Exec {
275  public:
277  static int configure(Geant4Kernel& kernel);
279  static int initialize(Geant4Kernel& kernel);
281  static int run(Geant4Kernel& kernel);
283  static int terminate(Geant4Kernel& kernel);
284  };
285 
286  } // End namespace Simulation
287 } // End namespace DD4hep
288 #endif // DD4HEP_DDG4_GEANT4KERNEL_H
void add(const std::string &name, const Property &property)
Add a new property.
virtual Geant4ActionPhase * addPhase(const std::string &name, const std::type_info &arg1, const std::type_info &arg2, const std::type_info &arg3, bool throw_on_exist)
Add a new phase to the phase.
void setTrackMgr(G4TrackingManager *mgr)
Access the tracking manager.
Definition: Geant4Kernel.h:157
The property class to assign options to actions.
PropertyManager & properties()
Access to the properties of the object.
Definition: Geant4Kernel.h:167
ClientOutputLevels m_clientLevels
Property: Client output levels.
Definition: Geant4Kernel.h:81
Geant4Kernel & worker(unsigned long thread_identifier, bool create_if=false)
Access worker instance by it's identifier.
static unsigned long int thread_self()
Access thread identifier.
Geant4Kernel & declareProperty(const std::string &nam, T &val)
Declare property.
Definition: Geant4Kernel.h:262
int numWorkers() const
Access number of workers.
GlobalActions m_globalFilters
Globally registered filters of sensitive detectors.
Definition: Geant4Kernel.h:71
std::map< std::string, int > ClientOutputLevels
Definition: Geant4Kernel.h:49
virtual Geant4ActionPhase * addSimplePhase(const std::string &name, bool throw_on_exist)
Add a new phase to the phase.
Geant4Kernel * m_kernel
Reference to embedding object.
Definition: Geant4Kernel.h:129
int m_outputLevel
Property: Output level.
Definition: Geant4Kernel.h:79
virtual int runEvents(int num_events)
Run the simulation: Simulate the number of events "num_events" and modify the property "NumEvents"...
Geant4ActionPhase & operator[](const std::string &name) const
Phase access to the map.
G4RunManager * m_runManager
Reference to the run manager.
Definition: Geant4Kernel.h:53
Geant4ActionPhase * addPhase(const std::string &name, bool throw_on_exist=true)
Add a new phase to the phase.
Definition: Geant4Kernel.h:216
GlobalActions m_globalActions
Globally registered actions.
Definition: Geant4Kernel.h:69
static int terminate(Geant4Kernel &kernel)
Terminate the application.
Definition: Geant4Exec.cpp:620
LCDD & lcdd() const
Access to detector description.
Definition: Geant4Kernel.h:153
const std::string & directoryName() const
Access the command directory.
Definition: Geant4Kernel.h:159
virtual int initialize()
Run the simulation: Initialize Geant4.
PrintLevel
Definition: Printout.h:47
std::map< unsigned long, Geant4Kernel * > Workers
Definition: Geant4Kernel.h:46
Property & property(const std::string &name)
Access single property.
std::string m_uiName
Property: Name of the UI action. Must be member of the global actions.
Definition: Geant4Kernel.h:75
Main executor steering the Geant4 execution.
Definition: Geant4Kernel.h:274
PhaseSelector(Geant4Kernel *kernel)
Standard constructor.
virtual int run()
Run the simulation: Simulate the number of events given by the property "NumEvents".
virtual Geant4Kernel & createWorker()
Create identified worker instance.
Geant4ActionPhase * getPhase(const std::string &name)
Access phase by name.
static int initialize(Geant4Kernel &kernel)
Initialize the application.
Definition: Geant4Exec.cpp:583
Geant4Kernel(Geant4Kernel *m, unsigned long identifier)
Standard constructor for workers.
Class, which allows all Geant4Action derivatives to access the DDG4 kernel structures.
Definition: Geant4Kernel.h:43
Geant4Kernel * m_master
Parent reference.
Definition: Geant4Kernel.h:90
virtual ~Geant4Kernel()
Default destructor.
Class, which allows all Geant4Action to be stored.
PropertyManager m_properties
Property pool.
Definition: Geant4Kernel.h:61
Geant4Kernel & registerGlobalAction(Geant4Action *action)
Register action by name to be retrieved when setting up and connecting action objects.
Geant4Kernel & master() const
Thread's master context.
Definition: Geant4Kernel.h:107
Embedded helper class to facilitate map access to the phases.
Definition: Geant4Kernel.h:126
Workers m_workers
Worker threads.
Definition: Geant4Kernel.h:67
PrintLevel setOutputLevel(PrintLevel new_level)
Set the global output level of the kernel object; returns previous value.
virtual void loadXML(const char *fname)
Load XML file.
bool hasProperty(const std::string &name) const
Check property for existence.
Geant4ActionPhase * addPhase(const std::string &name, bool throw_on_exist=true)
Add a new phase to the phase.
Definition: Geant4Kernel.h:226
PrintLevel outputLevel() const
Access the output level.
Definition: Geant4Kernel.h:181
virtual int terminate()
Run the simulation: Terminate Geant4.
int m_numThreads
Property: Running in multi threaded context.
Definition: Geant4Kernel.h:86
virtual void destroyPhases()
Destroy all phases. To be called only at shutdown.
G4TrackingManager * m_trackMgr
Reference to Geant4 track manager.
Definition: Geant4Kernel.h:57
DD4hep::Geometry::LCDD LCDD
Definition: Geant4Kernel.h:45
std::string m_controlName
Property: Name of the G4UI command tree.
Definition: Geant4Kernel.h:73
LCDD * m_lcdd
Detector description object.
Definition: Geant4Kernel.h:59
Phases m_phases
Action phases.
Definition: Geant4Kernel.h:65
virtual bool executePhase(const std::string &name, const void **args) const
Execute phase action if it exists.
PhaseSelector & operator=(const PhaseSelector &c)
Assignment operator.
G4RunManager & runManager()
Access to the Geant4 run manager.
virtual void loadGeometry(const std::string &compact_file)
Construct detector geometry using lcdd plugin.
static int configure(Geant4Kernel &kernel)
Configure the application.
Definition: Geant4Exec.cpp:514
Geant4Action * globalFilter(const std::string &filter_name, bool throw_if_not_present=true)
Retrieve filter from repository.
class DD4hep::Simulation::Geant4Kernel::PhaseSelector phase
Generic context to extend user, run and event information.
Action phase definition. Client callback at various stage of the simulation processing.
G4TrackingManager * trackMgr() const
Access the tracking manager.
Definition: Geant4Kernel.h:155
long m_numEvent
Property: Number of events to be executed in batch mode.
Definition: Geant4Kernel.h:77
The main interface to the DD4hep detector description package.
Definition: LCDD.h:82
Geant4Kernel & shared() const
Shared action context.
Definition: Geant4Kernel.h:110
unsigned long m_id
Flag: Master instance (id<0) or worker (id >= 0)
Definition: Geant4Kernel.h:88
unsigned long id() const
Access worker identifier.
Definition: Geant4Kernel.h:161
const Phases & phases() const
Access phase phases.
Definition: Geant4Kernel.h:151
std::map< std::string, Geant4ActionPhase * > Phases
Definition: Geant4Kernel.h:47
virtual int configure()
Run the simulation: Configure Geant4.
static int run(Geant4Kernel &kernel)
Run the application and simulate events.
Definition: Geant4Exec.cpp:595
virtual bool removePhase(const std::string &name)
Remove an existing phase from the phase. If not existing returns false.
std::map< std::string, Geant4Action * > GlobalActions
Definition: Geant4Kernel.h:48
Geant4Action * globalAction(const std::string &action_name, bool throw_if_not_present=true)
Retrieve action from repository.
static Geant4Kernel & instance(LCDD &lcdd)
Instance accessor.
G4UIdirectory * m_control
Top level control directory.
Definition: Geant4Kernel.h:55
Manager to ease the handling of groups of properties.
void printProperties() const
Print the property values.
TGeoShape TGeoMedium * m
Definition: Volumes.cpp:294
Geant4Kernel & registerGlobalFilter(Geant4Action *filter)
Register filter by name to be retrieved when setting up and connecting filter objects.
Geant4ActionPhase * addPhase(const std::string &name, bool throw_on_exist=true)
Add a new phase to the phase.
Definition: Geant4Kernel.h:221
PrintLevel getOutputLevel(const std::string object) const
Retrieve the global output level of a named object.