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