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);
g2->SetLineColor(kBlue);
g3->SetLineColor(kRed);
auto mg = new TMultiGraph();
mg->Add(g1);
mg->Add(g2);
mg->Add(g3);
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;
}