CMS 3D CMS Logo

Grid1D.h
Go to the documentation of this file.
1 #ifndef Grid1D_H
2 #define Grid1D_H
3 #include <cmath>
4 #include <algorithm>
6 
8 public:
9  typedef float Scalar;
10  // typedef double Scalar;
11 
12  Grid1D() {}
13 
14  Grid1D(Scalar lower, Scalar upper, int nodes) : lower_(lower), upper_(upper), edges_(nodes - 2) {
15  stepinv_ = (nodes - 1) / (upper - lower);
16  }
17 
18  Scalar step() const { return 1. / stepinv_; }
19  Scalar lower() const { return lower_; }
20  Scalar upper() const { return upper_; }
21  int nodes() const { return edges_ + 2; }
22  int cells() const { return edges_ + 1; }
23 
24  Scalar node(int i) const { return i * step() + lower(); }
25 
26  bool inRange(int i) const { return i >= 0 && i <= edges_; }
27 
28  // return index and fractional part...
29  int index(Scalar a, Scalar& f) const {
30  Scalar b;
31  f = modff((a - lower()) * stepinv_, &b);
32  return b;
33  }
34 
35  // move index and fraction in range..
36  void normalize(int& ind, Scalar& f) const {
37  if (ind < 0) {
38  f -= ind;
39  ind = 0;
40  } else if (ind > edges_) {
41  f += ind - edges_;
42  ind = edges_;
43  }
44  }
45 
47  Scalar b = (a - lower()) / step();
48  Scalar c = floor(b);
49  Scalar tmp = (b - c < 0.5) ? std::max(c, 0.f) : std::min(c + 1.f, static_cast<Scalar>(nodes() - 1));
50  return tmp * step() + lower();
51  }
52 
54  int index(Scalar a) const {
55  int ind = static_cast<int>((a - lower()) / step());
56  // FIXME: this causes an exception to be thrown later. Should be tested
57  // more carefully before release
58  // if (ind < -1 || ind > cells()) {
59  // std::cout << "**** ind = " << ind << " cells: " << cells() << std::endl;
60  // return -1;
61  // }
62  return std::max(0, std::min(cells() - 1, ind));
63  }
64 
65 private:
69  int edges_; // number of lower edges = nodes-2...
70 };
71 
72 #endif
Grid1D::closestNode
Scalar closestNode(Scalar a) const
Definition: Grid1D.h:46
Visibility.h
mps_fire.i
i
Definition: mps_fire.py:355
Grid1D::lower_
Scalar lower_
Definition: Grid1D.h:67
step
step
Definition: StallMonitor.cc:94
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
min
T min(T a, T b)
Definition: MathUtil.h:58
Grid1D::inRange
bool inRange(int i) const
Definition: Grid1D.h:26
Grid1D::nodes
int nodes() const
Definition: Grid1D.h:21
Grid1D::upper
Scalar upper() const
Definition: Grid1D.h:20
createJobs.tmp
tmp
align.sh
Definition: createJobs.py:716
Grid1D::index
int index(Scalar a) const
returns valid index, or -1 if the value is outside range +/- one cell.
Definition: Grid1D.h:54
Grid1D
Definition: Grid1D.h:7
Grid1D::Grid1D
Grid1D(Scalar lower, Scalar upper, int nodes)
Definition: Grid1D.h:14
class-composition.nodes
nodes
Definition: class-composition.py:75
Grid1D::step
Scalar step() const
Definition: Grid1D.h:18
Grid1D::index
int index(Scalar a, Scalar &f) const
Definition: Grid1D.h:29
b
double b
Definition: hdecay.h:118
a
double a
Definition: hdecay.h:119
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
dso_internal
#define dso_internal
Definition: Visibility.h:13
Grid1D::Scalar
float Scalar
Definition: Grid1D.h:9
HltBtagPostValidation_cff.c
c
Definition: HltBtagPostValidation_cff.py:31
Grid1D::upper_
Scalar upper_
Definition: Grid1D.h:68
postprocess-scan-build.cells
cells
Definition: postprocess-scan-build.py:13
pileupCalc.upper
upper
Definition: pileupCalc.py:214
Grid1D::node
Scalar node(int i) const
Definition: Grid1D.h:24
Grid1D::stepinv_
Scalar stepinv_
Definition: Grid1D.h:66
Grid1D::cells
int cells() const
Definition: Grid1D.h:22
Grid1D::Grid1D
Grid1D()
Definition: Grid1D.h:12
Grid1D::edges_
int edges_
Definition: Grid1D.h:69
Grid1D::normalize
void normalize(int &ind, Scalar &f) const
Definition: Grid1D.h:36
Grid1D::lower
Scalar lower() const
Definition: Grid1D.h:19