rf402_datahandling.cxx

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 "RooCategory.h"
00021 #include "TCanvas.h"
00022 #include "RooPlot.h"
00023 #include "TFile.h"
00024 using namespace RooFit ;
00025 
00026 
00027 class TestBasic402 : public RooFitTestUnit
00028 {
00029 public: 
00030   TestBasic402(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Basic operations on datasets",refFile,writeRef,verbose) {} ;
00031   Bool_t testCode() {
00032 
00033   // Binned (RooDataHist) and unbinned datasets (RooDataSet) share
00034   // many properties and inherit from a common abstract base class
00035   // (RooAbsData), that provides an interface for all operations
00036   // that can be performed regardless of the data format
00037 
00038   RooRealVar  x("x","x",-10,10) ;
00039   RooRealVar  y("y","y", 0, 40) ;
00040   RooCategory c("c","c") ;
00041   c.defineType("Plus",+1) ;
00042   c.defineType("Minus",-1) ;
00043 
00044 
00045 
00046   // 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 
00047   // --------------------------------------------------------------
00048 
00049   // RooDataSet is an unbinned dataset (a collection of points in N-dimensional space)
00050   RooDataSet d("d","d",RooArgSet(x,y,c)) ;
00051 
00052   // Unlike RooAbsArgs (RooAbsPdf,RooFormulaVar,....) datasets are not attached to 
00053   // the variables they are constructed from. Instead they are attached to an internal 
00054   // clone of the supplied set of arguments
00055 
00056   // Fill d with dummy values
00057   Int_t i ;
00058   for (i=0 ; i<1000 ; i++) {
00059     x = i/50 - 10 ;
00060     y = sqrt(1.0*i) ;
00061     c.setLabel((i%2)?"Plus":"Minus") ;
00062 
00063     // We must explicitly refer to x,y,c here to pass the values because
00064     // d is not linked to them (as explained above)
00065     d.add(RooArgSet(x,y,c)) ;
00066   }
00067 
00068 
00069   // 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
00070   // -------------------------------------------------------------
00071 
00072   // The reduce() function returns a new dataset which is a subset of the original
00073   RooDataSet* d1 = (RooDataSet*) d.reduce(RooArgSet(x,c)) ; 
00074   RooDataSet* d2 = (RooDataSet*) d.reduce(RooArgSet(y)) ;   
00075   RooDataSet* d3 = (RooDataSet*) d.reduce("y>5.17") ; 
00076   RooDataSet* d4 = (RooDataSet*) d.reduce(RooArgSet(x,c),"y>5.17") ; 
00077 
00078   regValue(d3->numEntries(),"rf403_nd3") ;
00079   regValue(d4->numEntries(),"rf403_nd4") ;
00080 
00081   // The merge() function adds two data set column-wise
00082   d1->merge(d2) ;
00083 
00084   // The append() function addes two datasets row-wise
00085   d1->append(*d3) ;
00086 
00087   regValue(d1->numEntries(),"rf403_nd1") ;
00088 
00089   
00090 
00091 
00092   // 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 
00093   // ---------------------------------------------------------
00094 
00095   // A binned dataset can be constructed empty, from an unbinned dataset, or
00096   // from a ROOT native histogram (TH1,2,3)
00097 
00098   // The binning of real variables (like x,y) is done using their fit range
00099   // 'get/setRange()' and number of specified fit bins 'get/setBins()'.
00100   // Category dimensions of binned datasets get one bin per defined category state
00101   x.setBins(10) ;
00102   y.setBins(10) ;
00103   RooDataHist dh("dh","binned version of d",RooArgSet(x,y),d) ;
00104 
00105   RooPlot* yframe = y.frame(Bins(10),Title("Operations on binned datasets")) ;
00106   dh.plotOn(yframe) ; // plot projection of 2D binned data on y
00107 
00108   // Reduce the 2-dimensional binned dataset to a 1-dimensional binned dataset
00109   //
00110   // All reduce() methods are interfaced in RooAbsData. All reduction techniques
00111   // demonstrated on unbinned datasets can be applied to binned datasets as well.
00112   RooDataHist* dh2 = (RooDataHist*) dh.reduce(y,"x>0") ;
00113 
00114   // Add dh2 to yframe and redraw
00115   dh2->plotOn(yframe,LineColor(kRed),MarkerColor(kRed),Name("dh2")) ;
00116 
00117   regPlot(yframe,"rf402_plot1") ;
00118 
00119   delete d1 ;
00120   delete d2 ;
00121   delete d3 ;
00122   delete d4 ;
00123   delete dh2 ;
00124   return kTRUE ;
00125   }
00126 } ;

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