source: trunk/lib/statistics/Histogram.cc @ 372

Last change on this file since 372 was 372, checked in by Peter, 17 years ago

modified function normalize in histogram

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.0 KB
Line 
1// $Id: Histogram.cc 372 2005-08-05 14:14:02Z peter $
2
3#include <c++_tools/statistics/Histogram.h>
4
5#include <cmath>
6#include <fstream>
7
8
9namespace theplu {
10namespace statistics {
11
12
13Histogram::Histogram(void)
14  : xmax_(0), xmin_(0), sum_all_(), sum_histogram_()
15{
16}
17
18
19
20Histogram::Histogram(const Histogram& b)
21{
22  *this=b;
23}
24
25
26
27Histogram::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
36Histogram::~Histogram(void)
37{
38}
39
40
41
42int 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
57void Histogram::normalize(bool choice)
58{
59  double scale_factor;
60  if (choice) 
61    scale_factor = sum_all_.sum_w();
62  else
63    scale_factor = sum_all_.sum_w()*spacing();
64  for (size_t i=0; i<histogram_.size(); i++)
65    histogram_[i]/=scale_factor;
66
67}
68
69
70void Histogram::reset(void)
71{
72  for (u_int i=0; i<histogram_.size(); i++)
73    histogram_[i]=0;
74  sum_all_.reset();
75  sum_histogram_.reset();
76}
77
78
79
80const Histogram& Histogram::operator=(const Histogram& b)
81{
82  if (this==&b)
83    return *this;
84  histogram_=b.histogram_;
85  xmax_=b.xmax_;
86  xmin_=b.xmin_;
87  sum_all_=b.sum_all_;
88  sum_histogram_=b.sum_histogram_;
89  return *this;
90}
91
92
93
94std::ostream& operator<<(std::ostream& s,const Histogram& histogram)
95{
96  s << "# histogram min : " << histogram.xmin() << '\n';
97  s << "# histogram max : " << histogram.xmax() << '\n';
98  s << "# number of bins: " << histogram.nof_bins() << '\n';
99  s << "# nof points in histogram : " 
100    << histogram.averager_histogram().sum_w() << '\n';
101  s << "# nof points in total:      " 
102    << histogram.averager_all().sum_w() << '\n';
103  s << "# column 1: center of observation bin\n"
104    << "# column 2: frequency\n";
105
106  for (u_int i=0; i<histogram.nof_bins(); i++) {
107    s.width(12);
108    s << histogram.observation_value(i);
109    s.width(12);
110    s << histogram[i] << '\n';
111  }
112
113  return s;
114}
115
116}} // of namespace statistics and namespace theplu
Note: See TracBrowser for help on using the repository browser.