npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
HistoAxis.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_HISTOAXIS_HH_
2 #define NPSTAT_HISTOAXIS_HH_
3 
4 /*!
5 // \file HistoAxis.hh
6 //
7 // \brief Histogram axis with equidistant bins
8 //
9 // Author: I. Volobouev
10 //
11 // July 2010
12 */
13 
14 #include <utility>
15 
16 #include "geners/ClassId.hh"
17 
19 #include "npstat/nm/Interval.hh"
20 
21 namespace npstat {
22  template <typename Numeric, class Axis> class HistoND;
23  class DualHistoAxis;
24 
25  /**
26  // Class which contain the information needed to define a histogram axis.
27  // All bins will have the same width. See NUHistoAxis and DualHistoAxis
28  // classes for non-uniform binning.
29  */
30  class HistoAxis
31  {
32  public:
33  /**
34  // Minimum and maximum will be internally swapped
35  // if the minimum parameter is larger than the maximum
36  */
37  HistoAxis(unsigned nBins, double min, double max,
38  const char* label=0);
39 
40  //@{
41  /** Examine axis properties */
42  inline double min() const {return min_;}
43  inline double max() const {return max_;}
44  inline Interval<double> interval() const
45  {return Interval<double>(min_, max_);}
46  inline double length() const {return max_ - min_;}
47  inline unsigned nBins() const {return nBins_;}
48  inline double binWidth(const int /*binNum*/=0) const {return bw_;}
49  inline const std::string& label() const {return label_;}
50  inline bool isUniform() const {return true;}
51  //@}
52 
53  /** Return the coordinate of the given bin center */
54  inline double binCenter(const int binNum) const
55  {return min_ + (binNum + 0.5)*bw_;}
56 
57  /** Return the coordinate of the given bin left edge */
58  inline double leftBinEdge(const int binNum) const
59  {return min_ + binNum*bw_;}
60 
61  /** Return the coordinate of the given bin right edge */
62  inline double rightBinEdge(const int binNum) const
63  {return min_ + (binNum + 1)*bw_;}
64 
65  /** Return the coordinate interval occupied by the given bin */
66  inline Interval<double> binInterval(const int binNum) const
67  {return Interval<double>(min_+binNum*bw_, min_+(binNum+1)*bw_);}
68 
69  /** Change the axis label */
70  inline void setLabel(const char* newlabel)
71  {label_ = newlabel ? newlabel : "";}
72 
73  /**
74  // This method returns arbitrary integer bin number, including
75  // negative numbers and numbers which can exceed nBins()-1
76  */
77  int binNumber(double x) const;
78 
79  /**
80  // This method returns the closest valid bin number
81  // (>= 0 and below nBins() )
82  */
83  unsigned closestValidBin(double x) const;
84 
85  /**
86  // Return the mapper which calculates floating point bin number
87  // given the coordinate. The resulting bin number can go above
88  // and below the axis range. If "mapLeftEdgeTo0" is specified
89  // as "false", it is the center of the first bin which gets
90  // mapped to 0.
91  */
92  LinearMapper1d binNumberMapper(bool mapLeftEdgeTo0=true) const;
93 
94  /**
95  // Floating point bin number given the coordinate (no bin number
96  // truncation of any kind is performed). Works in exactly the same
97  // way as the mapper returned by the previous method.
98  */
99  inline double fltBinNumber(const double x,
100  const bool mapLeftEdgeTo0=true) const
101  {return (x - min_)/bw_ - (mapLeftEdgeTo0 ? 0.0 : 0.5);}
102 
103  /**
104  // The following function returns a mapper that can be
105  // helpful in scanning a kernel (a density function) for
106  // subsequent convolution with the histogram which contains
107  // this axis.
108  */
109  CircularMapper1d kernelScanMapper(bool doubleRange) const;
110 
111  bool operator==(const HistoAxis&) const;
112  bool operator!=(const HistoAxis&) const;
113 
114  /** Comparison of axis coordinates within given tolerance */
115  bool isClose(const HistoAxis&, double tol) const;
116 
117  /** Return rebinned axis */
118  HistoAxis rebin(unsigned newBins) const;
119 
120  //@{
121  /** Method related to "geners" I/O */
122  inline gs::ClassId classId() const {return gs::ClassId(*this);}
123  bool write(std::ostream& of) const;
124  //@}
125 
126  static inline const char* classname() {return "npstat::HistoAxis";}
127  static inline unsigned version() {return 1;}
128  static HistoAxis* read(const gs::ClassId& id, std::istream& in);
129 
130  private:
131  double min_;
132  double max_;
133  double bw_;
134  std::string label_;
135  unsigned nBins_;
136 
137  template <typename Numeric, class Axis> friend class HistoND;
138  friend class DualHistoAxis;
139 
140  inline unsigned overflowIndex(
141  const double x, unsigned* binNumber) const
142  {
143  if (x < min_)
144  return 0U;
145  else if (x >= max_)
146  return 2U;
147  else
148  {
149  const unsigned bin = static_cast<unsigned>((x - min_)/bw_);
150  *binNumber = bin >= nBins_ ? nBins_ - 1U : bin;
151  return 1U;
152  }
153  }
154 
155  unsigned overflowIndexWeighted(double x, unsigned* binNumber,
156  double *weight) const;
157 #ifdef SWIG
158  public:
159  inline std::pair<double,double> range() const
160  {return std::pair<double,double>(min_, max_);}
161 #endif // SWIG
162 
163  // SWIG can't instantiate vectors of classes without default
164  // constructors. This is why the default constructor has to be
165  // placed here.
166  inline HistoAxis() : min_(0.0), max_(0.0), bw_(0.0), nBins_(0) {}
167  };
168 }
169 
170 #endif // NPSTAT_HISTOAXIS_HH_
Linear transformation for circular topology.
Template to represent intervals in one dimension.
Definition: CircularMapper1d.hh:24
Definition: HistoAxis.hh:31
double min() const
Definition: HistoAxis.hh:42
double fltBinNumber(const double x, const bool mapLeftEdgeTo0=true) const
Definition: HistoAxis.hh:99
CircularMapper1d kernelScanMapper(bool doubleRange) const
double leftBinEdge(const int binNum) const
Definition: HistoAxis.hh:58
HistoAxis(unsigned nBins, double min, double max, const char *label=0)
gs::ClassId classId() const
Definition: HistoAxis.hh:122
void setLabel(const char *newlabel)
Definition: HistoAxis.hh:70
bool isClose(const HistoAxis &, double tol) const
int binNumber(double x) const
Interval< double > binInterval(const int binNum) const
Definition: HistoAxis.hh:66
HistoAxis rebin(unsigned newBins) const
unsigned closestValidBin(double x) const
double rightBinEdge(const int binNum) const
Definition: HistoAxis.hh:62
LinearMapper1d binNumberMapper(bool mapLeftEdgeTo0=true) const
double binCenter(const int binNum) const
Definition: HistoAxis.hh:54
Definition: Interval.hh:29
Definition: LinearMapper1d.hh:20
Definition: AbsArrayProjector.hh:14