ROOT logo

From $ROOTSYS/tutorials/roofit/rf401_importttreethx.C

//////////////////////////////////////////////////////////////////////////
//
// 'DATA AND CATEGORIES' RooFit tutorial macro #401
// 
// Overview of advanced option for importing data from ROOT TTree and THx histograms
// Basic import options are demonstrated in rf102_dataimport.C
//
//
// 07/2008 - Wouter Verkerke 
// 
/////////////////////////////////////////////////////////////////////////

#ifndef __CINT__
#include "RooGlobalFunc.h"
#endif
#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"

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") ;
  c.defineType("SampleA") ;
  c.defineType("SampleB") ;
  c.defineType("SampleC") ;

  // 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
  map<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 ;
}



 rf401_importttreethx.C:1
 rf401_importttreethx.C:2
 rf401_importttreethx.C:3
 rf401_importttreethx.C:4
 rf401_importttreethx.C:5
 rf401_importttreethx.C:6
 rf401_importttreethx.C:7
 rf401_importttreethx.C:8
 rf401_importttreethx.C:9
 rf401_importttreethx.C:10
 rf401_importttreethx.C:11
 rf401_importttreethx.C:12
 rf401_importttreethx.C:13
 rf401_importttreethx.C:14
 rf401_importttreethx.C:15
 rf401_importttreethx.C:16
 rf401_importttreethx.C:17
 rf401_importttreethx.C:18
 rf401_importttreethx.C:19
 rf401_importttreethx.C:20
 rf401_importttreethx.C:21
 rf401_importttreethx.C:22
 rf401_importttreethx.C:23
 rf401_importttreethx.C:24
 rf401_importttreethx.C:25
 rf401_importttreethx.C:26
 rf401_importttreethx.C:27
 rf401_importttreethx.C:28
 rf401_importttreethx.C:29
 rf401_importttreethx.C:30
 rf401_importttreethx.C:31
 rf401_importttreethx.C:32
 rf401_importttreethx.C:33
 rf401_importttreethx.C:34
 rf401_importttreethx.C:35
 rf401_importttreethx.C:36
 rf401_importttreethx.C:37
 rf401_importttreethx.C:38
 rf401_importttreethx.C:39
 rf401_importttreethx.C:40
 rf401_importttreethx.C:41
 rf401_importttreethx.C:42
 rf401_importttreethx.C:43
 rf401_importttreethx.C:44
 rf401_importttreethx.C:45
 rf401_importttreethx.C:46
 rf401_importttreethx.C:47
 rf401_importttreethx.C:48
 rf401_importttreethx.C:49
 rf401_importttreethx.C:50
 rf401_importttreethx.C:51
 rf401_importttreethx.C:52
 rf401_importttreethx.C:53
 rf401_importttreethx.C:54
 rf401_importttreethx.C:55
 rf401_importttreethx.C:56
 rf401_importttreethx.C:57
 rf401_importttreethx.C:58
 rf401_importttreethx.C:59
 rf401_importttreethx.C:60
 rf401_importttreethx.C:61
 rf401_importttreethx.C:62
 rf401_importttreethx.C:63
 rf401_importttreethx.C:64
 rf401_importttreethx.C:65
 rf401_importttreethx.C:66
 rf401_importttreethx.C:67
 rf401_importttreethx.C:68
 rf401_importttreethx.C:69
 rf401_importttreethx.C:70
 rf401_importttreethx.C:71
 rf401_importttreethx.C:72
 rf401_importttreethx.C:73
 rf401_importttreethx.C:74
 rf401_importttreethx.C:75
 rf401_importttreethx.C:76
 rf401_importttreethx.C:77
 rf401_importttreethx.C:78
 rf401_importttreethx.C:79
 rf401_importttreethx.C:80
 rf401_importttreethx.C:81
 rf401_importttreethx.C:82
 rf401_importttreethx.C:83
 rf401_importttreethx.C:84
 rf401_importttreethx.C:85
 rf401_importttreethx.C:86
 rf401_importttreethx.C:87
 rf401_importttreethx.C:88
 rf401_importttreethx.C:89
 rf401_importttreethx.C:90
 rf401_importttreethx.C:91
 rf401_importttreethx.C:92
 rf401_importttreethx.C:93
 rf401_importttreethx.C:94
 rf401_importttreethx.C:95
 rf401_importttreethx.C:96
 rf401_importttreethx.C:97
 rf401_importttreethx.C:98
 rf401_importttreethx.C:99
 rf401_importttreethx.C:100
 rf401_importttreethx.C:101
 rf401_importttreethx.C:102
 rf401_importttreethx.C:103
 rf401_importttreethx.C:104
 rf401_importttreethx.C:105
 rf401_importttreethx.C:106
 rf401_importttreethx.C:107
 rf401_importttreethx.C:108
 rf401_importttreethx.C:109
 rf401_importttreethx.C:110
 rf401_importttreethx.C:111
 rf401_importttreethx.C:112
 rf401_importttreethx.C:113
 rf401_importttreethx.C:114
 rf401_importttreethx.C:115
 rf401_importttreethx.C:116
 rf401_importttreethx.C:117
 rf401_importttreethx.C:118
 rf401_importttreethx.C:119
 rf401_importttreethx.C:120
 rf401_importttreethx.C:121
 rf401_importttreethx.C:122
 rf401_importttreethx.C:123
 rf401_importttreethx.C:124
 rf401_importttreethx.C:125
 rf401_importttreethx.C:126
 rf401_importttreethx.C:127
 rf401_importttreethx.C:128
 rf401_importttreethx.C:129
 rf401_importttreethx.C:130
 rf401_importttreethx.C:131
 rf401_importttreethx.C:132
 rf401_importttreethx.C:133
 rf401_importttreethx.C:134
 rf401_importttreethx.C:135
 rf401_importttreethx.C:136
 rf401_importttreethx.C:137
 rf401_importttreethx.C:138
 rf401_importttreethx.C:139
 rf401_importttreethx.C:140
 rf401_importttreethx.C:141
 rf401_importttreethx.C:142
 rf401_importttreethx.C:143
 rf401_importttreethx.C:144
 rf401_importttreethx.C:145
 rf401_importttreethx.C:146
 rf401_importttreethx.C:147
 rf401_importttreethx.C:148
 rf401_importttreethx.C:149
 rf401_importttreethx.C:150
 rf401_importttreethx.C:151
 rf401_importttreethx.C:152
 rf401_importttreethx.C:153
 rf401_importttreethx.C:154
 rf401_importttreethx.C:155
 rf401_importttreethx.C:156
 rf401_importttreethx.C:157
 rf401_importttreethx.C:158
 rf401_importttreethx.C:159
 rf401_importttreethx.C:160
 rf401_importttreethx.C:161
 rf401_importttreethx.C:162
 rf401_importttreethx.C:163
 rf401_importttreethx.C:164