ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
rf511_wsfactory_basic.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// 'ORGANIZATION AND SIMULTANEOUS FITS' RooFit tutorial macro #511
4 ///
5 /// Basic use of the 'object factory' associated with a workspace
6 /// to rapidly build p.d.f.s functions and their parameter components
7 ///
8 ///
9 /// \macro_code
10 /// \author 04/2009 - Wouter Verkerke
11 
12 
13 #ifndef __CINT__
14 #include "RooGlobalFunc.h"
15 #endif
16 #include "RooRealVar.h"
17 #include "RooDataSet.h"
18 #include "RooGaussian.h"
19 #include "RooConstVar.h"
20 #include "RooChebychev.h"
21 #include "RooAddPdf.h"
22 #include "RooWorkspace.h"
23 #include "RooPlot.h"
24 #include "TCanvas.h"
25 #include "TAxis.h"
26 using namespace RooFit ;
27 
28 
29 void rf511_wsfactory_basic(Bool_t compact=kFALSE)
30 {
31  RooWorkspace* w = new RooWorkspace("w") ;
32 
33  // C r e a t i n g a n d a d d i n g b a s i c p . d . f . s
34  // ----------------------------------------------------------------
35 
36 
37  // Remake example p.d.f. of tutorial rs502_wspacewrite.C:
38  //
39  // Basic p.d.f. construction: ClassName::ObjectName(constructor arguments)
40  // Variable construction : VarName[x,xlo,xhi], VarName[xlo,xhi], VarName[x]
41  // P.d.f. addition : SUM::ObjectName(coef1*pdf1,...coefM*pdfM,pdfN)
42  //
43 
44  if (!compact) {
45 
46  // Use object factory to build p.d.f. of tutorial rs502_wspacewrite
47  w->factory("Gaussian::sig1(x[-10,10],mean[5,0,10],0.5)") ;
48  w->factory("Gaussian::sig2(x,mean,1)") ;
49  w->factory("Chebychev::bkg(x,{a0[0.5,0.,1],a1[-0.2,0.,1.]})") ;
50  w->factory("SUM::sig(sig1frac[0.8,0.,1.]*sig1,sig2)") ;
51  w->factory("SUM::model(bkgfrac[0.5,0.,1.]*bkg,sig)") ;
52 
53  } else {
54 
55  // Use object factory to build p.d.f. of tutorial rs502_wspacewrite but
56  // - Contracted to a single line recursive expression,
57  // - Omitting explicit names for components that are not referred to explicitly later
58 
59  w->factory("SUM::model(bkgfrac[0.5,0.,1.]*Chebychev::bkg(x[-10,10],{a0[0.5,0.,1],a1[-0.2,0.,1.]}),"
60  "SUM(sig1frac[0.8,0.,1.]*Gaussian(x,mean[5,0,10],0.5), Gaussian(x,mean,1)))") ;
61  }
62 
63 
64  // A d v a n c e d p . d . f . c o n s t r u c t o r a r g u m e n t s
65  // ----------------------------------------------------------------
66  //
67  // P.d.f. constructor arguments may by any type of RooAbsArg, but also
68  //
69  // Double_t --> converted to RooConst(...)
70  // {a,b,c} --> converted to RooArgSet() or RooArgList() depending on required ctor arg
71  // dataset name --> convered to RooAbsData reference for any dataset residing in the workspace
72  // enum --> Any enum label that belongs to an enum defined in the (base) class
73 
74 
75  // Make a dummy dataset p.d.f. 'model' and import it in the workspace
76  RooDataSet* data = w->pdf("model")->generate(*w->var("x"),1000) ;
77  w->import(*data,Rename("data")) ;
78 
79  // Construct a KEYS p.d.f. passing a dataset name and an enum type defining the
80  // mirroring strategy
81  w->factory("KeysPdf::k(x,data,NoMirror,0.2)") ;
82 
83 
84  // Print workspace contents
85  w->Print() ;
86 
87  // Make workspace visible on command line
88  gDirectory->Add(w) ;
89 
90 }
91 
92 
friend class RooWorkspace
Definition: RooAbsArg.h:472
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
RooAbsPdf * pdf(const char *name) const
Retrieve p.d.f (RooAbsPdf) with given name. A null pointer is returned if not found.
RooCmdArg Rename(const char *suffix)
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
RooRealVar * var(const char *name) const
Retrieve real-valued variable (RooRealVar) with given name. A null pointer is returned if not found...
RooFactoryWSTool & factory()
Return instance to factory tool.
Bool_t import(const RooAbsArg &arg, const RooCmdArg &arg1=RooCmdArg(), 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(), const RooCmdArg &arg9=RooCmdArg())
Import a RooAbsArg object, e.g.
void Print(Option_t *opts=0) const
Print contents of the workspace.
RooDataSet * generate(const RooArgSet &whatVars, Int_t nEvents, const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none())
Generate a new dataset containing the specified variables with events sampled from our distribution...
Definition: RooAbsPdf.cxx:1702
#define gDirectory
Definition: TDirectory.h:221
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:42