00001
00002
00003
00004 #include "analysis/Histogram.h"
00005
00006 #include <math.h>
00007 #include <cstdio>
00008 #include <assert.h>
00009
00010 using namespace std;
00011
00012 Histogram* first=0;
00013
00014
00015 Histogram* firstHist(){return first;}
00016
00017 Histogram::Histogram(std::string title, double from, double to, double step)
00018 : SampleStatistic()
00019 , m_title(title)
00020 , m_from(from)
00021 , m_to(to)
00022 , m_step( step? step : fabs(to-from)/25.0)
00023 {
00024 setRange(from, to , step);
00025
00026
00027 if( first==0 )
00028 first = this;
00029 else {
00030 Histogram* h= first;
00031 while( h->m_next)
00032 h=h->m_next;
00033 h->m_next = this;
00034 }
00035
00036 m_next = 0;
00037 }
00038 void Histogram::setRange(double from, double to, double step)
00039 {
00040 m_from = from;
00041 m_to = to;
00042 m_step = step? step : fabs(to-from)/25.0;
00043
00044 m_bins = unsigned(0.5+fabs(m_to-m_from)/m_step);
00045 m_hist.resize(m_bins);
00046 clear();
00047 }
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 void
00065 clearAllHists()
00066 {
00067 for(Histogram* h = first; h; h = h->next())
00068 h->clear();
00069 }
00070 Histogram::~Histogram()
00071 {
00072 }
00073 void Histogram::clear()
00074 {
00075 for(unsigned i=0; i< m_bins; i++)
00076 m_hist[i]=(float)0.0;
00077 m_total=m_under=m_over=0;
00078 SampleStatistic::reset();
00079 }
00080 void
00081 printAllHists(ostream& cout)
00082 {
00083 for(Histogram* h = first; h; h = h->next())
00084 h->print(cout);
00085 }
00086
00087 void
00088 Histogram::fill(double x, double w)
00089 {
00090 unsigned index ;
00091 m_total+=w;
00092 if( x<m_from) m_under+=w;
00093 else if( (index = (unsigned)((x-m_from)/m_step)) >= m_bins) m_over+=w;
00094 else (m_hist[index])+=(float)w;
00095
00096 SampleStatistic::operator+=(x);
00097 }
00098 void Histogram::print(ostream& cout)
00099 {
00100 ostream& os = cout;
00101 os << "---------------------------------\n";
00102 os << m_title << '\n';
00103 os << m_from << ',' << m_to << ',' << m_step << '\n';
00104 os << "Total: " << total()
00105 << ", under: " << under()
00106 << ", over: " << over() << '\n';
00107 os << "Mean: " << mean()
00108 << ", RMS: " << stdDev()
00109 << ", Min: " << min()
00110 << ", Max: " << max() << '\n';
00111 double x = m_from;
00112 os << "Left Edge Contents\n";
00113 for(unsigned i = 0; i< m_bins ; i++) {
00114 char buff[40];
00115 if( fabs(x) < m_step*1.e-5) x=0;
00116 sprintf(buff,"%10.5g%10.0f\n", x, m_hist[i]);
00117 os << buff; x+=m_step;
00118 }
00119 os.flush();
00120 }