// Copyright (C) 2010, Guy Barrand. All rights reserved. // See the file tools.license for terms. #ifndef tools_histo_c1d #define tools_histo_cld #include "base_cloud" #include "../mnmx" #include "h1d" namespace tools { namespace histo { class c1d : public base_cloud { public: static const std::string& s_class() { static const std::string s_v("tools::histo::c1d"); return s_v; } public: bool set_title(const std::string& a_title){ m_title = a_title; if(m_histo) m_histo->set_title(a_title); return true; } unsigned int dimension() const {return 1;} bool reset() { clear(); delete m_histo; m_histo = 0; return true; } unsigned int entries() const { return m_histo ? m_histo->all_entries() : m_ws.size(); } public: double sum_of_weights() const { return (m_histo ? m_histo->sum_bin_heights() : m_Sw); } bool convert_to_histogram(){ if( (m_cnv_x_num<=0) || (m_cnv_x_max<=m_cnv_x_min) ) { // Cloud min, max should be included in the histo. double dx = 0.01 * (upper_edge() - lower_edge())/BINS(); return convert(BINS(),lower_edge(),upper_edge() + dx); } else { return convert(m_cnv_x_num,m_cnv_x_min,m_cnv_x_max); } } bool is_converted() const {return m_histo ? true : false;} bool scale(double a_scale) { if(m_histo) { return m_histo->scale(a_scale); } else { unsigned int number = m_ws.size(); for(unsigned int index=0;index=m_limit)){ convert_to_histogram(); } if(m_histo) { return m_histo->fill(aX,aW); } else { if(m_xs.size()) { m_lower_x = mn(aX,m_lower_x); m_upper_x = mx(aX,m_upper_x); } else { m_lower_x = aX; m_upper_x = aX; } m_xs.push_back(aX); m_ws.push_back(aW); m_Sw += aW; double xw = aX * aW; m_Sxw += xw; m_Sx2w += aX * xw; return true; } } double lower_edge() const { return (m_histo ? m_histo->axis().lower_edge() : m_lower_x); } double upper_edge() const { return (m_histo ? m_histo->axis().upper_edge() : m_upper_x); } double value(unsigned int aIndex) const {return (m_histo ?0:m_xs[aIndex]);} double weight(unsigned int aIndex) const {return (m_histo ?0:m_ws[aIndex]);} double mean() const { return (m_histo ? m_histo->mean() : (m_Sw?m_Sxw/m_Sw:0)); } double rms() const { double _rms = 0; //FIXME nan. if(m_histo) { _rms = m_histo->rms(); } else { if(m_Sw==0) { } else { double _mean = m_Sxw / m_Sw; _rms = ::sqrt(::fabs( (m_Sx2w / m_Sw) - _mean * _mean)); } } return _rms; } bool convert(unsigned int aBins,double aLowerEdge,double aUpperEdge){ if(m_histo) return true; m_histo = new histo::h1d(base_cloud::title(),aBins,aLowerEdge,aUpperEdge); if(!m_histo) return false; bool status = fill_histogram(*m_histo); clear(); return status; } bool convert(const std::vector& aEdges) { if(m_histo) return true; m_histo = new histo::h1d(base_cloud::title(),aEdges); if(!m_histo) return false; bool status = fill_histogram(*m_histo); clear(); return status; } const histo::h1d& histogram() const { if(!m_histo) const_cast(*this).convert_to_histogram(); return *m_histo; } bool fill_histogram(histo::h1d& a_histo) const { unsigned int number = m_xs.size(); for(unsigned int index=0;index m_xs; double m_lower_x; double m_upper_x; double m_Sxw; double m_Sx2w; // unsigned int m_cnv_x_num; double m_cnv_x_min; double m_cnv_x_max; histo::h1d* m_histo; }; }} #endif