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

#ifndef tools_wcsv_histo
#define tools_wcsv_histo

#include <ostream>
#include <vector>
#include <string>

namespace tools {
namespace wcsv {

template <class AXIS>
inline void axis_to(std::ostream& a_writer,const AXIS& a_axis,char a_hc) {
  if(a_axis.m_fixed) {
    a_writer << a_hc
             << "axis fixed " << a_axis.m_number_of_bins
             << " " << a_axis.m_minimum_value
             << " " << a_axis.m_maximum_value
             << std::endl;
  } else {
    a_writer << a_hc << "axis edges";
    for(unsigned int iedge=0;iedge<a_axis.m_edges.size();iedge++) {
      a_writer << " " << a_axis.m_edges[iedge];
    }
    a_writer << std::endl;
  }
}

template <class ANNOTATION>
inline void annotations_to(std::ostream& a_writer,const ANNOTATION& a_ans,char a_hc) {
  typename ANNOTATION::const_iterator it;
  for(it=a_ans.begin();it!=a_ans.end();++it) {
    a_writer << a_hc << "annotation " << (*it).first << " " << (*it).second << std::endl;
  }
}

template <class HIST>
inline void h_header(std::ostream& a_writer,const std::string& a_class,const HIST& a_h,char a_hc = '#') {
  a_writer << a_hc << "class " << a_class << std::endl;
  a_writer << a_hc << "title " << a_h.title() << std::endl;
  a_writer << a_hc << "dimension " << a_h.dimension() << std::endl;
  for(unsigned int iaxis=0;iaxis<a_h.dimension();iaxis++) axis_to(a_writer,a_h.get_axis(iaxis),a_hc);
 {const std::vector<typename HIST::coordinate_t>& planes = a_h.in_range_planes_xyw();
  if(planes.size()) {
    a_writer << a_hc << "planes_Sxyw";
    for(unsigned int iplane=0;iplane<planes.size();iplane++) a_writer << " " << planes[iplane];
    a_writer << std::endl;
  }}
  annotations_to(a_writer,a_h.annotations(),a_hc);
  a_writer << a_hc << "bin_number " << a_h.get_bins() << std::endl;
}

template <class HIST>
inline bool hto(std::ostream& a_writer,const std::string& a_class,const HIST& a_h,
                char a_sep = ',',char a_hc = '#',bool a_header = true) {
  if(a_header) h_header(a_writer,a_class,a_h,a_hc);

 {a_writer << "entries" << a_sep << "Sw" << a_sep << "Sw2";
  for(unsigned int iaxis=0;iaxis<a_h.dimension();iaxis++) {
    a_writer << a_sep << "Sxw" << iaxis << a_sep << "Sx2w" << iaxis;
  }
  a_writer << std::endl;}

  typedef typename HIST::coordinate_t coordinate_t;
  typedef typename HIST::dim_t dim_t;
  typedef typename HIST::offset_t offset_t;
  typedef typename HIST::num_entries_t num_entries_t;
  typedef typename HIST::weight_t weight_t;

  dim_t _dim = a_h.dimension();
  offset_t _bins = a_h.get_bins();

  const std::vector<num_entries_t>& _bin_entries = a_h.bins_entries();
  const std::vector<weight_t>& _bin_Sw = a_h.bins_sum_w();
  const std::vector<weight_t>& _bin_Sw2 = a_h.bins_sum_w2();
  const std::vector< std::vector<coordinate_t> >& _bin_Sxw = a_h.bins_sum_xw();
  const std::vector< std::vector<coordinate_t> >& _bin_Sx2w = a_h.bins_sum_x2w();

  for(unsigned int i=0;i<_bins;i++) {
    a_writer << _bin_entries[i] << a_sep << _bin_Sw[i] << a_sep << _bin_Sw2[i];
    for(unsigned int iaxis=0;iaxis<_dim;iaxis++) {
      a_writer << a_sep << _bin_Sxw[i][iaxis] << a_sep << _bin_Sx2w[i][iaxis];
    }
    a_writer << std::endl;
  }

  a_h.not_a_profile(); //trick to be sure to use this function on an histo and not a profile.

  return true;
}

template <class PROF>
inline void p_header(std::ostream& a_writer,const std::string& a_class,const PROF& a_prof,char a_hc = '#') {
  a_writer << a_hc << "class " << a_class << std::endl;
  a_writer << a_hc << "title " << a_prof.title() << std::endl;
  a_writer << a_hc << "dimension " << a_prof.dimension() << std::endl;
  for(unsigned int iaxis=0;iaxis<a_prof.dimension();iaxis++) axis_to(a_writer,a_prof.get_axis(iaxis),a_hc);
 {const std::vector<typename PROF::coordinate_t>& planes = a_prof.in_range_planes_xyw();
  if(planes.size()) {
    a_writer << a_hc << "planes_Sxyw";
    for(unsigned int iplane=0;iplane<planes.size();iplane++) a_writer << " " << planes[iplane];
    a_writer << std::endl;
  }}
  annotations_to(a_writer,a_prof.annotations(),a_hc);
  a_writer << a_hc << "cut_v " << (a_prof.cut_v()?"true":"false") << std::endl;
  a_writer << a_hc << "min_v " << a_prof.min_v() << std::endl;
  a_writer << a_hc << "max_v " << a_prof.max_v() << std::endl;
  a_writer << a_hc << "bin_number " << a_prof.get_bins() << std::endl;
}

template <class PROF>
inline bool pto(std::ostream& a_writer,const std::string& a_class,const PROF& a_prof,
                char a_sep = ',',char a_hc = '#',bool a_header = true) {
  if(a_header) p_header(a_writer,a_class,a_prof,a_hc);

 {a_writer << "entries" << a_sep << "Sw" << a_sep << "Sw2" << a_sep << "Svw" << a_sep << "Sv2w";
  for(unsigned int iaxis=0;iaxis<a_prof.dimension();iaxis++) {
    a_writer << a_sep << "Sxw" << iaxis << a_sep << "Sx2w" << iaxis;
  }
  a_writer << std::endl;}

  typedef typename PROF::coordinate_t coordinate_t;
  typedef typename PROF::dim_t dim_t;
  typedef typename PROF::offset_t offset_t;
  typedef typename PROF::num_entries_t num_entries_t;
  typedef typename PROF::weight_t weight_t;

  dim_t _dim = a_prof.dimension();
  offset_t _bins = a_prof.get_bins();

  const std::vector<num_entries_t>& _bin_entries = a_prof.bins_entries();
  const std::vector<weight_t>& _bin_Sw = a_prof.bins_sum_w();
  const std::vector<weight_t>& _bin_Sw2 = a_prof.bins_sum_w2();
  const std::vector< std::vector<coordinate_t> >& _bin_Sxw = a_prof.bins_sum_xw();
  const std::vector< std::vector<coordinate_t> >& _bin_Sx2w = a_prof.bins_sum_x2w();

  typedef typename PROF::vs_t vs_t;
  const vs_t& _bin_Svw = a_prof.bins_sum_vw();
  const vs_t& _bin_Sv2w = a_prof.bins_sum_v2w();

  for(unsigned int i=0;i<_bins;i++) {
    a_writer << _bin_entries[i] << a_sep << _bin_Sw[i] << a_sep << _bin_Sw2[i]
             << a_sep << _bin_Svw[i] << a_sep << _bin_Sv2w[i];
    for(unsigned int iaxis=0;iaxis<_dim;iaxis++) {
      a_writer << a_sep << _bin_Sxw[i][iaxis] << a_sep << _bin_Sx2w[i][iaxis];
    }
    a_writer << std::endl;
  }

  return true;
}

}}

#endif
