 ROOT   Reference Guide rf212_plottingInRanges_blinding.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
4///
5/// Plot a PDF in disjunct ranges, and get normalisation right.
6///
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.
11///
12/// In this tutorial, we show how to explicitly choose the normalisation when plotting using NormRange().
13///
14/// Thanks to Marc Escalier for asking how to do this correctly.
15///
16/// \macro_image
17/// \macro_code
18/// \macro_output
19///
20/// \date 03/2020
21/// \author Stephan Hageboeck
22
23#include <RooDataSet.h>
24#include <RooExponential.h>
25#include <RooPlot.h>
26#include <RooRealVar.h>
27#include <TCanvas.h>
28
29using namespace RooFit;
30
31void rf212_plottingInRanges_blinding()
32{
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);
37
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);
42
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"));
46
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);
51
52
53 // Here we will plot the results
54 TCanvas *canvas=new TCanvas("canvas","canvas",800,600);
55 canvas->Divide(2,1);
56
57
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 // ----------------------------------------------------------------------------------------------------------
63
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"));
67
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"));
73
74 plotFrame->Draw();
75
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 // ----------------------------------------------------------------------------------------------------------
82
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"));
86
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));
92
93 plotFrameWithNormRange->Draw();
94
95 canvas->Draw();
96
97}
@ 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