# Residual plot fitting

**URL:** <https://root-forum.cern.ch/t/residual-plot-fitting/47721>\
**Category:** Newbie\
**Created:** [November 18, 2021, 12:28pm UTC](https://root-forum.cern.ch/t/residual-plot-fitting/47721 "2021-11-18T12:28:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saradindu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/saradindu/32/26687_2.png) [@Saradindu](https://root-forum.cern.ch/u/Saradindu)\
**Post date:** [November 18, 2021, 12:28pm UTC](https://root-forum.cern.ch/t/residual-plot-fitting/47721/1 "2021-11-18T12:28:11Z")

</div>

Hii ,  
I have a data set ( text file) . I plot a graph and fitted it using quadratic equation.  
I need to fit the residuals of fitting. I can do it for histograms but how do I do it for graph ?  
Can you privide some code ?

Dummy text file :

```auto
Energy Sigma Sigmaerror

439 1.4 .14
450 1.2 .04
583 1.4 .05
610 1.4 .04
1730 1.5 .09
1765 1.6 .04
2050 1.9 .13
2205 1.9 .08
2614 1.9 .05
2904 1.9 .07
4082 2.6 .17
5551 3.7 .19
6475 4.01 .27
6986 3.99 .24

```

I fitted this using a0+a1_x+a2_x\*\*2 where x is energy

Thanking you,

Saradindu

---

<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:** [November 18, 2021, 12:41pm UTC](https://root-forum.cern.ch/t/residual-plot-fitting/47721/2 "2021-11-18T12:41:16Z")

</div>

I guess @moneta can help.  
It would be nice to post the macro you have now.

---

<div class="post-metadata">

**Author:** ![Saradindu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/saradindu/32/26687_2.png) [@Saradindu](https://root-forum.cern.ch/u/Saradindu)\
**Post date:** [November 18, 2021, 12:44pm UTC](https://root-forum.cern.ch/t/residual-plot-fitting/47721/3 "2021-11-18T12:44:19Z")

</div>

Hello ,  
This is used to plot the data file . After that I just used fit function of root to fit the data points . I don’t have macro for plotting the residuals .

```auto
Int_t n=100;
Float_t x1,ex1,y1,ey1;
Int_t i=0;
Float_t x[100],ex[100],y[100],ey[100];
while(1){
        ncols=fscanf(fp,"%f %f %f",&x1,&y1,&ey1);
        if (ncols<0) break;
        x[i]=x1;
// ex[i]=ex1;
        y[i]=y1;
        ey[i]=ey1;
// cout<<x[i]<<"\t"<<ex[i]<<"\t"<<y[i]<<"\t"<<ey[i]<<endl;
        cout<<x1<<"\t"<<ex1<<"\t"<<y1<<"\t"<<ey1<<endl;
        i++;
        nlines++;
        }
cout<<"found"<<nlines<<endl;
fclose(fp);

TGraphErrors*gr=new TGraphErrors(nlines,x,y,ex,ey);
TCanvas*c1=new TCanvas("c1","graph",100,10,900,600);
c1->SetFillColor(0);

gr->SetMaximum(6);
gr->SetMinimum(0);

gr->SetMarkerStyle(21);
gr->SetLineColor(1);
gr->SetTitle(" Sigma vs Energy Graph");
gr->SetMarkerSize(1);
gr->GetXaxis()->CenterTitle();
gr->GetYaxis()->CenterTitle();
gr->GetYaxis()->SetTitle(" Sigma (keV)");
gr->GetXaxis()->SetTitle(" Energy (keV)");
gr->Draw("AP");
return c1;
}

```

Thanks  
Saradindu

---

<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:** [November 18, 2021, 3:13pm UTC](https://root-forum.cern.ch/t/residual-plot-fitting/47721/4 "2021-11-18T15:13:58Z")

</div>

Hi,  
If you have an histogram you can use TRatioPlot. For a Graph, you can early loop on the data points and compute the residuals of the fit and fill a new TGraphErrors.  
If you don’t know how to do it, I could make a small example for you

Cheers

Lorenzo

---

<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:** [November 18, 2021, 3:17pm UTC](https://root-forum.cern.ch/t/residual-plot-fitting/47721/5 "2021-11-18T15:17:04Z")

</div>

I think I have one ready:

```auto
{
  TRandom* rand = new TRandom;

  TGraphErrors* gra = new TGraphErrors;
  for(Int_t i = 1; i <= 10; i++){
    Double_t x = i*100;
    Double_t y = rand->Gaus(x, TMath::Sqrt(x));
    gra->SetPoint(i - 1, x, y);
    gra->SetPointError(i - 1, 0, TMath::Sqrt(y));
  } // i

  TCanvas* can = new TCanvas("can");
  can->SetBottomMargin(0.3);
  // gra->Draw("ape");
  TF1* f1 = new TF1("f1", "[0]*x");
  gra->Fit(f1);
  gra->Draw("ape");
  f1->Draw("l same");
  
  TPad* pad = new TPad("pad", "pad", 0., 0., 1., 1.);
  pad->SetTopMargin(0.7);
  pad->Draw();
  pad->SetFillStyle(0);
  pad->cd();

  TGraphErrors* residual = new TGraphErrors;
  for(Int_t i = 1; i <= 10; i++){
    Double_t x = i*100;
    Double_t y0 = f1->Eval(x);
    Double_t y = (gra->GetY()[i - 1] - y0)/TMath::Sqrt(y0);
    Double_t ey = gra->GetErrorY(i - 1)/TMath::Sqrt(y0);
    residual->SetPoint(i - 1, x, y);
    residual->SetPointError(i - 1, 0, ey);
  } // i
  
  residual->GetHistogram()->SetMaximum(5);
  residual->GetHistogram()->SetMinimum(-5);
  residual->Draw("ape");
}

```

 ![Screenshot 2021-11-18 at 16.16.39](https://root-forum.cern.ch/uploads/default/original/3X/1/7/17a0d69222506435fac8444c3125ef93e34d7f08.png)

---

<div class="post-metadata">

**Author:** ![Saradindu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/saradindu/32/26687_2.png) [@Saradindu](https://root-forum.cern.ch/u/Saradindu)\
**Post date:** [November 21, 2021, 9:25am UTC](https://root-forum.cern.ch/t/residual-plot-fitting/47721/6 "2021-11-21T09:25:49Z")

</div>

Thank you very much . I am able to do it for histogram .  
But I could not do it for Graph .

If you put one example using graph it will be very much helpful .

Regards  
Saradindu

---

<div class="post-metadata">

**Author:** ![Saradindu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/saradindu/32/26687_2.png) [@Saradindu](https://root-forum.cern.ch/u/Saradindu)\
**Post date:** [November 21, 2021, 9:26am UTC](https://root-forum.cern.ch/t/residual-plot-fitting/47721/7 "2021-11-21T09:26:30Z")

</div>

Thank you very much .  
I am trying with this and get back to you.

regards  
Saradindu
