| 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 | 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. | 
|---|
| 57 | * | 
|---|
| 58 | * @param other other histogram | 
|---|
| 59 | * @return ref to this instance | 
|---|
| 60 | */ | 
|---|
| 61 | Histogram& operator+=(const Histogram &other); | 
|---|
| 62 |  | 
|---|
| 63 | /** Subtracting another histogram from this one. | 
|---|
| 64 | * | 
|---|
| 65 | * @param other other histogram | 
|---|
| 66 | * @return ref to this instance | 
|---|
| 67 | */ | 
|---|
| 68 | Histogram& operator-=(const Histogram &other); | 
|---|
| 69 |  | 
|---|
| 70 | /** States whether each bin of this histogram has count of zero. | 
|---|
| 71 | * | 
|---|
| 72 | * @return true - all bins are zero, false - else | 
|---|
| 73 | */ | 
|---|
| 74 | bool isEmpty() const; | 
|---|
| 75 |  | 
|---|
| 76 | private: | 
|---|
| 77 | /** Returns the iterator to the bin representing the lower end into which the \a value fits. | 
|---|
| 78 | * | 
|---|
| 79 | * @param _value value to fit. | 
|---|
| 80 | * @return iterator to bin or to bins.end() if not matching | 
|---|
| 81 | */ | 
|---|
| 82 | Bins_t::iterator getLowerEndBin(const double _value); | 
|---|
| 83 |  | 
|---|
| 84 | /** Returns the iterator to the bin representing the upper end into which the \a value fits. | 
|---|
| 85 | * | 
|---|
| 86 | * @param _value value to fit. | 
|---|
| 87 | * @return iterator to bin or to bins.end() if not matching | 
|---|
| 88 | */ | 
|---|
| 89 | Bins_t::iterator getHigherEndBin(const double _value); | 
|---|
| 90 |  | 
|---|
| 91 | private: | 
|---|
| 92 | //!> vector of bins containing the histogram | 
|---|
| 93 | Bins_t bins; | 
|---|
| 94 | //!> width of bin | 
|---|
| 95 | double binwidth; | 
|---|
| 96 | //!> number of bins | 
|---|
| 97 | int CountBins; | 
|---|
| 98 | }; | 
|---|
| 99 |  | 
|---|
| 100 | /** Function to print an arbitrary pair to ostream. | 
|---|
| 101 | * | 
|---|
| 102 | * @param ost output stream | 
|---|
| 103 | * @param elem element to print | 
|---|
| 104 | * @return ref to given ostream for concatenation | 
|---|
| 105 | */ | 
|---|
| 106 | std::ostream & operator<<(std::ostream &ost, const Histogram::Bin_t &elem); | 
|---|
| 107 |  | 
|---|
| 108 |  | 
|---|
| 109 | #endif /* HISTOGRAM_HPP_ */ | 
|---|