CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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,
14  const double min, const double max,
15  const char* label)
16  : min_(min), max_(max), label_(label ? label : ""), npt_(nCoords)
17  {
18  if (!(npt_ > 1U && npt_ < UINT_MAX/2U - 1U))
19  throw npstat::NpstatInvalidArgument("In npstat::UniformAxis constructor: "
20  "number of points is out of range");
21  if (min_ > max_)
23  bw_ = (max_ - min_)/(npt_ - 1U);
24  if (max_ == min_)
26  "In npstat::UniformAxis constructor: "
27  "minimum and maximum must be distinct");
28  }
29 
30  std::pair<unsigned,double> UniformAxis::getInterval(const double x) const
31  {
32  if (x <= min_)
33  return std::pair<unsigned,double>(0U, 1.0);
34  else if (x >= max_)
35  return std::pair<unsigned,double>(npt_ - 2U, 0.0);
36  else
37  {
38  unsigned binnum = static_cast<unsigned>(floor((x - min_)/bw_));
39  if (binnum > npt_ - 2U)
40  binnum = npt_ - 2U;
41  double w = binnum + 1.0 - (x - min_)/bw_;
42  if (w < 0.0)
43  w = 0.0;
44  else if (w > 1.0)
45  w = 1.0;
46  return std::pair<unsigned,double>(binnum, w);
47  }
48  }
49 
50  std::pair<unsigned,double> UniformAxis::linearInterval(const double x) const
51  {
52  if (x <= min_)
53  return std::pair<unsigned,double>(0U, 1.0 - (x - min_)/bw_);
54  else if (x >= max_)
55  return std::pair<unsigned,double>(npt_ - 2U, (max_ - x)/bw_);
56  else
57  {
58  unsigned binnum = static_cast<unsigned>(floor((x - min_)/bw_));
59  if (binnum > npt_ - 2U)
60  binnum = npt_ - 2U;
61  double w = binnum + 1.0 - (x - min_)/bw_;
62  if (w < 0.0)
63  w = 0.0;
64  else if (w > 1.0)
65  w = 1.0;
66  return std::pair<unsigned,double>(binnum, w);
67  }
68  }
69 
70  std::vector<double> UniformAxis::coords() const
71  {
72  std::vector<double> vec;
73  vec.reserve(npt_);
74  const unsigned nptm1 = npt_ - 1U;
75  for (unsigned i=0; i<nptm1; ++i)
76  vec.push_back(min_ + bw_*i);
77  vec.push_back(max_);
78  return vec;
79  }
80 
81  double UniformAxis::coordinate(const unsigned i) const
82  {
83  if (i >= npt_)
85  "In npstat::UniformAxis::coordinate: index out of range");
86  if (i == npt_ - 1U)
87  return max_;
88  else
89  return min_ + bw_*i;
90  }
91 
92  bool UniformAxis::isClose(const UniformAxis& r, const double tol) const
93  {
94  return closeWithinTolerance(min_, r.min_, tol) &&
95  closeWithinTolerance(max_, r.max_, tol) &&
96  label_ == r.label_ &&
97  npt_ == r.npt_;
98  }
99 
101  {
102  return min_ == r.min_ &&
103  max_ == r.max_ &&
104  label_ == r.label_ &&
105  npt_ == r.npt_;
106  }
107 
108  bool UniformAxis::write(std::ostream& of) const
109  {
110  gs::write_pod(of, min_);
111  gs::write_pod(of, max_);
112  gs::write_pod(of, label_);
113  gs::write_pod(of, npt_);
114  return !of.fail();
115  }
116 
117  UniformAxis* UniformAxis::read(const gs::ClassId& id, std::istream& in)
118  {
119  static const gs::ClassId current(gs::ClassId::makeId<UniformAxis>());
120  current.ensureSameId(id);
121 
122  double min = 0.0, max = 0.0;
124  unsigned nBins = 0;
125 
126  gs::read_pod(in, &min);
127  gs::read_pod(in, &max);
128  gs::read_pod(in, &label);
129  gs::read_pod(in, &nBins);
130 
131  if (!in.fail())
132  return new UniformAxis(nBins, min, max, label.c_str());
133  else
134  throw gs::IOReadFailure("In npstat::UniformAxis::read: "
135  "input stream failure");
136  }
137 }
bool isClose(const UniformAxis &r, double tol) const
Definition: UniformAxis.cc:92
int i
Definition: DBlmapReader.cc:9
Uniformly spaced coordinate sets for use in constructing rectangular grids.
bool operator==(const UniformAxis &r) const
Definition: UniformAxis.cc:100
bool closeWithinTolerance(const double &a, const double &b, const double &tol)
double max() const
Definition: UniformAxis.h:37
#define min(a, b)
Definition: mlp_lapack.h:161
double coordinate(unsigned i) const
Definition: UniformAxis.cc:81
Exceptions for the npstat namespace.
const T & max(const T &a, const T &b)
const std::string & label() const
Definition: UniformAxis.h:38
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
bool write(std::ostream &of) const
Definition: UniformAxis.cc:108
Determine if two doubles are within requested relative tolerance of each other.
std::string label_
Definition: UniformAxis.h:91
std::vector< double > coords() const
Definition: UniformAxis.cc:70
static UniformAxis * read(const gs::ClassId &id, std::istream &in)
Definition: UniformAxis.cc:117
double min() const
Definition: UniformAxis.h:36
std::pair< unsigned, double > getInterval(double coordinate) const
Definition: UniformAxis.cc:30
T w() const
Definition: DDAxes.h:10
std::pair< unsigned, double > linearInterval(double coordinate) const
Definition: UniformAxis.cc:50