Logo ROOT  
Reference Guide
exampleFunctor.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_math
3/// \notebook
4/// Tutorial illustrating how creating a TF1 class using functor or class member functions
5///
6/// can be run with:
7///
8/// ~~~{.cpp}
9/// root > .x exampleFunctor.C
10/// root > .x exampleFunctor.C+ with ACLIC
11/// ~~~
12///
13/// \macro_image
14/// \macro_code
15///
16/// \author Lorenzo Moneta
17
18#include "TF1.h"
19#include "TMath.h"
20#include "TLegend.h"
21
22
23double MyFunc (double *x, double *p ) {
24 return TMath::Gaus(x[0],p[0],p[1] );
25}
26
27// function object (functor)
28struct MyDerivFunc {
29 MyDerivFunc(TF1 * f): fFunc(f) {}
30 double operator() (double *x, double * ) const {
31 return fFunc->Derivative(*x);
32 }
33 TF1 * fFunc;
34};
35// function class with a member function
36struct MyIntegFunc {
37 MyIntegFunc(TF1 * f): fFunc(f) {}
38 double Integral (double *x, double * ) const {
39 double a = fFunc->GetXmin();
40 return fFunc->Integral(a, *x);
41 }
42 TF1 * fFunc;
43};
44
45
46
47void exampleFunctor() {
48
49 double xmin = -10; double xmax = 10;
50
51 // create TF1 using a free C function
52 TF1 * f1 = new TF1("f1",MyFunc,xmin,xmax,2);
53 f1->SetParameters(0.,1.);
54 f1->SetMaximum(3); f1->SetMinimum(-1);
55 f1->Draw();
56
57 // Derivative function
58 // example to create TF1 using a functor
59
60 // in order to work with interpreter the function object must be created and lived all time for all time
61 // of the TF1. In compiled mode, the function object can be passed by value (recommended) and there
62 // is also no need to specify the type of the function class. Example is as follows:
63 //
64 // `TF1 * f2 = new TF1("f2",MyDerivFunc(f1), xmin, xmax,0); // only for C++ compiled mode`
65
66 MyDerivFunc * deriv = new MyDerivFunc(f1);
67 TF1 * f2 = new TF1("f2",deriv, xmin, xmax, 0);
68
70 f2->Draw("same");
71
72
73 // integral function
74 // example to create a TF1 using a member function of a user class
75
76 // in order to work with interpreter the function object must be created and lived all time for all time
77 // of the TF1. In compiled mode there is no need to specify the type of the function class and the name
78 // of the member function
79 //
80 // `TF1 * f3 = new TF1("f3",intg,&MyIntegFunc::Integral,xmin,xmax, 0); // only for C++ compiled mode`
81
82 MyIntegFunc * intg = new MyIntegFunc(f1);
83 TF1 * f3 = new TF1("f3",intg,&MyIntegFunc::Integral, xmin, xmax, 0);
84
85 f3->SetLineColor(kRed);
86 f3->Draw("same");
87
88 TLegend * l = new TLegend(0.78, 0.25, 0.97 ,0.45);
89 l->AddEntry(f1, "Func");
90 l->AddEntry(f2, "Deriv.");
91 l->AddEntry(f3, "Integral");
92 l->Draw();
93}
#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:210
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:3396
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:3409
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF1.cxx:1320
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