Re: [ROOT] Functions and fits

From: Rene Brun (Rene.Brun@cern.ch)
Date: Thu Apr 20 2000 - 18:28:18 MEST


Hi Yannick,
You do not need to define a TF1 like:  
  TF1 *f1= new TF1("f1","gaus",xmin,xmax);
when the function is directly one of the basic functions. You can do
directly:
    t->Fit("gaus")

When you call TH1::Fit, this will copy the fitted function object
in the list of functions attached to the histogram.
This is to permit fitting many different histograms in a loop with the
same function prototype.
You can access this function via
   TF1 *f = (TF1*)t->GetFunction("f1")
  and then access the function parameters via f->GetParameters, etc

When you archive the histogram to a file, the fitted function is also
archived with the histogram.

When you fit an histogram with a function f, by default the range
is the histogram range. If you specify the option "R", then you restrict
the fit to the range specified when creating the TF1 object f.

Rene Brun


On Thu,
20 Apr 2000, Patois Yannick wrote:

> Hello rooters,
> 
> I probably didnt fully understood how one declare functions and fit with
> it.
> 
> Having to fit several gaussian spectra, I defined the following
> function (simplified) :
> 
> void fit_on_mean(TH1 *t,int half_width,int right_shift=0)
> {
>   int binm=t->FindBin(t->GetMean());
>   double xmin =
> t->GetXaxis()->GetBinCenter(binm-half_width+right_shift);
>   double xmax =
> t->GetXaxis()->GetBinCenter(binm+half_width+right_shift);
> 
>   TF1 *f1= new TF1("f1","gaus",xmin,xmax);
>   t->Fit("f1","RQ");
> }
> 
> There's a thing I dont fully understand : why does is (seems to) work ?
> The appropriate "f1" function 'sticks' to the histograms, even when f1
> got redefined.
> 
> And maybe it is not really working and this explain why I got some
> strange beahvior from time to time.
> 
> Then I wanted to display the function outside of it's borders even if
> the fit itself was restricted and I tried that :
> 
> void fit_on_mean(TH1 *t,int half_width,int right_shift=0)
> {
>   int binm=t->FindBin(t->GetMean());
>   double xmin =
> t->GetXaxis()->GetBinCenter(binm-half_width+right_shift);
>   double xmax =
> t->GetXaxis()->GetBinCenter(binm+half_width+right_shift);
>   
>   TF1 *f1= new TF1("f1","gaus",xmin,xmax);
>   t->Fit("f1","RQ");
>   TF1 *f2= new TF1("f2","gaus");
>   for (int i=0;i<3;i++) f2->SetParameter(i,f1->GetParameter(i));
>   f2->SetLineStyle(2);
>   f2->Draw("same");
>   f1->Draw("same");
> }
> 
> And it didnt worked at all !
> 
> What I seems to understand is that writing t->Fit("f1") somehow 'attach'
> f1 to t, but i'm not sure if I can completly dispose f1 after (by
> redefining it like I do when calling several time my function), and how
> I can 'attach' a function to an histogram without making any fit (in my
> case I only want a dashed curve that complete the displaying of the
> fitted function).
> 
> 
> 	Yannick
> 
>  _/ Yannick Patois _________________ Address (home) __________________
> | irc(undernet): Garp on #france25+ | La Villa des Sciences            |
> | email : patois@ganil.fr           | 12, avenue de Cambridge          |
> | web :http://www.sura.org/~patois/ | 14200 Herouville-Saint-Clair     |
> | Tel/Fax-home:+33 (0)2 31 94 50 32 | FRANCE                           |
> |___________________________________|__________________________________|
> 
> 



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