Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf104_classfactory.C File Reference

Detailed Description

View in nbviewer Open in SWAN Basic functionality: The class factory for functions and pdfs

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 class
root>.x rf104_classfactory.C+ // run compiled code
␛[1mRooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby␛[0m
Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University
All rights reserved, please read http://roofit.sourceforge.net/license.txt
(MyPdfV3) An instance of MyPdfV3.
[#1] INFO:Minization -- createNLL: caching constraint set under name CONSTR_OF_PDF_pdf_FOR_OBS_y with 0 entries
[#1] INFO:Minization -- RooMinimizer::optimizeConst: activating const optimization
**********
** 1 **SET PRINT 1
**********
**********
** 2 **SET NOGRAD
**********
PARAMETER DEFINITIONS:
NO. NAME VALUE STEP SIZE LIMITS
1 b 2.00000e+00 2.00000e+00 -1.00000e+01 1.00000e+01
**********
** 3 **SET ERR 0.5
**********
**********
** 4 **SET PRINT 1
**********
**********
** 5 **SET STR 1
**********
NOW USING STRATEGY 1: TRY TO BALANCE SPEED AGAINST RELIABILITY
**********
** 6 **MIGRAD 500 1
**********
FIRST CALL TO USER FUNCTION AT NEW START POINT, WITH IFLAG=4.
START MIGRAD MINIMIZATION. STRATEGY 1. CONVERGENCE WHEN EDM .LT. 1.00e-03
FCN=2568.49 FROM MIGRAD STATUS=INITIATE 4 CALLS 5 TOTAL
EDM= unknown STRATEGY= 1 NO ERROR MATRIX
EXT PARAMETER CURRENT GUESS STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 b 2.00000e+00 2.00000e+00 2.05758e-01 5.02230e+01
ERR DEF= 0.5
MIGRAD MINIMIZATION HAS CONVERGED.
MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=2568.28 FROM MIGRAD STATUS=CONVERGED 17 CALLS 18 TOTAL
EDM=9.63237e-09 STRATEGY= 1 ERROR MATRIX ACCURATE
EXT PARAMETER STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 b 1.91995e+00 1.24398e-01 4.43576e-04 7.74259e-03
ERR DEF= 0.5
EXTERNAL ERROR MATRIX. NDIM= 25 NPAR= 1 ERR DEF=0.5
1.548e-02
**********
** 7 **SET ERR 0.5
**********
**********
** 8 **SET PRINT 1
**********
**********
** 9 **HESSE 500
**********
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=2568.28 FROM HESSE STATUS=OK 5 CALLS 23 TOTAL
EDM=9.63457e-09 STRATEGY= 1 ERROR MATRIX ACCURATE
EXT PARAMETER INTERNAL INTERNAL
NO. NAME VALUE ERROR STEP SIZE VALUE
1 b 1.91995e+00 1.24398e-01 1.77431e-05 1.93194e-01
ERR DEF= 0.5
EXTERNAL ERROR MATRIX. NDIM= 25 NPAR= 1 ERR DEF=0.5
1.548e-02
[#1] INFO:Minization -- RooMinimizer::optimizeConst: deactivating const optimization
[#1] INFO:NumericIntegration -- RooRealIntegral::init(GenPdf_Int[x]) using numeric integrator RooIntegrator1D to calculate Int(x)
[#1] INFO:NumericIntegration -- RooRealIntegral::init(GenPdf_Int[x]) using numeric integrator RooIntegrator1D to calculate Int(x)
[#1] INFO:NumericIntegration -- RooRealIntegral::init(GenPdf_Int[x]) using numeric integrator RooIntegrator1D to calculate Int(x)
[#1] INFO:Minization -- RooMinimizer::optimizeConst: activating const optimization
**********
** 10 **SET PRINT 1
**********
**********
** 11 **SET NOGRAD
**********
PARAMETER DEFINITIONS:
NO. NAME VALUE STEP SIZE LIMITS
1 alpha 5.00000e+00 9.90000e-01 1.00000e-01 1.00000e+01
**********
** 12 **SET ERR 0.5
**********
**********
** 13 **SET PRINT 1
**********
**********
** 14 **SET STR 1
**********
NOW USING STRATEGY 1: TRY TO BALANCE SPEED AGAINST RELIABILITY
**********
** 15 **MIGRAD 500 1
**********
FIRST CALL TO USER FUNCTION AT NEW START POINT, WITH IFLAG=4.
START MIGRAD MINIMIZATION. STRATEGY 1. CONVERGENCE WHEN EDM .LT. 1.00e-03
FCN=178267 FROM MIGRAD STATUS=INITIATE 6 CALLS 7 TOTAL
EDM= unknown STRATEGY= 1 NO ERROR MATRIX
EXT PARAMETER CURRENT GUESS STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 alpha 5.00000e+00 9.90000e-01 2.01369e-01 2.50886e+02
ERR DEF= 0.5
MIGRAD MINIMIZATION HAS CONVERGED.
MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=178266 FROM MIGRAD STATUS=CONVERGED 14 CALLS 15 TOTAL
EDM=3.47331e-06 STRATEGY= 1 ERROR MATRIX ACCURATE
EXT PARAMETER STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 alpha 4.98248e+00 1.85558e-02 1.09523e-03 -4.97115e-01
ERR DEF= 0.5
EXTERNAL ERROR MATRIX. NDIM= 25 NPAR= 1 ERR DEF=0.5
3.443e-04
**********
** 16 **SET ERR 0.5
**********
**********
** 17 **SET PRINT 1
**********
**********
** 18 **HESSE 500
**********
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=178266 FROM HESSE STATUS=OK 5 CALLS 20 TOTAL
EDM=3.46509e-06 STRATEGY= 1 ERROR MATRIX ACCURATE
EXT PARAMETER INTERNAL INTERNAL
NO. NAME VALUE ERROR STEP SIZE VALUE
1 alpha 4.98248e+00 1.85557e-02 2.19047e-04 -1.36416e-02
ERR DEF= 0.5
EXTERNAL ERROR MATRIX. NDIM= 25 NPAR= 1 ERR DEF=0.5
3.443e-04
[#1] INFO:Minization -- RooMinimizer::optimizeConst: deactivating const optimization
[#1] INFO:NumericIntegration -- RooRealIntegral::init(GenPdf_Int[x]) using numeric integrator RooIntegrator1D to calculate Int(x)
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"
#include "TROOT.h"
using namespace RooFit;
{
// W r i t e c l a s s s k e l e t o n c o d e
// --------------------------------------------------
// Write skeleton pdf 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 pdf 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 pdf 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
gROOT->ProcessLineSync(".x MyPdfV3.cxx+");
// Create instance of MyPdfV3 class
RooRealVar a("a", "a", 1);
RooRealVar b("b", "b", 2, -10, 10);
RooRealVar y("y", "y", -10, 10);
// We need to hide the type to run in a ROOT macro
RooWorkspace w("w");
w.factory("MyPdfV3::pdf(y[-10,10], a[1], b[2,-10,10])");
auto pdf = w.pdf("pdf");
// Generate toy data from pdf and plot data and pdf 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 pdf
RooDataSet *data2 = genpdf->generate(x, 50000);
// Fit the interpreted pdf to the generated data
genpdf->fitTo(*data2);
// Make a plot of the data and the pdf 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();
}
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
const Bool_t kFALSE
Definition RtypesCore.h:92
const Bool_t kTRUE
Definition RtypesCore.h:91
#define gROOT
Definition TROOT.h:406
#define gPad
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
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())
See RooAbsPdf::generate(const RooArgSet&,const RooCmdArg&,const RooCmdArg&,const RooCmdArg&,...
Definition RooAbsPdf.h:58
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.
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
Helper calling plotOn(RooPlot*, RooLinkedList&) const.
Definition RooAbsPdf.h:121
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:29
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...
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:33
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:44
TAxis * GetYaxis() const
Definition RooPlot.cxx:1263
static RooPlot * frame(const RooAbsRealLValue &var, Double_t xmin, Double_t xmax, Int_t nBins)
Create a new frame for a given variable in x.
Definition RooPlot.cxx:249
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:691
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:39
The RooWorkspace is a persistable container for RooFit projects.
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:293
The Canvas class.
Definition TCanvas.h:23
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...
Date
July 2008
Author
Wouter Verkerke

Definition in file rf104_classfactory.C.