ATLAS Offline Software
Loading...
Searching...
No Matches
G4UA::UserActionSvc Class Reference

A service which manages the user actions for G4 simulation. More...

#include <UserActionSvc.h>

Inheritance diagram for G4UA::UserActionSvc:
Collaboration diagram for G4UA::UserActionSvc:

Public Member Functions

 UserActionSvc (const std::string &name, ISvcLocator *pSvcLocator)
 Standard constructor.
StatusCode initialize () override
 Initialize the service.
StatusCode initializeActionsMaster () override final
 Initialize the user run actions for the main thread.
StatusCode initializeActions () override final
 Initialize the user actions for the current thread.
StatusCode getSecondaryActions (std::vector< G4UserSteppingAction * > &actions) override final
StatusCode addActionTool (const ToolHandle< IUserActionTool > &service_tool) override final
void G4RunTool (IG4RunTool *) override final
StatusCode BeginOfAthenaEvent (HitCollectionMap &) override final
 Calls BeginOfAthenaEvent on each UserAction tool.
StatusCode EndOfAthenaEvent (HitCollectionMap &) override final
 Calls EndOfAthenaEvent on each UserAction tool.

Private Attributes

Handles to ATLAS action tools
ToolHandleArray< IUserActionToolm_userActionTools
 User action tools.
ATLAS Geant4 user actions
ThreadActionHolder< G4AtlasRunActionm_runActions
 Thread-local run action.
ThreadActionHolder< G4AtlasPrimaryGeneratorActionm_primaryGeneratorActions
 Thread-local primary generator action.
ThreadActionHolder< G4AtlasEventActionm_eventActions
 Thread-local event action.
ThreadActionHolder< G4AtlasStackingActionm_stackingActions
 Thread-local stacking action.
ThreadActionHolder< G4AtlasTrackingActionm_trackingActions
 Thread-local tracking action.
ThreadActionHolder< G4AtlasSteppingActionm_steppingActions
 Thread-local stepping action.

Detailed Description

A service which manages the user actions for G4 simulation.

Author
Steve Farrell Steve.nosp@m.n.Fa.nosp@m.rrell.nosp@m.@cer.nosp@m.n.ch

Definition at line 35 of file UserActionSvc.h.

Constructor & Destructor Documentation

◆ UserActionSvc()

G4UA::UserActionSvc::UserActionSvc ( const std::string & name,
ISvcLocator * pSvcLocator )

Standard constructor.

Definition at line 21 of file UserActionSvc.cxx.

23 : base_class(name, pSvcLocator)
24 , m_userActionTools(this)
25 {
26 declareProperty("UserActionTools", m_userActionTools);
27 }
ToolHandleArray< IUserActionTool > m_userActionTools
User action tools.

Member Function Documentation

◆ addActionTool()

StatusCode G4UA::UserActionSvc::addActionTool ( const ToolHandle< IUserActionTool > & service_tool)
finaloverride

Definition at line 50 of file UserActionSvc.cxx.

50 {
51
52 if (!service_tool.isPublic()){
53 ATH_MSG_FATAL("Only public tools are allowed");
54 return StatusCode::FAILURE;
55 }
56 if (service_tool.empty()) {
57 ATH_MSG_FATAL("NO point in adding empty tool handles here.");
58 return StatusCode::FAILURE;
59 }
60 ATH_MSG_INFO(" Add new tool "<<service_tool.name());
61 m_userActionTools.push_back(service_tool);
62 return StatusCode::SUCCESS;
63 }
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)

◆ BeginOfAthenaEvent()

StatusCode G4UA::UserActionSvc::BeginOfAthenaEvent ( HitCollectionMap & hitCollections)
finaloverride

Calls BeginOfAthenaEvent on each UserAction tool.

Definition at line 223 of file UserActionSvc.cxx.

224 {
225 for(auto& actionTool : m_userActionTools) {
226 if(actionTool->BeginOfAthenaEvent(hitCollections).isFailure()) {
227 ATH_MSG_ERROR("BeginOfAthenaEvent failed for " << actionTool->name());
228 return StatusCode::FAILURE;
229 }
230 }
231 return StatusCode::SUCCESS;
232 }
#define ATH_MSG_ERROR(x)

◆ EndOfAthenaEvent()

StatusCode G4UA::UserActionSvc::EndOfAthenaEvent ( HitCollectionMap & hitCollections)
finaloverride

Calls EndOfAthenaEvent on each UserAction tool.

Definition at line 234 of file UserActionSvc.cxx.

235 {
236 for(auto& actionTool : m_userActionTools) {
237 if(actionTool->EndOfAthenaEvent(hitCollections).isFailure()) {
238 ATH_MSG_ERROR("EndOfAthenaEvent failed for " << actionTool->name());
239 return StatusCode::FAILURE;
240 }
241 }
242 return StatusCode::SUCCESS;
243 }

◆ G4RunTool()

void G4UA::UserActionSvc::G4RunTool ( IG4RunTool * g4RunTool)
finaloverride

Definition at line 29 of file UserActionSvc.cxx.

