/
ale-toys-cls-example.c
95 lines (69 loc) · 2.48 KB
/
ale-toys-cls-example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "TFile.h"
#include "TString.h"
#include "TROOT.h"
#include "RooWorkspace.h"
#include "RooAbsData.h"
#include "RooRealVar.h"
#include "RooProfileLL.h"
#include "RooFitResult.h"
#include "RooStats/HypoTestResult.h"
#include "RooStats/HypoTestInverter.h"
#include "RooStats/FrequentistCalculator.h"
#include "RooStats/ToyMCSampler.h"
#include "RooStats/ProfileLikelihoodTestStat.h"
#include <iostream>
#include <fstream>
using namespace RooFit;
using namespace RooStats;
void RunToyScan5(TString fileName, double startVal, double stopVal, TString outFile) {
int nToys = 1000 ;
gROOT->LoadMacro("RooBetaPdf.cxx+") ;
gROOT->LoadMacro("RooRatio.cxx+") ;
gROOT->LoadMacro("RooPosDefCorrGauss.cxx+") ;
// get relevant objects out of the "ws" file
TFile *file = TFile::Open(fileName);
if(!file){
cout <<"file not found" << endl;
return;
}
RooWorkspace* w = (RooWorkspace*) file->Get("ws");
if(!w){
cout <<"workspace not found" << endl;
return;
}
ModelConfig* mc = (ModelConfig*) w->obj("SbModel");
RooAbsData* data = w->data("ra2b_observed_rds");
if( !data || !mc ){
w->Print();
cout << "data or ModelConfig was not found" <<endl;
return;
}
RooRealVar* myPOI = (RooRealVar*) mc->GetParametersOfInterest()->first();
myPOI->setRange(0, 1000.);
ModelConfig* bModel = (ModelConfig*) w->obj("BModel");
ModelConfig* sbModel = (ModelConfig*) w->obj("SbModel");
ProfileLikelihoodTestStat profll(*sbModel->GetPdf());
profll.SetOneSided(1);
TestStatistic * testStat = &profll;
HypoTestCalculatorGeneric * hc = 0;
hc = new FrequentistCalculator(*data, *bModel, *sbModel);
ToyMCSampler *toymcs = (ToyMCSampler*)hc->GetTestStatSampler();
toymcs->SetNEventsPerToy(1);
toymcs->SetTestStatistic(testStat);
((FrequentistCalculator *)hc)->SetToys(nToys,nToys);
HypoTestInverter calc(*hc);
calc.SetConfidenceLevel(0.95);
calc.UseCLs(true);
calc.SetVerbose(true);
calc.SetFixedScan(5,startVal,stopVal);
HypoTestInverterResult * res_toysCLs_calculator = calc.GetInterval();
// dump results string to output file
ofstream outStream ;
outStream.open(outFile,ios::app) ;
outStream << "CLs = " << res_toysCLs_calculator->UpperLimit()
<< " CLs_exp = " << res_toysCLs_calculator->GetExpectedUpperLimit(0)
<< " CLs_exp(-1s) = " << res_toysCLs_calculator->GetExpectedUpperLimit(-1)
<< " CLs_exp(+1s) = " << res_toysCLs_calculator->GetExpectedUpperLimit(1) << endl ;
outStream.close() ;
return ;
}