Logo ROOT   6.18/05
Reference Guide
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
exampleFunctor.C File Reference

Detailed Description

View in nbviewer Open in SWAN Tutorial illustrating how creating a TF1 class using functor or class member functions

can be run with:

root > .x exampleFunctor.C
root > .x exampleFunctor.C+ with ACLIC
#include "TF1.h"
#include "TMath.h"
#include "TLegend.h"
double MyFunc (double *x, double *p ) {
return TMath::Gaus(x[0],p[0],p[1] );
}
// function object (functor)
struct MyDerivFunc {
MyDerivFunc(TF1 * f): fFunc(f) {}
double operator() (double *x, double * ) const {
return fFunc->Derivative(*x);
}
TF1 * fFunc;
};
// function class with a member function
struct MyIntegFunc {
MyIntegFunc(TF1 * f): fFunc(f) {}
double Integral (double *x, double * ) const {
double a = fFunc->GetXmin();
return fFunc->Integral(a, *x);
}
TF1 * fFunc;
};
void exampleFunctor() {
double xmin = -10; double xmax = 10;
// create TF1 using a free C function
TF1 * f1 = new TF1("f1",MyFunc,xmin,xmax,2);
f1->SetParameters(0.,1.);
f1->Draw();
// Derivative function
// example to create TF1 using a functor
// in order to work with interpreter the function object must be created and lived all time for all time
// of the TF1. In compiled mode, the function object can be passed by value (recommended) and there
// is also no need to specify the type of the function class. Example is as follows:
//
// `TF1 * f2 = new TF1("f2",MyDerivFunc(f1), xmin, xmax,0); // only for C++ compiled mode`
MyDerivFunc * deriv = new MyDerivFunc(f1);
TF1 * f2 = new TF1("f2",deriv, xmin, xmax, 0);
f2->Draw("same");
// integral function
// example to create a TF1 using a member function of a user class
// in order to work with interpreter the function object must be created and lived all time for all time
// of the TF1. In compiled mode there is no need to specify the type of the function class and the name
// of the member function
//
// `TF1 * f3 = new TF1("f3",intg,&MyIntegFunc::Integral,xmin,xmax, 0); // only for C++ compiled mode`
MyIntegFunc * intg = new MyIntegFunc(f1);
TF1 * f3 = new TF1("f3",intg,&MyIntegFunc::Integral, xmin, xmax, 0);
f3->Draw("same");
TLegend * l = new TLegend(0.78, 0.25, 0.97 ,0.45);
l->AddEntry(f1, "Func");
l->AddEntry(f2, "Deriv.");
l->AddEntry(f3, "Integral");
l->Draw();
}
#define f(i)
Definition: RSha256.hxx:104
@ kRed
Definition: Rtypes.h:64
@ kBlue
Definition: Rtypes.h:64
float xmin
Definition: THbookFile.cxx:93
float xmax
Definition: THbookFile.cxx:93
TRObject operator()(const T1 &t1) const
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
1-Dim function class
Definition: TF1.h:211
virtual void SetMaximum(Double_t maximum=-1111)
Set the maximum value along Y for this function In case the function is already drawn,...
Definition: TF1.cxx:3393
virtual void SetMinimum(Double_t minimum=-1111)
Set the minimum value along Y for this function In case the function is already drawn,...
Definition: TF1.cxx:3406
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF1.cxx:1317
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:638
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:23
Double_t x[n]
Definition: legend1.C:17
TF1 * f1
Definition: legend1.C:11
Double_t Gaus(Double_t x, Double_t mean=0, Double_t sigma=1, Bool_t norm=kFALSE)
Calculate a gaussian function with mean and sigma.
Definition: TMath.cxx:448
auto * l
Definition: textangle.C:4
auto * a
Definition: textangle.C:12
Author
Lorenzo Moneta

Definition in file exampleFunctor.C.