Logo ROOT   6.14/05
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' RooFit tutorial macro #401
5 ///
6 /// Overview of advanced option for importing data from ROOT TTree and THx histograms
7 /// Basic import options are demonstrated in rf102_dataimport.C
8 ///
9 /// \macro_output
10 /// \macro_code
11 /// \author 07/2008 - Wouter Verkerke
12 
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 
31 
32 TH1* makeTH1(const char* name, Double_t mean, Double_t sigma) ;
33 TTree* makeTTree() ;
34 
35 
36 void rf401_importttreethx()
37 {
38  // 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
39  // --------------------------------------------------------------------------
40 
41  // Create thee ROOT TH1 histograms
42  TH1* hh_1 = makeTH1("hh1",0,3) ;
43  TH1* hh_2 = makeTH1("hh2",-3,1) ;
44  TH1* hh_3 = makeTH1("hh3",+3,4) ;
45 
46  // Declare observable x
47  RooRealVar x("x","x",-10,10) ;
48 
49  // Create category observable c that serves as index for the ROOT histograms
50  RooCategory c("c","c") ;
51  c.defineType("SampleA") ;
52  c.defineType("SampleB") ;
53  c.defineType("SampleC") ;
54 
55  // Create a binned dataset that imports contents of all TH1 mapped by index category c
56  RooDataHist* dh = new RooDataHist("dh","dh",x,Index(c),Import("SampleA",*hh_1),Import("SampleB",*hh_2),Import("SampleC",*hh_3)) ;
57  dh->Print() ;
58 
59  // Alternative constructor form for importing multiple histograms
60  map<string,TH1*> hmap ;
61  hmap["SampleA"] = hh_1 ;
62  hmap["SampleB"] = hh_2 ;
63  hmap["SampleC"] = hh_3 ;
64  RooDataHist* dh2 = new RooDataHist("dh","dh",x,c,hmap) ;
65  dh2->Print() ;
66 
67 
68 
69  // 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
70  // -----------------------------------------------------------------------------------------
71 
72  TTree* tree = makeTTree() ;
73 
74  // Define observables y,z
75  RooRealVar y("y","y",-10,10) ;
76  RooRealVar z("z","z",-10,10) ;
77 
78  // Import only observables (y,z)
79  RooDataSet ds("ds","ds",RooArgSet(x,y),Import(*tree)) ;
80  ds.Print() ;
81 
82  // Import observables (x,y,z) but only event for which (y+z<0) is true
83  RooDataSet ds2("ds2","ds2",RooArgSet(x,y,z),Import(*tree),Cut("y+z<0")) ;
84  ds2.Print() ;
85 
86 
87 
88  // 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
89  // ---------------------------------------------------------------
90 
91  // Import integer tree branch as RooRealVar
92  RooRealVar i("i","i",0,5) ;
93  RooDataSet ds3("ds3","ds3",RooArgSet(i,x),Import(*tree)) ;
94  ds3.Print() ;
95 
96  // Define category i
97  RooCategory icat("i","i") ;
98  icat.defineType("State0",0) ;
99  icat.defineType("State1",1) ;
100 
101  // Import integer tree branch as RooCategory (only events with i==0 and i==1
102  // will be imported as those are the only defined states)
103  RooDataSet ds4("ds4","ds4",RooArgSet(icat,x),Import(*tree)) ;
104  ds4.Print() ;
105 
106 
107 
108  // 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
109  // ----------------------------------------------------------------------------------------
110 
111  // Create three RooDataSets in (y,z)
112  RooDataSet* dsA = (RooDataSet*) ds2.reduce(RooArgSet(x,y),"z<-5") ;
113  RooDataSet* dsB = (RooDataSet*) ds2.reduce(RooArgSet(x,y),"abs(z)<5") ;
114  RooDataSet* dsC = (RooDataSet*) ds2.reduce(RooArgSet(x,y),"z>5") ;
115 
116  // Create a dataset that imports contents of all the above datasets mapped by index category c
117  RooDataSet* dsABC = new RooDataSet("dsABC","dsABC",RooArgSet(x,y),Index(c),Import("SampleA",*dsA),Import("SampleB",*dsB),Import("SampleC",*dsC)) ;
118 
119  dsABC->Print() ;
120 
121 }
122 
123 
124 
125 TH1* makeTH1(const char* name, Double_t mean, Double_t sigma)
126 {
127  // Create ROOT TH1 filled with a Gaussian distribution
128 
129  TH1D* hh = new TH1D(name,name,100,-10,10) ;
130  for (int i=0 ; i<1000 ; i++) {
131  hh->Fill(gRandom->Gaus(mean,sigma)) ;
132  }
133  return hh ;
134 }
135 
136 
137 
138 TTree* makeTTree()
139 {
140  // Create ROOT TTree filled with a Gaussian distribution in x and a uniform distribution in y
141 
142  TTree* tree = new TTree("tree","tree") ;
143  Double_t* px = new Double_t ;
144  Double_t* py = new Double_t ;
145  Double_t* pz = new Double_t ;
146  Int_t* pi = new Int_t ;
147  tree->Branch("x",px,"x/D") ;
148  tree->Branch("y",py,"y/D") ;
149  tree->Branch("z",pz,"z/D") ;
150  tree->Branch("i",pi,"i/I") ;
151  for (int i=0 ; i<100 ; i++) {
152  *px = gRandom->Gaus(0,3) ;
153  *py = gRandom->Uniform()*30 - 15 ;
154  *pz = gRandom->Gaus(0,5) ;
155  *pi = i % 3 ;
156  tree->Fill() ;
157  }
158  return tree ;
159 }
160 
161 
162 
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3251
static constexpr double pi
RooCmdArg Cut(const char *cutSpec)
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:256
int Int_t
Definition: RtypesCore.h:41
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:359
RooDataSet is a container class to hold N-dimensional binned data.
Definition: RooDataHist.h:40
Double_t x[n]
Definition: legend1.C:17
const Double_t sigma
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:610
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
RooCategory represents a fundamental (non-derived) discrete value object.
Definition: RooCategory.h:24
RooCmdArg Import(const char *state, TH1 &histo)
RooCmdArg Index(RooCategory &icat)
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooAbsData.h:159
double Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
The TH1 histogram class.
Definition: TH1.h:56
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:627
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
THist< 1, double, THistStatContent, THistStatUncertainty > TH1D
Definition: THist.hxx:284
#define c(i)
Definition: RSha256.hxx:101
Definition: tree.py:1
char name[80]
Definition: TGX11.cxx:109