ATLAS Offline Software
TruthLinkRepointTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 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  AthAlgTool(t,n,p) {
28  declareInterface<DerivationFramework::IAugmentationTool>(this);
29 }
31  ATH_CHECK(m_recoKey.initialize());
32  if (m_decOutput.value().empty()) {
33  ATH_MSG_FATAL("Please enter a a valid output decorator");
34  return StatusCode::FAILURE;
35  }
36  ATH_CHECK(m_targetKeys.initialize());
37  m_decorKey = m_recoKey.key() + "." + m_decOutput;
38  ATH_CHECK(m_decorKey.initialize());
39  return StatusCode::SUCCESS;
40 }
41 
42 // Destructor
44 
45 // Function to do dressing, implements interface in IAugmentationTool
47  const EventContext& ctx = Gaudi::Hive::currentContext();
48  // Retrieve the truth collections
49  std::vector<const xAOD::TruthParticleContainer*> targets{};
50  targets.reserve(m_targetKeys.size());
51 
53  output_decorator (m_decorKey, ctx);
54 
55  for (const SG::ReadHandleKey<xAOD::TruthParticleContainer>& key : m_targetKeys) {
57  if (!readHandle.isValid()) {
58  ATH_MSG_FATAL("Failed to retrieve "<<key.fullKey());
59  return StatusCode::FAILURE;
60  }
61  targets.emplace_back(readHandle.cptr());
62  }
63 
64 
65  SG::ReadHandle<xAOD::IParticleContainer> inputCont{m_recoKey, ctx};
66  if (!inputCont.isValid()) {
67  ATH_MSG_FATAL("Failed to retrive "<<m_recoKey.fullKey());
68  return StatusCode::FAILURE;
69  }
70  for ( const xAOD::IParticle* input : *inputCont) {
72  output_decorator(*input) = ElementLink<xAOD::TruthParticleContainer>{};
73  for (const xAOD::TruthParticleContainer* target : targets) {
74  int index = find_match(truthPart, target);
75 
76  if (index >=0) output_decorator(*input) = ElementLink<xAOD::TruthParticleContainer>(*target, index);
77 
78  }
79  }
80 
81  return StatusCode::SUCCESS;
82 }
83 
84 // Find a match by unique ID in a different container
86 {
87  // See if it's already gone
88  if (!p) return -1;
89  // Look through the mini-collection
90  for (int i=0;i<int(c->size());++i){
91  if (c->at(i) && HepMC::is_same_particle(p,c->at(i))) return i;
92  }
93  // Note: just fine if it wasn't in the mini-collection
94  return -1;
95 }
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
TruthLinkRepointTool.h
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
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:367
DerivationFramework::TruthLinkRepointTool::initialize
virtual StatusCode initialize() override final
Definition: TruthLinkRepointTool.cxx:30
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ElectronContainer.h
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
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
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DerivationFramework::TruthLinkRepointTool::find_match
static int find_match(const xAOD::TruthParticle *p, const xAOD::TruthParticleContainer *c)
Definition: TruthLinkRepointTool.cxx:85
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
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()
DerivationFramework::TruthLinkRepointTool::addBranches
virtual StatusCode addBranches() const override final
Pass the thinning service
Definition: TruthLinkRepointTool.cxx:46
copySelective.target
string target
Definition: copySelective.py:37
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.
AthAlgTool
Definition: AthAlgTool.h:26
python.compressB64.c
def c
Definition: compressB64.py:93
PhotonContainer.h
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37