// $Id$ // // File: DHistogram.cc // Created: Mon Aug 1 08:56:58 EDT 2011 // Creator: davidl (on Linux ifarm1102 2.6.18-128.7.1.el5 x86_64) // #include #include // for NULL #include using namespace std; #include "DHistogram.h" //--------------------------------- // DHistogram (Constructor) //--------------------------------- DHistogram::DHistogram(int Nbins, double lowEdge, double highEdge) { this->Nbins = Nbins; this->lowEdge = lowEdge; this->highEdge = highEdge; binWidth = (highEdge - lowEdge)/(double)Nbins; half_binWidth = binWidth/2.0; if(Nbins>0 && Nbins<100000000){ content = new double[Nbins+1]; xvals = new double[Nbins+1]; for(int bin=1; bin<=Nbins; bin++){ content[bin] = 0.0; xvals[bin] = lowEdge + (double)(bin-1)*binWidth + half_binWidth; } }else{ content = NULL; xvals = NULL; } } //--------------------------------- // ~DHistogram (Destructor) //--------------------------------- DHistogram::~DHistogram() { if(content!=NULL) delete[] content; if(xvals!=NULL) delete[] xvals; } //--------------------------------- // Add //--------------------------------- void DHistogram::Add(const DHistogram *h) { if(h->GetBinWidth()!=binWidth || h->GetNbins()!=Nbins){ _DBG_<<"Histogram parameters don't match!!"<GetBinContent(bin); } } //--------------------------------- // FindBin //--------------------------------- int DHistogram::FindBin(double x) const { int bin = 1 + (int)floor((x-lowEdge)/binWidth); return bin; } //--------------------------------- // FindFirstBinAbove //--------------------------------- int DHistogram::FindFirstBinAbove(double val) const { for(int bin=1; bin<=Nbins; bin++){ if(content[bin] >= val)return bin; } return -1; } //--------------------------------- // Fill //--------------------------------- double DHistogram::Fill(double x, double weight) { int bin = 1 + (int)floor((x-lowEdge)/binWidth); if(bin<1 || bin>Nbins)return 0.0; content[bin] += weight; return content[bin]; } //--------------------------------- // GetNbins //--------------------------------- int DHistogram::GetNbins(void) const { return Nbins; } //--------------------------------- // GetBinLowEdge //--------------------------------- double DHistogram::GetBinLowEdge(int bin) const { return lowEdge + (double)(bin-1)*binWidth; } //--------------------------------- // GetBinCenter //--------------------------------- double DHistogram::GetBinCenter(int bin) const { if(bin<1 || bin>Nbins)return 0.0; return xvals[bin]; } //--------------------------------- // GetBinContent //--------------------------------- double DHistogram::GetBinContent(int bin) const { if(bin<1 || bin>Nbins)return 0.0; return content[bin]; } //--------------------------------- // GetBinWidth //--------------------------------- double DHistogram::GetBinWidth(void) const { return binWidth; } //--------------------------------- // Integral //--------------------------------- double DHistogram::Integral(void) const { double I=0.0; for(int bin=1; bin<=Nbins; bin++) I += content[bin]; return I; } //--------------------------------- // MakeTH1D //--------------------------------- TH1D* DHistogram::MakeTH1D(string name, string title) const { TH1D *h = new TH1D(name.c_str(), title.c_str(), Nbins, lowEdge, highEdge); for(int bin=1; bin<=Nbins; bin++) h->SetBinContent(bin, content[bin]); return h; } //--------------------------------- // Reset //--------------------------------- void DHistogram::Reset(void) { for(int bin=1; bin<=Nbins; bin++)content[bin] = 0.0; } //--------------------------------- // Scale //--------------------------------- void DHistogram::Scale(double scale) { for(int bin=1; bin<=Nbins; bin++)content[bin] *= scale; } //--------------------------------- // SetBinContent //--------------------------------- void DHistogram::SetBinContent(int bin, double val) { if(bin<1 || bin>Nbins)return; content[bin] = val; }