ATLAS Offline Software
Loading...
Searching...
No Matches
TruthPreselectionTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
5// ISF_Algs includes
8
10// Public methods:
12
13// Constructors
15ISF::TruthPreselectionTool::TruthPreselectionTool( const std::string& t, const std::string& n, const IInterface* p ) :
16 base_class( t, n, p )
17{
18}
19
20// Athena Algorithm's Hooks
23{
24 ATH_MSG_VERBOSE ( "Initializing TruthPreselectionTool Algorithm" );
25
26 if (!m_genParticleFilters.empty()) ATH_CHECK(m_genParticleFilters.retrieve());
28
29 // intialziation successful
30 return StatusCode::SUCCESS;
31}
32
34bool ISF::TruthPreselectionTool::passesFilters(HepMC::ConstGenParticlePtr& part, const ToolHandleArray<IGenParticleFilter>& filters) const
35{
36 // TODO: implement this as a std::find_if with a lambda function
37 for ( const auto& filter : filters ) {
38 // determine if the particle passes current filter
39 bool passFilter = filter->pass(part);
40 ATH_MSG_VERBOSE("Filter '" << filter.typeAndName() << "' returned: "
41 << (passFilter ? "true, will keep particle."
42 : "false, will remove particle."));
43
44 if (!passFilter) return false;
45 }
46
47 return true;
48}
49
51{
52 bool b_sim = false;
53 if (m_quasiStableFilter->pass(part)) {
55 }
56 return b_sim;
57}
58
60{
61 // TODO: investigate making this more efficient
62 // Recursively loop over ancestral particles looking for a quasi-stable particle
63 if (!part->production_vertex() || part->production_vertex()->particles_in().empty()) { return false; }
64 for ( auto ancestor: part->production_vertex()->particles_in() ) {
65 // Check ancestor particle for Attribute
66 if ( ancestor->attribute<HepMC3::IntAttribute>(HepMCStr::ShadowParticleId) ) { return true; }
67 if (hasQuasiStableAncestorParticle(ancestor)) { return true; }
68 }
69 return false;
70}
71
73{
74 // All outgoing particles have already been removed.
75 if ( vtx->particles_out().empty() ) { return true; }
76 return false;
77}
78
79std::unique_ptr<HepMC::GenEvent> ISF::TruthPreselectionTool::filterGenEvent(const HepMC::GenEvent& inputEvent) const {
82 std::unique_ptr<HepMC::GenEvent> outputEvent = std::make_unique<HepMC::GenEvent>(inputEvent);
83 if (inputEvent.run_info()) {
84 outputEvent->set_run_info(std::make_shared<HepMC3::GenRunInfo>(*(inputEvent.run_info().get())));
85 }
86 if (inputEvent.heavy_ion()) {
87 outputEvent->set_heavy_ion(std::make_shared<HepMC::GenHeavyIon>(*(inputEvent.heavy_ion())));
88 }
89 HepMC::fillBarcodesAttribute(outputEvent.get());
90
91 // First loop: flag the particles which should be passed to simulation
92 for (auto& particle: outputEvent->particles()) {
93 HepMC::ConstGenParticlePtr cparticle = particle;
94 if (passesFilters(cparticle, m_genParticleFilters)) {
95 // Particle to be simulated
96 const int shadowId = particle->id();
97 // Version 1 Use the Id
98 particle->add_attribute(HepMCStr::ShadowParticleId,
99 std::make_shared<HepMC3::IntAttribute>(shadowId));
100 // Version 2 Directly save the ConstGenParticlePtr - needs to link to a version of the GenEvent after zero-lifetime positioner as been applied.
101 // HepMC::ConstGenParticlePtr& shadow = inputEvent.particles().at(shadowId);
102 // particle->add_attribute(HepMCStr::ShadowParticle,
103 // std::make_shared<HepMC::ShadowParticle>(particle));
104 }
105 }
106 // Second loop(s): flag particles (and vertices) to be removed (i.e. those
107 // with ancestor particle flagged to be passed to simulation).
108 for (;;) {
109 std::vector<HepMC::GenParticlePtr> p_to_remove;
110 std::vector<HepMC::GenVertexPtr> v_to_remove;
111 for (auto& particle: outputEvent->particles()) {
112 HepMC::ConstGenParticlePtr cparticle = particle;
113 if (hasQuasiStableAncestorParticle(cparticle)) {
114 p_to_remove.push_back(particle);
115 }
116 }
117 for (auto& particle: p_to_remove) outputEvent->remove_particle(particle);
118 for (auto& vertex: outputEvent->vertices()) {
119 HepMC::ConstGenVertexPtr cvertex = vertex;
120 if (isPostQuasiStableParticleVertex(cvertex)) {
121 v_to_remove.push_back(vertex);
122 }
123 }
124 for (auto& vertex: v_to_remove) outputEvent->remove_vertex(vertex);
125 if (p_to_remove.empty() && v_to_remove.empty()) break;
126 }
127
128 return outputEvent;
129}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
TruthPreselectionTool(const std::string &t, const std::string &n, const IInterface *p)
Constructor with parameters.
ToolHandleArray< IGenParticleFilter > m_genParticleFilters
Filter passes if a difference between the decision of m_genParticleOldFilters and m_genParticleNewFil...
ToolHandle< IGenParticleFilter > m_quasiStableFilter
virtual std::unique_ptr< HepMC::GenEvent > filterGenEvent(const HepMC::GenEvent &inputEvent) const override final
IGenEventFilter interface.
bool hasQuasiStableAncestorParticle(HepMC::ConstGenParticlePtr &part) const
virtual StatusCode initialize() override final
Athena algorithm's interface method initialize().
bool passesFilters(HepMC::ConstGenParticlePtr &part, const ToolHandleArray< IGenParticleFilter > &filters) const
check if the given particle passes all filters
bool identifiedQuasiStableParticleForSim(HepMC::ConstGenParticlePtr &part) const
bool isPostQuasiStableParticleVertex(HepMC::ConstGenVertexPtr &vtx) const
const std::string ShadowParticleId
void fillBarcodesAttribute(GenEvent *e)
Definition GenEvent.h:393
HepMC3::ConstGenParticlePtr ConstGenParticlePtr
Definition GenParticle.h:20
HepMC3::ConstGenVertexPtr ConstGenVertexPtr
Definition GenVertex.h:24
HepMC3::GenEvent GenEvent
Definition GenEvent.h:39