ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
rf603_multicpu.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// 'LIKELIHOOD AND MINIMIZATION' RooFit tutorial macro #603
4 ///
5 /// Setting up a multi-core parallelized unbinned maximum likelihood fit
6 ///
7 ///
8 ///
9 /// \macro_code
10 /// \author 07/2008 - Wouter Verkerke
11 
12 
13 #ifndef __CINT__
14 #include "RooGlobalFunc.h"
15 #endif
16 #include "RooRealVar.h"
17 #include "RooDataSet.h"
18 #include "RooGaussian.h"
19 #include "RooConstVar.h"
20 #include "RooPolynomial.h"
21 #include "RooAddPdf.h"
22 #include "RooProdPdf.h"
23 #include "TCanvas.h"
24 #include "TAxis.h"
25 #include "RooPlot.h"
26 using namespace RooFit ;
27 
28 
29 void rf603_multicpu()
30 {
31 
32  // C r e a t e 3 D p d f a n d d a t a
33  // -------------------------------------------
34 
35  // Create observables
36  RooRealVar x("x","x",-5,5) ;
37  RooRealVar y("y","y",-5,5) ;
38  RooRealVar z("z","z",-5,5) ;
39 
40  // Create signal pdf gauss(x)*gauss(y)*gauss(z)
41  RooGaussian gx("gx","gx",x,RooConst(0),RooConst(1)) ;
42  RooGaussian gy("gy","gy",y,RooConst(0),RooConst(1)) ;
43  RooGaussian gz("gz","gz",z,RooConst(0),RooConst(1)) ;
44  RooProdPdf sig("sig","sig",RooArgSet(gx,gy,gz)) ;
45 
46  // Create background pdf poly(x)*poly(y)*poly(z)
47  RooPolynomial px("px","px",x,RooArgSet(RooConst(-0.1),RooConst(0.004))) ;
48  RooPolynomial py("py","py",y,RooArgSet(RooConst(0.1),RooConst(-0.004))) ;
49  RooPolynomial pz("pz","pz",z) ;
50  RooProdPdf bkg("bkg","bkg",RooArgSet(px,py,pz)) ;
51 
52  // Create composite pdf sig+bkg
53  RooRealVar fsig("fsig","signal fraction",0.1,0.,1.) ;
54  RooAddPdf model("model","model",RooArgList(sig,bkg),fsig) ;
55 
56  // Generate large dataset
57  RooDataSet* data = model.generate(RooArgSet(x,y,z),200000) ;
58 
59 
60 
61  // P a r a l l e l f i t t i n g
62  // -------------------------------
63 
64  // In parallel mode the likelihood calculation is split in N pieces,
65  // that are calculated in parallel and added a posteriori before passing
66  // it back to MINUIT.
67 
68  // Use four processes and time results both in wall time and CPU time
69  model.fitTo(*data,NumCPU(4),Timer(kTRUE)) ;
70 
71 
72 
73  // P a r a l l e l M C p r o j e c t i o n s
74  // ----------------------------------------------
75 
76  // Construct signal, total likelihood projection on (y,z) observables and likelihood ratio
77  RooAbsPdf* sigyz = sig.createProjection(x) ;
78  RooAbsPdf* totyz = model.createProjection(x) ;
79  RooFormulaVar llratio_func("llratio","log10(@0)-log10(@1)",RooArgList(*sigyz,*totyz)) ;
80 
81  // Calculate likelihood ratio for each event, define subset of events with high signal likelihood
82  data->addColumn(llratio_func) ;
83  RooDataSet* dataSel = (RooDataSet*) data->reduce(Cut("llratio>0.7")) ;
84 
85  // Make plot frame and plot data
86  RooPlot* frame = x.frame(Title("Projection on X with LLratio(y,z)>0.7"),Bins(40)) ;
87  dataSel->plotOn(frame) ;
88 
89  // Perform parallel projection using MC integration of pdf using given input dataSet.
90  // In this mode the data-weighted average of the pdf is calculated by splitting the
91  // input dataset in N equal pieces and calculating in parallel the weighted average
92  // one each subset. The N results of those calculations are then weighted into the
93  // final result
94 
95  // Use four processes
96  model.plotOn(frame,ProjWData(*dataSel),NumCPU(4)) ;
97 
98 
99  new TCanvas("rf603_multicpu","rf603_multicpu",600,600) ;
100  gPad->SetLeftMargin(0.15) ; frame->GetYaxis()->SetTitleOffset(1.6) ; frame->Draw() ;
101 
102 }
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:245
Float_t pz
Definition: hprod.C:33
RooCmdArg Cut(const char *cutSpec)
RooProdPdf is an efficient implementation of a product of PDFs of the form.
Definition: RooProdPdf.h:31
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1118
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:399
RooCmdArg ProjWData(const RooAbsData &projData, Bool_t binData=kFALSE)
Float_t py
Definition: hprod.C:33
RooCmdArg Title(const char *name)
Double_t x[n]
Definition: legend1.C:17
virtual RooAbsArg * addColumn(RooAbsArg &var, Bool_t adjustRange=kTRUE)
Add a column with the values of the given (function) argument to this dataset.
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
RooCmdArg NumCPU(Int_t nCPU, Int_t interleave=0)
friend class RooArgSet
Definition: RooAbsArg.h:469
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
Plot dataset on specified frame.
Definition: RooAbsData.cxx:626
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:2989
Float_t z[5]
Definition: Ifit.C:16
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
RooCmdArg Timer(Bool_t flag=kTRUE)
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
A RooPlot is a plot frame and a container for graphics objects within that frame. ...
Definition: RooPlot.h:41
Double_t y[n]
Definition: legend1.C:17
Float_t px
Definition: hprod.C:33
RooConstVar & RooConst(Double_t val)
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
RooCmdArg Bins(Int_t nbin)
#define gPad
Definition: TVirtualPad.h:288
RooPolynomial implements a polynomial p.d.f of the form By default coefficient a_0 is chosen to be 1...
Definition: RooPolynomial.h:28
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:559