Files
geant4/source/analysis/include/tools/histo/h2
T
2016-06-09 16:46:55 +02:00

150 lines
3.7 KiB
Plaintext

// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef tools_histo_h2
#define tools_histo_h2
#include "b2"
namespace tools {
namespace histo {
template <class TC,class TN,class TW,class TH>
class h2 : public b2<TC,TN,TW,TH> {
typedef b2<TC,TN,TW,TH> parent;
public:
typedef typename b2<TC,TN,TW,TH>::bn_t bn_t;
protected:
virtual TH get_bin_height(int a_offset) const { //TH should be the same as TW
return parent::m_bin_Sw[a_offset];
}
public:
virtual TH bin_error(int aI,int aJ) const {
if(parent::m_bin_number==0) return 0;
bn_t ibin;
if(!parent::m_axes[0].in_range_to_absolute_index(aI,ibin)) return 0;
bn_t jbin;
if(!parent::m_axes[1].in_range_to_absolute_index(aJ,jbin)) return 0;
bn_t offset = ibin + jbin * parent::m_axes[1].m_offset;
return ::sqrt(parent::m_bin_Sw2[offset]);
}
public:
bool multiply(TW aFactor){
if(!parent::base_multiply(aFactor)) return false;
this->update_fast_getters();
return true;
}
bool scale(TW aFactor) {return multiply(aFactor);}
void copy_from_data(const histo_data<TC,TN,TW>& a_from) {
parent::base_from_data(a_from);
}
histo_data<TC,TN,TW> get_histo_data() const {
return parent::base_get_data();
}
bool reset() {
parent::base_reset();
this->update_fast_getters();
return true;
}
bool fill(TC aX,TC aY,TW aWeight = 1) {
//m_coords[0] = aX;
//m_coords[1] = aY;
//return fill_bin(m_coords,aWeight);
if(parent::m_dimension<=0) return false;
bn_t ibin,jbin;
if(!parent::m_axes[0].coord_to_absolute_index(aX,ibin)) return false;
if(!parent::m_axes[1].coord_to_absolute_index(aY,jbin)) return false;
bn_t offset = ibin + jbin * parent::m_axes[1].m_offset;
parent::m_bin_entries[offset]++;
parent::m_bin_Sw[offset] += aWeight;
parent::m_bin_Sw2[offset] += aWeight * aWeight;
TC xw = aX * aWeight;
TC x2w = aX * xw;
parent::m_bin_Sxw[offset][0] += xw;
parent::m_bin_Sx2w[offset][0] += x2w;
TC yw = aY * aWeight;
TC y2w = aY * yw;
parent::m_bin_Sxw[offset][1] += yw;
parent::m_bin_Sx2w[offset][1] += y2w;
bool inRange = true;
if(ibin==0) inRange = false;
else if(ibin==(parent::m_axes[0].m_number_of_bins+1)) inRange = false;
if(jbin==0) inRange = false;
else if(jbin==(parent::m_axes[1].m_number_of_bins+1)) inRange = false;
if(inRange) {
parent::m_in_range_entries++;
parent::m_in_range_Sw += aWeight;
parent::m_in_range_Sxw += xw;
parent::m_in_range_Sx2w += x2w;
parent::m_in_range_Syw += yw;
parent::m_in_range_Sy2w += y2w;
}
return true;
}
bool add(const h2& a_histo){
parent::base_add(a_histo);
this->update_fast_getters();
return true;
}
bool subtract(const h2& a_histo){
parent::base_subtract(a_histo);
this->update_fast_getters();
return true;
}
bool multiply(const h2& a_histo) {
if(!parent::base_multiply(a_histo)) return false;
this->update_fast_getters();
return true;
}
bool divide(const h2& a_histo) {
if(!parent::base_divide(a_histo)) return false;
this->update_fast_getters();
return true;
}
public:
h2(const std::string& a_title,
bn_t aXnumber,TC aXmin,TC aXmax,
bn_t aYnumber,TC aYmin,TC aYmax)
: parent(a_title,aXnumber,aXmin,aXmax,aYnumber,aYmin,aYmax)
{}
h2(const std::string& a_title,
const std::vector<TC>& aEdgesX,
const std::vector<TC>& aEdgesY)
: parent(a_title,aEdgesX,aEdgesY)
{}
virtual ~h2(){}
public:
h2(const h2& a_from): parent(a_from){}
h2& operator=(const h2& a_from){
parent::operator=(a_from);
return *this;
}
};
}}
#endif