29 {
30 for (auto& tool : m_userActionTools) {
31 tool->G4RunTool(g4RunTool);
32 }
33 }

◆ getSecondaryActions()

StatusCode G4UA::UserActionSvc::getSecondaryActions ( std::vector< G4UserSteppingAction * > & actions)
finaloverride

Definition at line 215 of file UserActionSvc.cxx.

215 {
216
217 // Only stepping actions can return secondaries? Maybe turn this into a templated method
218 actions = m_steppingActions.get()->getActions();
219
220 return StatusCode::SUCCESS;
221 }
ThreadActionHolder< G4AtlasSteppingAction > m_steppingActions
Thread-local stepping action.

◆ initialize()

StatusCode G4UA::UserActionSvc::initialize ( )
override

Initialize the service.

Definition at line 38 of file UserActionSvc.cxx.

39 {
40 ATH_MSG_INFO("Initializing " << m_userActionTools.size() << " user action tools.");
41
42 for(const auto& action : m_userActionTools) {
43 ATH_MSG_INFO( " -> " << action.name() );
44 }
45
46 ATH_CHECK( m_userActionTools.retrieve() );
47
48 return StatusCode::SUCCESS;
49 }
#define ATH_CHECK
Evaluate an expression and check for errors.

◆ initializeActions()

StatusCode G4UA::UserActionSvc::initializeActions ( )
finaloverride

Initialize the user actions for the current thread.

Definition at line 91 of file UserActionSvc.cxx.

92 {
93 // This method is called concurrently in AthenaMT during initialization.
94 // We conservatively just lock the whole thing to protect MsgStream and
95 // downstream code.
96 static std::mutex userActionMutex;
97 std::lock_guard<std::mutex> userActionLock(userActionMutex);
98
99 ATH_MSG_DEBUG("initializeActions");
100
101 // sanity check: this is to make sure there are no other instances of this
102 // svc that have been already initialized, or that nobody else in some other
103 // code has already been registering actions to the run manager, which would
104 // be a major error in the configuration checking the run action is probably
105 // enough fo multiple instances of this service, since all roles are usually
106 // set at the same time. but other code may as well have registered just one
107 // role, so it is safer to do all checks here.
108
109 if( G4RunManager::GetRunManager()->GetUserRunAction() ||
110 G4RunManager::GetRunManager()->GetUserPrimaryGeneratorAction() ||
111 G4RunManager::GetRunManager()->GetUserEventAction() ||
112 G4RunManager::GetRunManager()->GetUserStackingAction() ||
113 G4RunManager::GetRunManager()->GetUserTrackingAction() ||
114 G4RunManager::GetRunManager()->GetUserSteppingAction() )
115 {
116 ATH_MSG_FATAL("UserActionSvc has found that actions were already " <<
117 "registered to the G4RunManager. Check your code/configuration");
118 return StatusCode::FAILURE;
119 }
120
121 // Retrieve the new user actions
122 G4AtlasUserActions actions;
123 for(auto& tool : m_userActionTools) {
124 ATH_CHECK( tool->fillUserAction(actions) );
125 }
126
127 // Initialize the ATLAS run action.
128 if(m_runActions.get()) {
129 ATH_MSG_ERROR("Run action already exists for current thread!");
130 return StatusCode::FAILURE;
131 }
132 auto runAction = std::make_unique<G4AtlasRunAction>();
133 // Assign run plugins
134 for(auto* action : actions.runActions)
135 runAction->addRunAction(action);
136 G4RunManager::GetRunManager()->SetUserAction( runAction.get() );
137 m_runActions.set( std::move(runAction) );
138
139 // Initialize the ATLAS primary generator action.
140 if(m_primaryGeneratorActions.get()) {
141 ATH_MSG_ERROR("Primary generator action already exists for current thread!");
142 return StatusCode::FAILURE;
143 }
144 auto primaryGeneratorAction = std::make_unique<G4AtlasPrimaryGeneratorAction>();
145 // Assign run plugins
146 for(auto* action : actions.primaryGeneratorActions)
147 primaryGeneratorAction->addPrimaryGeneratorAction(action);
148 G4RunManager::GetRunManager()->SetUserAction( primaryGeneratorAction.get() );
149 m_primaryGeneratorActions.set( std::move(primaryGeneratorAction) );
150
151 // Initialize the ATLAS event action.
152 if(m_eventActions.get()) {
153 ATH_MSG_ERROR("Event action already exists for current thread!");
154 return StatusCode::FAILURE;
155 }
156 auto eventAction = std::make_unique<G4AtlasEventAction>();
157 // Assign event plugins
158 for(auto* action : actions.eventActions) {
159 eventAction->addEventAction(action);
160 // set the event manager
161 action->SetEventManager( G4EventManager::GetEventManager() );
162 }
163 G4RunManager::GetRunManager()->SetUserAction( eventAction.get() );
164 m_eventActions.set( std::move(eventAction) );
165
166 // Initialize the ATLAS stacking action.
167 if(m_stackingActions.get()) {
168 ATH_MSG_ERROR("Stacking action already exists for current thread!");
169 return StatusCode::FAILURE;
170 }
171 auto stackAction = std::make_unique<G4AtlasStackingAction>();
172 // Assign stacking plugins
173 for(auto* action : actions.stackingActions) {
174 stackAction->addAction(action);
175 // set the stack manager
176 action->SetStackManager( G4EventManager::GetEventManager()->GetStackManager() );
177 }
178 G4RunManager::GetRunManager()->SetUserAction( stackAction.get() );
179 m_stackingActions.set( std::move(stackAction) );
180
181 // Initialize the ATLAS tracking action.
182 if(m_trackingActions.get()) {
183 ATH_MSG_ERROR("Tracking action already exists for current thread!");
184 return StatusCode::FAILURE;
185 }
186 auto trackAction = std::make_unique<G4AtlasTrackingAction>();
187 // Assign tracking plugins
188 for(auto* action : actions.trackingActions) {
189 trackAction->addTrackAction(action);
190 // set the tracking manager
191 action->SetTrackingManagerPointer ( G4EventManager::GetEventManager()->GetTrackingManager() );
192 }
193 G4RunManager::GetRunManager()->SetUserAction( trackAction.get() );
194 m_trackingActions.set( std::move(trackAction) );
195
196 // Initialize the ATLAS stepping action.
197 if(m_steppingActions.get()) {
198 ATH_MSG_ERROR("Stepping action already exists for current thread!");
199 return StatusCode::FAILURE;
200 }
201 auto stepAction = std::make_unique<G4AtlasSteppingAction>();
202 // Assign stepping plugins
203 for(auto* action : actions.steppingActions) {
204 stepAction->addAction(action);
205 // set the stepping manager
206 action->SetSteppingManagerPointer( G4EventManager::GetEventManager()->GetTrackingManager()->GetSteppingManager() );
207 }
208 G4RunManager::GetRunManager()->SetUserAction( stepAction.get() );
209 m_steppingActions.set( std::move(stepAction) );
210
211 return StatusCode::SUCCESS;
212 }
#define ATH_MSG_DEBUG(x)
ThreadActionHolder< G4AtlasEventAction > m_eventActions
Thread-local event action.
ThreadActionHolder< G4AtlasPrimaryGeneratorAction > m_primaryGeneratorActions
Thread-local primary generator action.
ThreadActionHolder< G4AtlasTrackingAction > m_trackingActions
Thread-local tracking action.
ThreadActionHolder< G4AtlasStackingAction > m_stackingActions
Thread-local stacking action.
ThreadActionHolder< G4AtlasRunAction > m_runActions
Thread-local run action.

