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