File indexing completed on 2025-01-30 09:17:35
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DD4hep/InstanceCount.h>
0016 #include <DDG4/Geant4TrackingAction.h>
0017 #include <DDG4/Geant4MonteCarloTruth.h>
0018 #include <DDG4/Geant4TrackInformation.h>
0019
0020
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
0036 Geant4TrackingActionSequence::Geant4TrackingActionSequence(Geant4Context* ctxt, const std::string& nam)
0037 : Geant4Action(ctxt, nam) {
0038 m_needsControl = true;
0039 InstanceCount::increment(this);
0040 }
0041
0042
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
0054 void Geant4TrackingActionSequence::updateContext(Geant4Context* ctxt) {
0055 m_context = ctxt;
0056 m_actors.updateContext(ctxt);
0057 }
0058
0059
0060 void Geant4TrackingActionSequence::configureFiber(Geant4Context* thread_context) {
0061 m_actors(&Geant4Action::configureFiber, thread_context);
0062 }
0063
0064
0065 Geant4TrackingAction* Geant4TrackingActionSequence::get(const std::string& nam) const {
0066 return m_actors.get(FindByName(TypeName::split(nam).second));
0067 }
0068
0069
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
0081 void Geant4TrackingActionSequence::begin(const G4Track* track) {
0082 m_front(track);
0083 m_actors(&Geant4TrackingAction::begin, track);
0084 m_begin(track);
0085 }
0086
0087
0088 void Geant4TrackingActionSequence::end(const G4Track* track) {
0089 m_end(track);
0090 m_actors(&Geant4TrackingAction::end, track);
0091 m_final(track);
0092 }
0093
0094
0095 Geant4TrackingAction::Geant4TrackingAction(Geant4Context* ctxt, const std::string& nam)
0096 : Geant4Action(ctxt, nam) {
0097 InstanceCount::increment(this);
0098 }
0099
0100
0101 Geant4TrackingAction::~Geant4TrackingAction() {
0102 InstanceCount::decrement(this);
0103 }
0104
0105
0106 void Geant4TrackingAction::begin(const G4Track*) {
0107 }
0108
0109
0110 void Geant4TrackingAction::end(const G4Track*) {
0111 }
0112
0113
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
0120 Geant4SharedTrackingAction::Geant4SharedTrackingAction(Geant4Context* ctxt, const std::string& nam)
0121 : Geant4TrackingAction(ctxt, nam), m_action(0)
0122 {
0123 InstanceCount::increment(this);
0124 }
0125
0126
0127 Geant4SharedTrackingAction::~Geant4SharedTrackingAction() {
0128 detail::releasePtr(m_action);
0129 InstanceCount::decrement(this);
0130 }
0131
0132
0133 void Geant4SharedTrackingAction::configureFiber(Geant4Context* thread_context) {
0134 m_action->configureFiber(thread_context);
0135 }
0136
0137
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
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
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 }