npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
GridAxis.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_GRIDAXIS_HH_
2 #define NPSTAT_GRIDAXIS_HH_
3 
4 /*!
5 // \file GridAxis.hh
6 //
7 // \brief Non-uniformly spaced coordinate sets for use in constructing
8 // rectangular grids
9 //
10 // Author: I. Volobouev
11 //
12 // July 2010
13 */
14 
15 #include <vector>
16 #include <utility>
17 #include <string>
18 #include <iostream>
19 
20 #include "geners/ClassId.hh"
21 
22 namespace npstat {
23  /**
24  // Information needed to define an axis of a rectangular grid.
25  // The distance between grid points can change from point to point.
26  //
27  // The UniformAxis class will be more efficient in representing
28  // equidistant grids.
29  */
30  class GridAxis
31  {
32  public:
33  //@{
34  /**
35  // The number of grid coordinates provided must be at least 2.
36  // Coordinates will be sorted internally in the increasing order.
37  */
38  explicit GridAxis(const std::vector<double>& coords,
39  bool useLogSpace=false);
40  GridAxis(const std::vector<double>& coords, const char* label,
41  bool useLogSpace=false);
42  //@}
43 
44  //@{
45  /**
46  // Construct a simple unlabeled grid using explicit point coordinates.
47  // This grid will use linear interpolation (not log space).
48  */
49  GridAxis(double x0, double x1);
50  GridAxis(double x0, double x1, double x2);
51  GridAxis(double x0, double x1, double x2, double x3);
52  GridAxis(double x0, double x1, double x2, double x3, double x4);
53  //@}
54 
55  //@{
56  /** Basic accessor returning a parameter provided in the constructor */
57  inline const std::vector<double>& coords() const {return coords_;}
58  inline const std::string& label() const {return label_;}
59  inline bool usesLogSpace() const {return useLogSpace_;}
60  //@}
61 
62  /**
63  // This method returns the grid interval number and
64  // the weight of the point at the left side of the interval.
65  // The weight will be set to 1 if the given coordinate coincides
66  // with the grid point and will decay to 0 linearly as the
67  // coordinate moves towards the next point on the right.
68  //
69  // The coordinates below the leftmost grid point are mapped
70  // into the 0th interval with weight 1. The coordinates above
71  // the rightmost grid point are mapped into the last interval
72  // with weight 0 for the left point (it is expected that weight 1
73  // will then be assigned to the right point).
74  */
75  std::pair<unsigned,double> getInterval(double coordinate) const;
76 
77  /**
78  // This method returns the grid interval number and
79  // the weight of the point at the left side of the interval.
80  // The weight will be set to 1 if the given coordinate coincides
81  // with the grid point and will decay to 0 linearly as the
82  // coordinate moves towards the next point on the right.
83  // The weight for the point on the right should be set to
84  // one minus the weight on the left.
85  //
86  // The coordinates outside of grid boundaries will result in
87  // weights which are less than zero or more than one. They
88  // will be calculated by linear extrapolation from the closest
89  // interval in the grid (i.e., leftmost or rightmost).
90  */
91  std::pair<unsigned,double> linearInterval(double coordinate) const;
92 
93  //@{
94  /** Convenience accessor */
95  inline unsigned nCoords() const {return npt_;}
96  inline double coordinate(const unsigned i) const
97  {return coords_.at(i);}
98  inline double min() const {return coords_.front();}
99  inline double max() const {return coords_.back();}
100  inline double length() const {return coords_.back() - coords_.front();}
101  inline bool isUniform() const {return false;}
102  inline unsigned nIntervals() const {return coords_.size() - 1;}
103  inline double intervalWidth(const unsigned i=0) const
104  {return coords_.at(i+1) - coords_.at(i);}
105  //@}
106 
107  /** Compare two grids for equality */
108  bool operator==(const GridAxis& r) const;
109 
110  /** Logical negation of operator== */
111  inline bool operator!=(const GridAxis& r) const
112  {return !(*this == r);}
113 
114  /**
115  // Check for closeness of coordinates with another axis
116  // within the given relative tolerance
117  */
118  bool isClose(const GridAxis& r, double tol) const;
119 
120  /** Modify the axis label */
121  inline void setLabel(const char* newlabel)
122  {label_ = newlabel ? newlabel : "";}
123 
124  //@{
125  /** Method related to "geners" I/O */
126  inline gs::ClassId classId() const {return gs::ClassId(*this);}
127  bool write(std::ostream& of) const;
128  //@}
129 
130  static inline const char* classname() {return "npstat::GridAxis";}
131  static inline unsigned version() {return 2;}
132  static GridAxis* read(const gs::ClassId& id, std::istream& in);
133 
134  private:
135  void initialize();
136 
137  std::vector<double> coords_;
138  std::vector<double> logs_;
139  std::string label_;
140  unsigned npt_;
141  bool useLogSpace_;
142 
143 #ifdef SWIG
144  public:
145  inline std::pair<double,double> range() const
146  {return std::pair<double,double>(min(), max());}
147 #endif // SWIG
148 
149  inline GridAxis() : npt_(0), useLogSpace_(false) {}
150  };
151 }
152 
153 #endif // NPSTAT_GRIDAXIS_HH_
Definition: GridAxis.hh:31
GridAxis(const std::vector< double > &coords, bool useLogSpace=false)
gs::ClassId classId() const
Definition: GridAxis.hh:126
unsigned nCoords() const
Definition: GridAxis.hh:95
bool isClose(const GridAxis &r, double tol) const
GridAxis(double x0, double x1)
bool operator==(const GridAxis &r) const
bool operator!=(const GridAxis &r) const
Definition: GridAxis.hh:111
std::pair< unsigned, double > linearInterval(double coordinate) const
void setLabel(const char *newlabel)
Definition: GridAxis.hh:121
const std::vector< double > & coords() const
Definition: GridAxis.hh:57
std::pair< unsigned, double > getInterval(double coordinate) const
Definition: AbsArrayProjector.hh:14