00001 #include <math.h>
00002 #include <cstring>
00003 #include "Hist.h"
00004
00005 namespace pypsalg {
00006
00007 Hist1D::Hist1D(unsigned nbins, double low, double high) :
00008 axis(nbins, low, high)
00009 {
00010 data = make_ndarray<double>(nbins);
00011 std::memset(data.data(), 0, data.size()*sizeof(double));
00012 }
00013
00014 ndarray<double, 1> Hist1D::get()
00015 {
00016 return data;
00017 }
00018
00019 void Hist1D::fill(double val, double weight)
00020 {
00021 int bin = axis.bin(val);
00022 if(bin >= 0 && bin < (int) axis.nbins()) {
00023 data[bin] += weight;
00024 }
00025 }
00026
00027 void Hist1D::fill(ndarray<double, 1> vals, double weight)
00028 {
00029 const ndarray<double,1>::shape_t* shape = vals.shape();
00030 for (unsigned i=0; i<shape[0]; i++) {
00031 fill(vals[i],weight);
00032 }
00033 }
00034
00035 void Hist1D::fill(ndarray<double, 1> vals, ndarray<double, 1> weights)
00036 {
00037 const ndarray<double,1>::shape_t* shapevals = vals.shape();
00038 const ndarray<double,1>::shape_t* shapeweights = weights.shape();
00039 assert (shapevals[0]==shapeweights[0]);
00040 for (unsigned i=0; i<shapevals[0]; i++) {
00041 fill(vals[i],weights[i]);
00042 }
00043 }
00044
00045 void Hist1D::fill(ndarray<double, 2> valsWeights)
00046 {
00047 const ndarray<double,2>::shape_t* shape = valsWeights.shape();
00048 assert(shape[1]==2);
00049 for (unsigned i=0; i<shape[0]; i++) {
00050 fill(valsWeights[i][0],valsWeights[i][1]);
00051 }
00052 }
00053
00054 Hist2D::Hist2D(unsigned nbinsx, double xlow, double xhigh,
00055 unsigned nbinsy, double ylow, double yhigh) :
00056 _xaxis(nbinsx, xlow, xhigh),
00057 _yaxis(nbinsy, ylow, yhigh)
00058 {
00059 _data = make_ndarray<double>(nbinsx, nbinsy);
00060 std::memset(_data.data(), 0, _data.size()*sizeof(double));
00061 }
00062
00063 ndarray<double, 2> Hist2D::get()
00064 {
00065 return _data;
00066 }
00067
00068 void Hist2D::fill(double xval, double yval, double weight)
00069 {
00070 int binx = _xaxis.bin(xval);
00071 int biny = _yaxis.bin(yval);
00072 if(binx >= 0 && binx < (int) _xaxis.nbins() &&
00073 biny >= 0 && biny < (int) _yaxis.nbins()) {
00074 _data[binx][biny] += weight;
00075 }
00076 }
00077
00078 void Hist2D::fill(ndarray<double, 2> xyvals, double weight)
00079 {
00080 const ndarray<double,1>::shape_t* shape = xyvals.shape();
00081 assert(shape[1]==2);
00082 for (unsigned i=0; i<shape[0]; i++) {
00083 fill(xyvals[i][0],xyvals[i][1],weight);
00084 }
00085 }
00086
00087 void Hist2D::fill(ndarray<double, 2> xyWeightVals)
00088 {
00089 const ndarray<double,1>::shape_t* shape = xyWeightVals.shape();
00090 assert(shape[1]==3);
00091 for (unsigned i=0; i<shape[0]; i++) {
00092 fill(xyWeightVals[i][0],xyWeightVals[i][1],xyWeightVals[i][2]);
00093 }
00094 }
00095
00096 void Hist2D::fill(ndarray<double, 1> xvals, ndarray<double, 1> yvals, ndarray<double, 1> weights)
00097 {
00098 const ndarray<double,1>::shape_t* xshape = xvals.shape();
00099 const ndarray<double,1>::shape_t* yshape = yvals.shape();
00100 const ndarray<double,1>::shape_t* wshape = weights.shape();
00101 assert(xshape[0]==yshape[0]);
00102 assert(xshape[0]==wshape[0]);
00103 for (unsigned i=0; i<xshape[0]; i++) {
00104 fill(xvals[i],yvals[i],weights[i]);
00105 }
00106 }
00107
00108 void Hist2D::fill(ndarray<double, 1> xvals, ndarray<double, 1> yvals, double weight)
00109 {
00110 const ndarray<double,1>::shape_t* xshape = xvals.shape();
00111 const ndarray<double,1>::shape_t* yshape = yvals.shape();
00112 assert(xshape[0]==yshape[0]);
00113 for (unsigned i=0; i<xshape[0]; i++) {
00114 fill(xvals[i],yvals[i],weight);
00115 }
00116 }
00117
00118 }