npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
NUHistoAxis.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_NUHISTOAXIS_HH_
2 #define NPSTAT_NUHISTOAXIS_HH_
3 
4 /*!
5 // \file NUHistoAxis.hh
6 //
7 // \brief Histogram axis with non-uniform bin spacing
8 //
9 // Author: I. Volobouev
10 //
11 // December 2011
12 */
13 
14 #include <vector>
15 #include <utility>
16 
17 #include "geners/ClassId.hh"
18 #include "npstat/nm/Interval.hh"
19 
20 namespace npstat {
21  template <typename Numeric, class Axis> class HistoND;
22  class DualHistoAxis;
23 
24  /**
25  // This class can be used to create histograms with non-uniform binning
26  */
28  {
29  public:
30  /**
31  // The number of bin edges provided must be at least 2. Edge
32  // coordinates will be sorted internally in the increasing order.
33  // The number of bins will be less by 1 than the number of edges.
34  */
35  NUHistoAxis(const std::vector<double>& binEdges, const char* label = 0);
36 
37  //@{
38  /** Examine axis properties */
39  inline double min() const {return min_;}
40  inline double max() const {return max_;}
41  inline Interval<double> interval() const
42  {return Interval<double>(min_, max_);}
43  inline double length() const {return max_ - min_;}
44  inline unsigned nBins() const {return nBins_;}
45  inline double binWidth(const int binNum) const
46  {return binEdges_.at(binNum+1) - binEdges_.at(binNum);}
47  inline const std::string& label() const {return label_;}
48  inline bool isUniform() const {return uniform_;}
49  //@}
50 
51  /** Return the coordinate of the given bin left edge */
52  inline double leftBinEdge(const int binNum) const
53  {return binEdges_.at(binNum);}
54 
55  /** Return the coordinate of the given bin right edge */
56  inline double rightBinEdge(const int binNum) const
57  {return binEdges_.at(binNum + 1);}
58 
59  /** Return the coordinate of the given bin center */
60  inline double binCenter(const int binNum) const
61  {return 0.5*(binEdges_.at(binNum) + binEdges_.at(binNum + 1));}
62 
63  /** Return the coordinate interval occupied by the given bin */
64  inline Interval<double> binInterval(const int binNum) const
65  {return Interval<double>(binEdges_.at(binNum),
66  binEdges_.at(binNum + 1));}
67 
68  /** Change the axis label */
69  inline void setLabel(const char* newlabel)
70  {label_ = newlabel ? newlabel : "";}
71 
72  /**
73  // This method returns -1 for values below the lower limit and
74  // "nBins()" for values equal to or above the upper limit
75  */
76  int binNumber(double x) const;
77 
78  /**
79  // Floating point bin number given the coordinate. Useful for
80  // interpolation methods and such.
81  */
82  double fltBinNumber(double x, bool mapLeftEdgeTo0=true) const;
83 
84  /**
85  // This method returns the closest valid bin number
86  // (>= 0 and below nBins() )
87  */
88  unsigned closestValidBin(double x) const;
89 
90  bool operator==(const NUHistoAxis&) const;
91  bool operator!=(const NUHistoAxis&) const;
92 
93  /** Comparison of axis coordinates within given tolerance */
94  bool isClose(const NUHistoAxis&, double tol) const;
95 
96  /** Return uniformly rebinned axis */
97  NUHistoAxis rebin(unsigned newBins) const;
98 
99  //@{
100  /** Method related to "geners" I/O */
101  inline gs::ClassId classId() const {return gs::ClassId(*this);}
102  bool write(std::ostream& of) const;
103  //@}
104 
105  static inline const char* classname() {return "npstat::NUHistoAxis";}
106  static inline unsigned version() {return 1;}
107  static NUHistoAxis* read(const gs::ClassId& id, std::istream& in);
108 
109  private:
110  NUHistoAxis(unsigned nBins, double min, double max,
111  const char* label = 0);
112 
113  double min_;
114  double max_;
115  std::vector<double> binEdges_;
116  std::string label_;
117  unsigned nBins_;
118  bool uniform_;
119 
120  template <typename Numeric, class Axis> friend class HistoND;
121  friend class DualHistoAxis;
122 
123  inline unsigned overflowIndex(
124  const double x, unsigned* binNum) const
125  {
126  if (x < min_)
127  return 0U;
128  else if (x >= max_)
129  return 2U;
130  else
131  {
132  *binNum = binNumber(x);
133  return 1U;
134  }
135  }
136 
137 #ifdef SWIG
138  public:
139  inline std::pair<double,double> range() const
140  {return std::pair<double,double>(min_, max_);}
141 #endif // SWIG
142 
143  // SWIG can't instantiate vectors of classes without default
144  // constructors. This is why the default constructor has to be
145  // placed here.
146  inline NUHistoAxis() : min_(0.0), max_(0.0),
147  nBins_(0), uniform_(false) {}
148  };
149 }
150 
151 #endif // NPSTAT_NUHISTOAXIS_HH_
Template to represent intervals in one dimension.
Definition: Interval.hh:29
Definition: NUHistoAxis.hh:28
double leftBinEdge(const int binNum) const
Definition: NUHistoAxis.hh:52
double rightBinEdge(const int binNum) const
Definition: NUHistoAxis.hh:56
bool isClose(const NUHistoAxis &, double tol) const
Interval< double > binInterval(const int binNum) const
Definition: NUHistoAxis.hh:64
NUHistoAxis(const std::vector< double > &binEdges, const char *label=0)
unsigned closestValidBin(double x) const
NUHistoAxis rebin(unsigned newBins) const
void setLabel(const char *newlabel)
Definition: NUHistoAxis.hh:69
double binCenter(const int binNum) const
Definition: NUHistoAxis.hh:60
gs::ClassId classId() const
Definition: NUHistoAxis.hh:101
double min() const
Definition: NUHistoAxis.hh:39
double fltBinNumber(double x, bool mapLeftEdgeTo0=true) const
int binNumber(double x) const
Definition: AbsArrayProjector.hh:14