CMS 3D CMS Logo

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, const double min, const double max, const char* label)
14  : min_(min), max_(max), label_(label ? label : ""), npt_(nCoords) {
15  if (!(npt_ > 1U && npt_ < UINT_MAX / 2U - 1U))
17  "In npstat::UniformAxis constructor: "
18  "number of points is out of range");
19  if (min_ > max_)
21  bw_ = (max_ - min_) / (npt_ - 1U);
22  if (max_ == min_)
24  "In npstat::UniformAxis constructor: "
25  "minimum and maximum must be distinct");
26  }
27 
28  std::pair<unsigned, double> UniformAxis::getInterval(const double x) const {
29  if (x <= min_)
30  return std::pair<unsigned, double>(0U, 1.0);
31  else if (x >= max_)
32  return std::pair<unsigned, double>(npt_ - 2U, 0.0);
33  else {
34  unsigned binnum = static_cast<unsigned>(floor((x - min_) / bw_));
35  if (binnum > npt_ - 2U)
36  binnum = npt_ - 2U;
37  double w = binnum + 1.0 - (x - min_) / bw_;
38  if (w < 0.0)
39  w = 0.0;
40  else if (w > 1.0)
41  w = 1.0;
42  return std::pair<unsigned, double>(binnum, w);
43  }
44  }
45 
46  std::pair<unsigned, double> UniformAxis::linearInterval(const double x) const {
47  if (x <= min_)
48  return std::pair<unsigned, double>(0U, 1.0 - (x - min_) / bw_);
49  else if (x >= max_)
50  return std::pair<unsigned, double>(npt_ - 2U, (max_ - x) / bw_);
51  else {
52  unsigned binnum = static_cast<unsigned>(floor((x - min_) / bw_));
53  if (binnum > npt_ - 2U)
54  binnum = npt_ - 2U;
55  double w = binnum + 1.0 - (x - min_) / bw_;
56  if (w < 0.0)
57  w = 0.0;
58  else if (w > 1.0)
59  w = 1.0;
60  return std::pair<unsigned, double>(binnum, w);
61  }
62  }
63 
64  std::vector<double> UniformAxis::coords() const {
65  std::vector<double> vec;
66  vec.reserve(npt_);
67  const unsigned nptm1 = npt_ - 1U;
68  for (unsigned i = 0; i < nptm1; ++i)
69  vec.push_back(min_ + bw_ * i);
70  vec.push_back(max_);
71  return vec;
72  }
73 
74  double UniformAxis::coordinate(const unsigned i) const {
75  if (i >= npt_)
76  throw npstat::NpstatOutOfRange("In npstat::UniformAxis::coordinate: index out of range");
77  if (i == npt_ - 1U)
78  return max_;
79  else
80  return min_ + bw_ * i;
81  }
82 
83  bool UniformAxis::isClose(const UniformAxis& r, const double tol) const {
84  return closeWithinTolerance(min_, r.min_, tol) && closeWithinTolerance(max_, r.max_, tol) && label_ == r.label_ &&
85  npt_ == r.npt_;
86  }
87 
88  bool UniformAxis::operator==(const UniformAxis& r) const {
89  return min_ == r.min_ && max_ == r.max_ && label_ == r.label_ && npt_ == r.npt_;
90  }
91 
92  bool UniformAxis::write(std::ostream& of) const {
93  gs::write_pod(of, min_);
94  gs::write_pod(of, max_);
95  gs::write_pod(of, label_);
96  gs::write_pod(of, npt_);
97  return !of.fail();
98  }
99 
100  UniformAxis* UniformAxis::read(const gs::ClassId& id, std::istream& in) {
101  static const gs::ClassId current(gs::ClassId::makeId<UniformAxis>());
102  current.ensureSameId(id);
103 
104  double min = 0.0, max = 0.0;
106  unsigned nBins = 0;
107 
108  gs::read_pod(in, &min);
109  gs::read_pod(in, &max);
110  gs::read_pod(in, &label);
111  gs::read_pod(in, &nBins);
112 
113  if (!in.fail())
114  return new UniformAxis(nBins, min, max, label.c_str());
115  else
116  throw gs::IOReadFailure(
117  "In npstat::UniformAxis::read: "
118  "input stream failure");
119  }
120 } // namespace npstat
UniformAxis.h
Uniformly spaced coordinate sets for use in constructing rectangular grids.
npstat::UniformAxis::coords
std::vector< double > coords() const
Definition: UniformAxis.cc:64
npstat::UniformAxis::max
double max() const
Definition: UniformAxis.h:35
mps_fire.i
i
Definition: mps_fire.py:428
npstat::UniformAxis
Definition: UniformAxis.h:27
min
T min(T a, T b)
Definition: MathUtil.h:58
npstat::UniformAxis::coordinate
double coordinate(unsigned i) const
Definition: UniformAxis.cc:74
closeWithinTolerance.h
Determine if two doubles are within requested relative tolerance of each other.
npstat::UniformAxis::label
const std::string & label() const
Definition: UniformAxis.h:36
npstat::UniformAxis::getInterval
std::pair< unsigned, double > getInterval(double coordinate) const
Definition: UniformAxis.cc:28
npstat::UniformAxis::max_
double max_
Definition: UniformAxis.h:83
npstat::UniformAxis::operator==
bool operator==(const UniformAxis &r) const
Definition: UniformAxis.cc:88
npstat
Definition: AbsArrayProjector.h:14
std::swap
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
Definition: DataFrameContainer.h:209
w
const double w
Definition: UKUtility.cc:23
npstat::UniformAxis::read
static UniformAxis * read(const gs::ClassId &id, std::istream &in)
Definition: UniformAxis.cc:100
seedmultiplicitymonitor_newtracking_cfi.nBins
nBins
Definition: seedmultiplicitymonitor_newtracking_cfi.py:8
npstat::NpstatInvalidArgument
Definition: NpstatException.h:38
npstat::UniformAxis::label_
std::string label_
Definition: UniformAxis.h:85
mitigatedMETSequence_cff.U
U
Definition: mitigatedMETSequence_cff.py:36
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
npstat::UniformAxis::npt_
unsigned npt_
Definition: UniformAxis.h:86
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
recoMuon::in
Definition: RecoMuonEnumerators.h:6
npstat::UniformAxis::write
bool write(std::ostream &of) const
Definition: UniformAxis.cc:92
alignCSCRings.r
r
Definition: alignCSCRings.py:93
npstat::NpstatOutOfRange
Definition: NpstatException.h:30
npstat::UniformAxis::UniformAxis
UniformAxis()
Definition: UniformAxis.h:88
npstat::UniformAxis::min_
double min_
Definition: UniformAxis.h:82
npstat::UniformAxis::linearInterval
std::pair< unsigned, double > linearInterval(double coordinate) const
Definition: UniformAxis.cc:46
npstat::closeWithinTolerance
bool closeWithinTolerance(const double &a, const double &b, const double &tol)
Definition: closeWithinTolerance.h:25
npstat::UniformAxis::min
double min() const
Definition: UniformAxis.h:34
NpstatException.h
Exceptions for the npstat namespace.
npstat::UniformAxis::bw_
double bw_
Definition: UniformAxis.h:84
label
const char * label
Definition: PFTauDecayModeTools.cc:11
npstat::UniformAxis::isClose
bool isClose(const UniformAxis &r, double tol) const
Definition: UniformAxis.cc:83