Logo ROOT   6.16/01
Reference Guide
rf102_dataimport.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
4/// 'BASIC FUNCTIONALITY' RooFit tutorial macro #102
5///
6/// Importing data from ROOT TTrees and THx histograms
7///
8/// \macro_image
9/// \macro_output
10/// \macro_code
11/// \author 07/2008 - Wouter Verkerke
12
13
14#include "RooRealVar.h"
15#include "RooDataSet.h"
16#include "RooDataHist.h"
17#include "RooGaussian.h"
18#include "TCanvas.h"
19#include "RooPlot.h"
20#include "TTree.h"
21#include "TH1D.h"
22#include "TRandom.h"
23using namespace RooFit ;
24
25TH1* makeTH1() ;
26TTree* makeTTree() ;
27
28
29void rf102_dataimport()
30{
31 // ---------------------------------------------------
32 // I m p o r t i n g R O O T h i s t o g r a m s
33 // ===================================================
34
35 // I m p o r t T H 1 i n t o a R o o D a t a H i s t
36 // ---------------------------------------------------------
37
38 // Create a ROOT TH1 histogram
39 TH1* hh = makeTH1() ;
40
41 // Declare observable x
42 RooRealVar x("x","x",-10,10) ;
43
44 // Create a binned dataset that imports contents of TH1 and associates its contents to observable 'x'
45 RooDataHist dh("dh","dh",x,Import(*hh)) ;
46
47
48 // P l o t a n d f i t a R o o D a t a H i s t
49 // ---------------------------------------------------
50
51 // Make plot of binned dataset showing Poisson error bars (RooFit default)
52 RooPlot* frame = x.frame(Title("Imported TH1 with Poisson error bars")) ;
53 dh.plotOn(frame) ;
54
55 // Fit a Gaussian p.d.f to the data
56 RooRealVar mean("mean","mean",0,-10,10) ;
57 RooRealVar sigma("sigma","sigma",3,0.1,10) ;
58 RooGaussian gauss("gauss","gauss",x,mean,sigma) ;
59 gauss.fitTo(dh) ;
60 gauss.plotOn(frame) ;
61
62 // P l o t a n d f i t a R o o D a t a H i s t w i t h i n t e r n a l e r r o r s
63 // ---------------------------------------------------------------------------------------------
64
65 // If histogram has custom error (i.e. its contents is does not originate from a Poisson process
66 // but e.g. is a sum of weighted events) you can data with symmetric 'sum-of-weights' error instead
67 // (same error bars as shown by ROOT)
68 RooPlot* frame2 = x.frame(Title("Imported TH1 with internal errors")) ;
69 dh.plotOn(frame2,DataError(RooAbsData::SumW2)) ;
70 gauss.plotOn(frame2) ;
71
72 // Please note that error bars shown (Poisson or SumW2) are for visualization only, the are NOT used
73 // in a maximum likelihood fit
74 //
75 // A (binned) ML fit will ALWAYS assume the Poisson error interpretation of data (the mathematical definition
76 // of likelihood does not take any external definition of errors). Data with non-unit weights can only be correctly
77 // fitted with a chi^2 fit (see rf602_chi2fit.C)
78
79
80 // -----------------------------------------
81 // I m p o r t i n g R O O T T T r e e s
82 // =========================================
83
84
85 // I m p o r t T T r e e i n t o a R o o D a t a S e t
86 // -----------------------------------------------------------
87
88 TTree* tree = makeTTree() ;
89
90 // Define 2nd observable y
91 RooRealVar y("y","y",-10,10) ;
92
93 // Construct unbinned dataset importing tree branches x and y matching between branches and RooRealVars
94 // is done by name of the branch/RRV
95 //
96 // Note that ONLY entries for which x,y have values within their allowed ranges as defined in
97 // RooRealVar x and y are imported. Since the y values in the import tree are in the range [-15,15]
98 // and RRV y defines a range [-10,10] this means that the RooDataSet below will have less entries than the TTree 'tree'
99
100 RooDataSet ds("ds","ds",RooArgSet(x,y),Import(*tree)) ;
101
102
103
104 // U s e a s c i i i m p o r t / e x p o r t f o r d a t a s e t s
105 // ------------------------------------------------------------------------------------
106 {
107 // Write data to output stream
108 std::ofstream outstream("/tmp/rf102_testData.txt");
109 // Optionally, adjust the stream here (e.g. std::setprecision)
110 ds.write(outstream);
111 outstream.close();
112 }
113
114
115 //Read data from input stream. The variables of the dataset need to be supplied
116 //to the RooDataSet::read() function.
117 std::cout << "\n-----------------------\nReading data from ASCII\n";
118 RooDataSet * dataReadBack = RooDataSet::read("/tmp/rf102_testData.txt",
119 RooArgList(x, y), //variables to be read. If the file has more fields, these are ignored.
120 "D"); //Prints if a RooFit message stream listens for debug messages. Use Q for quiet.
121
122 dataReadBack->Print("V");
123
124 std::cout << "\nOriginal data, line 20:\n";
125 ds.get(20)->Print("V");
126
127 std::cout << "\nRead-back data, line 20:\n";
128 dataReadBack->get(20)->Print("V");
129
130
131
132 // P l o t d a t a s e t s w i t h m u l t i p l e b i n n i n g c h o i c e s
133 // ------------------------------------------------------------------------------------
134
135 // Print number of events in dataset
136 ds.Print() ;
137
138 // Print unbinned dataset with default frame binning (100 bins)
139 RooPlot* frame3 = y.frame(Title("Unbinned data shown in default frame binning")) ;
140 ds.plotOn(frame3) ;
141
142 // Print unbinned dataset with custom binning choice (20 bins)
143 RooPlot* frame4 = y.frame(Title("Unbinned data shown with custom binning")) ;
144 ds.plotOn(frame4,Binning(20)) ;
145
146 RooPlot* frame5 = y.frame(Title("Unbinned data read back from ASCII file")) ;
147 ds.plotOn(frame5,Binning(20)) ;
148 dataReadBack->plotOn(frame5, Binning(20), MarkerColor(kRed), MarkerStyle(5));
149
150 // Draw all frames on a canvas
151 TCanvas* c = new TCanvas("rf102_dataimport","rf102_dataimport",1000,800) ;
152 c->Divide(3,2) ;
153 c->cd(1) ; gPad->SetLeftMargin(0.15) ; frame->GetYaxis()->SetTitleOffset(1.4) ; frame->Draw() ;
154 c->cd(2) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.4) ; frame2->Draw() ;
155
156 c->cd(4) ; gPad->SetLeftMargin(0.15) ; frame3->GetYaxis()->SetTitleOffset(1.4) ; frame3->Draw() ;
157 c->cd(5) ; gPad->SetLeftMargin(0.15) ; frame4->GetYaxis()->SetTitleOffset(1.4) ; frame4->Draw() ;
158 c->cd(6) ; gPad->SetLeftMargin(0.15) ; frame4->GetYaxis()->SetTitleOffset(1.4) ; frame5->Draw() ;
159
160}
161
162
163
164
165TH1* makeTH1()
166{
167 // Create ROOT TH1 filled with a Gaussian distribution
168
169 TH1D* hh = new TH1D("hh","hh",25,-10,10) ;
170 for (int i=0 ; i<100 ; i++) {
171 hh->Fill(gRandom->Gaus(0,3)) ;
172 }
173 return hh ;
174}
175
176
177TTree* makeTTree()
178{
179 // Create ROOT TTree filled with a Gaussian distribution in x and a uniform distribution in y
180
181 TTree* tree = new TTree("tree","tree") ;
182 Double_t* px = new Double_t ;
183 Double_t* py = new Double_t ;
184 tree->Branch("x",px,"x/D") ;
185 tree->Branch("y",py,"y/D") ;
186 for (int i=0 ; i<100 ; i++) {
187 *px = gRandom->Gaus(0,3) ;
188 *py = gRandom->Uniform()*30 - 15 ;
189 tree->Fill() ;
190 }
191 return tree ;
192}
193
194
#define c(i)
Definition: RSha256.hxx:101
double Double_t
Definition: RtypesCore.h:55
@ kRed
Definition: Rtypes.h:63
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
#define gPad
Definition: TVirtualPad.h:286
virtual void Print(Option_t *options=0) const
This method must be overridden when a class wants to print itself.
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooAbsData.h:161
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
Calls RooPlot* plotOn(RooPlot* frame, const RooLinkedList& cmdList) const ;.
Definition: RooAbsData.cxx:531
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
RooDataSet is a container class to hold N-dimensional binned data.
Definition: RooDataHist.h:40
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:31
virtual const RooArgSet * get(Int_t index) const
Return RooArgSet with coordinates of event 'index'.
Definition: RooDataSet.cxx:995
static RooDataSet * read(const char *filename, const RooArgList &variables, const char *opts="", const char *commonPath="", const char *indexCatName=0)
Read given list of ascii files, and construct a data set, using the given ArgList as structure defini...
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition: RooPlot.h:41
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1123
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:558
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:294
The Canvas class.
Definition: TCanvas.h:31
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:614
The TH1 histogram class.
Definition: TH1.h:56
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3251
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:256
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:627
const Double_t sigma
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
RooCmdArg MarkerColor(Color_t color)
RooCmdArg Binning(const RooAbsBinning &binning)
RooCmdArg MarkerStyle(Style_t style)
RooCmdArg Import(const char *state, TH1 &histo)
RooCmdArg DataError(Int_t)
static constexpr double gauss
Definition: tree.py:1
const char * Title
Definition: TXMLSetup.cxx:67