| 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 |
|
|---|
| 17 | #include <iosfwd>
|
|---|
| 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;
|
|---|
| 36 | //!> grant output operator access
|
|---|
| 37 | friend std::ostream & operator<<(std::ostream &ost, const Histogram &histogram);
|
|---|
| 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 | Histogram() :
|
|---|
| 52 | binwidth(0.5),
|
|---|
| 53 | offset(0.)
|
|---|
| 54 | {}
|
|---|
| 55 |
|
|---|
| 56 | /** Constructor for class Histogram.
|
|---|
| 57 | *
|
|---|
| 58 | * @param offset where the bins should start
|
|---|
| 59 | * @param binwidth width of the bins
|
|---|
| 60 | */
|
|---|
| 61 | Histogram(const BinLowerEnd _offset, const double _binwidth) :
|
|---|
| 62 | binwidth(_binwidth),
|
|---|
| 63 | offset(_offset)
|
|---|
| 64 | {}
|
|---|
| 65 |
|
|---|
| 66 | /** Constructor for class Histogram.
|
|---|
| 67 | *
|
|---|
| 68 | * @param samples samples to put in the histogram.
|
|---|
| 69 | * @param offset where the bins should start
|
|---|
| 70 | * @param binwidth width of the bins
|
|---|
| 71 | */
|
|---|
| 72 | Histogram(const samples_t &samples, const BinLowerEnd _offset, const double _binwidth);
|
|---|
| 73 |
|
|---|
| 74 | /** Adding another histogram onto this one.
|
|---|
| 75 | *
|
|---|
| 76 | * \note The operation is area-conserving, i.e. the new area is the sum of
|
|---|
| 77 | * both areas.
|
|---|
| 78 | *
|
|---|
| 79 | * @param other other histogram
|
|---|
| 80 | * @return ref to this instance
|
|---|
| 81 | */
|
|---|
| 82 | Histogram& operator+=(const Histogram &other);
|
|---|
| 83 |
|
|---|
| 84 | /** Assignment operator.
|
|---|
| 85 | *
|
|---|
| 86 | * @param other other histogram to make ourselves equal to
|
|---|
| 87 | * @return ref to this instance
|
|---|
| 88 | */
|
|---|
| 89 | Histogram& operator=(const Histogram &other);
|
|---|
| 90 |
|
|---|
| 91 | /** Subtracting another histogram from this one.
|
|---|
| 92 | *
|
|---|
| 93 | * \note The operation is area-conserving, i.e. the new area is the
|
|---|
| 94 | * difference of both areas.
|
|---|
| 95 | *
|
|---|
| 96 | * @param other other histogram
|
|---|
| 97 | * @return ref to this instance
|
|---|
| 98 | */
|
|---|
| 99 | Histogram& operator-=(const Histogram &other);
|
|---|
| 100 |
|
|---|
| 101 | /** States whether each bin of this histogram has count of zero.
|
|---|
| 102 | *
|
|---|
| 103 | * @return true - all bins are zero, false - else
|
|---|
| 104 | */
|
|---|
| 105 | bool isEmpty() const;
|
|---|
| 106 |
|
|---|
| 107 | /** Sums all found weights times the Histogram::binwidth.
|
|---|
| 108 | *
|
|---|
| 109 | * @return sum over all weights times width.
|
|---|
| 110 | */
|
|---|
| 111 | double area() const;
|
|---|
| 112 |
|
|---|
| 113 | /** Adds given vector of samples to the histogram.
|
|---|
| 114 | *
|
|---|
| 115 | * @param samples vector of samples to add
|
|---|
| 116 | */
|
|---|
| 117 | void addSamples(const samples_t &samples);
|
|---|
| 118 |
|
|---|
| 119 | private:
|
|---|
| 120 | /** Returns the iterator to the bin representing the lower end into which the \a value fits.
|
|---|
| 121 | *
|
|---|
| 122 | * @param _value value to fit.
|
|---|
| 123 | * @return iterator to bin or to bins.end() if not matching
|
|---|
| 124 | */
|
|---|
| 125 | Bins_t::iterator getLowerEndBin(const double _value);
|
|---|
| 126 |
|
|---|
| 127 | /** Returns the iterator to the bin representing the upper end into which the \a value fits.
|
|---|
| 128 | *
|
|---|
| 129 | * @param _value value to fit.
|
|---|
| 130 | * @return iterator to bin or to bins.end() if not matching
|
|---|
| 131 | */
|
|---|
| 132 | Bins_t::iterator getHigherEndBin(const double _value);
|
|---|
| 133 |
|
|---|
| 134 | /** Returns the lower end regardless of whether such a bin exists.
|
|---|
| 135 | *
|
|---|
| 136 | * @param _value value to place into a bin
|
|---|
| 137 | * @return start of would-be bin to contain this \a _value
|
|---|
| 138 | */
|
|---|
| 139 | BinLowerEnd getLowerEnd(const double _value) const;
|
|---|
| 140 |
|
|---|
| 141 | /** Helper function that contains all the logic of how to superpose two
|
|---|
| 142 | * histograms.
|
|---|
| 143 | *
|
|---|
| 144 | * Is called by Histogram::operator+=() and Histogram::operator-=()
|
|---|
| 145 | *
|
|---|
| 146 | * @param other other histogram
|
|---|
| 147 | * @param prefactor +1. is then addition, -1. is subtraction.
|
|---|
| 148 | */
|
|---|
| 149 | void superposeOtherHistogram(const Histogram &other, const double prefactor);
|
|---|
| 150 |
|
|---|
| 151 | /** Helper function to add missing bins in superposition operation.
|
|---|
| 152 | *
|
|---|
| 153 | * We add here enough bins, initialized to weight zero such that the bin
|
|---|
| 154 | * of [LowerEnd, NextLowerEnd) fully fits. Does nothing if the bins are
|
|---|
| 155 | * already present.
|
|---|
| 156 | *
|
|---|
| 157 | * @param LowerEnd lowerend of the other bin (to superpose)
|
|---|
| 158 | * @param NextLowerEnd lower end of bin (to superpose) next adjacent to other
|
|---|
| 159 | */
|
|---|
| 160 | void extendMissingBins(const BinLowerEnd LowerEnd, const BinLowerEnd NextLowerEnd);
|
|---|
| 161 |
|
|---|
| 162 | /** Helper function to print BinLowerEnd and BinWeight to a string for every bin.
|
|---|
| 163 | *
|
|---|
| 164 | * @return string containing information on each consecutive bin
|
|---|
| 165 | */
|
|---|
| 166 | std::string printBins() const;
|
|---|
| 167 |
|
|---|
| 168 | private:
|
|---|
| 169 | //!> vector of bins containing the histogram
|
|---|
| 170 | Bins_t bins;
|
|---|
| 171 | //!> width of bin
|
|---|
| 172 | const double binwidth;
|
|---|
| 173 | //!> offset for the start of the bins
|
|---|
| 174 | const BinLowerEnd offset;
|
|---|
| 175 | };
|
|---|
| 176 |
|
|---|
| 177 | /** Function to print an arbitrary pair to ostream.
|
|---|
| 178 | *
|
|---|
| 179 | * @param ost output stream
|
|---|
| 180 | * @param elem element to print
|
|---|
| 181 | * @return ref to given ostream for concatenation
|
|---|
| 182 | */
|
|---|
| 183 | std::ostream & operator<<(std::ostream &ost, const Histogram::Bin_t &elem);
|
|---|
| 184 |
|
|---|
| 185 | /** Function to print histogram to ostream.
|
|---|
| 186 | *
|
|---|
| 187 | * @param ost output stream
|
|---|
| 188 | * @param histogram histogram to print
|
|---|
| 189 | * @return ref to given ostream for concatenation
|
|---|
| 190 | */
|
|---|
| 191 | std::ostream & operator<<(std::ostream &ost, const Histogram &histogram);
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 | #endif /* HISTOGRAM_HPP_ */
|
|---|