source: trunk/src/Histogram.h @ 231

Last change on this file since 231 was 231, checked in by Peter, 17 years ago

filled incomplete stuff

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.3 KB
Line 
1// $Id: Histogram.h 231 2005-02-21 14:50:17Z peter $
2
3#ifndef _theplu_cpptools_histogram_
4#define _theplu_cpptools_histogram_
5
6#include <string>
7#include <vector>
8
9#include "WeightedAverager.h"
10
11
12namespace theplu {
13namespace cpptools {
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 WeightedAverager 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 WeightedAverager object.
66    ///
67    inline const statistics::WeightedAverager& averager_all(void) const
68      { return sum_all_; }
69
70    ///
71    /// Gives access to the WeightedAverager 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 WeightedAverager object.
78    ///
79    inline const statistics::WeightedAverager& 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    void normalize(double scale_factor=1);
93
94    ///
95    /// @return The value of the histogram corresponding to bin \a
96    /// bin.
97    ///
98    /// @note No check is done that \a bin is within the size of the
99    /// histogram.
100    ///
101    inline double observation_value(const size_t bin) const
102      { return xmin_+spacing()*(bin+0.5); }
103
104    ///
105    /// Set everyting to default values, here it means that everything
106    /// is set to zero except the boundary values that are kept.
107    ///
108    void reset(void);
109
110    ///
111    /// @return The size of the bins in the histogram.
112    ///
113    inline double spacing(void) const { return (xmax_-xmin_)/nof_bins(); }
114
115    ///
116    /// @return The histogram upper boundary.
117    ///
118    /// @note The upper boundary value is inside the histogram.
119    ///
120    inline double xmax(void) const { return xmax_; }
121
122    ///
123    /// @return The histogram lower boundary.
124    ///
125    /// @note The lower boundary value is outside the histogram.
126    ///
127    inline double xmin(void) const { return xmin_; }
128
129    ///
130    /// @return The count of bin \a bin.in the histogram.
131    inline double operator[](size_t bin) const { return histogram_[bin]; } 
132
133    ///
134    /// The assignment operator
135    const Histogram& operator=(const Histogram&);
136
137  private:
138    // Returns zero if outside boundaries
139    inline size_t bin(double d)
140    { return (((d<xmin_) || (d>xmax_)) ? 0 :
141              static_cast<size_t>(floor((d-xmin_)/spacing() ))); }
142
143    std::vector<double> histogram_;
144    double xmax_;
145    double xmin_;
146    statistics::WeightedAverager sum_all_;      // average of all data
147    statistics::WeightedAverager sum_histogram_;// average of data in histogram
148  };
149
150///
151/// The Histogram output operator
152///
153std::ostream& operator<<(std::ostream& s,const Histogram&);
154
155}} // of namespace cpptools and namespace theplu
156
157#endif
Note: See TracBrowser for help on using the repository browser.