rf402_datahandling.C

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'DATA AND CATEGORIES' RooFit tutorial macro #402
00004 // 
00005 // Tools for manipulation of (un)binned datasets
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 "RooConstVar.h"
00021 #include "RooCategory.h"
00022 #include "TCanvas.h"
00023 #include "TAxis.h"
00024 #include "RooPlot.h"
00025 #include "TFile.h"
00026 using namespace RooFit ;
00027 
00028 // WVE Add reduction by range
00029 
00030 
00031 void rf402_datahandling()
00032 {
00033 
00034   // Binned (RooDataHist) and unbinned datasets (RooDataSet) share
00035   // many properties and inherit from a common abstract base class
00036   // (RooAbsData), that provides an interface for all operations
00037   // that can be performed regardless of the data format
00038 
00039   RooRealVar  x("x","x",-10,10) ;
00040   RooRealVar  y("y","y", 0, 40) ;
00041   RooCategory c("c","c") ;
00042   c.defineType("Plus",+1) ;
00043   c.defineType("Minus",-1) ;
00044 
00045 
00046 
00047   // B a s i c   O p e r a t i o n s   o n   u n b i n n e d   d a t a s e t s 
00048   // --------------------------------------------------------------
00049 
00050   // RooDataSet is an unbinned dataset (a collection of points in N-dimensional space)
00051   RooDataSet d("d","d",RooArgSet(x,y,c)) ;
00052 
00053   // Unlike RooAbsArgs (RooAbsPdf,RooFormulaVar,....) datasets are not attached to 
00054   // the variables they are constructed from. Instead they are attached to an internal 
00055   // clone of the supplied set of arguments
00056 
00057   // Fill d with dummy values
00058   Int_t i ;
00059   for (i=0 ; i<1000 ; i++) {
00060     x = i/50 - 10 ;
00061     y = sqrt(1.0*i) ;
00062     c.setLabel((i%2)?"Plus":"Minus") ;
00063 
00064     // We must explicitly refer to x,y,c here to pass the values because
00065     // d is not linked to them (as explained above)
00066     d.add(RooArgSet(x,y,c)) ;
00067   }
00068   d.Print("v") ;
00069   cout << endl ;
00070 
00071   // The get() function returns a pointer to the internal copy of the RooArgSet(x,y,c)
00072   // supplied in the constructor
00073   const RooArgSet* row = d.get() ;
00074   row->Print("v") ;
00075   cout << endl ;
00076 
00077   // Get with an argument loads a specific data point in row and returns
00078   // a pointer to row argset. get() always returns the same pointer, unless
00079   // an invalid row number is specified. In that case a null ptr is returned
00080   d.get(900)->Print("v") ;
00081   cout << endl ;
00082 
00083 
00084 
00085   // R e d u c i n g ,   A p p e n d i n g   a n d   M e r g i n g
00086   // -------------------------------------------------------------
00087 
00088   // The reduce() function returns a new dataset which is a subset of the original
00089   cout << endl << ">> d1 has only columns x,c" << endl ;
00090   RooDataSet* d1 = (RooDataSet*) d.reduce(RooArgSet(x,c)) ; 
00091   d1->Print("v") ;
00092 
00093   cout << endl << ">> d2 has only column y" << endl ;
00094   RooDataSet* d2 = (RooDataSet*) d.reduce(RooArgSet(y)) ;   
00095   d2->Print("v") ;
00096 
00097   cout << endl << ">> d3 has only the points with y>5.17" << endl ;
00098   RooDataSet* d3 = (RooDataSet*) d.reduce("y>5.17") ; 
00099   d3->Print("v") ;
00100 
00101   cout << endl << ">> d4 has only columns x,c for data points with y>5.17" << endl ;
00102   RooDataSet* d4 = (RooDataSet*) d.reduce(RooArgSet(x,c),"y>5.17") ; 
00103   d4->Print("v") ;
00104 
00105   // The merge() function adds two data set column-wise
00106   cout << endl << ">> merge d2(y) with d1(x,c) to form d1(x,c,y)" << endl ;
00107   d1->merge(d2) ; 
00108   d1->Print("v") ;
00109 
00110   // The append() function addes two datasets row-wise
00111   cout << endl << ">> append data points of d3 to d1" << endl ;
00112   d1->append(*d3) ;
00113   d1->Print("v") ;  
00114 
00115 
00116 
00117   // O p e r a t i o n s   o n   b i n n e d   d a t a s e t s 
00118   // ---------------------------------------------------------
00119 
00120   // A binned dataset can be constructed empty, from an unbinned dataset, or
00121   // from a ROOT native histogram (TH1,2,3)
00122 
00123   cout << ">> construct dh (binned) from d(unbinned) but only take the x and y dimensions," << endl 
00124        << ">> the category 'c' will be projected in the filling process" << endl ;
00125     
00126   // The binning of real variables (like x,y) is done using their fit range
00127   // 'get/setRange()' and number of specified fit bins 'get/setBins()'.
00128   // Category dimensions of binned datasets get one bin per defined category state
00129   x.setBins(10) ;
00130   y.setBins(10) ;
00131   RooDataHist dh("dh","binned version of d",RooArgSet(x,y),d) ;
00132   dh.Print("v") ;
00133 
00134   RooPlot* yframe = y.frame(Bins(10),Title("Operations on binned datasets")) ;
00135   dh.plotOn(yframe) ; // plot projection of 2D binned data on y
00136 
00137   // Examine the statistics of a binned dataset
00138   cout << ">> number of bins in dh   : " << dh.numEntries() << endl ;
00139   cout << ">> sum of weights in dh   : " << dh.sum(kFALSE)  << endl ;
00140   cout << ">> integral over histogram: " << dh.sum(kTRUE)   << endl ; // accounts for bin volume
00141 
00142   // Locate a bin from a set of coordinates and retrieve its properties
00143   x = 0.3 ;  y = 20.5 ;
00144   cout << ">> retrieving the properties of the bin enclosing coordinate (x,y) = (0.3,20.5) " << endl ;
00145   cout << " bin center:" << endl ;
00146   dh.get(RooArgSet(x,y))->Print("v") ; // load bin center coordinates in internal buffer
00147   cout << " weight = " << dh.weight() << endl ; // return weight of last loaded coordinates
00148 
00149   // Reduce the 2-dimensional binned dataset to a 1-dimensional binned dataset
00150   //
00151   // All reduce() methods are interfaced in RooAbsData. All reduction techniques
00152   // demonstrated on unbinned datasets can be applied to binned datasets as well.
00153   cout << ">> Creating 1-dimensional projection on y of dh for bins with x>0" << endl ;
00154   RooDataHist* dh2 = (RooDataHist*) dh.reduce(y,"x>0") ;
00155   dh2->Print("v") ;
00156 
00157   // Add dh2 to yframe and redraw
00158   dh2->plotOn(yframe,LineColor(kRed),MarkerColor(kRed)) ;
00159 
00160 
00161 
00162   // S a v i n g   a n d   l o a d i n g   f r o m   f i l e 
00163   // -------------------------------------------------------
00164 
00165   // Datasets can be persisted with ROOT I/O
00166   cout << endl << ">> Persisting d via ROOT I/O" << endl ;
00167   TFile f("rf402_datahandling.root","RECREATE") ;
00168   d.Write() ;
00169   f.ls() ;
00170 
00171   // To read back in future session:
00172   // > TFile f("rf402_datahandling.root") ;
00173   // > RooDataSet* d = (RooDataSet*) f.FindObject("d") ;
00174   
00175 
00176 
00177   new TCanvas("rf402_datahandling","rf402_datahandling",600,600) ;
00178   gPad->SetLeftMargin(0.15) ; yframe->GetYaxis()->SetTitleOffset(1.4) ; yframe->Draw() ;
00179 
00180 }

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