ROOT logo
// 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
//
//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 (reccomended) and there 
   // is also no need to specify the type of the function class. Example is as follow: 
   // 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, "MyDerivFunc"); 

   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, "MyIntegFunc","Integral"); 

   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();


}
 exampleFunctor.C:1
 exampleFunctor.C:2
 exampleFunctor.C:3
 exampleFunctor.C:4
 exampleFunctor.C:5
 exampleFunctor.C:6
 exampleFunctor.C:7
 exampleFunctor.C:8
 exampleFunctor.C:9
 exampleFunctor.C:10
 exampleFunctor.C:11
 exampleFunctor.C:12
 exampleFunctor.C:13
 exampleFunctor.C:14
 exampleFunctor.C:15
 exampleFunctor.C:16
 exampleFunctor.C:17
 exampleFunctor.C:18
 exampleFunctor.C:19
 exampleFunctor.C:20
 exampleFunctor.C:21
 exampleFunctor.C:22
 exampleFunctor.C:23
 exampleFunctor.C:24
 exampleFunctor.C:25
 exampleFunctor.C:26
 exampleFunctor.C:27
 exampleFunctor.C:28
 exampleFunctor.C:29
 exampleFunctor.C:30
 exampleFunctor.C:31
 exampleFunctor.C:32
 exampleFunctor.C:33
 exampleFunctor.C:34
 exampleFunctor.C:35
 exampleFunctor.C:36
 exampleFunctor.C:37
 exampleFunctor.C:38
 exampleFunctor.C:39
 exampleFunctor.C:40
 exampleFunctor.C:41
 exampleFunctor.C:42
 exampleFunctor.C:43
 exampleFunctor.C:44
 exampleFunctor.C:45
 exampleFunctor.C:46
 exampleFunctor.C:47
 exampleFunctor.C:48
 exampleFunctor.C:49
 exampleFunctor.C:50
 exampleFunctor.C:51
 exampleFunctor.C:52
 exampleFunctor.C:53
 exampleFunctor.C:54
 exampleFunctor.C:55
 exampleFunctor.C:56
 exampleFunctor.C:57
 exampleFunctor.C:58
 exampleFunctor.C:59
 exampleFunctor.C:60
 exampleFunctor.C:61
 exampleFunctor.C:62
 exampleFunctor.C:63
 exampleFunctor.C:64
 exampleFunctor.C:65
 exampleFunctor.C:66
 exampleFunctor.C:67
 exampleFunctor.C:68
 exampleFunctor.C:69
 exampleFunctor.C:70
 exampleFunctor.C:71
 exampleFunctor.C:72
 exampleFunctor.C:73
 exampleFunctor.C:74
 exampleFunctor.C:75
 exampleFunctor.C:76
 exampleFunctor.C:77
 exampleFunctor.C:78
 exampleFunctor.C:79
 exampleFunctor.C:80
 exampleFunctor.C:81
 exampleFunctor.C:82
 exampleFunctor.C:83
 exampleFunctor.C:84
 exampleFunctor.C:85