CMS 3D CMS Logo

HcalInterpolatedTableFunctor.cc
Go to the documentation of this file.
1 #include <algorithm>
3 
5 
6 inline static double interpolateStep(
7  const double x0, const double step, const double y0, const double y1, const double x) {
8  return y0 + (y1 - y0) * ((x - x0) / step);
9 }
10 
12  : xmin_(0.), xmax_(0.), leftExtrapolationLinear_(false), rightExtrapolationLinear_(false) {}
13 
15  const double ixmin,
16  const double ixmax,
17  const bool leftExtrapolationLinear,
18  const bool rightExtrapolationLinear)
19  : values_(values),
20  xmin_(ixmin),
21  xmax_(ixmax),
22  leftExtrapolationLinear_(leftExtrapolationLinear),
23  rightExtrapolationLinear_(rightExtrapolationLinear) {
24  if (values_.size() < 2)
25  throw cms::Exception(
26  "In HcalInterpolatedTableFunctor constructor:"
27  " insufficient number of points");
28  if (xmin_ >= xmax_)
29  throw cms::Exception(
30  "In HcalInterpolatedTableFunctor constructor:"
31  " invalid min and/or max coordinates");
32 }
33 
34 double HcalInterpolatedTableFunctor::operator()(const double x) const {
35  double result = 0.0;
36  const std::size_t sz = values_.size();
37  const std::size_t szm1 = sz - 1;
38  const double step = (xmax_ - xmin_) / szm1;
39 
40  if (x >= xmax_) {
42  result = interpolateStep(xmax_ - step, step, values_[sz - 2], values_[szm1], x);
43  else
44  result = values_[szm1];
45  } else if (x <= xmin_) {
48  else
49  result = values_[0];
50  } else {
51  const std::size_t ux = static_cast<std::size_t>((x - xmin_) / step);
52  if (ux >= szm1)
53  return values_[szm1];
54  result = interpolateStep(ux * step + xmin_, step, values_[ux], values_[ux + 1], x);
55  }
56 
57  return result;
58 }
59 
61  return isStrictlyIncreasing(values_.begin(), values_.end()) || isStrictlyDecreasing(values_.begin(), values_.end());
62 }
63 
65  if (!isStrictlyMonotonous())
66  throw cms::Exception(
67  "In HcalInterpolatedTableFunctor::inverse:"
68  " can't invert non-monotonous functor");
69  const std::size_t sz = values_.size();
70  const std::size_t szm1 = sz - 1;
71  const double step = (xmax_ - xmin_) / szm1;
72  std::vector<std::pair<double, double> > points;
73  points.reserve(sz);
74  for (std::size_t i = 0; i < sz; ++i) {
75  const double x = (i == szm1 ? xmax_ : xmin_ + step * i);
76  points.push_back(std::make_pair(values_[i], x));
77  }
80  if (values_[0] > values_[sz - 1])
81  std::swap(l, r);
82  return HcalPiecewiseLinearFunctor(points, l, r);
83 }
84 
85 BOOST_CLASS_EXPORT_IMPLEMENT(HcalInterpolatedTableFunctor)
static double interpolateStep(const double x0, const double step, const double y0, const double y1, const double x)
static bool isStrictlyIncreasing(Iter begin, Iter const end)
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:112
static bool isStrictlyDecreasing(Iter begin, Iter const end)
double operator()(double x) const override
float x
step
Definition: StallMonitor.cc:83
HcalPiecewiseLinearFunctor inverse() const