# Reduce TGraph point

**URL:** <https://root-forum.cern.ch/t/reduce-tgraph-point/43371>\
**Category:** Newbie\
**Created:** [February 2, 2021, 11:32pm UTC](https://root-forum.cern.ch/t/reduce-tgraph-point/43371 "2021-02-02T23:32:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ROOTer1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/977dab/32.png) [@ROOTer1](https://root-forum.cern.ch/u/ROOTer1)\
**Post date:** [February 2, 2021, 11:32pm UTC](https://root-forum.cern.ch/t/reduce-tgraph-point/43371/1 "2021-02-02T23:32:44Z")

</div>

Dear, is there a good way to reduce a TGraph having too many points without loosing the interpolation? or is there a smoothing function?

---

<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:** [February 3, 2021, 8:12am UTC](https://root-forum.cern.ch/t/reduce-tgraph-point/43371/2 "2021-02-03T08:12:21Z")

</div>

> [@ROOTer1](#):
>
> is there a good way to reduce a TGraph

You can use [RemovePoint](https://root.cern/doc/master/classTGraph.html#a740cc2dd6bb8dd99612eaa41ff10ba99).

---

<div class="post-metadata">

**Author:** ![ROOTer1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/977dab/32.png) [@ROOTer1](https://root-forum.cern.ch/u/ROOTer1)\
**Post date:** [February 3, 2021, 6:23pm UTC](https://root-forum.cern.ch/t/reduce-tgraph-point/43371/3 "2021-02-03T18:23:12Z")

</div>

Hi Olivier, thanks a lot for the suggestion, but I’m looking for a way to remove the points in a way that I don’t loose the shape too much. is there a smoothing function to do that?

---

<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:** [February 4, 2021, 7:27am UTC](https://root-forum.cern.ch/t/reduce-tgraph-point/43371/4 "2021-02-04T07:27:17Z")

</div>

Some automatic tool ? I am not aware of a such thing.  
A smoothing function will smooth the existing points but will not  
compare the smoothing with a previous one to see if the shape was kept.  
I guess you will have to create you own program to do that.  
May be @moneta has more ideas about it.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [February 4, 2021, 8:07am UTC](https://root-forum.cern.ch/t/reduce-tgraph-point/43371/5 "2021-02-04T08:07:56Z")

</div>

Hi,  
It is not clear to me what is your goal ? Why reducing the points ? The method to be used will depend on what you need to do. For example, if you are just interest in the shape, you can fit the Graph points and then just store the function parameters describing the shape.

Lorenzo

---

<div class="post-metadata">

**Author:** ![ROOTer1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/977dab/32.png) [@ROOTer1](https://root-forum.cern.ch/u/ROOTer1)\
**Post date:** [February 4, 2021, 9:34am UTC](https://root-forum.cern.ch/t/reduce-tgraph-point/43371/6 "2021-02-04T09:34:16Z")

</div>

Hi Lorenzo, I want to reduce the size of the rootfile, fitting doesn’t always work unfortunately. I was thinking if could use splines to smooth it using less points, is there any recommendation?

---

<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:** [February 4, 2021, 9:49am UTC](https://root-forum.cern.ch/t/reduce-tgraph-point/43371/7 "2021-02-04T09:49:57Z")

</div>

Just to have an idea: how many points have your “big” `TGraph`s ? and ideally how many should they have ?

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [February 4, 2021, 10:06am UTC](https://root-forum.cern.ch/t/reduce-tgraph-point/43371/8 "2021-02-04T10:06:22Z")

</div>

It depend again on how much you want to reduce the TGraph and which resolution you want to have.  
A very simple thing to do if you have a lot of points, is you make a TF1 with nix grid points and you save the TF1 object, if the number of Graph points is much larger than npx.

Lorenzo

---

<div class="post-metadata">

**Author:** ![ROOTer1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/977dab/32.png) [@ROOTer1](https://root-forum.cern.ch/u/ROOTer1)\
**Post date:** [February 4, 2021, 10:18am UTC](https://root-forum.cern.ch/t/reduce-tgraph-point/43371/9 "2021-02-04T10:18:23Z")

</div>

Basically I have too many TGraphs in one file, and the number of points is different in each of them, however you can clearly see that the density of points is too high in the defined range of each of these

Thanks a lot for the suggestion, but I’m not sure I understand how to make TF1 with nix grid points, is there an example somewhere? Many thanks!

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [February 4, 2021, 10:52am UTC](https://root-forum.cern.ch/t/reduce-tgraph-point/43371/10 "2021-02-04T10:52:22Z")

</div>

Here is an example:

```auto
// g is a pointer to a Graph object
TF1 * f1 = new TF1("f1Name",[&](double *x, double *){ return g->Eval(x[0]); },xmin, xmax,0);
f1->SetNpx(npx); // set number of points to sample the Graph
f1->Write(); 

```

---

<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:** [February 4, 2021, 1:26pm UTC](https://root-forum.cern.ch/t/reduce-tgraph-point/43371/11 "2021-02-04T13:26:14Z")

</div>

or:

```auto
{
   TCanvas *c = new TCanvas("c","c",700,500);
   int i;
   double x, y;

   // Create a graph with 10000 points
   TGraph *gr1 = new TGraph();
   gr1->SetMarkerColor(kYellow);
   for ( i=0; i<10000; i++) {
      x = i*0.001;
      y = 10*sin(x+0.2);
      gr1->AddPoint(x,y);
   }
   gr1->Draw("A*");

   // make a graph with only 10 points from the previous one.
   TGraph *gr2 = new TGraph();
   gr2->SetMarkerColor(kRed);
   for ( i=0; i<10; i++) {
      x = i*1.;
      y = gr1->Eval(x);
      gr2->AddPoint(x,y);
   }
   gr2->Draw("*");
}

```

 ![Screenshot 2021-02-04 at 14.25.51](https://root-forum.cern.ch/uploads/default/original/3X/c/0/c09ffc3c8f9d3cc62e8ccd628ab0f268563e5e98.jpeg)
