Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
rf504_simwstool.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -nodraw
4 /// Organisation and simultaneous fits: using RooSimWSTool to construct a simultaneous p.d.f that is built of variations
5 /// of an input p.d.f
6 ///
7 /// \macro_output
8 /// \macro_code
9 /// \author 07/2008 - Wouter Verkerke
10 
11 #include "RooRealVar.h"
12 #include "RooCategory.h"
13 #include "RooDataSet.h"
14 #include "RooGaussian.h"
15 #include "RooConstVar.h"
16 #include "RooPolynomial.h"
17 #include "RooSimultaneous.h"
18 #include "RooAddPdf.h"
19 #include "RooWorkspace.h"
20 #include "RooSimWSTool.h"
21 #include "RooPlot.h"
22 #include "TCanvas.h"
23 #include "TAxis.h"
24 #include "TFile.h"
25 #include "TH1.h"
26 using namespace RooFit;
27 
28 void rf504_simwstool()
29 {
30  // C r e a t e m a s t e r p d f
31  // ---------------------------------
32 
33  // Construct gauss(x,m,s)
34  RooRealVar x("x", "x", -10, 10);
35  RooRealVar m("m", "m", 0, -10, 10);
36  RooRealVar s("s", "s", 1, -10, 10);
37  RooGaussian gauss("g", "g", x, m, s);
38 
39  // Construct poly(x,p0)
40  RooRealVar p0("p0", "p0", 0.01, 0., 1.);
41  RooPolynomial poly("p", "p", x, p0);
42 
43  // Construct model = f*gauss(x) + (1-f)*poly(x)
44  RooRealVar f("f", "f", 0.5, 0., 1.);
45  RooAddPdf model("model", "model", RooArgSet(gauss, poly), f);
46 
47  // C r e a t e c a t e g o r y o b s e r v a b l e s f o r s p l i t t i n g
48  // ----------------------------------------------------------------------------------
49 
50  // Define two categories that can be used for splitting
51  RooCategory c("c", "c");
52  c.defineType("run1");
53  c.defineType("run2");
54 
55  RooCategory d("d", "d");
56  d.defineType("foo");
57  d.defineType("bar");
58 
59  // S e t u p S i m W S T o o l
60  // -----------------------------
61 
62  // Import ingredients in a workspace
63  RooWorkspace w("w", "w");
64  w.import(RooArgSet(model, c, d));
65 
66  // Make Sim builder tool
67  RooSimWSTool sct(w);
68 
69  // B u i l d a s i m u l t a n e o u s m o d e l w i t h o n e s p l i t
70  // ---------------------------------------------------------------------------------
71 
72  // Construct a simultaneous p.d.f with the following form
73  //
74  // model_run1(x) = f*gauss_run1(x,m_run1,s) + (1-f)*poly
75  // model_run2(x) = f*gauss_run2(x,m_run2,s) + (1-f)*poly
76  // simpdf(x,c) = model_run1(x) if c=="run1"
77  // = model_run2(x) if c=="run2"
78  //
79  // Returned p.d.f is owned by the workspace
80  RooSimultaneous *model_sim = sct.build("model_sim", "model", SplitParam("m", "c"));
81 
82  // Print tree structure of model
83  model_sim->Print("t");
84 
85  // Adjust model_sim parameters in workspace
86  w.var("m_run1")->setVal(-3);
87  w.var("m_run2")->setVal(+3);
88 
89  // Print contents of workspace
90  w.Print("v");
91 
92  // B u i l d a s i m u l t a n e o u s m o d e l w i t h p r o d u c t s p l i t
93  // -----------------------------------------------------------------------------------------
94 
95  // Build another simultaneous p.d.f using a composite split in states c X d
96  RooSimultaneous *model_sim2 = sct.build("model_sim2", "model", SplitParam("p0", "c,d"));
97 
98  // Print tree structure of this model
99  model_sim2->Print("t");
100 }