ATLAS Offline Software
TruthLinkRepointTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 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 
54  for (const SG::ReadHandleKey<xAOD::TruthParticleContainer>& key : m_targetKeys) {
56  if (!readHandle.isValid()) {
57  ATH_MSG_FATAL("Failed to retrieve "<<key.fullKey());
58  return StatusCode::FAILURE;
59  }
60  targets.emplace_back(readHandle.cptr());
61  }
62 
63 
64  SG::ReadHandle<xAOD::IParticleContainer> inputCont{m_recoKey, ctx};
65  if (!inputCont.isValid()) {
66  ATH_MSG_FATAL("Failed to retrive "<<m_recoKey.fullKey());
67  return StatusCode::FAILURE;
68  }
69  for ( const xAOD::IParticle* input : *inputCont) {
71  output_decorator(*input) = ElementLink<xAOD::TruthParticleContainer>{};
72  for (const xAOD::TruthParticleContainer* target : targets) {
73  int index = find_match(truthPart, target);
74 
75  if (index >=0) output_decorator(*input) = ElementLink<xAOD::TruthParticleContainer>(*target, index);
76 
77  }
78  }
79 
80  return StatusCode::SUCCESS;
81 }
82 
83 // Find a match by unique ID in a different container
85 {
86  // See if it's already gone
87  if (!p) return -1;
88  // Look through the mini-collection
89  for (int i=0;i<int(c->size());++i){
90  if (c->at(i) && HepMC::uniqueID(p) == HepMC::uniqueID(c->at(i))) return i;
91  }
92  // Note: just fine if it wasn't in the mini-collection
93  return -1;
94 }
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
TruthLinkRepointTool.h
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
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:40
xAODTruthHelpers.h
DerivationFramework::TruthLinkRepointTool::initialize
virtual StatusCode initialize() override final
Definition: TruthLinkRepointTool.cxx:30
SG::Decorator
Helper class to provide type-safe access to aux data.
Definition: Decorator.h:58
ElectronContainer.h
lumiFormat.i
int i
Definition: lumiFormat.py:92
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:41
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
HepMC::uniqueID
int uniqueID(const T &p)
Definition: MagicNumbers.h:113
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:84
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
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
COOLRates.target
target
Definition: COOLRates.py:1106
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