CMS 3D CMS Logo

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