inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/histo/c1d
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_histo_c1d
00005 #define inlib_histo_cld
00006 
00007 #include "base_cloud"
00008 
00009 #include <inlib/mnmx>
00010 
00011 #include "h1d"
00012 
00013 namespace inlib {
00014 namespace histo {
00015 
00016 class c1d : public base_cloud {
00017 public:
00018   bool set_title(const std::string& a_title){
00019     m_title = a_title;
00020     if(m_histo) m_histo->set_title(a_title);
00021     return true;
00022   }
00023 
00024   unsigned int dimension() const {return 1;}
00025   bool reset() { 
00026     clear();
00027     delete m_histo;
00028     m_histo = 0;
00029     return true;
00030   }
00031   unsigned int entries() const { 
00032     return m_histo ? m_histo->all_entries() : m_ws.size();
00033   }
00034 public:
00035   double sum_of_weights() const { 
00036     return (m_histo ? m_histo->sum_bin_heights() : m_Sw);
00037   }
00038 
00039   bool convert_to_histogram(){
00040     if( (m_cnv_x_num<=0) || (m_cnv_x_max<=m_cnv_x_min) ) {
00041       // Cloud min, max should be included in the histo.
00042       double dx = 0.01 * (upper_edge() - lower_edge())/BINS();
00043       return convert(BINS(),lower_edge(),upper_edge() + dx);
00044     } else {
00045       return convert(m_cnv_x_num,m_cnv_x_min,m_cnv_x_max);
00046     }
00047   }
00048 
00049   bool is_converted() const {return m_histo ? true : false;}
00050   bool scale(double a_scale) {
00051     if(m_histo) {
00052       return m_histo->scale(a_scale);
00053     } else {
00054       unsigned int number = m_ws.size();
00055       for(unsigned int index=0;index<number;index++) m_ws[index] *= a_scale;
00056       m_Sw *= a_scale;
00057       m_Sxw *= a_scale;
00058       m_Sx2w *= a_scale;
00059       return true;
00060     }
00061   }
00062 
00063 public:
00064   bool fill(double aX,double aW = 1){
00065     if(!m_histo && (m_limit!=UNLIMITED()) && 
00066        ((int)m_xs.size()>=m_limit)){
00067       convert_to_histogram();
00068     }
00069   
00070     if(m_histo) {
00071       return m_histo->fill(aX,aW);
00072     } else {
00073       if(m_xs.size()) {
00074         m_lower_x = inlib::mn<double>(aX,m_lower_x);
00075         m_upper_x = inlib::mx<double>(aX,m_upper_x);
00076       } else {
00077         m_lower_x = aX;
00078         m_upper_x = aX;
00079       }
00080       m_xs.push_back(aX);
00081       m_ws.push_back(aW);
00082       m_Sw += aW;
00083       double xw = aX * aW;
00084       m_Sxw += xw;
00085       m_Sx2w += aX * xw;
00086       return true;
00087     }
00088   }
00089 
00090   double lower_edge() const { 
00091     return (m_histo ? m_histo->axis().lower_edge() : m_lower_x);
00092   }
00093   double upper_edge() const { 
00094     return (m_histo ? m_histo->axis().upper_edge() : m_upper_x);
00095   }
00096   double value(unsigned int aIndex) const {return (m_histo ?0:m_xs[aIndex]);}
00097   double weight(unsigned int aIndex) const {return (m_histo ?0:m_ws[aIndex]);}
00098   double mean() const {
00099     return (m_histo ? m_histo->mean() : (m_Sw?m_Sxw/m_Sw:0));
00100   }
00101   double rms() const {
00102     double rms = 0; //FIXME nan.
00103     if(m_histo) {
00104       rms = m_histo->rms();
00105     } else {
00106       if(m_Sw==0) {
00107       } else {
00108         double mean = m_Sxw / m_Sw;
00109         rms = ::sqrt(::fabs( (m_Sx2w / m_Sw) - mean * mean));
00110       }
00111     }
00112     return rms;
00113   }
00114 
00115   bool convert(unsigned int aBins,double aLowerEdge,double aUpperEdge){
00116     if(m_histo) return true;
00117     m_histo = new histo::h1d(this->title(),aBins,aLowerEdge,aUpperEdge);
00118     if(!m_histo) return false;
00119     bool status = fill_histogram(*m_histo);
00120     clear();
00121     return status;
00122   }
00123 
00124   bool convert(const std::vector<double>& aEdges) {
00125     if(m_histo) return true;
00126     m_histo = new histo::h1d(this->title(),aEdges);
00127     if(!m_histo) return false;
00128     bool status = fill_histogram(*m_histo);
00129     clear();
00130     return status;
00131   }
00132 
00133   const histo::h1d& histogram() const {
00134     if(!m_histo) const_cast<c1d&>(*this).convert_to_histogram();
00135     return *m_histo;
00136   }
00137   bool fill_histogram(histo::h1d& a_histo) const {
00138     unsigned int number = m_xs.size();
00139     for(unsigned int index=0;index<number;index++) {
00140       if(!a_histo.fill(m_xs[index],m_ws[index])) return false;
00141     }
00142     return true;
00143   }
00144   bool set_conversion_parameters(unsigned int aCnvXnumber,
00145                                  double aCnvXmin,double aCnvXmax){
00146     m_cnv_x_num = aCnvXnumber;
00147     m_cnv_x_min = aCnvXmin;
00148     m_cnv_x_max = aCnvXmax;
00149     return true;
00150   }
00151 
00152 public:
00153   c1d()
00154   :base_cloud(UNLIMITED())
00155   ,m_lower_x(0),m_upper_x(0)
00156   ,m_Sxw(0),m_Sx2w(0)
00157   ,m_cnv_x_num(0),m_cnv_x_min(0),m_cnv_x_max(0),m_histo(0)
00158   {}
00159   
00160   c1d(const std::string& a_title,int aLimit = -1)
00161   :base_cloud(aLimit)
00162   ,m_lower_x(0),m_upper_x(0)
00163   ,m_Sxw(0),m_Sx2w(0)
00164   ,m_cnv_x_num(0),m_cnv_x_min(0),m_cnv_x_max(0),m_histo(0)
00165   {
00166     set_title(a_title);
00167   }
00168 
00169   virtual ~c1d(){delete m_histo;}
00170 public:
00171   c1d(const c1d& a_from)
00172   :base_cloud(a_from)
00173   ,m_xs(a_from.m_xs)
00174   ,m_lower_x(a_from.m_lower_x)
00175   ,m_upper_x(a_from.m_upper_x)
00176   ,m_Sxw(a_from.m_Sxw)
00177   ,m_Sx2w(a_from.m_Sx2w)
00178   ,m_cnv_x_num(a_from.m_cnv_x_num)
00179   ,m_cnv_x_min(a_from.m_cnv_x_min)
00180   ,m_cnv_x_max(a_from.m_cnv_x_max)
00181   ,m_histo(0)
00182   {
00183     if(a_from.m_histo) {
00184       m_histo = new histo::h1d(*a_from.m_histo);
00185     }
00186   }
00187 
00188   c1d& operator=(const c1d& a_from){
00189     base_cloud::operator=(a_from);
00190     m_xs = a_from.m_xs;
00191     m_lower_x = a_from.m_lower_x;
00192     m_upper_x = a_from.m_upper_x;
00193     m_Sxw = a_from.m_Sxw;
00194     m_Sx2w = a_from.m_Sx2w;
00195     m_cnv_x_num = a_from.m_cnv_x_num;
00196     m_cnv_x_min = a_from.m_cnv_x_min;
00197     m_cnv_x_max = a_from.m_cnv_x_max;
00198     delete m_histo;
00199     m_histo = 0;
00200     if(a_from.m_histo) {
00201       m_histo = new histo::h1d(*a_from.m_histo);
00202     }
00203     return *this;
00204   }
00205 
00206 protected:
00207   void clear(){
00208     m_lower_x = 0;
00209     m_upper_x = 0;
00210     m_Sw = 0;
00211     m_Sxw = 0;
00212     m_Sx2w = 0;
00213     m_xs.clear();
00214     m_ws.clear();
00215   }
00216 
00217 protected:
00218   std::vector<double> m_xs;
00219   double m_lower_x;
00220   double m_upper_x;
00221   double m_Sxw;
00222   double m_Sx2w;
00223   //
00224   unsigned int m_cnv_x_num;
00225   double m_cnv_x_min;
00226   double m_cnv_x_max;
00227   histo::h1d* m_histo;
00228 };
00229 
00230 }}
00231 
00232 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines