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