# Sum of TGraph integrals

Dears rooters,

I have 2 TGraph that represent the fluxes. I would like to have the total flux doing the sum of the integral of the 2 curves… is it possbile? how can i do it?

thanks a lot in advance!! You can compute the two TGraph Integrals and sum them.

Dears couet and Willie_E_Coyote,

Actually in fact the 2 TGraph don’t have the same number of points, or same x.
I understood that TGraph::Integral is a pseudo-integral between a close polygon, so for that I can add a points in order to do it. But is not clear how can I sum the 2 integrals.
At the end I would like to obtain a function coming from the sum of the intrgral, in order to know the y values at specific x values.

thanks a lot in advance Try to play with:

``````{
// create a test graph
TGraph *g = new TGraph(100);
for (Int_t i = 0; i < g->GetN(); i++) g->SetPoint(i, 0.1 * i, TMath::Sin(0.1 * i));
// in ROOT 6, you can easily make a TF1 from the TGraph::Eval function
// g->Sort(); // sort points along x axis (points must be in increasing order)
TF1 *f = new TF1("f",
[&](double*x, double *p){ return (((x >= g->GetX()) && (x <= g->GetX()[(g->GetN() - 1)])) ? g->Eval(x) : 0.0); },
g->GetX(), g->GetX()[(g->GetN() - 1)], 0);
f->SetNpx(3 * g->GetN());
g->Draw("A*");
std::cout << "pseudo-integral = " << g->Integral() << std::endl;
f->Draw("SAME");
std::cout << "integral = " << f->Integral(f->GetXmin(), f->GetXmax()) << std::endl;
}
``````

BTW. Depending on the “shapes” of your graphs, you may face problems with ROOT’s “integrators”:

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