1 | // $Id: Histogram.cc 231 2005-02-21 14:50:17Z peter $ |
---|
2 | |
---|
3 | #include "Histogram.h" |
---|
4 | |
---|
5 | #include <cmath> |
---|
6 | #include <fstream> |
---|
7 | |
---|
8 | |
---|
9 | namespace theplu { |
---|
10 | namespace cpptools { |
---|
11 | |
---|
12 | |
---|
13 | Histogram::Histogram(void) |
---|
14 | : xmax_(0), xmin_(0), sum_all_(), sum_histogram_() |
---|
15 | { |
---|
16 | } |
---|
17 | |
---|
18 | |
---|
19 | |
---|
20 | Histogram::Histogram(const Histogram& b) |
---|
21 | { |
---|
22 | *this=b; |
---|
23 | } |
---|
24 | |
---|
25 | |
---|
26 | |
---|
27 | Histogram::Histogram(const double min, const double max, const size_t n) |
---|
28 | : histogram_(std::vector<double>(n,0.0)), |
---|
29 | xmax_(max), xmin_(min), |
---|
30 | sum_all_(), sum_histogram_() |
---|
31 | { |
---|
32 | } |
---|
33 | |
---|
34 | |
---|
35 | |
---|
36 | Histogram::~Histogram(void) |
---|
37 | { |
---|
38 | } |
---|
39 | |
---|
40 | |
---|
41 | |
---|
42 | int Histogram::add(const double x, const double w) |
---|
43 | { |
---|
44 | sum_all_.add(x,w); |
---|
45 | if (x<xmin_) |
---|
46 | return -1; |
---|
47 | else if (x>=xmax_) |
---|
48 | return 1; |
---|
49 | |
---|
50 | sum_histogram_.add(x,w); |
---|
51 | histogram_[bin(x)] += w; |
---|
52 | return 0; |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | |
---|
57 | void Histogram::normalize(double scale_factor) |
---|
58 | { |
---|
59 | for (size_t i=0; i<histogram_.size(); i++) |
---|
60 | histogram_[i]/=scale_factor; |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | void Histogram::reset(void) |
---|
65 | { |
---|
66 | for (u_int i=0; i<histogram_.size(); i++) |
---|
67 | histogram_[i]=0; |
---|
68 | sum_all_.reset(); |
---|
69 | sum_histogram_.reset(); |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | |
---|
74 | const Histogram& Histogram::operator=(const Histogram& b) |
---|
75 | { |
---|
76 | if (this==&b) |
---|
77 | return *this; |
---|
78 | histogram_=b.histogram_; |
---|
79 | xmax_=b.xmax_; |
---|
80 | xmin_=b.xmin_; |
---|
81 | sum_all_=b.sum_all_; |
---|
82 | sum_histogram_=b.sum_histogram_; |
---|
83 | return *this; |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | |
---|
88 | std::ostream& operator<<(std::ostream& s,const Histogram& histogram) |
---|
89 | { |
---|
90 | s << "# histogram min : " << histogram.xmin() << '\n'; |
---|
91 | s << "# histogram max : " << histogram.xmax() << '\n'; |
---|
92 | s << "# number of bins: " << histogram.nof_bins() << '\n'; |
---|
93 | s << "# nof points in histogram : " |
---|
94 | << histogram.averager_histogram().sum_w() << '\n'; |
---|
95 | s << "# nof points in total: " |
---|
96 | << histogram.averager_all().sum_w() << '\n'; |
---|
97 | s << "# column 1: center of observation bin\n" |
---|
98 | << "# column 2: frequency\n"; |
---|
99 | |
---|
100 | for (u_int i=0; i<histogram.nof_bins(); i++) { |
---|
101 | s.width(12); |
---|
102 | s << histogram.observation_value(i); |
---|
103 | s.width(12); |
---|
104 | s << histogram[i] << '\n'; |
---|
105 | } |
---|
106 | |
---|
107 | return s; |
---|
108 | } |
---|
109 | |
---|
110 | }} // of namespace cpptools and namespace theplu |
---|