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
float Scalar
Definition: Grid1D.h:9
int cells() const
Definition: Grid1D.h:22
Scalar upper() const
Definition: Grid1D.h:20
int edges_
Definition: Grid1D.h:69
void normalize(int &ind, Scalar &f) const
Definition: Grid1D.h:36
Scalar lower() const
Definition: Grid1D.h:19
Scalar step() const
Definition: Grid1D.h:18
int index(Scalar a, Scalar &f) const
Definition: Grid1D.h:29
Scalar stepinv_
Definition: Grid1D.h:66
Grid1D()
Definition: Grid1D.h:12
Scalar node(int i) const
Definition: Grid1D.h:24
#define dso_internal
Definition: Visibility.h:13
double f[11][100]
Scalar closestNode(Scalar a) const
Definition: Grid1D.h:46
bool inRange(int i) const
Definition: Grid1D.h:26
int nodes() const
Definition: Grid1D.h:21
Definition: Grid1D.h:7
double b
Definition: hdecay.h:118
double a
Definition: hdecay.h:119
Grid1D(Scalar lower, Scalar upper, int nodes)
Definition: Grid1D.h:14
Scalar upper_
Definition: Grid1D.h:68
step
Definition: StallMonitor.cc:98
int index(Scalar a) const
returns valid index, or -1 if the value is outside range +/- one cell.
Definition: Grid1D.h:54
tmp
align.sh
Definition: createJobs.py:716
Scalar lower_
Definition: Grid1D.h:67