ROOT logo

From $ROOTSYS/tutorials/roofit/rf603_multicpu.C

//////////////////////////////////////////////////////////////////////////
//
// 'LIKELIHOOD AND MINIMIZATION' RooFit tutorial macro #603
// 
// Setting up a multi-core parallelized unbinned maximum likelihood fit
//
//
//
// 07/2008 - Wouter Verkerke 
// 
/////////////////////////////////////////////////////////////////////////

#ifndef __CINT__
#include "RooGlobalFunc.h"
#endif
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooConstVar.h"
#include "RooPolynomial.h"
#include "RooAddPdf.h"
#include "RooProdPdf.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"
using namespace RooFit ;


void rf603_multicpu()
{

  // C r e a t e   3 D   p d f   a n d   d a t a 
  // -------------------------------------------

  // Create observables
  RooRealVar x("x","x",-5,5) ;
  RooRealVar y("y","y",-5,5) ;
  RooRealVar z("z","z",-5,5) ;

  // Create signal pdf gauss(x)*gauss(y)*gauss(z) 
  RooGaussian gx("gx","gx",x,RooConst(0),RooConst(1)) ;
  RooGaussian gy("gy","gy",y,RooConst(0),RooConst(1)) ;
  RooGaussian gz("gz","gz",z,RooConst(0),RooConst(1)) ;
  RooProdPdf sig("sig","sig",RooArgSet(gx,gy,gz)) ;

  // Create background pdf poly(x)*poly(y)*poly(z) 
  RooPolynomial px("px","px",x,RooArgSet(RooConst(-0.1),RooConst(0.004))) ;
  RooPolynomial py("py","py",y,RooArgSet(RooConst(0.1),RooConst(-0.004))) ;
  RooPolynomial pz("pz","pz",z) ;
  RooProdPdf bkg("bkg","bkg",RooArgSet(px,py,pz)) ;

  // Create composite pdf sig+bkg
  RooRealVar fsig("fsig","signal fraction",0.1,0.,1.) ;
  RooAddPdf model("model","model",RooArgList(sig,bkg),fsig) ;

  // Generate large dataset
  RooDataSet* data = model.generate(RooArgSet(x,y,z),200000) ;



  // P a r a l l e l   f i t t i n g 
  // -------------------------------

  // In parallel mode the likelihood calculation is split in N pieces,
  // that are calculated in parallel and added a posteriori before passing
  // it back to MINUIT.

  // Use four processes and time results both in wall time and CPU time
  model.fitTo(*data,NumCPU(4),Timer(kTRUE)) ;



  // P a r a l l e l   M C   p r o j e c t i o n s 
  // ----------------------------------------------

  // Construct signal, total likelihood projection on (y,z) observables and likelihood ratio
  RooAbsPdf* sigyz = sig.createProjection(x) ;
  RooAbsPdf* totyz = model.createProjection(x) ;
  RooFormulaVar llratio_func("llratio","log10(@0)-log10(@1)",RooArgList(*sigyz,*totyz)) ;

  // Calculate likelihood ratio for each event, define subset of events with high signal likelihood
  data->addColumn(llratio_func) ;
  RooDataSet* dataSel = (RooDataSet*) data->reduce(Cut("llratio>0.7")) ;

  // Make plot frame and plot data
  RooPlot* frame = x.frame(Title("Projection on X with LLratio(y,z)>0.7"),Bins(40)) ;
  dataSel->plotOn(frame) ;

  // Perform parallel projection using MC integration of pdf using given input dataSet. 
  // In this mode the data-weighted average of the pdf is calculated by splitting the
  // input dataset in N equal pieces and calculating in parallel the weighted average
  // one each subset. The N results of those calculations are then weighted into the
  // final result
  
  // Use four processes
  model.plotOn(frame,ProjWData(*dataSel),NumCPU(4)) ;


  new TCanvas("rf603_multicpu","rf603_multicpu",600,600) ;
  gPad->SetLeftMargin(0.15) ; frame->GetYaxis()->SetTitleOffset(1.6) ; frame->Draw() ;

}
 rf603_multicpu.C:1
 rf603_multicpu.C:2
 rf603_multicpu.C:3
 rf603_multicpu.C:4
 rf603_multicpu.C:5
 rf603_multicpu.C:6
 rf603_multicpu.C:7
 rf603_multicpu.C:8
 rf603_multicpu.C:9
 rf603_multicpu.C:10
 rf603_multicpu.C:11
 rf603_multicpu.C:12
 rf603_multicpu.C:13
 rf603_multicpu.C:14
 rf603_multicpu.C:15
 rf603_multicpu.C:16
 rf603_multicpu.C:17
 rf603_multicpu.C:18
 rf603_multicpu.C:19
 rf603_multicpu.C:20
 rf603_multicpu.C:21
 rf603_multicpu.C:22
 rf603_multicpu.C:23
 rf603_multicpu.C:24
 rf603_multicpu.C:25
 rf603_multicpu.C:26
 rf603_multicpu.C:27
 rf603_multicpu.C:28
 rf603_multicpu.C:29
 rf603_multicpu.C:30
 rf603_multicpu.C:31
 rf603_multicpu.C:32
 rf603_multicpu.C:33
 rf603_multicpu.C:34
 rf603_multicpu.C:35
 rf603_multicpu.C:36
 rf603_multicpu.C:37
 rf603_multicpu.C:38
 rf603_multicpu.C:39
 rf603_multicpu.C:40
 rf603_multicpu.C:41
 rf603_multicpu.C:42
 rf603_multicpu.C:43
 rf603_multicpu.C:44
 rf603_multicpu.C:45
 rf603_multicpu.C:46
 rf603_multicpu.C:47
 rf603_multicpu.C:48
 rf603_multicpu.C:49
 rf603_multicpu.C:50
 rf603_multicpu.C:51
 rf603_multicpu.C:52
 rf603_multicpu.C:53
 rf603_multicpu.C:54
 rf603_multicpu.C:55
 rf603_multicpu.C:56
 rf603_multicpu.C:57
 rf603_multicpu.C:58
 rf603_multicpu.C:59
 rf603_multicpu.C:60
 rf603_multicpu.C:61
 rf603_multicpu.C:62
 rf603_multicpu.C:63
 rf603_multicpu.C:64
 rf603_multicpu.C:65
 rf603_multicpu.C:66
 rf603_multicpu.C:67
 rf603_multicpu.C:68
 rf603_multicpu.C:69
 rf603_multicpu.C:70
 rf603_multicpu.C:71
 rf603_multicpu.C:72
 rf603_multicpu.C:73
 rf603_multicpu.C:74
 rf603_multicpu.C:75
 rf603_multicpu.C:76
 rf603_multicpu.C:77
 rf603_multicpu.C:78
 rf603_multicpu.C:79
 rf603_multicpu.C:80
 rf603_multicpu.C:81
 rf603_multicpu.C:82
 rf603_multicpu.C:83
 rf603_multicpu.C:84
 rf603_multicpu.C:85
 rf603_multicpu.C:86
 rf603_multicpu.C:87
 rf603_multicpu.C:88
 rf603_multicpu.C:89
 rf603_multicpu.C:90
 rf603_multicpu.C:91
 rf603_multicpu.C:92
 rf603_multicpu.C:93
 rf603_multicpu.C:94
 rf603_multicpu.C:95
 rf603_multicpu.C:96
 rf603_multicpu.C:97
 rf603_multicpu.C:98
 rf603_multicpu.C:99
 rf603_multicpu.C:100
 rf603_multicpu.C:101
 rf603_multicpu.C:102
 rf603_multicpu.C:103