File indexing completed on 2025-11-03 09:00:33
0001 
0002 
0003 
0004 
0005 
0006 
0007 
0008 
0009 
0010 
0011 
0012 
0013 
0014 
0015 #include <DD4hep/InstanceCount.h>
0016 #include <DDG4/Geant4StackingAction.h>
0017 
0018 
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 
0028 Geant4StackingAction::Geant4StackingAction(Geant4Context* ctxt, const std::string& nam)
0029   : Geant4Action(ctxt, nam) {
0030   InstanceCount::increment(this);
0031 }
0032 
0033 
0034 Geant4StackingAction::~Geant4StackingAction() {
0035   InstanceCount::decrement(this);
0036 }
0037 
0038 
0039 TrackClassification 
0040 Geant4StackingAction::classifyNewTrack(G4StackManager* , const G4Track* )  {
0041   return TrackClassification();
0042 }
0043 
0044 
0045 Geant4SharedStackingAction::Geant4SharedStackingAction(Geant4Context* ctxt, const std::string& nam)
0046   : Geant4StackingAction(ctxt, nam), m_action(0)
0047 {
0048   InstanceCount::increment(this);
0049 }
0050 
0051 
0052 Geant4SharedStackingAction::~Geant4SharedStackingAction()   {
0053   detail::releasePtr(m_action);
0054   InstanceCount::decrement(this);
0055 }
0056 
0057 
0058 void Geant4SharedStackingAction::configureFiber(Geant4Context* thread_context)   {
0059   m_action->configureFiber(thread_context);
0060 }
0061 
0062 
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 
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 
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 
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 
0107 Geant4StackingActionSequence::Geant4StackingActionSequence(Geant4Context* ctxt, const std::string& nam)
0108   : Geant4Action(ctxt, nam) {
0109   m_needsControl = true;
0110   InstanceCount::increment(this);
0111 }
0112 
0113 
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 
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 
0133 void Geant4StackingActionSequence::updateContext(Geant4Context* ctxt)    {
0134   m_context = ctxt;
0135   m_actors.updateContext(ctxt);
0136 }
0137 
0138 
0139 void Geant4StackingActionSequence::configureFiber(Geant4Context* thread_context)   {
0140   m_actors(&Geant4Action::configureFiber, thread_context);
0141 }
0142 
0143 
0144 Geant4StackingAction* Geant4StackingActionSequence::get(const std::string& nam) const   {
0145   return m_actors.get(FindByName(TypeName::split(nam).second));
0146 }
0147 
0148 
0149 void Geant4StackingActionSequence::newStage(G4StackManager* stackManager) {
0150   m_actors(&Geant4StackingAction::newStage, stackManager);
0151   m_newStage(stackManager);
0152 }
0153 
0154 
0155 void Geant4StackingActionSequence::prepare(G4StackManager* stackManager) {
0156   m_actors(&Geant4StackingAction::prepare, stackManager);
0157   m_prepare(stackManager);
0158 }
0159 
0160 
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 }