// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.

#ifndef tools_histo_b1
#define tools_histo_b1

#include "base_histo"

#include <ostream>

namespace tools {
namespace histo {

template <class TC,class TO,class TN,class TW,class TH>
class b1 : public base_histo<TC,TO,TN,TW,TH> {
  typedef base_histo<TC,TO,TN,TW,TH> parent;
protected:
  enum {AxisX=0};
public:
  typedef base_histo<TC,TO,TN,TW,TH> base_histo_t;
  typedef typename parent::axis_t axis_t;
  typedef typename parent::bn_t bn_t;
public:
  virtual TH bin_error(int) const = 0; //for print
public:
  // Partition :
  int coord_to_index(TC aCoord) const {
    return axis().coord_to_index(aCoord);
  }
  TC mean() const {
    //TC value;
    //parent::get_ith_axis_mean(AxisX,value); //can return false.
    //return value;
    if(parent::m_in_range_Sw==0) return 0;
    return parent::m_in_range_Sxw[0]/parent::m_in_range_Sw;
  }
  TC rms() const {
    //TC value;
    //parent::get_ith_axis_rms(AxisX,value); //can return false.
    //return value;
    if(parent::m_in_range_Sw==0) return 0;
    TC _mean = parent::m_in_range_Sxw[0]/parent::m_in_range_Sw;
    return ::sqrt(::fabs((parent::m_in_range_Sx2w[0] / parent::m_in_range_Sw) - _mean * _mean));
  }

  // bins :
  TN bin_entries(int aI) const {
    TO offset;
    if(!_find_offset(aI,offset)) return 0;
    return parent::m_bin_entries[offset];
  }

  TW bin_Sw(int aI) const {
    TO offset;
    if(!_find_offset(aI,offset)) return 0;
    return parent::m_bin_Sw[offset];
  }

  TW bin_Sw2(int aI) const {
    TO offset;
    if(!_find_offset(aI,offset)) return 0;
    return parent::m_bin_Sw2[offset];
  }
  TC bin_Sxw(int aI) const {
    TO offset;
    if(!_find_offset(aI,offset)) return 0;
    return parent::m_bin_Sxw[offset][AxisX];
  }
  TC bin_Sx2w(int aI) const {
    TO offset;
    if(!_find_offset(aI,offset)) return 0;
    return parent::m_bin_Sx2w[offset][AxisX];
  }

  TH bin_height(int aI) const {
    TO offset;
    if(!_find_offset(aI,offset)) return 0;
    return this->get_bin_height(offset);
  }

  TC bin_center(int aI) const {return parent::m_axes[0].bin_center(aI);}

  TC bin_mean(int aI) const {
    TO offset;
    if(!_find_offset(aI,offset)) return 0;
    TW sw = parent::m_bin_Sw[offset];
    if(sw==0) return 0;
    return parent::m_bin_Sxw[offset][AxisX]/sw;
  }

  TC bin_rms(int aI) const {
    TO offset;
    if(!_find_offset(aI,offset)) return 0;
    TW sw = parent::m_bin_Sw[offset];
    if(sw==0) return 0;
    TC sxw = parent::m_bin_Sxw[offset][AxisX];
    TC sx2w = parent::m_bin_Sx2w[offset][AxisX];
    TC _mean = sxw/sw;
    return ::sqrt(::fabs((sx2w / sw) - _mean * _mean));
  }

  // Axis :
  const axis_t& axis() const {return parent::m_axes[0];}
  axis_t& axis() {return parent::m_axes[0];} //touchy
public:
  //NOTE : print is a Python keyword.
  void hprint(std::ostream& a_out) {
    // A la HPRINT.
    a_out << parent::dimension() << parent::title() << std::endl;
    a_out
      << " * ENTRIES = " << parent::all_entries()
      << " * ALL CHANNELS = " << parent::sum_bin_heights()
      << " * UNDERFLOW = " << bin_height(axis_t::UNDERFLOW_BIN)
      << " * OVERFLOW = " << bin_height(axis_t::OVERFLOW_BIN)
      << std::endl;
    a_out
      << " * BIN WID = " << axis().bin_width(0)
      << " * MEAN VALUE = " << mean()
      << " * R . M . S = " << rms()
      << std::endl;

    // Some bins :
    bn_t bins = axis().bins();
    a_out
      << " * ENTRIES[0]   = "
      << bin_entries(0)
      << " * HEIGHT[0] = "
      << bin_height(0)
      << " * ERROR[0] = "
      << bin_error(0)
      << std::endl;
    a_out
      << " * ENTRIES[N/2] = "
      << bin_entries(bins/2)
      << " * HEIGHT[N/2] = "
      << bin_height(bins/2)
      << " * ERROR[N/2] = "
      << bin_error(bins/2)
      << std::endl;
    a_out
      << " * ENTRIES[N-1] = "
      << bin_entries(bins-1)
      << " * HEIGHT[N-1] = "
      << bin_height(bins-1)
      << " * ERROR[N-1] = "
      << bin_error(bins-1)
      << std::endl;
  }
protected:
  b1(const std::string& a_title,bn_t aXnumber,TC aXmin,TC aXmax) {
    parent::m_title = a_title;
    std::vector<bn_t> nbins;
    nbins.push_back(aXnumber);
    std::vector<TC> mins;
    mins.push_back(aXmin);
    std::vector<TC> maxs;
    maxs.push_back(aXmax);
    parent::configure(1,nbins,mins,maxs);
  }
  b1(const std::string& a_title,const std::vector<TC>& a_edges) {
    parent::m_title = a_title;
    std::vector< std::vector<TC> > edges(1);
    edges[0] = a_edges;
    parent::configure(1,edges);
  }

  virtual ~b1(){}
protected:
  b1(const b1& a_from):parent(a_from){}
  b1& operator=(const b1& a_from) {
    if(&a_from==this) return *this;
    parent::operator=(a_from);
    return *this;
  }
public:
  bool configure(bn_t aXnumber,TC aXmin,TC aXmax){
    std::vector<bn_t> nbins;
    nbins.push_back(aXnumber);
    std::vector<TC> mins;
    mins.push_back(aXmin);
    std::vector<TC> maxs;
    maxs.push_back(aXmax);
    return parent::configure(1,nbins,mins,maxs);
  }
  bool configure(const std::vector<TC>& a_edges) {
    std::vector< std::vector<TC> > edges(1);
    edges[0] = a_edges;
    return parent::configure(1,edges);
  }
protected:
  bool _find_offset(int aI,TO& a_offset) const {
    if(parent::m_dimension!=1) {a_offset=0;return false;}
    bn_t ibin;
    if(!parent::m_axes[0].in_range_to_absolute_index(aI,ibin)) {a_offset=0;return false;}
    a_offset = ibin;
    return true;
  }
};

}}

#endif




