ATLAS Offline Software
GenAccessor.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_GenAccessor_h_
5 #define _ExpressionEvaluation_GenAccessor_h_
6 
8 #include "StoreGate/ReadHandle.h"
10 
11 #include "BaseAccessor.h"
12 
13 #include <vector>
14 
15 namespace ExpressionParsing {
16 
19  class ScalarHelper {
20  public:
21  template <class T_Dest, class T_Cont, class T_Helper>
22  static void fillVector(T_Helper &helper, SG::ReadHandle<T_Cont> &handle, std::vector<T_Dest> &result) {
23  result.reserve(1);
24  result.push_back( static_cast<T_Dest>( helper.get( *handle ) ));
25  }
26 
27  template <class T_Dest, class T_Cont, class T_Helper>
28  static T_Dest getScalar(T_Helper &helper, SG::ReadHandle<T_Cont> &handle,const T_Dest &dummy) {
29  (void) dummy;
30  return static_cast<T_Dest>( helper.get( *handle ) );
31  }
32  };
33 
36  class VectorHelper {
37  public:
38  template <class T_Dest, class T_Cont, class T_Helper>
39  static void fillVector(T_Helper &helper, SG::ReadHandle<T_Cont> &handle, std::vector<T_Dest> &result) {
40  std::size_t n_elements=helper.size(*handle);
41  // helper.checkSize(n_elements);
42  result.reserve(n_elements);
43  for(std::size_t idx =0; idx <n_elements; ++idx) {
44  result.push_back( static_cast<T_Dest>( helper.get(*handle, idx)) );
45  }
46  }
47  template <class T_Dest, class T_Cont, class T_Helper>
48  static T_Dest getScalar(T_Helper &helper, SG::ReadHandle<T_Cont> &handle, const T_Dest &dummy) {
49  (void) dummy;
50  if (getContainerSize(*handle) != 1) {
52  }
53 
54  return static_cast<T_Dest>( helper.get(*handle, 0));
55  }
56  };
57 
62  template <class T_Cont, class T_HelperKit, class T_ScalarVectorHelper>
63  class GenAccessor : public BaseAccessor {
64  public:
65  GenAccessor(const SG::ReadHandleKey<T_Cont> &key, T_HelperKit &&helper_kit, IProxyLoader::VariableType variable_type)
66  : BaseAccessor(variable_type),
67  m_key(&key),
68  m_helperKit(std::move(helper_kit))
69  {}
70 
71  virtual int loadInt(const EventContext& ctx) const override {
72  return this->loadScalar<int>(ctx);
73  }
74  virtual double loadDouble(const EventContext& ctx) const override {
75  return this->loadScalar<double>(ctx);
76  }
77  virtual std::vector<int> loadVecInt(const EventContext& ctx) const override {
78  return this->loadVector<int>(ctx);
79  }
80  virtual std::vector<double> loadVec(const EventContext& ctx) const override {
81  return this->loadVector<double>(ctx);
82  }
83  protected:
84  template <class T_Dest>
85  std::vector<T_Dest> loadVector(const EventContext& ctx) const {
86  SG::ReadHandle<T_Cont> handle(*this->m_key, ctx);
87  if (!handle.isValid()) {
88  this->throwInvalidHandle(this->m_key->key());
89  }
90  std::vector<T_Dest> result;
91  if (getContainerSize(*handle)>0) {
92  auto helper( m_helperKit.create(ctx, handle));
93  T_ScalarVectorHelper::fillVector(helper, handle,result);
94  }
95  return result;
96  }
97 
98  template <class T_Dest>
99  T_Dest loadScalar(const EventContext& ctx) const {
100  SG::ReadHandle<T_Cont> handle(*this->m_key, ctx);
101  if (!handle.isValid()) {
102  this->throwInvalidHandle(this->m_key->key());
103  }
104  auto helper( m_helperKit.create(ctx, handle));
105  T_Dest dummy {};
106  return T_ScalarVectorHelper::getScalar(helper, handle, dummy);
107  }
108 
109  const SG::ReadHandleKey<T_Cont> *m_key; //< key to access the xAOD container
110  T_HelperKit m_helperKit; //< auxiliary class to create an access helper
111  };
112 }
113 #endif
ReadHandleKey.h
Property holding a SG store/key/clid from which a ReadHandle is made.
ExpressionParsing::VectorHelper
Auxiliary class to handle vector like containers (AuxVectorBase).
Definition: GenAccessor.h:36
get_generator_info.result
result
Definition: get_generator_info.py:21
ExpressionParsing::GenAccessor::m_key
const SG::ReadHandleKey< T_Cont > * m_key
Definition: GenAccessor.h:109
SG::ReadHandle< T_Cont >
ExpressionParsing::GenAccessor::loadVec
virtual std::vector< double > loadVec(const EventContext &ctx) const override
Definition: GenAccessor.h:80
ExpressionParsing::GenAccessor
Generic accessor to access xAOD object content.
Definition: GenAccessor.h:63
ExpressionParsing::ScalarHelper::fillVector
static void fillVector(T_Helper &helper, SG::ReadHandle< T_Cont > &handle, std::vector< T_Dest > &result)
Definition: GenAccessor.h:22
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
SG::ReadHandleKey< T_Cont >
ExpressionParsing::VectorHelper::getScalar
static T_Dest getScalar(T_Helper &helper, SG::ReadHandle< T_Cont > &handle, const T_Dest &dummy)
Definition: GenAccessor.h:48
runBeamSpotCalibration.helper
helper
Definition: runBeamSpotCalibration.py:112
ExpressionParsing::GenAccessor::loadVecInt
virtual std::vector< int > loadVecInt(const EventContext &ctx) const override
Definition: GenAccessor.h:77
ExpressionParsing::IProxyLoader::VariableType
VariableType
Definition: IProxyLoader.h:21
ExpressionParsing::GenAccessor::loadInt
virtual int loadInt(const EventContext &ctx) const override
Definition: GenAccessor.h:71
ExpressionParsing
Namespace holding all the expression evaluation code.
Definition: ExpressionParser.h:26
ExpressionParsing::GenAccessor::loadDouble
virtual double loadDouble(const EventContext &ctx) const override
Definition: GenAccessor.h:74
python.xAODType.dummy
dummy
Definition: xAODType.py:4
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::GenAccessor::loadScalar
T_Dest loadScalar(const EventContext &ctx) const
Definition: GenAccessor.h:99
ExpressionParsing::GenAccessor::m_helperKit
T_HelperKit m_helperKit
Definition: GenAccessor.h:110
SG::VarHandleBase::key
virtual const std::string & key() const override final
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleBase.cxx:64
ExpressionParsing::getContainerSize
std::size_t getContainerSize(const T &cont)
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:73
ExpressionParsing::BaseAccessor::throwVectorContainsNotOneElement
static void throwVectorContainsNotOneElement(const std::string &key, std::size_t n_elements)
Definition: BaseAccessor.h:25
ExpressionParsing::VectorHelper::fillVector
static void fillVector(T_Helper &helper, SG::ReadHandle< T_Cont > &handle, std::vector< T_Dest > &result)
Definition: GenAccessor.h:39
ExpressionParsing::BaseAccessor
Base class of xAOD object content accessors.
Definition: BaseAccessor.h:16
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
ReadHandle.h
Handle class for reading from StoreGate.
BaseAccessor.h
IProxyLoader.h
ExpressionParsing::ScalarHelper::getScalar
static T_Dest getScalar(T_Helper &helper, SG::ReadHandle< T_Cont > &handle, const T_Dest &dummy)
Definition: GenAccessor.h:28
ExpressionParsing::GenAccessor::loadVector
std::vector< T_Dest > loadVector(const EventContext &ctx) const
Definition: GenAccessor.h:85
ExpressionParsing::GenAccessor::GenAccessor
GenAccessor(const SG::ReadHandleKey< T_Cont > &key, T_HelperKit &&helper_kit, IProxyLoader::VariableType variable_type)
Definition: GenAccessor.h:65
ExpressionParsing::ScalarHelper
Auxiliary class to handle scalar like containers (AuxElement).
Definition: GenAccessor.h:19
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37