File indexing completed on 2025-01-18 09:14:25
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_action = action;
0063 return;
0064 }
0065 throw std::runtime_error("Geant4SharedGeneratorAction: Attempt to use invalid actor!");
0066 }
0067
0068
0069 void Geant4SharedGeneratorAction::operator()(G4Event* event) {
0070 if ( m_action ) {
0071 G4AutoLock protection_lock(&action_mutex); {
0072 ContextSwap swap(m_action,context());
0073 (*m_action)(event);
0074 }
0075 }
0076 }
0077
0078
0079 Geant4GeneratorActionSequence::Geant4GeneratorActionSequence(Geant4Context* ctxt, const std::string& nam)
0080 : Geant4Action(ctxt, nam) {
0081 m_needsControl = true;
0082 InstanceCount::increment(this);
0083 }
0084
0085
0086 Geant4GeneratorActionSequence::~Geant4GeneratorActionSequence() {
0087 m_actors(&Geant4GeneratorAction::release);
0088 m_actors.clear();
0089 m_calls.clear();
0090 InstanceCount::decrement(this);
0091 }
0092
0093
0094 void Geant4GeneratorActionSequence::updateContext(Geant4Context* ctxt) {
0095 m_context = ctxt;
0096 m_actors.updateContext(ctxt);
0097 }
0098
0099
0100 void Geant4GeneratorActionSequence::configureFiber(Geant4Context* thread_context) {
0101 m_actors(&Geant4Action::configureFiber, thread_context);
0102 }
0103
0104
0105 Geant4GeneratorAction* Geant4GeneratorActionSequence::get(const std::string& nam) const {
0106 return m_actors.get(FindByName(TypeName::split(nam).second));
0107 }
0108
0109
0110 void Geant4GeneratorActionSequence::adopt(Geant4GeneratorAction* action) {
0111 if (action) {
0112 G4AutoLock protection_lock(&action_mutex);
0113 action->addRef();
0114 m_actors.add(action);
0115 return;
0116 }
0117 except("Attempt to add invalid actor!");
0118 }
0119
0120
0121 void Geant4GeneratorActionSequence::operator()(G4Event* event) {
0122 if ( context()->kernel().processEvents() ) {
0123 m_actors(&Geant4GeneratorAction::operator(), event);
0124 m_calls(event);
0125 return;
0126 }
0127 throw DD4hep_Stop_Processing();
0128 }