Computing an integral based on a TGraph

Dear ROOT Support,

I have a dataset, read from a file with x and y values, out of which I make a TGraph. I wish to integrate the graph between two values along the x-axis, which sounds trivial, but certainly is not. I understand I have three options:

  1. Make a closed polygon
  2. Generate a histogram from the TGraph
  3. Fit the graph

As I do not wish to fit the graph, I am left with options 1) and 2).

Option 2) would be rather painless, as it is easy to compute an integral from a histogram. However, the histogram I get is empty (drawing the TGraph with option “A”, as it says one can do to make GetHistogram successful, renders the drawing of the TGraph empty, that’s why I have toggled it off in the attached example. The result is the same). Perhaps the GetHistogram function of TGraph only provides the axis; in such case, I’d suggest to rename the function. Any help on retrieving a non-empty histogram from a TGraph would be appreciated.

Option 1) turns out to work in mysterious ways. I have attached a minimal working example in pyroot, in which I create a closed polygon by adding two points along y=0; one at the origo in the beginning, and one at the maximum x-value at the end. Integrating the full polygon works, but any other index ranges give unexpected numbers? (1.07 KB)

1 Like

Why don’t you use: … 1819cf7533

I am (see attached code); it’s not working. I’ve made the whole graph a polygon as described, but it only gives the correct answer if I do the integral of the full graph; Integral(0,-1). If that’s the only things that works, I do not see why there is an option to give an index range.

Oh sorry … I did not see…
May be the simplest would be to make a temporary TGraph with the subrange you need ?

Ok. I don’t see how copying the TGraph (Clone) will help, because I don’t know how to manipulate it to create a closed polygon. What I can do is to add four points to the data, enclosing the region I want to integrate and make a second TGraph from this:

[code]#add points to make a closed polygon just where you want the integral

to be sure to have the right lines drawn, give all four points

x.append(x_stop), y.append(y[x_stop])
x.append(x_stop), y.append(0)
x.append(x_start), y.append(0)
x.append(x_start), y.append(y[x_start])
gr_int = TGraph( len(x), x, y )
print gr_int.Integral() [/code]

This seems to work.

So you confirm that the index range of the Integral function of TGraph is not useful, as well as that there is no way to extract a meaningful histogram from a TGraph (in such case, I as mentioned suggest to rename the function GetHistogram)?

Yes, to close the polygon, you should add the points yourself.