GGS(GenericGEANT4Simulation)Software  2.99.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Macros
GGSMultiUserAction.cpp
Go to the documentation of this file.
1 /*
2  * GGSMultiUserAction.cpp
3  *
4  * Created on: 26 Oct 2020
5  * Author: Nicola Mori
6  */
7 
13 
14 #include "G4RunManager.hh"
15 
16 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
17 
19  : GGSUserAction(), _messenger(this, "/GGS/userActions/", "User actions build messenger") {}
20 
21 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
22 
24 
25  // Warning: this destructor will be called by ~G4RunManager, which runs after
26  // ~GGSRunManager. So at this stage the child GGSRunManager is no
27  // more available and thus the mother G4Manager must be referenced.
28  // TODO: handle the destruction of the actions manager inside ~GGSRunManager, when
29  // the GGSRunManager interface has been properly extended to handle the
30  // actions manager.
31  G4RunManager *runManager = G4RunManager::GetRunManager();
32  if (runManager->GetUserSteppingAction() == this)
33  runManager->SetUserAction((G4UserSteppingAction *)0);
34  if (runManager->GetUserTrackingAction() == this)
35  runManager->SetUserAction((G4UserTrackingAction *)0);
36  if (runManager->GetUserEventAction() == this)
37  runManager->SetUserAction((G4UserEventAction *)0);
38  if (runManager->GetUserRunAction() == this)
39  runManager->SetUserAction((G4UserRunAction *)0);
40  if (runManager->GetUserStackingAction() == this)
41  runManager->SetUserAction((G4UserStackingAction *)0);
42 }
43 
44 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
45 
46 void GGSMultiUserAction::AddAction(std::unique_ptr<GGSUserAction> &&userAction) {
47  if (userAction) {
48  _userActions.push_back(std::move(userAction));
49  _userActions.back()->SetTrackingManagerPointer(fpTrackingManager);
50  }
51 }
52 
53 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
54 
55 void GGSMultiUserAction::AddAction(std::unique_ptr<G4UserSteppingAction> &&steppingAction) {
56  if (steppingAction) {
57  _steppingActions.push_back(std::move(steppingAction));
58  }
59 }
60 
61 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
62 
63 void GGSMultiUserAction::AddAction(std::unique_ptr<G4UserTrackingAction> &&trackingAction) {
64  if (trackingAction) {
65  _trackingActions.push_back(std::move(trackingAction));
66  _trackingActions.back()->SetTrackingManagerPointer(fpTrackingManager);
67  }
68 }
69 
70 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
71 
72 void GGSMultiUserAction::AddAction(std::unique_ptr<G4UserEventAction> &&eventAction) {
73  if (eventAction) {
74  _eventActions.push_back(std::move(eventAction));
75  }
76 }
77 
78 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
79 
80 void GGSMultiUserAction::AddAction(std::unique_ptr<G4UserRunAction> &&runAction) {
81  if (runAction) {
82  _runActions.push_back(std::move(runAction));
83  }
84 }
85 
86 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
87 
88 void GGSMultiUserAction::AddAction(std::unique_ptr<G4UserStackingAction> &&stackingAction) {
89  if (stackingAction) {
90  _stackingActions.push_back(std::move(stackingAction));
91  }
92 }
93 
94 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
95 
96 void GGSMultiUserAction::UserSteppingAction(const G4Step *step) {
97 
98  // Call routine for general actions
99  for (unsigned int i = 0; i < _userActions.size(); i++)
100  _userActions[i]->UserSteppingAction(step);
101 
102  // Call routine for stepping actions
103  for (unsigned int i = 0; i < _steppingActions.size(); i++)
104  _steppingActions[i]->UserSteppingAction(step);
105 }
106 
107 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
108 
109 void GGSMultiUserAction::PreUserTrackingAction(const G4Track *track) {
110 
111  // Call routine for general actions
112  for (unsigned int i = 0; i < _userActions.size(); i++)
113  _userActions[i]->PreUserTrackingAction(track);
114 
115  // Call routine for tracking actions
116  for (unsigned int i = 0; i < _trackingActions.size(); i++)
117  _trackingActions[i]->PreUserTrackingAction(track);
118 }
119 
120 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
121 
122 void GGSMultiUserAction::PostUserTrackingAction(const G4Track *track) {
123 
124  // Call routine for general actions
125  for (unsigned int i = 0; i < _userActions.size(); i++)
126  _userActions[i]->PostUserTrackingAction(track);
127 
128  // Call routine for tracking actions
129  for (unsigned int i = 0; i < _trackingActions.size(); i++)
130  _trackingActions[i]->PostUserTrackingAction(track);
131 }
132 
133 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
134 
135 void GGSMultiUserAction::BeginOfEventAction(const G4Event *event) {
136 
137  // Call routine for general actions
138  for (unsigned int i = 0; i < _userActions.size(); i++)
139  _userActions[i]->BeginOfEventAction(event);
140 
141  // Call routine for event actions
142  for (unsigned int i = 0; i < _eventActions.size(); i++)
143  _eventActions[i]->BeginOfEventAction(event);
144 }
145 
146 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
147 
148 void GGSMultiUserAction::EndOfEventAction(const G4Event *event) {
149 
150  GGSRunManagerExtensions &rmExt = dynamic_cast<GGSRunManagerExtensions &>(*(G4RunManager::GetRunManager()));
151  if (!(rmExt.IsCurrentEventKilled())) {
152 
153  // Call routine for general actions
154  for (unsigned int i = 0; i < _userActions.size(); i++)
155  _userActions[i]->EndOfEventAction(event);
156 
157  // Call routine for event actions
158  for (unsigned int i = 0; i < _eventActions.size(); i++)
159  _eventActions[i]->EndOfEventAction(event);
160 
161  GGSRootFileService::GetInstance()._FillDefaultEventsTrees();
162  }
163 }
164 
165 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
166 
167 void GGSMultiUserAction::BeginOfRunAction(const G4Run *run) {
168 
169  // Call routine for general actions
170  for (unsigned int i = 0; i < _userActions.size(); i++)
171  _userActions[i]->BeginOfRunAction(run);
172 
173  // Call routine for run actions
174  for (unsigned int i = 0; i < _runActions.size(); i++)
175  _runActions[i]->BeginOfRunAction(run);
176 }
177 
178 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
179 
180 void GGSMultiUserAction::EndOfRunAction(const G4Run *run) {
181 
182  // Call routine for general actions
183  for (unsigned int i = 0; i < _userActions.size(); i++)
184  _userActions[i]->EndOfRunAction(run);
185 
186  // Call routine for run actions
187  for (unsigned int i = 0; i < _runActions.size(); i++)
188  _runActions[i]->EndOfRunAction(run);
189 }
190 
191 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
192 
193 G4ClassificationOfNewTrack GGSMultiUserAction::ClassifyNewTrack(const G4Track *aTrack) {
194 
195  int classif, actionClassif;
196  classif = -100;
197  GGSUserAction *classifierUA = nullptr;
198  G4UserStackingAction *classifierSA = nullptr;
199 
200  for (auto &actionPtr : _userActions) {
201  actionClassif = actionPtr->ClassifyNewTrack(aTrack);
202  if (actionClassif != -100) {
203  if (actionClassif != classif && classif != -100) {
204  G4ExceptionDescription ed;
205  std::string classifierUAClassName = typeid(*classifierUA).name();
206  auto *actionRawPtr = actionPtr.get(); // Call the unique_ptr method here to avoid a clang warning about possible
207  // side effects if calling it inside typeid
208  std::string thisUAClassName = typeid(*actionRawPtr).name();
209  ed << "Classification of track " << aTrack->GetTrackID() << " from GGS action " << classifierUAClassName << " ("
210  << classif << ") is different from classification from GGS action " << thisUAClassName << " ("
211  << actionClassif << ")";
212  G4Exception("GGSMultiUserAction::ClassifyNewTrack", "Clashing track classifications",
213  G4ExceptionSeverity::FatalException, ed);
214  } else {
215  classif = actionClassif;
216  classifierUA = actionPtr.get();
217  }
218  }
219  }
220  for (auto &actionPtr : _stackingActions) {
221  actionClassif = actionPtr->ClassifyNewTrack(aTrack);
222  if (actionClassif != classif && classif != -100) {
223  G4ExceptionDescription ed;
224  std::string classifierActionType, classifierClassName;
225  if (classifierUA != nullptr) {
226  classifierActionType = "GGS";
227  classifierClassName = typeid(*classifierUA).name();
228  } else if (classifierSA != nullptr) {
229  classifierActionType = "stacking";
230  classifierClassName = typeid(*classifierSA).name();
231  }
232  auto *actionRawPtr = actionPtr.get(); // Call the unique_ptr method here to avoid a clang warning about possible
233  // side effects if calling it inside typeid
234  auto thisSAClassName = typeid(*actionRawPtr).name();
235  ed << "Classification of track " << aTrack->GetTrackID() << " from " << classifierActionType << " action "
236  << classifierClassName << " (" << classif << ") is different from classification from stacking action "
237  << thisSAClassName << " (" << actionClassif << ")";
238  G4Exception("GGSMultiUserAction::ClassifyNewTrack", "Clashing track classifications",
239  G4ExceptionSeverity::FatalException, ed);
240  } else {
241  classif = actionClassif;
242  classifierSA = actionPtr.get();
243  }
244  }
245  if (classif == -100) {
246  return G4UserStackingAction::ClassifyNewTrack(aTrack);
247  } else {
248  return (G4ClassificationOfNewTrack)classif;
249  }
250 }
251 
252 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
253 
255  for (unsigned int i = 0; i < _userActions.size(); i++) {
256  _userActions[i]->NewStage();
257  }
258 }
259 
260 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
261 
263 
264  for (unsigned int i = 0; i < _userActions.size(); i++) {
265  _userActions[i]->PrepareNewEvent();
266  }
267 }
void BeginOfRunAction(const G4Run *run)
Override of BeginOfRunAction method.
GGSMultiUserAction()
constructor.
void PostUserTrackingAction(const G4Track *track)
Override of PostUserTrackingAction method.
void UserSteppingAction(const G4Step *step)
Override of UserSteppingAction method.
G4ClassificationOfNewTrack ClassifyNewTrack(const G4Track *)
Override of the ClassifyNewTrack method.
Definition: GGSUserAction.h:54
void PrepareNewEvent()
Override of PrepareNewEvent method.
G4ClassificationOfNewTrack ClassifyNewTrack(const G4Track *aTrack)
Override of ClassifyNewTrack method.
void EndOfRunAction(const G4Run *run)
Override of EndOfRunAction method.
Class with additional functionalities for run managers.
void NewStage()
Override of NewStage method.
bool IsCurrentEventKilled()
Getter method for killed event flag.
~GGSMultiUserAction()
Destructor.
void AddAction(std::unique_ptr< GGSUserAction > &&userAction)
Adds a general user action.
static GGSRootFileService & GetInstance()
Get reference to GGSRootFileService unique instance.
void EndOfEventAction(const G4Event *event)
Override of EndOfEventAction method.
void BeginOfEventAction(const G4Event *event)
Override of BeginOfEventAction method.
Mother class for user actions in GGS.
Definition: GGSUserAction.h:27
void PreUserTrackingAction(const G4Track *track)
Override of PreUserTrackingAction method.