source: trunk/src/Histogram.cc @ 215

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

Continued work on new Histogram.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.9 KB
Line 
1// $Id: Histogram.cc 215 2004-11-06 23:07:03Z jari $
2
3#include "Histogram.h"
4
5#include <cmath>
6#include <fstream>
7
8
9namespace theplu {
10namespace cpptools {
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  std::cout << "Histogram::add Jari, check me!" << std::endl;
45
46  sum_all_.add(x,w);
47  if (x<xmin_)
48    return -1;
49  else if (x>=xmax_)
50    return 1;
51 
52  // Peter fill this.
53  sum_all_.add(x,w);
54  return 0;
55}
56
57
58
59void Histogram::normalize(double scale_factor)
60{
61  for (size_t i=0; i<histogram_.size(); i++)
62    histogram_[i]/=scale_factor;
63}
64
65
66
67void Histogram::reset(void)
68{
69  for (u_int i=0; i<histogram_.size(); i++)
70    histogram_[i]=0;
71  sum_all_.reset();
72  sum_histogram_.reset();
73}
74
75
76
77const Histogram& Histogram::operator=(const Histogram& b)
78{
79  if (this==&b)
80    return *this;
81  histogram_=b.histogram_;
82  xmax_=b.xmax_;
83  xmin_=b.xmin_;
84  sum_all_=b.sum_all_;
85  sum_histogram_=b.sum_histogram_;
86  return *this;
87}
88
89
90
91std::ostream& operator<<(std::ostream& s,const Histogram& histogram)
92{
93  s << "# histogram min : " << histogram.xmin() << '\n';
94  s << "# histogram max : " << histogram.xmax() << '\n';
95  s << "# number of bins: " << histogram.nof_bins() << '\n';
96  s << "# histogram averager: " << histogram.averager_histogram() << '\n';
97  s << "#       all averager: " << histogram.averager_all() << '\n';
98
99  s << "# column 1: center of observation bin\n"
100    << "# column 2: frequency\n";
101
102  for (u_int i=0; i<histogram.nof_bins(); i++) {
103    s.width(12);
104    s << histogram.xmin()+(i+0.5)*histogram.spacing();
105    s.width(12);
106    s << histogram[i] << '\n';
107  }
108
109  return s;
110}
111
112}} // of namespace cpptools and namespace theplu
Note: See TracBrowser for help on using the repository browser.