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 
10  typedef float Scalar;
11  // typedef double Scalar;
12 
13  Grid1D() {}
14 
15  Grid1D( Scalar lower, Scalar upper, int nodes) :
16  lower_(lower), upper_(upper), edges_(nodes-2) {
17  stepinv_ = (nodes-1)/(upper - lower);
18  }
19 
20 
21  Scalar step() const {return 1./stepinv_;}
22  Scalar lower() const {return lower_;}
23  Scalar upper() const {return upper_;}
24  int nodes() const {return edges_+2;}
25  int cells() const {return edges_+1;}
26 
27  Scalar node( int i) const { return i*step() + lower();}
28 
29  bool inRange(int i) const {
30  return i>=0 && i<=edges_;
31  }
32 
33  // return index and fractional part...
34  int index(Scalar a, Scalar & f) const {
35  Scalar b;
36  f = modff((a-lower())*stepinv_, &b);
37  return b;
38  }
39 
40  // move index and fraction in range..
41  void normalize(int & ind, Scalar & f) const {
42  if (ind<0) {
43  f -= ind;
44  ind = 0;
45  }
46  else if (ind>edges_) {
47  f += ind-edges_;
48  ind = edges_;
49  }
50  }
51 
52 
53  Scalar closestNode( Scalar a) const {
54  Scalar b = (a-lower())/step();
55  Scalar c = floor(b);
56  Scalar tmp = (b-c < 0.5) ? std::max(c,0.f) : std::min(c+1.f,static_cast<Scalar>(nodes()-1));
57  return tmp*step()+lower();
58  }
59 
61  int index( Scalar a) const {
62  int ind = static_cast<int>((a-lower())/step());
63  // FIXME: this causes an exception to be thrown later. Should be tested
64  // more carefully before release
65  // if (ind < -1 || ind > cells()) {
66  // std::cout << "**** ind = " << ind << " cells: " << cells() << std::endl;
67  // return -1;
68  // }
69  return std::max(0, std::min( cells()-1, ind));
70  }
71 
72 
73 private:
74 
75  Scalar stepinv_;
76  Scalar lower_;
77  Scalar upper_;
78  int edges_; // number of lower edges = nodes-2...
79 
80 };
81 
82 #endif
Scalar step() const
Definition: Grid1D.h:21
float Scalar
Definition: Grid1D.h:10
int edges_
Definition: Grid1D.h:78
int index(Scalar a) const
returns valid index, or -1 if the value is outside range +/- one cell.
Definition: Grid1D.h:61
bool inRange(int i) const
Definition: Grid1D.h:29
Scalar node(int i) const
Definition: Grid1D.h:27
Scalar stepinv_
Definition: Grid1D.h:75
Grid1D()
Definition: Grid1D.h:13
int nodes() const
Definition: Grid1D.h:24
double f[11][100]
int cells() const
Definition: Grid1D.h:25
T min(T a, T b)
Definition: MathUtil.h:58
Scalar lower() const
Definition: Grid1D.h:22
Definition: Grid1D.h:7
Scalar closestNode(Scalar a) const
Definition: Grid1D.h:53
double b
Definition: hdecay.h:120
#define dso_internal
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
double a
Definition: hdecay.h:121
void normalize(int &ind, Scalar &f) const
Definition: Grid1D.h:41
Grid1D(Scalar lower, Scalar upper, int nodes)
Definition: Grid1D.h:15
Scalar upper_
Definition: Grid1D.h:77
step
int index(Scalar a, Scalar &f) const
Definition: Grid1D.h:34
Scalar lower_
Definition: Grid1D.h:76
Scalar upper() const
Definition: Grid1D.h:23