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