Warning, /include/Geant4/tools/histo/h2 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_h2
0005 #define tools_histo_h2
0006
0007 #include "b2"
0008
0009 namespace tools {
0010 namespace histo {
0011
0012 template <class TC,class TO,class TN,class TW,class TH>
0013 class h2 : public b2<TC,TO,TN,TW,TH> {
0014 typedef b2<TC,TO,TN,TW,TH> parent;
0015 public:
0016 typedef histo_data<TC,TO,TN,TW> hd_t;
0017 typedef typename b2<TC,TO,TN,TW,TH>::bn_t bn_t;
0018 protected:
0019 virtual TH get_bin_height(TO a_offset) const { //TH should be the same as TW
0020 return parent::m_bin_Sw[a_offset];
0021 }
0022 public:
0023
0024 virtual TH bin_error(int aI,int aJ) const {
0025 TO offset;
0026 if(!parent::_find_offset(aI,aJ,offset)) return 0;
0027 return ::sqrt(parent::m_bin_Sw2[offset]);
0028 }
0029
0030 public:
0031 bool multiply(TW a_factor){return parent::base_multiply(a_factor);}
0032 bool scale(TW a_factor) {return multiply(a_factor);}
0033
0034 void copy_from_data(const hd_t& a_from) {parent::base_from_data(a_from);}
0035 hd_t get_histo_data() const {return *this;} //deprecated. Keep it for g4tools.
0036
0037 bool reset() {
0038 parent::base_reset();
0039 return true;
0040 }
0041
0042 bool fill(TC aX,TC aY,TW aWeight = 1) {
0043 if(parent::m_dimension!=2) return false;
0044
0045 bn_t ibin,jbin;
0046 if(!parent::m_axes[0].coord_to_absolute_index(aX,ibin)) return false;
0047 if(!parent::m_axes[1].coord_to_absolute_index(aY,jbin)) return false;
0048 TO offset = ibin + jbin * parent::m_axes[1].m_offset;
0049
0050 parent::m_bin_entries[offset]++;
0051 parent::m_bin_Sw[offset] += aWeight;
0052 parent::m_bin_Sw2[offset] += aWeight * aWeight;
0053
0054 TC xw = aX * aWeight;
0055 TC x2w = aX * xw;
0056 parent::m_bin_Sxw[offset][0] += xw;
0057 parent::m_bin_Sx2w[offset][0] += x2w;
0058
0059 TC yw = aY * aWeight;
0060 TC y2w = aY * yw;
0061 parent::m_bin_Sxw[offset][1] += yw;
0062 parent::m_bin_Sx2w[offset][1] += y2w;
0063
0064 bool inRange = true;
0065 if(ibin==0) inRange = false;
0066 else if(ibin==(parent::m_axes[0].m_number_of_bins+1)) inRange = false;
0067
0068 if(jbin==0) inRange = false;
0069 else if(jbin==(parent::m_axes[1].m_number_of_bins+1)) inRange = false;
0070
0071 parent::m_all_entries++;
0072 if(inRange) {
0073 parent::m_in_range_plane_Sxyw[0] += aX * aY * aWeight;
0074
0075 // fast getters :
0076 parent::m_in_range_entries++;
0077 parent::m_in_range_Sw += aWeight;
0078 parent::m_in_range_Sw2 += aWeight*aWeight;
0079
0080 parent::m_in_range_Sxw[0] += xw;
0081 parent::m_in_range_Sx2w[0] += x2w;
0082
0083 parent::m_in_range_Sxw[1] += yw;
0084 parent::m_in_range_Sx2w[1] += y2w;
0085 }
0086
0087 return true;
0088 }
0089
0090 bool set_bin_content(bn_t a_ibin,bn_t a_jbin,
0091 TN a_entries,TW a_Sw,TW a_Sw2,
0092 TC a_Sxw,TC a_Sx2w,TC a_Syw,TC a_Sy2w) {
0093 if(parent::m_dimension!=2) return false;
0094 if(a_ibin>(parent::m_axes[0].m_number_of_bins+1)) return false;
0095 if(a_jbin>(parent::m_axes[1].m_number_of_bins+1)) return false;
0096
0097 bool inRange = true;
0098 if(a_ibin==0) inRange = false;
0099 else if(a_ibin==(parent::m_axes[0].m_number_of_bins+1)) inRange = false;
0100 if(a_jbin==0) inRange = false;
0101 else if(a_jbin==(parent::m_axes[1].m_number_of_bins+1)) inRange = false;
0102
0103 TO offset = a_ibin + a_jbin * parent::m_axes[1].m_offset;
0104
0105 parent::m_all_entries -= parent::m_bin_entries[offset];
0106 if(inRange) {
0107 parent::m_in_range_entries -= parent::m_bin_entries[offset];
0108 parent::m_in_range_Sw -= parent::m_bin_Sw[offset];
0109 parent::m_in_range_Sw2 -= parent::m_bin_Sw2[offset];
0110 parent::m_in_range_Sxw[0] -= parent::m_bin_Sxw[offset][0];
0111 parent::m_in_range_Sx2w[0] -= parent::m_bin_Sx2w[offset][0];
0112 parent::m_in_range_Sxw[1] -= parent::m_bin_Sxw[offset][1];
0113 parent::m_in_range_Sx2w[1] -= parent::m_bin_Sx2w[offset][1];
0114 }
0115
0116 parent::m_bin_entries[offset] = a_entries;
0117 parent::m_bin_Sw[offset] = a_Sw;
0118 parent::m_bin_Sw2[offset] = a_Sw2;
0119
0120 parent::m_bin_Sxw[offset][0] = a_Sxw;
0121 parent::m_bin_Sx2w[offset][0] = a_Sx2w;
0122 parent::m_bin_Sxw[offset][1] = a_Syw;
0123 parent::m_bin_Sx2w[offset][1] = a_Sy2w;
0124
0125 parent::m_all_entries += a_entries;
0126 if(inRange) {
0127 //parent::m_in_range_plane_Sxyw[0] ??? ill-defined.
0128
0129 parent::m_in_range_entries += a_entries;
0130 parent::m_in_range_Sw += a_Sw;
0131 parent::m_in_range_Sw2 += a_Sw2;
0132
0133 parent::m_in_range_Sxw[0] += a_Sxw;
0134 parent::m_in_range_Sx2w[0] += a_Sx2w;
0135 parent::m_in_range_Sxw[1] += a_Syw;
0136 parent::m_in_range_Sx2w[1] += a_Sy2w;
0137 }
0138
0139 return true;
0140 }
0141
0142 bool get_bin_content(bn_t a_ibin,bn_t a_jbin,
0143 TN& a_entries,TW& a_Sw,TW& a_Sw2,
0144 TC& a_Sxw,TC& a_Sx2w,
0145 TC& a_Syw,TC& a_Sy2w) {
0146 if(parent::m_dimension!=2) {
0147 a_entries = 0;a_Sw = 0;a_Sw2 = 0;
0148 a_Sxw = 0;a_Sx2w = 0;
0149 a_Syw = 0;a_Sy2w = 0;
0150 return false;
0151 }
0152 if(a_ibin>(parent::m_axes[0].m_number_of_bins+1)) {
0153 a_entries = 0;a_Sw = 0;a_Sw2 = 0;
0154 a_Sxw = 0;a_Sx2w = 0;
0155 a_Syw = 0;a_Sy2w = 0;
0156 return false;
0157 }
0158 if(a_jbin>(parent::m_axes[1].m_number_of_bins+1)) {
0159 a_entries = 0;a_Sw = 0;a_Sw2 = 0;
0160 a_Sxw = 0;a_Sx2w = 0;
0161 a_Syw = 0;a_Sy2w = 0;
0162 return false;
0163 }
0164
0165 TO offset = a_ibin + a_jbin * parent::m_axes[1].m_offset;
0166
0167 a_entries = parent::m_bin_entries[offset];
0168 a_Sw = parent::m_bin_Sw[offset];
0169 a_Sw2 = parent::m_bin_Sw2[offset];
0170
0171 a_Sxw = parent::m_bin_Sxw[offset][0];
0172 a_Sx2w = parent::m_bin_Sx2w[offset][0];
0173 a_Syw = parent::m_bin_Sxw[offset][1];
0174 a_Sy2w = parent::m_bin_Sx2w[offset][1];
0175
0176 return true;
0177 }
0178
0179 bool add(const h2& a_histo){
0180 parent::base_add(a_histo);
0181 return true;
0182 }
0183 bool subtract(const h2& a_histo){
0184 parent::base_subtract(a_histo);
0185 return true;
0186 }
0187
0188 bool multiply(const h2& a_histo) {
0189 return parent::base_multiply(a_histo);
0190 }
0191
0192 bool divide(const h2& a_histo) {
0193 return parent::base_divide(a_histo);
0194 }
0195
0196 bool equals_TH(const h2& a_from,const TW& a_prec,TW(*a_fabs)(TW)) const {
0197 if(!parent::equals_TH(a_from,a_prec,a_fabs,true)) return false;
0198 return true;
0199 }
0200
0201 void not_a_profile() const {}
0202
0203 public: //CERN-ROOT API
0204 bool Fill(TC aX,TC aY,TW aWeight = 1) {return fill(aX,aY,aWeight);}
0205
0206 public:
0207 h2(const std::string& a_title,bn_t aXnumber,TC aXmin,TC aXmax,bn_t aYnumber,TC aYmin,TC aYmax)
0208 :parent(a_title,aXnumber,aXmin,aXmax,aYnumber,aYmin,aYmax)
0209 {}
0210 h2(const std::string& a_title,const std::vector<TC>& a_edges_x,const std::vector<TC>& a_edges_y)
0211 :parent(a_title,a_edges_x,a_edges_y)
0212 {}
0213
0214 virtual ~h2(){}
0215 public:
0216 h2(const h2& a_from): parent(a_from){}
0217 h2& operator=(const h2& a_from){
0218 parent::operator=(a_from);
0219 return *this;
0220 }
0221 };
0222
0223 }}
0224
0225 #endif
0226