Logo ROOT  
Reference Guide
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
5/// Plot a PDF in disjunct ranges, and get normalisation right.
7/// Usually, when comparing a fit to data, one should first plot the data, and then the PDF.
8/// In this case, the PDF is automatically normalised to match the number of data events in the plot.
9/// However, when plotting only a sub-range, when e.g. a signal region has to be blinded,
10/// one has to exclude the blinded region from the computation of the normalisation.
12/// In this tutorial, we show how to explicitly choose the normalisation when plotting using `NormRange()`.
14/// Thanks to Marc Escalier for asking how to do this correctly.
16/// \macro_image
17/// \macro_code
18/// \macro_output
20/// \date 03/2020
21/// \author Stephan Hageboeck
23#include <RooDataSet.h>
24#include <RooExponential.h>
25#include <RooPlot.h>
26#include <RooRealVar.h>
27#include <TCanvas.h>
29using namespace RooFit;
31void rf212_plottingInRanges_blinding()
33 // Make a fit model
34 RooRealVar x("x", "The observable", 1, 30);
35 RooRealVar tau("tau", "The exponent", -0.1337, -10., -0.1);
36 RooExponential exp("exp", "A falling exponential function", x, tau);
38 // Define the sidebands (e.g. background regions)
39 x.setRange("full", 1, 30);
40 x.setRange("left", 1, 10);
41 x.setRange("right", 20, 30);
43 // Generate toy data, and cut out the blinded region.
44 RooDataSet* data = exp.generate(x, 1000);
45 auto blindedData = data->reduce(CutRange("left,right"));
47 // Kick tau a bit, and run an unbinned fit where the blinded data are missing.
48 // ----------------------------------------------------------------------------------------------------------
49 tau.setVal(-2.);
50 exp.fitTo(*blindedData);
53 // Here we will plot the results
54 TCanvas *canvas=new TCanvas("canvas","canvas",800,600);
55 canvas->Divide(2,1);
58 // Wrong:
59 // Plotting each slice on its own normalises the PDF over its plotting range. For the full curve, that means
60 // that the blinded region where data is missing is included in the normalisation calculation. The PDF therefore
61 // comes out too low, and doesn't match up with the slices in the side bands, which are normalised to "their" data.
62 // ----------------------------------------------------------------------------------------------------------
64 std::cout << "Now plotting with unique normalisation for each slice." << std::endl;
65 canvas->cd(1);
66 RooPlot* plotFrame = x.frame(RooFit::Title("Wrong: Each slice normalised over its plotting range"));
68 // Plot only the blinded data, and then plot the PDF over the full range as well as both sidebands
69 blindedData->plotOn(plotFrame);
70 exp.plotOn(plotFrame, LineColor(kRed), Range("full"));
71 exp.plotOn(plotFrame, LineColor(kBlue), Range("left"));
72 exp.plotOn(plotFrame, LineColor(kGreen), Range("right"));
74 plotFrame->Draw();
76 // Right:
77 // Make the same plot, but normalise each piece with respect to the regions "left" AND "right". This requires setting
78 // a "NormRange", which tells RooFit over which range the PDF has to be integrated to normalise.
79 // This is means that the normalisation of the blue and green curves is slightly different from the left plot,
80 // because they get a common scale factor.
81 // ----------------------------------------------------------------------------------------------------------
83 std::cout << "\n\nNow plotting with correct norm ranges:" << std::endl;
84 canvas->cd(2);
85 RooPlot* plotFrameWithNormRange = x.frame(RooFit::Title("Right: All slices have common normalisation"));
87 // Plot only the blinded data, and then plot the PDF over the full range as well as both sidebands
88 blindedData->plotOn(plotFrameWithNormRange);
89 exp.plotOn(plotFrameWithNormRange, LineColor(kBlue), Range("left"), RooFit::NormRange("left,right"));
90 exp.plotOn(plotFrameWithNormRange, LineColor(kGreen), Range("right"), RooFit::NormRange("left,right"));
91 exp.plotOn(plotFrameWithNormRange, LineColor(kRed), Range("full"), RooFit::NormRange("left,right"), LineStyle(10));
93 plotFrameWithNormRange->Draw();
95 canvas->Draw();
@ kRed
Definition: Rtypes.h:64
@ kGreen
Definition: Rtypes.h:64
@ kBlue
Definition: Rtypes.h:64
double exp(double)
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
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
Exponential PDF.
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition: RooPlot.h:44
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:712
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:35
The Canvas class.
Definition: TCanvas.h:27
virtual void Draw(Option_t *option="")
Draw a canvas.
Definition: TCanvas.cxx:838
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:701
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1165
RooCmdArg Title(const char *name)
RooCmdArg NormRange(const char *rangeNameList)
RooCmdArg CutRange(const char *rangeName)
RooCmdArg LineColor(Color_t color)
RooCmdArg LineStyle(Style_t style)
Double_t x[n]
Definition: legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Ta Range(0, 0, 1, 1)