1 | // $Id: Histogram.cc 675 2006-10-10 12:08:45Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) The authors contributing to this file. |
---|
5 | |
---|
6 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
7 | |
---|
8 | The yat library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU General Public License as |
---|
10 | published by the Free Software Foundation; either version 2 of the |
---|
11 | License, or (at your option) any later version. |
---|
12 | |
---|
13 | The yat library is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
21 | 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "yat/statistics/Histogram.h" |
---|
25 | |
---|
26 | #include <cmath> |
---|
27 | #include <fstream> |
---|
28 | |
---|
29 | |
---|
30 | namespace theplu { |
---|
31 | namespace statistics { |
---|
32 | |
---|
33 | |
---|
34 | Histogram::Histogram(void) |
---|
35 | : xmax_(0), xmin_(0), sum_all_(), sum_histogram_() |
---|
36 | { |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | Histogram::Histogram(const Histogram& b) |
---|
42 | { |
---|
43 | *this=b; |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | |
---|
48 | Histogram::Histogram(const double min, const double max, const size_t n) |
---|
49 | : histogram_(std::vector<double>(n,0.0)), |
---|
50 | xmax_(max), xmin_(min), |
---|
51 | sum_all_(), sum_histogram_() |
---|
52 | { |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | |
---|
57 | Histogram::~Histogram(void) |
---|
58 | { |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | |
---|
63 | int Histogram::add(const double x, const double w) |
---|
64 | { |
---|
65 | sum_all_.add(x,w); |
---|
66 | if (x<xmin_) |
---|
67 | return -1; |
---|
68 | else if (x>=xmax_) |
---|
69 | return 1; |
---|
70 | |
---|
71 | sum_histogram_.add(x,w); |
---|
72 | histogram_[bin(x)] += w; |
---|
73 | return 0; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | |
---|
78 | void Histogram::normalize(bool choice) |
---|
79 | { |
---|
80 | double scale_factor; |
---|
81 | if (choice) |
---|
82 | scale_factor = sum_all_.sum_w(); |
---|
83 | else |
---|
84 | scale_factor = sum_all_.sum_w()*spacing(); |
---|
85 | for (size_t i=0; i<histogram_.size(); i++) |
---|
86 | histogram_[i]/=scale_factor; |
---|
87 | |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | void Histogram::reset(void) |
---|
92 | { |
---|
93 | for (u_int i=0; i<histogram_.size(); i++) |
---|
94 | histogram_[i]=0; |
---|
95 | sum_all_.reset(); |
---|
96 | sum_histogram_.reset(); |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | const Histogram& Histogram::operator=(const Histogram& b) |
---|
102 | { |
---|
103 | if (this==&b) |
---|
104 | return *this; |
---|
105 | histogram_=b.histogram_; |
---|
106 | xmax_=b.xmax_; |
---|
107 | xmin_=b.xmin_; |
---|
108 | sum_all_=b.sum_all_; |
---|
109 | sum_histogram_=b.sum_histogram_; |
---|
110 | return *this; |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | std::ostream& operator<<(std::ostream& s,const Histogram& histogram) |
---|
116 | { |
---|
117 | s << "# histogram min : " << histogram.xmin() << '\n'; |
---|
118 | s << "# histogram max : " << histogram.xmax() << '\n'; |
---|
119 | s << "# number of bins: " << histogram.nof_bins() << '\n'; |
---|
120 | s << "# nof points in histogram : " |
---|
121 | << histogram.averager_histogram().sum_w() << '\n'; |
---|
122 | s << "# nof points in total: " |
---|
123 | << histogram.averager_all().sum_w() << '\n'; |
---|
124 | s << "# column 1: center of observation bin\n" |
---|
125 | << "# column 2: frequency\n"; |
---|
126 | |
---|
127 | for (u_int i=0; i<histogram.nof_bins(); i++) { |
---|
128 | s.width(12); |
---|
129 | s << histogram.observation_value(i); |
---|
130 | s.width(12); |
---|
131 | s << histogram[i] << '\n'; |
---|
132 | } |
---|
133 | |
---|
134 | return s; |
---|
135 | } |
---|
136 | |
---|
137 | }} // of namespace statistics and namespace theplu |
---|