Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:17:35

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 // Framework include files
0015 #include <DD4hep/InstanceCount.h>
0016 #include <DDG4/Geant4TrackingAction.h>
0017 #include <DDG4/Geant4MonteCarloTruth.h>
0018 #include <DDG4/Geant4TrackInformation.h>
0019 
0020 // Geant4 include files
0021 #include <G4Track.hh>
0022 #include <G4Threading.hh>
0023 #include <G4AutoLock.hh>
0024 #include <G4TrackingManager.hh>
0025 #include <G4VUserTrackInformation.hh>
0026 
0027 using namespace dd4hep::sim;
0028 
0029 class G4Step;
0030 class G4TouchableHistory;
0031 namespace {
0032   G4Mutex action_mutex=G4MUTEX_INITIALIZER;
0033 }
0034 
0035 /// Standard constructor
0036 Geant4TrackingActionSequence::Geant4TrackingActionSequence(Geant4Context* ctxt, const std::string& nam)
0037   : Geant4Action(ctxt, nam) {
0038   m_needsControl = true;
0039   InstanceCount::increment(this);
0040 }
0041 
0042 /// Default destructor
0043 Geant4TrackingActionSequence::~Geant4TrackingActionSequence() {
0044   m_actors(&Geant4TrackingAction::release);
0045   m_actors.clear();
0046   m_front.clear();
0047   m_final.clear();
0048   m_begin.clear();
0049   m_end.clear();
0050   InstanceCount::decrement(this);
0051 }
0052 
0053 /// Set or update client context
0054 void Geant4TrackingActionSequence::updateContext(Geant4Context* ctxt)    {
0055   m_context = ctxt;
0056   m_actors.updateContext(ctxt);
0057 }
0058 
0059 /// Set or update client for the use in a new thread fiber
0060 void Geant4TrackingActionSequence::configureFiber(Geant4Context* thread_context)   {
0061   m_actors(&Geant4Action::configureFiber, thread_context);
0062 }
0063 
0064 /// Get an action by name
0065 Geant4TrackingAction* Geant4TrackingActionSequence::get(const std::string& nam) const   {
0066   return m_actors.get(FindByName(TypeName::split(nam).second));
0067 }
0068 
0069 /// Add an actor responding to all callbacks. Sequence takes ownership.
0070 void Geant4TrackingActionSequence::adopt(Geant4TrackingAction* action) {
0071   if (action) {
0072     G4AutoLock protection_lock(&action_mutex);
0073     action->addRef();
0074     m_actors.add(action);
0075     return;
0076   }
0077   throw std::runtime_error("Geant4TrackingActionSequence: Attempt to add invalid actor!");
0078 }
0079 
0080 /// Pre-track action callback
0081 void Geant4TrackingActionSequence::begin(const G4Track* track) {
0082   m_front(track);
0083   m_actors(&Geant4TrackingAction::begin, track);
0084   m_begin(track);
0085 }
0086 
0087 /// Post-track action callback
0088 void Geant4TrackingActionSequence::end(const G4Track* track) {
0089   m_end(track);
0090   m_actors(&Geant4TrackingAction::end, track);
0091   m_final(track);
0092 }
0093 
0094 /// Standard constructor
0095 Geant4TrackingAction::Geant4TrackingAction(Geant4Context* ctxt, const std::string& nam)
0096   : Geant4Action(ctxt, nam) {
0097   InstanceCount::increment(this);
0098 }
0099 
0100 /// Default destructor
0101 Geant4TrackingAction::~Geant4TrackingAction() {
0102   InstanceCount::decrement(this);
0103 }
0104 
0105 /// Pre-track action callback
0106 void Geant4TrackingAction::begin(const G4Track*) {
0107 }
0108 
0109 /// Post-track action callback
0110 void Geant4TrackingAction::end(const G4Track*) {
0111 }
0112 
0113 /// Mark the track to be kept for MC truth propagation
0114 void Geant4TrackingAction::mark(const G4Track* track) const    {
0115   Geant4MonteCarloTruth* truth = context()->event().extension<Geant4MonteCarloTruth>(false);
0116   if ( truth ) truth->mark(track,true);
0117 }
0118 
0119 /// Standard constructor
0120 Geant4SharedTrackingAction::Geant4SharedTrackingAction(Geant4Context* ctxt, const std::string& nam)
0121   : Geant4TrackingAction(ctxt, nam), m_action(0)
0122 {
0123   InstanceCount::increment(this);
0124 }
0125 
0126 /// Default destructor
0127 Geant4SharedTrackingAction::~Geant4SharedTrackingAction()   {
0128   detail::releasePtr(m_action);
0129   InstanceCount::decrement(this);
0130 }
0131 
0132 /// Set or update client for the use in a new thread fiber
0133 void Geant4SharedTrackingAction::configureFiber(Geant4Context* thread_context)   {
0134   m_action->configureFiber(thread_context);
0135 }
0136 
0137 /// Underlying object to be used during the execution of this thread
0138 void Geant4SharedTrackingAction::use(Geant4TrackingAction* action)   {
0139   if (action) {
0140     action->addRef();
0141     m_properties.adopt(action->properties());
0142     m_action = action;
0143     return;
0144   }
0145   except("Attempt to use invalid actor!");
0146 }
0147 
0148 /// Begin-of-track callback
0149 void Geant4SharedTrackingAction::begin(const G4Track* track)   {
0150   if ( m_action )  {
0151     G4AutoLock protection_lock(&action_mutex);    {
0152       ContextSwap swap(m_action,context());
0153       m_action->begin(track);
0154     }
0155   }
0156 }
0157 
0158 /// End-of-track callback
0159 void Geant4SharedTrackingAction::end(const G4Track* track)   {
0160   if ( m_action )  {
0161     G4AutoLock protection_lock(&action_mutex);  {
0162       ContextSwap swap(m_action,context());
0163       m_action->end(track);
0164     }
0165   }
0166 }