ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
rs_numberCountingCombination.C
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////
2 //
3 // 'Number Counting Example' RooStats tutorial macro #100
4 // author: Kyle Cranmer
5 // date Nov. 2008
6 //
7 // This tutorial shows an example of a combination of
8 // two searches using number counting with background uncertainty.
9 //
10 // The macro uses a RooStats "factory" to construct a PDF
11 // that represents the two number counting analyses with background
12 // uncertainties. The uncertainties are taken into account by
13 // considering a sideband measurement of a size that corresponds to the
14 // background uncertainty. The problem has been studied in these references:
15 // http://arxiv.org/abs/physics/0511028
16 // http://arxiv.org/abs/physics/0702156
17 // http://cdsweb.cern.ch/record/1099969?ln=en
18 //
19 // After using the factory to make the model, we use a RooStats
20 // ProfileLikelihoodCalculator for a Hypothesis test and a confidence interval.
21 // The calculator takes into account systematics by eliminating nuisance parameters
22 // with the profile likelihood. This is equivalent to the method of MINOS.
23 //
24 /////////////////////////////////////////////////////////////////////////
25 
26 #ifndef __CINT__
27 #include "RooGlobalFunc.h"
28 #endif
31 #include "RooStats/ConfInterval.h"
34 #include "RooRealVar.h"
35 
36 // use this order for safety on library loading
37 using namespace RooFit ;
38 using namespace RooStats ;
39 
40 
41 // declare three variations on the same tutorial
45 
46 ////////////////////////////////////////////
47 // main driver to choose one
49 {
50  if(flag==1)
52  if(flag==2)
54  if(flag==3)
56 }
57 
58 /////////////////////////////////////////////
60 {
61 
62  /////////////////////////////////////////
63  // An example of a number counting combination with two channels.
64  // We consider both hypothesis testing and the equivalent confidence interval.
65  /////////////////////////////////////////
66 
67 
68  /////////////////////////////////////////
69  // The Model building stage
70  /////////////////////////////////////////
71 
72  // Step 1, define arrays with signal & bkg expectations and background uncertainties
73  Double_t s[2] = {20.,10.}; // expected signal
74  Double_t b[2] = {100.,100.}; // expected background
75  Double_t db[2] = {.0100,.0100}; // fractional background uncertainty
76 
77 
78  // Step 2, use a RooStats factory to build a PDF for a
79  // number counting combination and add it to the workspace.
80  // We need to give the signal expectation to relate the masterSignal
81  // to the signal contribution in the individual channels.
82  // The model neglects correlations in background uncertainty,
83  // but they could be added without much change to the example.
85  RooWorkspace* wspace = new RooWorkspace();
86  f.AddModel(s,2,wspace,"TopLevelPdf", "masterSignal");
87 
88  // Step 3, use a RooStats factory to add datasets to the workspace.
89  // Step 3a.
90  // Add the expected data to the workspace
91  f.AddExpData(s, b, db, 2, wspace, "ExpectedNumberCountingData");
92 
93  // see below for a printout of the workspace
94  // wspace->Print(); //uncomment to see structure of workspace
95 
96  /////////////////////////////////////////
97  // The Hypothesis testing stage:
98  /////////////////////////////////////////
99  // Step 4, Define the null hypothesis for the calculator
100  // Here you need to know the name of the variables corresponding to hypothesis.
101  RooRealVar* mu = wspace->var("masterSignal");
102  RooArgSet* poi = new RooArgSet(*mu);
103  RooArgSet* nullParams = new RooArgSet("nullParams");
104  nullParams->addClone(*mu);
105  // here we explicitly set the value of the parameters for the null
106  nullParams->setRealValue("masterSignal",0);
107 
108  // Step 5, Create a calculator for doing the hypothesis test.
109  // because this is a
110  ProfileLikelihoodCalculator plc( *wspace->data("ExpectedNumberCountingData"),
111  *wspace->pdf("TopLevelPdf"), *poi, 0.05, nullParams);
112 
113 
114  // Step 6, Use the Calculator to get a HypoTestResult
115  HypoTestResult* htr = plc.GetHypoTest();
116  assert(htr != 0);
117  cout << "-------------------------------------------------" << endl;
118  cout << "The p-value for the null is " << htr->NullPValue() << endl;
119  cout << "Corresponding to a signifcance of " << htr->Significance() << endl;
120  cout << "-------------------------------------------------\n\n" << endl;
121 
122  /* expected case should return:
123  -------------------------------------------------
124  The p-value for the null is 0.015294
125  Corresponding to a signifcance of 2.16239
126  -------------------------------------------------
127  */
128 
129  //////////////////////////////////////////
130  // Confidence Interval Stage
131 
132  // Step 8, Here we re-use the ProfileLikelihoodCalculator to return a confidence interval.
133  // We need to specify what are our parameters of interest
134  RooArgSet* paramsOfInterest = nullParams; // they are the same as before in this case
135  plc.SetParameters(*paramsOfInterest);
136  LikelihoodInterval* lrint = (LikelihoodInterval*) plc.GetInterval(); // that was easy.
137  lrint->SetConfidenceLevel(0.95);
138 
139  // Step 9, make a plot of the likelihood ratio and the interval obtained
140  //paramsOfInterest->setRealValue("masterSignal",1.);
141  // find limits
142  double lower = lrint->LowerLimit(*mu);
143  double upper = lrint->UpperLimit(*mu);
144 
145  LikelihoodIntervalPlot lrPlot(lrint);
146  lrPlot.SetMaximum(3.);
147  lrPlot.Draw();
148 
149  // Step 10a. Get upper and lower limits
150  cout << "lower limit on master signal = " << lower << endl;
151  cout << "upper limit on master signal = " << upper << endl;
152 
153 
154  // Step 10b, Ask if masterSignal=0 is in the interval.
155  // Note, this is equivalent to the question of a 2-sigma hypothesis test:
156  // "is the parameter point masterSignal=0 inside the 95% confidence interval?"
157  // Since the signficance of the Hypothesis test was > 2-sigma it should not be:
158  // eg. we exclude masterSignal=0 at 95% confidence.
159  paramsOfInterest->setRealValue("masterSignal",0.);
160  cout << "-------------------------------------------------" << endl;
161  std::cout << "Consider this parameter point:" << std::endl;
162  paramsOfInterest->first()->Print();
163  if( lrint->IsInInterval(*paramsOfInterest) )
164  std::cout << "It IS in the interval." << std::endl;
165  else
166  std::cout << "It is NOT in the interval." << std::endl;
167  cout << "-------------------------------------------------\n\n" << endl;
168 
169  // Step 10c, We also ask about the parameter point masterSignal=2, which is inside the interval.
170  paramsOfInterest->setRealValue("masterSignal",2.);
171  cout << "-------------------------------------------------" << endl;
172  std::cout << "Consider this parameter point:" << std::endl;
173  paramsOfInterest->first()->Print();
174  if( lrint->IsInInterval(*paramsOfInterest) )
175  std::cout << "It IS in the interval." << std::endl;
176  else
177  std::cout << "It is NOT in the interval." << std::endl;
178  cout << "-------------------------------------------------\n\n" << endl;
179 
180 
181  delete lrint;
182  delete htr;
183  delete wspace;
184  delete poi;
185  delete nullParams;
186 
187 
188 
189  /*
190  // Here's an example of what is in the workspace
191  // wspace->Print();
192  RooWorkspace(NumberCountingWS) Number Counting WS contents
193 
194  variables
195  ---------
196  (x_0,masterSignal,expected_s_0,b_0,y_0,tau_0,x_1,expected_s_1,b_1,y_1,tau_1)
197 
198  p.d.f.s
199  -------
200  RooProdPdf::joint[ pdfs=(sigRegion_0,sideband_0,sigRegion_1,sideband_1) ] = 2.20148e-08
201  RooPoisson::sigRegion_0[ x=x_0 mean=splusb_0 ] = 0.036393
202  RooPoisson::sideband_0[ x=y_0 mean=bTau_0 ] = 0.00398939
203  RooPoisson::sigRegion_1[ x=x_1 mean=splusb_1 ] = 0.0380088
204  RooPoisson::sideband_1[ x=y_1 mean=bTau_1 ] = 0.00398939
205 
206  functions
207  --------
208  RooAddition::splusb_0[ set1=(s_0,b_0) set2=() ] = 120
209  RooProduct::s_0[ compRSet=(masterSignal,expected_s_0) compCSet=() ] = 20
210  RooProduct::bTau_0[ compRSet=(b_0,tau_0) compCSet=() ] = 10000
211  RooAddition::splusb_1[ set1=(s_1,b_1) set2=() ] = 110
212  RooProduct::s_1[ compRSet=(masterSignal,expected_s_1) compCSet=() ] = 10
213  RooProduct::bTau_1[ compRSet=(b_1,tau_1) compCSet=() ] = 10000
214 
215  datasets
216  --------
217  RooDataSet::ExpectedNumberCountingData(x_0,y_0,x_1,y_1)
218 
219  embedded precalculated expensive components
220  -------------------------------------------
221  */
222 
223 }
224 
225 
226 
228 {
229 
230  /////////////////////////////////////////
231  // The same example with observed data in a main
232  // measurement and an background-only auxiliary
233  // measurement with a factor tau more background
234  // than in the main measurement.
235 
236  /////////////////////////////////////////
237  // The Model building stage
238  /////////////////////////////////////////
239 
240  // Step 1, define arrays with signal & bkg expectations and background uncertainties
241  // We still need the expectation to relate signal in different channels with the master signal
242  Double_t s[2] = {20.,10.}; // expected signal
243 
244 
245  // Step 2, use a RooStats factory to build a PDF for a
246  // number counting combination and add it to the workspace.
247  // We need to give the signal expectation to relate the masterSignal
248  // to the signal contribution in the individual channels.
249  // The model neglects correlations in background uncertainty,
250  // but they could be added without much change to the example.
252  RooWorkspace* wspace = new RooWorkspace();
253  f.AddModel(s,2,wspace,"TopLevelPdf", "masterSignal");
254 
255  // Step 3, use a RooStats factory to add datasets to the workspace.
256  // Add the observed data to the workspace
257  Double_t mainMeas[2] = {123.,117.}; // observed main measurement
258  Double_t bkgMeas[2] = {111.23,98.76}; // observed background
259  Double_t dbMeas[2] = {.011,.0095}; // observed fractional background uncertainty
260  f.AddData(mainMeas, bkgMeas, dbMeas, 2, wspace,"ObservedNumberCountingData");
261 
262  // see below for a printout of the workspace
263  // wspace->Print(); //uncomment to see structure of workspace
264 
265  /////////////////////////////////////////
266  // The Hypothesis testing stage:
267  /////////////////////////////////////////
268  // Step 4, Define the null hypothesis for the calculator
269  // Here you need to know the name of the variables corresponding to hypothesis.
270  RooRealVar* mu = wspace->var("masterSignal");
271  RooArgSet* poi = new RooArgSet(*mu);
272  RooArgSet* nullParams = new RooArgSet("nullParams");
273  nullParams->addClone(*mu);
274  // here we explicitly set the value of the parameters for the null
275  nullParams->setRealValue("masterSignal",0);
276 
277  // Step 5, Create a calculator for doing the hypothesis test.
278  // because this is a
279  ProfileLikelihoodCalculator plc( *wspace->data("ObservedNumberCountingData"),
280  *wspace->pdf("TopLevelPdf"), *poi, 0.05, nullParams);
281 
282  wspace->var("tau_0")->Print();
283  wspace->var("tau_1")->Print();
284 
285  // Step 7, Use the Calculator to get a HypoTestResult
286  HypoTestResult* htr = plc.GetHypoTest();
287  cout << "-------------------------------------------------" << endl;
288  cout << "The p-value for the null is " << htr->NullPValue() << endl;
289  cout << "Corresponding to a signifcance of " << htr->Significance() << endl;
290  cout << "-------------------------------------------------\n\n" << endl;
291 
292  /* observed case should return:
293  -------------------------------------------------
294  The p-value for the null is 0.0351669
295  Corresponding to a signifcance of 1.80975
296  -------------------------------------------------
297  */
298 
299 
300  //////////////////////////////////////////
301  // Confidence Interval Stage
302 
303  // Step 8, Here we re-use the ProfileLikelihoodCalculator to return a confidence interval.
304  // We need to specify what are our parameters of interest
305  RooArgSet* paramsOfInterest = nullParams; // they are the same as before in this case
306  plc.SetParameters(*paramsOfInterest);
307  LikelihoodInterval* lrint = (LikelihoodInterval*) plc.GetInterval(); // that was easy.
308  lrint->SetConfidenceLevel(0.95);
309 
310  // Step 9c. Get upper and lower limits
311  cout << "lower limit on master signal = " << lrint->LowerLimit(*mu ) << endl;
312  cout << "upper limit on master signal = " << lrint->UpperLimit(*mu ) << endl;
313 
314  delete lrint;
315  delete htr;
316  delete wspace;
317  delete nullParams;
318  delete poi;
319 
320 
321 }
322 
323 
325 {
326 
327  /////////////////////////////////////////
328  // The same example with observed data in a main
329  // measurement and an background-only auxiliary
330  // measurement with a factor tau more background
331  // than in the main measurement.
332 
333  /////////////////////////////////////////
334  // The Model building stage
335  /////////////////////////////////////////
336 
337  // Step 1, define arrays with signal & bkg expectations and background uncertainties
338  // We still need the expectation to relate signal in different channels with the master signal
339  Double_t s[2] = {20.,10.}; // expected signal
340 
341  // Step 2, use a RooStats factory to build a PDF for a
342  // number counting combination and add it to the workspace.
343  // We need to give the signal expectation to relate the masterSignal
344  // to the signal contribution in the individual channels.
345  // The model neglects correlations in background uncertainty,
346  // but they could be added without much change to the example.
348  RooWorkspace* wspace = new RooWorkspace();
349  f.AddModel(s,2,wspace,"TopLevelPdf", "masterSignal");
350 
351  // Step 3, use a RooStats factory to add datasets to the workspace.
352  // Add the observed data to the workspace in the on-off problem.
353  Double_t mainMeas[2] = {123.,117.}; // observed main measurement
354  Double_t sideband[2] = {11123.,9876.}; // observed sideband
355  Double_t tau[2] = {100.,100.}; // ratio of bkg in sideband to bkg in main measurement, from experimental design.
356  f.AddDataWithSideband(mainMeas, sideband, tau, 2, wspace,"ObservedNumberCountingDataWithSideband");
357 
358  // see below for a printout of the workspace
359  // wspace->Print(); //uncomment to see structure of workspace
360 
361  /////////////////////////////////////////
362  // The Hypothesis testing stage:
363  /////////////////////////////////////////
364  // Step 4, Define the null hypothesis for the calculator
365  // Here you need to know the name of the variables corresponding to hypothesis.
366  RooRealVar* mu = wspace->var("masterSignal");
367  RooArgSet* poi = new RooArgSet(*mu);
368  RooArgSet* nullParams = new RooArgSet("nullParams");
369  nullParams->addClone(*mu);
370  // here we explicitly set the value of the parameters for the null
371  nullParams->setRealValue("masterSignal",0);
372 
373  // Step 5, Create a calculator for doing the hypothesis test.
374  // because this is a
375  ProfileLikelihoodCalculator plc( *wspace->data("ObservedNumberCountingDataWithSideband"),
376  *wspace->pdf("TopLevelPdf"), *poi, 0.05, nullParams);
377 
378 
379  // Step 7, Use the Calculator to get a HypoTestResult
380  HypoTestResult* htr = plc.GetHypoTest();
381  cout << "-------------------------------------------------" << endl;
382  cout << "The p-value for the null is " << htr->NullPValue() << endl;
383  cout << "Corresponding to a signifcance of " << htr->Significance() << endl;
384  cout << "-------------------------------------------------\n\n" << endl;
385 
386  /* observed case should return:
387  -------------------------------------------------
388  The p-value for the null is 0.0352035
389  Corresponding to a signifcance of 1.80928
390  -------------------------------------------------
391  */
392 
393 
394  //////////////////////////////////////////
395  // Confidence Interval Stage
396 
397  // Step 8, Here we re-use the ProfileLikelihoodCalculator to return a confidence interval.
398  // We need to specify what are our parameters of interest
399  RooArgSet* paramsOfInterest = nullParams; // they are the same as before in this case
400  plc.SetParameters(*paramsOfInterest);
401  LikelihoodInterval* lrint = (LikelihoodInterval*) plc.GetInterval(); // that was easy.
402  lrint->SetConfidenceLevel(0.95);
403 
404 
405 
406  // Step 9c. Get upper and lower limits
407  cout << "lower limit on master signal = " << lrint->LowerLimit(*mu ) << endl;
408  cout << "upper limit on master signal = " << lrint->UpperLimit(*mu ) << endl;
409 
410  delete lrint;
411  delete htr;
412  delete wspace;
413  delete nullParams;
414  delete poi;
415 
416 
417 }
void rs_numberCountingCombination_expected()
void Draw(const Option_t *options=0)
draw the likelihood interval or contour for the 1D case a RooPlot is drawn by default of the profiled...
Bool_t setRealValue(const char *name, Double_t newVal=0, Bool_t verbose=kFALSE)
Set value of a RooAbsRealLValye stored in set with given name to newVal No error messages are printed...
Definition: RooArgSet.cxx:548
void SetMaximum(const Double_t theMaximum)
LikelihoodInterval is a concrete implementation of the RooStats::ConfInterval interface.
RooAbsData * data(const char *name) const
Retrieve dataset (binned or unbinned) with given name. A null pointer is returned if not found...
#define assert(cond)
Definition: unittest.h:542
HypoTestResult is a base class for results from hypothesis tests.
This class provides simple and straightforward utilities to plot a LikelihoodInterval object...
ProfileLikelihoodCalculator is a concrete implementation of CombinedCalculator (the interface class f...
static const float upper
Definition: main.cpp:49
Double_t LowerLimit(const RooRealVar &param)
return the lower bound of the interval on a given parameter
void AddExpData(Double_t *sigExp, Double_t *bkgExp, Double_t *db, Int_t nbins, RooWorkspace *ws, const char *dsName="ExpectedNumberCountingData")
TFile * f
RooAbsArg * first() const
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooAbsArg.h:227
RooAbsPdf * pdf(const char *name) const
Retrieve p.d.f (RooAbsPdf) with given name. A null pointer is returned if not found.
void rs_numberCountingCombination_observed()
virtual Double_t NullPValue() const
Return p-value for null hypothesis.
void rs_numberCountingCombination_observedWithTau()
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
Double_t UpperLimit(const RooRealVar &param)
return the upper bound of the interval on a given parameter
virtual Double_t Significance() const
familiar name for the Null p-value in terms of 1-sided Gaussian significance
RooRealVar * var(const char *name) const
Retrieve real-valued variable (RooRealVar) with given name. A null pointer is returned if not found...
double Double_t
Definition: RtypesCore.h:55
virtual Bool_t IsInInterval(const RooArgSet &) const
check if given point is in the interval
virtual void SetConfidenceLevel(Double_t cl)
set the confidence level for the interval (e.g 0.682 for a 1-sigma interval)
void AddDataWithSideband(Double_t *mainMeas, Double_t *sideband, Double_t *tau, Int_t nbins, RooWorkspace *ws, const char *dsName="ExpectedNumberCountingData")
void AddData(Double_t *mainMeas, Double_t *bkgMeas, Double_t *db, Int_t nbins, RooWorkspace *ws, const char *dsName="NumberCountingData")
void rs_numberCountingCombination(int flag=1)
virtual RooAbsArg * addClone(const RooAbsArg &var, Bool_t silent=kFALSE)
Add clone of specified element to an owning set.
Definition: RooArgSet.cxx:475
void AddModel(Double_t *sigExp, Int_t nchan, RooWorkspace *ws, const char *pdfName="CombinedPdf", const char *masterSignalName="masterSignal")
static const float lower
Definition: main.cpp:48
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:42