ATLAS Offline Software
PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.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_Utils_h_
5 #define _ExpressionEvaluation_Utils_h_
6 
7 #include <stdexcept>
8 #include "GaudiKernel/DataHandle.h"
12 
13 namespace ExpressionParsing {
16  template <class T_Key>
17  void initializeHandle(T_Key *key, std::vector<Gaudi::DataHandle *> &new_input_handles) {
18  if (key) {
19  if (key->initialize().isFailure()) {
20  std::stringstream msg;
21  msg << "Failed to initialize read handle " << key->key();
22  throw std::runtime_error( msg.str());
23  }
24  key->init();
25  new_input_handles.push_back( key);
26  }
27  }
28 
29 
30  // helper functions to provide a uniform interface for AuxElement and AuxVectorBase
31  template <class T_Aux>
32  bool isAvailable(const T_Aux &cont, SG::auxid_t auxid);
33 
34  template <>
35  bool isAvailable<SG::AuxVectorBase>(const SG::AuxVectorBase &cont, SG::auxid_t auxid) {
36  return cont.isAvailable(auxid);
37  }
38 
39  template <>
40  bool isAvailable<SG::AuxElement>(const SG::AuxElement &cont, SG::auxid_t auxid) {
41  return cont.container() && cont.container()->isAvailable(auxid);
42  }
43 
44  // helper functions to provide a uniform interface for AuxElement and AuxVectorBase
45  template <class T>
46  class SizeHelper {
47  public:
48  static std::size_t getSize(const T &cont) {
49  (void) cont;
50  return 1;
51  }
52  };
53 
54  template <typename T>
55  class SizeHelper<std::vector<T> > {
56  public:
57  static std::size_t getSize(const std::vector<T> &cont) { return cont.size(); }
58  };
59 
60  template <>
61  class SizeHelper<SG::AuxVectorBase > {
62  public:
63  static std::size_t getSize(const SG::AuxVectorBase &cont) { return cont.size_v(); }
64  };
65 
66  template <>
67  class SizeHelper<SG::AuxElement > {
68  public:
69  static std::size_t getSize(const SG::AuxElement &cont) { return cont.container() ? cont.container()->size_v() : 0; }
70  };
71 
72  template <class T>
73  std::size_t getContainerSize(const T &cont) {
74  return SizeHelper<T>::getSize(cont);
75  }
76 
77  template <class T>
78  const SG::AuxVectorData *getVectorData(const T &cont) {
79  return &cont;
80  }
81 
82  template <>
83  const SG::AuxVectorData *getVectorData<SG::AuxElement>(const SG::AuxElement &cont) {
84  return cont.container();
85  }
86 
87  template <class T_Derived>
88  class Singleton {
89  public:
90  static T_Derived &instance() {
91  if (!s_instance) {
92  s_instance.set(std::make_unique<T_Derived>());
93  }
94  return *s_instance;
95  }
96  private:
98  };
99  template <class T_Derived>
101 
102 }
103 #endif
ExpressionParsing::SizeHelper
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:46
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
SG::AuxElement
Base class for elements of a container that can have aux data.
Definition: AuxElement.h:483
ExpressionParsing::ATLAS_THREAD_SAFE
CxxUtils::CachedUniquePtrT< T_Derived > Singleton< T_Derived >::s_instance ATLAS_THREAD_SAFE
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:100
CxxUtils::CachedUniquePtrT< T_Derived >
AuxVectorBase.h
Manage index tracking and synchronization of auxiliary data.
SG::AuxVectorBase
Manage index tracking and synchronization of auxiliary data.
Definition: AuxVectorBase.h:98
ExpressionParsing::Singleton::instance
static T_Derived & instance()
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:90
ExpressionParsing::isAvailable
bool isAvailable(const T_Aux &cont, SG::auxid_t auxid)
SG::AuxVectorData::isAvailable
bool isAvailable(auxid_t id) const
Test to see if a variable exists in the store.
SG::auxid_t
size_t auxid_t
Identifier for a particular aux data item.
Definition: AuxTypes.h:27
vector
Definition: MultiHisto.h:13
ExpressionParsing
Namespace holding all the expression evaluation code.
Definition: ExpressionParser.h:26
ExpressionParsing::SizeHelper< std::vector< T > >::getSize
static std::size_t getSize(const std::vector< T > &cont)
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:57
ExpressionParsing::Singleton
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:88
ExpressionParsing::getContainerSize
std::size_t getContainerSize(const T &cont)
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:73
SG::AuxVectorBase::isAvailable
bool isAvailable(const std::string &name, const std::string &clsname="") const
Test to see if a variable exists in the store.
ExpressionParsing::Singleton::ATLAS_THREAD_SAFE
static CxxUtils::CachedUniquePtrT< T_Derived > s_instance ATLAS_THREAD_SAFE
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:97
ExpressionParsing::SizeHelper< SG::AuxVectorBase >::getSize
static std::size_t getSize(const SG::AuxVectorBase &cont)
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:63
ExpressionParsing::getVectorData
const SG::AuxVectorData * getVectorData(const T &cont)
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:78
ExpressionParsing::SizeHelper::getSize
static std::size_t getSize(const T &cont)
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:48
SG::AuxVectorData
Manage lookup of vectors of auxiliary data.
Definition: AuxVectorData.h:168
SG::AuxElement::container
const SG::AuxVectorData * container() const
Return the container holding this element.
ExpressionParsing::SizeHelper< SG::AuxElement >::getSize
static std::size_t getSize(const SG::AuxElement &cont)
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:69
checker_macros.h
Define macros for attributes used to control the static checker.
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
SG::AuxVectorData::size_v
virtual size_t size_v() const =0
Return the size of the container.
AuxElement.h
Base class for elements of a container that can have aux data.
ExpressionParsing::initializeHandle
void initializeHandle(T_Key *key, std::vector< Gaudi::DataHandle * > &new_input_handles)
Auxiliary function to initialize newly create data handles.
Definition: PhysicsAnalysis/CommonTools/ExpressionEvaluation/src/Utils.h:17
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37