source: trunk/lib/statistics/Histogram.h @ 363

Last change on this file since 363 was 363, checked in by Jari Häkkinen, 17 years ago

Added todo tag on normalize.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1// $Id: Histogram.h 363 2005-08-04 22:23:00Z jari $
2
3#ifndef _theplu_statistics_histogram_
4#define _theplu_statistics_histogram_
5
6#include <c++_tools/statistics/AveragerWeighted.h>
7
8#include <string>
9#include <vector>
10
11
12namespace theplu {
13namespace statistics {
14
15  ///
16  /// Histograms provide a convenient way of presenting the
17  /// distribution of a set of data. A histogram consists of a set of
18  /// bins which count the number of events falling into these
19  /// bins. Currently only one dimensional histograms with uniformly
20  /// spaced bins are supported.
21  ///
22  class Histogram
23  {
24  public:
25
26    ///
27    /// The default constructor.
28    ///
29    Histogram(void);
30
31    ///
32    /// The copy constructor.
33    ///
34    Histogram(const Histogram&);
35
36    ///
37    /// Construct a histogram object that covers \f$(xmin,xmax]\f$
38    /// with the bin spacing \f$(xmax-xmin)/n\f$.
39    ///
40    Histogram(const double xmin, const double xmax, const size_t n);
41
42    virtual ~Histogram(void);
43
44    ///
45    /// Update the histogram by adding \a weight to the bin whose
46    /// range contains the observation \a x. No bins are updated when
47    /// \a x lies outside the range of the histogram but the value is
48    /// added to the overall integral of the histogram.
49    ///
50    /// @short Add an data point to the histogram.
51    ///
52    /// @return 0 if \a x lies within the range of the histogram, -1
53    /// if \a x is smaller than the lower limit of the histogram, and
54    /// similarly, 1 is returned if \a x is greater than or equal to
55    /// the upper limit.
56    ///
57    int add(const double x,const double weight=1.0);
58
59    ///
60    /// Gives access to the AveragerWeighted object that keeps track of
61    /// average of all events presented to the histogram.
62    ///
63    /// @short Average of all events presented to the histogram.
64    ///
65    /// @return A const reference to an AveragerWeighted object.
66    ///
67    inline const statistics::AveragerWeighted& averager_all(void) const
68      { return sum_all_; }
69
70    ///
71    /// Gives access to the AveragerWeighted object that keeps track of
72    /// average of events that fits within the histogram lower and
73    /// upper limits. This function is equivalent to averager().
74    ///
75    /// @short Average of events fitting within histogram.
76    ///
77    /// @return A const reference to an AveragerWeighted object.
78    ///
79    inline const statistics::AveragerWeighted& averager_histogram(void) const
80      { return sum_histogram_; }
81
82    ///
83    /// @return The number of bins in the histogram
84    ///
85    inline size_t nof_bins(void) const { return histogram_.size(); }
86
87    ///
88    /// Rescale the histogram using the scale factor \a
89    /// scale_factor. You may need to averager_all or
90    /// averager_histogram to set the proper scale factor.
91    ///
92    /// @todo Redefine the behaviour of this function. The parameter
93    /// should define the integral of the function.
94    ///
95    void normalize(double scale_factor=1);
96
97    ///
98    /// @return The value of the histogram corresponding to bin \a
99    /// bin.
100    ///
101    /// @note No check is done that \a bin is within the size of the
102    /// histogram.
103    ///
104    inline double observation_value(const size_t bin) const
105      { return xmin_+spacing()*(bin+0.5); }
106
107    ///
108    /// Set everyting to default values, here it means that everything
109    /// is set to zero except the boundary values that are kept.
110    ///
111    void reset(void);
112
113    ///
114    /// @return The size of the bins in the histogram.
115    ///
116    inline double spacing(void) const { return (xmax_-xmin_)/nof_bins(); }
117
118    ///
119    /// @return The histogram upper boundary.
120    ///
121    /// @note The upper boundary value is inside the histogram.
122    ///
123    inline double xmax(void) const { return xmax_; }
124
125    ///
126    /// @return The histogram lower boundary.
127    ///
128    /// @note The lower boundary value is outside the histogram.
129    ///
130    inline double xmin(void) const { return xmin_; }
131
132    ///
133    /// @return The count of bin \a bin.in the histogram.
134    inline double operator[](size_t bin) const { return histogram_[bin]; } 
135
136    ///
137    /// The assignment operator
138    const Histogram& operator=(const Histogram&);
139
140  private:
141    // Returns zero if outside boundaries
142    inline size_t bin(double d)
143    { return (((d<xmin_) || (d>xmax_)) ? 0 :
144              static_cast<size_t>(floor((d-xmin_)/spacing() ))); }
145
146    std::vector<double> histogram_;
147    double xmax_;
148    double xmin_;
149    statistics::AveragerWeighted sum_all_;      // average of all data
150    statistics::AveragerWeighted sum_histogram_;// average of data in histogram
151  };
152
153///
154/// The Histogram output operator
155///
156std::ostream& operator<<(std::ostream& s,const Histogram&);
157
158}} // of namespace statistics and namespace theplu
159
160#endif
Note: See TracBrowser for help on using the repository browser.