LDMX Software
StepPrinter.cxx
1
2#include "Biasing/Utility/StepPrinter.h"
3
4/*~~~~~~~~~~~~*/
5/* Geant4 */
6/*~~~~~~~~~~~~*/
7#include "G4Step.hh"
8
9namespace biasing {
10namespace utility {
11
12StepPrinter::StepPrinter(const std::string& name,
14 : simcore::UserAction(name, parameters) {
15 trackID_ = parameters.getParameter<int>("track_id");
16 processName_ = parameters.getParameter<std::string>("process_name");
17 depth_ = parameters.getParameter<int>("depth");
18}
19
20void StepPrinter::stepping(const G4Step* step) {
21 // Get the track associated with this step
22 auto track{step->GetTrack()};
23
24 const auto trackID{track->GetTrackID()};
25 const auto parent{track->GetParentID()};
26 // Don't bother filling the map if we aren't going to use it
27 if (depth_ > 0) {
28 trackParents_[trackID] = parent;
29 }
30
31 auto process{track->GetCreatorProcess()};
32 std::string processName{process ? process->GetProcessName() : "Primary"};
33 // Unwrap biasing part of process name if present
34 if (processName.find("biasWrapper") != std::string::npos) {
35 std::size_t pos = processName.find_first_of("(") + 1;
36 processName = processName.substr(pos, processName.size() - pos - 1);
37 }
38
39 // This could be a negated condition, but it is easier to read this way
40 //
42 if (trackID == trackID_ || // We are the track of interest
43 trackMap.isDescendant(
44 trackID, trackID_,
45 depth_) || // We are a descendent of the track of interest
46 processName ==
47 processName_ // The parent process was the process of interest
48 ) {
49 // This is an interesting track -> Carry on processing
50 } else {
51 return;
52 }
53 // Get the particle name.
54 const auto particleName{track->GetParticleDefinition()->GetParticleName()};
55
56 // Get the energy of the particle
57 const auto energy{step->GetPostStepPoint()->GetTotalEnergy()};
58
59 // Get the volume the particle is in.
60 auto volume{track->GetVolume()};
61 auto volumeName{volume->GetName()};
62
63 // Get the next volume (can fail if current volume is WorldPV and next is
64 // outside the world)
65 auto nextVolume{track->GetNextVolume() ? track->GetNextVolume()->GetName()
66 : "undefined"};
67
68 // Get the region
69 auto regionName{volume->GetLogicalVolume()->GetRegion()->GetName()};
70
71 std::cout << " Step " << track->GetCurrentStepNumber() << " ("
72 << track->GetParticleDefinition()->GetParticleName() << ") {"
73 << " Energy: " << energy << " Track ID: " << track->GetTrackID()
74 << " Particle currently in: " << volumeName
75 << " Region: " << regionName << " Next volume: " << nextVolume
76 << " Weight: " << track->GetWeight() << " Parent: " << parent
77 << " (" << processName << ") "
78 << " Children:";
79 for (auto const& child : *(step->GetSecondaryInCurrentStep())) {
80 std::cout << " (" << child->GetTotalEnergy()
81 << "): " << child->GetParticleDefinition()->GetPDGEncoding();
82 }
83
84 std::cout << " }" << std::endl;
85}
86
87} // namespace utility
88} // namespace biasing
89
90DECLARE_ACTION(biasing::utility, StepPrinter)
StepPrinter(const std::string &name, framework::config::Parameters &parameters)
Constructor.
int trackID_
The track ID to filter on.
Definition StepPrinter.h:46
void stepping(const G4Step *step) override
Stepping action called when a step is taken during tracking of a particle.
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:27
T getParameter(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:89
TrackMap & getTrackMap()
Get a handle to the current TrackMap for the event.
static TrackingAction * get()
Get a pointer to the current UserTrackingAction from the G4RunManager.