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