Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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"
20 using namespace RooFit;
21 
22 TH1 *makeTH1();
23 TTree *makeTTree();
24 
25 void 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("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("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 
166 TH1 *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 
177 TTree *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 }