inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/histo/h2
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_h2
00005 #define inlib_histo_h2
00006 
00007 #include "b2"
00008 
00009 namespace inlib {
00010 namespace histo {
00011 
00012 template <class TC,class TN,class TW,class TH>
00013 class h2 : public b2<TC,TN,TW,TH> {
00014   typedef b2<TC,TN,TW,TH> parent;
00015 public:
00016   typedef typename b2<TC,TN,TW,TH>::bn_t bn_t;
00017 protected:
00018   virtual TH get_bin_height(int a_offset) const { //TH should be the same as TW
00019     return parent::m_bin_Sw[a_offset];
00020   }
00021 public:
00022 
00023   virtual TH bin_error(int aI,int aJ) const {
00024     if(parent::m_bin_number==0) return 0;
00025     bn_t ibin;
00026     if(!parent::m_axes[0].in_range_to_absolute_index(aI,ibin)) return 0;
00027     bn_t jbin;
00028     if(!parent::m_axes[1].in_range_to_absolute_index(aJ,jbin)) return 0;
00029     bn_t offset = ibin + jbin * parent::m_axes[1].m_offset;
00030     return ::sqrt(parent::m_bin_Sw2[offset]);
00031   }
00032 
00033 public:
00034   bool multiply(TW aFactor){
00035     if(!parent::base_multiply(aFactor)) return false;
00036     this->update_fast_getters();
00037     return true;
00038   }
00039   bool scale(TW aFactor) {return multiply(aFactor);}
00040 
00041   void copy_from_data(const histo_data<TC,TN,TW>& a_from) {
00042     parent::base_from_data(a_from);
00043   }
00044   histo_data<TC,TN,TW> get_histo_data() const {
00045     return parent::base_get_data();
00046   }
00047 
00048   bool reset() {
00049     parent::base_reset();
00050     this->update_fast_getters();
00051     return true;
00052   }
00053 
00054   bool fill(TC aX,TC aY,TW aWeight = 1) {
00055     //m_coords[0] = aX;
00056     //m_coords[1] = aY;
00057     //return fill_bin(m_coords,aWeight);
00058   
00059     if(parent::m_dimension<=0) return false;
00060   
00061     bn_t ibin,jbin;
00062     if(!parent::m_axes[0].coord_to_absolute_index(aX,ibin)) return false;
00063     if(!parent::m_axes[1].coord_to_absolute_index(aY,jbin)) return false;
00064   
00065     bn_t offset = ibin + jbin * parent::m_axes[1].m_offset;
00066   
00067     parent::m_bin_entries[offset]++;
00068     parent::m_bin_Sw[offset] += aWeight;
00069     parent::m_bin_Sw2[offset] += aWeight * aWeight;
00070   
00071     TC xw = aX * aWeight;
00072     TC x2w = aX * xw;
00073     parent::m_bin_Sxw[offset][0] += xw;
00074     parent::m_bin_Sx2w[offset][0] += x2w;
00075   
00076     TC yw = aY * aWeight;
00077     TC y2w = aY * yw;
00078     parent::m_bin_Sxw[offset][1] += yw;
00079     parent::m_bin_Sx2w[offset][1] += y2w;
00080   
00081     bool inRange = true;
00082     if(ibin==0) inRange = false;
00083     else if(ibin==(parent::m_axes[0].m_number_of_bins+1)) inRange = false;
00084   
00085     if(jbin==0) inRange = false;
00086     else if(jbin==(parent::m_axes[1].m_number_of_bins+1)) inRange = false;
00087   
00088     if(inRange) {
00089       parent::m_in_range_entries++;
00090       parent::m_in_range_Sw += aWeight;
00091   
00092       parent::m_in_range_Sxw += xw;
00093       parent::m_in_range_Sx2w += x2w;
00094   
00095       parent::m_in_range_Syw += yw;
00096       parent::m_in_range_Sy2w += y2w;
00097     }
00098    
00099     return true;
00100   }
00101   
00102   bool add(const h2& a_histo){
00103     parent::base_add(a_histo);
00104     this->update_fast_getters();
00105     return true;
00106   }
00107   bool subtract(const h2& a_histo){
00108     parent::base_subtract(a_histo);
00109     this->update_fast_getters();
00110     return true;
00111   }
00112 
00113   bool multiply(const h2& a_histo) {  
00114     if(!parent::base_multiply(a_histo)) return false;
00115     this->update_fast_getters();
00116     return true;
00117   }
00118   
00119   bool divide(const h2& a_histo) {
00120     if(!parent::base_divide(a_histo)) return false;
00121     this->update_fast_getters();
00122     return true;
00123   }
00124 
00125 public:
00126   h2(const std::string& a_title,
00127                      bn_t aXnumber,TC aXmin,TC aXmax,
00128                      bn_t aYnumber,TC aYmin,TC aYmax)
00129   : parent(a_title,aXnumber,aXmin,aXmax,aYnumber,aYmin,aYmax)
00130   {}
00131   h2(const std::string& a_title,
00132                      const std::vector<TC>& aEdgesX,
00133                      const std::vector<TC>& aEdgesY)
00134   : parent(a_title,aEdgesX,aEdgesY)
00135   {}
00136 
00137   virtual ~h2(){}
00138 public:
00139   h2(const h2& a_from): parent(a_from){}
00140   h2& operator=(const h2& a_from){
00141     parent::operator=(a_from);
00142     return *this;
00143   }
00144 };
00145 
00146 }}
00147 
00148 #endif
00149 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines