LDMX Software
MidShowerNuclearBkgdFilter.cxx
1#include "Biasing/MidShowerNuclearBkgdFilter.h"
2
3#include "G4EventManager.hh"
4#include "G4RunManager.hh"
5#include "G4Step.hh"
6#include "SimCore/UserTrackInformation.h"
7
8namespace biasing {
9
11 const std::string& name, framework::config::Parameters& parameters)
12 : simcore::UserAction(name, parameters) {
13 threshold_ = parameters.getParameter<double>("threshold");
14 nuclear_processes_ = {"photonNuclear", "electronNuclear"};
15}
16
18 /* debug printout
19 std::cout
20 << "[ MidShowerNuclearBkgdFilter ]: "
21 << "("
22 << G4EventManager::GetEventManager()->GetConstCurrentEvent()->GetEventID()
23 << ") "
24 << "starting new simulation event." << std::endl;
25 */
27}
28
29void MidShowerNuclearBkgdFilter::stepping(const G4Step* step) {
30 // skip steps that are outside the calorimeter region
31 if (isOutsideCalorimeterRegion(step)) return;
32
33 if (getEventInfo()->wasLastStepPN() or getEventInfo()->wasLastStepEN()) {
34 // there are interesting secondaries in this step
35 double pre_energy = step->GetPreStepPoint()->GetTotalEnergy();
36 double post_energy = step->GetPostStepPoint()->GetTotalEnergy();
37 total_process_energy_ += pre_energy - post_energy;
38
39 const G4Track* track = step->GetTrack();
40 /* debug printout
41 std::cout << "[ MidShowerNuclearBkgdFilter ]: "
42 << "("
43 << G4EventManager::GetEventManager()
44 ->GetConstCurrentEvent()
45 ->GetEventID()
46 << ") "
47 << "Track " << track->GetParentID() << " created "
48 << track->GetTrackID() << " which went from " << pre_energy
49 << " MeV to " << post_energy << " via a nuclear process." <<
50 std::endl;
51 */
52 // make sure this track is saved
53 save(track);
54 } else if (const G4Track * track{step->GetTrack()};
55 track->GetCurrentStepNumber() == 1 and
56 isNuclearProcess(track->GetCreatorProcess())) {
57 save(track);
58 }
59}
60
62 /* debug printout
63 std::cout
64 << "[ MidShowerNuclearBkgdFilter ]: "
65 << "("
66 << G4EventManager::GetEventManager()->GetConstCurrentEvent()->GetEventID()
67 << ") " << total_process_energy_ << " MeV went nuclear." << std::endl;
68 */
70 AbortEvent("Not enough energy went to the input process.");
71 return;
72}
73
75 const G4Step* step) const {
76 // the pointers in this chain are assumed to be always valid
77 auto reg{step->GetTrack()->GetVolume()->GetLogicalVolume()->GetRegion()};
78 if (reg) return (reg->GetName() != "CalorimeterRegion");
79 // region is nullptr ==> no region defined for current volume
80 // ==> outside CalorimeterRegion
81 return true;
82}
83
85 const G4VProcess* proc) const {
86 if (proc) {
87 const G4String& proc_name{proc->GetProcessName()};
88 for (auto const& option : nuclear_processes_) {
89 if (proc_name.contains(option)) return true;
90 } // loop over nuclear processes
91 } // pointer exists
92 return false;
93}
94
95void MidShowerNuclearBkgdFilter::save(const G4Track* track) const {
96 auto track_info{simcore::UserTrackInformation::get(track)};
97 track_info->setSaveFlag(true);
98 return;
99}
100
101void MidShowerNuclearBkgdFilter::AbortEvent(const std::string& reason) const {
102 if (G4RunManager::GetRunManager()->GetVerboseLevel() > 1) {
103 std::cout << "[ MidShowerNuclearBkgdFilter ]: "
104 << "("
105 << G4EventManager::GetEventManager()
106 ->GetConstCurrentEvent()
107 ->GetEventID()
108 << ") " << reason << " Aborting event." << std::endl;
109 }
110 G4RunManager::GetRunManager()->AbortEvent();
111 return;
112}
113} // namespace biasing
114
115DECLARE_ACTION(biasing, MidShowerNuclearBkgdFilter)
void save(const G4Track *track) const
Helper to save the passed track.
void AbortEvent(const std::string &reason) const
Helper to abort an event with a message.
void BeginOfEventAction(const G4Event *event) override
Reset the total energy going to the configured process.
bool isOutsideCalorimeterRegion(const G4Step *step) const
Checks if the passed step is outside of the CalorimeterRegion.
void NewStage() override
When using the PartialEnergySorter, the first time that a new stage begins is when all particles are ...
MidShowerNuclearBkgdFilter(const std::string &name, framework::config::Parameters &parameters)
Class constructor.
bool isNuclearProcess(const G4VProcess *proc) const
Checks if the passed process is any of the nuclear interactions.
void stepping(const G4Step *step) override
We follow the simulation along each step and check if any secondaries of the input process were creat...
double threshold_
Minimum energy [MeV] that the process products need to have to keep the event.
std::vector< std::string > nuclear_processes_
Processes to look for.
double total_process_energy_
Total energy gone to the process in the current event.
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
UserEventInformation * getEventInfo() const
Get a handle to the event information.
static UserTrackInformation * get(const G4Track *track)
get
void setSaveFlag(bool saveFlag)
Set the save flag so the associated track will be persisted as a Trajectory.