CMS 3D CMS Logo

LinInterpolatedTable1D.cc
Go to the documentation of this file.
1 #include <memory>
2 
4 
5 namespace fftjetcms {
7  : data_(2, c),
8  xmin_(0.0),
9  xmax_(1.0),
10  binwidth_(1.0),
11  npoints_(2U),
12  leftExtrapolationLinear_(false),
13  rightExtrapolationLinear_(false),
14  monotonous_(false),
15  monotonicityKnown_(true) {}
16 
18  return xmin_ == r.xmin_ && xmax_ == r.xmax_ && binwidth_ == r.binwidth_ && npoints_ == r.npoints_ &&
19  leftExtrapolationLinear_ == r.leftExtrapolationLinear_ &&
20  rightExtrapolationLinear_ == r.rightExtrapolationLinear_ && data_ == r.data_;
21  }
22 
24  if (!monotonicityKnown_) {
25  monotonous_ = true;
26  const double delta = data_[npoints_ - 1U] - data_[0];
27  if (delta == 0.0)
28  monotonous_ = false;
29  const double sg = delta > 0.0 ? 1.0 : -1.0;
30  for (unsigned i = 1; i < npoints_ && monotonous_; ++i)
31  if ((data_[i] - data_[i - 1]) * sg <= 0.0)
32  monotonous_ = false;
33  monotonicityKnown_ = true;
34  }
35  return monotonous_;
36  }
37 
38  std::unique_ptr<LinInterpolatedTable1D> LinInterpolatedTable1D::inverse(const unsigned npoints,
39  const bool leftExtrapolationLinear,
40  const bool rightExtrapolationLinear) const {
41  if (!isMonotonous())
42  return std::unique_ptr<LinInterpolatedTable1D>(nullptr);
43 
44  std::vector<std::pair<double, double> > points;
45  points.reserve(npoints_);
46 
47  if (data_[npoints_ - 1U] > data_[0]) {
48  points.push_back(std::pair<double, double>(data_[0], xmin_));
49  for (unsigned i = 1; i < npoints_ - 1U; ++i)
50  points.push_back(std::pair<double, double>(data_[i], xmin_ + i * binwidth_));
51  points.push_back(std::pair<double, double>(data_[npoints_ - 1U], xmax_));
52  } else {
53  points.push_back(std::pair<double, double>(data_[npoints_ - 1U], xmax_));
54  for (unsigned i = npoints_ - 2U; i > 0; --i)
55  points.push_back(std::pair<double, double>(data_[i], xmin_ + i * binwidth_));
56  points.push_back(std::pair<double, double>(data_[0], xmin_));
57  }
58 
59  return std::make_unique<LinInterpolatedTable1D>(points, npoints, leftExtrapolationLinear, rightExtrapolationLinear);
60  }
61 
62  double LinInterpolatedTable1D::operator()(const double& x) const {
63  if (x <= xmin_) {
65  return data_[0] + (data_[1] - data_[0]) * ((x - xmin_) / binwidth_);
66  else
67  return data_[0];
68  } else if (x >= xmax_) {
70  return data_[npoints_ - 1U] - (data_[npoints_ - 2U] - data_[npoints_ - 1U]) * ((x - xmax_) / binwidth_);
71  else
72  return data_[npoints_ - 1U];
73  } else {
74  const unsigned ux = static_cast<unsigned>((x - xmin_) / binwidth_);
75  if (ux >= npoints_ - 1U)
76  return data_[npoints_ - 1U];
77  const double delta = x - (ux * binwidth_ + xmin_);
78  return data_[ux] + (data_[ux + 1U] - data_[ux]) * delta / binwidth_;
79  }
80  }
81 } // namespace fftjetcms
bool operator==(const LinInterpolatedTable1D &r) const
static const int npoints
std::unique_ptr< LinInterpolatedTable1D > inverse(unsigned npoints, bool leftExtrapolationLinear, bool rightExtrapolationLinear) const
double operator()(const double &x) const override
float x
LinInterpolatedTable1D(const RealN *data, unsigned npoints, double x_min, double x_max, bool leftExtrapolationLinear, bool rightExtrapolationLinear)