00001 #include "FastShowerUtils/FunctionStats.h"
00002 #include "FastShowerUtils/ParticleParameters.h"
00003 #include <iostream>
00004 #include <iomanip>
00005 #include <stream.h>
00006 #include <cmath>
00007
00008 using std::ostream;
00009 namespace FastShower{
00010
00011
00012 FunctionStats::FunctionStats():
00013 m_lastIsValid(false), m_lastX(0.), m_n(0), m_sumX(0.), m_sumXsq(0.){
00014 }
00015
00016 FunctionStats::FunctionStats(const FunctionStats& rhs){
00017 m_lastIsValid = rhs.m_lastIsValid;
00018 m_lastX = rhs.m_lastX;
00019 m_n = rhs.m_n;
00020 m_sumX = rhs.m_sumX;
00021 m_sumXsq = rhs.m_sumXsq;
00022 }
00023
00024 FunctionStats& FunctionStats::operator=(const FunctionStats& rhs){
00025 if (&rhs == this) return *this;
00026 m_lastIsValid = rhs.m_lastIsValid;
00027 m_lastX = rhs.m_lastX;
00028 m_n = rhs.m_n;
00029 m_sumX = rhs.m_sumX;
00030 m_sumXsq = rhs.m_sumXsq;
00031 return *this;
00032 }
00033
00034 void FunctionStats::bump(double x){
00035 m_lastIsValid=true;
00036 m_lastX=x;
00037 ++m_n;
00038 m_sumX+=x;
00039 m_sumXsq+=(x*x);
00040 }
00041
00042 void FunctionStats::bump(double x, const ParticleParameters*){
00043 this->bump(x);
00044 }
00045
00046 int FunctionStats::nCalls() const {return m_n;}
00047
00048 double FunctionStats::mean() const {
00049 return (m_n>0)? m_sumX/(static_cast<double>(m_n)) :0.;
00050 }
00051
00052 double FunctionStats::variance() const {
00053 if(m_n==0) return 0.;
00054
00055 double calls = static_cast<double>(m_n);
00056 double xSqAv = m_sumXsq/calls;
00057 double xAvSq = (m_sumX*m_sumX)/(calls*calls);
00058 return sqrt(abs(xSqAv-xAvSq));
00059
00060 }
00061
00062 double FunctionStats::lastX() const {return m_lastX;}
00063
00064 void FunctionStats::report(std::ostream& ostr) const {
00065
00066 ostr<<*this<<endl;
00067 }
00068 void FunctionStats::header(std::ostream& ostr) const {
00069 ostr.width(40);
00070 ostr<<"Calls";
00071 ostr.width(10);
00072 ostr<<"Last x";
00073 ostr.width(10);
00074 ostr<<"Mean";
00075 ostr.width(10);
00076 ostr<<"Variance";
00077 ostr<<endl;
00078 }
00079 std::ostream& operator<<(std::ostream& ostr, const FunctionStats& f){
00080 ostr.width(40);
00081 ostr<<f.nCalls()<<" ";
00082
00083 ostr.setf(ios::scientific, ios::floatfield);
00084 ostr.precision(3);
00085
00086 if(f.lastIsValid()){
00087 ostr<<f.lastX()<<" ";
00088 }else{
00089 ostr<<"---------"<<" ";
00090 }
00091 ostr<<f.mean()<<" "
00092 <<f.variance()<<" "
00093 ;
00094 return ostr;
00095 }
00096 void FunctionStats::prime(){m_lastIsValid=false;}
00097 bool FunctionStats::lastIsValid() const {return m_lastIsValid;}
00098 std::ostream& operator<<(std::ostream& ostr, const FunctionStats* f){
00099 ostr << *f;
00100 return ostr;
00101 }
00102 }
00103
00104
00105
00106
00107
00108