ATLAS Offline Software
Loading...
Searching...
No Matches
MuonTruthClassifierFallback.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4// Runs on muons without a truth particle link.
5// Finds the nearest stable truth particle and adds its info to the muon.
7
11
12namespace {
13 static const SG::Decorator<float> decorator_dR("MCTFallback_dR");
14 static const SG::Decorator<int> decorator_type("MCTFallback_truthType");
15 static const SG::Decorator<int> decorator_origin("MCTFallback_truthOrigin");
16
17 static const SG::Decorator<float> decorator_pu_dR("MCTFallbackPU_dR");
18 static const SG::Decorator<int> decorator_pu_type("MCTFallbackPU_truthType");
19 static const SG::Decorator<int> decorator_pu_origin("MCTFallbackPU_truthOrigin");
20 // TODO Check if these last two should be ReadDecorHandles instead
21 static const SG::Accessor<int> acc_tT("truthType");
22 static const SG::Accessor<int> acc_tO("truthOrigin");
23
24} // namespace
25
26// Athena initialize
28 ATH_MSG_VERBOSE("initialize() ...");
30
31 ATH_CHECK(m_containerKey.initialize());
32 ATH_CHECK(m_truthSGKey.initialize());
33 ATH_CHECK(m_truthPileupSGKey.initialize());
34 ATH_CHECK(m_truthMuonSGKey.initialize());
35
36 // FIXME These WriteDecorHandles are not being used. The
37 // Decorators above are (incorrectly) used instead.
38 ATH_CHECK(m_Truth_dR_Key.initialize());
39 ATH_CHECK(m_Truth_type_Key.initialize());
40 ATH_CHECK(m_Truth_origin_Key.initialize());
41
42 ATH_CHECK(m_Truth_PU_dR_Key.initialize());
43 ATH_CHECK(m_Truth_PU_type_Key.initialize());
44 ATH_CHECK(m_Truth_PU_origin_Key.initialize());
45
46 return StatusCode::SUCCESS;
47}
48
49StatusCode DerivationFramework::MuonTruthClassifierFallback::addBranches(const EventContext& ctx) const {
50 // Retrieve main particle collection
51
53 if (!parts.isValid()) {
54 ATH_MSG_ERROR("No Muon collection with name " << m_containerKey.fullKey() << " found in StoreGate!");
55 return StatusCode::FAILURE;
56 }
58 if (!tec.isValid()) {
59 ATH_MSG_ERROR("No truth collection with name " << m_truthSGKey.fullKey() << " found in StoreGate!");
60 return StatusCode::FAILURE;
61 }
63 if (!tpec.isValid()) {
64 ATH_MSG_DEBUG("No truth pileup collection with name " << m_truthPileupSGKey.fullKey() << " found in StoreGate. Pile-up information will not be filled");
65 }
66
68 if (!truthMuons.isValid()) {
69 ATH_MSG_ERROR("No truth muon collection with name " << m_truthMuonSGKey.fullKey() << " found in StoreGate!");
70 return StatusCode::FAILURE;
71 }
72
73 // Set up the decorator
74
75 for (const xAOD::IParticle* part : *parts) {
76 const xAOD::TruthParticle* closest = nullptr;
77 float minDR{FLT_MAX};
78 for (const xAOD::TruthParticle* muTruth : *truthMuons) {
79 const float dR = xAOD::P4Helpers::deltaR2(muTruth, part);
80 if (dR > minDR) continue;
81 closest = muTruth;
82 minDR = dR;
83 }
84
85 for (const xAOD::TruthEvent* event : *tec) {
86 for (size_t parti = 0; parti < event->nTruthParticles(); parti++) {
87 const xAOD::TruthParticle* tpart = event->truthParticle(parti);
88 if (!tpart || !MC::isStable(tpart) || HepMC::is_simulation_particle(tpart) || !tpart->charge() || tpart->isMuon() ||
89 tpart->pt() < m_minPt)
90 continue;
91 const float dR = xAOD::P4Helpers::deltaR2(tpart, part);
92 if (dR > minDR) continue;
93 closest = tpart;
94 minDR = dR;
95 }
96 }
97
98 decorator_dR(*part) = closest ? std::sqrt(minDR) : -1;
99
100 int newType{-1}, newOrigin{-1};
101 if (closest && closest->isMuon()) {
102 newType = acc_tT(*closest);
103 newOrigin = acc_tO(*closest);
104 } else if (closest) {
105 auto res = m_mcTruthClassifier->particleTruthClassifier(closest);
106 newType = res.first;
107 newOrigin = res.second;
108 }
109 decorator_type(*part) = newType;
110 decorator_origin(*part) = newOrigin;
111
112 decorator_pu_dR(*part) = -1;
113 decorator_pu_type(*part) = -1;
114 decorator_pu_origin(*part) = -1;
115
116 minDR = FLT_MAX;
117
118 if (tpec.isValid()) {
119 const xAOD::TruthParticle* closestPileup = nullptr;
120 for (auto event : *tpec) {
121 for (size_t parti = 0; parti < event->nTruthParticles(); parti++) {
122 const xAOD::TruthParticle* tpart = event->truthParticle(parti);
123 if (!tpart || !MC::isStable(tpart) || HepMC::is_simulation_particle(tpart) || !tpart->charge() || tpart->isMuon() ||
124 tpart->pt() < m_minPt)
125 continue;
126 const float dR = xAOD::P4Helpers::deltaR2(tpart, part);
127 if (dR > minDR) continue;
128 closestPileup = tpart;
129 minDR = dR;
130 }
131 }
132 decorator_pu_dR(*part) = (closestPileup ? std::sqrt(minDR) : -1);
133 int newPileupType{-1}, newPileupOrigin{-1};
134 if (closestPileup) {
135 auto res = m_mcTruthClassifier->particleTruthClassifier(closestPileup);
136 newPileupType = res.first;
137 newPileupOrigin = res.second;
138 }
139 decorator_pu_type(*part) = newPileupType;
140 decorator_pu_origin(*part) = newPileupOrigin;
141 }
142 }
143
144 return StatusCode::SUCCESS;
145}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
ATLAS-specific HepMC functions.
std::pair< std::vector< unsigned int >, bool > res
SG::ReadHandleKey< xAOD::TruthPileupEventContainer > m_truthPileupSGKey
SG::ReadHandleKey< xAOD::TruthEventContainer > m_truthSGKey
SG::WriteDecorHandleKey< xAOD::IParticleContainer > m_Truth_type_Key
SG::WriteDecorHandleKey< xAOD::IParticleContainer > m_Truth_origin_Key
virtual StatusCode addBranches(const EventContext &ctx) const override
SG::WriteDecorHandleKey< xAOD::IParticleContainer > m_Truth_dR_Key
SG::WriteDecorHandleKey< xAOD::IParticleContainer > m_Truth_PU_dR_Key
SG::ReadHandleKey< xAOD::IParticleContainer > m_containerKey
SG::WriteDecorHandleKey< xAOD::IParticleContainer > m_Truth_PU_origin_Key
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_truthMuonSGKey
SG::WriteDecorHandleKey< xAOD::IParticleContainer > m_Truth_PU_type_Key
Helper class to provide type-safe access to aux data.
Helper class to provide type-safe access to aux data.
Definition Decorator.h:59
virtual bool isValid() override final
Can the handle be successfully dereferenced?
Class providing the definition of the 4-vector interface.
virtual double pt() const override final
The transverse momentum ( ) of the particle.
double charge() const
Physical charge.
bool isMuon() const
Whether the particle is a muon (or antimuon)
bool is_simulation_particle(const T &p)
Method to establish if a particle (or barcode) was created during the simulation (TODO update to be s...
bool isStable(const T &p)
Identify if the particle is stable, i.e. has not decayed.
double deltaR2(double rapidity1, double phi1, double rapidity2, double phi2)
from bare rapidity,phi
TruthEvent_v1 TruthEvent
Typedef to implementation.
Definition TruthEvent.h:17
TruthParticle_v1 TruthParticle
Typedef to implementation.