ATLAS Offline Software
Loading...
Searching...
No Matches
TruthVertexFixerAlg.cxx
Go to the documentation of this file.
1// Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2
3// Local include(s).
5
6#include "removePrefix.h"
7
8// Framework include(s).
12
13// EDM include(s).
16
17// System include(s).
18#include <cassert>
19
20namespace xAODMaker {
21
23
24 // initialize handles
25 ANA_CHECK(m_inputContainerKey.initialize());
26 ANA_CHECK(m_outputContainerKey.initialize());
27
28 // Return gracefully.
29 return StatusCode::SUCCESS;
30}
31
32StatusCode TruthVertexFixerAlg::execute(const EventContext& ctx) const {
33
34 // Access the input container.
35 auto inputContainer = SG::makeHandle(m_inputContainerKey, ctx);
36
37 // Create the output container and its auxiliary store.
38 auto outputContainer = std::make_unique<xAOD::TruthVertexContainer>();
39 auto outputAuxContainer = std::make_unique<xAOD::AuxContainerBase>();
40 outputContainer->setStore(outputAuxContainer.get());
41
42 // Variable(s) specific to the old schema.
43 static const SG::ConstAccessor<int> barcodeAcc("barcode");
44 static const SG::ConstAccessor<int> idAcc("id");
45
46 // Deep-copy the objects in a relatively slow/inefficient way.
47 bool warningPrinted = false;
48 for (const xAOD::TruthVertex* input : *inputContainer) {
49 // Create the output vertex as a (deep) copy of the input one.
51 outputContainer->push_back(output);
52 *output = *input;
53 // Fix up the output vertex if needed.
54 if (barcodeAcc.isAvailable(*output) && idAcc.isAvailable(*output)) {
55 // Access the variables copied from the in-file container.
56 const int id = idAcc(*output);
57 const int barcode = barcodeAcc(*output);
58 // convert "old" id + barcode to "new" status values
59 output->setStatus(HepMC::new_vertex_status_from_old(id, barcode));
60 // The old barcode is still a unique identifier
61 output->setUid(barcode);
62 } else if (warningPrinted == false) {
63 // Print a warning if not, but don't fail.
65 "One or both of xAOD::TruthVertex barcode and id are not available. "
66 "This algorithm should not be run with these inputs.");
67 warningPrinted = true;
68 }
69
70 // Fix the truth particle links on it if needed.
71 for (const std::string& linkName : m_particleLinks.value()) {
72 // Access the link in question.
74 linkAcc(linkName);
75 if (!linkAcc.isAvailable(*output)) {
76 ANA_MSG_ERROR("TruthParticle link \"" << linkName
77 << "\" is not available on "
78 "container \""
79 << m_inputContainerKey.key()
80 << "\"");
81 return StatusCode::FAILURE;
82 }
83 auto& links = linkAcc(*output);
84 // Update the name of the container that it's pointing to.
85 for (auto& link : links) {
86 link.resetWithKeyAndIndex(
87 Details::removePrefix(link.dataID(), m_linkPrefixToRemove.value()),
88 link.index());
89 }
90 }
91
92 // Fix the truth vertex links on it if needed.
93 for (const std::string& linkName : m_vertexLinks.value()) {
94 // Access the link in question.
96 linkAcc(linkName);
97 if (!linkAcc.isAvailable(*output)) {
98 ANA_MSG_ERROR("TruthParticle link \"" << linkName
99 << "\" is not available on "
100 "container \""
101 << m_inputContainerKey.key()
102 << "\"");
103 return StatusCode::FAILURE;
104 }
105 auto& links = linkAcc(*output);
106 // Update the name of the container that it's pointing to.
107 for (auto& link : links) {
108 link.resetWithKeyAndIndex(
109 Details::removePrefix(link.dataID(), m_linkPrefixToRemove.value()),
110 link.index());
111 }
112 }
113 }
114
115 // Record the modified truth vertices.
116 ANA_CHECK(
118 .record(std::move(outputContainer), std::move(outputAuxContainer)));
120 "Recorded TruthVertexContainer with key: " << m_outputContainerKey.key());
121
122 // Return gracefully.
123 return StatusCode::SUCCESS;
124}
125} // namespace xAODMaker
Handle class for reading from StoreGate.
Handle class for recording to StoreGate.
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
#define ANA_MSG_WARNING(xmsg)
Macro printing warning messages.
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
#define ANA_CHECK(EXP)
check whether the given expression was successful
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.
virtual StatusCode execute(const EventContext &ctx) const override
Function executing the algorithm.
virtual StatusCode initialize() override
Function initialising the algorithm.
Gaudi::Property< std::vector< std::string > > m_particleLinks
Names of the truth particle links to fix.
SG::ReadHandleKey< xAOD::TruthVertexContainer > m_inputContainerKey
The keys of the input xAOD truth containers.
SG::WriteHandleKey< xAOD::TruthVertexContainer > m_outputContainerKey
The keys for the output xAOD truth containers.
Gaudi::Property< std::string > m_linkPrefixToRemove
Prefix to remove from the link names.
Gaudi::Property< std::vector< std::string > > m_vertexLinks
Names of the truth vertex links to fix.
int new_vertex_status_from_old(const int oldStatus, const int barcode)
Get vertex 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.
TruthVertex_v1 TruthVertex
Typedef to implementation.
Definition TruthVertex.h:15