# Fitline not showing

**URL:** <https://root-forum.cern.ch/t/fitline-not-showing/41312>\
**Category:** Newbie\
**Created:** [September 6, 2020, 8:03pm UTC](https://root-forum.cern.ch/t/fitline-not-showing/41312 "2020-09-06T20:03:53Z")\
**Posts on this page:** 6\
**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, 8:03pm UTC](https://root-forum.cern.ch/t/fitline-not-showing/41312/1 "2020-09-06T20:03:53Z")

</div>

I am trying to plot a linear fit through five points that have error bars. But it does not appear. 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(){
	
	// 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);
	}
	

   c1->Update();
   
	return 0;
}

```

---

<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:** [September 7, 2020, 7:26am UTC](https://root-forum.cern.ch/t/fitline-not-showing/41312/2 "2020-09-07T07:26:04Z")

</div>

We need the file `A1_line.dat` to run your macro.

---

<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 7, 2020, 12:43pm UTC](https://root-forum.cern.ch/t/fitline-not-showing/41312/3 "2020-09-07T12:43:43Z")

</div>

[A1\_line.cpp](https://root-forum.cern.ch/uploads/short-url/7RoeasfMmEaTb87rE4NcyitN4Kp.cpp) (40 Bytes)

Sorry about that, here you go. I converted it from `.dat` to `.cpp` so the forum can accept it.

---

<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:** [September 7, 2020, 1:31pm UTC](https://root-forum.cern.ch/t/fitline-not-showing/41312/4 "2020-09-07T13:31:55Z")

</div>

your points are not ordered along X

---

<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 7, 2020, 2:27pm UTC](https://root-forum.cern.ch/t/fitline-not-showing/41312/5 "2020-09-07T14:27:53Z")

</div>

What do you mean? The file is ordered x y dy.

---

<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:** [September 7, 2020, 2:34pm UTC](https://root-forum.cern.ch/t/fitline-not-showing/41312/6 "2020-09-07T14:34:18Z")

</div>

See what I get when I draw your graph with option “L”.

 ![Screenshot 2020-09-07 at 16.33.22](https://root-forum.cern.ch/uploads/default/original/3X/0/8/0853faaffa10587ce0eed5e5619850a6a18491dd.jpeg)
