ROOT » HIST » HIST » TF1Parameters

class TF1Parameters


TF1: 1-Dim function class

A TF1 object is a 1-Dim function defined between a lower and upper limit.
The function may be a simple function (see TFormula) or a precompiled user function.
The function may have associated parameters.
TF1 graphics function is via the TH1/TGraph drawing functions.

The following types of functions can be created:

A - Expression using variable x and no parameters

Case 1: inline expression using standard C++ functions/operators

   TF1 *fa1 = new TF1("fa1","sin(x)/x",0,10);
   fa1->Draw();
output of html604/MACRO_TF1Parameters_1.gif

Case 2: inline expression using TMath functions without parameters

   TF1 *fa2 = new TF1("fa2","TMath::DiLog(x)",0,10);
   fa2->Draw();
output of html604/MACRO_TF1Parameters_3.gif

Case 3: inline expression using a CINT function by name

   Double_t myFunc(x) {
      return x+sin(x);
   }
   TF1 *fa3 = new TF1("fa3","myFunc(x)",-3,5);
   fa3->Draw();

B - Expression using variable x with parameters

Case 1: inline expression using standard C++ functions/operators

  • Example a:
    > TF1 *fa = new TF1("fa","[0]*x*sin([1]*x)",-3,3);
    This creates a function of variable x with 2 parameters. The parameters must be initialized via:
       fa->SetParameter(0,value_first_parameter);
       fa->SetParameter(1,value_second_parameter);
    
    Parameters may be given a name:
       fa->SetParName(0,"Constant");
    
  • Example b:
    > TF1 *fb = new TF1("fb","gaus(0)*expo(3)",0,10);
    gaus(0) is a substitute for [0]*exp(-0.5*((x-[1])/[2])**2) and (0) means start numbering parameters at 0. expo(3) is a substitute for exp([3]+[4]*x).

Case 2: inline expression using TMath functions with parameters

> TF1 *fb2 = new TF1("fa3","TMath::Landau(x,[0],[1],0)",-5,10); fb2->SetParameters(0.2,1.3); fb2->Draw();
output of html604/MACRO_TF1Parameters_5.gif

C - A general C function with parameters

Consider the macro myfunc.C below:
   // Macro myfunc.C
   Double_t myfunction(Double_t *x, Double_t *par)
   {
      Float_t xx =x[0];
      Double_t f = TMath::Abs(par[0]*sin(par[1]*xx)/xx);
      return f;
   }
   void myfunc()
   {
      TF1 *f1 = new TF1("myfunc",myfunction,0,10,2);
      f1->SetParameters(2,1);
      f1->SetParNames("constant","coefficient");
      f1->Draw();
   }
   void myfit()
   {
      TH1F *h1=new TH1F("h1","test",100,0,10);
      h1->FillRandom("myfunc",20000);
      TF1 *f1=gROOT->GetFunction("myfunc");
      f1->SetParameters(800,1);
      h1->Fit("myfunc");
   }

In an interactive session you can do:

   Root > .L myfunc.C
   Root > myfunc();
   Root > myfit();
TF1 objects can reference other TF1 objects (thanks John
Odonnell) of type A or B defined above. This excludes CINT interpreted functions
and compiled functions. However, there is a restriction. A function cannot
reference a basic function if the basic function is a polynomial polN.

Example:

   {
      TF1 *fcos = new TF1 ("fcos", "[0]*cos(x)", 0., 10.);
      fcos->SetParNames( "cos");
      fcos->SetParameter( 0, 1.1);
      TF1 *fsin = new TF1 ("fsin", "[0]*sin(x)", 0., 10.);
      fsin->SetParNames( "sin");
      fsin->SetParameter( 0, 2.1);
      TF1 *fsincos = new TF1 ("fsc", "fcos+fsin");
      TF1 *fs2 = new TF1 ("fs2", "fsc+fsc");
   }

D - A general C++ function object (functor) with parameters

A TF1 can be created from any C++ class implementing the operator()(double *x, double *p). The advantage of the function object is that he can have a state and reference therefore what-ever other object. In this way the user can customize his function.

Example:

