ATLAS Offline Software
SGxAODProxyLoader.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // SGxAODProxyLoader.h, (c) ATLAS Detector software
8 // Author: Thomas Gillam (thomas.gillam@cern.ch)
9 // ExpressionParsing library
11 
12 #ifndef SG_XAOD_PROXY_LOADER_H
13 #define SG_XAOD_PROXY_LOADER_H
14 
17 
18 #include "GaudiKernel/ServiceHandle.h"
19 #include "StoreGate/StoreGateSvc.h"
20 #include "SGTools/StlVectorClids.h"
21 #include "SGTools/BuiltinsClids.h"
26 
28 #include <any>
29 
30 #include "IAccessor.h"
31 
32 class TMethodCall;
33 class TVirtualCollectionProxy;
34 
35 namespace SG {
36  class AuxVectorBase;
37  class AuxElement;
38 }
39 #include <map>
40 
41 namespace ExpressionParsing {
42 
43 
47 
48  public:
50  SGxAODProxyLoader(StoreGateSvc_t &evtStore, bool verbose=false);
51  virtual ~SGxAODProxyLoader();
52 
53  virtual void reset();
54 
55  virtual IProxyLoader::VariableType variableTypeFromString(const std::string &varname) const;
56 
57  virtual int loadIntVariableFromString(const std::string &varname) const;
58  virtual double loadDoubleVariableFromString(const std::string &varname) const;
59  virtual std::vector<int> loadVecIntVariableFromString(const std::string &varname) const;
60  virtual std::vector<double> loadVecDoubleVariableFromString(const std::string &varname) const;
61 
64  class IParentHelper {
65  public:
69  virtual StatusCode declare(SG::VarHandleKey &handle) = 0;
70  };
71 
74  template <class T>
75  class ParentHelper : public IParentHelper {
76  public:
78  virtual StatusCode declare(SG::VarHandleKey &handle) override {
79  m_parent->declare(handle);
80  handle.setOwner(m_parent);
81  return handle.initialize();
82  }
83  private:
85  };
86 
89  template <class T>
91  return ParentHelper<T>(parent);
92  }
93 
105  const std::vector<std::string> &var_names,
106  const std::vector<std::string> &renounce,
107  const std::vector<const DataObjID *> &input_data_in,
108  const std::vector<const DataObjID *> &output_data_in,
109  std::vector<Gaudi::DataHandle *> &new_input_handles,
110  std::vector<Gaudi::DataHandle *> &new_output_handles);
111 
112  private:
113  void splitVarnameIntoContainerAndMethod(const std::string &varname, std::string &containerName, std::string &methodName) const;
114 
120  IAccessor &computeClassForVarname(const EventContext& ctx,
121  std::unordered_map<std::string, CxxUtils::CachedUniquePtrT<IAccessor> >::const_iterator accessor_iter,
122  const std::string &varname) const;
123 
129  std::pair<RootUtils::TSMethodCall, TVirtualCollectionProxy *>
130  getMethodCallAccessor(const std::string &method_name, const std::type_info &info) const;
131 
138  IAccessor &getAccessor(const EventContext& ctx, const std::string &varname) const {
139  std::unordered_map<std::string, CxxUtils::CachedUniquePtrT<IAccessor> >::const_iterator
140  accessor_iter = m_accessor.find(varname);
141  if (accessor_iter == m_accessor.end()) {
142  std::stringstream msg;
143  msg << "No accessor stub was created for " << varname << " i.e. variable is unknown.";
144  throw std::runtime_error(msg.str());
145  }
146  if (accessor_iter->second) return *(accessor_iter->second);
147  return computeClassForVarname(ctx,accessor_iter, varname);
148  }
149 
158  template <class T_Aux>
159  std::unique_ptr<IAccessor> createAccessor(const EventContext& ctx,
161  const SG::ReadDecorHandleKey<T_Aux> *decor_key,
162  SG::auxid_t method_id,
163  const std::string &method_name) const;
164 
166 
169  template <class T_Element_Key, class T_Vector_Key>
170  class ReadHandleMapTmpl : public std::unordered_map<std::string, std::any > {
171  public:
175  template <class T>
176  static T &checkedRef(T *a) {
177  if (!a) {
178  std::stringstream msg; msg << "Conversion to " << typeid(T).name() << " failed.";
179  throw std::runtime_error(msg.str());
180  }
181  return *a;
182  }
183 
185  static bool isVector(const std::any &anything) { return anything.type() == typeid(T_Vector_Key); }
187  static bool isElement(const std::any &anything) { return anything.type() == typeid(T_Element_Key); }
189  static T_Element_Key &elementKey(std::any &anything) { return checkedRef(std::any_cast<T_Element_Key>(&anything)); }
191  static T_Vector_Key &vectorKey(std::any &anything) { return checkedRef(std::any_cast<T_Vector_Key>(&anything)); }
193  static const T_Element_Key &elementKey(const std::any &anything) { return checkedRef(std::any_cast<T_Element_Key>(&anything)); }
195  static const T_Vector_Key &vectorKey(const std::any &anything) { return checkedRef(std::any_cast<T_Vector_Key>(&anything)); }
196  };
201  ReadHandleMap m_readKeys; //< Association of variable names and read handle keys to AuxElement or AuxVectorBase.
202  ReadDecorHandleMap m_decorKeys; //< Association of variable names and read decoration handle keys for AuxElement or AuxVectorBase.
203 
208  std::unordered_map<std::string, CxxUtils::CachedUniquePtrT<IAccessor> > m_accessor;
209 
216  std::unique_ptr<IAccessor> m_emptyVectorAccessor;
217  bool m_verbose = false;
218  };
219 
220 }
221 
222 #endif // SG_XAOD_PROXY_LOADER_H
grepfile.info
info
Definition: grepfile.py:38
ReadHandleKey.h
Property holding a SG store/key/clid from which a ReadHandle is made.
ExpressionParsing::SGxAODProxyLoader::loadIntVariableFromString
virtual int loadIntVariableFromString(const std::string &varname) const
Definition: SGxAODProxyLoader.cxx:216
ExpressionParsing::SGxAODProxyLoader::variableTypeFromString
virtual IProxyLoader::VariableType variableTypeFromString(const std::string &varname) const
Definition: SGxAODProxyLoader.cxx:193
ReadDecorHandleKey.h
Property holding a SG store/key/clid/attr name from which a ReadDecorHandle is made.
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
ExpressionParsing::SGxAODProxyLoader::m_verbose
bool m_verbose
Definition: SGxAODProxyLoader.h:217
ExpressionParsing::SGxAODProxyLoader::ReadHandleMapTmpl::checkedRef
static T & checkedRef(T *a)
Return a reference for the given pointer.
Definition: SGxAODProxyLoader.h:176
ExpressionParsing::SGxAODProxyLoader::IParentHelper
Interface of an auxiliary class to pass the parent, e.g.
Definition: SGxAODProxyLoader.h:64
ExpressionParsing::SGxAODProxyLoader::loadVecIntVariableFromString
virtual std::vector< int > loadVecIntVariableFromString(const std::string &varname) const
Definition: SGxAODProxyLoader.cxx:228
ExpressionParsing::SGxAODProxyLoader::~SGxAODProxyLoader
virtual ~SGxAODProxyLoader()
Definition: SGxAODProxyLoader.cxx:42
xAODVariableProxyLoaders.h
CxxUtils::CachedUniquePtrT
Cached pointer with atomic update.
Definition: CachedUniquePtr.h:54
ExpressionParsing::SGxAODProxyLoader::splitVarnameIntoContainerAndMethod
void splitVarnameIntoContainerAndMethod(const std::string &varname, std::string &containerName, std::string &methodName) const
Definition: SGxAODProxyLoader.cxx:52
ExpressionParsing::SGxAODProxyLoader::wrapParent
static ParentHelper< T > wrapParent(T *parent)
auxiliary method to create the auxiliary class to declare new data handles with the calling AthAlgToo...
Definition: SGxAODProxyLoader.h:90
ExpressionParsing::SGxAODProxyLoader::StoreGateSvc_t
ServiceHandle< StoreGateSvc > StoreGateSvc_t
Definition: SGxAODProxyLoader.h:49
ExpressionParsing::SGxAODProxyLoader::ReadHandleMapTmpl::elementKey
static T_Element_Key & elementKey(std::any &anything)
Get a reference to the element object referred to by any or throw an exception.
Definition: SGxAODProxyLoader.h:189
ExpressionParsing::SGxAODProxyLoader::ParentHelper::ParentHelper
ParentHelper(T *parent)
Definition: SGxAODProxyLoader.h:77
BuiltinsClids.h
ExpressionParsing::SGxAODProxyLoader::getMethodCallAccessor
std::pair< RootUtils::TSMethodCall, TVirtualCollectionProxy * > getMethodCallAccessor(const std::string &method_name, const std::type_info &info) const
Auxiliary method to create a TMethodCall and eventually also a collection proxy to call the given met...
Definition: SGxAODProxyLoader.cxx:60
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::SGxAODProxyLoader::m_readKeys
ReadHandleMap m_readKeys
Definition: SGxAODProxyLoader.h:201
ExpressionParsing::SGxAODProxyLoader::ReadHandleMapTmpl::elementKey
static const T_Element_Key & elementKey(const std::any &anything)
Get a reference to the element object referred to by any or throw an exception (read only).
Definition: SGxAODProxyLoader.h:193
ExpressionParsing::SGxAODProxyLoader::reset
virtual void reset()
Definition: SGxAODProxyLoader.cxx:46
ExpressionParsing::SGxAODProxyLoader::SGxAODProxyLoader
SGxAODProxyLoader(StoreGateSvc_t &evtStore, bool verbose=false)
Definition: SGxAODProxyLoader.cxx:40
ExpressionParsing::IProxyLoader
Definition: IProxyLoader.h:19
ExpressionParsing::SGxAODProxyLoader::m_accessor
std::unordered_map< std::string, CxxUtils::CachedUniquePtrT< IAccessor > > m_accessor
Association of variable names to accessors for corresponding xAOD object content.
Definition: SGxAODProxyLoader.h:208
ExpressionParsing::SGxAODProxyLoader::ParentHelper::m_parent
T * m_parent
Definition: SGxAODProxyLoader.h:84
ExpressionParsing::IProxyLoader::VariableType
VariableType
Definition: IProxyLoader.h:21
SG::auxid_t
size_t auxid_t
Identifier for a particular aux data item.
Definition: AuxTypes.h:27
StlVectorClids.h
IAccessor.h
ExpressionParsing::SGxAODProxyLoader::loadVecDoubleVariableFromString
virtual std::vector< double > loadVecDoubleVariableFromString(const std::string &varname) const
Definition: SGxAODProxyLoader.cxx:234
ExpressionParsing::SGxAODProxyLoader::ReadHandleMapTmpl::vectorKey
static const T_Vector_Key & vectorKey(const std::any &anything)
Get a reference to the vector object referred to by any or throw an exception (read only).
Definition: SGxAODProxyLoader.h:195
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ExpressionParsing::SGxAODProxyLoader::ParentHelper::declare
virtual StatusCode declare(SG::VarHandleKey &handle) override
This method needs to be implement to declare the given data handle.
Definition: SGxAODProxyLoader.h:78
CachedUniquePtr.h
Cached unique_ptr with atomic update.
ExpressionParsing
Namespace holding all the expression evaluation code.
Definition: ExpressionParser.h:26
test_pyathena.parent
parent
Definition: test_pyathena.py:15
ExpressionParsing::SGxAODProxyLoader::IParentHelper::~IParentHelper
~IParentHelper()
Definition: SGxAODProxyLoader.h:66
ExpressionParsing::SGxAODProxyLoader::ReadHandleMapTmpl::isVector
static bool isVector(const std::any &anything)
Check whether the given any is the "vector" object.
Definition: SGxAODProxyLoader.h:185
ExpressionParsing::SGxAODProxyLoader::ReadHandleMapTmpl
Particular map which is used to associate names to read handles of AuxElements or AuxVectorBase.
Definition: SGxAODProxyLoader.h:170
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
ExpressionParsing::SGxAODProxyLoader::IParentHelper::declare
virtual StatusCode declare(SG::VarHandleKey &handle)=0
This method needs to be implement to declare the given data handle.
ExpressionParsing::SGxAODProxyLoader::updateDataDependencies
bool updateDataDependencies(SGxAODProxyLoader::IParentHelper &parent, const std::vector< std::string > &var_names, const std::vector< std::string > &renounce, const std::vector< const DataObjID * > &input_data_in, const std::vector< const DataObjID * > &output_data_in, std::vector< Gaudi::DataHandle * > &new_input_handles, std::vector< Gaudi::DataHandle * > &new_output_handles)
Create extra data dependencies arising eventually from the given variables.
Definition: SGxAODProxyLoader.cxx:240
ExpressionParsing::SGxAODProxyLoader::m_decorKeys
ReadDecorHandleMap m_decorKeys
Definition: SGxAODProxyLoader.h:202
ExpressionParsing::SGxAODProxyLoader::getAccessor
IAccessor & getAccessor(const EventContext &ctx, const std::string &varname) const
Get an existing or create a new accessor for the given variable name.
Definition: SGxAODProxyLoader.h:138
ExpressionParsing::SGxAODProxyLoader::m_evtStore
StoreGateSvc_t m_evtStore
Definition: SGxAODProxyLoader.h:165
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
SG::VarHandleKey
A property holding a SG store/key/clid from which a VarHandle is made.
Definition: StoreGate/StoreGate/VarHandleKey.h:62
ExpressionParsing::IAccessor
Interface of auxiliary classes to access xAOD object content.
Definition: IAccessor.h:13
ExpressionParsing::SGxAODProxyLoader::ReadHandleMapTmpl::vectorKey
static T_Vector_Key & vectorKey(std::any &anything)
Get a reference to the vector object referred to by any or throw an exception.
Definition: SGxAODProxyLoader.h:191
LArG4AODNtuplePlotter.varname
def varname(hname)
Definition: LArG4AODNtuplePlotter.py:37
a
TList * a
Definition: liststreamerinfos.cxx:10
ExpressionParsing::SGxAODProxyLoader::createAccessor
std::unique_ptr< IAccessor > createAccessor(const EventContext &ctx, const SG::ReadHandleKey< T_Aux > &key, const SG::ReadDecorHandleKey< T_Aux > *decor_key, SG::auxid_t method_id, const std::string &method_name) const
Auxiliary method to create an accessor for the given method_name.
Definition: SGxAODProxyLoader.cxx:112
python.TriggerHandler.verbose
verbose
Definition: TriggerHandler.py:297
ExpressionParsing::SGxAODProxyLoader::ReadHandleMapTmpl::isElement
static bool isElement(const std::any &anything)
Check whether the given any is the "element" object.
Definition: SGxAODProxyLoader.h:187
ExpressionParsing::SGxAODProxyLoader::ParentHelper
Template of an auxiliary class to declare the new handles in the parent AthAlgTool,...
Definition: SGxAODProxyLoader.h:75
ExpressionParsing::SGxAODProxyLoader::loadDoubleVariableFromString
virtual double loadDoubleVariableFromString(const std::string &varname) const
Definition: SGxAODProxyLoader.cxx:222
ReadDecorHandle.h
Handle class for reading a decoration on an object.
ExpressionParsing::SGxAODProxyLoader::computeClassForVarname
IAccessor & computeClassForVarname(const EventContext &ctx, std::unordered_map< std::string, CxxUtils::CachedUniquePtrT< IAccessor > >::const_iterator accessor_iter, const std::string &varname) const
Auxiliary method to create an xAOD object content accessor for the given variable name.
Definition: SGxAODProxyLoader.cxx:136
ExpressionParsing::SGxAODProxyLoader::m_emptyVectorAccessor
std::unique_ptr< IAccessor > m_emptyVectorAccessor
Special accessor to handle empty vectors Accessor are constructed at time of first evaluation.
Definition: SGxAODProxyLoader.h:216
SG::ReadDecorHandleKey
Property holding a SG store/key/clid/attr name from which a ReadDecorHandle is made.
Definition: StoreGate/StoreGate/ReadDecorHandleKey.h:85
IProxyLoader.h
StoreGateSvc.h
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
ExpressionParsing::SGxAODProxyLoader
Auxiliary class to access content of xAOD objects referred to by variables of an expressions handled ...
Definition: SGxAODProxyLoader.h:46
ServiceHandle< StoreGateSvc >
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37