CMS 3D CMS Logo

UniformAxis.cc
Go to the documentation of this file.
1 #include <cmath>
2 #include <climits>
3 #include <algorithm>
5 
6 #include "Alignment/Geners/interface/binaryIO.hh"
7 #include "Alignment/Geners/interface/IOException.hh"
8 
11 
12 namespace npstat {
13  UniformAxis::UniformAxis(const unsigned nCoords, const double min, const double max, const char* label)
14  : min_(min), max_(max), label_(label ? label : ""), npt_(nCoords) {
15  if (!(npt_ > 1U && npt_ < UINT_MAX / 2U - 1U))
17  "In npstat::UniformAxis constructor: "
18  "number of points is out of range");
19  if (min_ > max_)
21  bw_ = (max_ - min_) / (npt_ - 1U);
22  if (max_ == min_)
24  "In npstat::UniformAxis constructor: "
25  "minimum and maximum must be distinct");
26  }
27 
28  std::pair<unsigned, double> UniformAxis::getInterval(const double x) const {
29  if (x <= min_)
30  return std::pair<unsigned, double>(0U, 1.0);
31  else if (x >= max_)
32  return std::pair<unsigned, double>(npt_ - 2U, 0.0);
33  else {
34  unsigned binnum = static_cast<unsigned>(floor((x - min_) / bw_));
35  if (binnum > npt_ - 2U)
36  binnum = npt_ - 2U;
37  double w = binnum + 1.0 - (x - min_) / bw_;
38  if (w < 0.0)
39  w = 0.0;
40  else if (w > 1.0)
41  w = 1.0;
42  return std::pair<unsigned, double>(binnum, w);
43  }
44  }
45 
46  std::pair<unsigned, double> UniformAxis::linearInterval(const double x) const {
47  if (x <= min_)
48  return std::pair<unsigned, double>(0U, 1.0 - (x - min_) / bw_);
49  else if (x >= max_)
50  return std::pair<unsigned, double>(npt_ - 2U, (max_ - x) / bw_);
51  else {
52  unsigned binnum = static_cast<unsigned>(floor((x - min_) / bw_));
53  if (binnum > npt_ - 2U)
54  binnum = npt_ - 2U;
55  double w = binnum + 1.0 - (x - min_) / bw_;
56  if (w < 0.0)
57  w = 0.0;
58  else if (w > 1.0)
59  w = 1.0;
60  return std::pair<unsigned, double>(binnum, w);
61  }
62  }
63 
64  std::vector<double> UniformAxis::coords() const {
65  std::vector<double> vec;
66  vec.reserve(npt_);
67  const unsigned nptm1 = npt_ - 1U;
68  for (unsigned i = 0; i < nptm1; ++i)
69  vec.push_back(min_ + bw_ * i);
70  vec.push_back(max_);
71  return vec;
72  }
73 
74  double UniformAxis::coordinate(const unsigned i) const {
75  if (i >= npt_)
76  throw npstat::NpstatOutOfRange("In npstat::UniformAxis::coordinate: index out of range");
77  if (i == npt_ - 1U)
78  return max_;
79  else
80  return min_ + bw_ * i;
81  }
82 
83  bool UniformAxis::isClose(const UniformAxis& r, const double tol) const {
84  return closeWithinTolerance(min_, r.min_, tol) && closeWithinTolerance(max_, r.max_, tol) && label_ == r.label_ &&
85  npt_ == r.npt_;
86  }
87 
88  bool UniformAxis::operator==(const UniformAxis& r) const {
89  return min_ == r.min_ && max_ == r.max_ && label_ == r.label_ && npt_ == r.npt_;
90  }
91 
92  bool UniformAxis::write(std::ostream& of) const {
93  gs::write_pod(of, min_);
94  gs::write_pod(of, max_);
95  gs::write_pod(of, label_);
96  gs::write_pod(of, npt_);
97  return !of.fail();
98  }
99 
100  UniformAxis* UniformAxis::read(const gs::ClassId& id, std::istream& in) {
101  static const gs::ClassId current(gs::ClassId::makeId<UniformAxis>());
102  current.ensureSameId(id);
103 
104  double min = 0.0, max = 0.0;
106  unsigned nBins = 0;
107 
108  gs::read_pod(in, &min);
109  gs::read_pod(in, &max);
110  gs::read_pod(in, &label);
111  gs::read_pod(in, &nBins);
112 
113  if (!in.fail())
114  return new UniformAxis(nBins, min, max, label.c_str());
115  else
116  throw gs::IOReadFailure(
117  "In npstat::UniformAxis::read: "
118  "input stream failure");
119  }
120 } // namespace npstat
Uniformly spaced coordinate sets for use in constructing rectangular grids.
T w() const
bool closeWithinTolerance(const double &a, const double &b, const double &tol)
double coordinate(unsigned i) const
Definition: UniformAxis.cc:74
double min() const
Definition: UniformAxis.h:34
bool write(std::ostream &of) const
Definition: UniformAxis.cc:92
char const * label
std::pair< unsigned, double > getInterval(double coordinate) const
Definition: UniformAxis.cc:28
Exceptions for the npstat namespace.
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
std::pair< unsigned, double > linearInterval(double coordinate) const
Definition: UniformAxis.cc:46
bool isClose(const UniformAxis &r, double tol) const
Definition: UniformAxis.cc:83
Determine if two doubles are within requested relative tolerance of each other.
std::string label_
Definition: UniformAxis.h:85
bool operator==(const UniformAxis &r) const
Definition: UniformAxis.cc:88
static UniformAxis * read(const gs::ClassId &id, std::istream &in)
Definition: UniformAxis.cc:100
float x
double max() const
Definition: UniformAxis.h:35
std::vector< double > coords() const
Definition: UniformAxis.cc:64
const std::string & label() const
Definition: UniformAxis.h:36