Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf104_classfactory.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
4/// Basic functionality: The class factory for functions and pdfs
5///
6/// NOTE: This demo uses code that is generated by the macro,
7/// therefore it cannot be compiled in one step by ACliC.
8/// To run this macro compiled with ACliC do
9///
10/// ~~~ {.cpp}
11/// root>.x rf104_classfactory.C // run interpreted to generate code
12/// root>.L MyPdfV3.cxx+ // Compile and load created class
13/// root>.x rf104_classfactory.C+ // run compiled code
14/// ~~~
15///
16/// \macro_image
17/// \macro_code
18/// \macro_output
19///
20/// \date July 2008
21/// \author Wouter Verkerke
22
23#include "RooRealVar.h"
24#include "RooDataSet.h"
25#include "RooGaussian.h"
26#include "TCanvas.h"
27#include "TAxis.h"
28#include "RooPlot.h"
29#include "RooClassFactory.h"
30#include "TROOT.h"
31
32using namespace RooFit;
33
35{
36 // W r i t e c l a s s s k e l e t o n c o d e
37 // --------------------------------------------------
38
39 // Write skeleton pdf class with variable x,a,b
40 // To use this class,
41 // - Edit the file MyPdfV1.cxx and implement the evaluate() method in terms of x,a and b
42 // - Compile and link class with '.x MyPdfV1.cxx+'
43 //
44 RooClassFactory::makePdf("MyPdfV1", "x,A,B");
45
46 // 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
47 // ---------------------------------------------------------------------
48
49 // Write skeleton pdf class with variable x,a,b and given formula expression
50 // To use this class,
51 // - Compile and link class with '.x MyPdfV2.cxx+'
52 //
53 RooClassFactory::makePdf("MyPdfV2", "x,A,B", "", "A*fabs(x)+pow(x-B,2)");
54
55 // 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
56 // ---------------------------------------------------------------------------------
57
58 // Write skeleton pdf class with variable x,a,b, given formula expression _and_
59 // given expression for analytical integral over x
60 // To use this class,
61 // - Compile and link class with '.x MyPdfV3.cxx+'
62 //
63 RooClassFactory::makePdf("MyPdfV3", "x,A,B", "", "A*fabs(x)+pow(x-B,2)", true, false,
64 "x:(A/2)*(pow(x.max(rangeName),2)+pow(x.min(rangeName),2))+(1./"
65 "3)*(pow(x.max(rangeName)-B,3)-pow(x.min(rangeName)-B,3))");
66
67 // 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
68 // ---------------------------------------------------------
69
70 // Compile MyPdfV3 class
71 gROOT->ProcessLineSync(".x MyPdfV3.cxx+");
72
73 // Create instance of MyPdfV3 class
74 RooRealVar a("a", "a", 1);
75 RooRealVar b("b", "b", 2, -10, 10);
76 RooRealVar y("y", "y", -10, 10);
77
78 // We need to hide the type to run in a ROOT macro
79 RooWorkspace w("w");
80 w.factory("MyPdfV3::pdf(y[-10,10], a[1], b[2,-10,10])");
81 auto pdf = w.pdf("pdf");
82
83 // Generate toy data from pdf and plot data and pdf on frame
84 RooPlot *frame1 = y.frame(Title("Compiled class MyPdfV3"));
85 std::unique_ptr<RooDataSet> data{pdf->generate(y, 1000)};
86 pdf->fitTo(*data, PrintLevel(-1));
87 data->plotOn(frame1);
88 pdf->plotOn(frame1);
89
90 // -----------------------------------------------------------------
91 // 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
92 // =================================================================
93
94 // Declare observable x
95 RooRealVar x("x", "x", -20, 20);
96
97 // The RooClassFactory::makePdfInstance() function performs code writing, compiling, linking
98 // and object instantiation in one go and can serve as a straight replacement of RooGenericPdf
99
100 RooRealVar alpha("alpha", "alpha", 5, 0.1, 10);
101 RooAbsPdf *genpdf =
102 RooClassFactory::makePdfInstance("GenPdf", "(1+0.1*fabs(x)+sin(sqrt(fabs(x*alpha+0.1))))", RooArgSet(x, alpha));
103
104 // Generate a toy dataset from the interpreted pdf
105 std::unique_ptr<RooDataSet> data2{genpdf->generate(x, 50000)};
106
107 // Fit the interpreted pdf to the generated data
108 genpdf->fitTo(*data2, PrintLevel(-1));
109
110 // Make a plot of the data and the pdf overlaid
111 RooPlot *frame2 = x.frame(Title("Compiled version of pdf of rf103"));
112 data2->plotOn(frame2);
113 genpdf->plotOn(frame2);
114
115 // Draw all frames on a canvas
116 TCanvas *c = new TCanvas("rf104_classfactory", "rf104_classfactory", 800, 400);
117 c->Divide(2);
118 c->cd(1);
119 gPad->SetLeftMargin(0.15);
120 frame1->GetYaxis()->SetTitleOffset(1.4);
121 frame1->Draw();
122 c->cd(2);
123 gPad->SetLeftMargin(0.15);
124 frame2->GetYaxis()->SetTitleOffset(1.4);
125 frame2->Draw();
126}
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
#define gROOT
Definition TROOT.h:406
#define gPad
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
RooPlot * plotOn(RooPlot *frame, const RooCmdArg &arg1={}, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={}, const RooCmdArg &arg9={}, const RooCmdArg &arg10={}) const override
Helper calling plotOn(RooPlot*, RooLinkedList&) const.
Definition RooAbsPdf.h:124
RooFit::OwningPtr< RooFitResult > fitTo(RooAbsData &data, CmdArgs_t const &... cmdArgs)
Fit PDF to given dataset.
Definition RooAbsPdf.h:157
RooFit::OwningPtr< RooDataSet > generate(const RooArgSet &whatVars, Int_t nEvents, const RooCmdArg &arg1, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={})
See RooAbsPdf::generate(const RooArgSet&,const RooCmdArg&,const RooCmdArg&,const RooCmdArg&,...
Definition RooAbsPdf.h:57
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
static bool makePdf(std::string const &name, std::string const &realArgNames="", std::string const &catArgNames="", std::string const &expression="1.0", bool hasAnaInt=false, bool hasIntGen=false, std::string const &intExpression="")
Write code for a RooAbsPdf implementation with class name 'name'.
static RooAbsPdf * makePdfInstance(std::string const &className, std::string const &name, std::string const &expression, const RooArgList &vars, std::string const &intExpression="")
Write, compile and load code and instantiate object for a RooAbsPdf implementation with class name 'n...
Plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:45
static RooPlot * frame(const RooAbsRealLValue &var, double xmin, double xmax, Int_t nBins)
Create a new frame for a given variable in x.
Definition RooPlot.cxx:225
TAxis * GetYaxis() const
Definition RooPlot.cxx:1264
void Draw(Option_t *options=nullptr) override
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:637
Variable that can be changed from the outside.
Definition RooRealVar.h:37
Persistable container for RooFit projects.
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:298
The Canvas class.
Definition TCanvas.h:23
RooCmdArg PrintLevel(Int_t code)
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26
const char * Title
Definition TXMLSetup.cxx:68