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/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 /* debug printout
16 std::cout
17 << "[ MidShowerDiMuonBkgdFilter ]: "
18 << "created instance " << name
19 << "with threshold " << threshold_ << " MeV"
20 << std::endl;
21 */
22}
23
25 /* debug printout
26 std::cout
27 << "[ MidShowerDiMuonBkgdFilter ]: "
28 << "("
29 << G4EventManager::GetEventManager()->GetConstCurrentEvent()->GetEventID()
30 << ") "
31 << "starting new simulation event." << std::endl;
32 */
34}
35
36void MidShowerDiMuonBkgdFilter::stepping(const G4Step* step) {
37 // skips steps that aren't done by photons (required for mu conv)
38 if (step->GetTrack()->GetParticleDefinition() != G4Gamma::Gamma()) return;
39 // skip steps that are outside the calorimeter region
40 if (isOutsideCalorimeterRegion(step)) return;
41 // check photon secondaries for muons
42 const G4TrackVector* secondaries{step->GetSecondary()};
43 if (secondaries == nullptr or secondaries->size() == 0) return;
44 // have left if secondaries is empty
45 bool found_muon{false};
46 for (const G4Track* secondary : *secondaries) {
47 if (abs(secondary->GetParticleDefinition()->GetPDGEncoding()) == 13) {
48 found_muon = true;
49 save(secondary);
50 }
51 }
52 if (not found_muon) return;
53 // there are interesting secondaries in this step
54 double pre_energy = step->GetPreStepPoint()->GetTotalEnergy();
55 double post_energy = step->GetPostStepPoint()->GetTotalEnergy();
56 total_process_energy_ += pre_energy - post_energy;
57 const G4Track* track = step->GetTrack();
58 /* debug printout
59 std::cout << "[ MidShowerDiMuonBkgdFilter ]: "
60 << "("
61 << G4EventManager::GetEventManager()
62 ->GetConstCurrentEvent()
63 ->GetEventID()
64 << ") "
65 << "Track " << track->GetParentID() << " created "
66 << track->GetTrackID() << " which went from " << pre_energy
67 << " MeV to " << post_energy << " MeV generating muons."
68 << std::endl;
69 */
70 // make sure this track is saved
71 save(track);
72}
73
75 /* debug printout
76 std::cout
77 << "[ MidShowerDiMuonBkgdFilter ]: "
78 << "("
79 << G4EventManager::GetEventManager()->GetConstCurrentEvent()->GetEventID()
80 << ") " << total_process_energy_ << " MeV was muonic." << std::endl;
81 */
83 AbortEvent("Not enough energy went to the input process.");
84 return;
85}
86
88 const G4Step* step) const {
89 // the pointers in this chain are assumed to be always valid
90 auto reg{step->GetTrack()->GetVolume()->GetLogicalVolume()->GetRegion()};
91 if (reg) return (reg->GetName() != "CalorimeterRegion");
92 // region is nullptr ==> no region defined for current volume
93 // ==> outside CalorimeterRegion
94 return true;
95}
96
97void MidShowerDiMuonBkgdFilter::save(const G4Track* track) const {
98 auto track_info{simcore::UserTrackInformation::get(track)};
99 track_info->setSaveFlag(true);
100 return;
101}
102
103void MidShowerDiMuonBkgdFilter::AbortEvent(const std::string& reason) const {
104 if (G4RunManager::GetRunManager()->GetVerboseLevel() > 1) {
105 std::cout << "[ MidShowerDiMuonBkgdFilter ]: "
106 << "("
107 << G4EventManager::GetEventManager()
108 ->GetConstCurrentEvent()
109 ->GetEventID()
110 << ") " << reason << " Aborting event." << std::endl;
111 }
112 G4RunManager::GetRunManager()->AbortEvent();
113 return;
114}
115} // namespace biasing
116
117DECLARE_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:27
T getParameter(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:89
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.