Logo ROOT   6.10/09
Reference Guide
rf104_classfactory.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 /// 'BASIC FUNCTIONALITY' RooFit tutorial macro #104
5 ///
6 /// The class factory for functions and p.d.f.s
7 ///
8 ///
9 /// NOTE: This demo uses code that is generated by the macro,
10 /// therefore it cannot be compiled in one step by ACliC.
11 /// To run this macro compiled with ACliC do
12 ///
13 /// ~~~ {.cpp}
14 /// root>.x rf104_classfactory.C // run interpreted to generate code
15 /// root>.L MyPdfV3.cxx+ // Compile and load created class
16 /// root>.x rf104_classfactory.C+ // run compiled code
17 /// ~~~
18 ///
19 /// \macro_image
20 /// \macro_output
21 /// \macro_code
22 /// \author 07/2008 - Wouter Verkerke
23 
24 #include "RooRealVar.h"
25 #include "RooDataSet.h"
26 #include "RooGaussian.h"
27 #include "TCanvas.h"
28 #include "TAxis.h"
29 #include "RooPlot.h"
30 #include "RooClassFactory.h"
31 #include "TROOT.h"
32 
33 
34 using namespace RooFit ;
35 
36 
37 void rf104_classfactory()
38 {
39  // W r i t e c l a s s s k e l e t o n c o d e
40  // --------------------------------------------------
41 
42  // Write skeleton p.d.f class with variable x,a,b
43  // To use this class,
44  // - Edit the file MyPdfV1.cxx and implement the evaluate() method in terms of x,a and b
45  // - Compile and link class with '.x MyPdfV1.cxx+'
46  //
47  RooClassFactory::makePdf("MyPdfV1","x,A,B") ;
48 
49 
50  // 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
51  // ---------------------------------------------------------------------
52 
53  // Write skeleton p.d.f class with variable x,a,b and given formula expression
54  // To use this class,
55  // - Compile and link class with '.x MyPdfV2.cxx+'
56  //
57  RooClassFactory::makePdf("MyPdfV2","x,A,B","","A*fabs(x)+pow(x-B,2)") ;
58 
59 
60  // 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
61  // ---------------------------------------------------------------------------------
62 
63  // Write skeleton p.d.f class with variable x,a,b, given formula expression _and_
64  // given expression for analytical integral over x
65  // To use this class,
66  // - Compile and link class with '.x MyPdfV3.cxx+'
67  //
68  RooClassFactory::makePdf("MyPdfV3","x,A,B","","A*fabs(x)+pow(x-B,2)",kTRUE,kFALSE,
69  "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))") ;
70 
71 
72 
73  // 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
74  // ---------------------------------------------------------
75 
76  // Compile MyPdfV3 class (only when running in CINT)
77  gROOT->ProcessLineSync(".x MyPdfV3.cxx+") ;
78 
79  // Create instance of MyPdfV3 class
80  RooRealVar a("a","a",1) ;
81  RooRealVar b("b","b",2,-10,10) ;
82  RooRealVar y("y","y",-10,10);
83  MyPdfV3 pdf("pdf","pdf",y,a,b) ;
84 
85  // Generate toy data from pdf and plot data and p.d.f on frame
86  RooPlot* frame1 = y.frame(Title("Compiled class MyPdfV3")) ;
87  RooDataSet* data = pdf.generate(y,1000) ;
88  pdf.fitTo(*data) ;
89  data->plotOn(frame1) ;
90  pdf.plotOn(frame1) ;
91 
92 
93  // -----------------------------------------------------------------
94  // 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
95  // =================================================================
96 
97  // Declare observable x
98  RooRealVar x("x","x",-20,20) ;
99 
100  // The RooClassFactory::makePdfInstance() function performs code writing, compiling, linking
101  // and object instantiation in one go and can serve as a straight replacement of RooGenericPdf
102 
103  RooRealVar alpha("alpha","alpha",5,0.1,10) ;
104  RooAbsPdf* genpdf = RooClassFactory::makePdfInstance("GenPdf","(1+0.1*fabs(x)+sin(sqrt(fabs(x*alpha+0.1))))",RooArgSet(x,alpha)) ;
105 
106  // Generate a toy dataset from the interpreted p.d.f
107  RooDataSet* data2 = genpdf->generate(x,50000) ;
108 
109  // Fit the interpreted p.d.f to the generated data
110  genpdf->fitTo(*data2) ;
111 
112  // Make a plot of the data and the p.d.f overlaid
113  RooPlot* frame2 = x.frame(Title("Compiled version of pdf of rf103")) ;
114  data2->plotOn(frame2) ;
115  genpdf->plotOn(frame2) ;
116 
117  // Draw all frames on a canvas
118  TCanvas* c = new TCanvas("rf104_classfactory","rf104_classfactory",800,400) ;
119  c->Divide(2) ;
120  c->cd(1) ; gPad->SetLeftMargin(0.15) ; frame1->GetYaxis()->SetTitleOffset(1.4) ; frame1->Draw() ;
121  c->cd(2) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.4) ; frame2->Draw() ;
122 
123 }
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:262
static RooAbsPdf * makePdfInstance(const char *className, const char *name, const char *expression, const RooArgList &vars, const char *intExpression=0)
Write, compile and load code and instantiate object for a RooAbsPdf implementation with class name 'n...
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1118
virtual RooPlot * plotOn(RooPlot *frame, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none()) const
Plot dataset on specified frame.
Definition: RooAbsData.cxx:552
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:679
#define gROOT
Definition: TROOT.h:375
TArc * a
Definition: textangle.C:12
virtual RooPlot * plotOn(RooPlot *frame, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none(), const RooCmdArg &arg9=RooCmdArg::none(), const RooCmdArg &arg10=RooCmdArg::none()) const
Plot (project) PDF on specified frame.
Definition: RooAbsPdf.h:105
RooCmdArg Title(const char *name)
Double_t x[n]
Definition: legend1.C:17
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
static Bool_t makePdf(const char *name, const char *realArgNames=0, const char *catArgNames=0, const char *expression="1.0", Bool_t hasAnaInt=kFALSE, Bool_t hasIntGen=kFALSE, const char *intExpression=0)
Write code for a RooAbsPdf implementation with class name 'name', taking RooAbsReal arguments with na...
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
A RooPlot is a plot frame and a container for graphics objects within that frame. ...
Definition: RooPlot.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:92
The Canvas class.
Definition: TCanvas.h:31
Double_t y[n]
Definition: legend1.C:17
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1135
RooDataSet * generate(const RooArgSet &whatVars, Int_t nEvents, const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none())
Generate a new dataset containing the specified variables with events sampled from our distribution...
Definition: RooAbsPdf.cxx:1702
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
#define gPad
Definition: TVirtualPad.h:284
virtual RooFitResult * fitTo(RooAbsData &data, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none())
Fit PDF to given dataset.
Definition: RooAbsPdf.cxx:1056
const Bool_t kTRUE
Definition: RtypesCore.h:91
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:559