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_GEANT4TRACKINGACTION_H
0014 #define DDG4_GEANT4TRACKINGACTION_H
0015 
0016 // Framework include files
0017 #include <DDG4/Geant4Action.h>
0018 #include <G4VUserTrackInformation.hh>
0019 
0020 class G4TrackingManager;
0021 class G4Track;
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 Geant4TrackInformation;
0031     class Geant4TrackingAction;
0032     class Geant4SharedTrackingAction;
0033     class Geant4TrackingActionSequence;
0034 
0035     /// Default base class for all geant 4 tracking actions used in DDG4.
0036     /**
0037      *  \author  M.Frank
0038      *  \version 1.0
0039      *  \ingroup DD4HEP_SIMULATION
0040      */
0041     class Geant4TrackingAction: public Geant4Action {
0042     public:
0043       typedef Geant4SharedTrackingAction shared_type;
0044 
0045       /// Define standard assignments and constructors
0046       DDG4_DEFINE_ACTION_CONSTRUCTORS(Geant4TrackingAction);
0047     public:
0048       /// Standard constructor
0049       Geant4TrackingAction(Geant4Context* context, const std::string& name = "");
0050       /// Default destructor
0051       virtual ~Geant4TrackingAction();
0052       /// Access the Geant4 tracking manager. Only use between tracking pre- and post action
0053       G4TrackingManager* trackMgr() const {
0054         return m_context ? m_context->trackMgr() : 0;
0055       }
0056       /// Mark the track to be kept for MC truth propagation
0057       void mark(const G4Track* track) const;
0058       /// Pre-track action callback
0059       virtual void begin(const G4Track* track);
0060       /// Post-track action callback
0061       virtual void end(const G4Track* track);
0062     };
0063 
0064     /// Implementation of the Geant4 shared track action
0065     /**
0066      * Wrapper to share single instances of track actions for
0067      * multi-threaded purposes. The wrapper ensures the locking
0068      * of the basic actions to avoid race conditions.
0069      *
0070      * Shared action should be 'fast'. The global lock otherwise
0071      * inhibits the efficient use of the multiple threads.
0072      *
0073      *  \author  M.Frank
0074      *  \version 1.0
0075      *  \ingroup DD4HEP_SIMULATION
0076      */
0077     class Geant4SharedTrackingAction : public Geant4TrackingAction {
0078     protected:
0079       /// Reference to the shared action
0080       Geant4TrackingAction* m_action = 0;
0081 
0082       /// Define standard assignments and constructors
0083       DDG4_DEFINE_ACTION_CONSTRUCTORS(Geant4SharedTrackingAction);
0084     public:
0085       /// Standard constructor
0086       Geant4SharedTrackingAction(Geant4Context* context, const std::string& nam);
0087       /// Default destructor
0088       virtual ~Geant4SharedTrackingAction();
0089       /// Set or update client for the use in a new thread fiber
0090       virtual void configureFiber(Geant4Context* thread_context)  override;
0091       /// Underlying object to be used during the execution of this thread
0092       virtual void use(Geant4TrackingAction* action);
0093       /// Begin-of-track callback
0094       virtual void begin(const G4Track* track)  override;
0095       /// End-of-track callback
0096       virtual void end(const G4Track* track)   override;
0097     };
0098 
0099     /// Concrete implementation of the Geant4 tracking action sequence
0100     /**
0101      * The sequence dispatches the callbacks for each tracking action
0102      * to all registered Geant4SteppingAction members and all
0103      * registered callbacks.
0104      *
0105      * Note Multi-Threading issue:
0106      * Neither callbacks not the action list is protected against multiple 
0107      * threads calling the Geant4 callbacks!
0108      * These must be protected in the user actions themselves.
0109      *
0110      *  \author  M.Frank
0111      *  \version 1.0
0112      *  \ingroup DD4HEP_SIMULATION
0113      */
0114     class Geant4TrackingActionSequence: public Geant4Action {
0115     protected:
0116       /// Callback sequence for pre tracking action
0117       CallbackSequence             m_front;
0118       /// Callback sequence for pre tracking action
0119       CallbackSequence             m_begin;
0120       /// Callback sequence for post tracking action
0121       CallbackSequence             m_end;
0122       /// Callback sequence for pre tracking action
0123       CallbackSequence             m_final;
0124       /// The list of action objects to be called
0125       Actors<Geant4TrackingAction> m_actors;
0126 
0127 
0128       /// Define standard assignments and constructors
0129       DDG4_DEFINE_ACTION_CONSTRUCTORS(Geant4TrackingActionSequence);
0130 
0131     public:
0132       /// Standard constructor
0133       Geant4TrackingActionSequence(Geant4Context* context, const std::string& name);
0134       /// Default destructor
0135       virtual ~Geant4TrackingActionSequence();
0136       /// Set or update client context
0137       virtual void updateContext(Geant4Context* ctxt)  override;
0138       /// Set or update client for the use in a new thread fiber
0139       virtual void configureFiber(Geant4Context* thread_context)  override;
0140       /// Get an action by name
0141       Geant4TrackingAction* get(const std::string& name) const;
0142       /// Register Pre-track action callback before anything else
0143       template <typename Q, typename T>
0144       void callUpFront(Q* p, void (T::*f)(const G4Track*),
0145                        CallbackSequence::Location where=CallbackSequence::END) {
0146         m_front.add(p, f, where);
0147       }
0148       /// Register Pre-track action callback
0149       template <typename Q, typename T>
0150       void callAtBegin(Q* p, void (T::*f)(const G4Track*),
0151                        CallbackSequence::Location where=CallbackSequence::END) {
0152         m_begin.add(p, f, where);
0153       }
0154       /// Register Post-track action callback
0155       template <typename Q, typename T>
0156       void callAtEnd(Q* p, void (T::*f)(const G4Track*),
0157                      CallbackSequence::Location where=CallbackSequence::END) {
0158         m_end.add(p, f, where);
0159       }
0160       /// Register Post-track action callback
0161       template <typename Q, typename T>
0162       void callAtFinal(Q* p, void (T::*f)(const G4Track*),
0163                        CallbackSequence::Location where=CallbackSequence::END) {
0164         m_final.add(p, f, where);
0165       }
0166       /// Add an actor responding to all callbacks. Sequence takes ownership.
0167       void adopt(Geant4TrackingAction* action);
0168       /// Pre-tracking action callback
0169       virtual void begin(const G4Track* track);
0170       /// Post-tracking action callback
0171       virtual void end(const G4Track* track);
0172     };
0173 
0174   }    // End namespace sim
0175 }      // End namespace dd4hep
0176 #endif // DDG4_GEANT4TRACKINGACTION_H