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