Back to home page

EIC code displayed by LXR

 
 

    


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

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 #ifndef DDG4_GEANT4STACKINGACTION_H
0014 #define DDG4_GEANT4STACKINGACTION_H
0015 
0016 /// Framework include files
0017 #include <DDG4/Geant4Action.h>
0018 
0019 /// Geant4 include files
0020 #include <G4ClassificationOfNewTrack.hh>
0021 
0022 /// Forward declarations
0023 class G4StackManager;
0024 
0025 /// Namespace for the AIDA detector description toolkit
0026 namespace dd4hep {
0027 
0028   /// Namespace for the Geant4 based simulation part of the AIDA detector description toolkit
0029   namespace sim {
0030 
0031     // Forward declarations
0032     class Geant4StackingAction;
0033     class Geant4SharedStackingAction;
0034     class Geant4StackingActionSequence;
0035 
0036     enum Geant4StackingActionTrackClassification {
0037       NoTrackClassification = 0xFEED
0038     };
0039     union TrackClassification {
0040       G4ClassificationOfNewTrack value;
0041       int                        type;
0042       TrackClassification() { type = NoTrackClassification; }
0043       TrackClassification(G4ClassificationOfNewTrack val) { value = val; }
0044     };
0045 
0046     /// Concrete implementation of the Geant4 stacking action base class
0047     /**
0048      *  \author  M.Frank
0049      *  \version 1.0
0050      *  \ingroup DD4HEP_SIMULATION
0051      */
0052     class Geant4StackingAction: public Geant4Action {
0053     public:
0054       friend class Geant4StackingActionSequence;
0055       typedef Geant4SharedStackingAction shared_type;
0056     public:
0057       /// Define standard assignments and constructors
0058       DDG4_DEFINE_ACTION_CONSTRUCTORS(Geant4StackingAction);
0059 
0060     public:
0061       /// Standard constructor
0062       Geant4StackingAction(Geant4Context* ctxt, const std::string& name);
0063       /// Default destructor
0064       virtual ~Geant4StackingAction();
0065       /// New-stage callback
0066       virtual void newStage(G4StackManager* /* stackManager */)   {
0067       }
0068       /// Preparation callback
0069       virtual void prepare(G4StackManager* /* stackManager */)   {
0070       }
0071       /// Return TrackClassification with enum G4ClassificationOfNewTrack or NoTrackClassification
0072       virtual TrackClassification 
0073     classifyNewTrack(G4StackManager* /* stackManager */, const G4Track* track);
0074     };
0075 
0076     /// Implementation of the Geant4 shared stacking action
0077     /**
0078      * Wrapper to share single instances of stacking actions for
0079      * multi-threaded purposes. The wrapper ensures the locking
0080      * of the basic actions to avoid race conditions.
0081      *
0082      * Shared action should be 'fast'. The global lock otherwise
0083      * inhibits the efficient use of the multiple threads.
0084      *
0085      *  \author  M.Frank
0086      *  \version 1.0
0087      *  \ingroup DD4HEP_SIMULATION
0088      */
0089     class Geant4SharedStackingAction : public Geant4StackingAction {
0090     protected:
0091       /// Reference to the shared action
0092       Geant4StackingAction* m_action;
0093       /// Define standard assignments and constructors
0094       DDG4_DEFINE_ACTION_CONSTRUCTORS(Geant4SharedStackingAction);
0095     public:
0096       /// Standard constructor
0097       Geant4SharedStackingAction(Geant4Context* context, const std::string& nam);
0098       /// Default destructor
0099       virtual ~Geant4SharedStackingAction();
0100       /// Set or update client for the use in a new thread fiber
0101       virtual void configureFiber(Geant4Context* thread_context)  override;
0102       /// Underlying object to be used during the execution of this thread
0103       virtual void use(Geant4StackingAction* action);
0104       /// New-stage callback
0105       virtual void newStage(G4StackManager* stackManager)  override;
0106       /// Preparation callback
0107       virtual void prepare(G4StackManager* stackManager)  override;
0108       /// Return TrackClassification with enum G4ClassificationOfNewTrack or NoTrackClassification
0109       virtual TrackClassification 
0110     classifyNewTrack(G4StackManager* stackManager, const G4Track* track)  override;
0111     };
0112 
0113     /// Concrete implementation of the Geant4 stacking action sequence
0114     /**
0115      * The sequence dispatches the callbacks for each stepping action
0116      * to all registered Geant4StackingAction members and all
0117      * registered callbacks.
0118      *
0119      * Note Multi-Threading issue:
0120      * Neither callbacks not the action list is protected against multiple 
0121      * threads calling the Geant4 callbacks!
0122      * These must be protected in the user actions themselves.
0123      *
0124      *  \author  M.Frank
0125      *  \version 1.0
0126      *  \ingroup DD4HEP_SIMULATION
0127      */
0128     class Geant4StackingActionSequence: public Geant4Action {
0129     protected:
0130       /// Callback sequence for the newStage call
0131       CallbackSequence m_newStage;
0132       // Callback sequence for the prepare call
0133       CallbackSequence m_prepare;
0134       /// The list of action objects to be called
0135       Actors<Geant4StackingAction> m_actors;
0136 
0137       /// Define standard assignments and constructors
0138       DDG4_DEFINE_ACTION_CONSTRUCTORS(Geant4StackingActionSequence);
0139     public:
0140       /// Standard constructor
0141       Geant4StackingActionSequence(Geant4Context* ctxt, const std::string& name);
0142       /// Default destructor
0143       virtual ~Geant4StackingActionSequence();
0144       /// Set or update client context
0145       virtual void updateContext(Geant4Context* ctxt)  override;
0146       /// Set or update client for the use in a new thread fiber
0147       virtual void configureFiber(Geant4Context* thread_context)  override;
0148       /// Get an action by name
0149       Geant4StackingAction* get(const std::string& name) const;
0150       /// Register begin-of-event callback. Types Q and T must be polymorph!
0151       template <typename T> void callAtNewStage(T* p, void (T::*f)(G4StackManager*)) {
0152         m_newStage.add(p, f);
0153       }
0154       /// Register end-of-event callback. Types Q and T must be polymorph!
0155       template <typename T> void callAtPrepare(T* p, void (T::*f)(G4StackManager*)) {
0156         m_prepare.add(p, f);
0157       }
0158       /// Add an actor responding to all callbacks. Sequence takes ownership.
0159       void adopt(Geant4StackingAction* action);
0160       /// New-stage callback
0161       virtual void newStage(G4StackManager* stackManager);
0162       /// Preparation callback
0163       virtual void prepare(G4StackManager* stackManager);
0164       /// Classify new track: The first call in the sequence returning non-null pointer wins!
0165       virtual TrackClassification 
0166     classifyNewTrack(G4StackManager* stackManager, const G4Track* track);
0167     };
0168 
0169   }    // End namespace sim
0170 }      // End namespace dd4hep
0171 #endif // DDG4_GEANT4STACKINGACTION_H