[c4fd03] | 1 | /*
|
---|
| 2 | * Histogram.hpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Jul 26, 2012
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #ifndef HISTOGRAM_HPP_
|
---|
| 9 | #define HISTOGRAM_HPP_
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 | // include config.h
|
---|
| 13 | #ifdef HAVE_CONFIG_H
|
---|
| 14 | #include <config.h>
|
---|
| 15 | #endif
|
---|
| 16 |
|
---|
[3f6bbb] | 17 | #include <iosfwd>
|
---|
[c4fd03] | 18 | #include <map>
|
---|
| 19 | #include <vector>
|
---|
| 20 |
|
---|
| 21 | class HistogramTest;
|
---|
| 22 |
|
---|
| 23 | /** This class generates a histogram from a given vector of sampled values.
|
---|
| 24 | *
|
---|
| 25 | * Most importantly, it also contains operator+=() and operator-=() to perform
|
---|
| 26 | * sum on the \b histograms.
|
---|
| 27 | *
|
---|
| 28 | * This is to be used with the OrthogonalSummation which requires these specific
|
---|
| 29 | * operator implementations.
|
---|
| 30 | *
|
---|
| 31 | */
|
---|
| 32 | class Histogram
|
---|
| 33 | {
|
---|
| 34 | //!> grant unit test access to private parts
|
---|
| 35 | friend class HistogramTest;
|
---|
[cf18c5] | 36 | //!> grant output operator access
|
---|
| 37 | friend std::ostream & operator<<(std::ostream &ost, const Histogram &histogram);
|
---|
[c4fd03] | 38 | public:
|
---|
| 39 | //!> named type for a vector of sample values
|
---|
| 40 | typedef std::vector<double> samples_t;
|
---|
| 41 | //!> named type for the start of a bin
|
---|
| 42 | typedef double BinLowerEnd;
|
---|
| 43 | //!> named type for the count of a bin, may be fractional
|
---|
| 44 | typedef double BinWeight;
|
---|
| 45 | //!> named type for the pair identifying a bin by its lower end and count
|
---|
| 46 | typedef std::pair< BinLowerEnd, BinWeight> Bin_t;
|
---|
| 47 | private:
|
---|
| 48 | //!> named type for a vector of bins
|
---|
| 49 | typedef std::map< BinLowerEnd, BinWeight > Bins_t;
|
---|
| 50 | public:
|
---|
| 51 | /** Constructor for class Histogram.
|
---|
| 52 | *
|
---|
| 53 | * @param samples samples to put in the histogram.
|
---|
| 54 | * @param CountBins number of bins (if negative, we choose number by statistical means)
|
---|
| 55 | */
|
---|
| 56 | Histogram(const samples_t &samples, const int _CountBins=-1);
|
---|
| 57 |
|
---|
| 58 | /** Adding another histogram onto this one.
|
---|
[45f4f96] | 59 | *
|
---|
| 60 | * \note The operation is area-conserving, i.e. the new area is the sum of
|
---|
| 61 | * both areas.
|
---|
[c4fd03] | 62 | *
|
---|
| 63 | * @param other other histogram
|
---|
| 64 | * @return ref to this instance
|
---|
| 65 | */
|
---|
| 66 | Histogram& operator+=(const Histogram &other);
|
---|
| 67 |
|
---|
| 68 | /** Subtracting another histogram from this one.
|
---|
[45f4f96] | 69 | *
|
---|
| 70 | * \note The operation is area-conserving, i.e. the new area is the
|
---|
| 71 | * difference of both areas.
|
---|
[c4fd03] | 72 | *
|
---|
| 73 | * @param other other histogram
|
---|
| 74 | * @return ref to this instance
|
---|
| 75 | */
|
---|
| 76 | Histogram& operator-=(const Histogram &other);
|
---|
| 77 |
|
---|
| 78 | /** States whether each bin of this histogram has count of zero.
|
---|
| 79 | *
|
---|
| 80 | * @return true - all bins are zero, false - else
|
---|
| 81 | */
|
---|
| 82 | bool isEmpty() const;
|
---|
| 83 |
|
---|
[45f4f96] | 84 | /** Sums all found weights times the Histogram::binwidth.
|
---|
| 85 | *
|
---|
| 86 | * @return sum over all weights times width.
|
---|
| 87 | */
|
---|
| 88 | double area() const;
|
---|
| 89 |
|
---|
[c4fd03] | 90 | private:
|
---|
[1c365e] | 91 | /** Returns the iterator to the bin representing the lower end into which the \a value fits.
|
---|
[c4fd03] | 92 | *
|
---|
| 93 | * @param _value value to fit.
|
---|
| 94 | * @return iterator to bin or to bins.end() if not matching
|
---|
| 95 | */
|
---|
[1c365e] | 96 | Bins_t::iterator getLowerEndBin(const double _value);
|
---|
| 97 |
|
---|
| 98 | /** Returns the iterator to the bin representing the upper end into which the \a value fits.
|
---|
| 99 | *
|
---|
| 100 | * @param _value value to fit.
|
---|
| 101 | * @return iterator to bin or to bins.end() if not matching
|
---|
| 102 | */
|
---|
| 103 | Bins_t::iterator getHigherEndBin(const double _value);
|
---|
[1dd419] | 104 |
|
---|
| 105 | /** Returns the lower end regardless of whether such a bin exists.
|
---|
| 106 | *
|
---|
| 107 | * @param _value value to place into a bin
|
---|
| 108 | * @return start of would-be bin to contain this \a _value
|
---|
| 109 | */
|
---|
| 110 | BinLowerEnd getLowerEnd(const double _value) const;
|
---|
| 111 |
|
---|
[22c4f57] | 112 | /** Helper function that contains all the logic of how to superpose two
|
---|
| 113 | * histograms.
|
---|
| 114 | *
|
---|
| 115 | * Is called by Histogram::operator+=() and Histogram::operator-=()
|
---|
| 116 | *
|
---|
| 117 | * @param other other histogram
|
---|
| 118 | * @param prefactor +1. is then addition, -1. is subtraction.
|
---|
| 119 | */
|
---|
| 120 | void superposeOtherHistogram(const Histogram &other, const double prefactor);
|
---|
[c4fd03] | 121 |
|
---|
[e4048f] | 122 | /** Helper function to add missing bins in superposition operation.
|
---|
| 123 | *
|
---|
| 124 | * We add here enough bins, initialized to weight zero such that the bin
|
---|
| 125 | * of [LowerEnd, NextLowerEnd) fully fits. Does nothing if the bins are
|
---|
| 126 | * already present.
|
---|
| 127 | *
|
---|
| 128 | * @param LowerEnd lowerend of the other bin (to superpose)
|
---|
| 129 | * @param NextLowerEnd lower end of bin (to superpose) next adjacent to other
|
---|
| 130 | */
|
---|
| 131 | void extendMissingBins(const BinLowerEnd LowerEnd, const BinLowerEnd NextLowerEnd);
|
---|
| 132 |
|
---|
[e5dbd5] | 133 | /** Helper function to print BinLowerEnd and BinWeight to a string for every bin.
|
---|
| 134 | *
|
---|
| 135 | * @return string containing information on each consecutive bin
|
---|
| 136 | */
|
---|
| 137 | std::string printBins() const;
|
---|
| 138 |
|
---|
[c4fd03] | 139 | private:
|
---|
| 140 | //!> vector of bins containing the histogram
|
---|
| 141 | Bins_t bins;
|
---|
| 142 | //!> width of bin
|
---|
[bb68c0] | 143 | const double binwidth;
|
---|
[c4fd03] | 144 | //!> number of bins
|
---|
| 145 | int CountBins;
|
---|
| 146 | };
|
---|
| 147 |
|
---|
[3f6bbb] | 148 | /** Function to print an arbitrary pair to ostream.
|
---|
| 149 | *
|
---|
| 150 | * @param ost output stream
|
---|
| 151 | * @param elem element to print
|
---|
| 152 | * @return ref to given ostream for concatenation
|
---|
| 153 | */
|
---|
| 154 | std::ostream & operator<<(std::ostream &ost, const Histogram::Bin_t &elem);
|
---|
| 155 |
|
---|
[cf18c5] | 156 | /** Function to print histogram to ostream.
|
---|
| 157 | *
|
---|
| 158 | * @param ost output stream
|
---|
| 159 | * @param histogram histogram to print
|
---|
| 160 | * @return ref to given ostream for concatenation
|
---|
| 161 | */
|
---|
| 162 | std::ostream & operator<<(std::ostream &ost, const Histogram &histogram);
|
---|
| 163 |
|
---|
[c4fd03] | 164 |
|
---|
| 165 | #endif /* HISTOGRAM_HPP_ */
|
---|