# Fitting Exponential Function

Dear All

Could anybody please suggest me how to fit a distribution with some exponential function in root.
I am using this at the moment:

TF1 func = new TF1(“func”,"(x/[0] + [1](1- TMath::Exp(-x/[2]))**[3])", 0,5);
func->SetParNames(“r”,“A”,“B”,“C”);
func->SetParLimits(0,0.002,1.5);
func->SetParLimits(1,0.0000013,-0.10);
func->SetParLimits(2,2.5,-0.20);
func->SetParLimits(3,0.001,0.50);
h->Fit(func);

I tried with the various values of ParLimits, but did not got any solution.

Thanks
Shikshit

Use option “B” (boundary) when fitting. see documentation.

```h->Fit(func,"B"); ```
Rene

Thanks Brun

I did the way you suggested , now its fitting the distribution but despite changing the parlimits of different parameters , I am not able to do the appropriate fitting to the distribution.

Shikshit

You must call the func->SetParameters(,) with your best guess for the parameters values.

Rene

Dear Rene Brun

Thanks again for your reply. Before applying the changes you suggested, I too made some changes as per my needs. It is that, i am fitting the given distribution with this function, where “x” used in the “func” represent the given distribution, while “y” refers to some other distribution.
Now by doing so i not even getting any fit however wrong it may be.
The present fit function which i am using is as:

TF1 func = new TF1(“func”,"(y/[0] + [1](1- TMath::Exp(-x/[2]))**[3])", 0,6);
func->SetParameters(0.0005,4.5,10.06,20.0);
func->SetParNames(“r”,“A”,“B”,“C”);
func->SetParLimits(0,0.008,0.0015);
func->SetParLimits(1,-0.05,100.5);
func->SetParLimits(2,-0.5,19.80);
func->SetParLimits(3,-1.001,50.5);
h->Fit(func,“B”,"",0,6);

Regards
Shikshit

Hi ,

I don;t understand your function definition. You are creating a 1D object (TF1) but you use 2 observables (x and y). Do you need to fit a 1D or 1 2D object ?
Anyway, as mentioned before, you need to set good initial parameters to have the fit working or it could be that the function you are creating does not model well your data

Best Regards

Lorenzo

Hi Lorenzo
yes I am fitting a 1-D object and using two variables (x & y) where
x-> dca_distribution of proton to which i am fitting this function
y-> dca_distribution of antiproton which i am just using in this function-(func). I am just a beginer, and as per my understanding i think i am not understanding to use this variable in a proper way.

here is the whole code in which i am using this fit function.

[b]void dca_fit(){
gROOT->Reset();
gStyle->SetOptFit(1);
TFile *f1 = new TFile(“star_wth_cut_dca_pbarp.root”);

TH1F h = (TH1F)f1->Get(“hdca_protonP_5”);

TH1F h1 = (TH1F)f1->Get(“hdca_protonN_5”);

TCanvas *c1 = new TCanvas(“c1”, “”, 600, 600);

c1->SetBorderMode(1);
c1->SetFillColor(kWhite);
// gStyle->SetOptStat("");

TAxis *x = h->GetXaxis();
//x->SetTitle(“p(#bar{p}){DCA}");
x->SetTitle("p
{DCA}”);

x->SetTitleColor(kBlack);
TAxis *y = h->GetYaxis();
y->SetTitle(“Counts”);
//h->SetMinimum(0);
//f1.ls();
h->SetLineColor(2);
h->SetMarkerColor(2);
h->SetLineWidth(2);
h->SetMarkerStyle(29);
h->Draw("");

Float_t hdca_protonP_5=0;
Float_t hdca_protonN_5=0;
Float_t hdca_protonP_5=x;
Float_t hdca_protonN_5=y;

TF1 func = new TF1(“func”,"(y/[0] + [1](1- TMath::Exp(-x/[2]))**[3])", 0,6);
func->SetParameters(0.0005,4.5,10.06,20.0);
func->SetParNames(“r”,“A”,“B”,“C”);
func->SetParLimits(0,0.008,0.0015);
func->SetParLimits(1,-0.05,100.5);
func->SetParLimits(2,-0.5,19.80);
func->SetParLimits(3,-1.001,50.5);

h->Fit(func,“B”,"",0,4);

}
[/b]

Regards
Shikshit

HI,

you cannot fit a 1D histogram with a 2D function. Furthermore, you cannot create a TF1 using a formula expression depending on the varaibles “x” and “y”. You would need eventually to create a TF2 and fit a 2D histogram

Lorenzo

Hi Lorenzo

Well let me clarify certain things

1. I am not using 2-D function.
2. While using a fit function, (my approach could be totally wrong ) but what i am trying to derive is :
Fitting my given dca_distribution with the fit function “y/[0] + [1]*(1- TMath::Exp(-x/[2]))**[3])”, where, i am using two variables x & y.

Here first part of the fn: y/[0] : is my signal and i am using hdca_protonN_5 which itself is a histogram.
While the sec. part of the fn: [1]*(1- TMath::Exp(-x/[2]))**[3]) : is my background and for simplicity sake i am using the distribution hdca_protonP_5 to which i am applying a fit fn. also.

I think this information might help resolving some queries.
Thanks
With Regards
Shikshit