OpenMPCD
Histogram.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * Defines the Histogram class.
4  */
5 
6 #ifndef OPENMPCD_HISTOGRAM_HPP
7 #define OPENMPCD_HISTOGRAM_HPP
8 
10 #include <OpenMPCD/Graph.hpp>
11 #include <OpenMPCD/Types.hpp>
12 
13 #include <vector>
14 
15 namespace OpenMPCD
16 {
17  /**
18  * Represents a histogram.
19  */
20  class Histogram
21  {
22  public:
23  typedef unsigned long BinContent; ///< The type of bin contents.
24  typedef std::vector<BinContent> Container; ///< The container for bins.
25 
26  public:
27  /**
28  * The constructor.
29  * @param[in] name The histogram name.
30  * @param[in] conf The simulation configuration.
31  */
32  Histogram(const std::string& name, const Configuration& conf);
33 
34  /**
35  * The constructor.
36  * @param[in] low The lowest bin's lower end.
37  * @param[in] high The highest bin's highest end.
38  * @param[in] binCount The number of bins.
39  */
40  Histogram(const FP low, const FP high, const unsigned int binCount)
41  : bins(binCount, 0), underflows(0), overflows(0), lowEnd(low), highEnd(high),
42  binSize((high-low)/binCount)
43  {
44  }
45 
46  /**
47  * Adds an entry to the histogram.
48  * @param[in] val The value to add.
49  */
50  void fill(const FP val);
51 
52  /**
53  * Returns the number of underflows.
54  */
56  {
57  return underflows;
58  }
59 
60  /**
61  * Returns the number of overflows.
62  */
64  {
65  return overflows;
66  }
67 
68  /**
69  * Returns the bins.
70  */
71  const Container& getBins() const
72  {
73  return bins;
74  }
75 
76  /**
77  * Returns the lower end of the histogram.
78  */
79  FP getLowEnd() const
80  {
81  return lowEnd;
82  }
83 
84  /**
85  * Returns the upper end of the histogram.
86  */
87  FP getHighEnd() const
88  {
89  return highEnd;
90  }
91 
92  /**
93  * Returns the bin size.
94  */
95  FP getBinSize() const
96  {
97  return binSize;
98  }
99 
100  /**
101  * Returns the integral of the histogram.
102  */
103  FP getIntegral() const;
104 
105  /**
106  * Returns the graph corresponding to this histogram, with the area normalized to 1.
107  * @param[in] binPoint Since the bin has a non-zero width and the graph only consists of points,
108  * this parameter can be used to control where the graph point corresponding
109  * to a bin is situated. 0 means the point is at the leftmost end of the bin,
110  * 1 corresponds to the rightmost end, and the values inbetween scale linearly.
111  * @throw InvalidArgumentException Throws if binPoint is not in the range [0,1].
112  */
113  const Graph getNormalizedGraph(const FP binPoint=0.5) const;
114 
115  /**
116  * Saves the histogram at the given path.
117  * @param[in] filename The filename to save to.
118  * @param[in] binPoint Since the bin has a non-zero width and the graph only consists of points,
119  * this parameter can be used to control where the graph point corresponding
120  * to a bin is situated. 0 means the point is at the leftmost end of the bin,
121  * 1 corresponds to the rightmost end, and the values inbetween scale linearly.
122  * @throw InvalidArgumentException Throws if binPoint is not in the range [0,1].
123  */
124  void save(const std::string& filename, const FP binPoint=0.5) const;
125 
126  private:
127  Container bins; ///< The histogram bins.
128  BinContent underflows; ///< The number of underflows.
129  BinContent overflows; ///< The number of overflows.
130  FP lowEnd; ///< The lower end of the histogram.
131  FP highEnd; ///< The higher end of the histogram.
132  FP binSize; ///< The size of each bin.
133  };
134 }
135 
136 #endif
OpenMPCD::Histogram::getIntegral
FP getIntegral() const
Returns the integral of the histogram.
Definition: Histogram.cpp:45
OpenMPCD::Histogram
Represents a histogram.
Definition: Histogram.hpp:20
OpenMPCD::Configuration
Represents the configuration of the simulation.
Definition: Configuration.hpp:28
OpenMPCD::Histogram::getHighEnd
FP getHighEnd() const
Returns the upper end of the histogram.
Definition: Histogram.hpp:87
OpenMPCD::Histogram::getLowEnd
FP getLowEnd() const
Returns the lower end of the histogram.
Definition: Histogram.hpp:79
OpenMPCD::Histogram::Histogram
Histogram(const FP low, const FP high, const unsigned int binCount)
The constructor.
Definition: Histogram.hpp:40
OpenMPCD::Histogram::getUnderflows
BinContent getUnderflows() const
Returns the number of underflows.
Definition: Histogram.hpp:55
OpenMPCD::Histogram::getNormalizedGraph
const Graph getNormalizedGraph(const FP binPoint=0.5) const
Returns the graph corresponding to this histogram, with the area normalized to 1.
Definition: Histogram.cpp:55
OpenMPCD::Histogram::getBinSize
FP getBinSize() const
Returns the bin size.
Definition: Histogram.hpp:95
OpenMPCD::Histogram::getOverflows
BinContent getOverflows() const
Returns the number of overflows.
Definition: Histogram.hpp:63
OpenMPCD::Histogram::save
void save(const std::string &filename, const FP binPoint=0.5) const
Saves the histogram at the given path.
Definition: Histogram.cpp:75
OpenMPCD::Histogram::BinContent
unsigned long BinContent
The type of bin contents.
Definition: Histogram.hpp:23
OpenMPCD::FP
double FP
Default floating point type.
Definition: Types.hpp:13
Types.hpp
OpenMPCD::Histogram::fill
void fill(const FP val)
Adds an entry to the histogram.
Definition: Histogram.cpp:21
OpenMPCD::Histogram::Histogram
Histogram(const std::string &name, const Configuration &conf)
The constructor.
Definition: Histogram.cpp:9
OpenMPCD::Histogram::getBins
const Container & getBins() const
Returns the bins.
Definition: Histogram.hpp:71
Configuration.hpp
Graph.hpp
OpenMPCD::Histogram::Container
std::vector< BinContent > Container
The container for bins.
Definition: Histogram.hpp:24
OpenMPCD::Graph
Represents a 2D graph.
Definition: Graph.hpp:19