Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/wcsv_histo is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_wcsv_histo
0005 #define tools_wcsv_histo
0006 
0007 #include <ostream>
0008 #include <vector>
0009 #include <string>
0010 
0011 namespace tools {
0012 namespace wcsv {
0013 
0014 template <class AXIS>
0015 inline void axis_to(std::ostream& a_writer,const AXIS& a_axis,char a_hc) {
0016   if(a_axis.m_fixed) {
0017     a_writer << a_hc
0018              << "axis fixed " << a_axis.m_number_of_bins
0019              << " " << a_axis.m_minimum_value
0020              << " " << a_axis.m_maximum_value
0021 //           << " " << a_axis.m_bin_width
0022              << std::endl;
0023   } else {
0024     a_writer << a_hc << "axis edges";
0025     for(unsigned int iedge=0;iedge<a_axis.m_edges.size();iedge++) {
0026       a_writer << " " << a_axis.m_edges[iedge];
0027     }
0028     a_writer << std::endl;
0029   }
0030 }
0031 
0032 template <class ANNOTATION>
0033 inline void annotations_to(std::ostream& a_writer,const ANNOTATION& a_ans,char a_hc) {
0034   typename ANNOTATION::const_iterator it;
0035   for(it=a_ans.begin();it!=a_ans.end();++it) {
0036     a_writer << a_hc << "annotation " << (*it).first << " " << (*it).second << std::endl;
0037   }
0038 }
0039 
0040 template <class HIST>
0041 inline void h_header(std::ostream& a_writer,const std::string& a_class,const HIST& a_h,char a_hc = '#') {
0042   a_writer << a_hc << "class " << a_class << std::endl;
0043   a_writer << a_hc << "title " << a_h.title() << std::endl;
0044   a_writer << a_hc << "dimension " << a_h.dimension() << std::endl;
0045   for(unsigned int iaxis=0;iaxis<a_h.dimension();iaxis++) axis_to(a_writer,a_h.get_axis(iaxis),a_hc);
0046  {const std::vector<typename HIST::coordinate_t>& planes = a_h.in_range_planes_xyw();
0047   if(planes.size()) {
0048     a_writer << a_hc << "planes_Sxyw";
0049     for(unsigned int iplane=0;iplane<planes.size();iplane++) a_writer << " " << planes[iplane];
0050     a_writer << std::endl;
0051   }}
0052   annotations_to(a_writer,a_h.annotations(),a_hc);
0053   a_writer << a_hc << "bin_number " << a_h.get_bins() << std::endl;
0054 }
0055 
0056 template <class HIST>
0057 inline bool hto(std::ostream& a_writer,const std::string& a_class,const HIST& a_h,
0058                 char a_sep = ',',char a_hc = '#',bool a_header = true) {
0059   if(a_header) h_header(a_writer,a_class,a_h,a_hc);
0060 
0061  {a_writer << "entries" << a_sep << "Sw" << a_sep << "Sw2";
0062   for(unsigned int iaxis=0;iaxis<a_h.dimension();iaxis++) {
0063     a_writer << a_sep << "Sxw" << iaxis << a_sep << "Sx2w" << iaxis;
0064   }
0065   a_writer << std::endl;}
0066 
0067   typedef typename HIST::coordinate_t coordinate_t;
0068   typedef typename HIST::dim_t dim_t;
0069   typedef typename HIST::offset_t offset_t;
0070   typedef typename HIST::num_entries_t num_entries_t;
0071   typedef typename HIST::weight_t weight_t;
0072 
0073   dim_t _dim = a_h.dimension();
0074   offset_t _bins = a_h.get_bins();
0075 
0076   const std::vector<num_entries_t>& _bin_entries = a_h.bins_entries();
0077   const std::vector<weight_t>& _bin_Sw = a_h.bins_sum_w();
0078   const std::vector<weight_t>& _bin_Sw2 = a_h.bins_sum_w2();
0079   const std::vector< std::vector<coordinate_t> >& _bin_Sxw = a_h.bins_sum_xw();
0080   const std::vector< std::vector<coordinate_t> >& _bin_Sx2w = a_h.bins_sum_x2w();
0081 
0082   for(unsigned int i=0;i<_bins;i++) {
0083     a_writer << _bin_entries[i] << a_sep << _bin_Sw[i] << a_sep << _bin_Sw2[i];
0084     for(unsigned int iaxis=0;iaxis<_dim;iaxis++) {
0085       a_writer << a_sep << _bin_Sxw[i][iaxis] << a_sep << _bin_Sx2w[i][iaxis];
0086     }
0087     a_writer << std::endl;
0088   }
0089 
0090   a_h.not_a_profile(); //trick to be sure to use this function on an histo and not a profile.
0091 
0092   return true;
0093 }
0094 
0095 template <class PROF>
0096 inline void p_header(std::ostream& a_writer,const std::string& a_class,const PROF& a_prof,char a_hc = '#') {
0097   a_writer << a_hc << "class " << a_class << std::endl;
0098   a_writer << a_hc << "title " << a_prof.title() << std::endl;
0099   a_writer << a_hc << "dimension " << a_prof.dimension() << std::endl;
0100   for(unsigned int iaxis=0;iaxis<a_prof.dimension();iaxis++) axis_to(a_writer,a_prof.get_axis(iaxis),a_hc);
0101  {const std::vector<typename PROF::coordinate_t>& planes = a_prof.in_range_planes_xyw();
0102   if(planes.size()) {
0103     a_writer << a_hc << "planes_Sxyw";
0104     for(unsigned int iplane=0;iplane<planes.size();iplane++) a_writer << " " << planes[iplane];
0105     a_writer << std::endl;
0106   }}
0107   annotations_to(a_writer,a_prof.annotations(),a_hc);
0108   a_writer << a_hc << "cut_v " << (a_prof.cut_v()?"true":"false") << std::endl;
0109   a_writer << a_hc << "min_v " << a_prof.min_v() << std::endl;
0110   a_writer << a_hc << "max_v " << a_prof.max_v() << std::endl;
0111   a_writer << a_hc << "bin_number " << a_prof.get_bins() << std::endl;
0112 }
0113 
0114 template <class PROF>
0115 inline bool pto(std::ostream& a_writer,const std::string& a_class,const PROF& a_prof,
0116                 char a_sep = ',',char a_hc = '#',bool a_header = true) {
0117   if(a_header) p_header(a_writer,a_class,a_prof,a_hc);
0118 
0119  {a_writer << "entries" << a_sep << "Sw" << a_sep << "Sw2" << a_sep << "Svw" << a_sep << "Sv2w";
0120   for(unsigned int iaxis=0;iaxis<a_prof.dimension();iaxis++) {
0121     a_writer << a_sep << "Sxw" << iaxis << a_sep << "Sx2w" << iaxis;
0122   }
0123   a_writer << std::endl;}
0124 
0125   typedef typename PROF::coordinate_t coordinate_t;
0126   typedef typename PROF::dim_t dim_t;
0127   typedef typename PROF::offset_t offset_t;
0128   typedef typename PROF::num_entries_t num_entries_t;
0129   typedef typename PROF::weight_t weight_t;
0130 
0131   dim_t _dim = a_prof.dimension();
0132   offset_t _bins = a_prof.get_bins();
0133 
0134   const std::vector<num_entries_t>& _bin_entries = a_prof.bins_entries();
0135   const std::vector<weight_t>& _bin_Sw = a_prof.bins_sum_w();
0136   const std::vector<weight_t>& _bin_Sw2 = a_prof.bins_sum_w2();
0137   const std::vector< std::vector<coordinate_t> >& _bin_Sxw = a_prof.bins_sum_xw();
0138   const std::vector< std::vector<coordinate_t> >& _bin_Sx2w = a_prof.bins_sum_x2w();
0139 
0140   typedef typename PROF::vs_t vs_t;
0141   const vs_t& _bin_Svw = a_prof.bins_sum_vw();
0142   const vs_t& _bin_Sv2w = a_prof.bins_sum_v2w();
0143 
0144   for(unsigned int i=0;i<_bins;i++) {
0145     a_writer << _bin_entries[i] << a_sep << _bin_Sw[i] << a_sep << _bin_Sw2[i]
0146              << a_sep << _bin_Svw[i] << a_sep << _bin_Sv2w[i];
0147     for(unsigned int iaxis=0;iaxis<_dim;iaxis++) {
0148       a_writer << a_sep << _bin_Sxw[i][iaxis] << a_sep << _bin_Sx2w[i][iaxis];
0149     }
0150     a_writer << std::endl;
0151   }
0152 
0153   return true;
0154 }
0155 
0156 }}
0157 
0158 #endif