CMS 3D CMS Logo

LinInterpolatedTable1D.h
Go to the documentation of this file.
1 //=========================================================================
2 // LinInterpolatedTable1D.h
3 //
4 // This class can be used to linearly interpolate data in one dimension.
5 // It differs from similar facilities in the fftjet package by its handling
6 // of the extrapolation beyound the grid limits.
7 //
8 // I. Volobouev
9 // April 2011
10 //=========================================================================
11 
12 #ifndef RecoJets_FFTJetAlgorithms_LinInterpolatedTable1D_h
13 #define RecoJets_FFTJetAlgorithms_LinInterpolatedTable1D_h
14 
15 #include <utility>
16 #include <vector>
17 #include <memory>
18 #include <algorithm>
19 
20 #include "fftjet/SimpleFunctors.hh"
22 
23 namespace fftjetcms {
24  class LinInterpolatedTable1D : public fftjet::Functor1<double, double> {
25  public:
26  // Constructor from a regularly spaced data. Extrapolation
27  // from the edge to infinity can be either linear or constant.
28  // "npoints" must be larger than 1.
29  template <typename RealN>
30  LinInterpolatedTable1D(const RealN* data,
31  unsigned npoints,
32  double x_min,
33  double x_max,
36 
37  // Constructor from a list of points, not necessarily regularly
38  // spaced (but must be sorted in the increasing order). The first
39  // member of the pair is the x coordinate, the second is the
40  // tabulated function value. The input list will be interpolated
41  // to "npoints" internal points linearly.
42  template <typename RealN>
43  LinInterpolatedTable1D(const std::vector<std::pair<RealN, RealN> >& v,
44  unsigned npoints,
47 
48  // Constructor which builds a function returning the given constant
49  explicit LinInterpolatedTable1D(double c);
50 
51  inline ~LinInterpolatedTable1D() override {}
52 
53  // Main calculations are performed inside the following operator
54  double operator()(const double& x) const override;
55 
56  // Comparisons (useful for testing)
57  bool operator==(const LinInterpolatedTable1D& r) const;
58  inline bool operator!=(const LinInterpolatedTable1D& r) const { return !(*this == r); }
59 
60  // Various simple inspectors
61  inline double xmin() const { return xmin_; }
62  inline double xmax() const { return xmax_; }
63  inline unsigned npoints() const { return npoints_; }
64  inline bool leftExtrapolationLinear() const { return leftExtrapolationLinear_; }
65  inline bool rightExtrapolationLinear() const { return rightExtrapolationLinear_; }
66  inline const double* data() const { return &data_[0]; }
67 
68  // The following checks whether the table is monotonous
69  // (and, therefore, can be inverted). Possible flat regions
70  // at the edges are not taken into account.
71  bool isMonotonous() const;
72 
73  // Generate the inverse lookup table. Note that it is only
74  // possible if the original table is monotonous (not taking
75  // into account possible flat regions at the edges). If the
76  // inversion is not possible, NULL pointer will be returned.
77  //
78  // The new table will have "npoints" points. The parameters
79  // "leftExtrapolationLinear" and "rightExtrapolationLinear"
80  // refer to the inverted table (note that left and right will
81  // exchange places if the original table is decreasing).
82  //
83  std::unique_ptr<LinInterpolatedTable1D> inverse(unsigned npoints,
85  bool rightExtrapolationLinear) const;
86 
87  private:
88  static inline double interpolateSimple(
89  const double x0, const double x1, const double y0, const double y1, const double x) {
90  return y0 + (y1 - y0) * ((x - x0) / (x1 - x0));
91  }
92 
93  std::vector<double> data_;
94  double xmin_;
95  double xmax_;
96  double binwidth_;
97  unsigned npoints_;
100  mutable bool monotonous_;
101  mutable bool monotonicityKnown_;
102  };
103 } // namespace fftjetcms
104 
105 // Implementation of the templated constructors
106 namespace fftjetcms {
107  template <typename RealN>
109  const unsigned npoints,
110  const double x_min,
111  const double x_max,
112  const bool leftExtrapolationLinear,
113  const bool rightExtrapolationLinear)
114  : data_(data, data + npoints),
115  xmin_(x_min),
116  xmax_(x_max),
117  binwidth_((x_max - x_min) / (npoints - 1U)),
118  npoints_(npoints),
119  leftExtrapolationLinear_(leftExtrapolationLinear),
120  rightExtrapolationLinear_(rightExtrapolationLinear),
121  monotonous_(false),
122  monotonicityKnown_(false) {
123  if (!data)
124  throw cms::Exception("FFTJetBadConfig") << "No data configured" << std::endl;
125  if (npoints <= 1U)
126  throw cms::Exception("FFTJetBadConfig") << "Not enough data points" << std::endl;
127  }
128 
129  template <typename RealN>
131  const unsigned npoints,
132  const bool leftExtrapolationLinear,
133  const bool rightExtrapolationLinear)
134  : xmin_(v[0].first),
135  xmax_(v[v.size() - 1U].first),
136  binwidth_((xmax_ - xmin_) / (npoints - 1U)),
137  npoints_(npoints),
138  leftExtrapolationLinear_(leftExtrapolationLinear),
139  rightExtrapolationLinear_(rightExtrapolationLinear),
140  monotonous_(false),
141  monotonicityKnown_(false) {
142  const unsigned len = v.size();
143  if (len <= 1U)
144  throw cms::Exception("FFTJetBadConfig") << "Not enough data for interpolation" << std::endl;
145 
146  if (npoints <= 1U)
147  throw cms::Exception("FFTJetBadConfig") << "Not enough interpolation table entries" << std::endl;
148 
149  const std::pair<RealN, RealN>* vdata = &v[0];
150  for (unsigned i = 1; i < len; ++i)
151  if (vdata[i - 1U].first >= vdata[i].first)
152  throw cms::Exception("FFTJetBadConfig") << "Input data is not sorted properly" << std::endl;
153 
154  unsigned shift = 0U;
156  ++npoints_;
157  xmin_ -= binwidth_;
158  shift = 1U;
159  }
161  ++npoints_;
162  xmax_ += binwidth_;
163  }
164 
165  data_.resize(npoints_);
166 
168  data_[0] = interpolateSimple(vdata[0].first, vdata[1].first, vdata[0].second, vdata[1].second, xmin_);
169  }
172  vdata[len - 2U].first, vdata[len - 1U].first, vdata[len - 2U].second, vdata[len - 1U].second, xmax_);
173  }
174 
175  data_[shift] = vdata[0].second;
176  data_[npoints - 1U + shift] = vdata[len - 1U].second;
177  unsigned ibelow = 0, iabove = 1;
178  for (unsigned i = 1; i < npoints - 1; ++i) {
179  const double x = xmin_ + (i + shift) * binwidth_;
180  while (static_cast<double>(v[iabove].first) <= x) {
181  ++ibelow;
182  ++iabove;
183  }
184  if (v[ibelow].first == v[iabove].first)
185  data_[i + shift] = (v[ibelow].second + v[iabove].second) / 2.0;
186  else
187  data_[i + shift] = interpolateSimple(v[ibelow].first, v[iabove].first, v[ibelow].second, v[iabove].second, x);
188  }
189  }
190 } // namespace fftjetcms
191 
192 #endif // RecoJets_FFTJetAlgorithms_LinInterpolatedTable1D_h
npoints
static const int npoints
Definition: NuclearInteractionFTFSimulator.h:38
fftjetcms::LinInterpolatedTable1D::operator!=
bool operator!=(const LinInterpolatedTable1D &r) const
Definition: LinInterpolatedTable1D.h:58
mps_fire.i
i
Definition: mps_fire.py:428
funct::false
false
Definition: Factorize.h:29
fftjetcms
Definition: AbsPileupCalculator.h:15
fftjetcms::LinInterpolatedTable1D::operator==
bool operator==(const LinInterpolatedTable1D &r) const
Definition: LinInterpolatedTable1D.cc:17
fftjetcms::LinInterpolatedTable1D::rightExtrapolationLinear
bool rightExtrapolationLinear() const
Definition: LinInterpolatedTable1D.h:65
fftjetcms::LinInterpolatedTable1D::xmax_
double xmax_
Definition: LinInterpolatedTable1D.h:95
fftjetcms::LinInterpolatedTable1D::monotonous_
bool monotonous_
Definition: LinInterpolatedTable1D.h:100
edm::second
U second(std::pair< T, U > const &p)
Definition: ParameterSet.cc:222
fftjetcms::LinInterpolatedTable1D::npoints
unsigned npoints() const
Definition: LinInterpolatedTable1D.h:63
findQualityFiles.v
v
Definition: findQualityFiles.py:179
dqmdumpme.first
first
Definition: dqmdumpme.py:55
fftjetcms::LinInterpolatedTable1D::rightExtrapolationLinear_
bool rightExtrapolationLinear_
Definition: LinInterpolatedTable1D.h:99
testProducerWithPsetDescEmpty_cfi.x1
x1
Definition: testProducerWithPsetDescEmpty_cfi.py:33
testProducerWithPsetDescEmpty_cfi.y1
y1
Definition: testProducerWithPsetDescEmpty_cfi.py:29
fftjetcms::LinInterpolatedTable1D::binwidth_
double binwidth_
Definition: LinInterpolatedTable1D.h:96
fftjetcms::LinInterpolatedTable1D::monotonicityKnown_
bool monotonicityKnown_
Definition: LinInterpolatedTable1D.h:101
fftjetcms::LinInterpolatedTable1D::xmin
double xmin() const
Definition: LinInterpolatedTable1D.h:61
fftjetcms::LinInterpolatedTable1D::inverse
std::unique_ptr< LinInterpolatedTable1D > inverse(unsigned npoints, bool leftExtrapolationLinear, bool rightExtrapolationLinear) const
Definition: LinInterpolatedTable1D.cc:38
mitigatedMETSequence_cff.U
U
Definition: mitigatedMETSequence_cff.py:36
fftjetcms::LinInterpolatedTable1D::isMonotonous
bool isMonotonous() const
Definition: LinInterpolatedTable1D.cc:23
mixed_calib_calo_ttbar_result.leftExtrapolationLinear
leftExtrapolationLinear
Definition: mixed_calib_calo_ttbar_result.py:10
fftjetcms::LinInterpolatedTable1D::xmax
double xmax() const
Definition: LinInterpolatedTable1D.h:62
fftjetcms::LinInterpolatedTable1D::operator()
double operator()(const double &x) const override
Definition: LinInterpolatedTable1D.cc:62
fftjetcms::LinInterpolatedTable1D::leftExtrapolationLinear
bool leftExtrapolationLinear() const
Definition: LinInterpolatedTable1D.h:64
fftjetcms::LinInterpolatedTable1D::data
const double * data() const
Definition: LinInterpolatedTable1D.h:66
fftjetcms::LinInterpolatedTable1D::interpolateSimple
static double interpolateSimple(const double x0, const double x1, const double y0, const double y1, const double x)
Definition: LinInterpolatedTable1D.h:88
fftjetcms::LinInterpolatedTable1D::xmin_
double xmin_
Definition: LinInterpolatedTable1D.h:94
fftjetcms::LinInterpolatedTable1D
Definition: LinInterpolatedTable1D.h:24
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
fftjetcms::LinInterpolatedTable1D::~LinInterpolatedTable1D
~LinInterpolatedTable1D() override
Definition: LinInterpolatedTable1D.h:51
HltBtagPostValidation_cff.c
c
Definition: HltBtagPostValidation_cff.py:31
alignCSCRings.r
r
Definition: alignCSCRings.py:93
edm::shift
static unsigned const int shift
Definition: LuminosityBlockID.cc:7
fftjetcms::LinInterpolatedTable1D::npoints_
unsigned npoints_
Definition: LinInterpolatedTable1D.h:97
Exception
Definition: hltDiff.cc:246
fftjetcms::LinInterpolatedTable1D::leftExtrapolationLinear_
bool leftExtrapolationLinear_
Definition: LinInterpolatedTable1D.h:98
Exception.h
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
fftjetcms::LinInterpolatedTable1D::LinInterpolatedTable1D
LinInterpolatedTable1D(const RealN *data, unsigned npoints, double x_min, double x_max, bool leftExtrapolationLinear, bool rightExtrapolationLinear)
Definition: LinInterpolatedTable1D.h:108
mixed_calib_calo_ttbar_result.rightExtrapolationLinear
rightExtrapolationLinear
Definition: mixed_calib_calo_ttbar_result.py:11
fftjetcms::LinInterpolatedTable1D::data_
std::vector< double > data_
Definition: LinInterpolatedTable1D.h:93
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443