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

Last change on this file since 2038 was 2038, checked in by Peter, 12 years ago

closes #554 - added function rescale in Histogram.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1// $Id: Histogram.cc 2038 2009-08-25 20:53:31Z peter $
2
3/*
4  Copyright (C) 2004 Jari Häkkinen
5  Copyright (C) 2005 Peter Johansson
6  Copyright (C) 2006 Jari Häkkinen
7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009 Peter Johansson
9
10  This file is part of the yat library, http://dev.thep.lu.se/yat
11
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 3 of the
15  License, or (at your option) any later version.
16
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with yat. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include "Histogram.h"
27
28#include <algorithm>
29#include <cassert>
30#include <cmath>
31#include <functional>
32#include <ostream>
33
34namespace theplu {
35namespace yat {
36namespace statistics {
37
38
39  Histogram::Histogram(void)
40    : xmax_(0), xmin_(0), sum_all_(), sum_histogram_()
41  {
42  }
43
44
45  Histogram::Histogram(const Histogram& b)
46  {
47    *this=b;
48  }
49
50
51  Histogram::Histogram(const double min, const double max, const size_t n)
52    : histogram_(std::vector<double>(n,0.0)),
53      xmax_(max), xmin_(min),
54      sum_all_(), sum_histogram_()
55  {
56  }
57
58
59  Histogram::~Histogram(void)
60  {
61  }
62
63
64  int Histogram::add(const double x, const double w)
65  {
66    sum_all_.add(x,w);
67    if (x<xmin_)
68      return -1;
69    else if (x>=xmax_)
70      return 1;
71
72    sum_histogram_.add(x,w);
73    histogram_[bin(x)] += w;
74    return 0;
75  }
76
77
78  const AveragerWeighted& Histogram::averager_all(void) const
79  {
80    return sum_all_;
81  }
82
83
84  const AveragerWeighted& Histogram::averager_histogram(void) const
85  {
86    return sum_histogram_;
87  }
88
89
90  size_t Histogram::bin(double d)
91  {
92    return (((d<xmin_) || (d>xmax_)) ? 0 :
93            static_cast<size_t>(floor((d-xmin_)/spacing() )));
94  }
95
96
97  size_t Histogram::nof_bins(void) const
98  {
99    return histogram_.size();
100  }
101
102
103  void Histogram::normalize(bool choice)
104  {
105    if (choice) 
106      rescale(1.0/sum_all_.sum_w());
107    else
108      rescale(1.0/sum_all_.sum_w()/spacing());
109  }
110
111
112  double Histogram::observation_value(const size_t k) const
113  {
114    return xmin_+spacing()*(k+0.5);
115  }
116
117
118  void Histogram::rescale(double factor)
119  {
120    for (size_t i=0; i<histogram_.size(); i++)
121      histogram_[i]*=factor;
122  }
123
124
125  void Histogram::reset(void)
126  {
127    for (size_t i=0; i<histogram_.size(); i++)
128      histogram_[i]=0;
129    sum_all_.reset();
130    sum_histogram_.reset();
131  }
132
133
134  double Histogram::spacing(void) const
135  {
136    return (xmax_-xmin_)/nof_bins();
137  }
138
139
140  double Histogram::xmax(void) const
141  {
142    return xmax_;
143  }
144
145
146  double Histogram::xmin(void) const
147  {
148    return xmin_;
149  }
150
151
152  double Histogram::operator[](size_t k) const
153  {
154    return histogram_[k];
155  }
156
157
158  const Histogram& Histogram::operator=(const Histogram& b)
159  {
160    if (this==&b)
161      return *this;
162    histogram_=b.histogram_;
163    xmax_=b.xmax_;
164    xmin_=b.xmin_;
165    sum_all_=b.sum_all_;
166    sum_histogram_=b.sum_histogram_;
167    return *this;
168  }
169
170
171  const Histogram& Histogram::operator+=(const Histogram& rhs)
172  {
173    assert(histogram_.size()==rhs.histogram_.size());
174    std::transform(histogram_.begin(), histogram_.end(),
175                   rhs.histogram_.begin(), histogram_.begin(),
176                   std::plus<double>());
177    assert(xmax_==rhs.xmax_);
178    assert(xmin_==rhs.xmin_);
179    sum_all_ += rhs.sum_all_;
180    sum_histogram_ += rhs.sum_histogram_;
181    return *this;
182  }
183
184
185  std::ostream& operator<<(std::ostream& s,const Histogram& histogram)
186  {
187    s << "# histogram min : " << histogram.xmin() << '\n';
188    s << "# histogram max : " << histogram.xmax() << '\n';
189    s << "# number of bins: " << histogram.nof_bins() << '\n';
190    s << "# nof points in histogram : " 
191      << histogram.averager_histogram().sum_w() << '\n';
192    s << "# nof points in total:      " 
193      << histogram.averager_all().sum_w() << '\n';
194    s << "# column 1: center of observation bin\n"
195      << "# column 2: frequency\n";
196
197    for (size_t i=0; i<histogram.nof_bins(); i++) {
198      s.width(12);
199      s << histogram.observation_value(i);
200      s.width(12);
201      s << histogram[i] << '\n';
202    }
203
204    return s;
205  }
206
207}}} // of namespace statistics, yat, and theplu
Note: See TracBrowser for help on using the repository browser.