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):
34  m_start(iStart), m_size(iSize) {}
35  size_t size() const { return m_size; }
36  bool empty() const {return m_size == 0; }
37  double const* start() const { return m_start; }
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  }
66 
68  {
69 
70  public:
71  explicit FormulaEvaluator(std::string const& iFormula);
72 
73  // ---------- const member functions ---------------------
74  template<typename V, typename P>
75  double evaluate( V const& iVariables, P const& iParameters) const {
76  if (m_nVariables > iVariables.size()) {
77  throwWrongNumberOfVariables(iVariables.size());
78  }
79  if (m_nParameters > iParameters.size()) {
80  throwWrongNumberOfParameters(iParameters.size());
81  }
82  return evaluate( formula::startingAddress(iVariables),
83  formula::startingAddress(iParameters));
84  }
85 
86  unsigned int numberOfParameters() const { return m_nParameters; }
87  unsigned int numberOfVariables() const { return m_nVariables; }
88 
89  std::vector<std::string> abstractSyntaxTree() const;
90 
91  private:
92  double evaluate(double const* iVariables, double const* iParameters) const;
93 
94  void throwWrongNumberOfVariables(size_t) const ;
95  void throwWrongNumberOfParameters(size_t) const;
96 
97  std::shared_ptr<formula::EvaluatorBase const> m_evaluator;
98  unsigned int m_nVariables = 0;
99  unsigned int m_nParameters = 0;
100 
101 };
102 }
103 
104 #endif
unsigned int numberOfParameters() const
double const * start() const
unsigned int numberOfVariables() const
double evaluate(V const &iVariables, P const &iParameters) const
double const * startingAddress(ArrayAdaptor const &iV)
std::pair< OmniClusterRef, TrackingParticleRef > P
ArrayAdaptor(double const *iStart, size_t iSize)
fixed size matrix
std::shared_ptr< formula::EvaluatorBase const > m_evaluator