Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:23

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 
0014 #ifndef DDG4_GEANT4EVENTACTION_H
0015 #define DDG4_GEANT4EVENTACTION_H
0016 
0017 // Framework include files
0018 #include <DDG4/Geant4Action.h>
0019 
0020 // Forward declarations
0021 class G4Event;
0022 
0023 /// Namespace for the AIDA detector description toolkit
0024 namespace dd4hep {
0025 
0026   /// Namespace for the Geant4 based simulation part of the AIDA detector description toolkit
0027   namespace sim {
0028 
0029     // Forward declarations
0030     class Geant4EventAction;
0031     class Geant4SharedEventAction;
0032     class Geant4EventActionSequence;
0033 
0034     /// Concrete basic implementation of the Geant4 event action
0035     /**
0036      * The EventAction is called for every event.
0037      *
0038      * This class is the base class for all user actions, which have
0039      * to hook into the begin- and end-of-event actions.
0040      * Typical use cases are the collection/computation of event
0041      * related properties.
0042      *
0043      * Examples of this functionality may include for example:
0044      * - Reset variables summing event related information in the
0045      *   begin-event callback.
0046      * - Monitoring activities such as filling histograms
0047      *   from hits collected during the end-event action.
0048      *
0049      *  \author  M.Frank
0050      *  \version 1.0
0051      *  \ingroup DD4HEP_SIMULATION
0052      */
0053     class Geant4EventAction : public Geant4Action {
0054     public:
0055       typedef Geant4SharedEventAction shared_type;
0056       
0057     protected:
0058       /// Define standard assignments and constructors
0059       DDG4_DEFINE_ACTION_CONSTRUCTORS(Geant4EventAction);
0060 
0061     public:
0062       /// Standard constructor
0063       Geant4EventAction(Geant4Context* context, const std::string& nam);
0064       /// Default destructor
0065       virtual ~Geant4EventAction();
0066       /// Begin-of-event callback
0067       virtual void begin(const G4Event* event);
0068       /// End-of-event callback
0069       virtual void end(const G4Event* event);
0070     };
0071 
0072     /// Implementation of the Geant4 shared event action
0073     /**
0074      * Wrapper to share single instances of event actions for
0075      * multi-threaded purposes. The wrapper ensures the locking
0076      * of the basic actions to avoid race conditions.
0077      *
0078      * Shared action should be 'fast'. The global lock otherwise
0079      * inhibits the efficient use of the multiple threads.
0080      *
0081      *  \author  M.Frank
0082      *  \version 1.0
0083      *  \ingroup DD4HEP_SIMULATION
0084      */
0085     class Geant4SharedEventAction : public Geant4EventAction {
0086     protected:
0087       /// Reference to the shared action
0088       Geant4EventAction* m_action { nullptr };
0089 
0090     protected:
0091       /// Define standard assignments and constructors
0092       DDG4_DEFINE_ACTION_CONSTRUCTORS(Geant4SharedEventAction);
0093 
0094     public:
0095       /// Standard constructor
0096       Geant4SharedEventAction(Geant4Context* context, const std::string& nam);
0097       /// Default destructor
0098       virtual ~Geant4SharedEventAction();
0099       /// Set or update client for the use in a new thread fiber
0100       virtual void configureFiber(Geant4Context* thread_context);
0101       /// Underlying object to be used during the execution of this thread
0102       virtual void use(Geant4EventAction* action);
0103       /// Begin-of-event callback
0104       virtual void begin(const G4Event* event);
0105       /// End-of-event callback
0106       virtual void end(const G4Event* event);
0107     };
0108 
0109     /// Concrete implementation of the Geant4 event action sequence
0110     /**
0111      * The sequence dispatches the callbacks at the beginning and the and
0112      * of an event to all registered Geant4EventAction members and all
0113      * registered callbacks.
0114      *
0115      * Note Multi-Threading issue:
0116      * Neither callbacks not the action list is protected against multiple 
0117      * threads calling the Geant4 callbacks!
0118      * These must be protected in the user actions themselves.
0119      *
0120      *  \author  M.Frank
0121      *  \version 1.0
0122      *  \ingroup DD4HEP_SIMULATION
0123      */
0124     class Geant4EventActionSequence : public Geant4Action {
0125     protected:
0126       /// Callback sequence for event initialization action
0127       CallbackSequence m_begin;
0128       /// Callback sequence for event finalization action
0129       CallbackSequence m_end;
0130       /// Callback sequence for event finalization action
0131       CallbackSequence m_final;
0132       /// The list of action objects to be called
0133       Actors<Geant4EventAction> m_actors;
0134       
0135     protected:
0136       /// Define standard assignments and constructors
0137       DDG4_DEFINE_ACTION_CONSTRUCTORS(Geant4EventActionSequence);
0138 
0139     public:
0140       /// Standard constructor
0141       Geant4EventActionSequence(Geant4Context* context, const std::string& nam);
0142       /// Default destructor
0143       virtual ~Geant4EventActionSequence();
0144       /// Set or update client context
0145       virtual void updateContext(Geant4Context* ctxt);
0146       /// Set or update client for the use in a new thread fiber
0147       virtual void configureFiber(Geant4Context* thread_context);
0148       /// Get an action by name
0149       Geant4EventAction* get(const std::string& name) const;
0150       /// Register begin-of-event callback
0151       template <typename Q, typename T>
0152       void callAtBegin(Q* p, void (T::*f)(const G4Event*)) {
0153         m_begin.add(p, f);
0154       }
0155       /// Register end-of-event callback
0156       template <typename Q, typename T>
0157       void callAtEnd(Q* p, void (T::*f)(const G4Event*)) {
0158         m_end.add(p, f);
0159       }
0160       /// Register event-cleanup callback (after end-of-event callback -- unordered)
0161       template <typename Q, typename T>
0162       void callAtFinal(Q* p, void (T::*f)(const G4Event*)) {
0163         m_final.add(p, f);
0164       }
0165       /// Add an actor responding to all callbacks. Sequence takes ownership.
0166       void adopt(Geant4EventAction* action);
0167       /// Begin-of-event callback
0168       virtual void begin(const G4Event* event);
0169       /// End-of-event callback
0170       virtual void end(const G4Event* event);
0171     };
0172 
0173   }    // End namespace sim
0174 }      // End namespace dd4hep
0175 #endif // DDG4_GEANT4EVENTACTION_H