ATLAS Offline Software
DecoratorAlg.icc
Go to the documentation of this file.
1 // In case the extension is confusing, this is a -*- C++ -*- file
2 /*
3  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
4 */
5 
6 #include "StoreGate/ReadDecorHandle.h"
7 
8 #include <exception>
9 
10 namespace FlavorTagDiscriminants {
11  template <typename C, typename D, typename N>
12  DecoratorAlg<C,D,N>::DecoratorAlg(
13  const std::string& name, ISvcLocator* svcloc):
14  AthReentrantAlgorithm(name, svcloc)
15  {
16  // cppcheck-suppress missingReturn; false positive
17  }
18 
19  template <typename C, typename D, typename N>
20  StatusCode DecoratorAlg<C,D,N>::initialize()
21  {
22  return initializeInternal();
23  }
24 
25  template <typename C, typename D, typename N>
26  StatusCode DecoratorAlg<C,D,N>::initializeInternal(
27  DecoratorAlg::ExtraDependencies extraDeps)
28  {
29  ATH_CHECK(m_containerKey.initialize());
30  ATH_CHECK(m_constituentKey.initialize());
31 
32  ATH_CHECK(m_decorator.retrieve());
33 
34  std::set<std::string> veto(
35  m_undeclaredReadDecorKeys.begin(),
36  m_undeclaredReadDecorKeys.end());
37 
38  // now we build data dependencies from the internal tools. We have
39  // to reserve the vectors here to prevent a segfault since read /
40  // write handles aren't movable once declaired as a property.
41  auto auxKeys = m_decorator->getAuxInputKeys();
42  auxKeys.merge(extraDeps);
43  m_aux.reserve(auxKeys.size());
44  for (const std::string& key: auxKeys) {
45  const std::string full = m_containerKey.key() + "." + key;
46  if (veto.count(full)) {
47  ATH_MSG_DEBUG("Not declaring accessor: " + full);
48  continue;
49  }
50  ATH_MSG_DEBUG("Adding accessor: " + full);
51  m_aux.emplace_back(this, key, full, "");
52  ATH_CHECK(m_aux.back().initialize());
53  }
54  m_constituentAux.reserve(m_decorator->getConstituentAuxInputKeys().size());
55  for (const std::string& key: m_decorator->getConstituentAuxInputKeys()) {
56  const std::string full = m_constituentKey.key() + "." + key;
57  if (veto.count(full)) {
58  ATH_MSG_DEBUG("Not declaring accessor: " + full);
59  continue;
60  }
61  ATH_MSG_DEBUG("Adding constituent accessor: " + full);
62  m_constituentAux.emplace_back(this, key, full, "");
63  ATH_CHECK(m_constituentAux.back().initialize());
64  }
65  m_decor.reserve(m_decorator->getDecoratorKeys().size());
66  for (const std::string& key: m_decorator->getDecoratorKeys()) {
67  const std::string full = m_containerKey.key() + "." + key;
68  m_decor.emplace_back(this, key, full, "");
69  ATH_CHECK(m_decor.back().initialize());
70  // look up auxids
71  SG::auxid_t auxid = SG::AuxTypeRegistry::instance().findAuxID(key);
72  ATH_MSG_DEBUG("Added decorator: " + full + ","
73  " key " + key + " has auxid " + std::to_string(auxid) );
74  m_auxids.push_back(auxid);
75  }
76  ATH_MSG_DEBUG("Finished setting up");
77  return StatusCode::SUCCESS;
78  }
79 
80 
81  template <typename C, typename D, typename N>
82  StatusCode DecoratorAlg<C,D,N>::execute(const EventContext& cxt ) const {
83  SG::ReadHandle<C> container(
84  m_containerKey, cxt);
85  if (!container.isValid()) {
86  ATH_MSG_ERROR("no container " << container.key());
87  return StatusCode::FAILURE;
88  }
89  ATH_MSG_DEBUG(
90  "Decorating " + std::to_string(container->size()) + " elements");
91  for (const auto* element: *container) {
92  m_decorator->decorate(*element);
93  }
94 
95  // Lock the decorations
96  //
97  for (SG::auxid_t id: m_auxids) {
98  ATH_MSG_DEBUG("locking auxid " << id << " in " << m_containerKey.key());
99  const_cast<C*>(container.cptr())->lockDecoration(id);
100  }
101 
102  return StatusCode::SUCCESS;
103  }
104 
105  template <typename C, typename D, typename N>
106  StatusCode DecoratorAlg<C,D,N>::finalize() {
107  return StatusCode::SUCCESS;
108  }
109 
110 }