Newer
Older
Markus Frank
committed
//==========================================================================
Markus Frank
committed
//--------------------------------------------------------------------------
// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
Markus Frank
committed
// All rights reserved.
Markus Frank
committed
// For the licensing terms see $DD4hepINSTALL/LICENSE.
// For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
Markus Frank
committed
// Author : M.Frank
//
//==========================================================================
// Framework include files
#include "DD4hep/InstanceCount.h"
#include "DDG4/Geant4SteppingAction.h"
Markus Frank
committed
// Geant4 headers
#include "G4Threading.hh"
#include "G4AutoLock.hh"
// C/C++ include files
#include <stdexcept>
Markus Frank
committed
Markus Frank
committed
using namespace std;
Markus Frank
committed
namespace {
G4Mutex action_mutex=G4MUTEX_INITIALIZER;
}
Markus Frank
committed
Geant4SteppingAction::Geant4SteppingAction(Geant4Context* ctxt, const string& nam)
Markus Frank
committed
: Geant4Action(ctxt, nam) {
InstanceCount::increment(this);
}
/// Default destructor
Geant4SteppingAction::~Geant4SteppingAction() {
InstanceCount::decrement(this);
}
/// User stepping callback
void Geant4SteppingAction::operator()(const G4Step*, G4SteppingManager*) {
Markus Frank
committed
Geant4SharedSteppingAction::Geant4SharedSteppingAction(Geant4Context* ctxt, const string& nam)
: Geant4SteppingAction(ctxt, nam), m_action(0)
Markus Frank
committed
{
InstanceCount::increment(this);
}
/// Default destructor
Geant4SharedSteppingAction::~Geant4SharedSteppingAction() {
Markus Frank
committed
InstanceCount::decrement(this);
}
/// Underlying object to be used during the execution of this thread
void Geant4SharedSteppingAction::use(Geant4SteppingAction* action) {
if (action) {
action->addRef();
Markus Frank
committed
m_properties.adopt(action->properties());
Markus Frank
committed
m_action = action;
return;
}
throw runtime_error("Geant4SharedSteppingAction: Attempt to use invalid actor!");
}
/// Set or update client for the use in a new thread fiber
void Geant4SharedSteppingAction::configureFiber(Geant4Context* thread_context) {
m_action->configureFiber(thread_context);
}
/// User stepping callback
void Geant4SharedSteppingAction::operator()(const G4Step* s, G4SteppingManager* m) {
if ( m_action ) {
G4AutoLock protection_lock(&action_mutex); {
ContextSwap swap(m_action,context());
(*m_action)(s,m);
}
}
}
/// Standard constructor
Geant4SteppingActionSequence::Geant4SteppingActionSequence(Geant4Context* ctxt, const string& nam)
Markus Frank
committed
: Geant4Action(ctxt, nam) {
InstanceCount::increment(this);
}
/// Default destructor
Geant4SteppingActionSequence::~Geant4SteppingActionSequence() {
m_actors(&Geant4SteppingAction::release);
m_actors.clear();
m_calls.clear();
InstanceCount::decrement(this);
}
Markus Frank
committed
/// Set or update client for the use in a new thread fiber
void Geant4SteppingActionSequence::configureFiber(Geant4Context* thread_context) {
m_actors(&Geant4Action::configureFiber, thread_context);
}
/// Set or update client context
void Geant4SteppingActionSequence::updateContext(Geant4Context* ctxt) {
m_context = ctxt;
m_actors.updateContext(ctxt);
}
/// Get an action by name
Geant4SteppingAction* Geant4SteppingActionSequence::get(const string& nam) const {
return m_actors.get(FindByName(TypeName::split(nam).second));
}
void Geant4SteppingActionSequence::operator()(const G4Step* step, G4SteppingManager* mgr) {
m_actors(&Geant4SteppingAction::operator(), step, mgr);
m_calls(step, mgr);
}
/// Add an actor responding to all callbacks. Sequence takes ownership.
void Geant4SteppingActionSequence::adopt(Geant4SteppingAction* action) {
if (action) {
Markus Frank
committed
G4AutoLock protection_lock(&action_mutex);
action->addRef();
m_actors.add(action);
return;
}
Markus Frank
committed
throw runtime_error("Geant4SteppingActionSequence: Attempt to add invalid actor!");