Logo ROOT  
Reference Guide
rf401_importttreethx.C File Reference

Detailed Description

View in nbviewer Open in SWAN Data and categories: advanced options for importing data from ROOT TTree and THx histograms

Basic import options are demonstrated in rf102_dataimport.C

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
RooDataHist::dh[c,x] = 300 bins (2964 weights)
RooDataHist::dh[c,x] = 300 bins (2964 weights)
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping event #7 because y cannot accommodate the value 13.3845
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping event #8 because y cannot accommodate the value 11.1861
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping event #12 because y cannot accommodate the value 13.7009
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping event #14 because y cannot accommodate the value -10.6852
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping ...
[#0] WARNING:DataHandling -- RooTreeDataStore::loadValues(ds) Ignored 35 out-of-range events
RooDataSet::ds[x,y] = 65 entries
[#1] INFO:InputArguments -- The formula y+z<0 claims to use the variables (x,y,z) but only (y,z) seem to be in use.
inputs: y+z<0
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds2) Skipping event #7 because y cannot accommodate the value 13.3845
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds2) Skipping event #8 because y cannot accommodate the value 11.1861
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds2) Skipping event #12 because y cannot accommodate the value 13.7009
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds2) Skipping event #14 because y cannot accommodate the value -10.6852
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds2) Skipping ...
[#0] WARNING:DataHandling -- RooTreeDataStore::loadValues(ds2) Ignored 36 out-of-range events
RooDataSet::ds2[x,y,z] = 26 entries
[#1] INFO:DataHandling -- RooAbsReal::attachToTree(i) TTree Int_t branch i will be converted to double precision.
RooDataSet::ds3[i,x] = 100 entries
[#1] INFO:DataHandling -- RooAbsCategory::attachToTree(i) TTree branch i will be interpreted as category index
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds4) Skipping event #2 because i cannot accommodate the value 2.43861e-152
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds4) Skipping event #5 because i cannot accommodate the value 2.43861e-152
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds4) Skipping event #8 because i cannot accommodate the value 2.43861e-152
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds4) Skipping event #11 because i cannot accommodate the value 2.43861e-152
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds4) Skipping ...
[#0] WARNING:DataHandling -- RooTreeDataStore::loadValues(ds4) Ignored 33 out-of-range events
RooDataSet::ds4[i,x] = 67 entries
RooDataSet::dsABC[c,x,y] = 26 entries
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooDataHist.h"
#include "RooCategory.h"
#include "RooGaussian.h"
#include "RooConstVar.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"
#include "TH1.h"
#include "TTree.h"
#include "TRandom.h"
#include <map>
using namespace RooFit;
TH1 *makeTH1(const char *name, Double_t mean, Double_t sigma);
TTree *makeTTree();
void rf401_importttreethx()
{
// I m p o r t m u l t i p l e T H 1 i n t o a R o o D a t a H i s t
// --------------------------------------------------------------------------
// Create thee ROOT TH1 histograms
TH1 *hh_1 = makeTH1("hh1", 0, 3);
TH1 *hh_2 = makeTH1("hh2", -3, 1);
TH1 *hh_3 = makeTH1("hh3", +3, 4);
// Declare observable x
RooRealVar x("x", "x", -10, 10);
// Create category observable c that serves as index for the ROOT histograms
RooCategory c("c", "c", {{"SampleA",0}, {"SampleB",1}, {"SampleC",2}});
// Create a binned dataset that imports contents of all TH1 mapped by index category c
RooDataHist *dh = new RooDataHist("dh", "dh", x, Index(c), Import("SampleA", *hh_1), Import("SampleB", *hh_2),
Import("SampleC", *hh_3));
dh->Print();
// Alternative constructor form for importing multiple histograms
std::map<std::string, TH1 *> hmap;
hmap["SampleA"] = hh_1;
hmap["SampleB"] = hh_2;
hmap["SampleC"] = hh_3;
RooDataHist *dh2 = new RooDataHist("dh", "dh", x, c, hmap);
dh2->Print();
// I m p o r t i n g a T T r e e i n t o a R o o D a t a S e t w i t h c u t s
// -----------------------------------------------------------------------------------------
TTree *tree = makeTTree();
// Define observables y,z
RooRealVar y("y", "y", -10, 10);
RooRealVar z("z", "z", -10, 10);
// Import only observables (y,z)
RooDataSet ds("ds", "ds", RooArgSet(x, y), Import(*tree));
ds.Print();
// Import observables (x,y,z) but only event for which (y+z<0) is true
RooDataSet ds2("ds2", "ds2", RooArgSet(x, y, z), Import(*tree), Cut("y+z<0"));
ds2.Print();
// I m p o r t i n g i n t e g e r T T r e e b r a n c h e s
// ---------------------------------------------------------------
// Import integer tree branch as RooRealVar
RooRealVar i("i", "i", 0, 5);
RooDataSet ds3("ds3", "ds3", RooArgSet(i, x), Import(*tree));
ds3.Print();
// Define category i
RooCategory icat("i", "i");
icat.defineType("State0", 0);
icat.defineType("State1", 1);
// Import integer tree branch as RooCategory (only events with i==0 and i==1
// will be imported as those are the only defined states)
RooDataSet ds4("ds4", "ds4", RooArgSet(icat, x), Import(*tree));
ds4.Print();
// I m p o r t m u l t i p l e R o o D a t a S e t s i n t o a R o o D a t a S e t
// ----------------------------------------------------------------------------------------
// Create three RooDataSets in (y,z)
RooDataSet *dsA = (RooDataSet *)ds2.reduce(RooArgSet(x, y), "z<-5");
RooDataSet *dsB = (RooDataSet *)ds2.reduce(RooArgSet(x, y), "abs(z)<5");
RooDataSet *dsC = (RooDataSet *)ds2.reduce(RooArgSet(x, y), "z>5");
// Create a dataset that imports contents of all the above datasets mapped by index category c
RooDataSet *dsABC = new RooDataSet("dsABC", "dsABC", RooArgSet(x, y), Index(c), Import("SampleA", *dsA),
Import("SampleB", *dsB), Import("SampleC", *dsC));
dsABC->Print();
}
TH1 *makeTH1(const char *name, Double_t mean, Double_t sigma)
{
// Create ROOT TH1 filled with a Gaussian distribution
TH1D *hh = new TH1D(name, name, 100, -10, 10);
for (int i = 0; i < 1000; i++) {
hh->Fill(gRandom->Gaus(mean, sigma));
}
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;
Double_t *pz = new Double_t;
Int_t *pi = new Int_t;
tree->Branch("x", px, "x/D");
tree->Branch("y", py, "y/D");
tree->Branch("z", pz, "z/D");
tree->Branch("i", pi, "i/I");
for (int i = 0; i < 100; i++) {
*px = gRandom->Gaus(0, 3);
*py = gRandom->Uniform() * 30 - 15;
*pz = gRandom->Gaus(0, 5);
*pi = i % 3;
tree->Fill();
}
return tree;
}
Date
July 2008
Author
Wouter Verkerke

Definition in file rf401_importttreethx.C.

c
#define c(i)
Definition: RSha256.hxx:101
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:274
tree
Definition: tree.py:1
TTree
A TTree represents a columnar dataset.
Definition: TTree.h:79
RooGaussian.h
TH1D
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:618
extract_docstrings.ds
ds
Definition: extract_docstrings.py:40
TRandom.h
Int_t
int Int_t
Definition: RtypesCore.h:45
TRandom::Uniform
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:672
RooAbsData::Print
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooAbsData.h:191
x
Double_t x[n]
Definition: legend1.C:17
TCanvas.h
TTree.h
RooDataSet.h
RooDataHist
The RooDataHist is a container class to hold N-dimensional binned data.
Definition: RooDataHist.h:39
RooFit::Cut
RooCmdArg Cut(const char *cutSpec)
Definition: RooGlobalFunc.cxx:81
RooFit
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition: RooCFunction1Binding.h:29
RooDataHist.h
RooPlot.h
TH1::Fill
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3327
gRandom
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
RooCategory.h
y
Double_t y[n]
Definition: legend1.C:17
RooRealVar.h
RooConstVar.h
sigma
const Double_t sigma
Definition: h1analysisProxy.h:11
Double_t
double Double_t
Definition: RtypesCore.h:59
TGeant4Unit::pi
static constexpr double pi
Definition: TGeant4SystemOfUnits.h:67
RooCategory
RooCategory is an object to represent discrete states.
Definition: RooCategory.h:27
RooAbsData::reduce
RooAbsData * reduce(const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg(), const RooCmdArg &arg3=RooCmdArg(), const RooCmdArg &arg4=RooCmdArg(), const RooCmdArg &arg5=RooCmdArg(), const RooCmdArg &arg6=RooCmdArg(), const RooCmdArg &arg7=RooCmdArg(), const RooCmdArg &arg8=RooCmdArg())
Create a reduced copy of this dataset.
Definition: RooAbsData.cxx:382
TAxis.h
TH1
TH1 is the base class of all histogramm classes in ROOT.
Definition: TH1.h:58
name
char name[80]
Definition: TGX11.cxx:110
RooDataSet
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
RooFit::Index
RooCmdArg Index(RooCategory &icat)
Definition: RooGlobalFunc.cxx:98
RooRealVar
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:37
TH1.h
RooArgSet
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:29
int
RooFit::Import
RooCmdArg Import(const char *state, TH1 &histo)
Definition: RooGlobalFunc.cxx:99