Reduce TGraph point

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

You can use RemovePoint.

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?

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.

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

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?

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

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

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!

Here is an example:

// 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(); 

or:

{
   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("*");
}