Re: [ROOT] Integral of a histogram

From: Rene Brun (Rene.Brun@cern.ch)
Date: Thu Jun 12 2003 - 06:47:09 MEST


Hi,

TH1::Integral returns the integral from bin to bin.
Another function could be introduced to return the integral including
the fractions of the two extreme bins.
You can find the bin number for a given value of x with
  int bin = h->GetXaxis()->FindBin(x);

and do something like:
  TAxis *axis = h->GetXaxis();
  int bmin = axis->FindBin(xmin); //in your case xmin=-1.5
  int bmax = axis->FindBin(xmax); //in your case xmax=0.8
  double integral = h->Integral(bmin,bmax);
  integral -= h->GetBinContent(bmin)*(xmin-axis->GetBinLowEdge(bmin))/
              axis->GetBinWidth(bmin);
  integral -= h->GetBinContent(bmax)*(axis->GetBinUpEdge(bmax)-xmax))/
              axis->GetBinWidth(bmax);

These few lines will work for fix or variable bin size histograms.

Rene Brun

On 
Thu, 12 Jun 2003, 
Luigi Esposito-MACRO wrote:

> Hello,
> how can I do the integral of a histogram between two values and not two 
> bin indeces?
> I mean something like that:
> h = new TH1F("h","",100,-3,3);
> h->FillRandom("gaus");
> h->Integral(-1.5,0.8);
> 
> Thank you,
> 
> 	Luillo
> 



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:12 MET