Logo ROOT  
Reference Guide
rf102_dataimport.C File Reference

Detailed Description

View in nbviewer Open in SWAN Basic functionality: importing data from ROOT TTrees and THx histograms.

RooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby
Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University
All rights reserved, please read http://roofit.sourceforge.net/license.txt
[#1] INFO:Minization -- RooMinimizer::optimizeConst: activating const optimization
**********
** 1 **SET PRINT 1
**********
**********
** 2 **SET NOGRAD
**********
PARAMETER DEFINITIONS:
NO. NAME VALUE STEP SIZE LIMITS
1 mean 0.00000e+00 2.00000e+00 -1.00000e+01 1.00000e+01
2 sigma 3.00000e+00 9.90000e-01 1.00000e-01 1.00000e+01
**********
** 3 **SET ERR 0.5
**********
**********
** 4 **SET PRINT 1
**********
**********
** 5 **SET STR 1
**********
NOW USING STRATEGY 1: TRY TO BALANCE SPEED AGAINST RELIABILITY
**********
** 6 **MIGRAD 1000 1
**********
FIRST CALL TO USER FUNCTION AT NEW START POINT, WITH IFLAG=4.
START MIGRAD MINIMIZATION. STRATEGY 1. CONVERGENCE WHEN EDM .LT. 1.00e-03
FCN=249.349 FROM MIGRAD STATUS=INITIATE 8 CALLS 9 TOTAL
EDM= unknown STRATEGY= 1 NO ERROR MATRIX
EXT PARAMETER CURRENT GUESS STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 mean 0.00000e+00 2.00000e+00 2.01358e-01 1.15556e+01
2 sigma 3.00000e+00 9.90000e-01 2.22742e-01 5.42294e+00
ERR DEF= 0.5
MIGRAD MINIMIZATION HAS CONVERGED.
MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=249.251 FROM MIGRAD STATUS=CONVERGED 23 CALLS 24 TOTAL
EDM=1.58964e-05 STRATEGY= 1 ERROR MATRIX ACCURATE
EXT PARAMETER STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 mean -1.05079e-01 2.95122e-01 3.29083e-04 -2.34747e-02
2 sigma 2.93926e+00 2.13363e-01 5.44955e-04 -8.23858e-02
ERR DEF= 0.5
EXTERNAL ERROR MATRIX. NDIM= 25 NPAR= 2 ERR DEF=0.5
8.712e-02 -9.823e-05
-9.823e-05 4.556e-02
PARAMETER CORRELATION COEFFICIENTS
NO. GLOBAL 1 2
1 0.00156 1.000 -0.002
2 0.00156 -0.002 1.000
**********
** 7 **SET ERR 0.5
**********
**********
** 8 **SET PRINT 1
**********
**********
** 9 **HESSE 1000
**********
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=249.251 FROM HESSE STATUS=OK 10 CALLS 34 TOTAL
EDM=1.58906e-05 STRATEGY= 1 ERROR MATRIX ACCURATE
EXT PARAMETER INTERNAL INTERNAL
NO. NAME VALUE ERROR STEP SIZE VALUE
1 mean -1.05079e-01 2.95122e-01 6.58167e-05 -1.05081e-02
2 sigma 2.93926e+00 2.13363e-01 1.08991e-04 -4.40523e-01
ERR DEF= 0.5
EXTERNAL ERROR MATRIX. NDIM= 25 NPAR= 2 ERR DEF=0.5
8.712e-02 -1.406e-04
-1.406e-04 4.556e-02
PARAMETER CORRELATION COEFFICIENTS
NO. GLOBAL 1 2
1 0.00223 1.000 -0.002
2 0.00223 -0.002 1.000
[#1] INFO:Minization -- RooMinimizer::optimizeConst: deactivating const optimization
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping event #0 because y cannot accommodate the value 14.424
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping event #3 because y cannot accommodate the value -12.0022
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping event #5 because y cannot accommodate the value 13.8261
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping event #6 because y cannot accommodate the value -14.9925
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping ...
[#0] WARNING:DataHandling -- RooTreeDataStore::loadValues(ds) Ignored 36 out-of-range events
-----------------------
Reading data from ASCII
[#1] INFO:DataHandling -- RooDataSet::read: reading file rf102_testData.txt
[#0] ERROR:DataHandling -- RooDataSet::read(static): read error at line 65
[#1] INFO:DataHandling -- RooDataSet::read: read 64 events (ignored 0 out of range events)
DataStore dataset (rf102_testData.txt)
Contains 64 entries
Observables:
1) x = 0.0174204 L(-10 - 10) "x"
2) y = 9.46654 L(-10 - 10) "y"
3) blindState = Normal(idx = 0)
"Blinding State"
Original data, line 20:
1) RooRealVar:: x = -0.79919
2) RooRealVar:: y = 0.0106407
Read-back data, line 20:
1) RooRealVar:: x = -0.79919
2) RooRealVar:: y = 0.0106407
3) RooCategory:: blindState = Normal(idx = 0)
RooDataSet::ds[x,y] = 64 entries
#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));
// 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
// ------------------------------------------------------------------------------------
{
// Write data to output stream
std::ofstream outstream("rf102_testData.txt");
// Optionally, adjust the stream here (e.g. std::setprecision)
ds.write(outstream);
outstream.close();
}
// Read data from input stream. The variables of the dataset need to be supplied
// to the RooDataSet::read() function.
std::cout << "\n-----------------------\nReading data from ASCII\n";
RooDataSet *dataReadBack =
RooDataSet::read("rf102_testData.txt",
RooArgList(x, y), // variables to be read. If the file has more fields, these are ignored.
"D"); // Prints if a RooFit message stream listens for debug messages. Use Q for quiet.
dataReadBack->Print("V");
std::cout << "\nOriginal data, line 20:\n";
ds.get(20)->Print("V");
std::cout << "\nRead-back data, line 20:\n";
dataReadBack->get(20)->Print("V");
// 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
// ------------------------------------------------------------------------------------
// 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));
RooPlot *frame5 = y.frame(Title("Unbinned data read back from ASCII file"));
ds.plotOn(frame5, Binning(20));
dataReadBack->plotOn(frame5, Binning(20), MarkerColor(kRed), MarkerStyle(5));
// Draw all frames on a canvas
TCanvas *c = new TCanvas("rf102_dataimport", "rf102_dataimport", 1000, 800);
c->Divide(3, 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(4);
gPad->SetLeftMargin(0.15);
frame3->GetYaxis()->SetTitleOffset(1.4);
frame3->Draw();
c->cd(5);
gPad->SetLeftMargin(0.15);
frame4->GetYaxis()->SetTitleOffset(1.4);
frame4->Draw();
c->cd(6);
gPad->SetLeftMargin(0.15);
frame4->GetYaxis()->SetTitleOffset(1.4);
frame5->Draw();
}
// Create ROOT TH1 filled with a Gaussian distribution
TH1 *makeTH1()
{
TH1D *hh = new TH1D("hh", "hh", 25, -10, 10);
for (int i = 0; i < 100; i++) {
hh->Fill(gRandom->Gaus(0, 3));
}
return hh;
}
// Create ROOT TTree filled with a Gaussian distribution in x and a uniform distribution in y
TTree *makeTTree()
{
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;
}
Date
07/2008
Author
Wouter Verkerke

