ATLAS Offline Software
MethodAccessor.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 #ifndef _ExpressionEvaluation_MethodAccessor_h_
5 #define _ExpressionEvaluation_MethodAccessor_h_
6 
8 #include "StoreGate/ReadHandle.h"
11 
12 #include "TClass.h"
15 #include "TMethodCall.h"
16 #include "TVirtualCollectionProxy.h"
17 #include "TFunction.h"
18 
19 #include <memory>
20 #include <map>
21 #include <stdexcept>
22 #include <sstream>
23 #include <cassert>
24 
25 namespace ExpressionParsing {
26 
29  template <class T_Cont,typename T_src>
31  public:
32  CollectionMethodHelper(const RootUtils::TSMethodCall& method_call, const TVirtualCollectionProxy &collection_proxy, const void *data, [[maybe_unused]] unsigned int n_elements)
33  : m_methodCall( method_call),
34  m_collectionProxy( collection_proxy.Generate())
35  {
36  // cppcheck-suppress assertWithSideEffect
37  assert( m_methodCall.call() != nullptr );
38  void* data_nc ATLAS_THREAD_SAFE = const_cast<void *>(data); // required by TVirtualCollectionProxy
39  m_collectionProxy->PushProxy(data_nc);
40  assert( m_collectionProxy->Size() == n_elements);
41  }
42 
43  std::size_t size(const T_Cont &/*cont*/) const {
44  return m_collectionProxy->Size();
45  }
46 
48  T_src get(const T_Cont &/*cont*/) const {
49  T_src ret;
50  assert( m_collectionProxy->Size() == 1);
51  m_methodCall.call()->Execute((*m_collectionProxy)[0], ret);
52  return ret;
53  }
54 
56  T_src get(const T_Cont &/*cont*/, std::size_t idx) const {
57  T_src ret;
58  void *element_data=(*m_collectionProxy)[idx];
59  m_methodCall.call()->Execute(element_data, ret);
60  return ret;
61  }
62 
65  class Kit {
66  public:
67  Kit(Kit &&) = default;
68  Kit(RootUtils::TSMethodCall &&method_call, TVirtualCollectionProxy &collection_proxy)
69  : m_methodCall(std::move(method_call)),
70  m_collectionProxy(&collection_proxy)
71  {}
72 
73  CollectionMethodHelper<T_Cont,T_src> create(const EventContext& /*ctx*/,
74  SG::ReadHandle<T_Cont> &handle) const
75  {
77  }
78  private:
80  const TVirtualCollectionProxy *m_collectionProxy;
81  };
82 
83  private:
85  std::unique_ptr<TVirtualCollectionProxy> m_collectionProxy;
86  };
87 
90  template <class T_Cont,typename T_src>
91  class MethodHelper {
92  public:
93  MethodHelper(const RootUtils::TSMethodCall& method_call, const void *data)
94  : m_methodCall( method_call),
95  m_data(data)
96  {
97  // cppcheck-suppress assertWithSideEffect
98  assert( m_methodCall.call() != nullptr );
99  }
100 
101  std::size_t size(const T_Cont &cont) const {
102  return getContainerSize(cont); //@TODO correct ? or 1 ?
103  }
104 
106  T_src get(const T_Cont &/*cont*/) const {
107  T_src ret;
108  void* data_nc ATLAS_THREAD_SAFE = const_cast<void *>(m_data); // required by TMethodCall
109  m_methodCall.call()->Execute(data_nc, ret);
110  return ret;
111  }
112 
114  T_src get(const T_Cont &/*cont*/, [[maybe_unused]] std::size_t idx) const {
115  T_src ret;
116  assert( idx==0);
117  void* data_nc ATLAS_THREAD_SAFE = const_cast<void *>(m_data); // required by TMethodCall
118  m_methodCall.call()->Execute(data_nc, ret);
119  return ret;
120  }
121 
124  class Kit {
125  public:
126  Kit(Kit &&) = default;
127  Kit(RootUtils::TSMethodCall &&method_call) : m_methodCall(std::move(method_call)) {}
128 
129  MethodHelper<T_Cont,T_src> create(const EventContext& /*ctx*/, SG::ReadHandle<T_Cont> &handle) const {
130  return MethodHelper<T_Cont,T_src>(m_methodCall,handle.cptr());
131  }
132  private:
134  };
135  private:
137  const void *m_data;
138  };
139 
142  class MethodAccessorFactory : public Singleton<MethodAccessorFactory> {
143  private:
145  friend class MethodAccessorFactory;
146  public:
147  virtual ~IMethodAccessorKit() {}
148  virtual std::unique_ptr<IAccessor> create( const SG::ReadHandleKey<SG::AuxVectorBase> &key,
149  RootUtils::TSMethodCall &&method_call,
150  TVirtualCollectionProxy *proxy=nullptr) const = 0;
151  virtual std::unique_ptr<IAccessor> create( const SG::ReadHandleKey<SG::AuxElement> &key,
152  RootUtils::TSMethodCall &&method_call,
153  TVirtualCollectionProxy *proxy=nullptr) const = 0;
154  };
155 
158  template <class T_src>
160  public:
163  : m_scalarType(scalar_type),
164  m_vectorType(vector_type)
165  {}
166 
168  virtual std::unique_ptr<IAccessor> create( const SG::ReadHandleKey<SG::AuxVectorBase> &key,
169  RootUtils::TSMethodCall &&method_call,
170  TVirtualCollectionProxy *proxy) const override {
171  if (!proxy) {
172  std::stringstream msg;
173  msg << "Cannot use method access of types SG::AuxVectorBase without a collection proxy.";
174  throw std::logic_error(msg.str());
175  }
176  return createAccessor<SG::AuxVectorBase, VectorHelper>(key,std::move(method_call),proxy,m_vectorType);
177  }
178 
180  virtual std::unique_ptr<IAccessor> create( const SG::ReadHandleKey<SG::AuxElement> &key,
181  RootUtils::TSMethodCall &&method_call,
182  TVirtualCollectionProxy *proxy=nullptr) const override {
183  if (proxy) {
184  return createAccessor<SG::AuxElement,VectorHelper>(key,std::move(method_call),proxy, (!proxy ? m_scalarType : m_vectorType));
185  }
186  else {
187  return createAccessor<SG::AuxElement,ScalarHelper>(key,std::move(method_call),proxy, (!proxy ? m_scalarType : m_vectorType));
188  }
189  // @TODO really vectorType and GenScalarAccessor if collection proxy is given ?
190  }
191  private:
192  template <class T_Aux, class T_ScalarVectorHelper>
193  std::unique_ptr<IAccessor> createAccessor( const SG::ReadHandleKey<T_Aux> &key,
194  RootUtils::TSMethodCall &&method_call,
195  TVirtualCollectionProxy *proxy,
196  ExpressionParsing::IProxyLoader::VariableType variable_type) const {
197  if (proxy) {
198  return std::make_unique<GenAccessor<T_Aux,
200  T_ScalarVectorHelper> >(key,
201  typename CollectionMethodHelper<T_Aux,T_src>::Kit(std::move(method_call), *proxy),
202  variable_type);
203  }
204  else {
205  return std::make_unique<GenAccessor<T_Aux,
207  T_ScalarVectorHelper> >(key,
208  typename MethodHelper<T_Aux,T_src>::Kit(std::move(method_call)),
209  variable_type);
210  }
211  }
214  };
215 
216  public:
218  m_kits.insert( std::make_pair(TInterpreter::EReturnType::kLong,std::make_unique<MethodAccessorKit<long> >(IProxyLoader::VT_INT,IProxyLoader::VT_VECINT)));
219  m_kits.insert( std::make_pair(TInterpreter::EReturnType::kDouble,std::make_unique<MethodAccessorKit<double> >(IProxyLoader::VT_DOUBLE, IProxyLoader::VT_VECDOUBLE)));
220  }
221 
224  std::unique_ptr<IAccessor> create(const SG::ReadHandleKey<SG::AuxElement> &key,
225  RootUtils::TSMethodCall &&method_call,
226  TVirtualCollectionProxy *proxy=nullptr) const {
227  return getKit(method_call).create(key,std::move(method_call), proxy);
228  }
229 
232  std::unique_ptr<IAccessor> create(const SG::ReadHandleKey<SG::AuxVectorBase> &key,
233  RootUtils::TSMethodCall &&method_call,
234  TVirtualCollectionProxy *proxy=nullptr) const {
235  return getKit(method_call).create(key,std::move(method_call), proxy);
236  }
237 
238  private:
239 
242  const IMethodAccessorKit &getKit(RootUtils::TSMethodCall &method_call) const {
243  std::map<TMethodCall::EReturnType, std::unique_ptr<IMethodAccessorKit> >::const_iterator
244  iter = m_kits.find(method_call.call()->ReturnType());
245  if (iter == m_kits.end()) {
246  std::stringstream msg;
247  msg << "ExpressionParsing::MethodAccessorFactory: no kit for return type " << method_call.call()->GetMethod()->GetReturnTypeNormalizedName ();
248  throw std::runtime_error(msg.str());
249  }
250  return *(iter->second);
251  }
252  std::map<TMethodCall::EReturnType, std::unique_ptr<IMethodAccessorKit> > m_kits;
253  };
254 
255 }
256 #endif
ReadHandleKey.h
Property holding a SG store/key/clid from which a ReadHandle is made.
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
ExpressionParsing::MethodHelper::Kit::ATLAS_THREAD_SAFE
RootUtils::TSMethodCall m_methodCall ATLAS_THREAD_SAFE
Definition: MethodAccessor.h:133
StateLessPT_NewConfig.proxy
proxy
Definition: StateLessPT_NewConfig.py:392
SG::ReadHandle::cptr
const_pointer_type cptr()
Dereference the pointer.
make_unique
std::unique_ptr< T > make_unique(Args &&... args)
Definition: SkimmingToolEXOT5.cxx:23
ExpressionParsing::MethodHelper::size
std::size_t size(const T_Cont &cont) const
Definition: MethodAccessor.h:101
SG::ReadHandle< T_Cont >
ExpressionParsing::CollectionMethodHelper::size
std::size_t size(const T_Cont &) const
Definition: MethodAccessor.h:43
ExpressionParsing::MethodAccessorFactory::MethodAccessorKit::m_vectorType
ExpressionParsing::IProxyLoader::VariableType m_vectorType
Definition: MethodAccessor.h:213
ExpressionParsing::MethodAccessorFactory::getKit
const IMethodAccessorKit & getKit(RootUtils::TSMethodCall &method_call) const
Get an specific class which creates the accessor for the given method.
Definition: MethodAccessor.h:242
ExpressionParsing::IProxyLoader::VT_VECDOUBLE
@ VT_VECDOUBLE
Definition: IProxyLoader.h:21
ExpressionParsing::MethodHelper::Kit::create
MethodHelper< T_Cont, T_src > create(const EventContext &, SG::ReadHandle< T_Cont > &handle) const
Definition: MethodAccessor.h:129
ExpressionParsing::MethodAccessorFactory
Class to create accessor which call methods of an AuxElement of an AuxVectorBase container (singleton...
Definition: MethodAccessor.h:142
ExpressionParsing::GenAccessor
Generic accessor to access xAOD object content.
Definition: GenAccessor.h:63
ExpressionParsing::MethodAccessorFactory::IMethodAccessorKit
Definition: MethodAccessor.h:144
ExpressionParsing::CollectionMethodHelper::Kit::create
CollectionMethodHelper< T_Cont, T_src > create(const EventContext &, SG::ReadHandle< T_Cont > &handle) const
Definition: MethodAccessor.h:73
ExpressionParsing::CollectionMethodHelper::m_collectionProxy
std::unique_ptr< TVirtualCollectionProxy > m_collectionProxy
Definition: MethodAccessor.h:85
ExpressionParsing::MethodHelper::m_data
const void * m_data
Definition: MethodAccessor.h:137
SG::ReadHandleKey
Property holding a SG store/key/clid from which a ReadHandle is made.
Definition: StoreGate/StoreGate/ReadHandleKey.h:39
ExpressionParsing::CollectionMethodHelper::CollectionMethodHelper
CollectionMethodHelper(const RootUtils::TSMethodCall &method_call, const TVirtualCollectionProxy &collection_proxy, const void *data, [[maybe_unused]] unsigned int n_elements)
Definition: MethodAccessor.h:32
ExpressionParsing::MethodAccessorFactory::m_kits
std::map< TMethodCall::EReturnType, std::unique_ptr< IMethodAccessorKit > > m_kits
Definition: MethodAccessor.h:252
ExpressionParsing::CollectionMethodHelper::Kit::Kit
Kit(Kit &&)=default
RootUtils::TSMethodCall
Helper for making a thread-safe function call.
Definition: TSMethodCall.h:33
ExpressionParsing::IProxyLoader::VariableType
VariableType
Definition: IProxyLoader.h:21
ExpressionParsing::IProxyLoader::VT_DOUBLE
@ VT_DOUBLE
Definition: IProxyLoader.h:21
ExpressionParsing::MethodAccessorFactory::MethodAccessorKit::m_scalarType
ExpressionParsing::IProxyLoader::VariableType m_scalarType
Definition: MethodAccessor.h:212
ExpressionParsing::MethodAccessorFactory::create
std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxElement > &key, RootUtils::TSMethodCall &&method_call, TVirtualCollectionProxy *proxy=nullptr) const
Create an accessor which calls the specified method of an AuxElement.
Definition: MethodAccessor.h:224
ExpressionParsing::MethodAccessorFactory::MethodAccessorFactory
MethodAccessorFactory()
Definition: MethodAccessor.h:217
IAccessor.h
ExpressionParsing::CollectionMethodHelper::Kit
Auxiliary class to create the corresponding auxiliary helper object.
Definition: MethodAccessor.h:65
ExpressionParsing::MethodHelper::ATLAS_THREAD_SAFE
RootUtils::TSMethodCall m_methodCall ATLAS_THREAD_SAFE
Definition: MethodAccessor.h:136
ExpressionParsing::MethodAccessorFactory::IMethodAccessorKit::create
virtual std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxElement > &key, RootUtils::TSMethodCall &&method_call, TVirtualCollectionProxy *proxy=nullptr) const =0
ExpressionParsing::CollectionMethodHelper::Kit::Kit
Kit(RootUtils::TSMethodCall &&method_call, TVirtualCollectionProxy &collection_proxy)
Definition: MethodAccessor.h:68
ExpressionParsing::MethodHelper::MethodHelper
MethodHelper(const RootUtils::TSMethodCall &method_call, const void *data)
Definition: MethodAccessor.h:93
ExpressionParsing
Namespace holding all the expression evaluation code.
Definition: ExpressionParser.h:26
ExpressionParsing::CollectionMethodHelper::Kit::m_collectionProxy
const TVirtualCollectionProxy * m_collectionProxy
Definition: MethodAccessor.h:80
ExpressionParsing::MethodAccessorFactory::MethodAccessorKit::create
virtual std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxVectorBase > &key, RootUtils::TSMethodCall &&method_call, TVirtualCollectionProxy *proxy) const override
create an accessor which called the specified method of an AuxVectorBase.
Definition: MethodAccessor.h:168
ExpressionParsing::MethodAccessorFactory::MethodAccessorKit::createAccessor
std::unique_ptr< IAccessor > createAccessor(const SG::ReadHandleKey< T_Aux > &key, RootUtils::TSMethodCall &&method_call, TVirtualCollectionProxy *proxy, ExpressionParsing::IProxyLoader::VariableType variable_type) const
Definition: MethodAccessor.h:193
ExpressionParsing::CollectionMethodHelper
Auxiliary class to handle method calls of vector containers (AuxVectorBase)
Definition: MethodAccessor.h:30
ExpressionParsing::CollectionMethodHelper::Kit::ATLAS_THREAD_SAFE
RootUtils::TSMethodCall m_methodCall ATLAS_THREAD_SAFE
Definition: MethodAccessor.h:79
ExpressionParsing::CollectionMethodHelper::get
T_src get(const T_Cont &, std::size_t idx) const
Get the specified element of the vector provided by the container.
Definition: MethodAccessor.h:56
ExpressionParsing::CollectionMethodHelper::get
T_src get(const T_Cont &) const
Get the scalar provided by the container.
Definition: MethodAccessor.h:48
ExpressionParsing::CollectionMethodHelper::ATLAS_THREAD_SAFE
RootUtils::TSMethodCall m_methodCall ATLAS_THREAD_SAFE
Definition: MethodAccessor.h:84
ExpressionParsing::MethodAccessorFactory::MethodAccessorKit::create
virtual std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxElement > &key, RootUtils::TSMethodCall &&method_call, TVirtualCollectionProxy *proxy=nullptr) const override
create an accessor which called the specified method of an AuxElement.
Definition: MethodAccessor.h:180
ExpressionParsing::IProxyLoader::VT_VECINT
@ VT_VECINT
Definition: IProxyLoader.h:21
RootUtils::TSMethodCall::call
TMethodCall * call()
Return a pointer to the thread-specific TMethodCall.
Definition: TSMethodCall.cxx:109
ExpressionParsing::MethodAccessorFactory::IMethodAccessorKit::~IMethodAccessorKit
virtual ~IMethodAccessorKit()
Definition: MethodAccessor.h:147
normalizedTypeinfoName.h
Convert a type_info to a normalized string representation (matching the names used in the root dictio...
ExpressionParsing::Singleton
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:88
ExpressionParsing::IProxyLoader::VT_INT
@ VT_INT
Definition: IProxyLoader.h:21
ExpressionParsing::MethodHelper::Kit
Auxiliary class to create the corresponding auxiliary helper object.
Definition: MethodAccessor.h:124
ExpressionParsing::MethodAccessorFactory::IMethodAccessorKit::create
virtual std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxVectorBase > &key, RootUtils::TSMethodCall &&method_call, TVirtualCollectionProxy *proxy=nullptr) const =0
ExpressionParsing::getContainerSize
std::size_t getContainerSize(const T &cont)
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:73
ExpressionParsing::MethodAccessorFactory::create
std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxVectorBase > &key, RootUtils::TSMethodCall &&method_call, TVirtualCollectionProxy *proxy=nullptr) const
Create an accessor which calls the specified method of an AuxVectorBase.
Definition: MethodAccessor.h:232
ExpressionParsing::MethodAccessorFactory::MethodAccessorKit
Auxiliary class to create a specific accessor which calls a method of an AuxElement or AuxVectorBase.
Definition: MethodAccessor.h:159
ExpressionParsing::getVectorData
const SG::AuxVectorData * getVectorData(const T &cont)
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:78
ExpressionParsing::MethodHelper
Auxiliary class to handle method calls of "scalar" containers (AuxElement).
Definition: MethodAccessor.h:91
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
ExpressionParsing::MethodHelper::Kit::Kit
Kit(Kit &&)=default
ExpressionParsing::MethodHelper::get
T_src get(const T_Cont &) const
Get the scalar provided by the container.
Definition: MethodAccessor.h:106
ReadHandle.h
Handle class for reading from StoreGate.
ExpressionParsing::MethodAccessorFactory::MethodAccessorKit::MethodAccessorKit
MethodAccessorKit(ExpressionParsing::IProxyLoader::VariableType scalar_type, ExpressionParsing::IProxyLoader::VariableType vector_type)
Definition: MethodAccessor.h:161
checker_macros.h
Define macros for attributes used to control the static checker.
ExpressionParsing::MethodHelper::Kit::Kit
Kit(RootUtils::TSMethodCall &&method_call)
Definition: MethodAccessor.h:127
IProxyLoader.h
ExpressionParsing::MethodHelper::get
T_src get(const T_Cont &, [[maybe_unused]] std::size_t idx) const
Get the specified element of the vector provided by the container.
Definition: MethodAccessor.h:114
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37