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

#ifndef tools_histo_h1
#define tools_histo_h1

#include "b1"

namespace tools {
namespace histo { //have that for h1 ?

//TC is for a coordinate.
//TO is for an offset used to identify a bin.
//TN is for a number of entries.
//TW is for a weight.
//TH is for a height. Should be the same as TW.

template <class TC,class TO,class TN,class TW,class TH>
class h1 : public b1<TC,TO,TN,TW,TH> {
  typedef b1<TC,TO,TN,TW,TH> parent;
public:
  typedef histo_data<TC,TO,TN,TW> hd_t;
  typedef typename parent::bn_t bn_t;
  typedef typename parent::axis_t axis_t;
protected:
  virtual TH get_bin_height(TO a_offset) const { //TH should be the same as TW
    return parent::m_bin_Sw[a_offset];
  }
public:
  virtual TH bin_error(int aI) const { //TH should be the same as TW
    TO offset;
    if(!parent::_find_offset(aI,offset)) return 0;
    return ::sqrt(parent::m_bin_Sw2[offset]);
  }

public:
  bool multiply(TW a_factor){return parent::base_multiply(a_factor);}
  bool scale(TW a_factor) {return multiply(a_factor);}

  void copy_from_data(const hd_t& a_from) {parent::base_from_data(a_from);}
  hd_t get_histo_data() const {return *this;} //deprecated. Keep it for g4tools.

  bool reset() {
    parent::base_reset();
    return true;
  }

  bool fill(TC aX,TW aWeight = 1) {
    if(parent::m_dimension!=1) return false;

    bn_t ibin;
    if(!parent::m_axes[0].coord_to_absolute_index(aX,ibin)) return false;

    TO offset = ibin;

    parent::m_bin_entries[offset]++;
    parent::m_bin_Sw[offset] += aWeight;
    parent::m_bin_Sw2[offset] += aWeight * aWeight;
  
    TC xw = aX * aWeight;
    TC x2w = aX * xw;
    parent::m_bin_Sxw[offset][0] += xw;
    parent::m_bin_Sx2w[offset][0] += x2w;

    bool inRange = true;
    if(ibin==0) inRange = false;
    else if(ibin==(parent::m_axes[0].m_number_of_bins+1)) inRange = false;

    parent::m_all_entries++;
    if(inRange) {
      // fast getters :
      parent::m_in_range_entries++;
      parent::m_in_range_Sw += aWeight;
      parent::m_in_range_Sw2 += aWeight*aWeight;

      parent::m_in_range_Sxw[0] += xw;
      parent::m_in_range_Sx2w[0] += x2w;
    }

    return true;
  }

  bool set_bin_content(bn_t a_ibin,TN a_entries,TW a_Sw,TW a_Sw2,TC a_Sxw,TC a_Sx2w) {
    if(parent::m_dimension!=1) return false;
    if(a_ibin>(parent::m_axes[0].m_number_of_bins+1)) return false;

    bool inRange = true;
    if(a_ibin==0) inRange = false;
    else if(a_ibin==(parent::m_axes[0].m_number_of_bins+1)) inRange = false;

    TO offset = a_ibin;

    parent::m_all_entries -= parent::m_bin_entries[offset];
    if(inRange) {
      parent::m_in_range_entries -= parent::m_bin_entries[offset];
      parent::m_in_range_Sw -= parent::m_bin_Sw[offset];
      parent::m_in_range_Sw2 -= parent::m_bin_Sw2[offset];
      parent::m_in_range_Sxw[0] -= parent::m_bin_Sxw[offset][0];
      parent::m_in_range_Sx2w[0] -= parent::m_bin_Sx2w[offset][0];
    }

    parent::m_bin_entries[offset] = a_entries;
    parent::m_bin_Sw[offset] = a_Sw;
    parent::m_bin_Sw2[offset] = a_Sw2;

    parent::m_bin_Sxw[offset][0] = a_Sxw;
    parent::m_bin_Sx2w[offset][0] = a_Sx2w;

    parent::m_all_entries += a_entries;
    if(inRange) {
      parent::m_in_range_entries += a_entries;
      parent::m_in_range_Sw += a_Sw;
      parent::m_in_range_Sw2 += a_Sw2;

      parent::m_in_range_Sxw[0] += a_Sxw;
      parent::m_in_range_Sx2w[0] += a_Sx2w;
    }

    return true;
  }

