Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf203_ranges.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
4/// Fitting and plotting in sub ranges.
5///
6/// \macro_image
7/// \macro_code
8/// \macro_output
9///
10/// \date July 2008
11/// \author Wouter Verkerke
12
13#include "RooRealVar.h"
14#include "RooDataSet.h"
15#include "RooGaussian.h"
16#include "RooConstVar.h"
17#include "RooPolynomial.h"
18#include "RooAddPdf.h"
19#include "RooFitResult.h"
20#include "RooPlot.h"
21#include "TCanvas.h"
22#include "TAxis.h"
23#include "TH1.h"
24using namespace RooFit;
25
26void rf203_ranges()
27{
28 // S e t u p m o d e l
29 // ---------------------
30
31 // Construct observables x
32 RooRealVar x("x", "x", -10, 10);
33
34 // Construct gaussx(x,mx,1)
35 RooRealVar mx("mx", "mx", 0, -10, 10);
36 RooGaussian gx("gx", "gx", x, mx, RooConst(1));
37
38 // Construct px = 1 (flat in x)
39 RooPolynomial px("px", "px", x);
40
41 // Construct model = f*gx + (1-f)px
42 RooRealVar f("f", "f", 0., 1.);
43 RooAddPdf model("model", "model", RooArgList(gx, px), f);
44
45 // Generated 10000 events in (x,y) from pdf model
46 RooDataSet *modelData = model.generate(x, 10000);
47
48 // F i t f u l l r a n g e
49 // ---------------------------
50
51 // Fit pdf to all data
52 std::unique_ptr<RooFitResult> r_full{model.fitTo(*modelData, Save(true), PrintLevel(-1))};
53
54 // F i t p a r t i a l r a n g e
55 // ----------------------------------
56
57 // Define "signal" range in x as [-3,3]
58 x.setRange("signal", -3, 3);
59
60 // Fit pdf only to data in "signal" range
61 std::unique_ptr<RooFitResult> r_sig{model.fitTo(*modelData, Save(true), Range("signal"), PrintLevel(-1))};
62
63 // P l o t / p r i n t r e s u l t s
64 // ---------------------------------------
65
66 // Make plot frame in x and add data and fitted model
67 RooPlot *frame = x.frame(Title("Fitting a sub range"));
68 modelData->plotOn(frame);
69 model.plotOn(frame, Range(""), LineStyle(kDashed), LineColor(kRed)); // Add shape in full ranged dashed
70 model.plotOn(frame); // By default only fitted range is shown
71
72 // Print fit results
73 cout << "result of fit on all data " << endl;
74 r_full->Print();
75 cout << "result of fit in in signal region (note increased error on signal fraction)" << endl;
76 r_sig->Print();
77
78 // Draw frame on canvas
79 new TCanvas("rf203_ranges", "rf203_ranges", 600, 600);
80 gPad->SetLeftMargin(0.15);
81 frame->GetYaxis()->SetTitleOffset(1.4);
82 frame->Draw();
83
84 return;
85}
#define f(i)
Definition RSha256.hxx:104
PrintLevel
Definition RooMinuit.h:6
@ kRed
Definition Rtypes.h:66
@ kDashed
Definition TAttLine.h:48
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char mx
#define gPad
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
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition RooAddPdf.h:34
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooDataSet is a container class to hold unbinned data.
Definition RooDataSet.h:57
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:43
static RooPlot * frame(const RooAbsRealLValue &var, double xmin, double xmax, Int_t nBins)
Create a new frame for a given variable in x.
Definition RooPlot.cxx:239
void Print(Option_t *options=nullptr) const override
This method must be overridden when a class wants to print itself.
Definition RooPlot.h:132
TAxis * GetYaxis() const
Definition RooPlot.cxx:1276
void Draw(Option_t *options=nullptr) override
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:649
RooPolynomial implements a polynomial p.d.f of the form.
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:40
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:298
The Canvas class.
Definition TCanvas.h:23
RooConstVar & RooConst(double val)
RooCmdArg Save(bool flag=true)
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...
Definition Common.h:18
const char * Title
Definition TXMLSetup.cxx:68
Ta Range(0, 0, 1, 1)