ExampleAction.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file ExampleAction.cxx
3 /// \brief Example UserAction w/ Geant4's user "hooks"
4 ///
5 /// \version $Id: ExampleAction.cxx,v 1.2 2012-09-20 21:47:05 greenc Exp $
6 /// \author rhatcher@fnal.gov
7 ////////////////////////////////////////////////////////////////////////
8 
9 #include "nutools/G4Base/ExampleAction.h"
11 
12 // self-register with the factory
13 #include "nutools/G4Base/UserActionFactory.h"
15 
16 // G4 includes
17 #include "Geant4/G4Event.hh"
18 #include "Geant4/G4Track.hh"
19 #include "Geant4/G4ThreeVector.hh"
20 #include "Geant4/G4ParticleDefinition.hh"
21 #include "Geant4/G4PrimaryParticle.hh"
22 #include "Geant4/G4DynamicParticle.hh"
23 #include "Geant4/G4VUserPrimaryParticleInformation.hh"
24 #include "Geant4/G4Step.hh"
25 #include "Geant4/G4StepPoint.hh"
26 #include "Geant4/G4VProcess.hh"
27 #include "Geant4/G4VPhysicalVolume.hh"
28 #include "Geant4/G4VTouchable.hh"
29 
30 // ROOT includes
31 #include <TLorentzVector.h>
32 #include <TString.h>
33 
34 // C/C++ includes
35 #include <algorithm>
36 #include <string>
37 
38 namespace altns {
39 
40  // Initialize static members.
41 
42  //-------------------------------------------------------------
43  // Constructor.
45  : fSomeValue(0)
46  , fVerbose(0)
47  , fStepMsgMaxPerEvt(42)
48  , fTrack2ndMsgMaxPerEvt(2)
49  {
50  /// Create the object
51  }
52 
53  //-------------------------------------------------------------
54  // Destructor.
56  {
57  /// Delete anything that we created with "new'.
58  }
59 
60  //-------------------------------------------------------------
62  {
63  /// Configure the object
64 
65  fSomeValue = pset.get< double >("SomeValue",0)*CLHEP::GeV;
66  fVerbose = pset.get< int >("Verbose",0);
67  fStepMsgMaxPerEvt = pset.get< int >("StepMsgMaxPerEvt",42);
68  fTrack2ndMsgMaxPerEvt = pset.get< int >("Track2ndMsgMaxPerEvt",2);
69 
70  }
71 
72  //-------------------------------------------------------------
73  void ExampleAction::PrintConfig(std::string const& /* opt */)
74  {
75  mf::LogInfo("ExampleAction")
76  << "ExampleAction::PrintConfig \n"
77  << " SomeValue " << fSomeValue << "\n"
78  << " Verbose " << fVerbose << "\n"
79  << " StepMsgMaxPerEvt " << fStepMsgMaxPerEvt << "\n"
80  << " Track2ndMsgMaxPerEvt " << fTrack2ndMsgMaxPerEvt << "\n";
81 
82  }
83 
84  //-------------------------------------------------------------
86  {
87  /// This method is invoked before converting the primary particles
88  /// to G4Track objects. A typical use of this method would be to
89  /// initialize and/or book histograms for a particular event.
90 
91  mf::LogInfo("ExampleAction")
92  << "ExampleAction::BeginOfEventAction EventID="
93  << event->GetEventID();
94 
95  fStepMsg = 0;
96  fTrack2ndMsg = 0;
97 
98  }
99 
100  //-------------------------------------------------------------
102  {
103  /// This method is invoked at the very end of event processing.
104  /// It is typically used for a simple analysis of the processed event.
105 
106  mf::LogInfo("ExampleAction")
107  << "ExampleAction::EndOfEventAction EventID="
108  << event->GetEventID();
109  }
110 
111  //-------------------------------------------------------------
112  void ExampleAction::PreTrackingAction(const G4Track* track)
113  {
114  /// This method is invoked before any stepping of this track
115  /// has occurred
116 
117  G4int parent_id = track->GetParentID();
118  if ( parent_id > 0 && fTrack2ndMsg > fTrack2ndMsgMaxPerEvt ) return;
119 
120  mf::LogInfo("ExampleAction")
121  << "ExampleAction::PreTrackingAction TrackID="
122  << track->GetTrackID()
123  << " is a " << track->GetParticleDefinition()->GetParticleName();
124  }
125 
126  //-------------------------------------------------------------
127  void ExampleAction::PostTrackingAction( const G4Track* track)
128  {
129  /// This method is invoked after all stepping of this track
130  /// has occurred
131 
132  G4int parent_id = track->GetParentID();
133  std::string extra_msg = "";
134  if ( parent_id > 0 ) {
135  ++fTrack2ndMsg;
136  if ( fTrack2ndMsg > fTrack2ndMsgMaxPerEvt ) return;
138  extra_msg = "...last such message this event";
139  }
140  }
141 
142  mf::LogInfo("ExampleAction")
143  << "ExampleAction::PostTrackingAction TrackID="
144  << track->GetTrackID()
145  << " " << extra_msg;
146  }
147 
148  //-------------------------------------------------------------
150  {
151  /// This method is invoked at each end of stepping
152 
153  ++fStepMsg;
154  if ( ++fStepMsg > fStepMsgMaxPerEvt ) return;
155 
156  std::string extra_msg = "";
157  if ( fStepMsg == fStepMsgMaxPerEvt ) {
158  extra_msg = "...last such message this event";
159  }
160 
161  mf::LogInfo("ExampleAction")
162  << "ExampleAction::SteppingAction TrackID="
163  << step->GetTrack()->GetTrackID()
164  << " " << extra_msg;
165 
166 
167  }
168 
169  //-------------------------------------------------------------
170  G4ClassificationOfNewTrack
172  {
173  /// This method is invoked by G4StackManager whenever a new G4Track
174  /// object is "pushed" onto a stack by G4EventManager. ClassifyNewTrack()
175  /// returns an enumerator, G4ClassificationOfNewTrack, whose value
176  /// indicates to which stack, if any, the track will be sent.
177  /// G4ClassificationOfNewTrack has four possible values:
178  /// fUrgent - track is placed in the urgent stack
179  /// fWaiting - track is placed in the waiting stack,
180  /// and will not be simulated until the urgent stack is empty
181  /// fPostpone - track is postponed to the next event
182  /// fKill - the track is deleted immediately and not stored in any stack.
183 
184  G4int parent_id = track->GetParentID();
185  std::string tsrc = "primary";
186  if ( parent_id < 0 ) tsrc = "postponed (from previous event)";
187  if ( parent_id > 0 ) tsrc = "secondary";
188 
189  mf::LogInfo("ExampleAction")
190  << "ExampleAction::StackClassifyNewTrack TrackID="
191  << track->GetTrackID()
192  << " ParentID=" << parent_id << " "
193  << track->GetDefinition()->GetParticleName()
194  << " (" << tsrc << " particle)";
195 
196  // One *must* return a classification
197  // Since we're not doing anything useful in NewStage/PrepareNewEvent
198  // the only things we should return are fUrgent or fKill
199  return fUrgent;
200  }
201 
202  //-------------------------------------------------------------
204  {
205  /// This method is invoked when the urgent stack is empty and the
206  /// waiting stack contains at least one G4Track object. Here the user
207  /// may kill or re-assign to different stacks all the tracks in the
208  /// waiting stack by calling the stackManager->ReClassify() method
209  /// which, in turn, calls the ClassifyNewTrack() method. If no user
210  /// action is taken, all tracks in the waiting stack are transferred
211  /// to the urgent stack. The user may also decide to abort the current
212  /// event even though some tracks may remain in the waiting stack by
213  /// calling stackManager->clear(). This method is valid and safe only
214  /// if it is called from the G4UserStackingAction class.
215 
216  mf::LogInfo("ExampleAction")
217  << "ExampleAction::StackNewStage";
218  }
219 
220  //-------------------------------------------------------------
222  {
223  /// This method is invoked at the beginning of each event. At this
224  /// point no primary particles have been converted to tracks, so the
225  /// urgent and waiting stacks are empty. However, there may be tracks
226  /// in the postponed-to-next-event stack; for each of these the
227  /// ClassifyNewTrack() method is called and the track is assigned
228  /// to the appropriate stack.
229 
230  mf::LogInfo("ExampleAction")
231  << "ExampleAction::StackPrepareNewEvent";
232  }
233 
234 } // end namespace
void SteppingAction(const G4Step *)
G4UserSteppingAction interface.
void PostTrackingAction(const G4Track *)
G4ClassificationOfNewTrack StackClassifyNewTrack(const G4Track *)
G4UserStackingAction interfaces.
std::string string
Definition: nybbler.cc:12
int fVerbose
verbosity
Definition: ExampleAction.h:54
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
int fStepMsgMaxPerEvt
shut up about steps
Definition: ExampleAction.h:55
void Config(fhicl::ParameterSet const &pset)
Override Config() to extract any necessary parameters.
int fTrack2ndMsgMaxPerEvt
shut up about 2ndary tracks
Definition: ExampleAction.h:56
#define USERACTIONREG3(_ns, _name, _fqname)
void PrintConfig(std::string const &opt)
Override PrintConfig() to print out current configuration.
void BeginOfEventAction(const G4Event *)
T get(std::string const &key) const
Definition: ParameterSet.h:231
int fTrack2ndMsg
of 2ndary track printed this evt?
Definition: ExampleAction.h:59
double fSomeValue
some user config value
Definition: ExampleAction.h:53
int fStepMsg
steps have we printed this evt?
Definition: ExampleAction.h:58
static const double GeV
Definition: Units.h:29
void PreTrackingAction(const G4Track *)
G4UserTrackingAction interfaces.
Event finding and building.
void EndOfEventAction(const G4Event *)