Logo ROOT  
Reference Guide
rf603_multicpu.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
4///
5/// Likelihood and minimization: setting up a multi-core parallelized unbinned maximum likelihood fit
6///
7/// \macro_image
8/// \macro_output
9/// \macro_code
10///
11/// \date 07/2008
12/// \author Wouter Verkerke
13
14#include "RooRealVar.h"
15#include "RooDataSet.h"
16#include "RooGaussian.h"
17#include "RooConstVar.h"
18#include "RooPolynomial.h"
19#include "RooAddPdf.h"
20#include "RooProdPdf.h"
21#include "TCanvas.h"
22#include "TAxis.h"
23#include "RooPlot.h"
24using namespace RooFit;
25
26void rf603_multicpu()
27{
28
29 // C r e a t e 3 D p d f a n d d a t a
30 // -------------------------------------------
31
32 // Create observables
33 RooRealVar x("x", "x", -5, 5);
34 RooRealVar y("y", "y", -5, 5);
35 RooRealVar z("z", "z", -5, 5);
36
37 // Create signal pdf gauss(x)*gauss(y)*gauss(z)
38 RooGaussian gx("gx", "gx", x, RooConst(0), RooConst(1));
39 RooGaussian gy("gy", "gy", y, RooConst(0), RooConst(1));
40 RooGaussian gz("gz", "gz", z, RooConst(0), RooConst(1));
41 RooProdPdf sig("sig", "sig", RooArgSet(gx, gy, gz));
42
43 // Create background pdf poly(x)*poly(y)*poly(z)
44 RooPolynomial px("px", "px", x, RooArgSet(RooConst(-0.1), RooConst(0.004)));
45 RooPolynomial py("py", "py", y, RooArgSet(RooConst(0.1), RooConst(-0.004)));
46 RooPolynomial pz("pz", "pz", z);
47 RooProdPdf bkg("bkg", "bkg", RooArgSet(px, py, pz));
48
49 // Create composite pdf sig+bkg
50 RooRealVar fsig("fsig", "signal fraction", 0.1, 0., 1.);
51 RooAddPdf model("model", "model", RooArgList(sig, bkg), fsig);
52
53 // Generate large dataset
54 RooDataSet *data = model.generate(RooArgSet(x, y, z), 200000);
55
56 // P a r a l l e l f i t t i n g
57 // -------------------------------
58
59 // In parallel mode the likelihood calculation is split in N pieces,
60 // that are calculated in parallel and added a posteriori before passing
61 // it back to MINUIT.
62
63 // Use four processes and time results both in wall time and CPU time
64 model.fitTo(*data, NumCPU(4), Timer(kTRUE));
65
66 // P a r a l l e l M C p r o j e c t i o n s
67 // ----------------------------------------------
68
69 // Construct signal, total likelihood projection on (y,z) observables and likelihood ratio
70 RooAbsPdf *sigyz = sig.createProjection(x);
71 RooAbsPdf *totyz = model.createProjection(x);
72 RooFormulaVar llratio_func("llratio", "log10(@0)-log10(@1)", RooArgList(*sigyz, *totyz));
73
74 // Calculate likelihood ratio for each event, define subset of events with high signal likelihood
75 data->addColumn(llratio_func);
76 RooDataSet *dataSel = (RooDataSet *)data->reduce(Cut("llratio>0.7"));
77
78 // Make plot frame and plot data
79 RooPlot *frame = x.frame(Title("Projection on X with LLratio(y,z)>0.7"), Bins(40));
80 dataSel->plotOn(frame);
81
82 // Perform parallel projection using MC integration of pdf using given input dataSet.
83 // In this mode the data-weighted average of the pdf is calculated by splitting the
84 // input dataset in N equal pieces and calculating in parallel the weighted average
85 // one each subset. The N results of those calculations are then weighted into the
86 // final result
87
88 // Use four processes
89 model.plotOn(frame, ProjWData(*dataSel), NumCPU(4));
90
91 new TCanvas("rf603_multicpu", "rf603_multicpu", 600, 600);
92 gPad->SetLeftMargin(0.15);
93 frame->GetYaxis()->SetTitleOffset(1.6);
94 frame->Draw();
95}
const Bool_t kTRUE
Definition: RtypesCore.h:89
#define gPad
Definition: TVirtualPad.h:287
RooAbsData * reduce(const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg(), const RooCmdArg &arg3=RooCmdArg(), const RooCmdArg &arg4=RooCmdArg(), const RooCmdArg &arg5=RooCmdArg(), const RooCmdArg &arg6=RooCmdArg(), const RooCmdArg &arg7=RooCmdArg(), const RooCmdArg &arg8=RooCmdArg())
Create a reduced copy of this dataset.
Definition: RooAbsData.cxx:381
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
Calls RooPlot* plotOn(RooPlot* frame, const RooLinkedList& cmdList) const ;.
Definition: RooAbsData.cxx:546
virtual RooAbsPdf * createProjection(const RooArgSet &iset)
Return a p.d.f that represent a projection of this p.d.f integrated over given observables.
Definition: RooAbsPdf.cxx:3342
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:29
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:21
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
virtual RooAbsArg * addColumn(RooAbsArg &var, Bool_t adjustRange=kTRUE)
Add a column with the values of the given (function) argument to this dataset.
A RooFormulaVar is a generic implementation of a real-valued object, which takes a RooArgList of serv...
Definition: RooFormulaVar.h:29
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
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:1277
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:712
RooPolynomial implements a polynomial p.d.f of the form.
Definition: RooPolynomial.h:28
RooProdPdf is an efficient implementation of a product of PDFs of the form.
Definition: RooProdPdf.h:31
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:35
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:294
The Canvas class.
Definition: TCanvas.h:27
RooConstVar & RooConst(Double_t val)
RooCmdArg Bins(Int_t nbin)
RooCmdArg NumCPU(Int_t nCPU, Int_t interleave=0)
RooCmdArg Timer(Bool_t flag=kTRUE)
RooCmdArg ProjWData(const RooAbsData &projData, Bool_t binData=kFALSE)
RooCmdArg Cut(const char *cutSpec)
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...
const char * Title
Definition: TXMLSetup.cxx:67