source: trunk/yat/statistics/Histogram.cc @ 718

Last change on this file since 718 was 718, checked in by Jari Häkkinen, 15 years ago

Addresses #170.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 KB
Line 
1// $Id: Histogram.cc 718 2006-12-26 09:56:26Z 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 "Histogram.h"
25
26#include <cmath>
27#include <fstream>
28
29namespace theplu {
30namespace yat {
31namespace statistics {
32
33
34  Histogram::Histogram(void)
35    : xmax_(0), xmin_(0), sum_all_(), sum_histogram_()
36  {
37  }
38
39
40  Histogram::Histogram(const Histogram& b)
41  {
42    *this=b;
43  }
44
45
46  Histogram::Histogram(const double min, const double max, const size_t n)
47    : histogram_(std::vector<double>(n,0.0)),
48      xmax_(max), xmin_(min),
49      sum_all_(), sum_histogram_()
50  {
51  }
52
53
54  Histogram::~Histogram(void)
55  {
56  }
57
58
59  int Histogram::add(const double x, const double w)
60  {
61    sum_all_.add(x,w);
62    if (x<xmin_)
63      return -1;
64    else if (x>=xmax_)
65      return 1;
66
67    sum_histogram_.add(x,w);
68    histogram_[bin(x)] += w;
69    return 0;
70  }
71
72
73  const statistics::AveragerWeighted& Histogram::averager_all(void) const
74  {
75    return sum_all_;
76  }
77
78
79  const statistics::AveragerWeighted& Histogram::averager_histogram(void) const
80  {
81    return sum_histogram_;
82  }
83
84
85  size_t Histogram::bin(double d)
86  {
87    return (((d<xmin_) || (d>xmax_)) ? 0 :
88            static_cast<size_t>(floor((d-xmin_)/spacing() )));
89  }
90
91
92  size_t Histogram::nof_bins(void) const
93  {
94    return histogram_.size();
95  }
96
97
98  void Histogram::normalize(bool choice)
99  {
100    double scale_factor;
101    if (choice) 
102      scale_factor = sum_all_.sum_w();
103    else
104      scale_factor = sum_all_.sum_w()*spacing();
105    for (size_t i=0; i<histogram_.size(); i++)
106      histogram_[i]/=scale_factor;
107  }
108
109
110  double Histogram::observation_value(const size_t k) const
111  {
112    return xmin_+spacing()*(k+0.5);
113  }
114
115
116  void Histogram::reset(void)
117  {
118    for (u_int i=0; i<histogram_.size(); i++)
119      histogram_[i]=0;
120    sum_all_.reset();
121    sum_histogram_.reset();
122  }
123
124
125  double Histogram::spacing(void) const
126  {
127    return (xmax_-xmin_)/nof_bins();
128  }
129
130
131  double Histogram::xmax(void) const
132  {
133    return xmax_;
134  }
135
136
137  double Histogram::xmin(void) const
138  {
139    return xmin_;
140  }
141
142
143  double Histogram::operator[](size_t k) const
144  {
145    return histogram_[k];
146  }
147
148
149  const Histogram& Histogram::operator=(const Histogram& b)
150  {
151    if (this==&b)
152      return *this;
153    histogram_=b.histogram_;
154    xmax_=b.xmax_;
155    xmin_=b.xmin_;
156    sum_all_=b.sum_all_;
157    sum_histogram_=b.sum_histogram_;
158    return *this;
159  }
160
161
162  std::ostream& operator<<(std::ostream& s,const Histogram& histogram)
163  {
164    s << "# histogram min : " << histogram.xmin() << '\n';
165    s << "# histogram max : " << histogram.xmax() << '\n';
166    s << "# number of bins: " << histogram.nof_bins() << '\n';
167    s << "# nof points in histogram : " 
168      << histogram.averager_histogram().sum_w() << '\n';
169    s << "# nof points in total:      " 
170      << histogram.averager_all().sum_w() << '\n';
171    s << "# column 1: center of observation bin\n"
172      << "# column 2: frequency\n";
173
174    for (u_int i=0; i<histogram.nof_bins(); i++) {
175      s.width(12);
176      s << histogram.observation_value(i);
177      s.width(12);
178      s << histogram[i] << '\n';
179    }
180
181    return s;
182  }
183
184}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.