Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef tools_histo_histo_data 5 #define tools_histo_histo_data 6 7 #include <vector> 8 #include <map> //for annotations 9 10 #include "axes" 11 #include "../eqT" 12 //#include "../vmanip" //vequ 13 14 namespace tools { 15 namespace histo { 16 17 inline unsigned int dim_planes(unsigned int a_dim) { 18 // m_dim = 1 -> 0 19 // m_dim = 2 -> 0+1=1 20 // m_dim = 3 -> 0+1+2=3 21 // m_dim = 4 -> 0+1+2+3=6 22 typedef unsigned int dim_t; 23 dim_t n = 0; 24 for(dim_t i=0;i<a_dim;i++) n += i; 25 return n; 26 } 27 28 //TC is for a coordinate. 29 //TO is for an offset used to identify a bin. 30 //TN is for a number of entries. 31 //TW is for a weight. 32 33 template <class TC,class TO,class TN,class TW> 34 class histo_data { 35 public: 36 typedef axis<TC,TO> axis_t; 37 typedef unsigned int dim_t; 38 typedef std::map<std::string,std::string> annotations_t; 39 public: 40 histo_data() 41 :m_dimension(0) 42 ,m_bin_number(0) 43 ,m_all_entries(0) 44 ,m_in_range_entries(0) 45 ,m_in_range_Sw(0) 46 ,m_in_range_Sw2(0) 47 {} 48 public: 49 histo_data(const histo_data& a_from) 50 :m_title(a_from.m_title) 51 ,m_dimension(a_from.m_dimension) 52 ,m_bin_number(a_from.m_bin_number) 53 ,m_bin_entries(a_from.m_bin_entries) 54 ,m_bin_Sw(a_from.m_bin_Sw) 55 ,m_bin_Sw2(a_from.m_bin_Sw2) 56 ,m_bin_Sxw(a_from.m_bin_Sxw) 57 ,m_bin_Sx2w(a_from.m_bin_Sx2w) 58 ,m_axes(a_from.m_axes) 59 ,m_in_range_plane_Sxyw(a_from.m_in_range_plane_Sxyw) 60 ,m_annotations(a_from.m_annotations) 61 ,m_all_entries(a_from.m_all_entries) 62 ,m_in_range_entries(a_from.m_in_range_entries) 63 ,m_in_range_Sw(a_from.m_in_range_Sw) 64 ,m_in_range_Sw2(a_from.m_in_range_Sw2) 65 ,m_in_range_Sxw(a_from.m_in_range_Sxw) 66 ,m_in_range_Sx2w(a_from.m_in_range_Sx2w) 67 {} 68 69 histo_data& operator=(const histo_data& a_from) { 70 if(&a_from==this) return *this; 71 m_title = a_from.m_title; 72 m_dimension = a_from.m_dimension; 73 m_bin_number = a_from.m_bin_number; 74 m_bin_entries = a_from.m_bin_entries; 75 m_bin_Sw = a_from.m_bin_Sw; 76 m_bin_Sw2 = a_from.m_bin_Sw2; 77 m_bin_Sxw = a_from.m_bin_Sxw; 78 m_bin_Sx2w = a_from.m_bin_Sx2w; 79 m_axes = a_from.m_axes; 80 m_in_range_plane_Sxyw = a_from.m_in_range_plane_Sxyw; 81 m_annotations = a_from.m_annotations; 82 m_all_entries = a_from.m_all_entries; 83 m_in_range_entries = a_from.m_in_range_entries; 84 m_in_range_Sw = a_from.m_in_range_Sw; 85 m_in_range_Sw2 = a_from.m_in_range_Sw2; 86 m_in_range_Sxw = a_from.m_in_range_Sxw; 87 m_in_range_Sx2w = a_from.m_in_range_Sx2w; 88 return *this; 89 } 90 91 virtual ~histo_data(){} 92 protected: 93 void reset_fast_getters(){ 94 //m_in_range_plane_Sxyw is not a fast getter. 95 m_all_entries = 0; 96 m_in_range_entries = 0; 97 m_in_range_Sw = 0; 98 m_in_range_Sw2 = 0; 99 m_in_range_Sxw.assign(m_dimension,0); 100 m_in_range_Sx2w.assign(m_dimension,0); 101 } 102 public: 103 void update_fast_getters() { 104 reset_fast_getters(); 105 {for(TO ibin=0;ibin<m_bin_number;ibin++) { 106 if(!histo::is_out(m_axes,ibin)) { 107 m_in_range_entries += m_bin_entries[ibin]; 108 m_in_range_Sw += m_bin_Sw[ibin]; 109 m_in_range_Sw2 += m_bin_Sw2[ibin]; 110 for(dim_t iaxis=0;iaxis<m_dimension;iaxis++) { 111 m_in_range_Sxw[iaxis] += m_bin_Sxw[ibin][iaxis]; 112 m_in_range_Sx2w[iaxis] += m_bin_Sx2w[ibin][iaxis]; 113 } 114 } 115 m_all_entries += m_bin_entries[ibin]; 116 }} 117 } 118 119 bool equals(const histo_data& a_from,const TW& a_prec,TW(*a_fabs)(TW)) const { 120 if(&a_from==this) return true; 121 if(m_title!=a_from.m_title) return false; 122 if(m_dimension!=a_from.m_dimension) return false; 123 if(m_bin_number!=a_from.m_bin_number) return false; 124 if(m_bin_entries!=a_from.m_bin_entries) return false; 125 //if(!vequ(m_bin_entries,a_from.m_bin_entries)) return false; 126 if(!vectors_are_equal(m_bin_Sw,a_from.m_bin_Sw,a_prec,a_fabs)) return false; 127 if(!vectors_are_equal(m_bin_Sw2,a_from.m_bin_Sw2,a_prec,a_fabs)) return false; 128 if(!vecvecs_are_equal(m_bin_Sxw,a_from.m_bin_Sxw,a_prec,a_fabs)) return false; 129 if(!vecvecs_are_equal(m_bin_Sx2w,a_from.m_bin_Sx2w,a_prec,a_fabs)) return false; 130 if(m_axes!=a_from.m_axes) return false; 131 if(!vectors_are_equal(m_in_range_plane_Sxyw,a_from.m_in_range_plane_Sxyw,a_prec,a_fabs)) return false; 132 if(m_annotations!=a_from.m_annotations) return false; 133 134 if(m_all_entries!=a_from.m_all_entries) return false; 135 if(m_in_range_entries!=a_from.m_in_range_entries) return false; 136 137 if(!numbers_are_equal(m_in_range_Sw,a_from.m_in_range_Sw,a_prec,a_fabs)) return false; 138 if(!numbers_are_equal(m_in_range_Sw2,a_from.m_in_range_Sw2,a_prec,a_fabs)) return false; 139 if(!vectors_are_equal(m_in_range_Sxw,a_from.m_in_range_Sxw,a_prec,a_fabs)) return false; 140 if(!vectors_are_equal(m_in_range_Sx2w,a_from.m_in_range_Sx2w,a_prec,a_fabs)) return false; 141 142 return true; 143 } 144 bool equals_TH(const histo_data& a_from,const TW& a_prec,TW(*a_fabs)(TW),bool a_cmp_bin_Sw2) const { 145 // used to compare with an histo built from a TH stream out from a CERN-ROOT file. 146 if(&a_from==this) return true; 147 if(m_title!=a_from.m_title) return false; 148 if(m_dimension!=a_from.m_dimension) return false; 149 if(m_bin_number!=a_from.m_bin_number) return false; 150 //if(m_bin_entries!=a_from.m_bin_entries) return false; 151 if(!vectors_are_equal(m_bin_Sw,a_from.m_bin_Sw,a_prec,a_fabs)) return false; 152 if(a_cmp_bin_Sw2) if(!vectors_are_equal(m_bin_Sw2,a_from.m_bin_Sw2,a_prec,a_fabs)) return false; 153 //if(!vecvecs_are_equal(m_bin_Sxw,a_from.m_bin_Sxw,a_prec,a_fabs)) return false; 154 //if(!vecvecs_are_equal(m_bin_Sx2w,a_from.m_bin_Sx2w,a_prec,a_fabs)) return false; 155 if(m_axes!=a_from.m_axes) return false; 156 if(!vectors_are_equal(m_in_range_plane_Sxyw,a_from.m_in_range_plane_Sxyw,a_prec,a_fabs)) return false; 157 //if(m_annotations!=a_from.m_annotations) return false; 158 159 if(m_all_entries!=a_from.m_all_entries) return false; 160 //if(m_in_range_entries!=a_from.m_in_range_entries) return false; 161 162 if(!numbers_are_equal(m_in_range_Sw,a_from.m_in_range_Sw,a_prec,a_fabs)) return false; 163 if(!numbers_are_equal(m_in_range_Sw2,a_from.m_in_range_Sw2,a_prec,a_fabs)) return false; 164 //if(!vectors_are_equal(m_in_range_Sxw,a_from.m_in_range_Sxw,a_prec,a_fabs)) return false; 165 //if(!vectors_are_equal(m_in_range_Sx2w,a_from.m_in_range_Sx2w,a_prec,a_fabs)) return false; 166 167 return true; 168 } 169 public: 170 // General : 171 std::string m_title; 172 dim_t m_dimension; 173 // Bins : 174 TO m_bin_number; 175 std::vector<TN> m_bin_entries; 176 std::vector<TW> m_bin_Sw; 177 std::vector<TW> m_bin_Sw2; 178 std::vector< std::vector<TC> > m_bin_Sxw; 179 std::vector< std::vector<TC> > m_bin_Sx2w; 180 // Axes : 181 std::vector<axis_t> m_axes; 182 // etc : 183 std::vector<TC> m_in_range_plane_Sxyw; // ill-defined relative to slicing, sub, div, mult operations. Handled because of CERN-ROOT. 184 std::map<std::string,std::string> m_annotations; 185 // fast getters : 186 TN m_all_entries; //used if reading from a ROOT file. 187 TN m_in_range_entries; 188 TW m_in_range_Sw; 189 TW m_in_range_Sw2; 190 std::vector<TC> m_in_range_Sxw; 191 std::vector<TC> m_in_range_Sx2w; 192 }; 193 194 }} 195 196 #endif