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 
26 // user include files
27 
28 // forward declarations
29 namespace reco {
30  namespace formula {
31  struct ArrayAdaptor {
32  ArrayAdaptor(double const* iStart, size_t iSize):
33  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  private:
38  double const* m_start;
39  size_t m_size;
40  };
41  inline double const* startingAddress(ArrayAdaptor const& iV) {
42  if(iV.empty()) {
43  return nullptr;
44  }
45  return iV.start();
46  }
47 
48  class EvaluatorBase;
49  inline double const* startingAddress(std::vector<double> const& iV) {
50  if(iV.empty()) {
51  return nullptr;
52  }
53  return &iV[0];
54  }
55 
56  template<size_t t>
57  inline double const* startingAddress(std::array<double,t> const& iV) {
58  if(iV.empty()) {
59  return nullptr;
60  }
61  return &iV[0];
62  }
63 
64  }
65 
67  {
68 
69  public:
70  explicit FormulaEvaluator(std::string const& iFormula);
71 
72  // ---------- const member functions ---------------------
73  template<typename V, typename P>
74  double evaluate( V const& iVariables, P const& iParameters) const {
75  if (m_nVariables > iVariables.size()) {
76  throwWrongNumberOfVariables(iVariables.size());
77  }
78  if (m_nParameters > iParameters.size()) {
79  throwWrongNumberOfParameters(iParameters.size());
80  }
81  return evaluate( formula::startingAddress(iVariables),
82  formula::startingAddress(iParameters));
83  }
84 
85  unsigned int numberOfParameters() const { return m_nParameters; }
86  unsigned int numberOfVariables() const { return m_nVariables; }
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 };
99 }
100 
101 #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