Warning, /include/Geant4/tools/histo/h1 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_h1
0005 #define tools_histo_h1
0006
0007 #include "b1"
0008
0009 namespace tools {
0010 namespace histo { //have that for h1 ?
0011
0012 //TC is for a coordinate.
0013 //TO is for an offset used to identify a bin.
0014 //TN is for a number of entries.
0015 //TW is for a weight.
0016 //TH is for a height. Should be the same as TW.
0017
0018 template <class TC,class TO,class TN,class TW,class TH>
0019 class h1 : public b1<TC,TO,TN,TW,TH> {
0020 typedef b1<TC,TO,TN,TW,TH> parent;
0021 public:
0022 typedef histo_data<TC,TO,TN,TW> hd_t;
0023 typedef typename parent::bn_t bn_t;
0024 typedef typename parent::axis_t axis_t;
0025 protected:
0026 virtual TH get_bin_height(TO a_offset) const { //TH should be the same as TW
0027 return parent::m_bin_Sw[a_offset];
0028 }
0029 public:
0030 virtual TH bin_error(int aI) const { //TH should be the same as TW
0031 TO offset;
0032 if(!parent::_find_offset(aI,offset)) return 0;
0033 return ::sqrt(parent::m_bin_Sw2[offset]);
0034 }
0035
0036 public:
0037 bool multiply(TW a_factor){return parent::base_multiply(a_factor);}
0038 bool scale(TW a_factor) {return multiply(a_factor);}
0039
0040 void copy_from_data(const hd_t& a_from) {parent::base_from_data(a_from);}
0041 hd_t get_histo_data() const {return *this;} //deprecated. Keep it for g4tools.
0042
0043 bool reset() {
0044 parent::base_reset();
0045 return true;
0046 }
0047
0048 bool fill(TC aX,TW aWeight = 1) {
0049 if(parent::m_dimension!=1) return false;
0050
0051 bn_t ibin;
0052 if(!parent::m_axes[0].coord_to_absolute_index(aX,ibin)) return false;
0053
0054 TO offset = ibin;
0055
0056 parent::m_bin_entries[offset]++;
0057 parent::m_bin_Sw[offset] += aWeight;
0058 parent::m_bin_Sw2[offset] += aWeight * aWeight;
0059
0060 TC xw = aX * aWeight;
0061 TC x2w = aX * xw;
0062 parent::m_bin_Sxw[offset][0] += xw;
0063 parent::m_bin_Sx2w[offset][0] += x2w;
0064
0065 bool inRange = true;
0066 if(ibin==0) inRange = false;
0067 else if(ibin==(parent::m_axes[0].m_number_of_bins+1)) inRange = false;
0068
0069 parent::m_all_entries++;
0070 if(inRange) {
0071 // fast getters :
0072 parent::m_in_range_entries++;
0073 parent::m_in_range_Sw += aWeight;
0074 parent::m_in_range_Sw2 += aWeight*aWeight;
0075
0076 parent::m_in_range_Sxw[0] += xw;
0077 parent::m_in_range_Sx2w[0] += x2w;
0078 }
0079
0080 return true;
0081 }
0082
0083 bool set_bin_content(bn_t a_ibin,TN a_entries,TW a_Sw,TW a_Sw2,TC a_Sxw,TC a_Sx2w) {
0084 if(parent::m_dimension!=1) return false;
0085 if(a_ibin>(parent::m_axes[0].m_number_of_bins+1)) return false;
0086
0087 bool inRange = true;
0088 if(a_ibin==0) inRange = false;
0089 else if(a_ibin==(parent::m_axes[0].m_number_of_bins+1)) inRange = false;
0090
0091 TO offset = a_ibin;
0092
0093 parent::m_all_entries -= parent::m_bin_entries[offset];
0094 if(inRange) {
0095 parent::m_in_range_entries -= parent::m_bin_entries[offset];
0096 parent::m_in_range_Sw -= parent::m_bin_Sw[offset];
0097 parent::m_in_range_Sw2 -= parent::m_bin_Sw2[offset];
0098 parent::m_in_range_Sxw[0] -= parent::m_bin_Sxw[offset][0];
0099 parent::m_in_range_Sx2w[0] -= parent::m_bin_Sx2w[offset][0];
0100 }
0101
0102 parent::m_bin_entries[offset] = a_entries;
0103 parent::m_bin_Sw[offset] = a_Sw;
0104 parent::m_bin_Sw2[offset] = a_Sw2;
0105
0106 parent::m_bin_Sxw[offset][0] = a_Sxw;
0107 parent::m_bin_Sx2w[offset][0] = a_Sx2w;
0108
0109 parent::m_all_entries += a_entries;
0110 if(inRange) {
0111 parent::m_in_range_entries += a_entries;
0112 parent::m_in_range_Sw += a_Sw;
0113 parent::m_in_range_Sw2 += a_Sw2;
0114
0115 parent::m_in_range_Sxw[0] += a_Sxw;
0116 parent::m_in_range_Sx2w[0] += a_Sx2w;
0117 }
0118
0119 return true;
0120 }
0121
0122 bool get_bin_content(bn_t a_ibin,TN& a_entries,TW& a_Sw,TW& a_Sw2,TC& a_Sxw,TC& a_Sx2w) {
0123 if(parent::m_dimension!=1) {
0124 a_entries = 0;a_Sw = 0;a_Sw2 = 0;a_Sxw = 0;a_Sx2w = 0;
0125 return false;
0126 }
0127 if(a_ibin>(parent::m_axes[0].m_number_of_bins+1)) {
0128 a_entries = 0;a_Sw = 0;a_Sw2 = 0;a_Sxw = 0;a_Sx2w = 0;
0129 return false;
0130 }
0131
0132 TO offset = a_ibin;
0133
0134 a_entries = parent::m_bin_entries[offset];
0135 a_Sw = parent::m_bin_Sw[offset];
0136 a_Sw2 = parent::m_bin_Sw2[offset];
0137
0138 a_Sxw = parent::m_bin_Sxw[offset][0];
0139 a_Sx2w = parent::m_bin_Sx2w[offset][0];
0140
0141 return true;
0142 }
0143
0144 bool add(const h1& a_histo){
0145 parent::base_add(a_histo);
0146 return true;
0147 }
0148 bool subtract(const h1& a_histo){
0149 parent::base_subtract(a_histo);
0150 return true;
0151 }
0152
0153 bool multiply(const h1& a_histo) {
0154 return parent::base_multiply(a_histo);
0155 }
0156
0157 bool divide(const h1& a_histo) {
0158 return parent::base_divide(a_histo);
0159 }
0160
0161 bool gather_bins(unsigned int a_factor) { //for exa 2,3.
0162 if(!a_factor) return false;
0163
0164 // actual bin number must be a multiple of a_factor.
0165
0166 const axis_t& _axis = parent::axis();
0167
0168 bn_t n = _axis.bins();
0169 if(!n) return false;
0170
0171 bn_t new_n = n/a_factor;
0172 if(a_factor*new_n!=n) return false;
0173
0174 h1* new_h = 0;
0175 if(_axis.is_fixed_binning()) {
0176 new_h = new h1(parent::m_title,new_n,_axis.lower_edge(),_axis.upper_edge());
0177 } else {
0178 const std::vector<TC>& _edges = _axis.edges();
0179 std::vector<TC> new_edges(new_n+1);
0180 for(bn_t ibin=0;ibin<new_n;ibin++) {
0181 new_edges[ibin] = _edges[ibin*a_factor];
0182 }
0183 new_edges[new_n] = _edges[n]; //upper edge.
0184 new_h = new h1(parent::m_title,new_edges);
0185 }
0186 if(!new_h) return false;
0187
0188 TO offset,new_offset,offac;
0189 for(bn_t ibin=0;ibin<new_n;ibin++) {
0190 new_offset = ibin+1;
0191 offset = a_factor*ibin+1;
0192 for(unsigned int ifac=0;ifac<a_factor;ifac++) {
0193 offac = offset+ifac;
0194 new_h->m_bin_entries[new_offset] += parent::m_bin_entries[offac];
0195 new_h->m_bin_Sw[new_offset] += parent::m_bin_Sw[offac];
0196 new_h->m_bin_Sw2[new_offset] += parent::m_bin_Sw2[offac];
0197 new_h->m_bin_Sxw[new_offset][0] += parent::m_bin_Sxw[offac][0];
0198 new_h->m_bin_Sx2w[new_offset][0] += parent::m_bin_Sx2w[offac][0];
0199 }
0200 }
0201
0202 //underflow :
0203 new_offset = 0;
0204 offac = 0;
0205 new_h->m_bin_entries[new_offset] = parent::m_bin_entries[offac];
0206 new_h->m_bin_Sw[new_offset] = parent::m_bin_Sw[offac];
0207 new_h->m_bin_Sw2[new_offset] = parent::m_bin_Sw2[offac];
0208 new_h->m_bin_Sxw[new_offset][0] = parent::m_bin_Sxw[offac][0];
0209 new_h->m_bin_Sx2w[new_offset][0] = parent::m_bin_Sx2w[offac][0];
0210
0211 //overflow :
0212 new_offset = new_n+1;
0213 offac = n+1;
0214 new_h->m_bin_entries[new_offset] = parent::m_bin_entries[offac];
0215 new_h->m_bin_Sw[new_offset] = parent::m_bin_Sw[offac];
0216 new_h->m_bin_Sw2[new_offset] = parent::m_bin_Sw2[offac];
0217 new_h->m_bin_Sxw[new_offset][0] = parent::m_bin_Sxw[offac][0];
0218 new_h->m_bin_Sx2w[new_offset][0] = parent::m_bin_Sx2w[offac][0];
0219
0220 *this = *new_h;
0221 return true;
0222 }
0223
0224 bool equals_TH(const h1& a_from,const TW& a_prec,TW(*a_fabs)(TW)) const {
0225 if(!parent::equals_TH(a_from,a_prec,a_fabs,true)) return false;
0226 return true;
0227 }
0228
0229 void not_a_profile() const {}
0230
0231 public: //CERN-ROOT API (for MEMPHYS sim).
0232 bool Fill(TC aX,TW aWeight = 1) {return fill(aX,aWeight);}
0233
0234 public:
0235 h1(const std::string& a_title,bn_t aXnumber,TC aXmin,TC aXmax)
0236 :parent(a_title,aXnumber,aXmin,aXmax){}
0237
0238 h1(const std::string& a_title,const std::vector<TC>& a_edges)
0239 :parent(a_title,a_edges){}
0240
0241 virtual ~h1(){}
0242 public:
0243 h1(const h1& a_from):parent(a_from){}
0244 h1& operator=(const h1& a_from){
0245 if(&a_from==this) return *this;
0246 parent::operator=(a_from);
0247 return *this;
0248 }
0249 };
0250
0251 }}
0252
0253 #endif
0254
0255
0256
0257