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