# Spline "fitting"

**URL:** <https://root-forum.cern.ch/t/spline-fitting/40127>\
**Category:** Newbie\
**Created:** [June 29, 2020, 1:29am UTC](https://root-forum.cern.ch/t/spline-fitting/40127 "2020-06-29T01:29:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhinab\_Mukhopadhyay](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/abhinab_mukhopadhyay/32/20444_2.png) [@Abhinab\_Mukhopadhyay](https://root-forum.cern.ch/u/Abhinab_Mukhopadhyay)\
**Post date:** [June 29, 2020, 1:29am UTC](https://root-forum.cern.ch/t/spline-fitting/40127/1 "2020-06-29T01:29:27Z")

</div>

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.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/2/8/28fca316ae993d8bf1c2d5548179d3d6bda5ab03.png). I have tried using data points as Knots. Here is the following code :

```auto
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.

---

<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:** [June 29, 2020, 6:54am UTC](https://root-forum.cern.ch/t/spline-fitting/40127/2 "2020-06-29T06:54:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Abhinab\_Mukhopadhyay](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/abhinab_mukhopadhyay/32/20444_2.png) [@Abhinab\_Mukhopadhyay](https://root-forum.cern.ch/u/Abhinab_Mukhopadhyay)\
**Post date:** [June 29, 2020, 7:12am UTC](https://root-forum.cern.ch/t/spline-fitting/40127/3 "2020-06-29T07:12:26Z")

</div>

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

```auto

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

```

---

<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:** [June 29, 2020, 7:14am UTC](https://root-forum.cern.ch/t/spline-fitting/40127/4 "2020-06-29T07:14:38Z")

</div>

The `numbers` arrays are not defined.

---

<div class="post-metadata">

**Author:** ![Abhinab\_Mukhopadhyay](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/abhinab_mukhopadhyay/32/20444_2.png) [@Abhinab\_Mukhopadhyay](https://root-forum.cern.ch/u/Abhinab_Mukhopadhyay)\
**Post date:** [June 29, 2020, 7:25am UTC](https://root-forum.cern.ch/t/spline-fitting/40127/5 "2020-06-29T07:25:29Z")

</div>

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

```auto
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.

---

<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:** [June 29, 2020, 7:53am UTC](https://root-forum.cern.ch/t/spline-fitting/40127/6 "2020-06-29T07:53:54Z")

</div>

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:

```auto
{
   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);
}

```

 ![Screenshot 2020-06-29 at 09.51.49](https://root-forum.cern.ch/uploads/default/original/3X/7/8/7849593317ea3482257fc2615f93e82116128810.jpeg)

---

<div class="post-metadata">

**Author:** ![Abhinab\_Mukhopadhyay](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/abhinab_mukhopadhyay/32/20444_2.png) [@Abhinab\_Mukhopadhyay](https://root-forum.cern.ch/u/Abhinab_Mukhopadhyay)\
**Post date:** [June 29, 2020, 4:23pm UTC](https://root-forum.cern.ch/t/spline-fitting/40127/7 "2020-06-29T16:23:07Z")

</div>

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);`

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/e/3/e3894416efa43803ea39204b1bed3add48937ef4.png)

---

<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:** [June 30, 2020, 6:07am UTC](https://root-forum.cern.ch/t/spline-fitting/40127/8 "2020-06-30T06:07:49Z")

</div>

> [@Abhinab\_Mukhopadhyay](#):
>
> how would I extend the line to cross the last point and 1st point?

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

---

<div class="post-metadata">

**Author:** ![OSchaile](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/o/e495f1/32.png) [@OSchaile](https://root-forum.cern.ch/u/OSchaile)\
**Post date:** [June 30, 2020, 7:49am UTC](https://root-forum.cern.ch/t/spline-fitting/40127/9 "2020-06-30T07:49:14Z")

</div>

“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.

---

<div class="post-metadata">

**Author:** ![Abhinab\_Mukhopadhyay](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/abhinab_mukhopadhyay/32/20444_2.png) [@Abhinab\_Mukhopadhyay](https://root-forum.cern.ch/u/Abhinab_Mukhopadhyay)\
**Post date:** [June 30, 2020, 4:45pm UTC](https://root-forum.cern.ch/t/spline-fitting/40127/10 "2020-06-30T16:45:23Z")

</div>

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