CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HistoAxis.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  HistoAxis::HistoAxis(const unsigned nbins, const double min,
14  const double max, const char* label)
15  : min_(min), max_(max), label_(label ? label : ""), nBins_(nbins)
16  {
17  if (!(nBins_ && nBins_ < UINT_MAX/2U - 1U))
18  throw npstat::NpstatInvalidArgument("In npstat::HistoAxis constructor: "
19  "number of bins is out of range");
20  if (min_ > max_)
22  bw_ = (max_ - min_)/nBins_;
23  }
24 
25  bool HistoAxis::isClose(const HistoAxis& r, const double tol) const
26  {
27  return closeWithinTolerance(min_, r.min_, tol) &&
28  closeWithinTolerance(max_, r.max_, tol) &&
29  label_ == r.label_ &&
30  nBins_ == r.nBins_;
31  }
32 
33  bool HistoAxis::operator==(const HistoAxis& r) const
34  {
35  return min_ == r.min_ &&
36  max_ == r.max_ &&
37  label_ == r.label_ &&
38  nBins_ == r.nBins_;
39  }
40 
41  bool HistoAxis::operator!=(const HistoAxis& r) const
42  {
43  return !(*this == r);
44  }
45 
46  int HistoAxis::binNumber(const double x) const
47  {
48  if (bw_)
49  {
50  int binnum = static_cast<int>(floor((x - min_)/bw_));
51  if (x >= max_)
52  {
53  if (binnum < static_cast<int>(nBins_))
54  binnum = nBins_;
55  }
56  else
57  {
58  if (binnum >= static_cast<int>(nBins_))
59  binnum = nBins_ - 1U;
60  }
61  return binnum;
62  }
63  else
64  {
65  if (x < min_)
66  return -1;
67  else
68  return nBins_;
69  }
70  }
71 
72  unsigned HistoAxis::closestValidBin(const double x) const
73  {
74  if (x <= min_)
75  return 0U;
76  else if (bw_ && x < max_)
77  {
78  const unsigned binnum = static_cast<unsigned>(floor((x-min_)/bw_));
79  if (binnum < nBins_)
80  return binnum;
81  }
82  return nBins_ - 1U;
83  }
84 
85  LinearMapper1d HistoAxis::binNumberMapper(const bool mapLeftEdgeTo0) const
86  {
87  if (!bw_) throw npstat::NpstatDomainError(
88  "In npstat::HistoAxis::binNumberMapper: "
89  "bin width is zero. Mapper can not be constructed.");
90  const double base = mapLeftEdgeTo0 ? min_/bw_ : min_/bw_ + 0.5;
91  return LinearMapper1d(1.0/bw_, -base);
92  }
93 
94  CircularMapper1d HistoAxis::kernelScanMapper(const bool doubleRange) const
95  {
96  if (!bw_) throw npstat::NpstatDomainError(
97  "In npstat::HistoAxis::kernelScanMapper: "
98  "bin width is zero. Mapper can not be constructed.");
99  double range = max_ - min_;
100  if (doubleRange)
101  range *= 2.0;
102  return CircularMapper1d(bw_, 0.0, range);
103  }
104 
106  const double x, unsigned* binNumber, double *weight) const
107  {
108  if (x < min_)
109  return 0U;
110  else if (x >= max_)
111  return 2U;
112  else
113  {
114  if (nBins_ <= 1U) throw npstat::NpstatInvalidArgument(
115  "In npstat::HistoAxis::overflowIndexWeighted: "
116  "must have more than one bin");
117  const double dbin = (x - min_)/bw_;
118  if (dbin <= 0.5)
119  {
120  *binNumber = 0;
121  *weight = 1.0;
122  }
123  else if (dbin >= nBins_ - 0.5)
124  {
125  *binNumber = nBins_ - 2;
126  *weight = 0.0;
127  }
128  else
129  {
130  const unsigned bin = static_cast<unsigned>(dbin - 0.5);
131  *binNumber = bin >= nBins_ - 1U ? nBins_ - 2U : bin;
132  *weight = 1.0 - (dbin - 0.5 - *binNumber);
133  }
134  return 1U;
135  }
136  }
137 
138  bool HistoAxis::write(std::ostream& of) const
139  {
140  gs::write_pod(of, min_);
141  gs::write_pod(of, max_);
142  gs::write_pod(of, label_);
143  gs::write_pod(of, nBins_);
144  return !of.fail();
145  }
146 
147  HistoAxis* HistoAxis::read(const gs::ClassId& id, std::istream& in)
148  {
149  static const gs::ClassId current(gs::ClassId::makeId<HistoAxis>());
150  current.ensureSameId(id);
151 
152  double min = 0.0, max = 0.0;
154  unsigned nBins = 0;
155 
156  gs::read_pod(in, &min);
157  gs::read_pod(in, &max);
158  gs::read_pod(in, &label);
159  gs::read_pod(in, &nBins);
160 
161  if (!in.fail())
162  return new HistoAxis(nBins, min, max, label.c_str());
163  else
164  throw gs::IOReadFailure("In npstat::HistoAxis::read: "
165  "input stream failure");
166  }
167 }
tuple base
Main Program
Definition: newFWLiteAna.py:92
bool closeWithinTolerance(const double &a, const double &b, const double &tol)
int binNumber(double x) const
Definition: HistoAxis.cc:46
const std::string & label() const
Definition: HistoAxis.h:49
std::string label_
Definition: HistoAxis.h:133
bool operator!=(const HistoAxis &) const
Definition: HistoAxis.cc:41
bool write(std::ostream &of) const
Definition: HistoAxis.cc:138
Exceptions for the npstat namespace.
const T & max(const T &a, const T &b)
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
static HistoAxis * read(const gs::ClassId &id, std::istream &in)
Definition: HistoAxis.cc:147
unsigned nBins() const
Definition: HistoAxis.h:47
Determine if two doubles are within requested relative tolerance of each other.
bool isClose(const HistoAxis &, double tol) const
Definition: HistoAxis.cc:25
CircularMapper1d kernelScanMapper(bool doubleRange) const
Definition: HistoAxis.cc:94
unsigned overflowIndexWeighted(double x, unsigned *binNumber, double *weight) const
Definition: HistoAxis.cc:105
bool operator==(const HistoAxis &) const
Definition: HistoAxis.cc:33
LinearMapper1d binNumberMapper(bool mapLeftEdgeTo0=true) const
Definition: HistoAxis.cc:85
double max() const
Definition: HistoAxis.h:43
unsigned nBins_
Definition: HistoAxis.h:134
Definition: DDAxes.h:10
int weight
Definition: histoStyle.py:50
unsigned closestValidBin(double x) const
Definition: HistoAxis.cc:72
Histogram axis with equidistant bins.
double min() const
Definition: HistoAxis.h:42