rf102_dataimport.C

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'BASIC FUNCTIONALITY' RooFit tutorial macro #102
00004 // 
00005 // Importing data from ROOT TTrees and THx histograms
00006 //
00007 //
00008 //
00009 // 07/2008 - Wouter Verkerke 
00010 // 
00011 /////////////////////////////////////////////////////////////////////////
00012 
00013 #ifndef __CINT__
00014 #include "RooGlobalFunc.h"
00015 #endif
00016 #include "RooRealVar.h"
00017 #include "RooDataSet.h"
00018 #include "RooDataHist.h"
00019 #include "RooGaussian.h"
00020 #include "TCanvas.h"
00021 #include "RooPlot.h"
00022 #include "TTree.h"
00023 #include "TH1D.h"
00024 #include "TRandom.h"
00025 using namespace RooFit ;
00026 
00027 TH1* makeTH1() ;
00028 TTree* makeTTree() ;
00029 
00030 
00031 void rf102_dataimport()
00032 {
00033   ////////////////////////////////////////////////////////
00034   // I m p o r t i n g   R O O T   h i s t o g r a m s  //
00035   ////////////////////////////////////////////////////////
00036 
00037   // I m p o r t   T H 1   i n t o   a   R o o D a t a H i s t
00038   // ---------------------------------------------------------
00039 
00040   // Create a ROOT TH1 histogram
00041   TH1* hh = makeTH1() ;
00042 
00043   // Declare observable x
00044   RooRealVar x("x","x",-10,10) ;
00045 
00046   // Create a binned dataset that imports contents of TH1 and associates its contents to observable 'x'
00047   RooDataHist dh("dh","dh",x,Import(*hh)) ;
00048 
00049 
00050   // P l o t   a n d   f i t   a   R o o D a t a H i s t
00051   // ---------------------------------------------------
00052 
00053   // Make plot of binned dataset showing Poisson error bars (RooFit default)
00054   RooPlot* frame = x.frame(Title("Imported TH1 with Poisson error bars")) ;
00055   dh.plotOn(frame) ; 
00056 
00057   // Fit a Gaussian p.d.f to the data
00058   RooRealVar mean("mean","mean",0,-10,10) ;
00059   RooRealVar sigma("sigma","sigma",3,0.1,10) ;
00060   RooGaussian gauss("gauss","gauss",x,mean,sigma) ;
00061   gauss.fitTo(dh) ;
00062   gauss.plotOn(frame) ;
00063 
00064   // P l o t   a n d   f i t   a   R o o D a t a H i s t   w i t h   i n t e r n a l   e r r o r s
00065   // ---------------------------------------------------------------------------------------------
00066 
00067   // If histogram has custom error (i.e. its contents is does not originate from a Poisson process
00068   // but e.g. is a sum of weighted events) you can data with symmetric 'sum-of-weights' error instead
00069   // (same error bars as shown by ROOT)
00070   RooPlot* frame2 = x.frame(Title("Imported TH1 with internal errors")) ;
00071   dh.plotOn(frame2,DataError(RooAbsData::SumW2)) ; 
00072   gauss.plotOn(frame2) ;
00073 
00074   // Please note that error bars shown (Poisson or SumW2) are for visualization only, the are NOT used
00075   // in a maximum likelihood fit
00076   //
00077   // A (binned) ML fit will ALWAYS assume the Poisson error interpretation of data (the mathematical definition 
00078   // of likelihood does not take any external definition of errors). Data with non-unit weights can only be correctly
00079   // fitted with a chi^2 fit (see rf602_chi2fit.C) 
00080 
00081 
00082   ////////////////////////////////////////////////
00083   // I m p o r t i n g   R O O T  T T r e e s   //
00084   ////////////////////////////////////////////////
00085 
00086 
00087   // I m p o r t   T T r e e   i n t o   a   R o o D a t a S e t
00088   // -----------------------------------------------------------
00089 
00090   TTree* tree = makeTTree() ;
00091 
00092   // Define 2nd observable y
00093   RooRealVar y("y","y",-10,10) ;
00094 
00095   // Construct unbinned dataset importing tree branches x and y matching between branches and RooRealVars 
00096   // is done by name of the branch/RRV 
00097   // 
00098   // Note that ONLY entries for which x,y have values within their allowed ranges as defined in 
00099   // RooRealVar x and y are imported. Since the y values in the import tree are in the range [-15,15]
00100   // and RRV y defines a range [-10,10] this means that the RooDataSet below will have less entries than the TTree 'tree'
00101 
00102   RooDataSet ds("ds","ds",RooArgSet(x,y),Import(*tree)) ;
00103 
00104 
00105   // P l o t   d a t a s e t   w i t h   m u l t i p l e   b i n n i n g   c h o i c e s
00106   // ------------------------------------------------------------------------------------
00107   
00108   // Print number of events in dataset
00109   ds.Print() ;
00110 
00111   // Print unbinned dataset with default frame binning (100 bins)
00112   RooPlot* frame3 = y.frame(Title("Unbinned data shown in default frame binning")) ;
00113   ds.plotOn(frame3) ;
00114   
00115   // Print unbinned dataset with custom binning choice (20 bins)
00116   RooPlot* frame4 = y.frame(Title("Unbinned data shown with custom binning")) ;
00117   ds.plotOn(frame4,Binning(20)) ;
00118   
00119   // Draw all frames on a canvas
00120   TCanvas* c = new TCanvas("rf102_dataimport","rf102_dataimport",800,800) ;
00121   c->Divide(2,2) ;
00122   c->cd(1) ; gPad->SetLeftMargin(0.15) ; frame->GetYaxis()->SetTitleOffset(1.4) ; frame->Draw() ;
00123   c->cd(2) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.4) ; frame2->Draw() ;
00124   c->cd(3) ; gPad->SetLeftMargin(0.15) ; frame3->GetYaxis()->SetTitleOffset(1.4) ; frame3->Draw() ;
00125   c->cd(4) ; gPad->SetLeftMargin(0.15) ; frame4->GetYaxis()->SetTitleOffset(1.4) ; frame4->Draw() ;
00126   
00127 }
00128 
00129 
00130 
00131 
00132 TH1* makeTH1() 
00133 {
00134   // Create ROOT TH1 filled with a Gaussian distribution
00135 
00136   TH1D* hh = new TH1D("hh","hh",25,-10,10) ;
00137   for (int i=0 ; i<100 ; i++) {
00138     hh->Fill(gRandom->Gaus(0,3)) ;
00139   }
00140   return hh ;
00141 }
00142 
00143 
00144 TTree* makeTTree() 
00145 {
00146   // Create ROOT TTree filled with a Gaussian distribution in x and a uniform distribution in y
00147 
00148   TTree* tree = new TTree("tree","tree") ;
00149   Double_t* px = new Double_t ;
00150   Double_t* py = new Double_t ;
00151   tree->Branch("x",px,"x/D") ;
00152   tree->Branch("y",py,"y/D") ;
00153   for (int i=0 ; i<100 ; i++) {
00154     *px = gRandom->Gaus(0,3) ;
00155     *py = gRandom->Uniform()*30 - 15 ;
00156     tree->Fill() ;
00157   }
00158   return tree ;
00159 }
00160 
00161 

Generated on Tue Jul 5 15:44:54 2011 for ROOT_528-00b_version by  doxygen 1.5.1