ATLAS Offline Software
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 
20 namespace 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 
32 StatusCode 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.
50  xAOD::TruthVertex* output = new xAOD::TruthVertex();
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
xAODMaker::TruthVertexFixerAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override
Function executing the algorithm.
Definition: TruthVertexFixerAlg.cxx:32
WriteHandle.h
Handle class for recording to StoreGate.
xAODMaker::TruthVertexFixerAlg::m_vertexLinks
Gaudi::Property< std::vector< std::string > > m_vertexLinks
Names of the truth vertex links to fix.
Definition: TruthVertexFixerAlg.h:53
xAODMaker::TruthVertexFixerAlg::initialize
virtual StatusCode initialize() override
Function initialising the algorithm.
Definition: TruthVertexFixerAlg.cxx:22
SG::Accessor
Helper class to provide type-safe access to aux data.
Definition: Control/AthContainers/AthContainers/Accessor.h:68
TruthParticleContainer.h
AuxContainerBase.h
ANA_MSG_ERROR
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:294
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
SG::ConstAccessor< int >
xAODMaker
Definition: StoreGateSvc.h:70
xAODMaker::Details::removePrefix
std::string removePrefix(std::string_view str, std::string_view prefix)
Remove a prefix from a string, if it exists.
Definition: removePrefix.cxx:8
xAODMaker::TruthVertexFixerAlg::m_linkPrefixToRemove
Gaudi::Property< std::string > m_linkPrefixToRemove
Prefix to remove from the link names.
Definition: TruthVertexFixerAlg.h:57
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:274
removePrefix.h
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
DMTest::links
links
Definition: CLinks_v1.cxx:22
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
HepMC::new_vertex_status_from_old
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.
Definition: MagicNumbers.h:414
ANA_MSG_WARNING
#define ANA_MSG_WARNING(xmsg)
Macro printing warning messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:292
xAODMaker::TruthVertexFixerAlg::m_outputContainerKey
SG::WriteHandleKey< xAOD::TruthVertexContainer > m_outputContainerKey
The keys for the output xAOD truth containers.
Definition: TruthVertexFixerAlg.h:44
xAOD::TruthVertex
TruthVertex_v1 TruthVertex
Typedef to implementation.
Definition: TruthVertex.h:15
xAODMaker::TruthVertexFixerAlg::m_inputContainerKey
SG::ReadHandleKey< xAOD::TruthVertexContainer > m_inputContainerKey
The keys of the input xAOD truth containers.
Definition: TruthVertexFixerAlg.h:39
xAOD::TruthVertex_v1
Class describing a truth vertex in the MC record.
Definition: TruthVertex_v1.h:37
MagicNumbers.h
TruthVertexFixerAlg.h
ReadHandle.h
Handle class for reading from StoreGate.
xAODMaker::TruthVertexFixerAlg::m_particleLinks
Gaudi::Property< std::vector< std::string > > m_particleLinks
Names of the truth particle links to fix.
Definition: TruthVertexFixerAlg.h:49
SG::ConstAccessor::isAvailable
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
ANA_MSG_DEBUG
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:288