Logo ROOT  
Reference Guide
No Matches
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
4/// Fitting and plotting in sub ranges.
6/// \macro_image
7/// \macro_code
8/// \macro_output
10/// \date July 2008
11/// \author Wouter Verkerke
13#include "RooRealVar.h"
14#include "RooDataSet.h"
15#include "RooGaussian.h"
16#include "RooPolynomial.h"
17#include "RooAddPdf.h"
18#include "RooFitResult.h"
19#include "RooPlot.h"
20#include "TCanvas.h"
21#include "TAxis.h"
22#include "TH1.h"
23using namespace RooFit;
25void rf203_ranges()
27 // S e t u p m o d e l
28 // ---------------------
30 // Construct observables x
31 RooRealVar x("x", "x", -10, 10);
33 // Construct gaussx(x,mx,1)
34 RooRealVar mx("mx", "mx", 0, -10, 10);
35 RooGaussian gx("gx", "gx", x, mx, 1.0);
37 // Construct px = 1 (flat in x)
38 RooPolynomial px("px", "px", x);
40 // Construct model = f*gx + (1-f)px
41 RooRealVar f("f", "f", 0., 1.);
42 RooAddPdf model("model", "model", RooArgList(gx, px), f);
44 // Generated 10000 events in (x,y) from pdf model
45 std::unique_ptr<RooDataSet> modelData{model.generate(x, 10000)};
47 // F i t f u l l r a n g e
48 // ---------------------------
50 // Fit pdf to all data
51 std::unique_ptr<RooFitResult> r_full{model.fitTo(*modelData, Save(true), PrintLevel(-1))};
53 // F i t p a r t i a l r a n g e
54 // ----------------------------------
56 // Define "signal" range in x as [-3,3]
57 x.setRange("signal", -3, 3);
59 // Fit pdf only to data in "signal" range
60 std::unique_ptr<RooFitResult> r_sig{model.fitTo(*modelData, Save(true), Range("signal"), PrintLevel(-1))};
62 // P l o t / p r i n t r e s u l t s
63 // ---------------------------------------
65 // Make plot frame in x and add data and fitted model
66 RooPlot *frame = x.frame(Title("Fitting a sub range"));
67 modelData->plotOn(frame);
68 model.plotOn(frame, Range(""), LineStyle(kDashed), LineColor(kRed)); // Add shape in full ranged dashed
69 model.plotOn(frame); // By default only fitted range is shown
71 // Print fit results
72 cout << "result of fit on all data " << endl;
73 r_full->Print();
74 cout << "result of fit in in signal region (note increased error on signal fraction)" << endl;
75 r_sig->Print();
77 // Draw frame on canvas
78 new TCanvas("rf203_ranges", "rf203_ranges", 600, 600);
79 gPad->SetLeftMargin(0.15);
80 frame->GetYaxis()->SetTitleOffset(1.4);
81 frame->Draw();
83 return;
#define f(i)
Definition RSha256.hxx:104
@ 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
Efficient implementation of a sum of PDFs of the form.
Definition RooAddPdf.h:33
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
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:237
void Print(Option_t *options=nullptr) const override
This method must be overridden when a class wants to print itself.
Definition RooPlot.h:134
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.
Variable that can be changed from the outside.
Definition RooRealVar.h:37
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
RooCmdArg Save(bool flag=true)
RooCmdArg PrintLevel(Int_t code)
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 JSONIO.h:26
const char * Title
Definition TXMLSetup.cxx:68
Ta Range(0, 0, 1, 1)