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-2025 CERN for the benefit of the ATLAS collaboration
4 */
5 
6 #include "StoreGate/ReadDecorHandle.h"
7 
8 #include <exception>
9 
10 namespace FlavorTagInference {
11  template <typename C, typename D, typename N>
12  StatusCode DecoratorAlg<C,D,N>::initialize()
13  {
14  return initializeInternal();
15  }
16 
17  template <typename C, typename D, typename N>
18  StatusCode DecoratorAlg<C,D,N>::initializeInternal(
19  DecoratorAlg::ExtraDependencies extraDeps)
20  {
21  ATH_CHECK(m_containerKey.initialize());
22  ATH_CHECK(m_constituentKey.initialize());
23  ATH_CHECK(m_electronKey.initialize(!m_electronKey.empty()));
24 
25  ATH_CHECK(m_decorator.retrieve());
26 
27  std::set<std::string> veto(
28  m_undeclaredReadDecorKeys.begin(),
29  m_undeclaredReadDecorKeys.end());
30 
31  // now we build data dependencies from the internal tools. We have
32  // to reserve the vectors here to prevent a segfault since read /
33  // write handles aren't movable once declaired as a property.
34  auto deps = m_decorator->getDependencies();
35  // jet inputs
36  auto auxKeys = deps.bTagInputs;
37  auxKeys.merge(extraDeps);
38  m_aux.reserve(auxKeys.size());
39  for (const std::string& key: auxKeys) {
40  const std::string full = m_containerKey.key() + "." + key;
41  if (veto.count(full)) {
42  ATH_MSG_DEBUG("Not declaring accessor: " + full);
43  continue;
44  }
45  ATH_MSG_DEBUG("Adding accessor: " + full);
46  m_aux.emplace_back(this, key, full, "");
47  ATH_CHECK(m_aux.back().initialize());
48  }
49  // track inputs
50  auto trackInputs = deps.trackInputs;
51  m_constituentAux.reserve(trackInputs.size());
52  for (const std::string& key: trackInputs) {
53  const std::string full = m_constituentKey.key() + "." + key;
54  if (veto.count(full)) {
55  ATH_MSG_DEBUG("Not declaring accessor: " + full);
56  continue;
57  }
58  ATH_MSG_DEBUG("Adding constituent accessor: " + full);
59  m_constituentAux.emplace_back(this, key, full, "");
60  ATH_CHECK(m_constituentAux.back().initialize());
61  }
62  // electron inputs
63  size_t n_electron_inputs = deps.electronInputs.size();
64  if (n_electron_inputs > 0 && m_electronKey.empty()) {
65  ATH_MSG_ERROR("using electrons without defining the input container");
66  return StatusCode::FAILURE;
67  }
68  m_electronAux.reserve(n_electron_inputs);
69  for (const std::string& key: deps.electronInputs) {
70  const std::string full = m_electronKey.key() + "." + key;
71  ATH_MSG_DEBUG("Adding electron accessor: " + full);
72  m_electronAux.emplace_back(this, key, full, "");
73  ATH_CHECK(m_electronAux.back().initialize());
74  }
75  // jet outputs
76  std::set<std::string> keys = deps.bTagOutputs;
77  m_decor.reserve(keys.size());
78  for (const std::string& key: keys) {
79  const std::string full = m_containerKey.key() + "." + key;
80  m_decor.emplace_back(this, key, full, "");
81  ATH_CHECK(m_decor.back().initialize());
82  // look up auxids
83  SG::auxid_t auxid = SG::AuxTypeRegistry::instance().findAuxID(key);
84  ATH_MSG_DEBUG("Added decorator: " + full + ","
85  " key " + key + " has auxid " + std::to_string(auxid) );
86  m_auxids.push_back(auxid);
87  }
88  ATH_MSG_DEBUG("Finished setting up");
89  return StatusCode::SUCCESS;
90  }
91 
92 
93  template <typename C, typename D, typename N>
94  StatusCode DecoratorAlg<C,D,N>::execute(const EventContext& cxt ) const {
95  SG::ReadHandle<C> container(
96  m_containerKey, cxt);
97  if (!container.isValid()) {
98  ATH_MSG_ERROR("no container " << container.key());
99  return StatusCode::FAILURE;
100  }
101  ATH_MSG_DEBUG(
102  "Decorating " + std::to_string(container->size()) + " elements");
103  for (const auto* element: *container) {
104  m_decorator->decorate(*element);
105  }
106 
107  // Lock the decorations
108  //
109  for (SG::auxid_t id: m_auxids) {
110  ATH_MSG_DEBUG("locking auxid " << id << " in " << m_containerKey.key());
111  const_cast<C*>(container.cptr())->lockDecoration(id);
112  }
113 
114  return StatusCode::SUCCESS;
115  }
116 
117  template <typename C, typename D, typename N>
118  StatusCode DecoratorAlg<C,D,N>::finalize() {
119  return StatusCode::SUCCESS;
120  }
121 
122 }