Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Histogram.cxx

Go to the documentation of this file.
00001 // file: Histogram.cxx
00002 // histogra.cpp,v 1.1.1.1 1994/04/18 18:12:27 burnett Exp
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 // pointer to the Histogram list
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     // setup linked list
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 /*void
00051 Histogram::printAll(const char* filename)
00052 {
00053   ofstream file(filename);
00054 //  if( file.bad() ){
00055 //    WARNING("Histogram: could not open file");
00056     assert(~file.bad());
00057 
00058  //   return;
00059   //}
00060   printAll(file);
00061   file.close();
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 }

Generated at Wed Nov 21 12:20:52 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000