ATLAS Offline Software
TruthParticleFixerAlg.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
27 
28  // Return gracefully.
29  return StatusCode::SUCCESS;
30 }
31 
32 StatusCode TruthParticleFixerAlg::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::TruthParticleContainer>();
39  auto outputAuxContainer = std::make_unique<xAOD::AuxContainerBase>();
40  outputContainer->setStore(outputAuxContainer.get());
41 
42  // Variable specific to the old schema.
43  static const SG::ConstAccessor<int> barcodeAcc("barcode");
44 
45  // Deep-copy the objects in a relatively slow/inefficient way.
46  bool warningPrinted = false;
47  for (const xAOD::TruthParticle* input : *inputContainer) {
48  // Create the output particle as a (deep) copy of the input one.
50  outputContainer->push_back(output);
51  *output = *input;
52  // Fix up the output particle if needed.
53  if (barcodeAcc.isAvailable(*output)) {
54  // Access the barcode copied from the in-file container.
55  const int barcode = barcodeAcc(*output);
56  // convert "old" status + barcode to "new" status values
57  output->setStatus(
58  HepMC::new_particle_status_from_old(output->status(), barcode));
59  // The old barcode is still a unique identifier
60  output->setUid(barcode);
61  } else if (warningPrinted == false) {
62  // Print a warning if not, but don't fail.
64  "xAOD::TruthParticle barcode is not available. This algorithm should "
65  "not be run with the current input.");
66  warningPrinted = true;
67  }
68 
69  // Fix the truth particle links on it if needed.
70  for (const std::string& linkName : m_particleLinks.value()) {
71  // Access the link in question.
73  linkAcc(linkName);
74  if (!linkAcc.isAvailable(*output)) {
75  ANA_MSG_ERROR("TruthParticle link \"" << linkName
76  << "\" is not available on "
77  "container \""
79  << "\"");
80  return StatusCode::FAILURE;
81  }
82  auto& links = linkAcc(*output);
83  // Update the name of the container that it's pointing to.
84  for (auto& link : links) {
85  link.resetWithKeyAndIndex(
86  Details::removePrefix(link.dataID(), m_linkPrefixToRemove.value()),
87  link.index());
88  }
89  }
90 
91  // Fix the truth vertex links on it if needed.
92  for (const std::string& linkName : m_vertexLinks.value()) {
93  // Access the link in question.
95  linkAcc(linkName);
96  if (!linkAcc.isAvailable(*output)) {
97  ANA_MSG_ERROR("TruthParticle link \"" << linkName
98  << "\" is not available on "
99  "container \""
101  << "\"");
102  return StatusCode::FAILURE;
103  }
104  auto& links = linkAcc(*output);
105  // Update the name of the container that it's pointing to.
106  for (auto& link : links) {
107  link.resetWithKeyAndIndex(
108  Details::removePrefix(link.dataID(), m_linkPrefixToRemove.value()),
109  link.index());
110  }
111  }
112  }
113 
114  // Record the modified truth particles.
115  ANA_CHECK(
117  .record(std::move(outputContainer), std::move(outputAuxContainer)));
118  ANA_MSG_DEBUG("Recorded TruthParticleContainer with key: "
120 
121  // Return gracefully.
122  return StatusCode::SUCCESS;
123 }
124 
125 } // namespace xAODMaker
WriteHandle.h
Handle class for recording to StoreGate.
xAODMaker::TruthParticleFixerAlg::m_particleLinks
Gaudi::Property< std::vector< std::string > > m_particleLinks
Names of the truth particle links to fix.
Definition: TruthParticleFixerAlg.h:49
SG::Accessor
Helper class to provide type-safe access to aux data.
Definition: Control/AthContainers/AthContainers/Accessor.h:68
xAODMaker::TruthParticleFixerAlg::initialize
virtual StatusCode initialize() override
Function initialising the algorithm.
Definition: TruthParticleFixerAlg.cxx:22
xAODMaker::TruthParticleFixerAlg::m_linkPrefixToRemove
Gaudi::Property< std::string > m_linkPrefixToRemove
Prefix to remove from the link names.
Definition: TruthParticleFixerAlg.h:57
TruthVertexContainer.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 >
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
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
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
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:37
TruthParticleFixerAlg.h
ANA_MSG_WARNING
#define ANA_MSG_WARNING(xmsg)
Macro printing warning messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:292
xAOD::TruthParticle
TruthParticle_v1 TruthParticle
Typedef to implementation.
Definition: Event/xAOD/xAODTruth/xAODTruth/TruthParticle.h:15
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
MagicNumbers.h
ReadHandle.h
Handle class for reading from StoreGate.
xAODMaker::TruthParticleFixerAlg::m_outputContainerKey
SG::WriteHandleKey< xAOD::TruthParticleContainer > m_outputContainerKey
The keys for the output xAOD truth containers.
Definition: TruthParticleFixerAlg.h:44
xAODMaker::TruthParticleFixerAlg::m_inputContainerKey
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_inputContainerKey
The keys of the input xAOD truth containers.
Definition: TruthParticleFixerAlg.h:39
SG::ConstAccessor::isAvailable
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
xAODMaker::TruthParticleFixerAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override
Function executing the algorithm.
Definition: TruthParticleFixerAlg.cxx:32
ANA_MSG_DEBUG
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:288
xAODMaker::TruthParticleFixerAlg::m_vertexLinks
Gaudi::Property< std::vector< std::string > > m_vertexLinks
Names of the truth vertex links to fix.
Definition: TruthParticleFixerAlg.h:53
HepMC::new_particle_status_from_old
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.
Definition: MagicNumbers.h:404