27 #ifdef G4MULTITHREADED
28 #include "G4MTRunManager.hh"
30 #include "G4RunManager.hh"
32 #include "G4UIdirectory.hh"
33 #include "G4Threading.hh"
34 #include "G4AutoLock.hh"
43 using namespace DD4hep::Simulation;
46 G4Mutex kernel_mutex=G4MUTEX_INITIALIZER;
57 : m_kernel(c.m_kernel) {
74 throw runtime_error(
format(
"Geant4Kernel",
"Attempt to access the nonexisting phase '%s'", nam.c_str()));
91 m_control->SetGuidance(
"Control for named Geant4 actions");
100 m_numThreads(1), m_id(ident), m_master(m), m_shared(0), phase(this)
111 m_control->SetGuidance(
"Control for thread specific Geant4 actions");
118 if (
this == s_main_instance.get() ) {
119 s_main_instance.release();
143 if ( 0 == s_main_instance.get() ) {
144 G4AutoLock protection_lock(&kernel_mutex); {
145 if ( 0 == s_main_instance.get() ) {
150 return *(s_main_instance.get());
155 unsigned long int thr_id = (
unsigned long int)::pthread_self();
165 printout(
INFO,
"Geant4Kernel",
"+++ Created worker instance id=%ul",identifier);
168 throw runtime_error(
format(
"Geant4Kernel",
"DDG4: Only the master instance may create workers."));
173 Workers::iterator i =
m_workers.find(identifier);
175 return *((*i).second);
177 else if ( identifier ==
m_id ) {
182 if ( identifier ==
self ) {
186 else if ( create_if ) {
189 throw runtime_error(
format(
"Geant4Kernel",
"DDG4: The Kernel object 0x%p does not exists!",(
void*)identifier));
203 printout(
ALWAYS,
"Geant4Kernel",
"OutputLevel[%s]: %d",(*i).first.c_str(),(*i).second);
242 #ifdef G4MULTITHREADED
245 G4MTRunManager* run_mgr =
new G4MTRunManager;
253 "but not supported by this compilation of Geant4.");
254 printout(
WARNING,
"Geant4Kernel",
"+++ Falling back to single threaded mode.");
259 throw runtime_error(
format(
"Geant4Kernel",
260 "DDG4: Only the master thread may instantiate "
261 "a G4RunManager object!"));
266 char* arg = (
char*) compact_file.c_str();
273 const char* args[] = { fname, 0 };
274 m_lcdd->
apply(
"DD4hepXMLLoader", 1, (
char**) args);
289 catch(
const exception&
e) {
290 printout(
FATAL,
"Geant4Kernel",
"+++ Exception while simulating:%s",e.what());
293 printout(
FATAL,
"Geant4Kernel",
"+++ UNKNOWN exception while simulating.");
305 printout(
INFO,
"Geant4Kernel",
"++ Terminate Geant4 and delete associated actions.");
316 if ( ptr ==
this &&
m_lcdd ) {
331 string nam = action->
name();
336 printout(
INFO,
"Geant4Kernel",
"++ Registered global action %s of type %s",
337 nam.c_str(),
typeName(
typeid(*action)).c_str());
340 throw runtime_error(
format(
"Geant4Kernel",
"DDG4: The action '%s' is already globally "
341 "registered. [Action-Already-Registered]", nam.c_str()));
343 throw runtime_error(
format(
"Geant4Kernel",
"DDG4: Attempt to globally register an invalid "
344 "action. [Action-Invalid]"));
351 if (throw_if_not_present) {
352 throw runtime_error(
format(
"Geant4Kernel",
"DDG4: The action '%s' is not globally "
353 "registered. [Action-Missing]", action_name.c_str()));
367 string nam = filter->
name();
374 throw runtime_error(
format(
"Geant4Kernel",
"DDG4: The filter '%s' is already globally "
375 "registered. [Filter-Already-Registered]", nam.c_str()));
377 throw runtime_error(
format(
"Geant4Kernel",
"DDG4: Attempt to globally register an invalid "
378 "filter. [Filter-Invalid]"));
385 if (throw_if_not_present) {
386 throw runtime_error(
format(
"Geant4Kernel",
"DDG4: The filter '%s' is not already globally "
387 "registered. [Filter-Missing]", filter_name.c_str()));
396 Phases::const_iterator i =
m_phases.find(nam);
398 (*i).second->execute(arguments);
406 Phases::const_iterator i =
m_phases.find(nam);
410 throw runtime_error(
format(
"Geant4Kernel",
"DDG4: The Geant4 action phase '%s' "
411 "does not exist. [No-Entry]", nam.c_str()));
416 return addPhase(name,
typeid(
void),
typeid(
void),
typeid(
void),throw_on_exist);
421 const type_info& arg2,
bool throw_on_exist) {
422 Phases::const_iterator i =
m_phases.find(nam);
428 else if (throw_on_exist) {
429 throw runtime_error(
format(
"Geant4Kernel",
"DDG4: The Geant4 action phase %s "
430 "already exists. [Already-Exists]", nam.c_str()));
437 Phases::iterator i =
m_phases.find(nam);
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.
The property class to assign options to actions.
IFACE * addExtension(CONCRETE *c)
Extend the sensitive detector element with an arbitrary structure accessible by the type...
PropertyManager & properties()
Access to the properties of the object.
ClientOutputLevels m_clientLevels
Property: Client output levels.
Geant4Kernel & worker(unsigned long thread_identifier, bool create_if=false)
Access worker instance by it's identifier.
virtual long apply(const char *factory, int argc, char **argv)=0
Manipulate geometry using factory converter.
static unsigned long int thread_self()
Access thread identifier.
Geant4Kernel & declareProperty(const std::string &nam, T &val)
Declare property.
int numWorkers() const
Access number of workers.
GlobalActions m_globalFilters
Globally registered filters of sensitive detectors.
void deletePtr(T *&p)
Helper to delete objects from heap and reset the pointer. Saves many many lines of code...
void setContext(Geant4Context *ctxt)
Set the thread's context.
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.
int m_outputLevel
Property: Output level.
static void decrement(T *)
Decrement count according to type information.
std::string typeName(const std::type_info &type)
ABI information about type names.
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.
GlobalActions m_globalActions
Globally registered actions.
static int terminate(Geant4Kernel &kernel)
Terminate the application.
Out version of the std auto_ptr implementation base either on auto_ptr or unique_ptr.
virtual int initialize()
Run the simulation: Initialize Geant4.
PrintLevel printLevel()
Access the current printer level.
Property & property(const std::string &name)
Access single property.
const std::string & name() const
Access name of the action.
std::string m_uiName
Property: Name of the UI action. Must be member of the global actions.
std::string format(const std::string &src, const std::string &fmt,...)
Build formatted string.
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.
Geant4Kernel(Geant4Kernel *m, unsigned long identifier)
Standard constructor for workers.
Class, which allows all Geant4Action derivatives to access the DDG4 kernel structures.
Geant4Kernel * m_master
Parent reference.
virtual ~Geant4Kernel()
Default destructor.
Class, which allows all Geant4Action to be stored.
void destroyObjects(M &m)
long addRef()
Increase reference count.
PropertyManager m_properties
Property pool.
bool isMultiThreaded() const
Geant4Kernel & registerGlobalAction(Geant4Action *action)
Register action by name to be retrieved when setting up and connecting action objects.
Embedded helper class to facilitate map access to the phases.
Workers m_workers
Worker threads.
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.
void releaseObjects(M &m)
virtual int terminate()
Run the simulation: Terminate Geant4.
int m_numThreads
Property: Running in multi threaded context.
virtual void destroyPhases()
Destroy all phases. To be called only at shutdown.
Geant4Context * workerContext()
Thread's Geant4 execution context.
static void destroyInstance()
Destroy the instance.
static void increment(T *)
Increment count according to type information.
G4TrackingManager * m_trackMgr
Reference to Geant4 track manager.
std::string m_controlName
Property: Name of the G4UI command tree.
bool exists(const std::string &name) const
Check for existence.
LCDD * m_lcdd
Detector description object.
Phases m_phases
Action phases.
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.
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.
long m_numEvent
Property: Number of events to be executed in batch mode.
The main interface to the DD4hep detector description package.
unsigned long m_id
Flag: Master instance (id<0) or worker (id >= 0)
virtual int configure()
Run the simulation: Configure Geant4.
int printout(PrintLevel severity, const char *src, const char *fmt,...)
Calls the display action with a given severity level.
static int run(Geant4Kernel &kernel)
Run the application and simulate events.
virtual bool removePhase(const std::string &name)
Remove an existing phase from the phase. If not existing returns false.
Geant4Action * globalAction(const std::string &action_name, bool throw_if_not_present=true)
Retrieve action from repository.
static Geant4Kernel & instance(LCDD &lcdd)
Instance accessor.
T * removeExtension(bool destroy=true)
Remove an existing extension object from the LCDD instance. If not destroyed, the instance is returne...
G4UIdirectory * m_control
Top level control directory.
void printProperties() const
Print the property values.
Default base class for all Geant 4 actions and derivates thereof.
virtual int terminate()
Terminate all associated action instances.
Geant4Kernel & registerGlobalFilter(Geant4Action *filter)
Register filter by name to be retrieved when setting up and connecting filter objects.
std::string arguments(int argc, char **argv)
Helper function to serialize argument list to a single string.
PrintLevel getOutputLevel(const std::string object) const
Retrieve the global output level of a named object.