ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
rf401_importttreethx.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// 'DATA AND CATEGORIES' RooFit tutorial macro #401
4 ///
5 /// Overview of advanced option for importing data from ROOT TTree and THx histograms
6 /// Basic import options are demonstrated in rf102_dataimport.C
7 ///
8 ///
9 /// \macro_code
10 /// \author 07/2008 - Wouter Verkerke
11 
12 
13 #ifndef __CINT__
14 #include "RooGlobalFunc.h"
15 #endif
16 #include "RooRealVar.h"
17 #include "RooDataSet.h"
18 #include "RooDataHist.h"
19 #include "RooCategory.h"
20 #include "RooGaussian.h"
21 #include "RooConstVar.h"
22 #include "TCanvas.h"
23 #include "TAxis.h"
24 #include "RooPlot.h"
25 #include "TH1.h"
26 #include "TTree.h"
27 #include "TRandom.h"
28 #include <map>
29 
30 using namespace RooFit ;
31 
32 
33 
34 TH1* makeTH1(const char* name, Double_t mean, Double_t sigma) ;
35 TTree* makeTTree() ;
36 
37 
38 void rf401_importttreethx()
39 {
40  // 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
41  // --------------------------------------------------------------------------
42 
43  // Create thee ROOT TH1 histograms
44  TH1* hh_1 = makeTH1("hh1",0,3) ;
45  TH1* hh_2 = makeTH1("hh2",-3,1) ;
46  TH1* hh_3 = makeTH1("hh3",+3,4) ;
47 
48  // Declare observable x
49  RooRealVar x("x","x",-10,10) ;
50 
51  // Create category observable c that serves as index for the ROOT histograms
52  RooCategory c("c","c") ;
53  c.defineType("SampleA") ;
54  c.defineType("SampleB") ;
55  c.defineType("SampleC") ;
56 
57  // Create a binned dataset that imports contents of all TH1 mapped by index category c
58  RooDataHist* dh = new RooDataHist("dh","dh",x,Index(c),Import("SampleA",*hh_1),Import("SampleB",*hh_2),Import("SampleC",*hh_3)) ;
59  dh->Print() ;
60 
61  // Alternative constructor form for importing multiple histograms
62  map<string,TH1*> hmap ;
63  hmap["SampleA"] = hh_1 ;
64  hmap["SampleB"] = hh_2 ;
65  hmap["SampleC"] = hh_3 ;
66  RooDataHist* dh2 = new RooDataHist("dh","dh",x,c,hmap) ;
67  dh2->Print() ;
68 
69 
70 
71  // 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
72  // -----------------------------------------------------------------------------------------
73 
74  TTree* tree = makeTTree() ;
75 
76  // Define observables y,z
77  RooRealVar y("y","y",-10,10) ;
78  RooRealVar z("z","z",-10,10) ;
79 
80  // Import only observables (y,z)
81  RooDataSet ds("ds","ds",RooArgSet(x,y),Import(*tree)) ;
82  ds.Print() ;
83 
84  // Import observables (x,y,z) but only event for which (y+z<0) is true
85  RooDataSet ds2("ds2","ds2",RooArgSet(x,y,z),Import(*tree),Cut("y+z<0")) ;
86  ds2.Print() ;
87 
88 
89 
90  // 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
91  // ---------------------------------------------------------------
92 
93  // Import integer tree branch as RooRealVar
94  RooRealVar i("i","i",0,5) ;
95  RooDataSet ds3("ds3","ds3",RooArgSet(i,x),Import(*tree)) ;
96  ds3.Print() ;
97 
98  // Define category i
99  RooCategory icat("i","i") ;
100  icat.defineType("State0",0) ;
101  icat.defineType("State1",1) ;
102 
103  // Import integer tree branch as RooCategory (only events with i==0 and i==1
104  // will be imported as those are the only defined states)
105  RooDataSet ds4("ds4","ds4",RooArgSet(icat,x),Import(*tree)) ;
106  ds4.Print() ;
107 
108 
109 
110  // 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
111  // ----------------------------------------------------------------------------------------
112 
113  // Create three RooDataSets in (y,z)
114  RooDataSet* dsA = (RooDataSet*) ds2.reduce(RooArgSet(x,y),"z<-5") ;
115  RooDataSet* dsB = (RooDataSet*) ds2.reduce(RooArgSet(x,y),"abs(z)<5") ;
116  RooDataSet* dsC = (RooDataSet*) ds2.reduce(RooArgSet(x,y),"z>5") ;
117 
118  // Create a dataset that imports contents of all the above datasets mapped by index category c
119  RooDataSet* dsABC = new RooDataSet("dsABC","dsABC",RooArgSet(x,y),Index(c),Import("SampleA",*dsA),Import("SampleB",*dsB),Import("SampleC",*dsC)) ;
120 
121  dsABC->Print() ;
122 
123 }
124 
125 
126 
127 TH1* makeTH1(const char* name, Double_t mean, Double_t sigma)
128 {
129  // Create ROOT TH1 filled with a Gaussian distribution
130 
131  TH1D* hh = new TH1D(name,name,100,-10,10) ;
132  for (int i=0 ; i<1000 ; i++) {
133  hh->Fill(gRandom->Gaus(mean,sigma)) ;
134  }
135  return hh ;
136 }
137 
138 
139 
140 TTree* makeTTree()
141 {
142  // Create ROOT TTree filled with a Gaussian distribution in x and a uniform distribution in y
143 
144  TTree* tree = new TTree("tree","tree") ;
145  Double_t* px = new Double_t ;
146  Double_t* py = new Double_t ;
147  Double_t* pz = new Double_t ;
148  Int_t* pi = new Int_t ;
149  tree->Branch("x",px,"x/D") ;
150  tree->Branch("y",py,"y/D") ;
151  tree->Branch("z",pz,"z/D") ;
152  tree->Branch("i",pi,"i/I") ;
153  for (int i=0 ; i<100 ; i++) {
154  *px = gRandom->Gaus(0,3) ;
155  *py = gRandom->Uniform()*30 - 15 ;
156  *pz = gRandom->Gaus(0,5) ;
157  *pi = i % 3 ;
158  tree->Fill() ;
159  }
160  return tree ;
161 }
162 
163 
164 
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
RooCmdArg Cut(const char *cutSpec)
return c
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:235
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4306
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooAbsData.h:157
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:399
RooDataSet is a container class to hold N-dimensional binned data.
Definition: RooDataHist.h:40
Double_t x[n]
Definition: legend1.C:17
friend class RooArgSet
Definition: RooAbsArg.h:469
Float_t z[5]
Definition: Ifit.C:16
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:613
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:25
RooCmdArg Import(const char *state, TH1 &histo)
RooCmdArg Index(RooCategory &icat)
tuple tree
Definition: tree.py:24
double Double_t
Definition: RtypesCore.h:55
friend class RooDataSet
Definition: RooAbsArg.h:534
Double_t y[n]
Definition: legend1.C:17
The TH1 histogram class.
Definition: TH1.h:80
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:606
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
Definition: TTree.cxx:1623
A TTree object has a header with a name and a title.
Definition: TTree.h:98