1 | // $Id: histogram.h 40 2004-02-20 14:03:25Z jari $ |
---|
2 | |
---|
3 | #ifndef _THEP_CPPTOOLS_HISTOGRAM_ |
---|
4 | #define _THEP_CPPTOOLS_HISTOGRAM_ |
---|
5 | |
---|
6 | #include <fstream> |
---|
7 | #include <iostream> |
---|
8 | #include <string> |
---|
9 | #include <gsl/gsl_histogram.h> |
---|
10 | |
---|
11 | |
---|
12 | namespace thep_cpp_tools |
---|
13 | { |
---|
14 | /** |
---|
15 | Class for binning of data. |
---|
16 | */ |
---|
17 | class histogram |
---|
18 | { |
---|
19 | public: |
---|
20 | /** |
---|
21 | Constructor taking three arguments. \n |
---|
22 | nbins = number of bins to split intervall between (uniformly) \n |
---|
23 | min = min-value \n |
---|
24 | max = max-value \n |
---|
25 | */ |
---|
26 | histogram( const size_t& nbins, const double& min, const double& max ); |
---|
27 | |
---|
28 | |
---|
29 | /** |
---|
30 | This function loads a text file into the histogram. The text |
---|
31 | file has two columns. The first is the value and the second is |
---|
32 | the intensity. |
---|
33 | */ |
---|
34 | void load_from_text_file( const std::string& filename ); |
---|
35 | |
---|
36 | |
---|
37 | /** |
---|
38 | Returns the intensity corresponding to bin \a n. |
---|
39 | */ |
---|
40 | inline double get_intensity( const size_t& n ) const |
---|
41 | { return gsl_histogram_get(h_,n); } |
---|
42 | |
---|
43 | |
---|
44 | /** |
---|
45 | Returns the range corresponding to bin \a n |
---|
46 | */ |
---|
47 | inline void get_range( const size_t& n, double& min, double& max ) const |
---|
48 | { gsl_histogram_get_range(h_,n,&min,&max); } |
---|
49 | |
---|
50 | |
---|
51 | /** |
---|
52 | Returns the number of bins in the distribution. |
---|
53 | */ |
---|
54 | inline size_t get_bins() const { return gsl_histogram_bins(h_); } |
---|
55 | |
---|
56 | |
---|
57 | /** |
---|
58 | Returns a random number from the cumulative |
---|
59 | distribution defined by data of histogram. |
---|
60 | (need to run init_probabilities() first). |
---|
61 | The parameter \a r is the value of the y-axis |
---|
62 | which is between [0,1]. If \a r is drawn from a |
---|
63 | uniform random distribution, the corresponding |
---|
64 | value out is the random-value of the data in |
---|
65 | the histogram. |
---|
66 | */ |
---|
67 | inline double get_probability( const double& r ) |
---|
68 | { return gsl_histogram_pdf_sample(p_,r); } |
---|
69 | |
---|
70 | |
---|
71 | /** |
---|
72 | Initialize probability function. |
---|
73 | */ |
---|
74 | void init_probabilities(); |
---|
75 | |
---|
76 | /** |
---|
77 | standard output operator. |
---|
78 | */ |
---|
79 | friend std::ostream& thep_cpp_tools::operator<< |
---|
80 | ( std::ostream& s_out, const histogram& hist ); |
---|
81 | |
---|
82 | |
---|
83 | /** |
---|
84 | Destructor freeing memory of internal GSL pointers. |
---|
85 | */ |
---|
86 | ~histogram(); |
---|
87 | |
---|
88 | private: |
---|
89 | gsl_histogram* h_; |
---|
90 | gsl_histogram_pdf* p_; |
---|
91 | }; |
---|
92 | |
---|
93 | |
---|
94 | } |
---|
95 | |
---|
96 | #endif |
---|