Just a comment: the Integral method provided with TGraph seems not to work with logarithmic binning (so in the end I wrote something myself). It would be nice to have an alternative method prodived (e.g. simple trapezoidal approximation) to do 2D integration for binnings other than linear.
Can you be a be more precise ? TGraph has no “binning”. TGraph can be drawn in log scales but that purely graphics… So what do you mean by “log binning” ?
You are right, my initial description was was not very precise. Actually, the idea with the log binning was a guess (and at a second look nonsense - sorry).
I had a look in the method in question: Double_t TGraph::Integral(Int_t first, Int_t last) const (v5.27.04).
And I found out that disagreement what I got for the integral was due to the fact that when I called the method the loop inside ( “for(Int_t i=first;i<=last;i++)” ) was adding as last element: “(fY[last]+fY[first])*(fX[first]-fX[last])” which to my mind it shouldn’t!!! So by changing in the source the loop to: “for(Int_t i=first;i<last;i++)” the integral is correct.
I think the TGraph::Integral method is correct. You have to use the TGraph indexing convention, the index starts from 0 and the index for the n-th point is n-1.
Yes, I know. Actually, inside the method it is checked that we don’t get out-of-bound with the indices (e.g.: “if(last >= fNpoints) last = fNpoints-1;”). But regardless if I integrate “from 0 to fNpoints-1” or “from 0 to fNpoints-2”, etc, the indices of the last term in the sum are i=last and j=first - which shouldn’t be!
Check out this small piece of python code:
from array import array
from ROOT import TGraph
TESTgr=TGraph( 3, array('f',[1,2,3]) , array('f',[1,1,1]) )
print 'TEST integral:',TESTgr.Integral()
The integral should be 2. Instead the output I get is:
TEST integral: 0.0
If I cout the variables of the loop of TGraph::Integral method I get:
i=0 j=1 fX[i]=1 fX[j]=2 sum term:2
i=1 j=2 fX[i]=2 fX[j]=3 sum term:2
i=2 j=0 fX[i]=3 fX[j]=1 sum term:-4
… which is then divided by 2: (2+2-4)/2=0. Obviously the last sum term is wrong as I described before!
TGraph::Integral returns the integral of a closed polygon. So in your specific example it is right to return 0 since the TGraph is not closed. To get the value you expect you should use the TGraph with :
Ok, I guess I skipped this - it’s actually written in the method description - I was just assuming that the method does what one would expect. Thanks for noting!
Anyways, wouldn’t it be nice to have a method to give the area between a series of points and the x-axis without the need to modify your graph? It could be quite useful - for example, to estimate the impact of a cut parameter.