class  MyFunctionObject {
 public:
   // use constructor to customize your function object
   double operator() (double *x, double *p) {
      // function implementation using class data members
   }
};
{
    ....
   MyFunctionObject * fobj = new MyFunctionObject(....);       // create the function object
   TF1 * f = new TF1("f",fobj,0,1,npar,"MyFunctionObject");    // create TF1 class.
   .....
}
When constructing the TF1 class, the name of the function object class is required only if running in CINT and it is not needed in compiled C++ mode. In addition in compiled mode the cfnution object can be passed to TF1 by value. See also the tutorial math/exampleFunctor.C for a running example.

E - A member function with parameters of a general C++ class

A TF1 can be created in this case from any member function of a class which has the signature of (double * , double *) and returning a double.

Example:

class  MyFunction {
 public:
   ...
   double Evaluate() (double *x, double *p) {
      // function implementation
   }
};
{
    ....
   MyFunction * fptr = new MyFunction(....);  // create the user function class
   TF1 * f = new TF1("f",fptr,&MyFunction::Evaluate,0,1,npar,"MyFunction","Evaluate");   // create TF1 class.
   .....
}
When constructing the TF1 class, the name of the function class and of the member function are required only if running in CINT and they are not need in compiled C++ mode. See also the tutorial math/exampleFunctor.C for a running example.
 

Function Members (Methods)

public:
virtual~TF1Parameters()
static TClass*Class()
Double_tGetParameter(Int_t iparam) const
Double_tGetParameter(const char* name) const
const Double_t*GetParameters() const
const char*GetParName(Int_t iparam) const
Int_tGetParNumber(const char* name) const
virtual TClass*IsA() const
TF1Parameters&operator=(const TF1Parameters& rhs)
const vector<double>&ParamsVec() const
voidSetParameter(Int_t iparam, Double_t value)
voidSetParameter(const char* name, Double_t value)
voidSetParameters(const Double_t* params)
voidSetParameters(Double_t p0, Double_t p1, Double_t p2 = 0, Double_t p3 = 0, Double_t p4 = 0, Double_t p5 = 0, Double_t p6 = 0, Double_t p7 = 0, Double_t p8 = 0, Double_t p9 = 0, Double_t p10 = 0)
voidSetParName(Int_t iparam, const char* name)
voidSetParNames(const char* name0 = "p0", const char* name1 = "p1", const char* name2 = "p2", const char* name3 = "p3", const char* name4 = "p4", const char* name5 = "p5", const char* name6 = "p6", const char* name7 = "p7", const char* name8 = "p8", const char* name9 = "p9", const char* name10 = "p10")
virtual voidShowMembers(TMemberInspector& insp) const
virtual voidStreamer(TBuffer&)
voidStreamerNVirtual(TBuffer& ClassDef_StreamerNVirtual_b)
TF1Parameters()
TF1Parameters(Int_t npar)
TF1Parameters(const TF1Parameters& rhs)
private:
boolCheckIndex(Int_t i) const

Data Members

private:
vector<string>fParNamesparameter names
vector<Double_t>fParametersparameter values

Class Charts

Class Charts

Function documentation

Int_t GetParNumber(const char* name) const
 return the parameter number given a name
 not very efficient but list of parameters is typically small
 could use a map if needed
void SetParameters(Double_t p0, Double_t p1, Double_t p2 = 0, Double_t p3 = 0, Double_t p4 = 0, Double_t p5 = 0, Double_t p6 = 0, Double_t p7 = 0, Double_t p8 = 0, Double_t p9 = 0, Double_t p10 = 0)
 set parameter values
void SetParNames(const char* name0 = "p0", const char* name1 = "p1", const char* name2 = "p2", const char* name3 = "p3", const char* name4 = "p4", const char* name5 = "p5", const char* name6 = "p6", const char* name7 = "p7", const char* name8 = "p8", const char* name9 = "p9", const char* name10 = "p10")
 set parameter names
TF1Parameters(const TF1Parameters& rhs)
 copy constructor
{}
TF1Parameters & operator=(const TF1Parameters& rhs)
 assignment
virtual ~TF1Parameters()
{}
Double_t GetParameter(Int_t iparam) const
 getter methods
Double_t GetParameter(const char* name) const
const Double_t * GetParameters() const
const std::vector<double> & ParamsVec() const
{ return fParameters; }
const char * GetParName(Int_t iparam) const
void SetParameter(Int_t iparam, Double_t value)
 setter methods
void SetParameters(const Double_t* params)
void SetParameter(const char* name, Double_t value)
void SetParName(Int_t iparam, const char* name)
bool CheckIndex(Int_t i) const