exampleFunctor.C

Go to the documentation of this file.
00001 // Tutorial illustrating how creating a TF1 class using functor or class member functions 
00002 //
00003 //  can be run with:
00004 //  root > .x exampleFunctor.C
00005 //  root > .x exampleFunctor.C+ with ACLIC
00006 //
00007 //Author: Lorenzo Moneta
00008 
00009 #include "TF1.h"
00010 #include "TMath.h"
00011 #include "TLegend.h"
00012 
00013 
00014 double MyFunc (double *x, double *p ) { 
00015    return TMath::Gaus(x[0],p[0],p[1] );
00016 }
00017 
00018 // function object (functor) 
00019 struct MyDerivFunc { 
00020    MyDerivFunc(TF1 * f): fFunc(f) {}
00021    double operator() (double *x, double * )  const { 
00022       return fFunc->Derivative(*x);
00023    }
00024    TF1 * fFunc; 
00025 };
00026 // function class with a member function
00027 struct MyIntegFunc { 
00028    MyIntegFunc(TF1 * f): fFunc(f) {}
00029    double Integral (double *x, double * ) const { 
00030       double a = fFunc->GetXmin();
00031       return fFunc->Integral(a, *x);
00032    }
00033    TF1 * fFunc; 
00034 };
00035 
00036 
00037 
00038 void exampleFunctor() { 
00039 
00040    double xmin = -10;  double xmax = 10; 
00041 
00042    // create TF1 using a free C function 
00043    TF1 * f1 = new TF1("f1",MyFunc,xmin,xmax,2);
00044    f1->SetParameters(0.,1.);
00045    f1->SetMaximum(3);   f1->SetMinimum(-1);
00046    f1->Draw(); 
00047 
00048    // Derivative function 
00049    // example to create TF1 using a functor 
00050 
00051    // in order to work with interpreter the function object must be created and lived all time for all time 
00052    // of the TF1. In compiled mode, the function object can be passed by value (reccomended) and there 
00053    // is also no need to specify the type of the function class. Example is as follow: 
00054    // TF1 * f2 = new TF1("f2",MyDerivFunc(f1), xmin, xmax,0); // only for C++ compiled mode 
00055    
00056    MyDerivFunc * deriv = new MyDerivFunc(f1);
00057    TF1 * f2 = new TF1("f2",deriv, xmin, xmax, 0, "MyDerivFunc"); 
00058 
00059    f2->SetLineColor(kBlue); 
00060    f2->Draw("same");
00061 
00062 
00063    // integral function 
00064    // example to create a TF1 using a member function of a user class 
00065   
00066    // in order to work with interpreter the function object must be created and lived all time for all time 
00067    // of the TF1. In compiled mode there is no need to specify the type of the function class and the name 
00068    // of the member function
00069    // TF1 * f3 = new TF1("f3",intg,&MyIntegFunc::Integral,xmin,xmax, 0); // only for C++ compiled mode 
00070 
00071    MyIntegFunc * intg = new MyIntegFunc(f1);
00072    TF1 * f3 = new TF1("f3",intg,&MyIntegFunc::Integral, xmin, xmax, 0, "MyIntegFunc","Integral"); 
00073 
00074    f3->SetLineColor(kRed); 
00075    f3->Draw("same");
00076 
00077    TLegend * l = new TLegend(0.78, 0.25, 0.97 ,0.45);
00078    l->AddEntry(f1, "Func");
00079    l->AddEntry(f2, "Deriv.");
00080    l->AddEntry(f3, "Integral");
00081    l->Draw();
00082 
00083 
00084 }

Generated on Tue Jul 5 15:44:49 2011 for ROOT_528-00b_version by  doxygen 1.5.1