ATLAS Offline Software
Loading...
Searching...
No Matches
GenAccessor.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4#ifndef _ExpressionEvaluation_GenAccessor_h_
5#define _ExpressionEvaluation_GenAccessor_h_
6
10
11#include "BaseAccessor.h"
12
13#include <vector>
14
15namespace ExpressionParsing {
16
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 auto val = helper.get( *handle );
25 // @TODO or throw an exception if the type conversion is not loss-less?
26 assert( val == static_cast<decltype(val)>(static_cast<T_Dest>(val)));
27 result.push_back( static_cast<T_Dest>(val));
28 }
29
30 template <class T_Dest, class T_Cont, class T_Helper>
31 static T_Dest getScalar(T_Helper &helper, SG::ReadHandle<T_Cont> &handle,const T_Dest &dummy) {
32 (void) dummy;
33 auto val = helper.get( *handle );
34 // @TODO or throw an exception if the type conversion is not loss-less?
35 assert( val == static_cast<decltype(val)>(static_cast<T_Dest>(val)));
36 return static_cast<T_Dest>( val );
37 }
38 };
39
43 public:
44 template <class T_Dest, class T_Cont, class T_Helper>
45 static void fillVector(T_Helper &helper, SG::ReadHandle<T_Cont> &handle, std::vector<T_Dest> &result) {
46 std::size_t n_elements=helper.size(*handle);
47 // helper.checkSize(n_elements);
48 result.reserve(n_elements);
49 for(std::size_t idx =0; idx <n_elements; ++idx) {
50 auto val = helper.get(*handle, idx);
51 // @TODO or throw an exception if the type conversion is not loss-less?
52 assert( val == static_cast<decltype(val)>(static_cast<T_Dest>(val)));
53 result.push_back( static_cast<T_Dest>( val ) );
54 }
55 }
56 template <class T_Dest, class T_Cont, class T_Helper>
57 static T_Dest getScalar(T_Helper &helper, SG::ReadHandle<T_Cont> &handle, const T_Dest &dummy) {
58 (void) dummy;
59 if (getContainerSize(*handle) != 1) {
61 }
62 auto val = helper.get(*handle, 0);
63 // @TODO or throw an exception if the type conversion is not loss-less?
64 assert( val == static_cast<decltype(val)>(static_cast<T_Dest>(val)));
65 return static_cast<T_Dest>(val);
66 }
67 };
68
73 template <class T_Cont, class T_HelperKit, class T_ScalarVectorHelper>
74 class GenAccessor : public BaseAccessor {
75 public:
76 GenAccessor(const SG::ReadHandleKey<T_Cont> &key, T_HelperKit &&helper_kit, IAccessor::VariableType variable_type)
77 : BaseAccessor(variable_type),
78 m_key(&key),
79 m_helperKit(std::move(helper_kit))
80 {}
81
82 virtual int loadInt(const EventContext& ctx, [[maybe_unused]] const std::string &var_name) const override {
83 return this->loadScalar<int>(ctx);
84 }
85 virtual double loadDouble(const EventContext& ctx, [[maybe_unused]] const std::string &var_name) const override {
86 return this->loadScalar<double>(ctx);
87 }
88 virtual std::vector<int> loadVecInt(const EventContext& ctx, [[maybe_unused]] const std::string &var_name) const override {
89 return this->loadVector<int>(ctx);
90 }
91 virtual std::vector<double> loadVec(const EventContext& ctx, [[maybe_unused]] const std::string &var_name) const override {
92 return this->loadVector<double>(ctx);
93 }
94 protected:
95 template <class T_Dest>
96 std::vector<T_Dest> loadVector(const EventContext& ctx) const {
97 SG::ReadHandle<T_Cont> handle(*this->m_key, ctx);
98 if (!handle.isValid()) {
99 this->throwInvalidHandle(this->m_key->key());
100 }
101 std::vector<T_Dest> result;
102 if (getContainerSize(*handle)>0) {
103 auto helper( m_helperKit.create(ctx, handle));
104 T_ScalarVectorHelper::fillVector(helper, handle,result);
105 }
106 return result;
107 }
108
109 template <class T_Dest>
110 T_Dest loadScalar(const EventContext& ctx) const {
111 SG::ReadHandle<T_Cont> handle(*this->m_key, ctx);
112 if (!handle.isValid()) {
113 this->throwInvalidHandle(this->m_key->key());
114 }
115 auto helper( m_helperKit.create(ctx, handle));
116 T_Dest dummy {};
117 return T_ScalarVectorHelper::getScalar(helper, handle, dummy);
118 }
119
120 const SG::ReadHandleKey<T_Cont> *m_key; //< key to access the xAOD container
121 T_HelperKit m_helperKit; //< auxiliary class to create an access helper
122 };
123}
124#endif
Property holding a SG store/key/clid from which a ReadHandle is made.
Handle class for reading from StoreGate.
static void throwVectorContainsNotOneElement(const std::string &key, std::size_t n_elements)
BaseAccessor(IAccessor::VariableType variable_type)
static void throwInvalidHandle(const std::string &key)
GenAccessor(const SG::ReadHandleKey< T_Cont > &key, T_HelperKit &&helper_kit, IAccessor::VariableType variable_type)
Definition GenAccessor.h:76
virtual std::vector< double > loadVec(const EventContext &ctx, const std::string &var_name) const override
Definition GenAccessor.h:91
const SG::ReadHandleKey< T_Cont > * m_key
virtual int loadInt(const EventContext &ctx, const std::string &var_name) const override
Definition GenAccessor.h:82
virtual std::vector< int > loadVecInt(const EventContext &ctx, const std::string &var_name) const override
Definition GenAccessor.h:88
std::vector< T_Dest > loadVector(const EventContext &ctx) const
Definition GenAccessor.h:96
T_Dest loadScalar(const EventContext &ctx) const
virtual double loadDouble(const EventContext &ctx, const std::string &var_name) const override
Definition GenAccessor.h:85
Auxiliary class to handle scalar like containers (AuxElement).
Definition GenAccessor.h:19
static void fillVector(T_Helper &helper, SG::ReadHandle< T_Cont > &handle, std::vector< T_Dest > &result)
Definition GenAccessor.h:22
static T_Dest getScalar(T_Helper &helper, SG::ReadHandle< T_Cont > &handle, const T_Dest &dummy)
Definition GenAccessor.h:31
Auxiliary class to handle vector like containers (AuxVectorBase).
Definition GenAccessor.h:42
static void fillVector(T_Helper &helper, SG::ReadHandle< T_Cont > &handle, std::vector< T_Dest > &result)
Definition GenAccessor.h:45
static T_Dest getScalar(T_Helper &helper, SG::ReadHandle< T_Cont > &handle, const T_Dest &dummy)
Definition GenAccessor.h:57
Property holding a SG store/key/clid from which a ReadHandle is made.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
virtual const std::string & key() const override final
Return the StoreGate ID for the referenced object.
Namespace holding all the expression evaluation code.
STL namespace.