1 | #include <fstream> |
---|

2 | #include <iostream> |
---|

3 | #include <string> |
---|

4 | #include <gsl/gsl_histogram.h> |
---|

5 | |
---|

6 | |
---|

7 | #ifndef _THEP_CPPTOOLS_HISTOGRAM_ |
---|

8 | #define _THEP_CPPTOOLS_HISTOGRAM_ |
---|

9 | |
---|

10 | namespace thep_cpp_tools |
---|

11 | { |
---|

12 | /** |
---|

13 | Class for binning of data. |
---|

14 | */ |
---|

15 | class histogram |
---|

16 | { |
---|

17 | public: |
---|

18 | /** |
---|

19 | Constructor taking three arguments. \n |
---|

20 | nbins = number of bins to split intervall between (uniformly) \n |
---|

21 | min = min-value \n |
---|

22 | max = max-value \n |
---|

23 | */ |
---|

24 | histogram( const size_t& nbins, const double& min, const double& max ); |
---|

25 | |
---|

26 | |
---|

27 | /** |
---|

28 | This function loads a text file into the histogram. The text |
---|

29 | file has two columns. The first is the value and the second is |
---|

30 | the intensity. |
---|

31 | */ |
---|

32 | void load_from_text_file( const std::string& filename ); |
---|

33 | |
---|

34 | |
---|

35 | /** |
---|

36 | Returns the intensity corresponding to bin \a n. |
---|

37 | */ |
---|

38 | inline double get_intensity( const size_t& n ) const; |
---|

39 | |
---|

40 | |
---|

41 | /** |
---|

42 | Returns the range corresponding to bin \a n |
---|

43 | */ |
---|

44 | inline void get_range( const size_t& n, double& min, double& max ) const; |
---|

45 | |
---|

46 | |
---|

47 | /** |
---|

48 | Returns the number of bins in the distribution. |
---|

49 | */ |
---|

50 | inline size_t get_bins() const; |
---|

51 | |
---|

52 | |
---|

53 | /** |
---|

54 | Returns a random number from the cumulative |
---|

55 | distribution defined by data of histogram. |
---|

56 | (need to run init_probabilities() first). |
---|

57 | The parameter \a r is the value of the y-axis |
---|

58 | which is between [0,1]. If \a r is drawn from a |
---|

59 | uniform random distribution, the corresponding |
---|

60 | value out is the random-value of the data in |
---|

61 | the histogram. |
---|

62 | */ |
---|

63 | inline double get_probability( const double& r ); |
---|

64 | |
---|

65 | |
---|

66 | /** |
---|

67 | Initialize probability function. |
---|

68 | */ |
---|

69 | void init_probabilities(); |
---|

70 | |
---|

71 | /** |
---|

72 | standard output operator. |
---|

73 | */ |
---|

74 | friend std::ostream& thep_cpp_tools::operator<< |
---|

75 | ( std::ostream& s_out, const histogram& hist ); |
---|

76 | |
---|

77 | |
---|

78 | /** |
---|

79 | Destructor freeing memory of internal GSL pointers. |
---|

80 | */ |
---|

81 | ~histogram(); |
---|

82 | |
---|

83 | private: |
---|

84 | gsl_histogram* h_; |
---|

85 | gsl_histogram_pdf* p_; |
---|

86 | }; |
---|

87 | |
---|

88 | #include "histogram.h" |
---|

89 | |
---|

90 | } |
---|

91 | |
---|

92 | #endif |
---|