The following types of functions can be created:
TF1 *fa1 = new TF1("fa1","sin(x)/x",0,10); fa1->Draw();
TF1 *fa2 = new TF1("fa2","TMath::DiLog(x)",0,10); fa2->Draw();
Double_t myFunc(x) { return x+sin(x); } TF1 *fa3 = new TF1("fa3","myFunc(x)",-3,5); fa3->Draw();
fa->SetParameter(0,value_first_parameter); fa->SetParameter(1,value_second_parameter);Parameters may be given a name:
fa->SetParName(0,"Constant");
// 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"); }
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. ..... }
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. ..... }
virtual | ~TF1Parameters() |
static TClass* | Class() |
Double_t | GetParameter(Int_t iparam) const |
Double_t | GetParameter(const char* name) const |
const Double_t* | GetParameters() const |
const char* | GetParName(Int_t iparam) const |
Int_t | GetParNumber(const char* name) const |
virtual TClass* | IsA() const |
TF1Parameters& | operator=(const TF1Parameters& rhs) |
const vector<double>& | ParamsVec() const |
void | SetParameter(Int_t iparam, Double_t value) |
void | SetParameter(const char* name, Double_t value) |
void | SetParameters(const Double_t* params) |
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) |
void | SetParName(Int_t iparam, const char* name) |
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") |
virtual void | ShowMembers(TMemberInspector& insp) const |
virtual void | Streamer(TBuffer&) |
void | StreamerNVirtual(TBuffer& ClassDef_StreamerNVirtual_b) |
TF1Parameters() | |
TF1Parameters(Int_t npar) | |
TF1Parameters(const TF1Parameters& rhs) |
bool | CheckIndex(Int_t i) const |
vector<string> | fParNames | parameter names |
vector<Double_t> | fParameters | parameter values |
return the parameter number given a name not very efficient but list of parameters is typically small could use a map if needed
set parameter values
set parameter names