1 | // $Id: Histogram.h 370 2005-08-05 13:26:10Z peter $ |
---|
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 | |
---|
12 | namespace theplu { |
---|
13 | namespace 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 in the middle of bin \a k. |
---|
99 | /// |
---|
100 | /// @note No check is done that \a k is within the size of the |
---|
101 | /// histogram. |
---|
102 | /// |
---|
103 | inline double observation_value(const size_t k) const |
---|
104 | { return xmin_+spacing()*(k+0.5); } |
---|
105 | |
---|
106 | /// |
---|
107 | /// Set everyting to default values, here it means that everything |
---|
108 | /// is set to zero except the boundary values that are kept. |
---|
109 | /// |
---|
110 | void reset(void); |
---|
111 | |
---|
112 | /// |
---|
113 | /// @return The size of the bins in the histogram. |
---|
114 | /// |
---|
115 | inline double spacing(void) const { return (xmax_-xmin_)/nof_bins(); } |
---|
116 | |
---|
117 | /// |
---|
118 | /// @return The histogram upper boundary. |
---|
119 | /// |
---|
120 | /// @note The upper boundary value is inside the histogram. |
---|
121 | /// |
---|
122 | inline double xmax(void) const { return xmax_; } |
---|
123 | |
---|
124 | /// |
---|
125 | /// @return The histogram lower boundary. |
---|
126 | /// |
---|
127 | /// @note The lower boundary value is outside the histogram. |
---|
128 | /// |
---|
129 | inline double xmin(void) const { return xmin_; } |
---|
130 | |
---|
131 | /// |
---|
132 | /// @return The count of bin \a k.in the histogram. |
---|
133 | inline double operator[](size_t k) const { return histogram_[k]; } |
---|
134 | |
---|
135 | /// |
---|
136 | /// The assignment operator |
---|
137 | const Histogram& operator=(const Histogram&); |
---|
138 | |
---|
139 | private: |
---|
140 | // Returns zero if outside boundaries |
---|
141 | inline size_t bin(double d) |
---|
142 | { return (((d<xmin_) || (d>xmax_)) ? 0 : |
---|
143 | static_cast<size_t>(floor((d-xmin_)/spacing() ))); } |
---|
144 | |
---|
145 | std::vector<double> histogram_; |
---|
146 | double xmax_; |
---|
147 | double xmin_; |
---|
148 | statistics::AveragerWeighted sum_all_; // average of all data |
---|
149 | statistics::AveragerWeighted sum_histogram_;// average of data in histogram |
---|
150 | }; |
---|
151 | |
---|
152 | /// |
---|
153 | /// The Histogram output operator |
---|
154 | /// |
---|
155 | std::ostream& operator<<(std::ostream& s,const Histogram&); |
---|
156 | |
---|
157 | }} // of namespace statistics and namespace theplu |
---|
158 | |
---|
159 | #endif |
---|