CMS 3D CMS Logo

FormulaEvaluator.h
Go to the documentation of this file.
1 #ifndef CommonTools_Utils_FormulaEvaluator_h
2 #define CommonTools_Utils_FormulaEvaluator_h
3 // -*- C++ -*-
4 //
5 // Package: CommonTools/Utils
6 // Class : FormulaEvaluator
7 //
16 //
17 // Original Author: Christopher Jones
18 // Created: Wed, 23 Sep 2015 21:12:11 GMT
19 //
20 
21 // system include files
22 #include <array>
23 #include <vector>
24 #include <memory>
25 #include <string>
26 
27 // user include files
28 
29 // forward declarations
30 namespace reco {
31  namespace formula {
32  struct ArrayAdaptor {
33  ArrayAdaptor(double const* iStart, size_t iSize) : m_start(iStart), m_size(iSize) {}
34  size_t size() const { return m_size; }
35  bool empty() const { return m_size == 0; }
36  double const* start() const { return m_start; }
37 
38  private:
39  double const* m_start;
40  size_t m_size;
41  };
42  inline double const* startingAddress(ArrayAdaptor const& iV) {
43  if (iV.empty()) {
44  return nullptr;
45  }
46  return iV.start();
47  }
48 
49  class EvaluatorBase;
50  inline double const* startingAddress(std::vector<double> const& iV) {
51  if (iV.empty()) {
52  return nullptr;
53  }
54  return &iV[0];
55  }
56 
57  template <size_t t>
58  inline double const* startingAddress(std::array<double, t> const& iV) {
59  if (iV.empty()) {
60  return nullptr;
61  }
62  return &iV[0];
63  }
64 
65  } // namespace formula
66 
68  public:
69  explicit FormulaEvaluator(std::string const& iFormula);
70 
71  // ---------- const member functions ---------------------
72  template <typename V, typename P>
73  double evaluate(V const& iVariables, P const& iParameters) const {
74  if (m_nVariables > iVariables.size()) {
75  throwWrongNumberOfVariables(iVariables.size());
76  }
77  if (m_nParameters > iParameters.size()) {
78  throwWrongNumberOfParameters(iParameters.size());
79  }
80  return evaluate(formula::startingAddress(iVariables), formula::startingAddress(iParameters));
81  }
82 
83  unsigned int numberOfParameters() const { return m_nParameters; }
84  unsigned int numberOfVariables() const { return m_nVariables; }
85 
86  std::vector<std::string> abstractSyntaxTree() const;
87 
88  private:
89  double evaluate(double const* iVariables, double const* iParameters) const;
90 
91  void throwWrongNumberOfVariables(size_t) const;
92  void throwWrongNumberOfParameters(size_t) const;
93 
94  std::shared_ptr<formula::EvaluatorBase const> m_evaluator;
95  unsigned int m_nVariables = 0;
96  unsigned int m_nParameters = 0;
97  };
98 } // namespace reco
99 
100 #endif
cms::cuda::V
cudaStream_t T uint32_t const T *__restrict__ const uint32_t *__restrict__ uint32_t int cudaStream_t V
Definition: HistoContainer.h:99
reco::formula::ArrayAdaptor::ArrayAdaptor
ArrayAdaptor(double const *iStart, size_t iSize)
Definition: FormulaEvaluator.h:33
reco::formula::ArrayAdaptor::m_size
size_t m_size
Definition: FormulaEvaluator.h:40
pfMETCorrectionType0_cfi.formula
formula
Definition: pfMETCorrectionType0_cfi.py:46
reco::FormulaEvaluator::m_nVariables
unsigned int m_nVariables
Definition: FormulaEvaluator.h:95
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:45
reco::FormulaEvaluator::throwWrongNumberOfParameters
void throwWrongNumberOfParameters(size_t) const
Definition: FormulaEvaluator.cc:946
reco::FormulaEvaluator
Definition: FormulaEvaluator.h:67
reco::formula::ArrayAdaptor::empty
bool empty() const
Definition: FormulaEvaluator.h:35
reco::FormulaEvaluator::numberOfVariables
unsigned int numberOfVariables() const
Definition: FormulaEvaluator.h:84
reco::FormulaEvaluator::m_evaluator
std::shared_ptr< formula::EvaluatorBase const > m_evaluator
Definition: FormulaEvaluator.h:94
reco::formula::ArrayAdaptor::m_start
double const * m_start
Definition: FormulaEvaluator.h:39
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
reco::FormulaEvaluator::numberOfParameters
unsigned int numberOfParameters() const
Definition: FormulaEvaluator.h:83
reco::FormulaEvaluator::throwWrongNumberOfVariables
void throwWrongNumberOfVariables(size_t) const
Definition: FormulaEvaluator.cc:942
reco::FormulaEvaluator::FormulaEvaluator
FormulaEvaluator(std::string const &iFormula)
Definition: FormulaEvaluator.cc:902
reco::formula::ArrayAdaptor
Definition: FormulaEvaluator.h:32
reco::formula::ArrayAdaptor::start
double const * start() const
Definition: FormulaEvaluator.h:36
reco::formula::ArrayAdaptor::size
size_t size() const
Definition: FormulaEvaluator.h:34
reco::FormulaEvaluator::evaluate
double evaluate(V const &iVariables, P const &iParameters) const
Definition: FormulaEvaluator.h:73
reco::formula::startingAddress
double const * startingAddress(ArrayAdaptor const &iV)
Definition: FormulaEvaluator.h:42
P
std::pair< OmniClusterRef, TrackingParticleRef > P
Definition: BDHadronTrackMonitoringAnalyzer.cc:202
reco::FormulaEvaluator::m_nParameters
unsigned int m_nParameters
Definition: FormulaEvaluator.h:96
reco::FormulaEvaluator::abstractSyntaxTree
std::vector< std::string > abstractSyntaxTree() const
Definition: FormulaEvaluator.cc:951