# 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:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![booghimen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/f07891/32.png) [@booghimen](https://root-forum.cern.ch/u/booghimen)\
**Post date:** [May 22, 2018, 7:15pm UTC](https://root-forum.cern.ch/t/sum-of-tgraph-integrals/29095/1 "2018-05-22T19:15:07Z")

</div>

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!! 🙂

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 23, 2018, 7:08am UTC](https://root-forum.cern.ch/t/sum-of-tgraph-integrals/29095/2 "2018-05-23T07:08:35Z")

</div>

You can compute the two [TGraph Integrals](https://root.cern/doc/master/classTGraph.html#a06b6dfc69d4fe9336ade71a47864f974) and sum them.

---

<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 23, 2018, 7:29am UTC](https://root-forum.cern.ch/t/sum-of-tgraph-integrals/29095/3 "2018-05-23T07:29:17Z")

</div>

> [@Numerical integration with ROOT. (Function defined in points)](https://root-forum.cern.ch/t/numerical-integration-with-root-function-defined-in-points/24651/3):
>
> Be aware that the [TGraph::Integral](https://root.cern/doc/master/classTGraph.html#a635997a6d81e4e89529aaf1819cf7533) name is extremely misleading. It calculates the “pseudo-integral” of a closed polygon, NOT the “integral” of the function. Try this: { TGraph \*g = new TGraph(); g-\>SetPoint(0, 1.0, 1.0); g-\>SetPoint(1, 2.0, 0.0); g-\>SetPoint(2, 3.0, 2.0); g-\>SetPoint(3, 4.0, 1.0); g-\>Draw("AL\*"); std::cout \<\< "ROOT's pseudo-integral = " \<\< g-\>Integral(0, 3) \<\< std::endl; }

> [@Plotting the differences between two graphs](https://root-forum.cern.ch/t/plotting-the-differences-between-two-graphs/29009/4):
>
> Note that the solution proposed by Olivier assumes that both graphs have exactly the same numbers of points and that their X coordinates are exactly the same (for all points). If any of the above conditions is not met, try: { // Note: this macro needs ROOT 6 // define some variables used below Double\_t x[100], y[100]; Int\_t n; // create the first graph n = 22; for (Int\_t i = 0; i \< n; i++) { x[i] = i \* TMath::Pi() / (n - 1.); y[i] = TMath::Sin(x[i]); } TGraph \*g…

---

<div class="post-metadata">

**Author:** ![booghimen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/f07891/32.png) [@booghimen](https://root-forum.cern.ch/u/booghimen)\
**Post date:** [May 24, 2018, 10:39pm UTC](https://root-forum.cern.ch/t/sum-of-tgraph-integrals/29095/4 "2018-05-24T22:39:24Z")

</div>

Dears couet and Willie\_E\_Coyote,

Thanks a lot for your reply!!  
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 🙂

---

<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 …

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [June 8, 2018, 5:52am UTC](https://root-forum.cern.ch/t/sum-of-tgraph-integrals/29095/6 "2018-06-08T05:52:27Z")

</div>

Closed on June 8, 2018, 5:52am UTC

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