ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
exampleFunctor.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_math
3 /// Tutorial illustrating how creating a TF1 class using functor or class member functions
4 ///
5 /// can be run with:
6 ///
7 /// ~~~{.cpp}
8 /// root > .x exampleFunctor.C
9 /// root > .x exampleFunctor.C+ with ACLIC
10 /// ~~~
11 ///
12 /// \macro_image
13 /// \macro_code
14 ///
15 /// \author Lorenzo Moneta
16 
17 #include "TF1.h"
18 #include "TMath.h"
19 #include "TLegend.h"
20 
21 
22 double MyFunc (double *x, double *p ) {
23  return TMath::Gaus(x[0],p[0],p[1] );
24 }
25 
26 // function object (functor)
27 struct MyDerivFunc {
28  MyDerivFunc(TF1 * f): fFunc(f) {}
29  double operator() (double *x, double * ) const {
30  return fFunc->Derivative(*x);
31  }
32  TF1 * fFunc;
33 };
34 // function class with a member function
35 struct MyIntegFunc {
36  MyIntegFunc(TF1 * f): fFunc(f) {}
37  double Integral (double *x, double * ) const {
38  double a = fFunc->GetXmin();
39  return fFunc->Integral(a, *x);
40  }
41  TF1 * fFunc;
42 };
43 
44 
45 
46 void exampleFunctor() {
47 
48  double xmin = -10; double xmax = 10;
49 
50  // create TF1 using a free C function
51  TF1 * f1 = new TF1("f1",MyFunc,xmin,xmax,2);
52  f1->SetParameters(0.,1.);
53  f1->SetMaximum(3); f1->SetMinimum(-1);
54  f1->Draw();
55 
56  // Derivative function
57  // example to create TF1 using a functor
58 
59  // in order to work with interpreter the function object must be created and lived all time for all time
60  // of the TF1. In compiled mode, the function object can be passed by value (recommended) and there
61  // is also no need to specify the type of the function class. Example is as follow:
62  // TF1 * f2 = new TF1("f2",MyDerivFunc(f1), xmin, xmax,0); // only for C++ compiled mode
63 
64  MyDerivFunc * deriv = new MyDerivFunc(f1);
65  TF1 * f2 = new TF1("f2",deriv, xmin, xmax, 0);
66 
67  f2->SetLineColor(kBlue);
68  f2->Draw("same");
69 
70 
71  // integral function
72  // example to create a TF1 using a member function of a user class
73 
74  // in order to work with interpreter the function object must be created and lived all time for all time
75  // of the TF1. In compiled mode there is no need to specify the type of the function class and the name
76  // of the member function
77  // TF1 * f3 = new TF1("f3",intg,&MyIntegFunc::Integral,xmin,xmax, 0); // only for C++ compiled mode
78 
79  MyIntegFunc * intg = new MyIntegFunc(f1);
80  TF1 * f3 = new TF1("f3",intg,&MyIntegFunc::Integral, xmin, xmax, 0);
81 
82  f3->SetLineColor(kRed);
83  f3->Draw("same");
84 
85  TLegend * l = new TLegend(0.78, 0.25, 0.97 ,0.45);
86  l->AddEntry(f1, "Func");
87  l->AddEntry(f2, "Deriv.");
88  l->AddEntry(f3, "Integral");
89  l->Draw();
90 }
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:432
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:35
Definition: Rtypes.h:61
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:373
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF1.cxx:1052
virtual void SetMinimum(Double_t minimum=-1111)
Set the minimum value along Y for this function In case the function is already drawn, set also the minimum in the helper histogram.
Definition: TF1.cxx:3090
TFile * f
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
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:453
virtual void SetMaximum(Double_t maximum=-1111)
Set the maximum value along Y for this function In case the function is already drawn, set also the maximum in the helper histogram.
Definition: TF1.cxx:3077
TRObject operator()(const T1 &t1) const
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:280
1-Dim function class
Definition: TF1.h:149
Definition: Rtypes.h:61