Exponential fit doesn't work


ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi Rooters!

Well, I’m working on TGraphError im going to show you bellow, but the exponential (gaussian fit) doesn’t work.

{
   c1 = new TCanvas("c1","Pure Z/ Cross section vs Energy  ",200,10,700,500);
   c1->SetFillColor(42);
   c1->SetGrid();
   c1->GetFrame()->SetFillColor(21);
   c1->GetFrame()->SetBorderSize(12);
   const Int_t n = 15;
   Double_t x[n]  = {20, 30, 34.4, 40, 50, 60, 70, 80, 85, 90, 91, 95, 100, 110, 120};
   Double_t y[n]  = {7.19E-02,1.85E-01,2.63E-01,4.00E-01,8.27E-01,1.78E+00,4.51E+00,1.77E+01,5.72E+01,7.38E+02,1.40E+03,2.93E+02,1.02E+02,3.78E+01,2.13E+01};
   Double_t ex[n] = {.0,.0,.0,.0,.0,.0,.0,.0,.0,.0,.0,.0,.0,.0,.0};
   Double_t ey[n] = {3.58E-05,9.20E-05,1.30E-04,1.98E-04,4.19E-04,9.06E-04,2.30E-03,9.35E-03,3.72E-02,3.98E-01,7.50E-01,1.34E-01,5.56E-02,2.07E-02,1.17E-02};
   gr = new TGraphErrors(n,x,y,ex,ey);
   gr->SetTitle("Cross Section (pb) vs Energy (GeV)");
   gr->SetMarkerColor(4);
   gr->SetMarkerStyle(21);
   gr->Draw("ALP");

    
   TF1 *f1 = new TF1("f1","pol1(0)",20,75);
   TF1 *f2 = new TF1("f2","[1400]*exp(-[1]*((x-[91])**2)/([2]*([0.5]**2)))",75,120); 
   f2->SetParameters(75,80,85,90,91,95,10,110,120);
   gr->Fit("f1","R"); 
   gr->Fit("f2","B");
  
 gr->Draw(); 
}

It runs the commands without any problem but the fitting won’t show in the graph. I’m new to all this thing so, I hope I’m not disregarding something important.

Thank you in adnance!

Try:

   TF1 *f2 = new TF1("f2", "[0]+[1]*x+[2]*exp(-[3]*(pow((x-[4]),2))/([5]*(pow([6],2))))", 10., 130.);
   f2->Print();
   f2->SetParameters(-0.3, 0.02, 1400., 0.1, 91., 1., 1.);
   f2->SetNpx(1000);
   gr->Fit("f2", "");

BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

Yes,I’m sorry. I’ll have it in mind.
It actually worked now!

Thanks a lot!!!

Please note that, in your “f2” function, parameters “[5]” and “[6]” are completely redundant / superfluous. They both should actually go into your parameter “[3]” (i.e. you should have just one parameter “[3]” = “[3]/([5]*(pow([6],2)))”).

Yes, right- put it this way to have it in the full gaussian form vissualy, but there’s no need, indeed.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.