#ifndef RooStats_BernsteinCorrection
#include "RooStats/BernsteinCorrection.h"
#endif
#include "RooGlobalFunc.h"
#include "RooDataSet.h"
#include "RooRealVar.h"
#include "RooAbsPdf.h"
#include "RooFit.h"
#include "RooFitResult.h"
#include "TMath.h"
#include <string>
#include <vector>
#include <stdio.h>
#include <sstream>
#include <iostream>
#include "RooEffProd.h"
#include "RooNLLVar.h"
#include "RooWorkspace.h"
#include "RooDataHist.h"
#include "RooHistPdf.h"
#include "RooBernstein.h"
#include "Math/MinimizerOptions.h"
ClassImp(RooStats::BernsteinCorrection) ;
using namespace RooFit;
using namespace RooStats;
using namespace std;
BernsteinCorrection::BernsteinCorrection(Double_t tolerance):
fMaxDegree(10), fMaxCorrection(100), fTolerance(tolerance){
}
Int_t BernsteinCorrection::ImportCorrectedPdf(RooWorkspace* wks,
const char* nominalName,
const char* varName,
const char* dataName){
RooRealVar* x = wks->var(varName);
RooAbsPdf* nominal = wks->pdf(nominalName);
RooAbsData* data = wks->data(dataName);
if (!x || !nominal || !data) {
cout << "Error: wrong name for pdf or variable or dataset - return -1 " << std::endl;
return -1;
}
std::cout << "BernsteinCorrection::ImportCorrectedPdf - Doing initial Fit with nominam model " << std::endl;
TString minimType = ROOT::Math::MinimizerOptions::DefaultMinimizerType();
int printLevel = ROOT::Math::MinimizerOptions::DefaultPrintLevel()-1;
RooFitResult* nominalResult = nominal->fitTo(*data,Save(),Minos(kFALSE), Hesse(kFALSE),PrintLevel(printLevel),Minimizer(minimType));
Double_t lastNll= nominalResult->minNll();
if (nominalResult->status() != 0 ) {
std::cout << "BernsteinCorrection::ImportCorrectedPdf - Error fit with nominal model failed - exit" << std::endl;
return -1;
}
std::stringstream log;
log << "------ Begin Bernstein Correction Log --------" << endl;
RooArgList coeff;
vector<RooRealVar*> coefficients;
Double_t q = 1E6;
Int_t degree = -1;
bool keepGoing = true;
while( keepGoing ) {
degree++;
std::stringstream str;
str<<"_"<<degree;
RooRealVar* newCoef = new RooRealVar(("c"+str.str()).c_str(),
"Bernstein basis poly coefficient",
1.0, 0., fMaxCorrection);
coeff.add(*newCoef);
coefficients.push_back(newCoef);
if (degree == 0) {
newCoef->setVal(1);
newCoef->setConstant(1);
continue;
}
RooBernstein* poly = new RooBernstein("poly", "Bernstein poly", *x, coeff);
RooEffProd* corrected = new RooEffProd("corrected","",*nominal,*poly);
RooFitResult* result = corrected->fitTo(*data,Save(),Minos(kFALSE), Hesse(kFALSE),PrintLevel(printLevel),Minimizer(minimType));
if (result->status() != 0) {
std::cout << "BernsteinCorrection::ImportCorrectedPdf - Error fit with corrected model failed" << std::endl;
return -1;
}
q = 2*(lastNll - result->minNll());
keepGoing = (degree < 1 || TMath::Prob(q,1) < fTolerance );
if (degree >= fMaxDegree) keepGoing = false;
if(!keepGoing){
wks->import(*corrected);
} else {
delete corrected;
delete poly;
}
if(degree != 0){
log << "degree = " << degree
<< " -log L("<<degree-1<<") = " << lastNll
<< " -log L(" << degree <<") = " << result->minNll()
<< " q = " << q
<< " P(chi^2_1 > q) = " << TMath::Prob(q,1) << endl;;
}
lastNll = result->minNll();
delete result;
}
log << "------ End Bernstein Correction Log --------" << endl;
cout << log.str();
return degree;
}
void BernsteinCorrection::CreateQSamplingDist(RooWorkspace* wks,
const char* nominalName,
const char* varName,
const char* dataName,
TH1F* samplingDist,
TH1F* samplingDistExtra,
Int_t degree,
Int_t nToys){
RooRealVar* x = wks->var(varName);
RooAbsPdf* nominal = wks->pdf(nominalName);
RooAbsData* data = wks->data(dataName);
if (!x || !nominal || !data) {
cout << "Error: wrong name for pdf or variable or dataset ! " << std::endl;
return;
}
std::stringstream log;
log << "------ Begin Bernstein Correction Log --------" << endl;
RooArgList coeff;
RooArgList coeffNull;
RooArgList coeffExtra;
vector<RooRealVar*> coefficients;
for(int i = 0; i<=degree+1; ++i) {
std::stringstream str;
str<<"_"<<i;
RooRealVar* newCoef = new RooRealVar(("c"+str.str()).c_str(),
"Bernstein basis poly coefficient",
1., 0., fMaxCorrection);
if(i<degree) coeffNull.add(*newCoef);
if(i<=degree) coeff.add(*newCoef);
coeffExtra.add(*newCoef);
coefficients.push_back(newCoef);
}
RooBernstein* poly
= new RooBernstein("poly", "Bernstein poly", *x, coeff);
RooBernstein* polyNull
= new RooBernstein("polyNull", "Bernstein poly", *x, coeffNull);
RooBernstein* polyExtra
= new RooBernstein("polyExtra", "Bernstein poly", *x, coeffExtra);
RooEffProd* corrected
= new RooEffProd("corrected","",*nominal,*poly);
RooEffProd* correctedNull
= new RooEffProd("correctedNull","",*nominal,*polyNull);
RooEffProd* correctedExtra
= new RooEffProd("correctedExtra","",*nominal,*polyExtra);
cout << "made pdfs, make toy generator" << endl;
RooDataHist dataHist("dataHist","",*x,*data);
RooHistPdf toyGen("toyGen","",*x,dataHist);
TString minimType = ROOT::Math::MinimizerOptions::DefaultMinimizerType();
int printLevel = ROOT::Math::MinimizerOptions::DefaultPrintLevel()-1;
RooFit::MsgLevel msglevel = RooMsgService::instance().globalKillBelow();
if (printLevel < 0) {
RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
}
Double_t q = 0, qExtra = 0;
for(int i=0; i<nToys; ++i){
cout << "on toy " << i << endl;
RooDataSet* tmpData = toyGen.generate(*x,data->numEntries());
RooFitResult* result
= corrected->fitTo(*tmpData,Save(),Minos(kFALSE),
Hesse(kFALSE),PrintLevel(printLevel),Minimizer(minimType));
RooFitResult* resultNull
= correctedNull->fitTo(*tmpData,Save(),Minos(kFALSE),
Hesse(kFALSE),PrintLevel(printLevel),Minimizer(minimType));
RooFitResult* resultExtra
= correctedExtra->fitTo(*tmpData,Save(),Minos(kFALSE),
Hesse(kFALSE),PrintLevel(printLevel),Minimizer(minimType));
q = 2*(resultNull->minNll() - result->minNll());
qExtra = 2*(result->minNll() - resultExtra->minNll());
samplingDist->Fill(q);
samplingDistExtra->Fill(qExtra);
if (printLevel > 0)
cout << "NLL Results: null " << resultNull->minNll() << " ref = " << result->minNll() << " extra" << resultExtra->minNll() << endl;
delete tmpData;
delete result;
delete resultNull;
delete resultExtra;
}
RooMsgService::instance().setGlobalKillBelow(msglevel);
}
BernsteinCorrection.cxx:1 BernsteinCorrection.cxx:2 BernsteinCorrection.cxx:3 BernsteinCorrection.cxx:4 BernsteinCorrection.cxx:5 BernsteinCorrection.cxx:6 BernsteinCorrection.cxx:7 BernsteinCorrection.cxx:8 BernsteinCorrection.cxx:9 BernsteinCorrection.cxx:10 BernsteinCorrection.cxx:11 BernsteinCorrection.cxx:12 BernsteinCorrection.cxx:13 BernsteinCorrection.cxx:14 BernsteinCorrection.cxx:15 BernsteinCorrection.cxx:16 BernsteinCorrection.cxx:17 BernsteinCorrection.cxx:18 BernsteinCorrection.cxx:19 BernsteinCorrection.cxx:20 BernsteinCorrection.cxx:21 BernsteinCorrection.cxx:22 BernsteinCorrection.cxx:23 BernsteinCorrection.cxx:24 BernsteinCorrection.cxx:25 BernsteinCorrection.cxx:26 BernsteinCorrection.cxx:27 BernsteinCorrection.cxx:28 BernsteinCorrection.cxx:29 BernsteinCorrection.cxx:30 BernsteinCorrection.cxx:31 BernsteinCorrection.cxx:32 BernsteinCorrection.cxx:33 BernsteinCorrection.cxx:34 BernsteinCorrection.cxx:35 BernsteinCorrection.cxx:36 BernsteinCorrection.cxx:37 BernsteinCorrection.cxx:38 BernsteinCorrection.cxx:39 BernsteinCorrection.cxx:40 BernsteinCorrection.cxx:41 BernsteinCorrection.cxx:42 BernsteinCorrection.cxx:43 BernsteinCorrection.cxx:44 BernsteinCorrection.cxx:45 BernsteinCorrection.cxx:46 BernsteinCorrection.cxx:47 BernsteinCorrection.cxx:48 BernsteinCorrection.cxx:49 BernsteinCorrection.cxx:50 BernsteinCorrection.cxx:51 BernsteinCorrection.cxx:52 BernsteinCorrection.cxx:53 BernsteinCorrection.cxx:54 BernsteinCorrection.cxx:55 BernsteinCorrection.cxx:56 BernsteinCorrection.cxx:57 BernsteinCorrection.cxx:58 BernsteinCorrection.cxx:59 BernsteinCorrection.cxx:60 BernsteinCorrection.cxx:61 BernsteinCorrection.cxx:62 BernsteinCorrection.cxx:63 BernsteinCorrection.cxx:64 BernsteinCorrection.cxx:65 BernsteinCorrection.cxx:66 BernsteinCorrection.cxx:67 BernsteinCorrection.cxx:68 BernsteinCorrection.cxx:69 BernsteinCorrection.cxx:70 BernsteinCorrection.cxx:71 BernsteinCorrection.cxx:72 BernsteinCorrection.cxx:73 BernsteinCorrection.cxx:74 BernsteinCorrection.cxx:75 BernsteinCorrection.cxx:76 BernsteinCorrection.cxx:77 BernsteinCorrection.cxx:78 BernsteinCorrection.cxx:79 BernsteinCorrection.cxx:80 BernsteinCorrection.cxx:81 BernsteinCorrection.cxx:82 BernsteinCorrection.cxx:83 BernsteinCorrection.cxx:84 BernsteinCorrection.cxx:85 BernsteinCorrection.cxx:86 BernsteinCorrection.cxx:87 BernsteinCorrection.cxx:88 BernsteinCorrection.cxx:89 BernsteinCorrection.cxx:90 BernsteinCorrection.cxx:91 BernsteinCorrection.cxx:92 BernsteinCorrection.cxx:93 BernsteinCorrection.cxx:94 BernsteinCorrection.cxx:95 BernsteinCorrection.cxx:96 BernsteinCorrection.cxx:97 BernsteinCorrection.cxx:98 BernsteinCorrection.cxx:99 BernsteinCorrection.cxx:100 BernsteinCorrection.cxx:101 BernsteinCorrection.cxx:102 BernsteinCorrection.cxx:103 BernsteinCorrection.cxx:104 BernsteinCorrection.cxx:105 BernsteinCorrection.cxx:106 BernsteinCorrection.cxx:107 BernsteinCorrection.cxx:108 BernsteinCorrection.cxx:109 BernsteinCorrection.cxx:110 BernsteinCorrection.cxx:111 BernsteinCorrection.cxx:112 BernsteinCorrection.cxx:113 BernsteinCorrection.cxx:114 BernsteinCorrection.cxx:115 BernsteinCorrection.cxx:116 BernsteinCorrection.cxx:117 BernsteinCorrection.cxx:118 BernsteinCorrection.cxx:119 BernsteinCorrection.cxx:120 BernsteinCorrection.cxx:121 BernsteinCorrection.cxx:122 BernsteinCorrection.cxx:123 BernsteinCorrection.cxx:124 BernsteinCorrection.cxx:125 BernsteinCorrection.cxx:126 BernsteinCorrection.cxx:127 BernsteinCorrection.cxx:128 BernsteinCorrection.cxx:129 BernsteinCorrection.cxx:130 BernsteinCorrection.cxx:131 BernsteinCorrection.cxx:132 BernsteinCorrection.cxx:133 BernsteinCorrection.cxx:134 BernsteinCorrection.cxx:135 BernsteinCorrection.cxx:136 BernsteinCorrection.cxx:137 BernsteinCorrection.cxx:138 BernsteinCorrection.cxx:139 BernsteinCorrection.cxx:140 BernsteinCorrection.cxx:141 BernsteinCorrection.cxx:142 BernsteinCorrection.cxx:143 BernsteinCorrection.cxx:144 BernsteinCorrection.cxx:145 BernsteinCorrection.cxx:146 BernsteinCorrection.cxx:147 BernsteinCorrection.cxx:148 BernsteinCorrection.cxx:149 BernsteinCorrection.cxx:150 BernsteinCorrection.cxx:151 BernsteinCorrection.cxx:152 BernsteinCorrection.cxx:153 BernsteinCorrection.cxx:154 BernsteinCorrection.cxx:155 BernsteinCorrection.cxx:156 BernsteinCorrection.cxx:157 BernsteinCorrection.cxx:158 BernsteinCorrection.cxx:159 BernsteinCorrection.cxx:160 BernsteinCorrection.cxx:161 BernsteinCorrection.cxx:162 BernsteinCorrection.cxx:163 BernsteinCorrection.cxx:164 BernsteinCorrection.cxx:165 BernsteinCorrection.cxx:166 BernsteinCorrection.cxx:167 BernsteinCorrection.cxx:168 BernsteinCorrection.cxx:169 BernsteinCorrection.cxx:170 BernsteinCorrection.cxx:171 BernsteinCorrection.cxx:172 BernsteinCorrection.cxx:173 BernsteinCorrection.cxx:174 BernsteinCorrection.cxx:175 BernsteinCorrection.cxx:176 BernsteinCorrection.cxx:177 BernsteinCorrection.cxx:178 BernsteinCorrection.cxx:179 BernsteinCorrection.cxx:180 BernsteinCorrection.cxx:181 BernsteinCorrection.cxx:182 BernsteinCorrection.cxx:183 BernsteinCorrection.cxx:184 BernsteinCorrection.cxx:185 BernsteinCorrection.cxx:186 BernsteinCorrection.cxx:187 BernsteinCorrection.cxx:188 BernsteinCorrection.cxx:189 BernsteinCorrection.cxx:190 BernsteinCorrection.cxx:191 BernsteinCorrection.cxx:192 BernsteinCorrection.cxx:193 BernsteinCorrection.cxx:194 BernsteinCorrection.cxx:195 BernsteinCorrection.cxx:196 BernsteinCorrection.cxx:197 BernsteinCorrection.cxx:198 BernsteinCorrection.cxx:199 BernsteinCorrection.cxx:200 BernsteinCorrection.cxx:201 BernsteinCorrection.cxx:202 BernsteinCorrection.cxx:203 BernsteinCorrection.cxx:204 BernsteinCorrection.cxx:205 BernsteinCorrection.cxx:206 BernsteinCorrection.cxx:207 BernsteinCorrection.cxx:208 BernsteinCorrection.cxx:209 BernsteinCorrection.cxx:210 BernsteinCorrection.cxx:211 BernsteinCorrection.cxx:212 BernsteinCorrection.cxx:213 BernsteinCorrection.cxx:214 BernsteinCorrection.cxx:215 BernsteinCorrection.cxx:216 BernsteinCorrection.cxx:217 BernsteinCorrection.cxx:218 BernsteinCorrection.cxx:219 BernsteinCorrection.cxx:220 BernsteinCorrection.cxx:221 BernsteinCorrection.cxx:222 BernsteinCorrection.cxx:223 BernsteinCorrection.cxx:224 BernsteinCorrection.cxx:225 BernsteinCorrection.cxx:226 BernsteinCorrection.cxx:227 BernsteinCorrection.cxx:228 BernsteinCorrection.cxx:229 BernsteinCorrection.cxx:230 BernsteinCorrection.cxx:231 BernsteinCorrection.cxx:232 BernsteinCorrection.cxx:233 BernsteinCorrection.cxx:234 BernsteinCorrection.cxx:235 BernsteinCorrection.cxx:236 BernsteinCorrection.cxx:237 BernsteinCorrection.cxx:238 BernsteinCorrection.cxx:239 BernsteinCorrection.cxx:240 BernsteinCorrection.cxx:241 BernsteinCorrection.cxx:242 BernsteinCorrection.cxx:243 BernsteinCorrection.cxx:244 BernsteinCorrection.cxx:245 BernsteinCorrection.cxx:246 BernsteinCorrection.cxx:247 BernsteinCorrection.cxx:248 BernsteinCorrection.cxx:249 BernsteinCorrection.cxx:250 BernsteinCorrection.cxx:251 BernsteinCorrection.cxx:252 BernsteinCorrection.cxx:253 BernsteinCorrection.cxx:254 BernsteinCorrection.cxx:255 BernsteinCorrection.cxx:256 BernsteinCorrection.cxx:257 BernsteinCorrection.cxx:258 BernsteinCorrection.cxx:259 BernsteinCorrection.cxx:260 BernsteinCorrection.cxx:261 BernsteinCorrection.cxx:262 BernsteinCorrection.cxx:263 BernsteinCorrection.cxx:264 BernsteinCorrection.cxx:265 BernsteinCorrection.cxx:266 BernsteinCorrection.cxx:267 BernsteinCorrection.cxx:268 BernsteinCorrection.cxx:269 BernsteinCorrection.cxx:270 BernsteinCorrection.cxx:271 BernsteinCorrection.cxx:272 BernsteinCorrection.cxx:273 BernsteinCorrection.cxx:274 BernsteinCorrection.cxx:275 BernsteinCorrection.cxx:276 BernsteinCorrection.cxx:277 BernsteinCorrection.cxx:278 BernsteinCorrection.cxx:279 BernsteinCorrection.cxx:280 BernsteinCorrection.cxx:281 BernsteinCorrection.cxx:282 BernsteinCorrection.cxx:283 BernsteinCorrection.cxx:284 BernsteinCorrection.cxx:285 BernsteinCorrection.cxx:286 BernsteinCorrection.cxx:287 BernsteinCorrection.cxx:288 BernsteinCorrection.cxx:289 BernsteinCorrection.cxx:290 BernsteinCorrection.cxx:291 BernsteinCorrection.cxx:292 BernsteinCorrection.cxx:293 BernsteinCorrection.cxx:294 BernsteinCorrection.cxx:295 BernsteinCorrection.cxx:296 BernsteinCorrection.cxx:297 BernsteinCorrection.cxx:298 BernsteinCorrection.cxx:299 BernsteinCorrection.cxx:300 BernsteinCorrection.cxx:301 BernsteinCorrection.cxx:302 BernsteinCorrection.cxx:303 BernsteinCorrection.cxx:304 BernsteinCorrection.cxx:305 BernsteinCorrection.cxx:306 BernsteinCorrection.cxx:307 BernsteinCorrection.cxx:308 BernsteinCorrection.cxx:309 BernsteinCorrection.cxx:310 BernsteinCorrection.cxx:311 BernsteinCorrection.cxx:312 BernsteinCorrection.cxx:313 BernsteinCorrection.cxx:314 BernsteinCorrection.cxx:315 BernsteinCorrection.cxx:316 BernsteinCorrection.cxx:317 BernsteinCorrection.cxx:318 BernsteinCorrection.cxx:319 BernsteinCorrection.cxx:320 BernsteinCorrection.cxx:321 BernsteinCorrection.cxx:322 BernsteinCorrection.cxx:323 BernsteinCorrection.cxx:324 BernsteinCorrection.cxx:325 BernsteinCorrection.cxx:326 BernsteinCorrection.cxx:327 BernsteinCorrection.cxx:328 BernsteinCorrection.cxx:329 BernsteinCorrection.cxx:330 BernsteinCorrection.cxx:331 BernsteinCorrection.cxx:332 BernsteinCorrection.cxx:333 BernsteinCorrection.cxx:334 BernsteinCorrection.cxx:335 BernsteinCorrection.cxx:336 BernsteinCorrection.cxx:337 BernsteinCorrection.cxx:338