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