Back to home page

EIC code displayed by LXR

 
 

    


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

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_GEANT4CONTEXT_H
0015 #define DDG4_GEANT4CONTEXT_H
0016 
0017 // Framework incloude files
0018 #include <DD4hep/Primitives.h>
0019 #include <DD4hep/ObjectExtensions.h>
0020 
0021 // Forward declarations
0022 class G4Run;
0023 class G4Track;
0024 class G4Event;
0025 class G4VTrajectory;
0026 class G4VPhysicalVolume;
0027 class G4TrackingManager;
0028 
0029 /// Namespace for the AIDA detector description toolkit
0030 namespace dd4hep {
0031 
0032   // Forward declarations
0033   class Detector;
0034   class DetElement;
0035 
0036   /// Namespace for the Geant4 based simulation part of the AIDA detector description toolkit
0037   namespace sim {
0038 
0039     class Geant4Run;
0040     class Geant4Event;
0041     class Geant4Kernel;
0042     class Geant4Random;
0043     class ContextUpdate;
0044     class Geant4RunActionSequence;
0045     class Geant4EventActionSequence;
0046     class Geant4SteppingActionSequence;
0047     class Geant4TrackingActionSequence;
0048     class Geant4StackingActionSequence;
0049     class Geant4GeneratorActionSequence;
0050     class Geant4SensDetSequences;
0051 
0052     ///  User run context for DDG4
0053     /**
0054      *  The context is accessible from the Geant4Context pointer,
0055      *  which is present in every Geant4Action.
0056      *  The run context is only valid however, if there is actually
0057      *  an run being procesed ie. only during the lifetime of the
0058      *  corresponding G4Run objects.
0059      *
0060      *  Please note:
0061      *  Extensions are only valid within the hosting run and are INVALID
0062      *  across different runs.
0063      *  Hence: They are only useful to extend data of an run.
0064      *
0065      *  \author  M.Frank
0066      *  \version 1.0
0067      *  \ingroup DD4HEP_SIMULATION
0068      */
0069     class  Geant4Run : public ObjectExtensions  {
0070       /// Reference to the original Geant4 run object
0071       const G4Run* m_run { nullptr };
0072     protected:
0073 
0074     public:
0075       /// Intializing constructor
0076       Geant4Run(const G4Run* run);
0077       /// Default destructor
0078       virtual ~Geant4Run();
0079       /// Access the G4Run directly: Automatic type conversion
0080       operator const G4Run&() const  {  return *m_run;  }
0081       /// Access the G4Event directly: Explicit G4Run accessor
0082       const G4Run& run() const       { return *m_run;   }
0083       /// Add an extension object to the detector element
0084       void* addExtension(unsigned long long int k, ExtensionEntry* e)  {
0085         return ObjectExtensions::addExtension(k, e);
0086       }
0087       /// Add user extension object. Ownership is transferred!
0088       template <typename T> T* addExtension(T* ptr, bool take_ownership=true)   {
0089         ExtensionEntry* e = take_ownership
0090           ? (ExtensionEntry*)new detail::DeleteExtension<T,T>(ptr)
0091           : (ExtensionEntry*)new detail::SimpleExtension<T,T>(ptr);
0092         return (T*)ObjectExtensions::addExtension(detail::typeHash64<T>(),e);
0093       }
0094       /// Access to type safe extension object. Exception is thrown if the object is invalid
0095       template <typename T> T* extension(bool alert=true) {
0096         return (T*)ObjectExtensions::extension(detail::typeHash64<T>(),alert);
0097       }
0098     };
0099 
0100     ///  User event context for DDG4
0101     /**
0102      *  The context is accessible from the Geant4Context pointer,
0103      *  which is present in every Geant4Action.
0104      *  The event context is only valid however, if there is actually
0105      *  an event being procesed ie. only during the lifetime of the
0106      *  corresponding G4Event objects.
0107      *
0108      *  Please note:
0109      *  Extensions are only valid within the hosting event and are INVALID
0110      *  across different events.
0111      *  Hence: They are only useful to extend data of an event.
0112      *
0113      *  Any random numbers used to process one event should be accessed
0114      *  from this location. The framework ensures that the same seeded
0115      *  sequence is used throughout the processing of one single event.
0116      *
0117      *  \author  M.Frank
0118      *  \version 1.0
0119      *  \ingroup DD4HEP_SIMULATION
0120      */
0121     class  Geant4Event : public ObjectExtensions  {
0122       /// Reference to the original Geant4 event object
0123       const G4Event* m_event  { nullptr };
0124       /// Reference to the main random number generator
0125       Geant4Random* m_random  { nullptr };
0126 
0127     public:
0128       /// Intializing constructor
0129       Geant4Event(const G4Event* run, Geant4Random* rndm);
0130       /// Default destructor
0131       virtual ~Geant4Event();
0132       /// Access the G4Event directly: Automatic type conversion
0133       operator const G4Event&() const  {  return *m_event;   }
0134       /// Access the G4Event directly: Explicit G4Event accessor
0135       const G4Event& event() const     {  return *m_event;   }
0136       /// Access the random number generator
0137       Geant4Random& random() const     {  return *m_random;  }
0138 
0139       /// Add an extension object to the detector element
0140       void* addExtension(unsigned long long int k, ExtensionEntry* e)  {
0141         return ObjectExtensions::addExtension(k, e);
0142       }
0143       /// Add user extension object. Ownership is transferred and object deleted at the end of the event.
0144       template <typename T> T* addExtension(T* ptr, bool take_ownership=true)   {
0145         ExtensionEntry* e = take_ownership
0146           ? (ExtensionEntry*)new detail::DeleteExtension<T,T>(ptr)
0147           : (ExtensionEntry*)new detail::SimpleExtension<T,T>(ptr);
0148         return (T*)ObjectExtensions::addExtension(detail::typeHash64<T>(),e);
0149       }
0150       /// Access to type safe extension object. Exception is thrown if the object is invalid
0151       template <typename T> T* extension(bool alert=true) {
0152         return (T*)ObjectExtensions::extension(detail::typeHash64<T>(),alert);
0153       }
0154     };
0155 
0156     /// Generic context to extend user, run and event information
0157     /**
0158      *  A valid instance of the Geant4Context is passed to every instance of a Geant4Action at 
0159      *  creation time.
0160      *
0161      *  The Geant4Context is the main thread specific accessor to the dd4hep, DDG4 and
0162      *  the user framework.
0163      *  - The access to the dd4hep objects is via the Geant4Context::detectorDescription() call,
0164      *  - the access to DDG4 as a whole is supported via Geant4Context::kernel() and
0165      *  - the access to the user framework using a specialized implementation of:
0166      *  template <typename T> T& userFramework()  const;
0167      *
0168      *  A user defined implementations must be specialized somewhere in a compilation unit
0169      *  of the user framework, not in a header file. The framework object could host
0170      *  e.g. references for histogramming, logging, data access etc.
0171      *
0172      *  This way any experiment/user related data processing framework can exhibit
0173      *  its essential tools to DDG4 actions.
0174      *
0175      *  A possible specialized implementations would look like the following:
0176      *
0177      *  struct Gaudi  {
0178      *    IMessageSvc*   msg;
0179      *    IHistogramSvc* histos;
0180      *    ....
0181      *  };
0182      *
0183      *  template<> Gaudi& Geant4Context::userFramework<Gaudi>()  const  {
0184      *    UserFramework& fw = m_kernel->userFramework();
0185      *    if ( fw.first && &typeid(T) == fw.second ) return *(T*)fw.first;
0186      *    throw std::runtime_error("No user specified framework context present!");
0187      *  }
0188      *
0189      *  To access the user framework then use the following call:
0190      *  Gaudi* fw = context->userFramework<Gaudi>();
0191      *
0192      *  of course after having initialized it:
0193      *  Gaudi * fw = ...;
0194      *  GaudiKernel& kernel = ...;
0195      *  kernel.setUserFramework(fw);
0196      *
0197      *  \author  M.Frank
0198      *  \version 1.0
0199      *  \ingroup DD4HEP_SIMULATION
0200      */
0201     class Geant4Context  {
0202     public:
0203       friend class Geant4Kernel;
0204       typedef std::pair<void*, const std::type_info*>   UserFramework;
0205 
0206     protected:
0207       /// Reference to the kernel object
0208       Geant4Kernel*      m_kernel = 0;
0209       /// Transient context variable - depending on the thread context: run reference
0210       Geant4Run*         m_run    = 0;
0211       /// Transient context variable - depending on the thread context: event reference
0212       Geant4Event*       m_event  = 0;
0213 
0214       /// Default constructor
0215       Geant4Context(Geant4Kernel* kernel);
0216 
0217     public:
0218       /// Default destructor
0219       virtual ~Geant4Context();
0220       /// Set the geant4 run reference
0221       void setRun(Geant4Run* new_run);
0222       /// Access the geant4 run -- valid only between BeginRun() and EndRun()!
0223       Geant4Run& run()  const;
0224       /// Access the geant4 run by ptr. Must be checked by clients!
0225       Geant4Run* runPtr()  const  { return m_run; }
0226       /// Set the geant4 event reference
0227       void setEvent(Geant4Event* new_event);
0228       /// Access the geant4 event -- valid only between BeginEvent() and EndEvent()!
0229       Geant4Event& event()  const;
0230       /// Access the geant4 event by ptr. Must be checked by clients!
0231       Geant4Event* eventPtr()  const     { return m_event; }
0232       /// Access to the kernel object
0233       Geant4Kernel& kernel()  const      { return *m_kernel;   }
0234       /// Access to geometry world
0235       G4VPhysicalVolume* world()  const;
0236 
0237       /// Access to the user framework. Specialized function to be implemented by the client
0238       template <typename T> T& framework()  const;
0239       /// Generic framework access
0240       UserFramework& userFramework() const;
0241       /// Access to detector description
0242       Detector& detectorDescription() const;
0243       /// Access the tracking manager
0244       G4TrackingManager* trackMgr() const;
0245       /// Create a user trajectory
0246       virtual G4VTrajectory* createTrajectory(const G4Track* track) const;
0247       /// Access to the main run action sequence from the kernel object
0248       Geant4RunActionSequence& runAction() const;
0249       /// Access to the main event action sequence from the kernel object
0250       Geant4EventActionSequence& eventAction() const;
0251       /// Access to the main stepping action sequence from the kernel object
0252       Geant4SteppingActionSequence& steppingAction() const;
0253       /// Access to the main tracking action sequence from the kernel object
0254       Geant4TrackingActionSequence& trackingAction() const;
0255       /// Access to the main stacking action sequence from the kernel object
0256       Geant4StackingActionSequence& stackingAction() const;
0257       /// Access to the main generator action sequence from the kernel object
0258       Geant4GeneratorActionSequence& generatorAction() const;
0259       /// Access to the sensitive detector sequences from the kernel object
0260       Geant4SensDetSequences& sensitiveActions() const;
0261     };
0262 
0263   }    // End namespace sim
0264 }      // End namespace dd4hep
0265 
0266 #endif // DDG4_GEANT4CONTEXT_H