◆ initializeActionsMaster()

StatusCode G4UA::UserActionSvc::initializeActionsMaster ( )
finaloverride

Initialize the user run actions for the main thread.

Definition at line 68 of file UserActionSvc.cxx.

69 {
70 // Retrieve the new user actions
71 G4AtlasUserActions actions;
72 for(auto& tool : m_userActionTools) {
73 ATH_CHECK( tool->fillUserAction(actions) );
74 }
75 auto runAction = std::make_unique<G4AtlasRunAction>();
76 // Assign run plugins
77 for(auto* action : actions.runActionsMaster)
78 {
79 runAction->addRunAction(action);
80 }
81 G4RunManager::GetRunManager()->SetUserAction( runAction.release() );
82 return StatusCode::SUCCESS;
83 }

Member Data Documentation

◆ m_eventActions

ThreadActionHolder<G4AtlasEventAction> G4UA::UserActionSvc::m_eventActions
private

Thread-local event action.

Definition at line 81 of file UserActionSvc.h.

◆ m_primaryGeneratorActions

ThreadActionHolder<G4AtlasPrimaryGeneratorAction> G4UA::UserActionSvc::m_primaryGeneratorActions
private

Thread-local primary generator action.

Definition at line 79 of file UserActionSvc.h.

◆ m_runActions

ThreadActionHolder<G4AtlasRunAction> G4UA::UserActionSvc::m_runActions
private

Thread-local run action.

Definition at line 77 of file UserActionSvc.h.

◆ m_stackingActions

ThreadActionHolder<G4AtlasStackingAction> G4UA::UserActionSvc::m_stackingActions
private

Thread-local stacking action.

Definition at line 83 of file UserActionSvc.h.

◆ m_steppingActions

ThreadActionHolder<G4AtlasSteppingAction> G4UA::UserActionSvc::m_steppingActions
private

Thread-local stepping action.

Definition at line 87 of file UserActionSvc.h.

◆ m_trackingActions

ThreadActionHolder<G4AtlasTrackingAction> G4UA::UserActionSvc::m_trackingActions
private

Thread-local tracking action.

Definition at line 85 of file UserActionSvc.h.

◆ m_userActionTools

ToolHandleArray<IUserActionTool> G4UA::UserActionSvc::m_userActionTools
private

User action tools.

Definition at line 69 of file UserActionSvc.h.


The documentation for this class was generated from the following files: