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
mps_fire.i
i
Definition: mps_fire.py:428
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
GridAxis.h
Non-uniformly spaced coordinate sets for use in constructing rectangular grids.
npstat::GridAxis::npt_
unsigned npt_
Definition: GridAxis.h:122
npstat::GridAxis::operator==
bool operator==(const GridAxis &r) const
Definition: GridAxis.cc:137
npstat::GridAxis::coords
const std::vector< double > & coords() const
Definition: GridAxis.h:43
npstat::GridAxis::initialize
void initialize()
Definition: GridAxis.cc:12
closeWithinTolerance.h
Determine if two doubles are within requested relative tolerance of each other.
npstat
Definition: AbsArrayProjector.h:14
npstat::GridAxis::linearInterval
std::pair< unsigned, double > linearInterval(double coordinate) const
Definition: GridAxis.cc:71
w
const double w
Definition: UKUtility.cc:23
npstat::GridAxis::read
static GridAxis * read(const gs::ClassId &id, std::istream &in)
Definition: GridAxis.cc:117
npstat::NpstatInvalidArgument
Definition: NpstatException.h:38
mitigatedMETSequence_cff.U
U
Definition: mitigatedMETSequence_cff.py:36
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
pfDeepBoostedJetPreprocessParams_cfi.lower_bound
lower_bound
Definition: pfDeepBoostedJetPreprocessParams_cfi.py:15
npstat::GridAxis::useLogSpace_
bool useLogSpace_
Definition: GridAxis.h:123
jetUpdater_cfi.sort
sort
Definition: jetUpdater_cfi.py:29
recoMuon::in
Definition: RecoMuonEnumerators.h:6
npstat::GridAxis::label_
std::string label_
Definition: GridAxis.h:121
npstat::NpstatDomainError
Definition: NpstatException.h:54
cmsLHEtoEOSManager.l
l
Definition: cmsLHEtoEOSManager.py:204
alignCSCRings.r
r
Definition: alignCSCRings.py:93
npstat::GridAxis::getInterval
std::pair< unsigned, double > getInterval(double coordinate) const
Definition: GridAxis.cc:48
npstat::GridAxis::isClose
bool isClose(const GridAxis &r, double tol) const
Definition: GridAxis.cc:141
dqm-mbProfile.log
log
Definition: dqm-mbProfile.py:17
npstat::GridAxis::coords_
std::vector< double > coords_
Definition: GridAxis.h:119
npstat::closeWithinTolerance
bool closeWithinTolerance(const double &a, const double &b, const double &tol)
Definition: closeWithinTolerance.h:25
npstat::GridAxis::write
bool write(std::ostream &of) const
Definition: GridAxis.cc:108
c
auto & c
Definition: CAHitNtupletGeneratorKernelsImpl.h:46
NpstatException.h
Exceptions for the npstat namespace.
npstat::GridAxis::GridAxis
GridAxis()
Definition: GridAxis.h:125
label
const char * label
Definition: PFTauDecayModeTools.cc:11
npstat::GridAxis::logs_
std::vector< double > logs_
Definition: GridAxis.h:120
npstat::GridAxis::label
const std::string & label() const
Definition: GridAxis.h:44
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443
npstat::GridAxis
Definition: GridAxis.h:30