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/G4User/PtrRetrieval.h"
7#include "SimCore/G4User/UserTrackInformation.h"
8
9namespace biasing {
10
12 const std::string& name, framework::config::Parameters& parameters)
13 : simcore::UserAction(name, parameters) {
14 threshold_ = parameters.getParameter<double>("threshold");
15 nuclear_processes_ = {"photonNuclear", "electronNuclear"};
16}
17
19 /* debug printout
20 std::cout
21 << "[ MidShowerNuclearBkgdFilter ]: "
22 << "("
23 << G4EventManager::GetEventManager()->GetConstCurrentEvent()->GetEventID()
24 << ") "
25 << "starting new simulation event." << std::endl;
26 */
28}
29
30void MidShowerNuclearBkgdFilter::stepping(const G4Step* step) {
31 // skip steps that are outside the calorimeter region
32 if (isOutsideCalorimeterRegion(step)) return;
33
34 if (getEventInfo()->wasLastStepPN() or getEventInfo()->wasLastStepEN()) {
35 // there are interesting secondaries in this step
36 double pre_energy = step->GetPreStepPoint()->GetTotalEnergy();
37 double post_energy = step->GetPostStepPoint()->GetTotalEnergy();
38 total_process_energy_ += pre_energy - post_energy;
39
40 const G4Track* track = step->GetTrack();
41 /* debug printout
42 std::cout << "[ MidShowerNuclearBkgdFilter ]: "
43 << "("
44 << G4EventManager::GetEventManager()
45 ->GetConstCurrentEvent()
46 ->GetEventID()
47 << ") "
48 << "Track " << track->GetParentID() << " created "
49 << track->GetTrackID() << " which went from " << pre_energy
50 << " MeV to " << post_energy << " via a nuclear process." <<
51 std::endl;
52 */
53 // make sure this track is saved
54 save(track);
55 } else if (const G4Track * track{step->GetTrack()};
56 track->GetCurrentStepNumber() == 1 and
57 isNuclearProcess(track->GetCreatorProcess())) {
58 save(track);
59 }
60}
61
63 /* debug printout
64 std::cout
65 << "[ MidShowerNuclearBkgdFilter ]: "
66 << "("
67 << G4EventManager::GetEventManager()->GetConstCurrentEvent()->GetEventID()
68 << ") " << total_process_energy_ << " MeV went nuclear." << std::endl;
69 */
71 AbortEvent("Not enough energy went to the input process.");
72 return;
73}
74
76 const G4Step* step) const {
77 static auto calorimeter_region =
78 simcore::g4user::ptrretrieval::getRegion("CalorimeterRegion");
79 if (!calorimeter_region) {
80 ldmx_log(warn)
81 << "Region 'CalorimeterRegion' not found in Geant4 region store";
82 }
83 // the pointers in this chain are assumed to be always valid
84 auto phys_vol{step->GetTrack()->GetVolume()};
85 auto log_vol{phys_vol ? phys_vol->GetLogicalVolume() : nullptr};
86 auto reg{log_vol ? log_vol->GetRegion() : nullptr};
87 return (reg != calorimeter_region);
88}
89
91 const G4VProcess* proc) const {
92 if (proc) {
93 const G4String& proc_name{proc->GetProcessName()};
94 for (auto const& option : nuclear_processes_) {
95 if (proc_name.contains(option)) return true;
96 } // loop over nuclear processes
97 } // pointer exists
98 return false;
99}
100
101void MidShowerNuclearBkgdFilter::save(const G4Track* track) const {
102 auto track_info{simcore::UserTrackInformation::get(track)};
103 track_info->setSaveFlag(true);
104 return;
105}
106
107void MidShowerNuclearBkgdFilter::AbortEvent(const std::string& reason) const {
108 if (G4RunManager::GetRunManager()->GetVerboseLevel() > 1) {
109 std::cout << "[ MidShowerNuclearBkgdFilter ]: " << "("
110 << G4EventManager::GetEventManager()
111 ->GetConstCurrentEvent()
112 ->GetEventID()
113 << ") " << reason << " Aborting event." << std::endl;
114 }
115 G4RunManager::GetRunManager()->AbortEvent();
116 return;
117}
118} // namespace biasing
119
#define DECLARE_ACTION(CLASS)
register a new UserAction with its factory
Definition UserAction.h:206
The basic premis of this filter is to add up all of the energy "lost" to the configured process.
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:29
UserEventInformation * getEventInfo() const
Get a handle to the event information.
static UserTrackInformation * get(const G4Track *track)
get
Dynamically loadable photonuclear models either from SimCore or external libraries implementing this ...