BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
AkimaFunction.h
Go to the documentation of this file.
1#ifndef AKIMAFUNCTION_H
2#define AKIMAFUNCTION_H
3
4#include "Math/Interpolator.h"
5#include "TNamed.h"
6
7#include <algorithm>
8#include <iostream>
9#include <memory>
10#include <vector>
11
12class AkimaFunction : public TNamed
13{
14 public:
15 std::vector<double> x;
16 std::vector<double> y;
17
18 std::unique_ptr<ROOT::Math::Interpolator> interpolator; //!
19
21 : TNamed()
22 {}
23
24 AkimaFunction(const char* name, const std::vector<double>& xVals, const std::vector<double>& yVals)
25 : TNamed(name, name)
26 , x(xVals)
27 , y(yVals)
28 {
29 SortPoints();
31 }
32
33 virtual ~AkimaFunction() {}
34
36 {
37 if (x.size() != y.size())
38 return;
39
40 std::vector<std::pair<double, double>> pairs;
41 pairs.reserve(x.size());
42
43 for (size_t i = 0; i < x.size(); ++i) {
44 pairs.emplace_back(x[i], y[i]);
45 }
46
47 std::sort(pairs.begin(), pairs.end());
48
49 for (size_t i = 0; i < pairs.size(); ++i) {
50 x[i] = pairs[i].first;
51 y[i] = pairs[i].second;
52 }
53 }
54
56 {
57 if (x.size() < 2)
58 return;
59
60 try {
62
63 interpolator = std::make_unique<ROOT::Math::Interpolator>(x, y, type);
64 } catch (...) {
65 std::cerr << "Interpolator build failed for " << GetName() << std::endl;
66 interpolator.reset();
67 }
68 }
69
70 void Init()
71 {
72 if (!interpolator) {
74 }
75 }
76
77 double Eval(double xx)
78 {
79 if (x.empty())
80 return 0.0;
81
82 if (!interpolator) {
83 Init();
84 }
85
86 if (!interpolator) {
87 return 0.0;
88 }
89
90 if (xx <= x.front())
91 return y.front();
92 if (xx >= x.back())
93 return y.back();
94
95 return interpolator->Eval(xx);
96 }
97
98 void Print(Option_t* option = "") const override
99 {
100 std::cout << "AkimaFunction: " << GetName() << " | N points = " << x.size() << std::endl;
101 }
102
104};
105
106#endif // AKIMAFUNCTION_H
int i
Definition P4_F32vec4.h:22
const char Option_t
#define ClassDefOverride(name, id)
std::unique_ptr< ROOT::Math::Interpolator > interpolator
virtual ~AkimaFunction()
AkimaFunction(const char *name, const std::vector< double > &xVals, const std::vector< double > &yVals)
std::vector< double > x
std::vector< double > y
void BuildInterpolator()
void Print(Option_t *option="") const override
double Eval(double xx)
const char * GetName() const override