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