Logo ROOT  
Reference Guide
rf203_ranges.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 ///
5 /// Fitting and plotting in sub ranges.
6 ///
7 /// \macro_image
8 /// \macro_output
9 /// \macro_code
10 ///
11 /// \date 07/2008
12 /// \author Wouter Verkerke
13 
14 #include "RooRealVar.h"
15 #include "RooDataSet.h"
16 #include "RooGaussian.h"
17 #include "RooConstVar.h"
18 #include "RooPolynomial.h"
19 #include "RooAddPdf.h"
20 #include "RooFitResult.h"
21 #include "RooPlot.h"
22 #include "TCanvas.h"
23 #include "TAxis.h"
24 #include "TH1.h"
25 using namespace RooFit;
26 
27 void rf203_ranges()
28 {
29  // S e t u p m o d e l
30  // ---------------------
31 
32  // Construct observables x
33  RooRealVar x("x", "x", -10, 10);
34 
35  // Construct gaussx(x,mx,1)
36  RooRealVar mx("mx", "mx", 0, -10, 10);
37  RooGaussian gx("gx", "gx", x, mx, RooConst(1));
38 
39  // Construct px = 1 (flat in x)
40  RooPolynomial px("px", "px", x);
41 
42  // Construct model = f*gx + (1-f)px
43  RooRealVar f("f", "f", 0., 1.);
44  RooAddPdf model("model", "model", RooArgList(gx, px), f);
45 
46  // Generated 10000 events in (x,y) from p.d.f. model
47  RooDataSet *modelData = model.generate(x, 10000);
48 
49  // F i t f u l l r a n g e
50  // ---------------------------
51 
52  // Fit p.d.f to all data
53  RooFitResult *r_full = model.fitTo(*modelData, Save(kTRUE));
54 
55  // F i t p a r t i a l r a n g e
56  // ----------------------------------
57 
58  // Define "signal" range in x as [-3,3]
59  x.setRange("signal", -3, 3);
60 
61  // Fit p.d.f only to data in "signal" range
62  RooFitResult *r_sig = model.fitTo(*modelData, Save(kTRUE), Range("signal"));
63 
64  // P l o t / p r i n t r e s u l t s
65  // ---------------------------------------
66 
67  // Make plot frame in x and add data and fitted model
68  RooPlot *frame = x.frame(Title("Fitting a sub range"));
69  modelData->plotOn(frame);
70  model.plotOn(frame, Range("Full"), LineStyle(kDashed), LineColor(kRed)); // Add shape in full ranged dashed
71  model.plotOn(frame); // By default only fitted range is shown
72 
73  // Print fit results
74  cout << "result of fit on all data " << endl;
75  r_full->Print();
76  cout << "result of fit in in signal region (note increased error on signal fraction)" << endl;
77  r_sig->Print();
78 
79  // Draw frame on canvas
80  new TCanvas("rf203_ranges", "rf203_ranges", 600, 600);
81  gPad->SetLeftMargin(0.15);
82  frame->GetYaxis()->SetTitleOffset(1.4);
83  frame->Draw();
84 
85  return;
86 }
RooPlot::Draw
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:691
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooAddPdf
Definition: RooAddPdf.h:32
f
#define f(i)
Definition: RSha256.hxx:122
RooArgList
Definition: RooArgList.h:21
RooGaussian.h
rf203_ranges
Definition: rf203_ranges.py:1
x
Double_t x[n]
Definition: legend1.C:17
RooGaussian
Definition: RooGaussian.h:25
RooAddPdf.h
TCanvas.h
RooDataSet.h
RooFitResult
Definition: RooFitResult.h:40
RooPolynomial.h
RooFit
Definition: RooCFunction1Binding.h:29
RooPolynomial
Definition: RooPolynomial.h:28
RooAbsData::plotOn
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
Definition: RooAbsData.cxx:547
RooPlot.h
RooPlot::GetYaxis
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1256
RooPlot
Definition: RooPlot.h:44
RooRealVar.h
kRed
@ kRed
Definition: Rtypes.h:66
RooFitResult.h
RooConstVar.h
RooFit::LineColor
RooCmdArg LineColor(Color_t color)
Definition: RooGlobalFunc.cxx:56
TCanvas
Definition: TCanvas.h:23
TAxis.h
RooFit::Range
RooCmdArg Range(const char *rangeName, Bool_t adjustNorm=kTRUE)
Definition: RooGlobalFunc.cxx:52
RooFitResult::Print
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooFitResult.h:66
kDashed
@ kDashed
Definition: TAttLine.h:48
gPad
#define gPad
Definition: TVirtualPad.h:287
RooDataSet
Definition: RooDataSet.h:33
make_cnn_model.model
model
Definition: make_cnn_model.py:6
RooFit::LineStyle
RooCmdArg LineStyle(Style_t style)
Definition: RooGlobalFunc.cxx:57
TAttAxis::SetTitleOffset
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:293
RooRealVar
Definition: RooRealVar.h:35
RooFit::Save
RooCmdArg Save(Bool_t flag=kTRUE)
Definition: RooGlobalFunc.cxx:187
TH1.h
RooFit::Title
RooCmdArg Title(const char *name)
Definition: RooGlobalFunc.cxx:173
RooFit::RooConst
RooConstVar & RooConst(Double_t val)
Definition: RooGlobalFunc.cxx:341