CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
NUHistoAxis.cc
Go to the documentation of this file.
1 #include <cmath>
2 #include <cassert>
3 #include <climits>
5 #include <algorithm>
6 
7 #include "Alignment/Geners/interface/binaryIO.hh"
8 #include "Alignment/Geners/interface/IOException.hh"
10 
13 
14 namespace npstat {
15  NUHistoAxis::NUHistoAxis(const std::vector<double>& binEdges,
16  const char* label)
17  : binEdges_(binEdges), nBins_(binEdges.size() - 1U), uniform_(false)
18  {
19  if (!(binEdges_.size() > 1U && binEdges_.size() < UINT_MAX/2U))
20  throw npstat::NpstatInvalidArgument("In npstat::NUHistoAxis constructor: "
21  "number of bin edges is out of range");
22  std::sort(binEdges_.begin(), binEdges_.end());
23  min_ = binEdges_[0];
25  if (label)
26  label_ = std::string(label);
27  }
28 
29  NUHistoAxis::NUHistoAxis(const unsigned nBins,
30  const double min, const double max,
31  const char* label)
32  : min_(min), max_(max), nBins_(nBins), uniform_(true)
33  {
34  if (!(nBins_ && nBins_ < UINT_MAX/2U - 1U))
35  throw npstat::NpstatInvalidArgument("In npstat::NUHistoAxis constructor: "
36  "number of bins is out of range");
37  if (min_ > max_)
40  if (label)
41  label_ = std::string(label);
42  }
43 
44  bool NUHistoAxis::isClose(const NUHistoAxis& r, const double tol) const
45  {
46  if (!(closeWithinTolerance(min_, r.min_, tol) &&
47  closeWithinTolerance(max_, r.max_, tol) &&
48  label_ == r.label_ &&
49  nBins_ == r.nBins_ &&
50  uniform_ == r.uniform_))
51  return false;
52  for (unsigned i=0; i<nBins_; ++i)
53  if (!closeWithinTolerance(binEdges_[i], r.binEdges_[i], tol))
54  return false;
55  return true;
56  }
57 
59  {
60  return min_ == r.min_ &&
61  max_ == r.max_ &&
62  label_ == r.label_ &&
63  nBins_ == r.nBins_ &&
64  binEdges_ == r.binEdges_ &&
65  uniform_ == r.uniform_;
66  }
67 
69  {
70  return !(*this == r);
71  }
72 
73  int NUHistoAxis::binNumber(const double x) const
74  {
75  const int delta = std::upper_bound(binEdges_.begin(), binEdges_.end(), x) -
76  binEdges_.begin();
77  return delta - 1;
78  }
79 
80  double NUHistoAxis::fltBinNumber(const double x, const bool mapLeftEdgeTo0) const
81  {
82  const int delta = std::upper_bound(binEdges_.begin(), binEdges_.end(), x) -
83  binEdges_.begin();
84  const int binnum = delta - 1;
85 
86  if (binnum < 0)
87  {
88  const double left = binEdges_[0];
89  const double right = binEdges_[1];
90  double bval = (x - left)/(right - left);
91  if (!mapLeftEdgeTo0)
92  bval -= 0.5;
93  if (bval < -1.0)
94  bval = -1.0;
95  return bval;
96  }
97  else if (static_cast<unsigned>(binnum) >= nBins_)
98  {
99  const double left = binEdges_[nBins_ - 1U];
100  const double right = binEdges_[nBins_];
101  double bval = nBins_ - 1U + (x - left)/(right - left);
102  if (!mapLeftEdgeTo0)
103  bval -= 0.5;
104  if (bval > static_cast<double>(nBins_))
105  bval = nBins_;
106  return bval;
107  }
108  else
109  {
110  const double left = binEdges_[binnum];
111  const double right = binEdges_[delta];
112  if (mapLeftEdgeTo0)
113  return binnum + (x - left)/(right - left);
114  else
115  {
116  // Bin center is mapped to binnum.
117  // Bin center of the next bin is mapped to binnum + 1.
118  // Bin center of the previos bin is mapped to binnum - 1.
119  const double binCenter = (left + right)/2.0;
120  if ((binnum == 0 && x <= binCenter) ||
121  (static_cast<unsigned>(binnum) == nBins_ - 1 && x >= binCenter))
122  return binnum + (x - left)/(right - left) - 0.5;
123  else if (x <= binCenter)
124  {
125  const double otherBinCenter = (left + binEdges_[binnum - 1])/2.0;
126  return binnum + (x - binCenter)/(binCenter - otherBinCenter);
127  }
128  else
129  {
130  const double otherBinCenter = (right + binEdges_[binnum + 1])/2.0;
131  return binnum + (x - binCenter)/(otherBinCenter - binCenter);
132  }
133  }
134  }
135  }
136 
137  unsigned NUHistoAxis::closestValidBin(const double x) const
138  {
139  const int delta = std::upper_bound(binEdges_.begin(), binEdges_.end(), x) -
140  binEdges_.begin();
141  int binnum = delta - 1;
142  if (binnum < 0)
143  binnum = 0;
144  else if (static_cast<unsigned>(binnum) >= nBins_)
145  binnum = nBins_ - 1U;
146  return binnum;
147  }
148 
149  bool NUHistoAxis::write(std::ostream& of) const
150  {
151  gs::write_pod_vector(of, binEdges_);
152  gs::write_pod(of, label_);
153  unsigned char c = uniform_;
154  gs::write_pod(of, c);
155  return !of.fail();
156  }
157 
158  NUHistoAxis* NUHistoAxis::read(const gs::ClassId& id, std::istream& in)
159  {
160  static const gs::ClassId current(gs::ClassId::makeId<NUHistoAxis>());
161  current.ensureSameId(id);
162 
163  std::vector<double> binEdges;
165  unsigned char unif;
166  gs::read_pod_vector(in, &binEdges);
167  gs::read_pod(in, &label);
168  gs::read_pod(in, &unif);
169  if (in.fail())
170  throw gs::IOReadFailure("In npstat::UHistoAxis::read: "
171  "input stream failure");
172  NUHistoAxis* result = new NUHistoAxis(binEdges, label.c_str());
173  result->uniform_ = unif;
174  return result;
175  }
176 }
dbl * delta
Definition: mlp_gen.cc:36
unsigned closestValidBin(double x) const
Definition: NUHistoAxis.cc:137
std::vector< double > binEdges_
Definition: NUHistoAxis.h:116
int i
Definition: DBlmapReader.cc:9
int binNumber(double x) const
Definition: NUHistoAxis.cc:73
bool write(std::ostream &of) const
Definition: NUHistoAxis.cc:149
bool closeWithinTolerance(const double &a, const double &b, const double &tol)
Histogram axis with non-uniform bin spacing.
bool operator!=(const NUHistoAxis &) const
Definition: NUHistoAxis.cc:68
Equidistant sequences of points in either linear or log space.
Exceptions for the npstat namespace.
std::string label_
Definition: NUHistoAxis.h:117
const T & max(const T &a, const T &b)
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
tuple result
Definition: query.py:137
Determine if two doubles are within requested relative tolerance of each other.
double binCenter(const int binNum) const
Definition: NUHistoAxis.h:61
static NUHistoAxis * read(const gs::ClassId &id, std::istream &in)
Definition: NUHistoAxis.cc:158
bool isClose(const NUHistoAxis &, double tol) const
Definition: NUHistoAxis.cc:44
double fltBinNumber(double x, bool mapLeftEdgeTo0=true) const
Definition: NUHistoAxis.cc:80
bool operator==(const NUHistoAxis &) const
Definition: NUHistoAxis.cc:58
volatile std::atomic< bool > shutdown_flag false
Definition: DDAxes.h:10
tuple size
Write out results.
const std::string & label() const
Definition: NUHistoAxis.h:48