Logo ROOT  
Reference Guide
rf401_importttreethx.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -nodraw
4 /// Data and categories: advanced options for importing data from ROOT TTree and THx histograms
5 ///
6 /// Basic import options are demonstrated in rf102_dataimport.C
7 ///
8 /// \macro_output
9 /// \macro_code
10 ///
11 /// \date July 2008
12 /// \author Wouter Verkerke
13 
14 #include "RooRealVar.h"
15 #include "RooDataSet.h"
16 #include "RooDataHist.h"
17 #include "RooCategory.h"
18 #include "RooGaussian.h"
19 #include "RooConstVar.h"
20 #include "TCanvas.h"
21 #include "TAxis.h"
22 #include "RooPlot.h"
23 #include "TH1.h"
24 #include "TTree.h"
25 #include "TRandom.h"
26 #include <map>
27 
28 using namespace RooFit;
29 
30 TH1 *makeTH1(const char *name, Double_t mean, Double_t sigma);
31 TTree *makeTTree();
32 
33 void rf401_importttreethx()
34 {
35  // 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
36  // --------------------------------------------------------------------------
37 
38  // Create thee ROOT TH1 histograms
39  TH1 *hh_1 = makeTH1("hh1", 0, 3);
40  TH1 *hh_2 = makeTH1("hh2", -3, 1);
41  TH1 *hh_3 = makeTH1("hh3", +3, 4);
42 
43  // Declare observable x
44  RooRealVar x("x", "x", -10, 10);
45 
46  // Create category observable c that serves as index for the ROOT histograms
47  RooCategory c("c", "c", {{"SampleA",0}, {"SampleB",1}, {"SampleC",2}});
48 
49  // Create a binned dataset that imports contents of all TH1 mapped by index category c
50  RooDataHist *dh = new RooDataHist("dh", "dh", x, Index(c), Import("SampleA", *hh_1), Import("SampleB", *hh_2),
51  Import("SampleC", *hh_3));
52  dh->Print();
53 
54  // Alternative constructor form for importing multiple histograms
55  std::map<std::string, TH1 *> hmap;
56  hmap["SampleA"] = hh_1;
57  hmap["SampleB"] = hh_2;
58  hmap["SampleC"] = hh_3;
59  RooDataHist *dh2 = new RooDataHist("dh", "dh", x, c, hmap);
60  dh2->Print();
61 
62  // 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
63  // -----------------------------------------------------------------------------------------
64 
65  TTree *tree = makeTTree();
66 
67  // Define observables y,z
68  RooRealVar y("y", "y", -10, 10);
69  RooRealVar z("z", "z", -10, 10);
70 
71  // Import only observables (y,z)
72  RooDataSet ds("ds", "ds", RooArgSet(x, y), Import(*tree));
73  ds.Print();
74 
75  // Import observables (x,y,z) but only event for which (y+z<0) is true
76  RooDataSet ds2("ds2", "ds2", RooArgSet(x, y, z), Import(*tree), Cut("y+z<0"));
77  ds2.Print();
78 
79  // 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
80  // ---------------------------------------------------------------
81 
82  // Import integer tree branch as RooRealVar
83  RooRealVar i("i", "i", 0, 5);
84  RooDataSet ds3("ds3", "ds3", RooArgSet(i, x), Import(*tree));
85  ds3.Print();
86 
87  // Define category i
88  RooCategory icat("i", "i");
89  icat.defineType("State0", 0);
90  icat.defineType("State1", 1);
91 
92  // Import integer tree branch as RooCategory (only events with i==0 and i==1
93  // will be imported as those are the only defined states)
94  RooDataSet ds4("ds4", "ds4", RooArgSet(icat, x), Import(*tree));
95  ds4.Print();
96 
97  // 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
98  // ----------------------------------------------------------------------------------------
99 
100  // Create three RooDataSets in (y,z)
101  RooDataSet *dsA = (RooDataSet *)ds2.reduce(RooArgSet(x, y), "z<-5");
102  RooDataSet *dsB = (RooDataSet *)ds2.reduce(RooArgSet(x, y), "abs(z)<5");
103  RooDataSet *dsC = (RooDataSet *)ds2.reduce(RooArgSet(x, y), "z>5");
104 
105  // Create a dataset that imports contents of all the above datasets mapped by index category c
106  RooDataSet *dsABC = new RooDataSet("dsABC", "dsABC", RooArgSet(x, y), Index(c), Import("SampleA", *dsA),
107  Import("SampleB", *dsB), Import("SampleC", *dsC));
108 
109  dsABC->Print();
110 }
111 
112 TH1 *makeTH1(const char *name, Double_t mean, Double_t sigma)
113 {
114  // Create ROOT TH1 filled with a Gaussian distribution
115 
116  TH1D *hh = new TH1D(name, name, 100, -10, 10);
117  for (int i = 0; i < 1000; i++) {
118  hh->Fill(gRandom->Gaus(mean, sigma));
119  }
120  return hh;
121 }
122 
123 TTree *makeTTree()
124 {
125  // Create ROOT TTree filled with a Gaussian distribution in x and a uniform distribution in y
126 
127  TTree *tree = new TTree("tree", "tree");
128  Double_t *px = new Double_t;
129  Double_t *py = new Double_t;
130  Double_t *pz = new Double_t;
131  Int_t *pi = new Int_t;
132  tree->Branch("x", px, "x/D");
133  tree->Branch("y", py, "y/D");
134  tree->Branch("z", pz, "z/D");
135  tree->Branch("i", pi, "i/I");
136  for (int i = 0; i < 100; i++) {
137  *px = gRandom->Gaus(0, 3);
138  *py = gRandom->Uniform() * 30 - 15;
139  *pz = gRandom->Gaus(0, 5);
140  *pi = i % 3;
141  tree->Fill();
142  }
143  return tree;
144 }
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