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"
24 using namespace RooFit;
25 
26 void 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 }
RooPlot::Draw
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:691
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooFit::ProjWData
RooCmdArg ProjWData(const RooAbsData &projData, Bool_t binData=kFALSE)
Definition: RooGlobalFunc.cxx:46
RooAddPdf
Definition: RooAddPdf.h:32
RooFit::Bins
RooCmdArg Bins(Int_t nbin)
Definition: RooGlobalFunc.cxx:174
RooArgList
Definition: RooArgList.h:21
RooGaussian.h
x
Double_t x[n]
Definition: legend1.C:17
RooGaussian
Definition: RooGaussian.h:25
rf603_multicpu
Definition: rf603_multicpu.py:1
RooAddPdf.h
TCanvas.h
RooDataSet.h
RooPolynomial.h
RooFit::Cut
RooCmdArg Cut(const char *cutSpec)
Definition: RooGlobalFunc.cxx:80
RooFormulaVar
Definition: RooFormulaVar.h:30
RooProdPdf.h
RooFit
Definition: RooCFunction1Binding.h:29
RooPolynomial
Definition: RooPolynomial.h:28
RooAbsData::plotOn
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
Definition: RooAbsData.cxx:547
RooPlot.h
RooPlot::GetYaxis
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1256
RooPlot
Definition: RooPlot.h:44
y
Double_t y[n]
Definition: legend1.C:17
RooRealVar.h
RooConstVar.h
RooFit::NumCPU
RooCmdArg NumCPU(Int_t nCPU, Int_t interleave=0)
Definition: RooGlobalFunc.cxx:157
RooAbsPdf::createProjection
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:3362
TCanvas
Definition: TCanvas.h:23
RooAbsData::reduce
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:382
TAxis.h
gPad
#define gPad
Definition: TVirtualPad.h:287
RooDataSet
Definition: RooDataSet.h:33
make_cnn_model.model
model
Definition: make_cnn_model.py:6
RooAbsPdf
Definition: RooAbsPdf.h:40
TAttAxis::SetTitleOffset
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:293
RooRealVar
Definition: RooRealVar.h:35
RooProdPdf
Definition: RooProdPdf.h:33
RooFit::Title
RooCmdArg Title(const char *name)
Definition: RooGlobalFunc.cxx:173
RooArgSet
Definition: RooArgSet.h:28
RooDataSet::addColumn
virtual RooAbsArg * addColumn(RooAbsArg &var, Bool_t adjustRange=kTRUE)
Add a column with the values of the given (function) argument to this dataset.
Definition: RooDataSet.cxx:1383
RooFit::Timer
RooCmdArg Timer(Bool_t flag=kTRUE)
Definition: RooGlobalFunc.cxx:188
RooFit::RooConst
RooConstVar & RooConst(Double_t val)
Definition: RooGlobalFunc.cxx:341