Warning, /include/Geant4/tools/histo/histo_data 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_histo_histo_data
0005 #define tools_histo_histo_data
0006
0007 #include <vector>
0008 #include <map> //for annotations
0009
0010 #include "axes"
0011 #include "../eqT"
0012
0013 namespace tools {
0014 namespace histo {
0015
0016 inline unsigned int dim_planes(unsigned int a_dim) {
0017 // m_dim = 1 -> 0
0018 // m_dim = 2 -> 0+1=1
0019 // m_dim = 3 -> 0+1+2=3
0020 // m_dim = 4 -> 0+1+2+3=6
0021 typedef unsigned int dim_t;
0022 dim_t n = 0;
0023 for(dim_t i=0;i<a_dim;i++) n += i;
0024 return n;
0025 }
0026
0027 //TC is for a coordinate.
0028 //TO is for an offset used to identify a bin.
0029 //TN is for a number of entries.
0030 //TW is for a weight.
0031
0032 template <class TC,class TO,class TN,class TW>
0033 class histo_data {
0034 public:
0035 typedef axis<TC,TO> axis_t;
0036 typedef unsigned int dim_t;
0037 typedef std::map<std::string,std::string> annotations_t;
0038 public:
0039 histo_data()
0040 :m_dimension(0)
0041 ,m_bin_number(0)
0042 ,m_all_entries(0)
0043 ,m_in_range_entries(0)
0044 ,m_in_range_Sw(0)
0045 ,m_in_range_Sw2(0)
0046 {}
0047 public:
0048 histo_data(const histo_data& a_from)
0049 :m_title(a_from.m_title)
0050 ,m_dimension(a_from.m_dimension)
0051 ,m_bin_number(a_from.m_bin_number)
0052 ,m_bin_entries(a_from.m_bin_entries)
0053 ,m_bin_Sw(a_from.m_bin_Sw)
0054 ,m_bin_Sw2(a_from.m_bin_Sw2)
0055 ,m_bin_Sxw(a_from.m_bin_Sxw)
0056 ,m_bin_Sx2w(a_from.m_bin_Sx2w)
0057 ,m_axes(a_from.m_axes)
0058 ,m_in_range_plane_Sxyw(a_from.m_in_range_plane_Sxyw)
0059 ,m_annotations(a_from.m_annotations)
0060 ,m_all_entries(a_from.m_all_entries)
0061 ,m_in_range_entries(a_from.m_in_range_entries)
0062 ,m_in_range_Sw(a_from.m_in_range_Sw)
0063 ,m_in_range_Sw2(a_from.m_in_range_Sw2)
0064 ,m_in_range_Sxw(a_from.m_in_range_Sxw)
0065 ,m_in_range_Sx2w(a_from.m_in_range_Sx2w)
0066 {}
0067
0068 histo_data& operator=(const histo_data& a_from) {
0069 if(&a_from==this) return *this;
0070 m_title = a_from.m_title;
0071 m_dimension = a_from.m_dimension;
0072 m_bin_number = a_from.m_bin_number;
0073 m_bin_entries = a_from.m_bin_entries;
0074 m_bin_Sw = a_from.m_bin_Sw;
0075 m_bin_Sw2 = a_from.m_bin_Sw2;
0076 m_bin_Sxw = a_from.m_bin_Sxw;
0077 m_bin_Sx2w = a_from.m_bin_Sx2w;
0078 m_axes = a_from.m_axes;
0079 m_in_range_plane_Sxyw = a_from.m_in_range_plane_Sxyw;
0080 m_annotations = a_from.m_annotations;
0081 m_all_entries = a_from.m_all_entries;
0082 m_in_range_entries = a_from.m_in_range_entries;
0083 m_in_range_Sw = a_from.m_in_range_Sw;
0084 m_in_range_Sw2 = a_from.m_in_range_Sw2;
0085 m_in_range_Sxw = a_from.m_in_range_Sxw;
0086 m_in_range_Sx2w = a_from.m_in_range_Sx2w;
0087 return *this;
0088 }
0089
0090 virtual ~histo_data(){}
0091 protected:
0092 void reset_fast_getters(){
0093 //m_in_range_plane_Sxyw is not a fast getter.
0094 m_all_entries = 0;
0095 m_in_range_entries = 0;
0096 m_in_range_Sw = 0;
0097 m_in_range_Sw2 = 0;
0098 m_in_range_Sxw.assign(m_dimension,0);
0099 m_in_range_Sx2w.assign(m_dimension,0);
0100 }
0101 public:
0102 void update_fast_getters() {
0103 reset_fast_getters();
0104 {for(TO ibin=0;ibin<m_bin_number;ibin++) {
0105 if(!histo::is_out(m_axes,ibin)) {
0106 m_in_range_entries += m_bin_entries[ibin];
0107 m_in_range_Sw += m_bin_Sw[ibin];
0108 m_in_range_Sw2 += m_bin_Sw2[ibin];
0109 for(dim_t iaxis=0;iaxis<m_dimension;iaxis++) {
0110 m_in_range_Sxw[iaxis] += m_bin_Sxw[ibin][iaxis];
0111 m_in_range_Sx2w[iaxis] += m_bin_Sx2w[ibin][iaxis];
0112 }
0113 }
0114 m_all_entries += m_bin_entries[ibin];
0115 }}
0116 }
0117
0118 bool equals(const histo_data& a_from,const TW& a_prec,TW(*a_fabs)(TW)) const {
0119 if(&a_from==this) return true;
0120 if(m_title!=a_from.m_title) return false;
0121 if(m_dimension!=a_from.m_dimension) return false;
0122 if(m_bin_number!=a_from.m_bin_number) return false;
0123 if(m_bin_entries!=a_from.m_bin_entries) return false;
0124 if(!vectors_are_equal(m_bin_Sw,a_from.m_bin_Sw,a_prec,a_fabs)) return false;
0125 if(!vectors_are_equal(m_bin_Sw2,a_from.m_bin_Sw2,a_prec,a_fabs)) return false;
0126 if(!vecvecs_are_equal(m_bin_Sxw,a_from.m_bin_Sxw,a_prec,a_fabs)) return false;
0127 if(!vecvecs_are_equal(m_bin_Sx2w,a_from.m_bin_Sx2w,a_prec,a_fabs)) return false;
0128 if(m_axes!=a_from.m_axes) return false;
0129 if(!vectors_are_equal(m_in_range_plane_Sxyw,a_from.m_in_range_plane_Sxyw,a_prec,a_fabs)) return false;
0130 if(m_annotations!=a_from.m_annotations) return false;
0131
0132 if(m_all_entries!=a_from.m_all_entries) return false;
0133 if(m_in_range_entries!=a_from.m_in_range_entries) return false;
0134
0135 if(!numbers_are_equal(m_in_range_Sw,a_from.m_in_range_Sw,a_prec,a_fabs)) return false;
0136 if(!numbers_are_equal(m_in_range_Sw2,a_from.m_in_range_Sw2,a_prec,a_fabs)) return false;
0137 if(!vectors_are_equal(m_in_range_Sxw,a_from.m_in_range_Sxw,a_prec,a_fabs)) return false;
0138 if(!vectors_are_equal(m_in_range_Sx2w,a_from.m_in_range_Sx2w,a_prec,a_fabs)) return false;
0139
0140 return true;
0141 }
0142 bool equals_TH(const histo_data& a_from,const TW& a_prec,TW(*a_fabs)(TW),bool a_cmp_bin_Sw2) const {
0143 // used to compare with an histo built from a TH stream out from a CERN-ROOT file.
0144 if(&a_from==this) return true;
0145 if(m_title!=a_from.m_title) return false;
0146 if(m_dimension!=a_from.m_dimension) return false;
0147 if(m_bin_number!=a_from.m_bin_number) return false;
0148 if(!vectors_are_equal(m_bin_Sw,a_from.m_bin_Sw,a_prec,a_fabs)) return false;
0149 if(a_cmp_bin_Sw2) if(!vectors_are_equal(m_bin_Sw2,a_from.m_bin_Sw2,a_prec,a_fabs)) return false;
0150 if(m_axes!=a_from.m_axes) return false;
0151 if(!vectors_are_equal(m_in_range_plane_Sxyw,a_from.m_in_range_plane_Sxyw,a_prec,a_fabs)) return false;
0152
0153 if(m_all_entries!=a_from.m_all_entries) return false;
0154
0155 if(!numbers_are_equal(m_in_range_Sw,a_from.m_in_range_Sw,a_prec,a_fabs)) return false;
0156 if(!numbers_are_equal(m_in_range_Sw2,a_from.m_in_range_Sw2,a_prec,a_fabs)) return false;
0157
0158 return true;
0159 }
0160 public:
0161 // General :
0162 std::string m_title;
0163 dim_t m_dimension;
0164 // Bins :
0165 TO m_bin_number;
0166 std::vector<TN> m_bin_entries;
0167 std::vector<TW> m_bin_Sw;
0168 std::vector<TW> m_bin_Sw2;
0169 std::vector< std::vector<TC> > m_bin_Sxw;
0170 std::vector< std::vector<TC> > m_bin_Sx2w;
0171 // Axes :
0172 std::vector<axis_t> m_axes;
0173 // etc :
0174 std::vector<TC> m_in_range_plane_Sxyw; // ill-defined relative to slicing, sub, div, mult operations. Handled because of CERN-ROOT.
0175 std::map<std::string,std::string> m_annotations;
0176 // fast getters :
0177 TN m_all_entries; //used if reading from a ROOT file.
0178 TN m_in_range_entries;
0179 TW m_in_range_Sw;
0180 TW m_in_range_Sw2;
0181 std::vector<TC> m_in_range_Sxw;
0182 std::vector<TC> m_in_range_Sx2w;
0183 };
0184
0185 }}
0186
0187 #endif