forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexampleFunctor.C
More file actions
93 lines (74 loc) · 2.51 KB
/
exampleFunctor.C
File metadata and controls
93 lines (74 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/// \file
/// \ingroup tutorial_math
/// \notebook
/// Tutorial illustrating how creating a TF1 class using functor or class member functions
///
/// can be run with:
///
/// ~~~{.cpp}
/// root > .x exampleFunctor.C
/// root > .x exampleFunctor.C+ with ACLIC
/// ~~~
///
/// \macro_image
/// \macro_code
///
/// \author Lorenzo Moneta
#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->SetMaximum(3); f1->SetMinimum(-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->SetLineColor(kBlue);
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->SetLineColor(kRed);
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();
}