LDMX Software
MidShowerDiMuonBkgdFilter.cxx
1#include "Biasing/MidShowerDiMuonBkgdFilter.h"
2
3#include "G4EventManager.hh"
4#include "G4Gamma.hh"
5#include "G4RunManager.hh"
6#include "G4Step.hh"
7#include "SimCore/G4User/PtrRetrieval.h"
8#include "SimCore/UserTrackInformation.h"
9
10namespace biasing {
11
13 const std::string& name, framework::config::Parameters& parameters)
14 : simcore::UserAction(name, parameters) {
15 threshold_ = parameters.getParameter<double>("threshold");
16 /* debug printout
17 std::cout
18 << "[ MidShowerDiMuonBkgdFilter ]: "
19 << "created instance " << name
20 << "with threshold " << threshold_ << " MeV"
21 << std::endl;
22 */
23}
24
26 /* debug printout
27 std::cout
28 << "[ MidShowerDiMuonBkgdFilter ]: "
29 << "("
30 << G4EventManager::GetEventManager()->GetConstCurrentEvent()->GetEventID()
31 << ") "
32 << "starting new simulation event." << std::endl;
33 */
35}
36
37void MidShowerDiMuonBkgdFilter::stepping(const G4Step* step) {
38 // skips steps that aren't done by photons (required for mu conv)
39 if (step->GetTrack()->GetParticleDefinition() != G4Gamma::Gamma()) return;
40 // skip steps that are outside the calorimeter region
41 if (isOutsideCalorimeterRegion(step)) return;
42 // check photon secondaries for muons
43 const G4TrackVector* secondaries{step->GetSecondary()};
44 if (secondaries == nullptr or secondaries->size() == 0) return;
45 // have left if secondaries is empty
46 bool found_muon{false};
47 for (const G4Track* secondary : *secondaries) {
48 if (abs(secondary->GetParticleDefinition()->GetPDGEncoding()) == 13) {
49 found_muon = true;
50 save(secondary);
51 }
52 }
53 if (not found_muon) return;
54 // there are interesting secondaries in this step
55 double pre_energy = step->GetPreStepPoint()->GetTotalEnergy();
56 double post_energy = step->GetPostStepPoint()->GetTotalEnergy();
57 total_process_energy_ += pre_energy - post_energy;
58 const G4Track* track = step->GetTrack();
59 /* debug printout
60 std::cout << "[ MidShowerDiMuonBkgdFilter ]: "
61 << "("
62 << G4EventManager::GetEventManager()
63 ->GetConstCurrentEvent()
64 ->GetEventID()
65 << ") "
66 << "Track " << track->GetParentID() << " created "
67 << track->GetTrackID() << " which went from " << pre_energy
68 << " MeV to " << post_energy << " MeV generating muons."
69 << std::endl;
70 */
71 // make sure this track is saved
72 save(track);
73}
74
76 /* debug printout
77 std::cout
78 << "[ MidShowerDiMuonBkgdFilter ]: "
79 << "("
80 << G4EventManager::GetEventManager()->GetConstCurrentEvent()->GetEventID()
81 << ") " << total_process_energy_ << " MeV was muonic." << std::endl;
82 */
84 AbortEvent("Not enough energy went to the input process.");
85 return;
86}
87
89 const G4Step* step) const {
90 static auto calorimeter_region =
91 simcore::g4user::ptrretrieval::getRegion("CalorimeterRegion");
92 if (!calorimeter_region) {
93 ldmx_log(warn)
94 << "Region 'CalorimeterRegion' not found in Geant4 region store";
95 }
96 // the pointers in this chain are assumed to be always valid
97 auto phys_vol{step->GetTrack()->GetVolume()};
98 auto log_vol{phys_vol ? phys_vol->GetLogicalVolume() : nullptr};
99 auto reg{log_vol ? log_vol->GetRegion() : nullptr};
100 return (reg != calorimeter_region);
101}
102
103void MidShowerDiMuonBkgdFilter::save(const G4Track* track) const {
104 auto track_info{simcore::UserTrackInformation::get(track)};
105 track_info->setSaveFlag(true);
106 return;
107}
108
109void MidShowerDiMuonBkgdFilter::AbortEvent(const std::string& reason) const {
110 if (G4RunManager::GetRunManager()->GetVerboseLevel() > 1) {
111 std::cout << "[ MidShowerDiMuonBkgdFilter ]: "
112 << "("
113 << G4EventManager::GetEventManager()
114 ->GetConstCurrentEvent()
115 ->GetEventID()
116 << ") " << reason << " Aborting event." << std::endl;
117 }
118 G4RunManager::GetRunManager()->AbortEvent();
119 return;
120}
121} // namespace biasing
122
123DECLARE_ACTION(biasing, MidShowerDiMuonBkgdFilter)
bool isOutsideCalorimeterRegion(const G4Step *step) const
Checks if the passed step is outside of the CalorimeterRegion.
double total_process_energy_
Total energy gone to the process in the current event.
MidShowerDiMuonBkgdFilter(const std::string &name, framework::config::Parameters &parameters)
Class constructor.
void AbortEvent(const std::string &reason) const
Helper to abort an event with a message.
void save(const G4Track *track) const
Helper to save the passed track.
double threshold_
Minimum energy [MeV] that the process products need to have to keep the event.
void NewStage() override
When using the PartialEnergySorter, the first time that a new stage begins is when all particles are ...
void BeginOfEventAction(const G4Event *event) override
Reset the total energy going to the muons.
void stepping(const G4Step *step) override
We follow the simulation along each step and check if any secondaries of the input process were creat...
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:29
static UserTrackInformation * get(const G4Track *track)
get