File indexing completed on 2025-12-15 09:28:06
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DD4hep/InstanceCount.h>
0016 #include <DDG4/Geant4GeneratorAction.h>
0017 #include <DDG4/Geant4Kernel.h>
0018
0019
0020 #include <G4Threading.hh>
0021 #include <G4AutoLock.hh>
0022
0023 using namespace dd4hep::sim;
0024
0025 namespace {
0026 G4Mutex action_mutex=G4MUTEX_INITIALIZER;
0027 }
0028
0029
0030 Geant4GeneratorAction::Geant4GeneratorAction(Geant4Context* ctxt, const std::string& nam)
0031 : Geant4Action(ctxt, nam) {
0032 InstanceCount::increment(this);
0033 }
0034
0035
0036 Geant4GeneratorAction::~Geant4GeneratorAction() {
0037 InstanceCount::decrement(this);
0038 }
0039
0040
0041 Geant4SharedGeneratorAction::Geant4SharedGeneratorAction(Geant4Context* ctxt, const std::string& nam)
0042 : Geant4GeneratorAction(ctxt, nam), m_action(0)
0043 {
0044 InstanceCount::increment(this);
0045 }
0046
0047
0048 Geant4SharedGeneratorAction::~Geant4SharedGeneratorAction() {
0049 detail::releasePtr(m_action);
0050 InstanceCount::decrement(this);
0051 }
0052
0053
0054 void Geant4SharedGeneratorAction::configureFiber(Geant4Context* thread_context) {
0055 m_action->configureFiber(thread_context);
0056 }
0057
0058
0059 void Geant4SharedGeneratorAction::use(Geant4GeneratorAction* action) {
0060 if (action) {
0061 action->addRef();
0062 m_properties.adopt(action->properties());
0063 m_action = action;
0064 return;
0065 }
0066 throw std::runtime_error("Geant4SharedGeneratorAction: Attempt to use invalid actor!");
0067 }
0068
0069
0070 void Geant4SharedGeneratorAction::operator()(G4Event* event) {
0071 if ( m_action ) {
0072 G4AutoLock protection_lock(&action_mutex); {
0073 ContextSwap swap(m_action,context());
0074 (*m_action)(event);
0075 }
0076 }
0077 }
0078
0079
0080 Geant4GeneratorActionSequence::Geant4GeneratorActionSequence(Geant4Context* ctxt, const std::string& nam)
0081 : Geant4Action(ctxt, nam) {
0082 m_needsControl = true;
0083 InstanceCount::increment(this);
0084 }
0085
0086
0087 Geant4GeneratorActionSequence::~Geant4GeneratorActionSequence() {
0088 m_actors(&Geant4GeneratorAction::release);
0089 m_actors.clear();
0090 m_calls.clear();
0091 InstanceCount::decrement(this);
0092 }
0093
0094
0095 void Geant4GeneratorActionSequence::updateContext(Geant4Context* ctxt) {
0096 m_context = ctxt;
0097 m_actors.updateContext(ctxt);
0098 }
0099
0100
0101 void Geant4GeneratorActionSequence::configureFiber(Geant4Context* thread_context) {
0102 m_actors(&Geant4Action::configureFiber, thread_context);
0103 }
0104
0105
0106 Geant4GeneratorAction* Geant4GeneratorActionSequence::get(const std::string& nam) const {
0107 return m_actors.get(FindByName(TypeName::split(nam).second));
0108 }
0109
0110
0111 void Geant4GeneratorActionSequence::adopt(Geant4GeneratorAction* action) {
0112 if (action) {
0113 G4AutoLock protection_lock(&action_mutex);
0114 action->addRef();
0115 m_actors.add(action);
0116 return;
0117 }
0118 except("Attempt to add invalid actor!");
0119 }
0120
0121
0122 void Geant4GeneratorActionSequence::operator()(G4Event* event) {
0123 if ( context()->kernel().processEvents() ) {
0124 m_actors(&Geant4GeneratorAction::operator(), event);
0125 m_calls(event);
0126 return;
0127 }
0128 throw DD4hep_Stop_Processing();
0129 }