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
double evaluate(V const &iVariables, P const &iParameters) const
void throwWrongNumberOfVariables(size_t) const
double const * start() const
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t V
std::vector< std::string > abstractSyntaxTree() const
unsigned int numberOfParameters() const
double const * startingAddress(ArrayAdaptor const &iV)
std::pair< OmniClusterRef, TrackingParticleRef > P
FormulaEvaluator(std::string const &iFormula)
ArrayAdaptor(double const *iStart, size_t iSize)
fixed size matrix
unsigned int numberOfVariables() const
std::shared_ptr< formula::EvaluatorBase const > m_evaluator
void throwWrongNumberOfParameters(size_t) const