Home | News | Documentation | Download

Exponential fit not showing

I am trying to apply an exponential fit to a series of points that display exponential decay:

2	95
4	75
6	76
8	64
10	74
12	66
14	43
16	38

Here is my code:

// Plot C1_exppick.dat

# 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] = "C1_exppick.dat";
	Int_t NumOfLines = 50;
	Int_t nArray = NumOfLines + 1;
	bool WantToFit = true; // switch for fitting
	
	char GraphTitle[10] = "C1";
	char GraphXLabel[10] = "X";
	char GraphYLabel[10] = "Y";
	
	char FitFunc[30] =  "expo"; // "[0] * exp ( t * [1] )";
	char DrawOption[10] = "AP" ; // see https://root.cern.ch/doc/master/classTGraphPainter.html
	int xmin = 0;
	int xmax = 100;
	
   // 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];
	cout<<x[0]<<setw(20)<<y[0]<<endl;
	
	for(int i = 0; i < NumOfLines; i++){
		
		inFile>>x[i]>>y[i];
		//cout<<x[i]<<setw(20)<<y[i]<<endl;
		
	}
	
	inFile.close();
	
	cout << "Creating canvas" << endl;
	// create the canvas
   TCanvas *c1 = new TCanvas("c1",GraphTitle,200,10,700,500);
   c1->SetGrid();
   
   // create the TGraphErrors and draw it
   TGraphErrors *gr = new TGraphErrors(nArray,x,y);
   // change title and axes titles here
   gr->SetTitle("C1;X;Y");
   gr->SetMarkerColor(4);
   gr->SetMarkerStyle(21);
   gr->Draw(DrawOption); 
   
      	// Define Fit Function
	if (WantToFit){
		TF1 *f = new TF1("f", FitFunc, xmin, xmax);
		//f->SetParNames("A0","A1","A2");
		f->SetParameters(1,1);
			
		gr->Fit(f);
		cout << "Fitting" << endl;
	}
	

   c1->Update();
   
	return 0;
}

Yet, when I run this, nothing appears:C1.pdf (14.0 KB)

I think @moneta can help you.

Hi,

Your fit seems to fail and the function is drawn at around zero values. You should set better initial values for the parameters, something like

f->SetParameters(4,-0.02);

Lorenzo

Hi, unfortunately changing the parameters has not resolved the issue.

I have attach the input file so the problem can be reproduced. C1_exppick.cpp (272 Bytes) C1.pdf (13.9 KB)

Hi,

You need to add also the optionB when fitting. This because expo is a pre-defined function that has the initial parameter automatically guessed when fitting. Unfortunately this automatic goes does not work in your case, probably because the data are not very good and the fit fails.
To set user values of parameters you need to provide the B option when fitting.
So do:

f->SetParameters(4,-0.02);
gr->Fit(f,"B");

The fit is not so good, but that depends on the data that have a large vertical spread

Lorenzo