CMS 3D CMS Logo

GridAxis.cc
Go to the documentation of this file.
1 #include <cmath>
2 #include <algorithm>
4 
5 #include "Alignment/Geners/interface/binaryIO.hh"
6 #include "Alignment/Geners/interface/IOException.hh"
7 
10 
11 namespace npstat {
13  if (npt_ <= 1U)
15  "In npstat::GridAxis::initialize: insufficient number "
16  "of coordinates (at least 2 are required)");
17  std::sort(coords_.begin(), coords_.end());
18  const double* c = &coords_[0];
19  if (useLogSpace_) {
20  logs_.reserve(npt_);
21  for (unsigned i = 0; i < npt_; ++i) {
22  if (c[i] <= 0.0)
24  "In npstat::GridAxis::initialize: in log space"
25  "all coordinates must be positive");
26  logs_.push_back(log(c[i]));
27  }
28  }
29 
30  // Can't have duplicate coordinates
31  for (unsigned i = 1; i < npt_; ++i)
32  if (c[i] <= c[i - 1U])
34  "In npstat::GridAxis::initialize: "
35  "all coordinates must be distinct");
36  }
37 
38  GridAxis::GridAxis(const std::vector<double>& coords, const bool useLogSpace)
39  : coords_(coords), npt_(coords_.size()), useLogSpace_(useLogSpace) {
40  initialize();
41  }
42 
43  GridAxis::GridAxis(const std::vector<double>& coords, const char* label, const bool useLogSpace)
44  : coords_(coords), label_(label ? label : ""), npt_(coords_.size()), useLogSpace_(useLogSpace) {
45  initialize();
46  }
47 
48  std::pair<unsigned, double> GridAxis::getInterval(const double x) const {
49  if (useLogSpace_)
50  if (x <= 0.0)
51  throw npstat::NpstatDomainError("In GridAxis::getInterval: argument must be positive");
52  const double* c = &coords_[0];
53  if (x <= c[0])
54  return std::pair<unsigned, double>(0U, 1.0);
55  else if (x >= c[npt_ - 1U])
56  return std::pair<unsigned, double>(npt_ - 2U, 0.0);
57  else {
58  unsigned ibnd = lower_bound(coords_.begin(), coords_.end(), x) - coords_.begin();
59  --ibnd;
60  if (useLogSpace_) {
61  const double* l = &logs_[0];
62  const double w = 1.0 - (log(x) - l[ibnd]) / (l[ibnd + 1U] - l[ibnd]);
63  return std::pair<unsigned, double>(ibnd, w);
64  } else {
65  const double w = 1.0 - (x - c[ibnd]) / (c[ibnd + 1U] - c[ibnd]);
66  return std::pair<unsigned, double>(ibnd, w);
67  }
68  }
69  }
70 
71  std::pair<unsigned, double> GridAxis::linearInterval(const double x) const {
72  if (useLogSpace_)
73  if (x <= 0.0)
74  throw npstat::NpstatDomainError("In GridAxis::linearInterval: argument must be positive");
75  const double* c = &coords_[0];
76  if (x <= c[0]) {
77  if (useLogSpace_) {
78  const double* l = &logs_[0];
79  const double bw = l[1] - l[0];
80  return std::pair<unsigned, double>(0U, 1.0 - (log(x) - l[0]) / bw);
81  } else {
82  const double bw = c[1] - c[0];
83  return std::pair<unsigned, double>(0U, 1.0 - (x - c[0]) / bw);
84  }
85  } else if (x >= c[npt_ - 1U]) {
86  if (useLogSpace_) {
87  const double* l = &logs_[0];
88  const double bw = l[npt_ - 1U] - l[npt_ - 2U];
89  return std::pair<unsigned, double>(npt_ - 2U, (l[npt_ - 1U] - log(x)) / bw);
90  } else {
91  const double bw = c[npt_ - 1U] - c[npt_ - 2U];
92  return std::pair<unsigned, double>(npt_ - 2U, (c[npt_ - 1U] - x) / bw);
93  }
94  } else {
95  unsigned ibnd = lower_bound(coords_.begin(), coords_.end(), x) - coords_.begin();
96  --ibnd;
97  if (useLogSpace_) {
98  const double* l = &logs_[0];
99  const double w = 1.0 - (log(x) - l[ibnd]) / (l[ibnd + 1U] - l[ibnd]);
100  return std::pair<unsigned, double>(ibnd, w);
101  } else {
102  const double w = 1.0 - (x - c[ibnd]) / (c[ibnd + 1U] - c[ibnd]);
103  return std::pair<unsigned, double>(ibnd, w);
104  }
105  }
106  }
107 
108  bool GridAxis::write(std::ostream& of) const {
109  // It is unlikely that this object will be written in isolation.
110  // So, do not bother with too many checks.
111  gs::write_pod_vector(of, coords_);
112  gs::write_pod(of, label_);
113  gs::write_pod(of, useLogSpace_);
114  return !of.fail();
115  }
116 
117  GridAxis* GridAxis::read(const gs::ClassId& id, std::istream& in) {
118  static const gs::ClassId current(gs::ClassId::makeId<GridAxis>());
119  current.ensureSameId(id);
120 
121  std::vector<double> coords;
122  gs::read_pod_vector(in, &coords);
123 
125  gs::read_pod(in, &label);
126 
127  bool useLogSpace;
128  gs::read_pod(in, &useLogSpace);
129 
130  if (in.fail())
131  throw gs::IOReadFailure(
132  "In npstat::GridAxis::read: "
133  "input stream failure");
134  return new GridAxis(coords, label.c_str(), useLogSpace);
135  }
136 
137  bool GridAxis::operator==(const GridAxis& r) const {
138  return useLogSpace_ == r.useLogSpace_ && coords_ == r.coords_ && label_ == r.label_;
139  }
140 
141  bool GridAxis::isClose(const GridAxis& r, const double tol) const {
142  if (!(useLogSpace_ == r.useLogSpace_ && label_ == r.label_))
143  return false;
144  const unsigned long n = coords_.size();
145  if (n != r.coords_.size())
146  return false;
147  for (unsigned long i = 0; i < n; ++i)
148  if (!closeWithinTolerance(coords_[i], r.coords_[i], tol))
149  return false;
150  return true;
151  }
152 } // namespace npstat
std::pair< unsigned, double > linearInterval(double coordinate) const
Definition: GridAxis.cc:71
void initialize()
Definition: GridAxis.cc:12
std::string label_
Definition: GridAxis.h:121
bool isClose(const GridAxis &r, double tol) const
Definition: GridAxis.cc:141
T w() const
bool closeWithinTolerance(const double &a, const double &b, const double &tol)
unsigned npt_
Definition: GridAxis.h:122
char const * label
Exceptions for the npstat namespace.
const std::string & label() const
Definition: GridAxis.h:44
Non-uniformly spaced coordinate sets for use in constructing rectangular grids.
Determine if two doubles are within requested relative tolerance of each other.
std::pair< unsigned, double > getInterval(double coordinate) const
Definition: GridAxis.cc:48
bool write(std::ostream &of) const
Definition: GridAxis.cc:108
std::vector< double > logs_
Definition: GridAxis.h:120
static GridAxis * read(const gs::ClassId &id, std::istream &in)
Definition: GridAxis.cc:117
float x
const std::vector< double > & coords() const
Definition: GridAxis.h:43
bool operator==(const GridAxis &r) const
Definition: GridAxis.cc:137
std::vector< double > coords_
Definition: GridAxis.h:119