CMS 3D CMS Logo

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, const double max, const char* label)
14  : min_(min), max_(max), label_(label ? label : ""), nBins_(nbins) {
15  if (!(nBins_ && nBins_ < UINT_MAX / 2U - 1U))
17  "In npstat::HistoAxis constructor: "
18  "number of bins is out of range");
19  if (min_ > max_)
21  bw_ = (max_ - min_) / nBins_;
22  }
23 
24  HistoAxis HistoAxis::rebin(const unsigned nbins) const { return HistoAxis(nbins, min_, max_, label_.c_str()); }
25 
26  bool HistoAxis::isClose(const HistoAxis& r, const double tol) const {
27  return closeWithinTolerance(min_, r.min_, tol) && closeWithinTolerance(max_, r.max_, tol) && label_ == r.label_ &&
28  nBins_ == r.nBins_;
29  }
30 
31  bool HistoAxis::operator==(const HistoAxis& r) const {
32  return min_ == r.min_ && max_ == r.max_ && label_ == r.label_ && nBins_ == r.nBins_;
33  }
34 
35  bool HistoAxis::operator!=(const HistoAxis& r) const { return !(*this == r); }
36 
37  int HistoAxis::binNumber(const double x) const {
38  if (bw_) {
39  int binnum = static_cast<int>(floor((x - min_) / bw_));
40  if (x >= max_) {
41  if (binnum < static_cast<int>(nBins_))
42  binnum = nBins_;
43  } else {
44  if (binnum >= static_cast<int>(nBins_))
45  binnum = nBins_ - 1U;
46  }
47  return binnum;
48  } else {
49  if (x < min_)
50  return -1;
51  else
52  return nBins_;
53  }
54  }
55 
56  unsigned HistoAxis::closestValidBin(const double x) const {
57  if (x <= min_)
58  return 0U;
59  else if (bw_ && x < max_) {
60  const unsigned binnum = static_cast<unsigned>(floor((x - min_) / bw_));
61  if (binnum < nBins_)
62  return binnum;
63  }
64  return nBins_ - 1U;
65  }
66 
67  LinearMapper1d HistoAxis::binNumberMapper(const bool mapLeftEdgeTo0) const {
68  if (!bw_)
70  "In npstat::HistoAxis::binNumberMapper: "
71  "bin width is zero. Mapper can not be constructed.");
72  const double base = mapLeftEdgeTo0 ? min_ / bw_ : min_ / bw_ + 0.5;
73  return LinearMapper1d(1.0 / bw_, -base);
74  }
75 
76  CircularMapper1d HistoAxis::kernelScanMapper(const bool doubleRange) const {
77  if (!bw_)
79  "In npstat::HistoAxis::kernelScanMapper: "
80  "bin width is zero. Mapper can not be constructed.");
81  double range = max_ - min_;
82  if (doubleRange)
83  range *= 2.0;
84  return CircularMapper1d(bw_, 0.0, range);
85  }
86 
87  unsigned HistoAxis::overflowIndexWeighted(const double x, unsigned* binNumber, double* weight) const {
88  if (x < min_)
89  return 0U;
90  else if (x >= max_)
91  return 2U;
92  else {
93  if (nBins_ <= 1U)
95  "In npstat::HistoAxis::overflowIndexWeighted: "
96  "must have more than one bin");
97  const double dbin = (x - min_) / bw_;
98  if (dbin <= 0.5) {
99  *binNumber = 0;
100  *weight = 1.0;
101  } else if (dbin >= nBins_ - 0.5) {
102  *binNumber = nBins_ - 2;
103  *weight = 0.0;
104  } else {
105  const unsigned bin = static_cast<unsigned>(dbin - 0.5);
106  *binNumber = bin >= nBins_ - 1U ? nBins_ - 2U : bin;
107  *weight = 1.0 - (dbin - 0.5 - *binNumber);
108  }
109  return 1U;
110  }
111  }
112 
113  bool HistoAxis::write(std::ostream& of) const {
114  gs::write_pod(of, min_);
115  gs::write_pod(of, max_);
116  gs::write_pod(of, label_);
117  gs::write_pod(of, nBins_);
118  return !of.fail();
119  }
120 
121  HistoAxis* HistoAxis::read(const gs::ClassId& id, std::istream& in) {
122  static const gs::ClassId current(gs::ClassId::makeId<HistoAxis>());
123  current.ensureSameId(id);
124 
125  double min = 0.0, max = 0.0;
127  unsigned nBins = 0;
128 
129  gs::read_pod(in, &min);
130  gs::read_pod(in, &max);
131  gs::read_pod(in, &label);
132  gs::read_pod(in, &nBins);
133 
134  if (!in.fail())
135  return new HistoAxis(nBins, min, max, label.c_str());
136  else
137  throw gs::IOReadFailure(
138  "In npstat::HistoAxis::read: "
139  "input stream failure");
140  }
141 } // namespace npstat
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
npstat::HistoAxis::nBins
unsigned nBins() const
Definition: HistoAxis.h:45
HistoAxis.h
Histogram axis with equidistant bins.
npstat::HistoAxis::rebin
HistoAxis rebin(unsigned newBins) const
Definition: HistoAxis.cc:24
npstat::HistoAxis::isClose
bool isClose(const HistoAxis &, double tol) const
Definition: HistoAxis.cc:26
npstat::HistoAxis::operator==
bool operator==(const HistoAxis &) const
Definition: HistoAxis.cc:31
npstat::HistoAxis::min
double min() const
Definition: HistoAxis.h:41
min
T min(T a, T b)
Definition: MathUtil.h:58
npstat::LinearMapper1d
Definition: LinearMapper1d.h:18
closeWithinTolerance.h
Determine if two doubles are within requested relative tolerance of each other.
npstat::HistoAxis::operator!=
bool operator!=(const HistoAxis &) const
Definition: HistoAxis.cc:35
npstat
Definition: AbsArrayProjector.h:14
npstat::HistoAxis::write
bool write(std::ostream &of) const
Definition: HistoAxis.cc:113
std::swap
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
Definition: DataFrameContainer.h:209
LaserClient_cfi.nbins
nbins
Definition: LaserClient_cfi.py:51
npstat::NpstatInvalidArgument
Definition: NpstatException.h:38
npstat::HistoAxis::min_
double min_
Definition: HistoAxis.h:126
npstat::HistoAxis::max
double max() const
Definition: HistoAxis.h:42
npstat::HistoAxis::label_
std::string label_
Definition: HistoAxis.h:129
mitigatedMETSequence_cff.U
U
Definition: mitigatedMETSequence_cff.py:36
npstat::HistoAxis::overflowIndexWeighted
unsigned overflowIndexWeighted(double x, unsigned *binNumber, double *weight) const
Definition: HistoAxis.cc:87
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
npstat::HistoAxis::label
const std::string & label() const
Definition: HistoAxis.h:47
npstat::HistoAxis
Definition: HistoAxis.h:31
npstat::HistoAxis::nBins_
unsigned nBins_
Definition: HistoAxis.h:130
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
npstat::HistoAxis::max_
double max_
Definition: HistoAxis.h:127
plotT0FromHistos.binNumber
def binNumber(station, sl)
Definition: plotT0FromHistos.py:6
recoMuon::in
Definition: RecoMuonEnumerators.h:6
npstat::HistoAxis::binNumberMapper
LinearMapper1d binNumberMapper(bool mapLeftEdgeTo0=true) const
Definition: HistoAxis.cc:67
npstat::HistoAxis::read
static HistoAxis * read(const gs::ClassId &id, std::istream &in)
Definition: HistoAxis.cc:121
npstat::HistoAxis::closestValidBin
unsigned closestValidBin(double x) const
Definition: HistoAxis.cc:56
npstat::NpstatDomainError
Definition: NpstatException.h:54
alignCSCRings.r
r
Definition: alignCSCRings.py:93
newFWLiteAna.bin
bin
Definition: newFWLiteAna.py:161
npstat::HistoAxis::bw_
double bw_
Definition: HistoAxis.h:128
npstat::HistoAxis::kernelScanMapper
CircularMapper1d kernelScanMapper(bool doubleRange) const
Definition: HistoAxis.cc:76
npstat::closeWithinTolerance
bool closeWithinTolerance(const double &a, const double &b, const double &tol)
Definition: closeWithinTolerance.h:25
NpstatException.h
Exceptions for the npstat namespace.
newFWLiteAna.base
base
Definition: newFWLiteAna.py:92
npstat::CircularMapper1d
Definition: CircularMapper1d.h:23
label
const char * label
Definition: PFTauDecayModeTools.cc:11
weight
Definition: weight.py:1
npstat::HistoAxis::HistoAxis
HistoAxis()
Definition: HistoAxis.h:149
npstat::HistoAxis::binNumber
int binNumber(double x) const
Definition: HistoAxis.cc:37