## From \$ROOTSYS/tutorials/roofit/rf102_dataimport.C

```//////////////////////////////////////////////////////////////////////////
//
// 'BASIC FUNCTIONALITY' RooFit tutorial macro #102
//
// Importing data from ROOT TTrees and THx histograms
//
//
//
// 07/2008 - Wouter Verkerke
//
/////////////////////////////////////////////////////////////////////////

#ifndef __CINT__
#include "RooGlobalFunc.h"
#endif
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooDataHist.h"
#include "RooGaussian.h"
#include "TCanvas.h"
#include "RooPlot.h"
#include "TTree.h"
#include "TH1D.h"
#include "TRandom.h"
using namespace RooFit ;

TH1* makeTH1() ;
TTree* makeTTree() ;

void rf102_dataimport()
{
////////////////////////////////////////////////////////
// I m p o r t i n g   R O O T   h i s t o g r a m s  //
////////////////////////////////////////////////////////

// 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
// ---------------------------------------------------------

// Create a ROOT TH1 histogram
TH1* hh = makeTH1() ;

// Declare observable x
RooRealVar x("x","x",-10,10) ;

// Create a binned dataset that imports contents of TH1 and associates its contents to observable 'x'
RooDataHist dh("dh","dh",x,Import(*hh)) ;

// P l o t   a n d   f i t   a   R o o D a t a H i s t
// ---------------------------------------------------

// Make plot of binned dataset showing Poisson error bars (RooFit default)
RooPlot* frame = x.frame(Title("Imported TH1 with Poisson error bars")) ;
dh.plotOn(frame) ;

// Fit a Gaussian p.d.f to the data
RooRealVar mean("mean","mean",0,-10,10) ;
RooRealVar sigma("sigma","sigma",3,0.1,10) ;
RooGaussian gauss("gauss","gauss",x,mean,sigma) ;
gauss.fitTo(dh) ;
gauss.plotOn(frame) ;

// 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
// ---------------------------------------------------------------------------------------------

// If histogram has custom error (i.e. its contents is does not originate from a Poisson process
// but e.g. is a sum of weighted events) you can data with symmetric 'sum-of-weights' error instead
// (same error bars as shown by ROOT)
RooPlot* frame2 = x.frame(Title("Imported TH1 with internal errors")) ;
dh.plotOn(frame2,DataError(RooAbsData::SumW2)) ;
gauss.plotOn(frame2) ;

// Please note that error bars shown (Poisson or SumW2) are for visualization only, the are NOT used
// in a maximum likelihood fit
//
// A (binned) ML fit will ALWAYS assume the Poisson error interpretation of data (the mathematical definition
// of likelihood does not take any external definition of errors). Data with non-unit weights can only be correctly
// fitted with a chi^2 fit (see rf602_chi2fit.C)

////////////////////////////////////////////////
// I m p o r t i n g   R O O T  T T r e e s   //
////////////////////////////////////////////////

// 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
// -----------------------------------------------------------

TTree* tree = makeTTree() ;

// Define 2nd observable y
RooRealVar y("y","y",-10,10) ;

// Construct unbinned dataset importing tree branches x and y matching between branches and RooRealVars
// is done by name of the branch/RRV
//
// Note that ONLY entries for which x,y have values within their allowed ranges as defined in
// RooRealVar x and y are imported. Since the y values in the import tree are in the range [-15,15]
// and RRV y defines a range [-10,10] this means that the RooDataSet below will have less entries than the TTree 'tree'

RooDataSet ds("ds","ds",RooArgSet(x,y),Import(*tree)) ;

// P l o t   d a t a s e t   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
// ------------------------------------------------------------------------------------

// Print number of events in dataset
ds.Print() ;

// Print unbinned dataset with default frame binning (100 bins)
RooPlot* frame3 = y.frame(Title("Unbinned data shown in default frame binning")) ;
ds.plotOn(frame3) ;

// Print unbinned dataset with custom binning choice (20 bins)
RooPlot* frame4 = y.frame(Title("Unbinned data shown with custom binning")) ;
ds.plotOn(frame4,Binning(20)) ;

// Draw all frames on a canvas
TCanvas* c = new TCanvas("rf102_dataimport","rf102_dataimport",800,800) ;
c->Divide(2,2) ;
c->cd(1) ; gPad->SetLeftMargin(0.15) ; frame->GetYaxis()->SetTitleOffset(1.4) ; frame->Draw() ;
c->cd(2) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.4) ; frame2->Draw() ;
c->cd(3) ; gPad->SetLeftMargin(0.15) ; frame3->GetYaxis()->SetTitleOffset(1.4) ; frame3->Draw() ;
c->cd(4) ; gPad->SetLeftMargin(0.15) ; frame4->GetYaxis()->SetTitleOffset(1.4) ; frame4->Draw() ;

}

TH1* makeTH1()
{
// Create ROOT TH1 filled with a Gaussian distribution

TH1D* hh = new TH1D("hh","hh",25,-10,10) ;
for (int i=0 ; i<100 ; i++) {
hh->Fill(gRandom->Gaus(0,3)) ;
}
return hh ;
}

TTree* makeTTree()
{
// Create ROOT TTree filled with a Gaussian distribution in x and a uniform distribution in y

TTree* tree = new TTree("tree","tree") ;
Double_t* px = new Double_t ;
Double_t* py = new Double_t ;
tree->Branch("x",px,"x/D") ;
tree->Branch("y",py,"y/D") ;
for (int i=0 ; i<100 ; i++) {
*px = gRandom->Gaus(0,3) ;
*py = gRandom->Uniform()*30 - 15 ;
tree->Fill() ;
}
return tree ;
}

```
rf102_dataimport.C:1
rf102_dataimport.C:2
rf102_dataimport.C:3
rf102_dataimport.C:4
rf102_dataimport.C:5
rf102_dataimport.C:6
rf102_dataimport.C:7
rf102_dataimport.C:8
rf102_dataimport.C:9
rf102_dataimport.C:10
rf102_dataimport.C:11
rf102_dataimport.C:12
rf102_dataimport.C:13
rf102_dataimport.C:14
rf102_dataimport.C:15
rf102_dataimport.C:16
rf102_dataimport.C:17
rf102_dataimport.C:18
rf102_dataimport.C:19
rf102_dataimport.C:20
rf102_dataimport.C:21
rf102_dataimport.C:22
rf102_dataimport.C:23
rf102_dataimport.C:24
rf102_dataimport.C:25
rf102_dataimport.C:26
rf102_dataimport.C:27
rf102_dataimport.C:28
rf102_dataimport.C:29
rf102_dataimport.C:30
rf102_dataimport.C:31
rf102_dataimport.C:32
rf102_dataimport.C:33
rf102_dataimport.C:34
rf102_dataimport.C:35
rf102_dataimport.C:36
rf102_dataimport.C:37
rf102_dataimport.C:38
rf102_dataimport.C:39
rf102_dataimport.C:40
rf102_dataimport.C:41
rf102_dataimport.C:42
rf102_dataimport.C:43
rf102_dataimport.C:44
rf102_dataimport.C:45
rf102_dataimport.C:46
rf102_dataimport.C:47
rf102_dataimport.C:48
rf102_dataimport.C:49
rf102_dataimport.C:50
rf102_dataimport.C:51
rf102_dataimport.C:52
rf102_dataimport.C:53
rf102_dataimport.C:54
rf102_dataimport.C:55
rf102_dataimport.C:56
rf102_dataimport.C:57
rf102_dataimport.C:58
rf102_dataimport.C:59
rf102_dataimport.C:60
rf102_dataimport.C:61
rf102_dataimport.C:62
rf102_dataimport.C:63
rf102_dataimport.C:64
rf102_dataimport.C:65
rf102_dataimport.C:66
rf102_dataimport.C:67
rf102_dataimport.C:68
rf102_dataimport.C:69
rf102_dataimport.C:70
rf102_dataimport.C:71
rf102_dataimport.C:72
rf102_dataimport.C:73
rf102_dataimport.C:74
rf102_dataimport.C:75
rf102_dataimport.C:76
rf102_dataimport.C:77
rf102_dataimport.C:78
rf102_dataimport.C:79
rf102_dataimport.C:80
rf102_dataimport.C:81
rf102_dataimport.C:82
rf102_dataimport.C:83
rf102_dataimport.C:84
rf102_dataimport.C:85
rf102_dataimport.C:86
rf102_dataimport.C:87
rf102_dataimport.C:88
rf102_dataimport.C:89
rf102_dataimport.C:90
rf102_dataimport.C:91
rf102_dataimport.C:92
rf102_dataimport.C:93
rf102_dataimport.C:94
rf102_dataimport.C:95
rf102_dataimport.C:96
rf102_dataimport.C:97
rf102_dataimport.C:98
rf102_dataimport.C:99
rf102_dataimport.C:100
rf102_dataimport.C:101
rf102_dataimport.C:102
rf102_dataimport.C:103
rf102_dataimport.C:104
rf102_dataimport.C:105
rf102_dataimport.C:106
rf102_dataimport.C:107
rf102_dataimport.C:108
rf102_dataimport.C:109
rf102_dataimport.C:110
rf102_dataimport.C:111
rf102_dataimport.C:112
rf102_dataimport.C:113
rf102_dataimport.C:114
rf102_dataimport.C:115
rf102_dataimport.C:116
rf102_dataimport.C:117
rf102_dataimport.C:118
rf102_dataimport.C:119
rf102_dataimport.C:120
rf102_dataimport.C:121
rf102_dataimport.C:122
rf102_dataimport.C:123
rf102_dataimport.C:124
rf102_dataimport.C:125
rf102_dataimport.C:126
rf102_dataimport.C:127
rf102_dataimport.C:128
rf102_dataimport.C:129
rf102_dataimport.C:130
rf102_dataimport.C:131
rf102_dataimport.C:132
rf102_dataimport.C:133
rf102_dataimport.C:134
rf102_dataimport.C:135
rf102_dataimport.C:136
rf102_dataimport.C:137
rf102_dataimport.C:138
rf102_dataimport.C:139
rf102_dataimport.C:140
rf102_dataimport.C:141
rf102_dataimport.C:142
rf102_dataimport.C:143
rf102_dataimport.C:144
rf102_dataimport.C:145
rf102_dataimport.C:146
rf102_dataimport.C:147
rf102_dataimport.C:148
rf102_dataimport.C:149
rf102_dataimport.C:150
rf102_dataimport.C:151
rf102_dataimport.C:152
rf102_dataimport.C:153
rf102_dataimport.C:154
rf102_dataimport.C:155
rf102_dataimport.C:156
rf102_dataimport.C:157
rf102_dataimport.C:158
rf102_dataimport.C:159
rf102_dataimport.C:160
rf102_dataimport.C:161
rf102_dataimport.C:162