CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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_) {
47  result = interpolateStep(xmin_, step, values_[0], values_[1], x);
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)
HcalPiecewiseLinearFunctor inverse() const
static bool isStrictlyIncreasing(Iter begin, Iter const end)
tuple result
Definition: mps_fire.py:311
static bool isStrictlyDecreasing(Iter begin, Iter const end)
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
uint16_t const *__restrict__ x
Definition: gpuClustering.h:43
double operator()(double x) const override
step
Definition: StallMonitor.cc:98