# Sum of TGraph integrals

**URL:** <https://root-forum.cern.ch/t/sum-of-tgraph-integrals/29095>\
**Category:** ROOT\
**Created:** [May 22, 2018, 7:15pm UTC](https://root-forum.cern.ch/t/sum-of-tgraph-integrals/29095 "2018-05-22T19:15:06Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 25, 2018, 5:50am UTC](https://root-forum.cern.ch/t/sum-of-tgraph-integrals/29095/5 "2018-05-25T05:50:27Z")

</div>

Try to play with:

```auto
{
  // 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[0] >= g->GetX()[0]) && (x[0] <= g->GetX()[(g->GetN() - 1)])) ? g->Eval(x[0]) : 0.0); },
                   g->GetX()[0], 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”:

> [@Wrong convolution of 2 functions](https://root-forum.cern.ch/t/wrong-convolution-of-2-functions/26205/3):
>
> I blame the ROOT’s “Integral” method, so try with FWHM=10 …

---

_[View the full topic](https://root-forum.cern.ch/t/sum-of-tgraph-integrals/29095)._
