TGraph Integral

I’ve recently discovered that the TGraph::Integral method does not return what I would consider an integral. After reading the documentation I find that instead it computes the area of a polygon defined by the points (the closure occurs between the first and last point). This could omit a lot of space under the curve (unless the first and last point end up at zero) and in the case of a line the integral computes to a value of zero!

I think it is very misleading to use the term integral and instead suggest that a proper trapezoidal integral function is implemented and the the current routine is renamed as PolyArea or something indicating that it is in fact not an Integral. I’m sure many users expect an Integral and do not dig into the documentation to find out that this function odes not provide that.

A script demonstrating the issue with polynomials:

$ root test.C
root [0]
Processing test.C...
Integral of x[0,10]: 50 ROOT TGraph: 0
Integral of x^2[0,10]: 333.333 ROOT TGraph: 165
Integral of x^3[0,10]: 2500 ROOT TGraph: 2475
   const int num = 11;
   double x[num] = {0,1,2,3,4,5,6,7,8,9,10};
   double p1[num];
   double p2[num];
   double p3[num];
   for (int i = 0; i < num; i++) {
      p1[i] = x[i];
      p2[i] = pow(x[i], 2);
      p3[i] = pow(x[i], 3);
   auto g1 = new TGraph(num, x, p1);
   auto g2 = new TGraph(num, x, p2);
   auto g3 = new TGraph(num, x, p3);
   auto mg = new TMultiGraph();
   std::cout << "Integral of x[0,10]: " << pow(10,2) / 2 << " ROOT TGraph: " << g1->Integral() << std::endl;
   std::cout << "Integral of x^2[0,10]: " << pow(10,3) / 3 << " ROOT TGraph: " << g2->Integral() << std::endl;
   std::cout << "Integral of x^3[0,10]: " << pow(10,4) / 4 << " ROOT TGraph: " << g3->Integral() << std::endl;
1 Like

If n is the number of points what a about adding a point being:

x[n] = x[n-1];
y[n] = y[0];

Yes, of course I can add points, but the message here is that TGraph::Integral is not a mathematical integral.

I think you made a mistake on your suggestion as well. To complete the shape to get an integral one would need to draw down from the last point to the horizontal axis. Assuming the graph has n points. You have the following:

x[n+1] = x[n]
y[n+1] = 0

Add additionally you need to make sure the first point is on the horizontal axis as well.

x[-1] = x[0]
y[-1] = 0

May be @moneta can comment on this also.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.