npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
DualHistoAxis.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_DUALHISTOAXIS_HH_
2 #define NPSTAT_DUALHISTOAXIS_HH_
3 
4 /*!
5 // \file DualHistoAxis.hh
6 //
7 // \brief Represent both equidistant and non-uniform histogram axis binning
8 //
9 // Author: I. Volobouev
10 //
11 // July 2012
12 */
13 
14 #include "npstat/stat/HistoAxis.hh"
16 
17 namespace npstat {
18  /**
19  // Histogram axis which can be either uniform or non-uniform.
20  // Will work a little bit slower than either HistoAxis or NUHistoAxis,
21  // but can be used in place of either one of them.
22  */
24  {
25  public:
26  // Constructors
27  inline DualHistoAxis(const NUHistoAxis& a)
28  : a_(a), u_(1U, 0.0, 1.0), uniform_(false) {}
29 
30  inline DualHistoAxis(const HistoAxis& u)
31  : a_(dummy_vec()), u_(u), uniform_(true) {}
32 
33  inline DualHistoAxis(const std::vector<double>& binEdges,
34  const char* label = 0)
35  : a_(binEdges, label), u_(1U, 0.0, 1.0), uniform_(false) {}
36 
37  inline DualHistoAxis(unsigned nBins, double min, double max,
38  const char* label = 0)
39  : a_(dummy_vec()), u_(nBins, min, max, label), uniform_(true) {}
40 
41  // Inspectors
42  inline bool isUniform() const {return uniform_;}
43 
44  inline double min() const
45  {return uniform_ ? u_.min() : a_.min();}
46 
47  inline double max() const
48  {return uniform_ ? u_.max() : a_.max();}
49 
50  inline Interval<double> interval() const
51  {return uniform_ ? u_.interval() : a_.interval();}
52 
53  inline double length() const
54  {return uniform_ ? u_.length() : a_.length();}
55 
56  inline unsigned nBins() const
57  {return uniform_ ? u_.nBins() : a_.nBins();}
58 
59  inline double binWidth(const int binNum) const
60  {return uniform_ ? u_.binWidth(binNum) : a_.binWidth(binNum);}
61 
62  inline const std::string& label() const
63  {return uniform_ ? u_.label() : a_.label();}
64 
65  inline double binCenter(const int binNum) const
66  {return uniform_ ? u_.binCenter(binNum) : a_.binCenter(binNum);}
67 
68  inline double leftBinEdge(const int binNum) const
69  {return uniform_ ? u_.leftBinEdge(binNum) : a_.leftBinEdge(binNum);}
70 
71  inline double rightBinEdge(const int binNum) const
72  {return uniform_ ? u_.rightBinEdge(binNum) : a_.rightBinEdge(binNum);}
73 
74  inline Interval<double> binInterval(const int binNum) const
75  {return uniform_ ? u_.binInterval(binNum) : a_.binInterval(binNum);}
76 
77  //@{
78  /**
79  // Return a pointer to the underlying axis. This will be
80  // a null pointer if the axis does not correspond to the
81  // constructed type.
82  */
83  inline const NUHistoAxis* getNUHistoAxis() const
84  {return uniform_ ? static_cast<const NUHistoAxis*>(0) : &a_;}
85 
86  inline const HistoAxis* getHistoAxis() const
87  {return uniform_ ? &u_ : static_cast<const HistoAxis*>(0);}
88  //@}
89 
90  /** Modify the axis label */
91  inline void setLabel(const char* newlabel)
92  {uniform_ ? u_.setLabel(newlabel) : a_.setLabel(newlabel);}
93 
94  /**
95  // This method returns arbitrary integer bin number.
96  // Possible output depends on whether the axis is uniform or not.
97  */
98  inline int binNumber(const double x) const
99  {return uniform_ ? u_.binNumber(x) : a_.binNumber(x);}
100 
101  /**
102  // Floating point bin number given the coordinate. Useful for
103  // interpolation methods and such.
104  */
105  inline double fltBinNumber(const double x,
106  const bool mapLeftEdgeTo0=true) const
107  {
108  return uniform_ ? u_.fltBinNumber(x, mapLeftEdgeTo0) :
109  a_.fltBinNumber(x, mapLeftEdgeTo0);
110  }
111 
112  /** Return the closest valid bin number (equal to or above 0 and below nBins() ) */
113  inline unsigned closestValidBin(const double x) const
114  {return uniform_ ? u_.closestValidBin(x) : a_.closestValidBin(x);}
115 
116  inline bool operator==(const DualHistoAxis& r) const
117  {return uniform_ == r.uniform_ && a_ == r.a_ && u_ == r.u_;}
118 
119  inline bool operator!=(const DualHistoAxis& r) const
120  {return !(*this == r);}
121 
122  /** Comparison within given tolerance */
123  inline bool isClose(const DualHistoAxis& r, const double tol) const
124  {
125  return uniform_ == r.uniform_ &&
126  a_.isClose(r.a_, tol) &&
127  u_.isClose(r.u_, tol);
128  }
129 
130  /** Return uniformly rebinned axis */
131  inline DualHistoAxis rebin(const unsigned newBins) const
132  {return DualHistoAxis(newBins, min(), max(), label().c_str());}
133 
134  //@{
135  // Method related to "geners" I/O
136  inline gs::ClassId classId() const {return gs::ClassId(*this);}
137  bool write(std::ostream& of) const;
138  //@}
139 
140  static inline const char* classname() {return "npstat::DualHistoAxis";}
141  static inline unsigned version() {return 1;}
142  static DualHistoAxis* read(const gs::ClassId& id, std::istream& in);
143 
144  private:
145  NUHistoAxis a_;
146  HistoAxis u_;
147  bool uniform_;
148 
149  template <typename Numeric, class Axis> friend class HistoND;
150 
151  inline unsigned overflowIndex(
152  const double x, unsigned* binNumber) const
153  {
154  return uniform_ ? u_.overflowIndex(x, binNumber) :
155  a_.overflowIndex(x, binNumber);
156  }
157 
158  inline static std::vector<double> dummy_vec()
159  {
160  std::vector<double> vec(2, 0.0);
161  vec[1] = 1.0;
162  return vec;
163  }
164 
165 #ifdef SWIG
166  public:
167  inline std::pair<double,double> range() const
168  {return uniform_ ? u_.range() : a_.range();}
169 #endif // SWIG
170 
171  // SWIG can't instantiate vectors of classes without default
172  // constructors. This is why the default constructor has to be
173  // placed here.
174  inline DualHistoAxis()
175  : a_(dummy_vec()), u_(1U, 0.0, 1.0), uniform_(true) {}
176  };
177 }
178 
179 #endif // NPSTAT_DUALHISTOAXIS_HH_
Histogram axis with equidistant bins.
Histogram axis with non-uniform bin spacing.
Definition: DualHistoAxis.hh:24
unsigned closestValidBin(const double x) const
Definition: DualHistoAxis.hh:113
DualHistoAxis rebin(const unsigned newBins) const
Definition: DualHistoAxis.hh:131
double fltBinNumber(const double x, const bool mapLeftEdgeTo0=true) const
Definition: DualHistoAxis.hh:105
void setLabel(const char *newlabel)
Definition: DualHistoAxis.hh:91
int binNumber(const double x) const
Definition: DualHistoAxis.hh:98
bool isClose(const DualHistoAxis &r, const double tol) const
Definition: DualHistoAxis.hh:123
const NUHistoAxis * getNUHistoAxis() const
Definition: DualHistoAxis.hh:83
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
double leftBinEdge(const int binNum) const
Definition: HistoAxis.hh:58
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
unsigned closestValidBin(double x) const
double rightBinEdge(const int binNum) const
Definition: HistoAxis.hh:62
double binCenter(const int binNum) const
Definition: HistoAxis.hh:54
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
unsigned closestValidBin(double x) const
void setLabel(const char *newlabel)
Definition: NUHistoAxis.hh:69
double binCenter(const int binNum) const
Definition: NUHistoAxis.hh:60
double min() const
Definition: NUHistoAxis.hh:39
double fltBinNumber(double x, bool mapLeftEdgeTo0=true) const
int binNumber(double x) const
Definition: AbsArrayProjector.hh:14