ATLAS Offline Software
TruthLinkRepointTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // TruthLinkRepointTool.cxx
7 // Truth links on some objects point to the main truth particle
8 // container, or to some other container that won't be saved in the
9 // output derivation. This re-points the links from the old
10 // container to the new container (and serves as a chance to clean
11 // up / harmonize the names of the decorations).
12 
13 #include "TruthLinkRepointTool.h"
14 #include "xAODMuon/MuonContainer.h"
19 #include "StoreGate/ReadHandle.h"
20 
22 
23 // Constructor
25  const std::string& n,
26  const IInterface* p ) :
27  base_class(t,n,p) {
28 }
30  ATH_CHECK(m_recoKey.initialize());
31  if (m_decOutput.value().empty()) {
32  ATH_MSG_FATAL("Please enter a a valid output decorator");
33  return StatusCode::FAILURE;
34  }
35  ATH_CHECK(m_targetKeys.initialize());
36  m_decorKey = m_recoKey.key() + "." + m_decOutput;
37  ATH_CHECK(m_decorKey.initialize());
38  return StatusCode::SUCCESS;
39 }
40 
41 // Destructor
43 
44 // Function to do dressing, implements interface in IAugmentationTool
46  // Retrieve the truth collections
47  std::vector<const xAOD::TruthParticleContainer*> targets{};
48  targets.reserve(m_targetKeys.size());
49 
51  output_decorator (m_decorKey, ctx);
52 
53  for (const SG::ReadHandleKey<xAOD::TruthParticleContainer>& key : m_targetKeys) {
55  if (!readHandle.isValid()) {
56  ATH_MSG_FATAL("Failed to retrieve "<<key.fullKey());
57  return StatusCode::FAILURE;
58  }
59  targets.emplace_back(readHandle.cptr());
60  }
61 
62 
63  SG::ReadHandle<xAOD::IParticleContainer> inputCont{m_recoKey, ctx};
64  if (!inputCont.isValid()) {
65  ATH_MSG_FATAL("Failed to retrieve "<<m_recoKey.fullKey());
66  return StatusCode::FAILURE;
67  }
68  for ( const xAOD::IParticle* input : *inputCont) {
70  output_decorator(*input) = ElementLink<xAOD::TruthParticleContainer>{};
71  for (const xAOD::TruthParticleContainer* target : targets) {
72  int index = find_match(truthPart, target);
73 
74  if (index >=0) output_decorator(*input) = ElementLink<xAOD::TruthParticleContainer>(*target, index);
75 
76  }
77  }
78 
79  return StatusCode::SUCCESS;
80 }
81 
82 // Find a match by unique ID in a different container
84 {
85  // See if it's already gone
86  if (!p) return -1;
87  // Look through the mini-collection
88  for (int i=0;i<int(c->size());++i){
89  if (c->at(i) && HepMC::is_same_particle(p,c->at(i))) return i;
90  }
91  // Note: just fine if it wasn't in the mini-collection
92  return -1;
93 }
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
TruthLinkRepointTool.h
SG::ReadHandle< xAOD::TruthParticleContainer >
index
Definition: index.py:1
TruthParticleContainer.h
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
SG::ReadHandleKey< xAOD::TruthParticleContainer >
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:41
xAODTruthHelpers.h
HepMC::is_same_particle
bool is_same_particle(const T1 &p1, const T2 &p2)
Method to establish if two particles in the GenEvent actually represent the same particle.
Definition: MagicNumbers.h:366
DerivationFramework::TruthLinkRepointTool::initialize
virtual StatusCode initialize() override final
Definition: TruthLinkRepointTool.cxx:29
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
ElectronContainer.h
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:727
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:37
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:100
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DerivationFramework::TruthLinkRepointTool::addBranches
virtual StatusCode addBranches(const EventContext &ctx) const override final
Definition: TruthLinkRepointTool.cxx:45
DerivationFramework::TruthLinkRepointTool::find_match
static int find_match(const xAOD::TruthParticle *p, const xAOD::TruthParticleContainer *c)
Definition: TruthLinkRepointTool.cxx:83
DataVector
Derived DataVector<T>.
Definition: DataVector.h:795
MagicNumbers.h
xAOD::TruthHelpers::getTruthParticle
const xAOD::TruthParticle * getTruthParticle(const xAOD::IParticle &p)
Return the truthParticle associated to the given IParticle (if any)
Definition: xAODTruthHelpers.cxx:25
MuonContainer.h
DerivationFramework::TruthLinkRepointTool::~TruthLinkRepointTool
~TruthLinkRepointTool()
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
copySelective.target
string target
Definition: copySelective.py:36
DerivationFramework::TruthLinkRepointTool::TruthLinkRepointTool
TruthLinkRepointTool(const std::string &t, const std::string &n, const IInterface *p)
Definition: TruthLinkRepointTool.cxx:24
ReadHandle.h
Handle class for reading from StoreGate.
python.compressB64.c
def c
Definition: compressB64.py:93
PhotonContainer.h
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37