Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:14:25

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/Geant4GeneratorAction.h>
0017 #include <DDG4/Geant4Kernel.h>
0018 
0019 // Geant4 headers
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 /// Standard constructor
0030 Geant4GeneratorAction::Geant4GeneratorAction(Geant4Context* ctxt, const std::string& nam)
0031   : Geant4Action(ctxt, nam) {
0032   InstanceCount::increment(this);
0033 }
0034 
0035 /// Default destructor
0036 Geant4GeneratorAction::~Geant4GeneratorAction() {
0037   InstanceCount::decrement(this);
0038 }
0039 
0040 /// Standard constructor
0041 Geant4SharedGeneratorAction::Geant4SharedGeneratorAction(Geant4Context* ctxt, const std::string& nam)
0042   : Geant4GeneratorAction(ctxt, nam), m_action(0)
0043 {
0044   InstanceCount::increment(this);
0045 }
0046 
0047 /// Default destructor
0048 Geant4SharedGeneratorAction::~Geant4SharedGeneratorAction()   {
0049   detail::releasePtr(m_action);
0050   InstanceCount::decrement(this);
0051 }
0052 
0053 /// Set or update client for the use in a new thread fiber
0054 void Geant4SharedGeneratorAction::configureFiber(Geant4Context* thread_context)   {
0055   m_action->configureFiber(thread_context);
0056 }
0057 
0058 /// Underlying object to be used during the execution of this thread
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 /// User generator callback
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 /// Standard constructor
0079 Geant4GeneratorActionSequence::Geant4GeneratorActionSequence(Geant4Context* ctxt, const std::string& nam)
0080   : Geant4Action(ctxt, nam) {
0081   m_needsControl = true;
0082   InstanceCount::increment(this);
0083 }
0084 
0085 /// Default destructor
0086 Geant4GeneratorActionSequence::~Geant4GeneratorActionSequence() {
0087   m_actors(&Geant4GeneratorAction::release);
0088   m_actors.clear();
0089   m_calls.clear();
0090   InstanceCount::decrement(this);
0091 }
0092 
0093 /// Set or update client context
0094 void Geant4GeneratorActionSequence::updateContext(Geant4Context* ctxt)    {
0095   m_context = ctxt;
0096   m_actors.updateContext(ctxt);
0097 }
0098 
0099 /// Set or update client for the use in a new thread fiber
0100 void Geant4GeneratorActionSequence::configureFiber(Geant4Context* thread_context)   {
0101   m_actors(&Geant4Action::configureFiber, thread_context);
0102 }
0103 
0104 /// Get an action by name
0105 Geant4GeneratorAction* Geant4GeneratorActionSequence::get(const std::string& nam) const   {
0106   return m_actors.get(FindByName(TypeName::split(nam).second));
0107 }
0108 
0109 /// Add an actor responding to all callbacks. Sequence takes ownership.
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 /// Generator callback
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 }