CbmRoot
CbmSplineFunction.h
Go to the documentation of this file.
1 /*
2  *====================================================================
3  *
4  * Class for linear spline function used by CbmBilinearSpline class in CbmModels package
5  *
6  * Authors: V.Vovchenko
7  *
8  * e-mail :
9  *
10  *====================================================================
11  *
12  * 2D spline class
13  *
14  *====================================================================
15  */
16 
17 #ifndef SPLINEFUNCTION_H
18 #define SPLINEFUNCTION_H
19 #include <algorithm>
20 #include <cmath>
21 #include <vector>
22 
24 public:
25  std::vector<std::pair<double, double>> vals;
26  SplineFunction() : vals() { vals.resize(0); }
27  SplineFunction(std::vector<double> x, std::vector<double> y) : vals() {
28  for (unsigned int i = 0; i < x.size(); ++i) {
29  vals.push_back(std::make_pair(x[i], y[i]));
30  }
31  sort(vals.begin(), vals.end());
32  }
33  void add_val(double x, double val) {
34  vals.push_back(std::make_pair(x, val));
35  sort(vals.begin(), vals.end());
36  }
37  double f(double arg) const {
38  if (vals.size() < 2) return 0.;
39  unsigned int ind = 0;
40  std::pair<double, double> op = std::make_pair(arg, 0.);
41  std::vector<std::pair<double, double>>::const_iterator it =
42  lower_bound(vals.begin(), vals.end(), op);
43  ind = distance(vals.begin(), it);
44  if (ind == 0)
45  return vals[0].second
46  + (arg - vals[0].first) * (vals[1].second - vals[0].second)
47  / (vals[1].first - vals[0].first);
48  if (ind == vals.size())
49  return vals[ind - 2].second
50  + (arg - vals[ind - 2].first)
51  * (vals[ind - 1].second - vals[ind - 2].second)
52  / (vals[ind - 1].first - vals[ind - 2].first);
53  return vals[ind - 1].second
54  + (arg - vals[ind - 1].first)
55  * (vals[ind].second - vals[ind - 1].second)
56  / (vals[ind].first - vals[ind - 1].first);
57  }
58  double fsquare(double arg) {
59  double ret = f(arg);
60  return ret * ret;
61  }
62  void clear() {
63  vals.resize(2);
64  vals[0].first = 0.;
65  vals[0].second = 0.;
66  vals[1].first = 1.;
67  vals[1].second = 0.;
68  }
69  void clearall() { vals.resize(0); }
70  void fill(std::vector<double> x, std::vector<double> y) {
71  vals.resize(0);
72  for (unsigned int i = 0; i < x.size(); ++i) {
73  vals.push_back(std::make_pair(x[i], y[i]));
74  }
75  sort(vals.begin(), vals.end());
76  }
77  void setConstant(double val) {
78  vals.resize(0);
79  vals.push_back(std::make_pair(0., val));
80  vals.push_back(std::make_pair(1., val));
81  }
82 };
83 
84 
85 #endif // SPLINEFUNCTION_H
SplineFunction::clearall
void clearall()
Definition: CbmSplineFunction.h:69
SplineFunction::setConstant
void setConstant(double val)
Definition: CbmSplineFunction.h:77
SplineFunction::SplineFunction
SplineFunction(std::vector< double > x, std::vector< double > y)
Definition: CbmSplineFunction.h:27
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
SplineFunction::fsquare
double fsquare(double arg)
Definition: CbmSplineFunction.h:58
SplineFunction::vals
std::vector< std::pair< double, double > > vals
Definition: CbmSplineFunction.h:25
SplineFunction::clear
void clear()
Definition: CbmSplineFunction.h:62
SplineFunction::fill
void fill(std::vector< double > x, std::vector< double > y)
Definition: CbmSplineFunction.h:70
SplineFunction
Definition: CbmSplineFunction.h:23
first
bool first
Definition: LKFMinuit.cxx:143
SplineFunction::f
double f(double arg) const
Definition: CbmSplineFunction.h:37
SplineFunction::SplineFunction
SplineFunction()
Definition: CbmSplineFunction.h:26
x
Double_t x
Definition: CbmMvdSensorDigiToHitTask.cxx:68
y
Double_t y
Definition: CbmMvdSensorDigiToHitTask.cxx:68
SplineFunction::add_val
void add_val(double x, double val)
Definition: CbmSplineFunction.h:33