Re: Numerical precision in fitting TGraph

From: Rene Brun (Rene.Brun@cern.ch)
Date: Wed Feb 23 2000 - 11:32:30 MET


Hi Marco,
Thanks for reporting this problem with TGraph::Fit when one fits
with a polynomial. I have found the origin of the problem
and fixed it in TGraph::LeastSquareFit. The last data point was ignored
by this function computing the initial value of parameters.
In your example, you can circumvent the problem by calling
  gr->Fit("pol3","w")

Rene Brun

Marco van Leeuwen wrote:
> 
> Hi All,
> 
> In the past few months, I twice stumbled across the same problem, which
> is probably due to some numerical imprecisions. The problem probably
> only pops up in some special cases, but it can be quite nagging, so I
> thought it is worthwhile to report.
> 
> In some cases, when one tries to fit the same data in a TGraph and a
> histogram, the TGraph fails and the histogram doesn't. This mainly
> occurs when the data are in some small range, not centered at zero (e.g.
> 1.7 to 1.9). It probably has to do with the initialization routines for
> standard functions (pol, gaus, expo). I can think of a couple of reasons
> why this should be so, and I even tried to investigate in the code, to
> find out, but I soon got lost. So maybe somebody who is more familiar
> with the code has an idea?? Strangely enough, it seems to me that the
> routines are largely identical, and moreover, everywhere double
> precision is used, but still there is this difference...
> 
> Included is a macro which reproduces this behaviour for the pol3 case.
> It initialises a TH1D and two TGraphErrors with the same data, but one
> of the TGraphErrors has a shifted x-axis. Try it and see for yourself...
> (Root v2.23/12 on Redhat)
> 
> By the way, this also triggers the question: Is there a way to use e.g.
> a "pol3" in a fit without having ROOT initialise the function itself??
> As far as I can see, ROOT always initialises the standardfunctions if
> you try to fit them to a TGraph or TH1...
> 
> Kind regards,
> 
> Marco van Leeuwen.
> 
> ----- fit_test.C
> 
> {{
>   gROOT->Reset();
>   TCanvas *c1=new TCanvas("c1","Canvas #1",500,500);
>   TCanvas *c2=new TCanvas("c2","Canvas #2",500,600);
>   c2->Divide(1,2,0,0,0);
>   TF1 *f1=new TF1("f1","pol3",1.75,1.96);
>   f1->SetParameter(0,1.30015e+07);
>   f1->SetParameter(1,-2.10148e+07);
>   f1->SetParameter(2,1.13154e+07);
>   f1->SetParameter(3,-2.02969e+06);
>   TH1D *h1 = new TH1D("h1","test histo",1000,0,2);
>   TGraphErrors *gr = new TGraphErrors(105);
>   TGraphErrors *gr2 = new TGraphErrors(105);
>   for (Int_t i=875;i<980;i++){
>     Float_t x=h1->GetBinCenter(i);
>     h1->SetBinContent(i,f1->Eval(x)+gRandom->Gaus(0,400));
>     h1->SetBinError(i,400);
>     gr->SetPoint(i-875,x,h1->GetBinContent(i));
>     gr->SetPointError(i-875,0,h1->GetBinError(i));
>     gr2->SetPoint(i-875,x-1.85,h1->GetBinContent(i));
>     gr2->SetPointError(i-875,0,h1->GetBinError(i));
>   }
>   h1->GetXaxis()->SetRange(875,980);
>   c1->cd();
>   h1->Draw();
>   h1->Fit("pol3");
>   c2->cd(1);
>   gPad->DrawFrame(1.74,-1200,1.96,1200);
>   gr->Draw("p");
>   gr->Fit("pol3");
>   c2->cd(2);
>   gPad->DrawFrame(-0.11,-1200,0.11,1200);
>   gr2->Draw("p");
>   gr2->Fit("pol3");
> }}



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:19 MET