ATLAS Offline Software
PlainAccessor.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 #ifndef ExpressionParsing_PlainAccessor_h_
5 #define ExpressionParsing_PlainAccessor_h_
6 
7 #include "StoreGate/ReadHandle.h"
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 
22 namespace 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;
52  return StdVectorHelper<T_src>();
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;
86  return PlainValueHelper<T_src>();
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) {
118  std::pair<std::unordered_map<std::string, std::any >::iterator, bool> ret = read_keys.insert(std::make_pair(var_name, std::move(key)));
119  if (!ret.second) {
120  if (ret.first != read_keys.end()
121  && ret.first->first == var_name
122  && ret.first->second.type().hash_code() == typeid(key).hash_code()) {
123  return true;
124  }
126  }
127  SG::ReadHandleKey< T_Cont > *key_final( std::any_cast<SG::ReadHandleKey< T_Cont > >( &ret.first->second));
128  if (!key_final) {
130  }
131  if (parent.declare(*key_final).isFailure()) {
133  }
134  if (verbose) dumpDeclare(key_final);
135  initializeHandle(key_final, new_input_handles);
136  if (verbose) {
137  std::cout << "DEBUG PlainAccessorFactory::registerReadKey register read handle key for " << var_name << " [" << typeid(T_Cont).name() << "]."<< std::endl;
138  }
139  return true;
140  }
141 
142  template <typename T, IProxyLoader::VariableType T_variable_type>
143  class StdVectorKit : public IKit {
144  public:
145  virtual std::unique_ptr<IAccessor> createAccessor( const std::any &handle_key) const override {
146  const SG::ReadHandleKey<std::vector<T> > *key = std::any_cast<SG::ReadHandleKey<std::vector<T> > >(&handle_key);
147  assert( key );
148  return std::make_unique<GenAccessor<std::vector<T>,
149  typename StdVectorHelper<T>::Kit,
150  VectorHelper> >(*key,
151  typename StdVectorHelper<T>::Kit(),
152  T_variable_type);
153  }
154  virtual bool registerReadKey(std::unordered_map<std::string, std::any > &read_keys,
156  const std::string &var_name,
157  std::vector<Gaudi::DataHandle *> &new_input_handles,
158  bool verbose) const override {
159  return PlainAccessorFactory::registerReadKey<std::vector<T> >(read_keys, parent, var_name, new_input_handles, verbose);
160  }
161  };
162  template <typename T, IProxyLoader::VariableType T_variable_type>
163  class PlainValueKit : public IKit {
164  public:
165  virtual std::unique_ptr<IAccessor> createAccessor( const std::any &handle_key) const override {
166  const SG::ReadHandleKey<T > *key = std::any_cast<SG::ReadHandleKey<T > >(&handle_key);
167  assert( key );
168  return std::make_unique<GenAccessor<T,
169  typename PlainValueHelper<T>::Kit,
170  ScalarHelper> >(*key,
171  typename PlainValueHelper<T>::Kit(),
172  T_variable_type);
173  }
174  virtual bool registerReadKey(std::unordered_map<std::string, std::any > &read_keys,
176  const std::string &var_name,
177  std::vector<Gaudi::DataHandle *> &new_input_handles,
178  bool verbose) const override {
179  return PlainAccessorFactory::registerReadKey<T >(read_keys, parent, var_name, new_input_handles, verbose);
180  }
181  };
182 
183  template <typename T,IProxyLoader::VariableType T_variable_type>
185  m_kits.insert( std::make_pair(typeid(std::vector<T>).hash_code(), std::make_unique< StdVectorKit<T,T_variable_type> >()));
186  m_kits.insert( std::make_pair(typeid(SG::ReadHandleKey<std::vector<T> >).hash_code(), std::make_unique< StdVectorKit<T,T_variable_type> >()));
187  }
188  template <typename T,IProxyLoader::VariableType T_variable_type>
190  m_kits.insert( std::make_pair(typeid(SG::ReadHandleKey<T>).hash_code(), std::make_unique<PlainValueKit<T,T_variable_type> >()));
191  m_kits.insert( std::make_pair(typeid(T).hash_code(), std::make_unique<PlainValueKit<T,T_variable_type> >()));
192  }
193 
195  // registerPlainValueKit<bool,IProxyLoader::VT_INT>();
196  registerPlainValueKit<bool,IProxyLoader::VT_INT>();
197  registerPlainValueKit<int,IProxyLoader::VT_INT>();
198  registerPlainValueKit<unsigned int,IProxyLoader::VT_INT>();
199  registerPlainValueKit<float,IProxyLoader::VT_DOUBLE>();
200  registerPlainValueKit<double,IProxyLoader::VT_DOUBLE>();
201 
202  registerStdVectorKit<bool,IProxyLoader::VT_VECINT>();
203  registerStdVectorKit<int,IProxyLoader::VT_VECINT>();
204  registerStdVectorKit<unsigned int,IProxyLoader::VT_VECINT>();
205  registerStdVectorKit<float,IProxyLoader::VT_VECDOUBLE>();
206  registerStdVectorKit<double,IProxyLoader::VT_VECDOUBLE>();
207  }
208  std::unique_ptr<IAccessor> createAccessor( const std::any &handle_key) const {
209  std::unordered_map<std::size_t,std::unique_ptr<IKit> >::const_iterator
210  kit_iter=m_kits.find(handle_key.type().hash_code());
211  if (kit_iter == m_kits.end()) {
212  std::stringstream msg;
213  msg << "ExpressionParsing::PlainAccessorFactory: no kit for type " << handle_key.type().name();
214  throw std::runtime_error(msg.str());
215  }
216  return kit_iter->second->createAccessor(handle_key);
217  }
218  bool registerReadKey(std::unordered_map<std::string, std::any > &read_keys,
219  const std::type_info &the_type,
221  const std::string &var_name,
222  std::vector<Gaudi::DataHandle *> &new_input_handles,
223  bool verbose=false) {
224  std::unordered_map<std::size_t,std::unique_ptr<IKit> >::const_iterator
225  kit_iter=m_kits.find(the_type.hash_code());
226  if (kit_iter == m_kits.end()) {
227  if (verbose) {
228  std::cout << "DEBUG PlainAccessorFactory::createReadKey no matching kit for " << the_type.name() << " " << the_type.hash_code() << std::endl;
229  for (const std::pair<const std::size_t,std::unique_ptr<IKit> > &elm : m_kits) {
230  std::cout << "DEBUG PlainAccessorFactory::createReadKey have " << elm.first << std::endl;
231  }
232  }
233  return false;
234  }
235  return kit_iter->second->registerReadKey(read_keys, parent, var_name, new_input_handles, verbose);
236  }
237  private:
238  std::unordered_map<std::size_t,std::unique_ptr<IKit> > m_kits;
239  };
240 
241 }
242 #endif
ExpressionParsing::PlainValueHelper::Kit
Auxiliary class to create the corresponding auxiliary helper object.
Definition: PlainAccessor.h:81
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
ReadHandleKey.h
Property holding a SG store/key/clid from which a ReadHandle is made.
ExpressionParsing::PlainAccessorFactory::registerReadKey
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)
Definition: PlainAccessor.h:112
ExpressionParsing::VectorHelper
Auxiliary class to handle vector like containers (AuxVectorBase).
Definition: GenAccessor.h:36
ExpressionParsing::PlainAccessorFactory::registerPlainValueKit
void registerPlainValueKit()
Definition: PlainAccessor.h:189
ExpressionParsing::PlainAccessorFactory::IKit
Definition: PlainAccessor.h:93
ExpressionParsing::PlainAccessorFactory::PlainValueKit
Definition: PlainAccessor.h:163
ExpressionParsing::SGxAODProxyLoader::IParentHelper
Interface of an auxiliary class to pass the parent, e.g.
Definition: SGxAODProxyLoader.h:64
make_unique
std::unique_ptr< T > make_unique(Args &&... args)
Definition: SkimmingToolEXOT5.cxx:23
CheckAppliedSFs.var_name
var_name
Definition: CheckAppliedSFs.py:241
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
ExpressionParsing::StdVectorHelper::size
std::size_t size(const std::vector< T_src > &cont) const
Definition: PlainAccessor.h:28
ExpressionParsing::GenAccessor
Generic accessor to access xAOD object content.
Definition: GenAccessor.h:63
ExpressionParsing::PlainAccessorFactory
Definition: PlainAccessor.h:91
ExpressionParsing::PlainAccessorFactory::StdVectorKit::registerReadKey
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
Definition: PlainAccessor.h:154
ExpressionParsing::PlainAccessorFactory::StdVectorKit::createAccessor
virtual std::unique_ptr< IAccessor > createAccessor(const std::any &handle_key) const override
Definition: PlainAccessor.h:145
ExpressionParsing::PlainAccessorFactory::registerReadKey
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)
Definition: PlainAccessor.h:218
SG::ReadHandleKey< T_Cont >
ExpressionParsing::PlainValueHelper::get
T_src get(const T_src &cont) const
Get the scalar provided by the container.
Definition: PlainAccessor.h:66
ExpressionParsing::PlainAccessorFactory::PlainAccessorFactory
PlainAccessorFactory()
Definition: PlainAccessor.h:194
ExpressionParsing::StdVectorHelper::get
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.
Definition: PlainAccessor.h:39
ExpressionParsing::StdVectorHelper::Kit
Auxiliary class to create the corresponding auxiliary helper object.
Definition: PlainAccessor.h:47
ExpressionParsing::PlainAccessorFactory::IKit::registerReadKey
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
GenAccessor.h
IAccessor.h
ExpressionParsing::PlainAccessorFactory::registerStdVectorKit
void registerStdVectorKit()
Definition: PlainAccessor.h:184
Utils.h
ExpressionParsing::PlainValueHelper::size
std::size_t size(const T_src &cont) const
Definition: PlainAccessor.h:60
ExpressionParsing
Namespace holding all the expression evaluation code.
Definition: ExpressionParser.h:26
test_pyathena.parent
parent
Definition: test_pyathena.py:15
ExpressionParsing::StdVectorHelper::get
T_src get(const std::vector< T_src > &cont) const
Get the scalar provided by the container.
Definition: PlainAccessor.h:33
ExpressionParsing::PlainValueHelper
Definition: PlainAccessor.h:58
ExpressionParsing::PlainAccessorFactory::createAccessor
std::unique_ptr< IAccessor > createAccessor(const std::any &handle_key) const
Definition: PlainAccessor.h:208
ExpressionParsing::Singleton
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:88
ExpressionParsing::PlainAccessorFactory::IKit::~IKit
virtual ~IKit()
Definition: PlainAccessor.h:95
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
ExpressionParsing::PlainAccessorFactory::m_kits
std::unordered_map< std::size_t, std::unique_ptr< IKit > > m_kits
Definition: PlainAccessor.h:238
ExpressionParsing::PlainAccessorFactory::throwFailedToAddHandle
static void throwFailedToAddHandle(const std::string &var_name)
Definition: PlainAccessor.h:105
ExpressionParsing::StdVectorHelper::Kit::create
StdVectorHelper< T_src > create(const EventContext &ctx, SG::ReadHandle< std::vector< T_src > > &handle) const
Definition: PlainAccessor.h:49
ExpressionParsing::PlainAccessorFactory::IKit::createAccessor
virtual std::unique_ptr< IAccessor > createAccessor(const std::any &handle_key) const =0
ExpressionParsing::dumpDeclare
void dumpDeclare(const T *key)
Function for debugging which dumps information about newly declared data handles.
Definition: DebugUtils.h:65
python.TriggerHandler.verbose
verbose
Definition: TriggerHandler.py:297
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
ExpressionParsing::PlainAccessorFactory::StdVectorKit
Definition: PlainAccessor.h:143
ExpressionParsing::PlainAccessorFactory::PlainValueKit::createAccessor
virtual std::unique_ptr< IAccessor > createAccessor(const std::any &handle_key) const override
Definition: PlainAccessor.h:165
ReadHandle.h
Handle class for reading from StoreGate.
ExpressionParsing::PlainValueHelper::Kit::create
PlainValueHelper< T_src > create(const EventContext &ctx, SG::ReadHandle< T_src > &handle) const
Definition: PlainAccessor.h:83
ExpressionParsing::PlainAccessorFactory::PlainValueKit::registerReadKey
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
Definition: PlainAccessor.h:174
ExpressionParsing::StdVectorHelper
Auxiliary class to handle method calls of "scalar" containers (AuxElement).
Definition: PlainAccessor.h:26
ExpressionParsing::PlainValueHelper::get
T_src get(const T_src &cont, std::size_t idx) const
Get the specified element of the vector provided by the container.
Definition: PlainAccessor.h:72
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
ExpressionParsing::initializeHandle
void initializeHandle(T_Key *key, std::vector< Gaudi::DataHandle * > &new_input_handles)
Auxiliary function to initialize newly create data handles.
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:17
ExpressionParsing::ScalarHelper
Auxiliary class to handle scalar like containers (AuxElement).
Definition: GenAccessor.h:19
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37