# Help with linear fit

**URL:** <https://root-forum.cern.ch/t/help-with-linear-fit/59263>\
**Category:** Newbie\
**Tags:** root\
**Created:** [May 7, 2024, 6:07pm UTC](https://root-forum.cern.ch/t/help-with-linear-fit/59263 "2024-05-07T18:07:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![liz](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/liz/32/36077_2.png) [@liz](https://root-forum.cern.ch/u/liz)\
**Post date:** [May 7, 2024, 6:07pm UTC](https://root-forum.cern.ch/t/help-with-linear-fit/59263/1 "2024-05-07T18:07:49Z")

</div>

Hello, I need help with a very simple problem: a linear fit. I don’t understand what is wrong with this code… The data shows up on the graph but not the fit…

```auto
#include "TCanvas.h"
#include "TFile.h"
#include "TH1.h"
#include "TF1.h"
#include "TAxis.h"
#include "TStyle.h"
#include <cmath>
#include <string>
#include <iostream>
using namespace std;

void linearfitcal(){
    gStyle->SetOptFit(1111);
    TCanvas *canvas=new TCanvas("canvas", "canvas", 800, 800);

    double x[3] = {196.45, 251,300};
    double ex[3]={0.198,0.210,0.154};
    double y[3]={0.02860271,0.0286013,0.02860063};
    double ey[3]={0};

    TGraphErrors *gr = new TGraphErrors(3,x,y,ex,ey);
    gr->SetMarkerStyle(24);
    gr->SetMarkerSize(0.5); 
    gr->Draw("AP");
   
    TF1 *fitFunc = new TF1("fitFunc", "pol1");
    //TF1 *fitFunc = new TF1("fitFunc", "expo");
    fitFunc->SetParName(0, "Intercept");
    fitFunc->SetParName(1, "Slope");
    //fitFunc->SetParameter(0,-8);
    //fitFunc->SetParameter(1,0.1);

    gr->Fit("fitFunc", "R");
    
    
    gr->SetTitle("Calibrazione;#Deltax [nm];#Delta#lambda_{r.u.}");

    TCanvas *canvas1=new TCanvas("canvas1", "canvas", 800, 800);
    TGraphErrors *residual= new TGraphErrors;

   
    for (int i=0; i<3; i++){
        residual->SetPoint(i , x[i], y[i]-fitFunc->Eval(x[i]));
        Double_t eyr = gr->GetErrorY(i);
        residual->SetPointError(i, 0, eyr);

    }

    residual->SetTitle("Residuals;#Deltax [nm];data_{meas} - data_{fit}");
    residual->SetMarkerStyle(21);
    residual->Draw("ape");

    // Determine the range of the residual plot
    double xmin = residual->GetXaxis()->GetXmin();
    double xmax = residual->GetXaxis()->GetXmax();

    // Draw a dotted horizontal line at y=0 within the range of the residual plot
    TLine *line = new TLine(xmin, 0, xmax, 0);
    line->SetLineStyle(2); // Dotted line style
    line->Draw();

    return;
}

```

---

<div class="post-metadata">

**Author:** ![sfpate](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@sfpate](https://root-forum.cern.ch/u/sfpate)\
**Post date:** [May 7, 2024, 6:50pm UTC](https://root-forum.cern.ch/t/help-with-linear-fit/59263/2 "2024-05-07T18:50:45Z")

</div>

Probably you should Fit before you Draw. ROOT does not automatically update the drawing just because you did a fit.

---

<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:** [May 9, 2024, 5:43pm UTC](https://root-forum.cern.ch/t/help-with-linear-fit/59263/3 "2024-05-09T17:43:21Z")

</div>

Hi,

Your fit does not work, first of all you are using option “R”, which should be removed in your case. Then fitting a TGraphErrors, the fit is not linear and you need to provide initial parameter values.  
I would suggest you first to fit ignoring the x error (in this case the fit is linear) and then fitting including the x errors. This can be done by doing:

```auto
gr->Fit("fitFunc", "EX0");
gr->Fit("fitFunc", "");

```

However, the final fit seems not to work well, probably because the given errors do not make much sense.

@sfpate, ROOT by default redraws your histogram (or graph) with the fitted function after doing a fit.

Lorenzo

---

<div class="post-metadata">

**Author:** ![liz](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/liz/32/36077_2.png) [@liz](https://root-forum.cern.ch/u/liz)\
**Post date:** [May 14, 2024, 7:06am UTC](https://root-forum.cern.ch/t/help-with-linear-fit/59263/4 "2024-05-14T07:06:55Z")

</div>

Thank you for the suggestion! Yes, the data is a bit problematic so I don’t think there’s much to do… but still thanks a lot! 🙂
