CMS 3D CMS Logo

HcalPiecewiseLinearFunctor.cc
Go to the documentation of this file.
1 #include <algorithm>
3 
5 
6 inline static double interpolateSimple(
7  const double x0, const double x1,
8  const double y0, const double y1,
9  const double x)
10 {
11  return y0 + (y1 - y0)*((x - x0)/(x1 - x0));
12 }
13 
15  : leftExtrapolationLinear_(false), rightExtrapolationLinear_(false)
16 {
17 }
18 
20  const std::vector<std::pair<double, double> >& points,
21  const bool leftExtrapolationLinear,
22  const bool rightExtrapolationLinear)
23  : leftExtrapolationLinear_(leftExtrapolationLinear),
24  rightExtrapolationLinear_(rightExtrapolationLinear)
25 {
26  const std::size_t sz = points.size();
27  if (sz)
28  {
29  std::vector<std::pair<double, double> > tmp(points);
30  std::sort(tmp.begin(), tmp.end());
31  abscissae_.reserve(sz);
32  values_.reserve(sz);
33  for (std::size_t i=0; i<sz; ++i)
34  {
35  abscissae_.push_back(tmp[i].first);
36  values_.push_back(tmp[i].second);
37  }
38  if (!isStrictlyIncreasing(abscissae_.begin(), abscissae_.end()))
39  throw cms::Exception("In HcalPiecewiseLinearFunctor constructor:"
40  " abscissae must not coincide");
41  }
42 }
43 
44 double HcalPiecewiseLinearFunctor::operator()(const double x) const
45 {
46  double result = 0.0;
47  const std::size_t sz = abscissae_.size();
48  if (sz)
49  {
50  if (sz > 1)
51  {
52  const std::size_t szm1 = sz - 1;
53  if (x >= abscissae_[szm1])
54  {
56  result = interpolateSimple(abscissae_[sz-2], abscissae_[szm1],
57  values_[sz-2], values_[szm1], x);
58  else
59  result = values_[szm1];
60  }
61  else if (x <= abscissae_[0])
62  {
64  result = interpolateSimple(abscissae_[0], abscissae_[1],
65  values_[0], values_[1], x);
66  else
67  result = values_[0];
68  }
69  else
70  {
71  const std::size_t cell = std::upper_bound(abscissae_.begin(), abscissae_.end(), x) -
72  abscissae_.begin() - 1;
73  result = interpolateSimple(abscissae_[cell], abscissae_[cell+1],
74  values_[cell], values_[cell+1], x);
75  }
76  }
77  else
78  result = values_[0];
79  }
80  return result;
81 }
82 
84 {
85  return isStrictlyIncreasing(values_.begin(), values_.end()) ||
86  isStrictlyDecreasing(values_.begin(), values_.end());
87 }
88 
90 {
91  if (!isStrictlyMonotonous())
92  throw cms::Exception("In HcalPiecewiseLinearFunctor::inverse:"
93  " can't invert non-monotonous functor");
94  const std::size_t sz = abscissae_.size();
95  std::vector<std::pair<double, double> > points;
96  points.reserve(sz);
97  for (std::size_t i=0; i<sz; ++i)
98  points.push_back(std::make_pair(values_[i], abscissae_[i]));
101  if (values_[0] > values_[sz - 1])
102  std::swap(l, r);
103  return HcalPiecewiseLinearFunctor(points, l, r);
104 }
105 
107 {
108  double result = 0.0;
109  if (!abscissae_.empty())
110  result = abscissae_[0];
111  return result;
112 }
113 
115 {
116  double result = 0.0;
117  if (!abscissae_.empty())
118  result = abscissae_.back();
119  return result;
120 }
121 
122 BOOST_CLASS_EXPORT_IMPLEMENT(HcalPiecewiseLinearFunctor)
int i
Definition: DBlmapReader.cc:9
virtual double operator()(double x) const override
static bool isStrictlyIncreasing(Iter begin, Iter const end)
U second(std::pair< T, U > const &p)
T x() const
Cartesian x coordinate.
virtual double xmin() const override
static bool isStrictlyDecreasing(Iter begin, Iter const end)
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
HcalPiecewiseLinearFunctor inverse() const
static double interpolateSimple(const double x0, const double x1, const double y0, const double y1, const double x)
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
virtual double xmax() const override