LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
UserActionManager.cxx
Go to the documentation of this file.
1 
13 
15 #include "nug4/G4Base/UserAction.h"
16 
17 #include "Geant4/G4Run.hh"
18 #include "Geant4/G4Event.hh"
19 #include "Geant4/G4Track.hh"
20 #include "Geant4/G4Step.hh"
21 
22 #include <vector>
23 #include <map>
24 
25 namespace g4b {
26 
28 
29  //-------------------------------------------------
31  {
32  }
33 
34  //-------------------------------------------------
35  // Standard implementation of a singleton pattern.
37  {
39  return &instance;
40  }
41 
42  //-------------------------------------------------
44  {
45  // This destructor is probably never going to be called. If it
46  // is, make sure all the UserAction classes we manage are deleted
47  // properly.
48  Close();
49  }
50 
51 
52  //-------------------------------------------------
54  {
55  // Since we adopted the pointers for the user-action classes we're
56  // managing, delete all of them.
57  for ( fuserActions_t::iterator i = fuserActions.begin(); i != fuserActions.end(); i++ ){
58  delete *i;
59  }
60 
61  fuserActions.clear();
62  }
63 
64  //-------------------------------------------------
65  G4int UserActionManager::GetIndex(std::string const& name) const
66  {
67  int indx=0;
68  for ( fuserActions_ptr_t i = fuserActions.begin(); i != fuserActions.end(); i++, indx++ ){
69  if ( (*i)->GetName() == name ) return indx;
70  }
71  // not found
72  return -1;
73  }
74 
75  //-------------------------------------------------
76  UserAction* UserActionManager::GetAction(std::string const& name) const
77  {
78  G4int indx = GetIndex(name);
79  if ( indx < 0 ) return 0; // not found
80  return fuserActions[indx];
81  }
82 
83  //-------------------------------------------------
84  void UserActionManager::PrintActionList(std::string const& opt) const
85  {
86  bool pcfg = ( opt.find("config") != std::string::npos );
87  std::cout << "UserActionManager::PrintActionList " << GetSize()
88  << " entries" << std::endl;
89  for ( G4int indx=0; indx < GetSize(); ++indx ) {
90  UserAction* action = GetAction(indx);
91  std::cout << " [" << indx << "] " << action->GetName()
92  << ( action->ProvidesStacking() ? " [stacking]":"" )
93  << std::endl;
94  if ( pcfg ) action->PrintConfig(opt);
95  }
96  }
97 
98  //-------------------------------------------------
99  // For the rest of the UserAction methods: invoke the corresponding
100  // method for each of the user-action classes we're managing.
101 
102  // Reminder: i is a vector<UserAction*>::iterator
103  // *i is a UserAction*
104 
105  //-------------------------------------------------
106  void UserActionManager::BeginOfRunAction(const G4Run* a_run)
107  {
108  for ( fuserActions_ptr_t i = fuserActions.begin(); i != fuserActions.end(); i++ ){
109  (*i)->BeginOfRunAction(a_run);
110  }
111  }
112 
113  //-------------------------------------------------
114  void UserActionManager::EndOfRunAction(const G4Run* a_run)
115  {
116  for ( fuserActions_ptr_t i = fuserActions.begin(); i != fuserActions.end(); i++ ){
117  (*i)->EndOfRunAction(a_run);
118  }
119  }
120 
121  //-------------------------------------------------
122  void UserActionManager::BeginOfEventAction(const G4Event* a_event)
123  {
124  for ( fuserActions_ptr_t i = fuserActions.begin(); i != fuserActions.end(); i++ ){
125  (*i)->BeginOfEventAction(a_event);
126  }
127  }
128 
129  //-------------------------------------------------
130  void UserActionManager::EndOfEventAction(const G4Event* a_event)
131  {
132  for ( fuserActions_ptr_t i = fuserActions.begin(); i != fuserActions.end(); i++ ) {
133  (*i)->EndOfEventAction(a_event);
134  }
135  }
136 
137  //-------------------------------------------------
138  void UserActionManager::PreUserTrackingAction(const G4Track* a_track)
139  {
140  for ( fuserActions_ptr_t i = fuserActions.begin(); i != fuserActions.end(); i++ ){
141  (*i)->PreTrackingAction(a_track);
142  }
143  }
144 
145  //-------------------------------------------------
146  void UserActionManager::PostUserTrackingAction(const G4Track* a_track)
147  {
148  for ( fuserActions_ptr_t i = fuserActions.begin(); i != fuserActions.end(); i++ ){
149  (*i)->PostTrackingAction(a_track);
150  }
151  }
152 
153  //-------------------------------------------------
154  void UserActionManager::UserSteppingAction(const G4Step* a_step)
155  {
156  for ( fuserActions_ptr_t i = fuserActions.begin(); i != fuserActions.end(); i++ ){
157  (*i)->SteppingAction(a_step);
158  }
159  }
160 
161  //-------------------------------------------------
162  G4ClassificationOfNewTrack
163  UserActionManager::ClassifyNewTrack(const G4Track* a_track)
164  {
165  std::map<G4ClassificationOfNewTrack,int> stackChoices;
166  for ( fuserActions_ptr_t i = fuserActions.begin(); i != fuserActions.end(); i++ ){
167  if ( (*i)->ProvidesStacking() ) {
168  G4ClassificationOfNewTrack choice = (*i)->StackClassifyNewTrack(a_track);
169  stackChoices[choice]++;
170  }
171  }
172  // based on all results pick an action
173  // fUrgent, // put into the urgent stack
174  // fWaiting, // put into the waiting stack
175  // fPostpone, // postpone to the next event
176  // fKill // kill without stacking
177 
178  //G4ClassificationOfNewTrack choice = fUrgent; // safe choice
179  // prioritize: anyone shoots it, it's dead;
180  // then postpone; then waiting; finally just process it
181  const G4ClassificationOfNewTrack priority[] =
182  { fKill, fPostpone, fWaiting, fUrgent };
183  const size_t nprio = sizeof(priority)/sizeof(G4ClassificationOfNewTrack);
184  for (unsigned int j=0; j<nprio; ++j) {
185  G4ClassificationOfNewTrack saction = priority[j];
186  if ( stackChoices[saction] > 0 ) return saction;
187  }
188  // shouldn't get here (already covered) ... but a fall back
189  return fUrgent;
190  }
191 
192  //-------------------------------------------------
194  {
195  for ( fuserActions_ptr_t i = fuserActions.begin(); i != fuserActions.end(); i++ ){
196  if ( (*i)->ProvidesStacking() ) {
197  (*i)->StackNewStage();
198  }
199  }
200  }
201 
202  //-------------------------------------------------
204  {
205  for ( fuserActions_ptr_t i = fuserActions.begin(); i != fuserActions.end(); i++ ){
206  if ( (*i)->ProvidesStacking() ) {
207  (*i)->StackPrepareNewEvent();
208  }
209  }
210  }
211 
212  //-------------------------------------------------
214  {
215  // do any managed UserActions do stacking?
216  bool doany = false;
217  for ( fuserActions_ptr_t i = fuserActions.begin(); i != fuserActions.end(); i++ ){
218  doany |= (*i)->ProvidesStacking(); // any == take the "or"
219  }
220  return doany;
221  }
222 
223 }// namespace
intermediate_table::iterator iterator
UserAction * GetAction(G4int i) const
virtual bool DoesAnyActionProvideStacking()
const std::string instance
virtual void UserSteppingAction(const G4Step *)
virtual bool ProvidesStacking()
Definition: UserAction.h:90
virtual void BeginOfEventAction(const G4Event *)
virtual void EndOfEventAction(const G4Event *)
G4int GetIndex(std::string const &name) const
see below
std::string const & GetName() const
Definition: UserAction.h:98
virtual void BeginOfRunAction(const G4Run *)
fuserActions_t::const_iterator fuserActions_ptr_t
basic interface to Geant4 for ART-based software
std::vector< UserAction * > fuserActions_t
void PrintActionList(std::string const &opt) const
static UserActionManager * Instance()
virtual void EndOfRunAction(const G4Run *)
static fuserActions_t fuserActions
virtual G4ClassificationOfNewTrack ClassifyNewTrack(const G4Track *)
virtual void PostUserTrackingAction(const G4Track *)
virtual void PreUserTrackingAction(const G4Track *)
virtual void PrintConfig(std::string const &)
Override PrintConfig() to print out current configuration.
Definition: UserAction.h:68