ATLAS Offline Software
xAODAccessor.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 _ExpressionEvaluation_xAODAccessor_h_
5 #define _ExpressionEvaluation_xAODAccessor_h_
6 
7 #include "GaudiKernel/DataHandle.h"
11 #include "StoreGate/ReadHandle.h"
15 
16 #include <memory>
17 #include <stdexcept>
18 #include <sstream>
19 
20 #include "Utils.h"
21 #include "GenAccessor.h"
22 
23 namespace ExpressionParsing {
26  template <class T_Cont,typename T_src>
28  public:
30  : m_accessor(SG::AuxTypeRegistry::instance().getName(auxid))
31  {}
32 
33  std::size_t size(const T_Cont &cont) const {
34  return getContainerSize(cont);
35  }
36 
38  T_src get(const T_Cont &cont) const {
39  return m_accessor( cont );
40  }
41 
43  T_src get(const T_Cont &cont, std::size_t idx) const {
44  return m_accessor( cont,idx );
45  }
46 
49  class Kit {
50  public:
51  Kit(Kit &&) = default;
52  Kit(SG::auxid_t auxid) : m_auxid(auxid) {}
53 
54  AccessorHelper<T_Cont,T_src> create(const EventContext& ctx, SG::ReadHandle<T_Cont> &handle) const {
55  (void) ctx;
56  (void) handle;
58  }
59  private:
61  };
62 
63  private:
65  };
66 
69  template <class T_Cont,typename T_src>
70  class DecorHelper {
71  public:
72  DecorHelper(const EventContext& ctx,const SG::ReadDecorHandleKey<T_Cont> &key,SG::ReadHandle<T_Cont> &handle)
73  : m_decorHandle(key, ctx),
74  m_data(getVectorData(*handle)->getDataArray(m_decorHandle.auxid()))
75  {
76  if (!m_decorHandle.isValid()) {
78  }
79  }
80 
81  std::size_t size(const T_Cont &cont) const {
82  return getContainerSize(cont);
83  }
84 
86  T_src get(const T_Cont &cont) const {
87  return m_decorHandle( cont );
88  }
89 
91  T_src get(const T_Cont &cont, std::size_t idx) const {
92  (void) cont;
94  }
95 
98  class Kit {
99  public:
100  Kit(Kit &&) = default;
102 
103  DecorHelper<T_Cont,T_src> create(const EventContext& ctx, SG::ReadHandle<T_Cont> &handle) const {
104  (void) handle;
105  try {
106  return DecorHelper<T_Cont,T_src>(ctx, *m_decorKey, handle);
107  }
108  catch (std::exception &err) {
109  dumpAux(*handle, 357);
110  throw err;
111  }
112  return DecorHelper<T_Cont,T_src>(ctx, *m_decorKey, handle);
113  }
114  private:
116  };
117 
118  private:
120  const void *m_data;
121  };
122 
123  class AccessorFactory;
124 
127  class IAccessorKit {
128  friend class AccessorFactory;
129  public:
130  virtual ~IAccessorKit() {}
131  virtual std::unique_ptr<IAccessor> create( const SG::ReadHandleKey<SG::AuxVectorBase> &key,
132  SG::auxid_t auxid,
133  const SG::ReadDecorHandleKey<SG::AuxVectorBase> *decor_key=nullptr) const = 0;
134  virtual std::unique_ptr<IAccessor> create( const SG::ReadHandleKey<SG::AuxElement> &key,
135  SG::auxid_t auxid,
136  const SG::ReadDecorHandleKey<SG::AuxElement> *decor_key=nullptr) const = 0;
137  };
138 
141  template <class T, IProxyLoader::VariableType T_variable_type>
142  class AccessorKit : public IAccessorKit {
143  virtual std::unique_ptr<IAccessor> create( const SG::ReadHandleKey<SG::AuxVectorBase> &key,
144  SG::auxid_t auxid,
145  const SG::ReadDecorHandleKey<SG::AuxVectorBase> *decor_key=nullptr) const override {
146  return createAccessor<SG::AuxVectorBase,VectorHelper>(key,auxid, decor_key);
147  }
148  virtual std::unique_ptr<IAccessor> create( const SG::ReadHandleKey<SG::AuxElement> &key,
149  SG::auxid_t auxid,
150  const SG::ReadDecorHandleKey<SG::AuxElement> *decor_key=nullptr) const override {
151  return createAccessor<SG::AuxElement,ScalarHelper>(key,auxid, decor_key);
152  }
153  private:
154  template <class T_Cont, class T_ScalarVectorHelper>
155  std::unique_ptr<IAccessor> createAccessor( const SG::ReadHandleKey<T_Cont> &key,
156  SG::auxid_t auxid,
157  const SG::ReadDecorHandleKey<T_Cont> *decor_key=nullptr) const {
158  if (decor_key) {
159  return std::make_unique<GenAccessor<T_Cont,
161  T_ScalarVectorHelper> >(key,
162  typename DecorHelper<T_Cont, T>::Kit(*decor_key),
163  T_variable_type);
164  }
165  else {
166  return std::make_unique<GenAccessor<T_Cont,
168  T_ScalarVectorHelper> >(key,
169  typename AccessorHelper<T_Cont,T>::Kit(auxid),
170  T_variable_type);
171  }
172  }
173  };
174 
175 
176 
180  // class AccessorFactory;
181  class AccessorFactory : public Singleton<AccessorFactory> {
182  public:
184  m_kits.insert( std::make_pair(typeid(float).hash_code(), std::make_unique<AccessorKit<float,IProxyLoader::VT_VECDOUBLE> >()));
185  m_kits.insert( std::make_pair(typeid(double).hash_code(), std::make_unique<AccessorKit<double,IProxyLoader::VT_VECDOUBLE> >()));
186  m_kits.insert( std::make_pair(typeid(bool).hash_code(), std::make_unique<AccessorKit<bool,IProxyLoader::VT_VECINT> >()));
187  m_kits.insert( std::make_pair(typeid(unsigned char).hash_code(), std::make_unique<AccessorKit<unsigned char,IProxyLoader::VT_VECINT> >()));
188  m_kits.insert( std::make_pair(typeid(unsigned short).hash_code(), std::make_unique<AccessorKit<unsigned short,IProxyLoader::VT_VECINT> >()));
189  m_kits.insert( std::make_pair(typeid(unsigned int).hash_code(), std::make_unique<AccessorKit<unsigned int,IProxyLoader::VT_VECINT> >()));
190  m_kits.insert( std::make_pair(typeid(unsigned long).hash_code(), std::make_unique<AccessorKit<unsigned long,IProxyLoader::VT_VECINT> >())); // @TODO might get cropped if int interface is used
191  m_kits.insert( std::make_pair(typeid(char).hash_code(), std::make_unique<AccessorKit<char,IProxyLoader::VT_VECINT> >()));
192  m_kits.insert( std::make_pair(typeid(short).hash_code(), std::make_unique<AccessorKit<short,IProxyLoader::VT_VECINT> >()));
193  m_kits.insert( std::make_pair(typeid(int).hash_code(), std::make_unique<AccessorKit<int,IProxyLoader::VT_VECINT> >()));
194  m_kits.insert( std::make_pair(typeid(long).hash_code(), std::make_unique<AccessorKit<long,IProxyLoader::VT_VECINT> >())); // @TODO might get cropped if int interface is used
195  }
196 
198  std::unique_ptr<IAccessor> create(const SG::ReadHandleKey<SG::AuxElement> &key,
199  SG::auxid_t auxid,
200  const SG::ReadDecorHandleKey<SG::AuxElement> *decor_key=nullptr) const {
201  return getKit(auxid).create(key,auxid, decor_key);
202  }
204  std::unique_ptr<IAccessor> create(const SG::ReadHandleKey<SG::AuxVectorBase> &key,
205  SG::auxid_t auxid,
206  const SG::ReadDecorHandleKey<SG::AuxVectorBase> *decor_key=nullptr) const {
207  return getKit(auxid).create(key,auxid, decor_key);
208  }
209 
210  private:
211 
214  const IAccessorKit &getKit(SG::auxid_t auxid) const {
215  const std::type_info* the_type_info = SG::AuxTypeRegistry::instance().getType (auxid);
216  if (!the_type_info) {
217  std::stringstream msg;
218  msg << "ExpressionParsing::AccessorFactory: no type information about " << auxid
219  << " (" << SG::AuxTypeRegistry::instance().getName(auxid) << ")";
220  throw std::runtime_error(msg.str());
221  }
222  std::map<std::size_t, std::unique_ptr<IAccessorKit> >::const_iterator
223  iter = m_kits.find(the_type_info->hash_code());
224  if (iter == m_kits.end()) {
225  std::stringstream msg;
226  msg << "ExpressionParsing::AccessorFactory: no type information about " << auxid
227  << " (" << SG::AuxTypeRegistry::instance().getName(auxid) << ")";
228  throw std::runtime_error(msg.str());
229  }
230  return *(iter->second);
231  }
232  std::map<std::size_t, std::unique_ptr<IAccessorKit> > m_kits;
233  };
234 
235 }
236 #endif
ReadHandleKey.h
Property holding a SG store/key/clid from which a ReadHandle is made.
ExpressionParsing::DecorHelper
Auxiliary class to handle decorator handle based xAOD object content access.
Definition: xAODAccessor.h:70
ExpressionParsing::AccessorKit::createAccessor
std::unique_ptr< IAccessor > createAccessor(const SG::ReadHandleKey< T_Cont > &key, SG::auxid_t auxid, const SG::ReadDecorHandleKey< T_Cont > *decor_key=nullptr) const
Definition: xAODAccessor.h:155
ExpressionParsing::IAccessorKit::create
virtual std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxVectorBase > &key, SG::auxid_t auxid, const SG::ReadDecorHandleKey< SG::AuxVectorBase > *decor_key=nullptr) const =0
ReadDecorHandleKey.h
Property holding a SG store/key/clid/attr name from which a ReadDecorHandle is made.
ExpressionParsing::AccessorFactory::AccessorFactory
AccessorFactory()
Definition: xAODAccessor.h:183
ExpressionParsing::DecorHelper::size
std::size_t size(const T_Cont &cont) const
Definition: xAODAccessor.h:81
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
SG::AuxDataTraits::index
static reference_type index(void *ptr, size_t ndx)
Look up an element in the container by index.
Definition: AuxDataTraits.h:62
ExpressionParsing::AccessorHelper::Kit::Kit
Kit(SG::auxid_t auxid)
Definition: xAODAccessor.h:52
ExpressionParsing::AccessorHelper::get
T_src get(const T_Cont &cont) const
Get the scalar provided by the container.
Definition: xAODAccessor.h:38
SG::AuxTypeRegistry::instance
static AuxTypeRegistry & instance()
Return the singleton registry instance.
Definition: AuxTypeRegistry.cxx:640
ExpressionParsing::AccessorFactory::m_kits
std::map< std::size_t, std::unique_ptr< IAccessorKit > > m_kits
Definition: xAODAccessor.h:232
make_unique
std::unique_ptr< T > make_unique(Args &&... args)
Definition: SkimmingToolEXOT5.cxx:23
SG::ReadHandle< T_Cont >
ExpressionParsing::IAccessorKit::~IAccessorKit
virtual ~IAccessorKit()
Definition: xAODAccessor.h:130
SG::AuxTypeRegistry::getName
std::string getName(SG::auxid_t auxid) const
Return the name of an aux data item.
Definition: AuxTypeRegistry.cxx:882
ExpressionParsing::GenAccessor
Generic accessor to access xAOD object content.
Definition: GenAccessor.h:63
ExpressionParsing::DecorHelper::get
T_src get(const T_Cont &cont) const
Get the scalar provided by the container.
Definition: xAODAccessor.h:86
ExpressionParsing::AccessorFactory::create
std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxVectorBase > &key, SG::auxid_t auxid, const SG::ReadDecorHandleKey< SG::AuxVectorBase > *decor_key=nullptr) const
create an accessor for the specified content of an AuxVectorBase.
Definition: xAODAccessor.h:204
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:55
ExpressionParsing::AccessorFactory::create
std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxElement > &key, SG::auxid_t auxid, const SG::ReadDecorHandleKey< SG::AuxElement > *decor_key=nullptr) const
create an accessor for the specified content of an AuxElement.
Definition: xAODAccessor.h:198
SG::ReadHandleKey
Property holding a SG store/key/clid from which a ReadHandle is made.
Definition: StoreGate/StoreGate/ReadHandleKey.h:39
AuxVectorBase.h
Manage index tracking and synchronization of auxiliary data.
ExpressionParsing::AccessorKit::create
virtual std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxElement > &key, SG::auxid_t auxid, const SG::ReadDecorHandleKey< SG::AuxElement > *decor_key=nullptr) const override
Definition: xAODAccessor.h:148
ExpressionParsing::AccessorHelper::Kit::Kit
Kit(Kit &&)=default
dumpTruth.getName
getName
Definition: dumpTruth.py:34
ExpressionParsing::AccessorHelper::AccessorHelper
AccessorHelper(SG::auxid_t auxid)
Definition: xAODAccessor.h:29
ExpressionParsing::IAccessorKit
Interface of a class to create an xAOD object content accessor.
Definition: xAODAccessor.h:127
instance
std::map< std::string, double > instance
Definition: Run_To_Get_Tags.h:8
ExpressionParsing::AccessorHelper::Kit
Auxiliary class to create the corresponding auxiliary helper object.
Definition: xAODAccessor.h:49
SG::auxid_t
size_t auxid_t
Identifier for a particular aux data item.
Definition: AuxTypes.h:27
SG::AuxTypeRegistry::getType
const std::type_info * getType(SG::auxid_t auxid) const
Return the type of an aux data item.
Definition: AuxTypeRegistry.cxx:909
GenAccessor.h
ExpressionParsing::DecorHelper::DecorHelper
DecorHelper(const EventContext &ctx, const SG::ReadDecorHandleKey< T_Cont > &key, SG::ReadHandle< T_Cont > &handle)
Definition: xAODAccessor.h:72
SG::ReadDecorHandle< T_Cont, T_src >
IAccessor.h
dqt_zlumi_pandas.err
err
Definition: dqt_zlumi_pandas.py:182
ExpressionParsing::DecorHelper::m_decorHandle
SG::ReadDecorHandle< T_Cont, T_src > m_decorHandle
Definition: xAODAccessor.h:119
ExpressionParsing::AccessorHelper::get
T_src get(const T_Cont &cont, std::size_t idx) const
Get the specified element of the vector provided by the container.
Definition: xAODAccessor.h:43
Utils.h
calibdata.exception
exception
Definition: calibdata.py:496
ExpressionParsing
Namespace holding all the expression evaluation code.
Definition: ExpressionParser.h:26
ExpressionParsing::IAccessorKit::create
virtual std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxElement > &key, SG::auxid_t auxid, const SG::ReadDecorHandleKey< SG::AuxElement > *decor_key=nullptr) const =0
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
ExpressionParsing::BaseAccessor::throwInvalidHandle
static void throwInvalidHandle(const std::string &key)
Definition: BaseAccessor.h:20
ExpressionParsing::Singleton
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:88
ExpressionParsing::AccessorKit::create
virtual std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxVectorBase > &key, SG::auxid_t auxid, const SG::ReadDecorHandleKey< SG::AuxVectorBase > *decor_key=nullptr) const override
Definition: xAODAccessor.h:143
ExpressionParsing::dumpAux
void dumpAux(const T &cont, SG::auxid_t method_id)
Method for debugging to dump information of all aux member of the given container and the specific au...
Definition: DebugUtils.h:55
ExpressionParsing::AccessorHelper::size
std::size_t size(const T_Cont &cont) const
Definition: xAODAccessor.h:33
ExpressionParsing::DecorHelper::Kit::Kit
Kit(const SG::ReadDecorHandleKey< T_Cont > &key)
Definition: xAODAccessor.h:101
ExpressionParsing::DecorHelper::get
T_src get(const T_Cont &cont, std::size_t idx) const
Get the specified element of the vector provided by the container.
Definition: xAODAccessor.h:91
ExpressionParsing::getContainerSize
std::size_t getContainerSize(const T &cont)
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:73
ExpressionParsing::AccessorKit
Implementation of an accessor creator which provides accessors for xAOD accessor or decor handle acce...
Definition: xAODAccessor.h:142
ExpressionParsing::AccessorHelper::Kit::create
AccessorHelper< T_Cont, T_src > create(const EventContext &ctx, SG::ReadHandle< T_Cont > &handle) const
Definition: xAODAccessor.h:54
ExpressionParsing::getVectorData
const SG::AuxVectorData * getVectorData(const T &cont)
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:78
ExpressionParsing::DecorHelper::Kit::m_decorKey
const SG::ReadDecorHandleKey< T_Cont > * m_decorKey
Definition: xAODAccessor.h:115
ExpressionParsing::DecorHelper::m_data
const void * m_data
Definition: xAODAccessor.h:120
ExpressionParsing::DecorHelper::Kit
Auxiliary class to create the corresponding auxiliary helper object.
Definition: xAODAccessor.h:98
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
ReadDecorHandle.h
Handle class for reading a decoration on an object.
ReadHandle.h
Handle class for reading from StoreGate.
SG::ReadDecorHandleKey< T_Cont >
ExpressionParsing::AccessorHelper::m_accessor
SG::AuxElement::ConstAccessor< T_src > m_accessor
Definition: xAODAccessor.h:64
ExpressionParsing::AccessorHelper::Kit::m_auxid
SG::auxid_t m_auxid
Definition: xAODAccessor.h:60
ExpressionParsing::DecorHelper::Kit::Kit
Kit(Kit &&)=default
ExpressionParsing::DecorHelper::Kit::create
DecorHelper< T_Cont, T_src > create(const EventContext &ctx, SG::ReadHandle< T_Cont > &handle) const
Definition: xAODAccessor.h:103
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
ExpressionParsing::AccessorFactory
Class which creates accessors for the specified xAOD object content (singleton).
Definition: xAODAccessor.h:181
AuxElement.h
Base class for elements of a container that can have aux data.
ExpressionParsing::AccessorFactory::getKit
const IAccessorKit & getKit(SG::auxid_t auxid) const
Get an auxiliary class which creates an accessor for the specified content.
Definition: xAODAccessor.h:214
ExpressionParsing::AccessorHelper
Auxiliary class to handle xAOD accessor based xAOD object content access.
Definition: xAODAccessor.h:27
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37