ATLAS Offline Software
Loading...
Searching...
No Matches
TruthParticleFixerAlg.cxx
Go to the documentation of this file.
1// Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
2
3// Local include(s).
5
6#include "removePrefix.h"
7
8// Framework include(s).
12
14
15// EDM include(s).
18
19// System include(s).
20#include <cassert>
21
22namespace xAODMaker {
23
25
26 // initialize handles
27 ANA_CHECK(m_inputContainerKey.initialize());
28 ANA_CHECK(m_outputContainerKey.initialize());
29
30 ATH_CHECK(m_uidKey.initialize());
31
32 // Return gracefully.
33 return StatusCode::SUCCESS;
34}
35
36StatusCode TruthParticleFixerAlg::execute(const EventContext& ctx) const {
37
38 // Access the input container.
39 auto inputContainer = SG::makeHandle(m_inputContainerKey, ctx);
40
41 // Create the output container and its auxiliary store.
42 auto outputContainer = std::make_unique<xAOD::TruthParticleContainer>();
43 auto outputAuxContainer = std::make_unique<xAOD::AuxContainerBase>();
44 outputContainer->setStore(outputAuxContainer.get());
45
46 // Variable specific to the old schema.
47 static const SG::ConstAccessor<int> barcodeAcc("barcode");
48
49 // Decorate old input collection with UID in case
51 if(!uid.isAvailable()){
52 for (const xAOD::TruthParticle* input : *inputContainer) {
53 uid(*input) = barcodeAcc(*input);
54 }
55 }
56
57 // Deep-copy the objects in a relatively slow/inefficient way.
58 bool warningPrinted = false;
59 for (const xAOD::TruthParticle* input : *inputContainer) {
60 // Create the output particle as a (deep) copy of the input one.
62 outputContainer->push_back(output);
63 *output = *input;
64 // Fix up the output particle if needed.
65 if (barcodeAcc.isAvailable(*output)) {
66 // Access the barcode copied from the in-file container.
67 const int barcode = barcodeAcc(*output);
68 // convert "old" status + barcode to "new" status values
69 output->setStatus(
70 HepMC::new_particle_status_from_old(output->status(), barcode));
71 // The old barcode is still a unique identifier
72 output->setUid(barcode);
73 } else if (warningPrinted == false) {
74 // Print a warning if not, but don't fail.
76 "xAOD::TruthParticle barcode is not available. This algorithm should "
77 "not be run with the current input.");
78 warningPrinted = true;
79 }
80
81 // Fix the truth particle links on it if needed.
82 for (const std::string& linkName : m_particleLinks.value()) {
83 // Access the link in question.
85 linkAcc(linkName);
86 if (!linkAcc.isAvailable(*output)) {
87 ANA_MSG_ERROR("TruthParticle link \"" << linkName
88 << "\" is not available on "
89 "container \""
90 << m_inputContainerKey.key()
91 << "\"");
92 return StatusCode::FAILURE;
93 }
94 auto& links = linkAcc(*output);
95 // Update the name of the container that it's pointing to.
96 for (auto& link : links) {
97 link.resetWithKeyAndIndex(
98 Details::removePrefix(link.dataID(), m_linkPrefixToRemove.value()),
99 link.index(),
100 ctx);
101 }
102 }
103
104 // Fix the truth vertex links on it if needed.
105 for (const std::string& linkName : m_vertexLinks.value()) {
106 // Access the link in question.
108 linkAcc(linkName);
109 if (!linkAcc.isAvailable(*output)) {
110 ANA_MSG_ERROR("TruthParticle link \"" << linkName
111 << "\" is not available on "
112 "container \""
113 << m_inputContainerKey.key()
114 << "\"");
115 return StatusCode::FAILURE;
116 }
117 auto& links = linkAcc(*output);
118 // Update the name of the container that it's pointing to.
119 for (auto& link : links) {
120 link.resetWithKeyAndIndex(
121 Details::removePrefix(link.dataID(), m_linkPrefixToRemove.value()),
122 link.index(),
123 ctx);
124 }
125 }
126 }
127
128 // Record the modified truth particles.
129 ANA_CHECK(
131 .record(std::move(outputContainer), std::move(outputAuxContainer)));
132 ANA_MSG_DEBUG("Recorded TruthParticleContainer with key: "
133 << m_outputContainerKey.key());
134
135 // Return gracefully.
136 return StatusCode::SUCCESS;
137}
138
139} // namespace xAODMaker
#define ATH_CHECK
Evaluate an expression and check for errors.
Handle class for reading from StoreGate.
Handle class for adding a decoration to an object.
Handle class for recording to StoreGate.
#define ANA_MSG_ERROR(xmsg,...)
Macro printing error messages.
#define ANA_MSG_DEBUG(xmsg,...)
Macro printing debug messages.
#define ANA_CHECK(EXP)
check whether the given expression was successful
#define ANA_MSG_WARNING(xmsg,...)
Macro printing warning messages.
Helper class to provide type-safe access to aux data.
Helper class to provide constant type-safe access to aux data.
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
Handle class for adding a decoration to an object.
Gaudi::Property< std::vector< std::string > > m_particleLinks
Names of the truth particle links to fix.
Gaudi::Property< std::string > m_linkPrefixToRemove
Prefix to remove from the link names.
virtual StatusCode initialize() override
Function initialising the algorithm.
SG::WriteHandleKey< xAOD::TruthParticleContainer > m_outputContainerKey
The keys for the output xAOD truth containers.
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_uidKey
virtual StatusCode execute(const EventContext &ctx) const override
Function executing the algorithm.
Gaudi::Property< std::vector< std::string > > m_vertexLinks
Names of the truth vertex links to fix.
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_inputContainerKey
The keys of the input xAOD truth containers.
int new_particle_status_from_old(const int oldStatus, const int barcode)
Get particle status in the new scheme from the barcode and status in the old scheme.
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
std::string removePrefix(std::string_view str, std::string_view prefix)
Remove a prefix from a string, if it exists.
TruthParticle_v1 TruthParticle
Typedef to implementation.