ATLAS Offline Software
Loading...
Searching...
No Matches
PlainAccessor.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4#ifndef ExpressionParsing_PlainAccessor_h_
5#define ExpressionParsing_PlainAccessor_h_
6
9#include "GaudiKernel/EventContext.h"
11
12#include <vector>
13#include <map>
14#include <iostream>
15#include <sstream>
16#include <typeinfo>
17#include <stdexcept>
18
19#include "GenAccessor.h"
20#include "Utils.h"
21
22namespace ExpressionParsing {
25 template <typename T_src>
27 public:
28 std::size_t size(const std::vector<T_src> &cont) const {
29 return cont.size(); //@TODO correct ? or 1 ?
30 }
31
33 T_src get(const std::vector<T_src> &cont) const {
34 (void) cont;
35 return cont[0];
36 }
37
39 T_src get(const std::vector<T_src> &cont, std::size_t idx) const {
40 (void) cont;
41 (void) idx;
42 return cont[idx];
43 }
44
47 class Kit {
48 public:
49 StdVectorHelper<T_src> create(const EventContext& ctx, SG::ReadHandle<std::vector<T_src> > &handle) const {
50 (void) ctx;
51 (void) handle;
53 }
54 };
55 };
56
57 template <typename T_src>
59 public:
60 std::size_t size(const T_src &cont) const {
61 (void) cont;
62 return 1; //@TODO correct ? or 1 ?
63 }
64
66 T_src get(const T_src &cont) const {
67 (void) cont;
68 return cont;
69 }
70
72 T_src get(const T_src &cont, std::size_t idx) const {
73 (void) cont;
74 (void) idx;
75 assert( idx == 0);
76 return cont;
77 }
78
81 class Kit {
82 public:
83 PlainValueHelper<T_src> create(const EventContext& ctx, SG::ReadHandle<T_src > &handle) const {
84 (void) ctx;
85 (void) handle;
87 }
88 };
89 };
90
91 class PlainAccessorFactory : public Singleton<PlainAccessorFactory> {
92 public:
93 class IKit {
94 public:
95 virtual ~IKit() {}
96 virtual std::unique_ptr<IAccessor> createAccessor( const std::any &handle_key) const = 0;
97 virtual bool registerReadKey(std::unordered_map<std::string, std::any > &read_keys,
99 const std::string &var_name,
100 std::vector<Gaudi::DataHandle *> &new_input_handles,
101 bool verbose) const = 0;
102 };
103
104
105 static void throwFailedToAddHandle(const std::string &var_name) {
106 std::stringstream msg;
107 msg << "Failed to add read handle key for " << var_name;
108 throw std::runtime_error(msg.str());
109 }
110
111 template <class T_Cont>
112 static bool registerReadKey(std::unordered_map<std::string, std::any > &read_keys,
114 const std::string &var_name,
115 std::vector<Gaudi::DataHandle *> &new_input_handles,
116 bool verbose) {
117 SG::ReadHandleKey< T_Cont > key(var_name);
118 const auto &[mapIterator, inserted] = read_keys.try_emplace(var_name, std::move(key));
119 if (!inserted) {
120 if ( mapIterator->first == var_name
121 && mapIterator->second.type().hash_code() == typeid(key).hash_code()) {
122 return true;
123 }
125 }
126 SG::ReadHandleKey< T_Cont > *key_final( std::any_cast<SG::ReadHandleKey< T_Cont > >( &mapIterator->second));
127 if (!key_final) {
129 }
130 if (parent.declare(*key_final).isFailure()) {
132 }
133 if (verbose) dumpDeclare(key_final);
134 initializeHandle(key_final, new_input_handles);
135 if (verbose) {
136 std::cout << "DEBUG PlainAccessorFactory::registerReadKey register read handle key for " << var_name << " [" << typeid(T_Cont).name() << "]."<< std::endl;
137 }
138 return true;
139 }
140
141 template <typename T, IProxyLoader::VariableType T_variable_type>
142 class StdVectorKit : public IKit {
143 public:
144 virtual std::unique_ptr<IAccessor> createAccessor( const std::any &handle_key) const override {
145 const SG::ReadHandleKey<std::vector<T> > *key = std::any_cast<SG::ReadHandleKey<std::vector<T> > >(&handle_key);
146 assert( key );
147 return std::make_unique<GenAccessor<std::vector<T>,
149 VectorHelper> >(*key,
150 typename StdVectorHelper<T>::Kit(),
151 T_variable_type);
152 }
153 virtual bool registerReadKey(std::unordered_map<std::string, std::any > &read_keys,
155 const std::string &var_name,
156 std::vector<Gaudi::DataHandle *> &new_input_handles,
157 bool verbose) const override {
158 return PlainAccessorFactory::registerReadKey<std::vector<T> >(read_keys, parent, var_name, new_input_handles, verbose);
159 }
160 };
161 template <typename T, IProxyLoader::VariableType T_variable_type>
162 class PlainValueKit : public IKit {
163 public:
164 virtual std::unique_ptr<IAccessor> createAccessor( const std::any &handle_key) const override {
165 const SG::ReadHandleKey<T > *key = std::any_cast<SG::ReadHandleKey<T > >(&handle_key);
166 assert( key );
167 return std::make_unique<GenAccessor<T,
169 ScalarHelper> >(*key,
170 typename PlainValueHelper<T>::Kit(),
171 T_variable_type);
172 }
173 virtual bool registerReadKey(std::unordered_map<std::string, std::any > &read_keys,
175 const std::string &var_name,
176 std::vector<Gaudi::DataHandle *> &new_input_handles,
177 bool verbose) const override {
178 return PlainAccessorFactory::registerReadKey<T >(read_keys, parent, var_name, new_input_handles, verbose);
179 }
180 };
181
182 template <typename T,IProxyLoader::VariableType T_variable_type>
184 m_kits.insert( std::make_pair(typeid(std::vector<T>).hash_code(), std::make_unique< StdVectorKit<T,T_variable_type> >()));
185 m_kits.insert( std::make_pair(typeid(SG::ReadHandleKey<std::vector<T> >).hash_code(), std::make_unique< StdVectorKit<T,T_variable_type> >()));
186 }
187 template <typename T,IProxyLoader::VariableType T_variable_type>
189 m_kits.insert( std::make_pair(typeid(SG::ReadHandleKey<T>).hash_code(), std::make_unique<PlainValueKit<T,T_variable_type> >()));
190 m_kits.insert( std::make_pair(typeid(T).hash_code(), std::make_unique<PlainValueKit<T,T_variable_type> >()));
191 }
192
207 std::unique_ptr<IAccessor> createAccessor( const std::any &handle_key) const {
208 std::unordered_map<std::size_t,std::unique_ptr<IKit> >::const_iterator
209 kit_iter=m_kits.find(handle_key.type().hash_code());
210 if (kit_iter == m_kits.end()) {
211 std::stringstream msg;
212 msg << "ExpressionParsing::PlainAccessorFactory: no kit for type " << handle_key.type().name();
213 throw std::runtime_error(msg.str());
214 }
215 return kit_iter->second->createAccessor(handle_key);
216 }
217 bool registerReadKey(std::unordered_map<std::string, std::any > &read_keys,
218 const std::type_info &the_type,
220 const std::string &var_name,
221 std::vector<Gaudi::DataHandle *> &new_input_handles,
222 bool verbose=false) {
223 std::unordered_map<std::size_t,std::unique_ptr<IKit> >::const_iterator
224 kit_iter=m_kits.find(the_type.hash_code());
225 if (kit_iter == m_kits.end()) {
226 if (verbose) {
227 std::cout << "DEBUG PlainAccessorFactory::createReadKey no matching kit for " << the_type.name() << " " << the_type.hash_code() << std::endl;
228 for (const std::pair<const std::size_t,std::unique_ptr<IKit> > &elm : m_kits) {
229 std::cout << "DEBUG PlainAccessorFactory::createReadKey have " << elm.first << std::endl;
230 }
231 }
232 return false;
233 }
234 return kit_iter->second->registerReadKey(read_keys, parent, var_name, new_input_handles, verbose);
235 }
236 private:
237 std::unordered_map<std::size_t,std::unique_ptr<IKit> > m_kits;
238 };
239
240}
241#endif
Property holding a SG store/key/clid from which a ReadHandle is made.
Handle class for reading from StoreGate.
Generic accessor to access xAOD object content.
Definition GenAccessor.h:63
virtual bool registerReadKey(std::unordered_map< std::string, std::any > &read_keys, SGxAODProxyLoader::IParentHelper &parent, const std::string &var_name, std::vector< Gaudi::DataHandle * > &new_input_handles, bool verbose) const =0
virtual std::unique_ptr< IAccessor > createAccessor(const std::any &handle_key) const =0
virtual std::unique_ptr< IAccessor > createAccessor(const std::any &handle_key) const override
virtual bool registerReadKey(std::unordered_map< std::string, std::any > &read_keys, SGxAODProxyLoader::IParentHelper &parent, const std::string &var_name, std::vector< Gaudi::DataHandle * > &new_input_handles, bool verbose) const override
virtual std::unique_ptr< IAccessor > createAccessor(const std::any &handle_key) const override
virtual bool registerReadKey(std::unordered_map< std::string, std::any > &read_keys, SGxAODProxyLoader::IParentHelper &parent, const std::string &var_name, std::vector< Gaudi::DataHandle * > &new_input_handles, bool verbose) const override
static bool registerReadKey(std::unordered_map< std::string, std::any > &read_keys, SGxAODProxyLoader::IParentHelper &parent, const std::string &var_name, std::vector< Gaudi::DataHandle * > &new_input_handles, bool verbose)
static void throwFailedToAddHandle(const std::string &var_name)
bool registerReadKey(std::unordered_map< std::string, std::any > &read_keys, const std::type_info &the_type, SGxAODProxyLoader::IParentHelper &parent, const std::string &var_name, std::vector< Gaudi::DataHandle * > &new_input_handles, bool verbose=false)
std::unordered_map< std::size_t, std::unique_ptr< IKit > > m_kits
std::unique_ptr< IAccessor > createAccessor(const std::any &handle_key) const
Auxiliary class to create the corresponding auxiliary helper object.
PlainValueHelper< T_src > create(const EventContext &ctx, SG::ReadHandle< T_src > &handle) const
std::size_t size(const T_src &cont) const
T_src get(const T_src &cont) const
Get the scalar provided by the container.
T_src get(const T_src &cont, std::size_t idx) const
Get the specified element of the vector provided by the container.
Interface of an auxiliary class to pass the parent, e.g.
Auxiliary class to handle scalar like containers (AuxElement).
Definition GenAccessor.h:19
Auxiliary class to create the corresponding auxiliary helper object.
StdVectorHelper< T_src > create(const EventContext &ctx, SG::ReadHandle< std::vector< T_src > > &handle) const
Auxiliary class to handle method calls of "scalar" containers (AuxElement).
T_src get(const std::vector< T_src > &cont) const
Get the scalar provided by the container.
std::size_t size(const std::vector< T_src > &cont) const
T_src get(const std::vector< T_src > &cont, std::size_t idx) const
Get the specified element of the vector provided by the container.
Auxiliary class to handle vector like containers (AuxVectorBase).
Definition GenAccessor.h:36
bool verbose
Definition hcg.cxx:73
Namespace holding all the expression evaluation code.
void initializeHandle(T_Key *key, std::vector< Gaudi::DataHandle * > &new_input_handles)
Auxiliary function to initialize newly create data handles.
void dumpDeclare(const T *key)
Function for debugging which dumps information about newly declared data handles.
Definition DebugUtils.h:65
MsgStream & msg
Definition testRead.cxx:32