Definition in file rf102_dataimport.C.

c
#define c(i)
Definition: RSha256.hxx:119
RooPlot::Draw
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:691
TRandom::Gaus
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
RooAbsData::SumW2
@ SumW2
Definition: RooAbsData.h:96
RooFit::DataError
RooCmdArg DataError(Int_t)
Definition: RooGlobalFunc.cxx:156
tree
Definition: tree.py:1
RooArgList
Definition: RooArgList.h:21
TTree
Definition: TTree.h:79
RooGaussian.h
TH1D
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:615
RooFit::MarkerStyle
RooCmdArg MarkerStyle(Style_t style)
Definition: RooGlobalFunc.cxx:85
TRandom.h
TRandom::Uniform
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:635
RooAbsData::Print
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooAbsData.h:177
x
Double_t x[n]
Definition: legend1.C:17
RooGaussian
Definition: RooGaussian.h:25
TCanvas.h
TTree.h
RooFit::Binning
RooCmdArg Binning(const RooAbsBinning &binning)
Definition: RooGlobalFunc.cxx:82
RooDataSet.h
RooDataSet::get
virtual const RooArgSet * get(Int_t index) const override
Return RooArgSet with coordinates of event 'index'.
Definition: RooDataSet.cxx:1038
RooDataHist
Definition: RooDataHist.h:39
TGeant4Unit::gauss
static constexpr double gauss
Definition: TGeant4SystemOfUnits.h:269
RooFit
Definition: RooCFunction1Binding.h:29
RooAbsData::plotOn
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
Definition: RooAbsData.cxx:547
RooDataHist.h
RooPlot.h
RooPlot::GetYaxis
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1256
TH1::Fill
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3274
gRandom
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
RooPlot
Definition: RooPlot.h:44
y
Double_t y[n]
Definition: legend1.C:17
RooRealVar.h
kRed
@ kRed
Definition: Rtypes.h:66
RooDataSet::read
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...
Definition: RooDataSet.cxx:1710
sigma
const Double_t sigma
Definition: h1analysisProxy.h:11
Double_t
double Double_t
Definition: RtypesCore.h:59
TCanvas
Definition: TCanvas.h:23
TH1
Definition: TH1.h:57
gPad
#define gPad
Definition: TVirtualPad.h:287
RooDataSet
Definition: RooDataSet.h:33
RooFit::MarkerColor
RooCmdArg MarkerColor(Color_t color)
Definition: RooGlobalFunc.cxx:87
RooAbsCollection::Print
virtual void Print(Option_t *options=0) const
This method must be overridden when a class wants to print itself.
Definition: RooAbsCollection.h:199
TAttAxis::SetTitleOffset
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:293
RooRealVar
Definition: RooRealVar.h:35
TH1D.h
RooFit::Title
RooCmdArg Title(const char *name)
Definition: RooGlobalFunc.cxx:173
RooArgSet
Definition: RooArgSet.h:28
RooFit::Import
RooCmdArg Import(const char *state, TH1 &histo)
Definition: RooGlobalFunc.cxx:98