Home | News | Documentation | Download

Spline "fitting"

Hi,

So I am having problems with TSpline fitting for some data sets. I wanted to try TSpline over normal fitting on some suggestions. The TSpline fits perfectly for small data sets close together but for this data set, it doesn’t. I have attached the picture below. The green line is TSpline.

. I have tried using data points as Knots. Here is the following code :

Double_t xx[12]={0};
Double_t yy[12]={0};

   for(int i=0;i<12;++i){
      xx[i]=numbers3[i];           // x data points
      yy[i]=numbers5[i];           // y data points
}

TSpline3 *spline3 = new TSpline3("Test",xx,yy,12,0,3000);
spline3->Draw("same");
 spline3->SetLineColor(kGreen);
 spline3->SetLineWidth(3);

Thank you for your answers/suggestions in advance.

Can you post a more complete macro ?
We cannot run what you sent.

Hi, here is a more complete set of code, please let me know if you need more.



   TGraphErrors *g = new TGraphErrors();
   for(int i=0;i<12;i++){                  
   g->SetPoint(i, numbers3[i],numbers5[i]);    // plot points
   g->SetPointError(i,0,numbers6[i]);          // plot errors 
   }

  
  g->Draw("AP");

  g->SetMarkerStyle(20);
  g->SetFillColor(6);
  g->SetFillStyle(3005);
  g->GetXaxis()->SetLimits(0,3100);
  g->GetYaxis()->SetLimits(-1,0.07195);

Double_t xx[12]={0};
Double_t yy[12]={0};

   for(int i=0;i<12;++i){
      xx[i]=numbers3[i];                // x data points 
      yy[i]=numbers5[i];              // y data points
}

TSpline3 *spline3 = new TSpline3("Test",xx,yy,12,0,3000);
spline3->Draw("same");
 spline3->SetLineColor(kGreen);
 spline3->SetLineWidth(3);

The numbers arrays are not defined.

Hi sorry the arrays are taken from the data file, I will post a snippet of data here I put in array.

double numbers3[12]={ 100, 1000, 1500,2000,200,20,2250,2500,2750,3000 ,40,500};
double numbers5[12]={ 0, 0.00080004, 0.00115, 0.0022, 5e-05, 5.00025e-05, 0.0023,0.004,0.005,0.0095,0,0.0004};
double numbers6[12]={ 0,0.00019993 ,0.000239654 ,0.000331297, 4.99987e-05,5.00012e-05 ,0.000479031, 0.000446318,0.000705337, 0.000685921,0 ,0.000141393};

Thank you for your help.

I think you should reorder your points along X in increasing order.
See what i get if I add the option L in the graph drawing:

{
   double numbers3[12]={ 100, 1000, 1500,2000,200,20,2250,2500,2750,3000 ,40,500};
   double numbers5[12]={ 0, 0.00080004, 0.00115, 0.0022, 5e-05, 5.00025e-05, 0.0023,0.004,0.005,0.0095,0,0.0004};
   double numbers6[12]={ 0,0.00019993 ,0.000239654 ,0.000331297, 4.99987e-05,5.00012e-05 ,0.000479031, 0.000446318,0.000705337, 0.000685921,0 ,0.000141393};

   Double_t xx[12]={0};
   Double_t yy[12]={0};

   TGraphErrors *g = new TGraphErrors();
   for(int i=0;i<12;i++){
      g->SetPoint(i, numbers3[i],numbers5[i]);    // plot points
      g->SetPointError(i,0,numbers6[i]);          // plot errors
   }


   g->Draw("ALP");

   g->SetMarkerStyle(20);
   g->SetFillColor(6);

   for(int i=0;i<12;++i){
      xx[i]=numbers3[i];                // x data points
      yy[i]=numbers5[i];              // y data points
   }

   TSpline3 *spline3 = new TSpline3("Test",xx,yy,12,0,3000);
   spline3->Draw("same");
   spline3->SetLineColor(kGreen);
   spline3->SetLineWidth(3);
}

Hello, it works pretty well. Another question, how would I extend the line to cross the last point and 1st point? I tried increasing the TSpline3 parameter to 3100 but it didn’t do anything. Also how would I get the fitting details such as FCN/ Chi.

TSpline3 *spline3 = new TSpline3("Test",xx,yy,12,0,3100);

You will need to add a point before the first point and one after the last point.

“Also how would I get the fitting details such as FCN/ Chi.”

There is a basic misunderstanding here:
A TSpline is *not * a fit but a smooth line through all data points,
so there no such things like Chi2.
You probably want something like a polynomial or an exponential.

Thank you all for your responses, It has greatly helped me.