# Fitline not appearing in Root

**URL:** <https://root-forum.cern.ch/t/fitline-not-appearing-in-root/41307>\
**Category:** Newbie\
**Created:** [September 6, 2020, 1:24am UTC](https://root-forum.cern.ch/t/fitline-not-appearing-in-root/41307 "2020-09-06T01:24:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cepheides](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@Cepheides](https://root-forum.cern.ch/u/Cepheides)\
**Post date:** [September 6, 2020, 1:24am UTC](https://root-forum.cern.ch/t/fitline-not-appearing-in-root/41307/1 "2020-09-06T01:24:20Z")

</div>

I am trying to write a code that simply places a linear fit across five data points. Here is the input file I am reading from:

```auto
1 2 3
2 4 5
3 7 2
4 7 1

```

And here is my code:

```auto
# include <iostream>
# include <fstream>
# include <math.h>
# include <iomanip>
# include <cmath>
# include <stdlib.h>
# include <cstdlib>
//# include <fstream.h>
# include <string.h>
# include <string>
//# include <dos.h> //For Sleep() 

# include "TROOT.h"
# include "TFile.h"
# include "TTree.h"
# include "TBrowser.h"
# include "TH1.h"
# include "TH2.h"
# include "TH3.h"
# include "TRandom.h"

int main(){
	
	char inputFileName[50] = "A1_line.dat";
	Int_t NumOfLines = 4;
	Int_t nArray = NumOfLines + 1;
	bool WantToFit = true;
	
	char FitFunc[20] = "[0]*x + [1]";
	int xmin = 0;
	int xmax = 5;
	
// create the coordinate arrays
   Double_t x[nArray];
   Double_t y[nArray]; 
   
      // create the error arrays
   Double_t dx[5] = {0};
   Double_t dy[nArray];
	
	ifstream inFile;
	inFile.open(inputFileName,ios::in);
	
	if(inFile.is_open()){
		cout<<"Input File was opened successfully"<<endl;
	}
	
	//Main loop filling arrays from file//
	inFile>>x[0]>>y[0]>>dy[0];
	cout<<x[0]<<setw(20)<<y[0]<<setw(20)<<dy[0]<<endl;
	
	for(int i = 1; i < NumOfLines; i++){
		
		inFile>>x[i]>>y[i]>>dy[i];
		cout<<x[i]<<setw(20)<<y[i]<<setw(20)<<dy[i]<<endl;
		
	}
	
	inFile.close();
	
   TCanvas *c1 = new TCanvas("c1","A1",200,10,700,500);
   c1->SetGrid();
   

   // create the TGraphErrors and draw it
   TGraphErrors *gr = new TGraphErrors(nArray,x,y,dx,dy);
   
      	// Define Fit Function
	if (WantToFit){
		TF1 *f = new TF1("f", FitFunc, xmin, xmax);
		f->SetParNames("slope","offset");
		f->SetParameters(0.5,1);
			
		gr->Fit(f);
	}
	
   gr->SetTitle("A1;X;Y");
   gr->SetMarkerColor(4);
   gr->SetMarkerStyle(21);
   gr->Draw("AP");
   c1->Update();
   
	return 0;
}

```

Here is the output whenever I run my code

```auto
Input File was opened successfully
1 2 3
2 4 5
3 7 2
4 7 1
Warning in <TCanvas::Constructor>: Deleting canvas with same name: c1
 FCN=3.59539e+307 FROM HESSE STATUS=FAILED 11 CALLS 83 TOTAL
                     EDM=0 STRATEGY= 1 ERROR MATRIX UNCERTAINTY 100.0 per cent
  EXT PARAMETER APPROXIMATE STEP FIRST   
  NO. NAME VALUE ERROR SIZE DERIVATIVE 
   1 slope -1.50000e+06 1.41421e+00 0.00000e+00 0.00000e+00
   2 offset 1.00000e+00 1.41421e+00 0.00000e+00 0.00000e+00
(int) 0
code:

```

Yet, when the graph is up, there is no fitline at all. What am I missing?

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [September 6, 2020, 10:33am UTC](https://root-forum.cern.ch/t/fitline-not-appearing-in-root/41307/2 "2020-09-06T10:33:00Z")

</div>

Do the fit _after_ drawing the graph.

```auto
   TGraphErrors *gr = new TGraphErrors(nArray,x,y,dx,dy);
   gr->SetTitle("A1;X;Y");
   gr->SetMarkerColor(4);
   gr->SetMarkerStyle(21);
   gr->Draw("AP");
  	// Define Fit Function
	if (WantToFit){
		TF1 *f = new TF1("f", FitFunc, xmin, xmax);
		f->SetParNames("slope","offset");
		f->SetParameters(0.5,1);			
		gr->Fit(f);
	}

```

Or draw again the fitted function after drawing gr:

```auto
   gr->Draw("AP");
   f->Draw("same");

```

---

<div class="post-metadata">

**Author:** ![Cepheides](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@Cepheides](https://root-forum.cern.ch/u/Cepheides)\
**Post date:** [September 6, 2020, 4:36pm UTC](https://root-forum.cern.ch/t/fitline-not-appearing-in-root/41307/3 "2020-09-06T16:36:28Z")

</div>

Hi, so here is the change I made. Yet, I still not see a fitline on my plot

```auto
# include <iostream>
# include <fstream>
# include <math.h>
# include <iomanip>
# include <cmath>
# include <stdlib.h>
# include <cstdlib>
//# include <fstream.h>
# include <string.h>
# include <string>
//# include <dos.h> //For Sleep() 

# include "TROOT.h"
# include "TFile.h"
# include "TTree.h"
# include "TBrowser.h"
# include "TH1.h"
# include "TH2.h"
# include "TH3.h"
# include "TRandom.h"

int main(){
	
	// controls
	char inputFileName[50] = "A1_line.dat";
	Int_t NumOfLines = 4;
	Int_t nArray = NumOfLines + 1;
	bool WantToFit = true;
	
	char FitFunc[20] = "[0]*x + [1]";
	int xmin = 0;
	int xmax = 5;
	
   // create the coordinate arrays
   Double_t x[nArray];
   Double_t y[nArray]; 
   
    // create the error arrays
   Double_t dx[5] = {0};
   Double_t dy[nArray];
	
	ifstream inFile;
	inFile.open(inputFileName,ios::in);
	
	if(inFile.is_open()){
		cout<<"Input File was opened successfully"<<endl;
	}
	
	//Main loop filling arrays from file//
	inFile>>x[0]>>y[0]>>dy[0];
	cout<<x[0]<<setw(20)<<y[0]<<setw(20)<<dy[0]<<endl;
	
	for(int i = 1; i < NumOfLines; i++){
		
		inFile>>x[i]>>y[i]>>dy[i];
		cout<<x[i]<<setw(20)<<y[i]<<setw(20)<<dy[i]<<endl;
		
	}
	
	inFile.close();
	
	
	// create the canvas
   TCanvas *c1 = new TCanvas("c1","A1",200,10,700,500);
   c1->SetGrid();
   
   // create the TGraphErrors and draw it
   TGraphErrors *gr = new TGraphErrors(nArray,x,y,dx,dy);
   gr->SetTitle("A1;X;Y");
   gr->SetMarkerColor(4);
   gr->SetMarkerStyle(21);
   gr->Draw("AP");
   
      	// Define Fit Function
	if (WantToFit){
		TF1 *f = new TF1("f", FitFunc, xmin, xmax);
		f->SetParNames("slope","offset");
		f->SetParameters(0.5,1);
			
		gr->Fit(f);
	}
	
   gr->Draw("AP");
   f->Draw("same");
	

   c1->Update();
   
	return 0;
}

```
