ROOT logo
//////////////////////////////////////////////////////////////////////////
//
// 'BASIC FUNCTIONALITY' RooFit tutorial macro #104
// 
// The class factory for functions and p.d.f.s
//
// 
// NOTE: This demo uses code that is generated by the macro, 
//       therefore it cannot be compiled in one step by ACliC.
//       To run this macro compiled with ACliC do
//
//         root>.x rf104_classfactory.C // run interpreted to generate code
//         root>.L MyPdfV3.cxx+         // Compile and load created classs
//         root>.x rf104_classfactory.C+ // run compiled code
//       
//
// 07/2008 - Wouter Verkerke 
//
/////////////////////////////////////////////////////////////////////////

#ifndef __CINT__
#include "RooGlobalFunc.h"
#endif
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"
#include "RooClassFactory.h"
#include "TROOT.h"

#ifndef __CINT__
#include "MyPdfV3.h"
#endif


using namespace RooFit ;


void rf104_classfactory()
{  
  // W r i t e   c l a s s   s k e l e t o n   c o d e
  // --------------------------------------------------

  // Write skeleton p.d.f class with variable x,a,b
  // To use this class, 
  //    - Edit the file MyPdfV1.cxx and implement the evaluate() method in terms of x,a and b
  //    - Compile and link class with '.x MyPdfV1.cxx+'
  //
  RooClassFactory::makePdf("MyPdfV1","x,A,B") ;


  // W i t h   a d d e d   i n i t i a l   v a l u e   e x p r e s s i o n
  // ---------------------------------------------------------------------

  // Write skeleton p.d.f class with variable x,a,b and given formula expression 
  // To use this class, 
  //    - Compile and link class with '.x MyPdfV2.cxx+'
  //
  RooClassFactory::makePdf("MyPdfV2","x,A,B","","A*fabs(x)+pow(x-B,2)") ;
  

  // W i t h   a d d e d   a n a l y t i c a l   i n t e g r a l   e x p r e s s i o n
  // ---------------------------------------------------------------------------------

  // Write skeleton p.d.f class with variable x,a,b, given formula expression _and_
  // given expression for analytical integral over x
  // To use this class, 
  //    - Compile and link class with '.x MyPdfV3.cxx+'
  //
  RooClassFactory::makePdf("MyPdfV3","x,A,B","","A*fabs(x)+pow(x-B,2)",kTRUE,kFALSE,
			   "x:(A/2)*(pow(x.max(rangeName),2)+pow(x.min(rangeName),2))+(1./3)*(pow(x.max(rangeName)-B,3)-pow(x.min(rangeName)-B,3))") ;



  // U s e   i n s t a n c e   o f   c r e a t e d   c l a s s 
  // ---------------------------------------------------------
 
  // Compile MyPdfV3 class (only when running in CINT)
#ifdef __CINT__
  gROOT->ProcessLineSync(".x MyPdfV3.cxx+") ;
#endif

  // Creat instance of MyPdfV3 class
  RooRealVar a("a","a",1) ;
  RooRealVar b("b","b",2,-10,10) ;
  RooRealVar y("y","y",-10,10);
  MyPdfV3 pdf("pdf","pdf",y,a,b) ;

  // Generate toy data from pdf and plot data and p.d.f on frame
  RooPlot* frame1 = y.frame(Title("Compiled class MyPdfV3")) ;
  RooDataSet* data = pdf.generate(y,1000) ;
  pdf.fitTo(*data) ;
  data->plotOn(frame1) ;
  pdf.plotOn(frame1) ;


  ///////////////////////////////////////////////////////////////////////
  // C o m p i l e d   v e r s i o n   o f   e x a m p l e   r f 1 0 3 //
  ///////////////////////////////////////////////////////////////////////

  // Declare observable x
  RooRealVar x("x","x",-20,20) ;

  // The RooClassFactory::makePdfInstance() function performs code writing, compiling, linking
  // and object instantiation in one go and can serve as a straight replacement of RooGenericPdf

  RooRealVar alpha("alpha","alpha",5,0.1,10) ;
  RooAbsPdf* genpdf = RooClassFactory::makePdfInstance("GenPdf","(1+0.1*fabs(x)+sin(sqrt(fabs(x*alpha+0.1))))",RooArgSet(x,alpha)) ;

  // Generate a toy dataset from the interpreted p.d.f
  RooDataSet* data2 = genpdf->generate(x,50000) ;

  // Fit the interpreted p.d.f to the generated data
  genpdf->fitTo(*data2) ;

  // Make a plot of the data and the p.d.f overlaid
  RooPlot* frame2 = x.frame(Title("Compiled version of pdf of rf103")) ;
  data2->plotOn(frame2) ;
  genpdf->plotOn(frame2) ;  

  // Draw all frames on a canvas
  TCanvas* c = new TCanvas("rf104_classfactory","rf104_classfactory",800,400) ;
  c->Divide(2) ;
  c->cd(1) ; gPad->SetLeftMargin(0.15) ; frame1->GetYaxis()->SetTitleOffset(1.4) ; frame1->Draw() ;
  c->cd(2) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.4) ; frame2->Draw() ;

}
 rf104_classfactory.C:1
 rf104_classfactory.C:2
 rf104_classfactory.C:3
 rf104_classfactory.C:4
 rf104_classfactory.C:5
 rf104_classfactory.C:6
 rf104_classfactory.C:7
 rf104_classfactory.C:8
 rf104_classfactory.C:9
 rf104_classfactory.C:10
 rf104_classfactory.C:11
 rf104_classfactory.C:12
 rf104_classfactory.C:13
 rf104_classfactory.C:14
 rf104_classfactory.C:15
 rf104_classfactory.C:16
 rf104_classfactory.C:17
 rf104_classfactory.C:18
 rf104_classfactory.C:19
 rf104_classfactory.C:20
 rf104_classfactory.C:21
 rf104_classfactory.C:22
 rf104_classfactory.C:23
 rf104_classfactory.C:24
 rf104_classfactory.C:25
 rf104_classfactory.C:26
 rf104_classfactory.C:27
 rf104_classfactory.C:28
 rf104_classfactory.C:29
 rf104_classfactory.C:30
 rf104_classfactory.C:31
 rf104_classfactory.C:32
 rf104_classfactory.C:33
 rf104_classfactory.C:34
 rf104_classfactory.C:35
 rf104_classfactory.C:36
 rf104_classfactory.C:37
 rf104_classfactory.C:38
 rf104_classfactory.C:39
 rf104_classfactory.C:40
 rf104_classfactory.C:41
 rf104_classfactory.C:42
 rf104_classfactory.C:43
 rf104_classfactory.C:44
 rf104_classfactory.C:45
 rf104_classfactory.C:46
 rf104_classfactory.C:47
 rf104_classfactory.C:48
 rf104_classfactory.C:49
 rf104_classfactory.C:50
 rf104_classfactory.C:51
 rf104_classfactory.C:52
 rf104_classfactory.C:53
 rf104_classfactory.C:54
 rf104_classfactory.C:55
 rf104_classfactory.C:56
 rf104_classfactory.C:57
 rf104_classfactory.C:58
 rf104_classfactory.C:59
 rf104_classfactory.C:60
 rf104_classfactory.C:61
 rf104_classfactory.C:62
 rf104_classfactory.C:63
 rf104_classfactory.C:64
 rf104_classfactory.C:65
 rf104_classfactory.C:66
 rf104_classfactory.C:67
 rf104_classfactory.C:68
 rf104_classfactory.C:69
 rf104_classfactory.C:70
 rf104_classfactory.C:71
 rf104_classfactory.C:72
 rf104_classfactory.C:73
 rf104_classfactory.C:74
 rf104_classfactory.C:75
 rf104_classfactory.C:76
 rf104_classfactory.C:77
 rf104_classfactory.C:78
 rf104_classfactory.C:79
 rf104_classfactory.C:80
 rf104_classfactory.C:81
 rf104_classfactory.C:82
 rf104_classfactory.C:83
 rf104_classfactory.C:84
 rf104_classfactory.C:85
 rf104_classfactory.C:86
 rf104_classfactory.C:87
 rf104_classfactory.C:88
 rf104_classfactory.C:89
 rf104_classfactory.C:90
 rf104_classfactory.C:91
 rf104_classfactory.C:92
 rf104_classfactory.C:93
 rf104_classfactory.C:94
 rf104_classfactory.C:95
 rf104_classfactory.C:96
 rf104_classfactory.C:97
 rf104_classfactory.C:98
 rf104_classfactory.C:99
 rf104_classfactory.C:100
 rf104_classfactory.C:101
 rf104_classfactory.C:102
 rf104_classfactory.C:103
 rf104_classfactory.C:104
 rf104_classfactory.C:105
 rf104_classfactory.C:106
 rf104_classfactory.C:107
 rf104_classfactory.C:108
 rf104_classfactory.C:109
 rf104_classfactory.C:110
 rf104_classfactory.C:111
 rf104_classfactory.C:112
 rf104_classfactory.C:113
 rf104_classfactory.C:114
 rf104_classfactory.C:115
 rf104_classfactory.C:116
 rf104_classfactory.C:117
 rf104_classfactory.C:118
 rf104_classfactory.C:119
 rf104_classfactory.C:120
 rf104_classfactory.C:121
 rf104_classfactory.C:122
 rf104_classfactory.C:123
 rf104_classfactory.C:124
 rf104_classfactory.C:125
 rf104_classfactory.C:126
 rf104_classfactory.C:127
 rf104_classfactory.C:128
 rf104_classfactory.C:129
 rf104_classfactory.C:130