ATLAS Offline Software
MuonTruthClassifierFallback.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 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 
10 #include "xAODMuon/MuonContainer.h"
11 
12 namespace {
13  static const SG::AuxElement::Decorator<float> decorator_dR("MCTFallback_dR");
14  static const SG::AuxElement::Decorator<int> decorator_type("MCTFallback_truthType");
15  static const SG::AuxElement::Decorator<int> decorator_origin("MCTFallback_truthOrigin");
16 
17  static const SG::AuxElement::Decorator<float> decorator_pu_dR("MCTFallbackPU_dR");
18  static const SG::AuxElement::Decorator<int> decorator_pu_type("MCTFallbackPU_truthType");
19  static const SG::AuxElement::Decorator<int> decorator_pu_origin("MCTFallbackPU_truthOrigin");
20 
21  static const SG::AuxElement::Accessor<int> acc_tT("truthType");
22  static const SG::AuxElement::Accessor<int> acc_tO("truthOrigin");
23 
24 } // namespace
25 // Constructor
27  const IInterface* p) :
28  AthAlgTool(t, n, p) {
29  declareInterface<DerivationFramework::IAugmentationTool>(this);
30 }
31 
32 // Athena initialize and finalize
34  ATH_MSG_VERBOSE("initialize() ...");
35  ATH_CHECK(m_mcTruthClassifier.retrieve());
36 
37  ATH_CHECK(m_containerKey.initialize());
38  ATH_CHECK(m_truthSGKey.initialize());
39  ATH_CHECK(m_truthPileupSGKey.initialize());
40  ATH_CHECK(m_truthMuonSGKey.initialize());
41 
43  m_Truth_dR_Key = m_containerKey.key() + ".MCTFallback_dR";
44  m_Truth_type_Key = m_containerKey.key() + ".MCTFallback_truthType";
45  m_Truth_origin_Key = m_containerKey.key() + ".MCTFallback_truthOrigin";
46  ATH_CHECK(m_Truth_dR_Key.initialize());
47  ATH_CHECK(m_Truth_type_Key.initialize());
48  ATH_CHECK(m_Truth_origin_Key.initialize());
49 
50  m_Truth_PU_dR_Key = m_containerKey.key() + ".MCTFallbackPU_dR";
51  m_Truth_PU_type_Key = m_containerKey.key() + ".MCTFallbackPU_truthType";
52  m_Truth_PU_origin_Key = m_containerKey.key() + ".MCTFallbackPU_truthOrigin";
53  ATH_CHECK(m_Truth_PU_dR_Key.initialize());
54  ATH_CHECK(m_Truth_PU_type_Key.initialize());
55  ATH_CHECK(m_Truth_PU_origin_Key.initialize());
56 
57  return StatusCode::SUCCESS;
58 }
59 
61  // Retrieve main particle collection
62  const EventContext& ctx = Gaudi::Hive::currentContext();
63 
65  if (!parts.isValid()) {
66  ATH_MSG_ERROR("No Muon collection with name " << m_containerKey.fullKey() << " found in StoreGate!");
67  return StatusCode::FAILURE;
68  }
69  SG::ReadHandle<xAOD::TruthEventContainer> tec{m_truthSGKey, ctx};
70  if (!tec.isValid()) {
71  ATH_MSG_ERROR("No truth collection with name " << m_truthSGKey.fullKey() << " found in StoreGate!");
72  return StatusCode::FAILURE;
73  }
74  SG::ReadHandle<xAOD::TruthPileupEventContainer> tpec{m_truthPileupSGKey, ctx};
75  if (!tpec.isValid()) {
76  ATH_MSG_DEBUG("No truth pileup collection with name " << m_truthPileupSGKey.fullKey() << " found in StoreGate. Pile-up information will not be filled");
77  }
78 
79  SG::ReadHandle<xAOD::TruthParticleContainer> truthMuons{m_truthMuonSGKey, ctx};
80  if (!truthMuons.isValid()) {
81  ATH_MSG_ERROR("No truth muon collection with name " << m_truthMuonSGKey.fullKey() << " found in StoreGate!");
82  return StatusCode::FAILURE;
83  }
84 
85  // Set up the decorator
86 
87  for (const xAOD::IParticle* part : *parts) {
88  const xAOD::TruthParticle* closest = nullptr;
89  float minDR{FLT_MAX};
90  for (const xAOD::TruthParticle* muTruth : *truthMuons) {
91  const float dR = xAOD::P4Helpers::deltaR2(muTruth, part);
92  if (dR > minDR) continue;
93  closest = muTruth;
94  minDR = dR;
95  }
96 
97  for (const xAOD::TruthEvent* event : *tec) {
98  for (size_t parti = 0; parti < event->nTruthParticles(); parti++) {
99  const xAOD::TruthParticle* tpart = event->truthParticle(parti);
100  if (!tpart || !MC::isStable(tpart) || HepMC::is_simulation_particle(tpart) || !tpart->charge() || tpart->isMuon() ||
101  tpart->pt() < m_minPt)
102  continue;
103  const float dR = xAOD::P4Helpers::deltaR2(tpart, part);
104  if (dR > minDR) continue;
105  closest = tpart;
106  minDR = dR;
107  }
108  }
109 
110  decorator_dR(*part) = closest ? std::sqrt(minDR) : -1;
111 
112  int newType{-1}, newOrigin{-1};
113  if (closest && closest->isMuon()) {
114  newType = acc_tT(*closest);
115  newOrigin = acc_tO(*closest);
116  } else if (closest) {
117  auto res = m_mcTruthClassifier->particleTruthClassifier(closest);
118  newType = res.first;
119  newOrigin = res.second;
120  }
121  decorator_type(*part) = newType;
122  decorator_origin(*part) = newOrigin;
123 
124  decorator_pu_dR(*part) = -1;
125  decorator_pu_type(*part) = -1;
126  decorator_pu_origin(*part) = -1;
127 
128  minDR = FLT_MAX;
129 
130  if (tpec.isValid()) {
131  const xAOD::TruthParticle* closestPileup = nullptr;
132  for (auto event : *tpec) {
133  for (size_t parti = 0; parti < event->nTruthParticles(); parti++) {
134  const xAOD::TruthParticle* tpart = event->truthParticle(parti);
135  if (!tpart || !MC::isStable(tpart) || HepMC::is_simulation_particle(tpart) || !tpart->charge() || tpart->isMuon() ||
136  tpart->pt() < m_minPt)
137  continue;
138  const float dR = xAOD::P4Helpers::deltaR2(tpart, part);
139  if (dR > minDR) continue;
140  closestPileup = tpart;
141  minDR = dR;
142  }
143  }
144  decorator_pu_dR(*part) = (closestPileup ? std::sqrt(minDR) : -1);
145  int newPileupType{-1}, newPileupOrigin{-1};
146  if (closestPileup) {
147  auto res = m_mcTruthClassifier->particleTruthClassifier(closestPileup);
148  newPileupType = res.first;
149  newPileupOrigin = res.second;
150  }
151  decorator_pu_type(*part) = newPileupType;
152  decorator_pu_origin(*part) = newPileupOrigin;
153  }
154  }
155 
156  return StatusCode::SUCCESS;
157 }
LArG4FSStartPointFilter.part
part
Definition: LArG4FSStartPointFilter.py:21
SG::Accessor
Helper class to provide type-safe access to aux data.
Definition: Control/AthContainers/AthContainers/Accessor.h:68
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
xAODP4Helpers.h
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
xAOD::P4Helpers::deltaR2
double deltaR2(double rapidity1, double phi1, double rapidity2, double phi2)
from bare rapidity,phi
Definition: xAODP4Helpers.h:111
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:41
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
DerivationFramework::MuonTruthClassifierFallback::addBranches
virtual StatusCode addBranches() const override
Pass the thinning service
Definition: MuonTruthClassifierFallback.cxx:60
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
SG::Decorator
Helper class to provide type-safe access to aux data.
Definition: Decorator.h:59
HepMC::is_simulation_particle
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...
Definition: MagicNumbers.h:355
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
DerivationFramework::MuonTruthClassifierFallback::initialize
StatusCode initialize() override
Definition: MuonTruthClassifierFallback.cxx:33
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:37
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:14
DerivationFramework::MuonTruthClassifierFallback::MuonTruthClassifierFallback
MuonTruthClassifierFallback(const std::string &t, const std::string &n, const IInterface *p)
Constructor with parameters.
Definition: MuonTruthClassifierFallback.cxx:26
xAOD::TruthEvent_v1
Class describing a signal truth event in the MC record.
Definition: TruthEvent_v1.h:35
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
MuonTruthClassifierFallback.h
MuonContainer.h
MC::isStable
bool isStable(const T &p)
Identify if the particle is stable, i.e. has not decayed.
Definition: HepMCHelpers.h:45
xAOD::TruthParticle_v1::isMuon
bool isMuon() const
Whether the particle is a muon (or antimuon)
xAOD::TruthParticle_v1::pt
virtual double pt() const override final
The transverse momentum ( ) of the particle.
Definition: TruthParticle_v1.cxx:166
doL1CaloHVCorrections.parts
parts
Definition: doL1CaloHVCorrections.py:334
AthAlgTool
Definition: AthAlgTool.h:26
xAOD::TruthParticle_v1::charge
double charge() const
Physical charge.
HepMCHelpers.h