  bool get_bin_content(bn_t a_ibin,TN& a_entries,TW& a_Sw,TW& a_Sw2,TC& a_Sxw,TC& a_Sx2w) {
    if(parent::m_dimension!=1) {
      a_entries = 0;a_Sw = 0;a_Sw2 = 0;a_Sxw = 0;a_Sx2w = 0;
      return false;
    }
    if(a_ibin>(parent::m_axes[0].m_number_of_bins+1)) {
      a_entries = 0;a_Sw = 0;a_Sw2 = 0;a_Sxw = 0;a_Sx2w = 0;
      return false;
    }

    TO offset = a_ibin;

    a_entries = parent::m_bin_entries[offset];
    a_Sw = parent::m_bin_Sw[offset];
    a_Sw2 = parent::m_bin_Sw2[offset];

    a_Sxw = parent::m_bin_Sxw[offset][0];
    a_Sx2w = parent::m_bin_Sx2w[offset][0];

    return true;
  }

  bool add(const h1& a_histo){
    parent::base_add(a_histo);
    return true;
  }
  bool subtract(const h1& a_histo){
    parent::base_subtract(a_histo);
    return true;
  }

  bool multiply(const h1& a_histo) {  
    return parent::base_multiply(a_histo);
  }
  
  bool divide(const h1& a_histo) {
    return parent::base_divide(a_histo);
  }

  bool gather_bins(unsigned int a_factor) { //for exa 2,3.
    if(!a_factor) return false;

    // actual bin number must be a multiple of a_factor.
    
    const axis_t& _axis = parent::axis();

    bn_t n = _axis.bins();
    if(!n) return false;

    bn_t new_n = n/a_factor;
    if(a_factor*new_n!=n) return false;

    h1* new_h = 0;
    if(_axis.is_fixed_binning()) {
      new_h = new h1(parent::m_title,new_n,_axis.lower_edge(),_axis.upper_edge());
    } else {
      const std::vector<TC>& _edges = _axis.edges();
      std::vector<TC> new_edges(new_n+1);
      for(bn_t ibin=0;ibin<new_n;ibin++) {
        new_edges[ibin] = _edges[ibin*a_factor];
      }
      new_edges[new_n] = _edges[n]; //upper edge.
      new_h = new h1(parent::m_title,new_edges);
    }
    if(!new_h) return false;

    TO offset,new_offset,offac;
    for(bn_t ibin=0;ibin<new_n;ibin++) {
      new_offset = ibin+1;
      offset = a_factor*ibin+1;
      for(unsigned int ifac=0;ifac<a_factor;ifac++) {
        offac = offset+ifac;
        new_h->m_bin_entries[new_offset] += parent::m_bin_entries[offac];
        new_h->m_bin_Sw[new_offset] += parent::m_bin_Sw[offac];
        new_h->m_bin_Sw2[new_offset] += parent::m_bin_Sw2[offac];
        new_h->m_bin_Sxw[new_offset][0] += parent::m_bin_Sxw[offac][0];
        new_h->m_bin_Sx2w[new_offset][0] += parent::m_bin_Sx2w[offac][0];
      }
    }

    //underflow :
    new_offset = 0;
    offac = 0;
    new_h->m_bin_entries[new_offset] = parent::m_bin_entries[offac];
    new_h->m_bin_Sw[new_offset] = parent::m_bin_Sw[offac];
    new_h->m_bin_Sw2[new_offset] = parent::m_bin_Sw2[offac];
    new_h->m_bin_Sxw[new_offset][0] = parent::m_bin_Sxw[offac][0];
    new_h->m_bin_Sx2w[new_offset][0] = parent::m_bin_Sx2w[offac][0];

    //overflow :
    new_offset = new_n+1;
    offac = n+1;
    new_h->m_bin_entries[new_offset] = parent::m_bin_entries[offac];
    new_h->m_bin_Sw[new_offset] = parent::m_bin_Sw[offac];
    new_h->m_bin_Sw2[new_offset] = parent::m_bin_Sw2[offac];
    new_h->m_bin_Sxw[new_offset][0] = parent::m_bin_Sxw[offac][0];
    new_h->m_bin_Sx2w[new_offset][0] = parent::m_bin_Sx2w[offac][0];

    *this = *new_h;
    return true;
  }
  
  bool equals_TH(const h1& a_from,const TW& a_prec,TW(*a_fabs)(TW)) const {
    if(!parent::equals_TH(a_from,a_prec,a_fabs,true)) return false;    
    return true;
  }
  
  void not_a_profile() const {}
  
public: //CERN-ROOT API (for MEMPHYS sim).
  bool Fill(TC aX,TW aWeight = 1) {return fill(aX,aWeight);}

public:
  h1(const std::string& a_title,bn_t aXnumber,TC aXmin,TC aXmax)
  :parent(a_title,aXnumber,aXmin,aXmax){}

  h1(const std::string& a_title,const std::vector<TC>& a_edges)
  :parent(a_title,a_edges){}

  virtual ~h1(){}
public:
  h1(const h1& a_from):parent(a_from){}
  h1& operator=(const h1& a_from){
    if(&a_from==this) return *this;
    parent::operator=(a_from);
    return *this;
  }
};

}}

#endif




