Re: big formulas with TFormula

From: Rene Brun (Rene.Brun@cern.ch)
Date: Mon Feb 09 1998 - 09:11:48 MET


Pascal Perrodo wrote:
> 
> Hi,
> 
> I use the class TFormula to define a function that I fit afterwards
> on a graph. OK.
> 
> Now my formula becomes big not with many parameters but just long and
> complicated on the mathematical side : many parenthetis, many
> fractions,...
> 
> Is there a way to simplify the definition of such a class TFormula ? I
> tried to do it using smaller TFormula classes and try to build the one I
> am interested, but it doesn't treat the parameters [0],[1],... in a
> consistent way for all formulas. How can I proceed ?

The TF1 class (derived from TFormula) supports 3 kinds of functions:
  1- simple formula that can be expressed in one line. The formula
     can refer to other formula with parameters. An example is given
     in the tutorial "fillrandom".
  2- Interpreted C++ functions as illustrated in the macro
myfit.C            below.This is the most convenient way for typical
minimization
     problems. The function may be multi-dimensional with
     many parameters
  3- Compiled C++ functions. Note that case2 can call a function
     of case3.

Rene Brun

//-----------------macro myfit.C--------------------------------
// This macro gets in memory an histogram from a root file
// and fits a user defined function.
// Note that a user defined function must always be defined
// as in this example:
//  - first parameter: array of variables (in this example only
1-dimension)
//  - second parameter: array of parameters
// Note also that in case of user defined functions, one must set
// an initial value for each parameter.

Double_t fitf(Double_t *x, Double_t *par)
{
   Double_t arg = 0;
   if (par[2]) arg = (x[0] - par[1])/par[2];

   Double_t fitval = par[0]*TMath::Exp(-0.5*arg*arg);
   return fitval;
}
void myfit()
{
   TFile *f = new TFile("hsimple.root");
   
   TCanvas *c1 = new TCanvas("c1","the fit canvas",500,400);

   TH1F *hpx = (TH1F*)f->Get("hpx");

// Creates a Root function based on function fitf above
   TF1 *func = new TF1("fitf",fitf,-2,2,3);

// Sets initial values and parameter names
   func->SetParameters(100,0,1);
   func->SetParNames("Constant","Mean_value","Sigma");

// Fit histogram in range defined by function
   hpx->Fit("fitf","r");

// Gets integral of function between fit limits
   printf("Integral of function = %g\n",func->Integral(-2,2));
}



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:34:29 MET