1 | #include "histogram.h" |
---|
2 | |
---|
3 | |
---|
4 | using namespace std; |
---|
5 | using namespace thep_cpp_tools; |
---|
6 | |
---|
7 | |
---|
8 | // Constructors and destructors |
---|
9 | //////////////////////////////// |
---|
10 | histogram::histogram( const size_t& nbins, const double& min, const double& max ) |
---|
11 | { |
---|
12 | h_ = gsl_histogram_alloc( nbins ); |
---|
13 | gsl_histogram_set_ranges_uniform( h_, min, max ); |
---|
14 | } |
---|
15 | |
---|
16 | |
---|
17 | histogram::~histogram() |
---|
18 | { |
---|
19 | gsl_histogram_free( h_ ); |
---|
20 | if( p_ != NULL ) gsl_histogram_pdf_free( p_ ); |
---|
21 | } |
---|
22 | |
---|
23 | |
---|
24 | // Functions (A-Z) |
---|
25 | ////////////////// |
---|
26 | void histogram::init_probabilities() |
---|
27 | { |
---|
28 | p_ = gsl_histogram_pdf_alloc( get_bins() ); |
---|
29 | gsl_histogram_pdf_init( p_, h_ ); |
---|
30 | } |
---|
31 | |
---|
32 | |
---|
33 | void histogram::load_from_text_file( const string& filename ) |
---|
34 | { |
---|
35 | ifstream fin( filename.c_str() ); |
---|
36 | if( !fin ) { cerr << "No such file: " << filename << endl; exit( -1 ); } |
---|
37 | while( fin.peek() != EOF ) |
---|
38 | { |
---|
39 | double val, intensity; |
---|
40 | fin >> val >> intensity; |
---|
41 | assert( intensity >= 0 ); |
---|
42 | gsl_histogram_accumulate( h_, val, intensity );//intensity ); |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | // Operators |
---|
48 | //////////// |
---|
49 | std::ostream& |
---|
50 | thep_cpp_tools::operator<< ( std::ostream& s_out, |
---|
51 | const thep_cpp_tools::histogram& hist ) |
---|
52 | { |
---|
53 | for( size_t i = 0; i < hist.get_bins(); ++i ) |
---|
54 | { |
---|
55 | double low, high; |
---|
56 | cout << i << endl; |
---|
57 | hist.get_range( i, low, high ); |
---|
58 | s_out << "[ " << low << ", " << high << " ]\t=\t" |
---|
59 | << hist.get_intensity( i ) << endl; |
---|
60 | } |
---|
61 | return s_out; |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | |
---|
66 | |
---|
67 | |
---|