12 #include "RooWorkspace.h"
13 #include "RooDataSet.h"
14 #include "RooDataHist.h"
15 #include "RooRealVar.h"
16 #include "RooFormulaVar.h"
17 #include "RooAddPdf.h"
18 #include "RooGlobalFunc.h"
19 #include "RooCategory.h"
20 #include "RooSimultaneous.h"
22 #include "RooFitResult.h"
23 #include "RooBinning.h"
24 #include "RooBinningCategory.h"
25 #include "RooMultiCategory.h"
26 #include "RooMappedCategory.h"
27 #include "RooThresholdCategory.h"
28 #include "Roo1DTable.h"
29 #include "RooMinuit.h"
30 #include "RooNLLVar.h"
31 #include "RooAbsDataStore.h"
32 #include "RooEfficiency.h"
33 #include "RooGaussian.h"
34 #include "RooChebychev.h"
35 #include "RooProdPdf.h"
36 #include "RooGenericPdf.h"
37 #include "RooExtendPdf.h"
39 #include "RooMsgService.h"
40 #include "Math/QuantFuncMathCore.h"
42 using namespace RooFit;
44 TagProbeFitter::TagProbeFitter(vector<string> inputFileNames,
string inputDirectoryName,
string inputTreeName,
string outputFileName,
int numCPU_,
bool saveWorkspace_,
bool floatShapeParameters_, std::vector<std::string> fixVars_){
45 inputTree =
new TChain((inputDirectoryName+
"/"+inputTreeName).c_str());
46 for(
size_t i=0;
i<inputFileNames.size();
i++){
47 inputTree->Add(inputFileNames[
i].c_str());
49 outputFile =
new TFile(outputFileName.c_str(),
"recreate");
50 outputDirectory =
outputFile->mkdir(inputDirectoryName.c_str());
52 saveWorkspace = saveWorkspace_;
54 floatShapeParameters = floatShapeParameters_;
57 if(!floatShapeParameters && fixVars.empty())
std::cout <<
"TagProbeFitter: " <<
"You wnat to fix some variables but do not specify them!";
59 gROOT->SetStyle(
"Plain");
60 gStyle->SetTitleFillColor(0);
61 gStyle->SetPalette(1);
62 gStyle->SetOptStat(0);
63 gStyle->SetPaintTextFormat(
".2f");
86 variables.addClone(RooRealVar(name.c_str(), title.c_str(), low, hi, units.c_str()));
91 RooCategory*
c = (RooCategory*) parameterParser.factory(expression.c_str());
95 c->SetName(name.c_str());
96 c->SetTitle(title.c_str());
102 expressionVars.push_back(make_pair(make_pair(expressionName,title), make_pair(expression,arguments)));
108 thresholdCategories.push_back(make_pair(make_pair(categoryName,title), make_pair(varName,cutValue)));
114 pdfs[
name] = pdfCommands;
125 string TagProbeFitter::calculateEfficiency(
string dirName, vector<string> effCats, vector<string> effStates, vector<string>& unbinnedVariables,
map<
string, vector<double> >& binnedReals,
map<
string, std::vector<string> >& binnedCategories, vector<string>& binToPDFmap){
127 outputDirectory->cd();
129 gDirectory->mkdir(dirName.c_str())->cd();
134 for(vector<string>::iterator
v=unbinnedVariables.begin();
v!=unbinnedVariables.end();
v++){
135 dataVars.addClone(
variables[
v->c_str()],
true);
136 if (binnedFit && (
v == unbinnedVariables.begin())) {
137 ((RooRealVar&)dataVars[
v->c_str()]).setBins(
massBins);
141 RooArgSet binnedVariables;
142 RooArgSet binCategories;
143 for(
map<
string, vector<double> >::iterator
v=binnedReals.begin();
v!=binnedReals.end();
v++){
144 TString
name =
v->first;
145 if (
variables.find(name) == 0) {
cerr <<
"Binned variable '"<<name<<
"' not found." << endl;
return "Error"; }
146 binnedVariables.addClone(
variables[name]);
147 ((RooRealVar&)binnedVariables[name]).setBinning( RooBinning(
v->second.size()-1, &
v->second[0]) );
148 binCategories.addClone( RooBinningCategory(name+
"_bins", name+
"_bins", (RooRealVar&)binnedVariables[name]) );
150 dataVars.addClone(binnedVariables,
true);
154 RooArgSet mappedCategories;
155 for(
map<
string, vector<string> >::iterator
v=binnedCategories.begin();
v!=binnedCategories.end();
v++){
156 TString name =
v->first;
157 if (
variables.find(name) == 0) {
cerr <<
"Binned category '"<<name<<
"' not found." << endl;
return "Error"; }
159 mappedCategories.addClone(RooMappedCategory(name+
"_bins", name+
"_bins", (RooCategory&)categories[name]));
160 for(
unsigned int i = 0;
i<
v->second.size();
i++){
161 ((RooMappedCategory&)mappedCategories[name+
"_bins"]).map(
v->second[
i].c_str(), name+
"_"+TString(
v->second[
i].c_str()).ReplaceAll(
",",
"_"));
164 dataVars.addClone(categories,
true);
167 for (vector<string>::const_iterator effCat = effCats.begin(); effCat != effCats.end(); ++effCat) {
168 if (
variables.find(effCat->c_str()) != 0) {
169 dataVars.addClone(
variables[effCat->c_str()],
true);
174 for(vector<pair<pair<string,string>, pair<
string, vector<string> > > >::const_iterator ev = expressionVars.begin(), eve = expressionVars.end(); ev != eve; ++ev){
175 for (vector<string>::const_iterator it = ev->second.second.begin(), ed = ev->second.second.end(); it != ed; ++it) {
181 for(vector<pair<pair<string,string>, pair<string, double> > >::const_iterator tc = thresholdCategories.begin(), tce = thresholdCategories.end(); tc != tce; ++tc){
182 if (
variables.find(tc->second.first.c_str())) dataVars.addClone(
variables[tc->second.first.c_str()],
true);
187 RooDataSet
data(
"data",
"data", inputTree,
189 "", (weightVar.empty() ? 0 : weightVar.c_str()));
192 for(vector<pair<pair<string,string>, pair<
string, vector<string> > > >::const_iterator ev = expressionVars.begin(), eve = expressionVars.end(); ev != eve; ++ev){
194 for (vector<string>::const_iterator it = ev->second.second.begin(), ed = ev->second.second.end(); it != ed; ++it) {
195 args.add(dataVars[it->c_str()]);
197 RooFormulaVar expr(ev->first.first.c_str(), ev->first.second.c_str(), ev->second.first.c_str(),
args);
198 RooRealVar *col = (RooRealVar *)
data.addColumn(expr);
199 dataVars.addClone(*col);
203 for(vector<pair<pair<string,string>, pair<string, double> > >::const_iterator tc = thresholdCategories.begin(), tce = thresholdCategories.end(); tc != tce; ++tc){
204 RooThresholdCategory
tmp(tc->first.first.c_str(), tc->first.second.c_str(), (RooAbsReal &)dataVars[tc->second.first.c_str()],
"above", 1);
205 tmp.addThreshold(tc->second.second,
"below",0);
206 RooCategory *cat = (RooCategory *)
data.addColumn(
tmp);
207 dataVars.addClone(*cat);
212 RooMultiCategory allCats(
"allCats",
"allCats", RooArgSet(binCategories, mappedCategories));
213 data.addColumn(allCats);
216 if (effCats.size() == 1) {
217 effName = effCats.front() +
"::" + effStates.front();
218 RooMappedCategory efficiencyCategory(
"_efficiencyCategory_",
"_efficiencyCategory_", (RooCategory&)dataVars[effCats.front().c_str()],
"Failed");
219 efficiencyCategory.map(effStates.front().c_str(),
"Passed");
220 data.addColumn( efficiencyCategory );
222 RooArgSet rooEffCats;
224 for (
size_t i = 0;
i < effCats.size(); ++
i) {
225 if (
i) { multiState +=
";"; effName +=
" && "; }
226 rooEffCats.add((RooCategory &) dataVars[effCats[
i].c_str()]);
227 multiState += effStates[
i];
228 effName = effCats[
i] +
"::" + effStates[
i];
231 RooMultiCategory efficiencyMultiCategory(
"_efficiencyMultiCategory_",
"_efficiencyMultiCategory_", rooEffCats);
232 RooMappedCategory efficiencyCategory(
"_efficiencyCategory_",
"_efficiencyCategory_", efficiencyMultiCategory,
"Failed");
233 efficiencyCategory.map(multiState.c_str(),
"Passed");
234 data.addColumn( efficiencyCategory );
237 RooMappedCategory pdfCategory(
"_pdfCategory_",
"_pdfCategory_", allCats, (binToPDFmap.size()>0)?binToPDFmap[0].c_str():
"");
238 for(
unsigned int i = 1;
i<binToPDFmap.size();
i+=2){
239 pdfCategory.map(binToPDFmap[
i].c_str(), binToPDFmap[
i+1].c_str());
241 data.addColumn( pdfCategory );
244 RooRealVar
efficiency(
"efficiency",
"Efficiency", 0, 1);
246 RooDataSet fitEfficiency(
"fit_eff",
"Efficiency from unbinned ML fit", RooArgSet(RooArgSet(binnedVariables, categories),
efficiency), StoreAsymError(RooArgSet(binnedVariables,
efficiency)));
248 RooDataSet cntEfficiency(
"cnt_eff",
"Efficiency from counting", RooArgSet(RooArgSet(binnedVariables, categories),
efficiency), StoreAsymError(RooArgSet(binnedVariables,
efficiency)));
251 if(!floatShapeParameters){
253 RooWorkspace*
w =
new RooWorkspace();
257 std::cout <<
"ALL dataset: calling doFitEfficiency with pdf: " << pdfCategory.getLabel() << std::endl;
258 doFitEfficiency(w, pdfCategory.getLabel(),
efficiency);
263 TIterator* it = allCats.typeIterator();
264 for(RooCatType*
t = (RooCatType*)it->Next();
t!=0;
t = (RooCatType*)it->Next() ){
266 TString catName =
t->GetName();
268 if(catName.Contains(
"NotMapped"))
continue;
270 RooDataSet* data_bin = (RooDataSet*)
data.reduce(
271 Cut(TString::Format(
"allCats==%d",
t->getVal())));
273 const RooArgSet* row = data_bin->get();
276 TString pdfName(((RooCategory*)row->find(
"_pdfCategory_"))->getLabel());
280 TString dirName = catName;
281 dirName.ReplaceAll(
"{",
"").ReplaceAll(
"}",
"").ReplaceAll(
";",
"__");
282 if(pdfName.Length() > 0){
283 dirName.Append(
"__").Append(pdfName);
286 cout<<
"Fitting bin: "<<dirName<<endl;
288 gDirectory->mkdir(dirName)->cd();
292 RooWorkspace* w =
new RooWorkspace();
294 w->import(*data_bin);
296 saveDistributionsPlot(w);
298 if(data_bin->numEntries()>0){
300 RooArgSet meanOfVariables;
301 TIterator* vit = binnedVariables.createIterator();
302 for(RooRealVar*
v = (RooRealVar*)vit->Next();
v!=0;
v = (RooRealVar*)vit->Next() ){
303 meanOfVariables.addClone(*
v);
304 double mean = w->data(
"data")->mean(*
v);
305 RooBinning
binning((RooBinning&)
v->getBinning());
306 int ind =
binning.binNumber(mean);
307 RooRealVar& newVar = (RooRealVar&)meanOfVariables[
v->GetName()];
314 meanOfVariables.addClone(*data_bin->get(0),
true);
318 doFitEfficiency(w, pdfName.Data(),
efficiency);
319 fitEfficiency.add( RooArgSet(meanOfVariables,
efficiency) );
327 cntEfficiency.add( RooArgSet(meanOfVariables,
efficiency) );
337 gDirectory->cd(
"..");
341 fitEfficiency.Write();
342 gDirectory->mkdir(
"fit_eff_plots")->cd();
343 saveEfficiencyPlots(fitEfficiency, effName, binnedVariables, mappedCategories);
344 gDirectory->cd(
"..");
351 cntEfficiency.Write();
352 gDirectory->mkdir(
"cnt_eff_plots")->cd();
353 saveEfficiencyPlots(cntEfficiency, effName, binnedVariables, mappedCategories);
354 gDirectory->cd(
"..");
365 createPdf(w, pdfs[pdfName]);
368 RooFitResult* res = 0;
370 RooAbsData *
data = w->data(
"data");
373 const RooArgSet *dataObs = data->get(0);
375 RooArgSet *obs = w->pdf(
"simPdf")->getObservables(dataObs);
376 RooDataHist *bdata =
new RooDataHist(
"data_binned",
"data_binned", *obs, *data);
382 double totPassing = data->sumEntries(
"_efficiencyCategory_==_efficiencyCategory_::Passed");
383 double totFailing = data->sumEntries(
"_efficiencyCategory_==_efficiencyCategory_::Failed");
401 if(!fixVars.empty()){
403 if(!floatShapeParameters && fixVarValues.empty()){
414 w->pdf(
"simPdf")->fitTo(*data, Save(
true), Extended(
true), NumCPU(numCPU), PrintLevel(quiet?-1:1), PrintEvalErrors(quiet?-1:1), Warnings(!quiet));
418 w->pdf(
"simPdf")->fitTo(*data, Save(
true), Extended(
true), NumCPU(numCPU), PrintLevel(quiet?-1:1), PrintEvalErrors(quiet?-1:1), Warnings(!quiet));
425 if(!floatShapeParameters) varRestorer(w);
433 res = w->pdf(
"simPdf")->fitTo(*data, Save(
true), Extended(
true), NumCPU(numCPU), Minos(*w->var(
"efficiency")), PrintLevel(quiet?-1:1), PrintEvalErrors(quiet?-1:1), Warnings(!quiet));
437 if(floatShapeParameters){
442 res = w->pdf(
"simPdf")->fitTo(*data, Save(
true), Extended(
true), NumCPU(numCPU), Minos(*w->var(
"efficiency")), PrintLevel(quiet?-1:1), PrintEvalErrors(quiet?-1:1), Warnings(!quiet));
448 res->Write(
"fitresults");
449 w->saveSnapshot(
"finalState",w->components());
452 RooRealVar*
e = (RooRealVar*) res->floatParsFinal().find(
"efficiency");
455 efficiency.setVal(e->getVal());
456 Double_t errLo = e->getErrorLo(), errHi = e->getErrorHi();
457 if (errLo == 0 && e->getVal() < 0.5) errLo = e->getMin()-e->getVal();
458 if (errHi == 0 && e->getVal() > 0.5) errHi = e->getMax()-e->getVal();
459 efficiency.setAsymError(errLo, errHi);
461 if (totPassing * totFailing == 0) {
462 RooRealVar* nS = (RooRealVar*) res->floatParsFinal().find(
"numSignalAll");
464 double cerr = ROOT::Math::beta_quantile( 1-(1.0-.68540158589942957)/2, 1, nS->getVal() );
474 if (totPassing == 0) {
475 efficiency.setVal(0);
476 efficiency.setAsymError(0,cerr);
478 efficiency.setVal(1);
479 efficiency.setAsymError(-cerr,0);
486 for(
unsigned int i=0;
i<pdfCommands.size();
i++){
487 const std::string &
command = pdfCommands[
i];
488 if (command.find(
"#import ") == 0) {
489 TDirectory *here = gDirectory;
490 w->import(command.substr(8).c_str());
493 TDirectory *here = gDirectory;
494 w->factory(command.c_str());
499 w->factory(
"expr::numSignalPass('efficiency*numSignalAll', efficiency, numSignalAll[0.,1e10])");
500 w->factory(
"expr::numSignalFail('(1-efficiency)*numSignalAll', efficiency, numSignalAll)");
501 TString sPass =
"signal", sFail =
"signal";
502 if (w->pdf(
"signalPass") != 0 && w->pdf(
"signalFail") != 0) {
503 if (w->pdf(
"signal") != 0)
throw std::logic_error(
"You must either define one 'signal' PDF or two PDFs ('signalPass', 'signalFail'), not both!");
504 sPass =
"signalPass"; sFail =
"signalFail";
505 }
else if (w->pdf(
"signal") != 0) {
506 if (w->pdf(
"signalPass") != 0 || w->pdf(
"signalFail") != 0) {
507 throw std::logic_error(
"You must either define one 'signal' PDF or two PDFs ('signalPass', 'signalFail'), not both!");
510 throw std::logic_error(
"You must either define one 'signal' PDF or two PDFs ('signalPass', 'signalFail')");
512 w->factory(
"SUM::pdfPass(numSignalPass*"+sPass+
", numBackgroundPass[0.,1e10]*backgroundPass)");
513 w->factory(
"SUM::pdfFail(numSignalFail*"+sFail+
", numBackgroundFail[0.,1e10]*backgroundFail)");
514 w->factory(
"SIMUL::simPdf(_efficiencyCategory_, Passed=pdfPass, Failed=pdfFail)");
516 if (w->pdf(
"simPdf") == 0)
throw std::runtime_error(
"Could not create simultaneous fit pdf.");
517 if(w->var(
"signalFractionInPassing") == 0)
518 w->factory(
"signalFractionInPassing[0.9]");
523 double signalEfficiency = w->var(
"efficiency")->getVal();
524 double signalFractionInPassing = w->var(
"signalFractionInPassing")->getVal();
525 double totPassing = w->data(
"data")->sumEntries(
"_efficiencyCategory_==_efficiencyCategory_::Passed");
526 double totFailinging = w->data(
"data")->sumEntries(
"_efficiencyCategory_==_efficiencyCategory_::Failed");
527 double numSignalAll = totPassing*signalFractionInPassing/signalEfficiency;
529 if(numSignalAll*(1-signalEfficiency) > totFailinging)
530 numSignalAll = totFailinging;
532 w->var(
"numSignalAll")->setVal(numSignalAll);
533 w->var(
"numBackgroundPass")->setVal(totPassing - numSignalAll*signalEfficiency);
534 w->var(
"numBackgroundFail")->setVal(totFailinging - numSignalAll*(1-signalEfficiency));
536 if (totPassing == 0) {
537 w->var(
"efficiency")->setVal(0.0);
538 w->var(
"efficiency")->setAsymError(0,1);
539 w->var(
"efficiency")->setConstant(
false);
540 w->var(
"numBackgroundPass")->setVal(0.0);
541 w->var(
"numBackgroundPass")->setConstant(
true);
542 w->var(
"numBackgroundFail")->setConstant(
false);
543 }
else if (totFailinging == 0) {
544 w->var(
"efficiency")->setVal(1.0);
545 w->var(
"efficiency")->setAsymError(-1,0);
546 w->var(
"efficiency")->setConstant(
false);
547 w->var(
"numBackgroundPass")->setConstant(
false);
548 w->var(
"numBackgroundFail")->setVal(0.0);
549 w->var(
"numBackgroundFail")->setConstant(
true);
551 w->var(
"efficiency")->setConstant(
false);
552 w->var(
"numBackgroundPass")->setConstant(
false);
553 w->var(
"numBackgroundFail")->setConstant(
false);
557 RooRealVar* fBkgPass = w->var(
"numBackgroundPass");
558 if(signalFractionInPassing==1.0) { fBkgPass->setVal(0.0); fBkgPass->setConstant(
true); }
561 w->saveSnapshot(
"initialState",w->components());
566 RooCategory& efficiencyCategory = *w->cat(
"_efficiencyCategory_");
567 RooAbsData* dataAll = (binnedFit ? w->data(
"data_binned") : w->data(
"data") );
568 RooAbsData* dataPass = dataAll->reduce(Cut(
"_efficiencyCategory_==_efficiencyCategory_::Passed"));
569 RooAbsData* dataFail = dataAll->reduce(Cut(
"_efficiencyCategory_==_efficiencyCategory_::Failed"));
570 RooAbsPdf& pdf = *w->pdf(
"simPdf");
571 RooArgSet *obs = pdf.getObservables(*dataAll);
572 RooRealVar*
mass = 0;
573 TIterator* it = obs->createIterator();
574 for(RooAbsArg*
v = (RooAbsArg*)it->Next();
v!=0;
v = (RooAbsArg*)it->Next() ){
575 if(!
v->InheritsFrom(
"RooRealVar"))
continue;
576 mass = (RooRealVar*)
v;
581 TCanvas
canvas(
"fit_canvas");
583 vector<RooPlot*> frames;
587 frames.push_back(mass->frame(Name(
"Passing"), Title(
"Passing Probes")));
588 frames.push_back(mass->frame(Name(
"Failing"), Title(
"Failing Probes")));
589 frames.push_back(mass->frame(Name(
"All"), Title(
"All Probes")));
591 frames.push_back(mass->frame(Name(
"Passing"), Title(
"Passing Probes"), Bins(
massBins)));
592 frames.push_back(mass->frame(Name(
"Failing"), Title(
"Failing Probes"), Bins(
massBins)));
593 frames.push_back(mass->frame(Name(
"All"), Title(
"All Probes"), Bins(
massBins)));
595 dataPass->plotOn(frames[0]);
596 pdf.plotOn(frames[0], Slice(efficiencyCategory,
"Passed"), ProjWData(*dataPass), LineColor(kGreen));
597 pdf.plotOn(frames[0], Slice(efficiencyCategory,
"Passed"), ProjWData(*dataPass), LineColor(kGreen), Components(
"backgroundPass"), LineStyle(kDashed));
601 dataFail->plotOn(frames[1]);
602 pdf.plotOn(frames[1], Slice(efficiencyCategory,
"Failed"), ProjWData(*dataFail), LineColor(kRed));
603 pdf.plotOn(frames[1], Slice(efficiencyCategory,
"Failed"), ProjWData(*dataFail), LineColor(kRed), Components(
"backgroundFail"), LineStyle(kDashed));
607 dataAll->plotOn(frames.back());
608 pdf.plotOn(frames.back(), ProjWData(*dataAll), LineColor(kBlue));
609 pdf.plotOn(frames.back(), ProjWData(*dataAll), LineColor(kBlue), Components(
"backgroundPass,backgroundFail"), LineStyle(kDashed));
610 frames.back()->Draw();
613 frames.push_back(mass->frame(Name(
"Fit Results"), Title(
"Fit Results")));
614 pdf.paramOn(frames.back(), dataAll,
"", 0,
"NELU", 0.1, 0.9, 0.9);
616 frames.back()->findObject(Form(
"%s_paramBox",pdf.GetName()))->Draw();
619 for (
size_t i=0;
i<frames.size();
i++) {
628 RooAbsData* dataAll = w->data(
"data");
629 RooAbsData* dataPass = dataAll->reduce(Cut(
"_efficiencyCategory_==_efficiencyCategory_::Passed"));
630 RooAbsData* dataFail = dataAll->reduce(Cut(
"_efficiencyCategory_==_efficiencyCategory_::Failed"));
632 const RooArgSet* vars = dataAll->get();
633 vector<RooRealVar*> reals;
634 TIterator* it = vars->createIterator();
635 for(RooAbsArg*
v = (RooAbsArg*)it->Next();
v!=0;
v = (RooAbsArg*)it->Next() ){
636 if(!
v->InheritsFrom(
"RooRealVar"))
continue;
637 reals.push_back((RooRealVar*)
v);
639 TCanvas
canvas(
"distributions_canvas");
640 canvas.Divide(3,reals.size());
641 vector<RooPlot*> frames;
642 for(
unsigned int i=0;
i<reals.size();
i++){
645 frames.push_back(reals[
i]->frame(Name(
"Passing"), Title(
"Passing Probes"), Bins(100)));
646 dataPass->plotOn(frames.back(), LineColor(kGreen));
647 dataPass->statOn(frames.back());
648 frames.back()->Draw();
651 frames.push_back(reals[
i]->frame(Name(
"Failing"), Title(
"Failing Probes"), Bins(100)));
652 dataFail->plotOn(frames.back(), LineColor(kRed));
653 dataFail->statOn(frames.back());
654 frames.back()->Draw();
657 frames.push_back(reals[
i]->frame(Name(
"All"), Title(
"All Probes"), Bins(100)));
658 dataAll->plotOn(frames.back(), LineColor(kBlue));
659 dataAll->statOn(frames.back());
660 frames.back()->Draw();
663 for (
size_t i=0;
i<frames.size();
i++) {
671 TIterator* v1it = binnedVariables.createIterator();
672 for(RooRealVar* v1 = (RooRealVar*)v1it->Next(); v1!=0; v1 = (RooRealVar*)v1it->Next() ){
673 RooArgSet binCategories1D;
674 TIterator* v2it = binnedVariables.createIterator();
675 for(RooRealVar* v2 = (RooRealVar*)v2it->Next(); v2!=0; v2 = (RooRealVar*)v2it->Next() ){
676 if(v2 == v1)
continue;
677 binCategories1D.addClone( RooBinningCategory(TString(v2->GetName())+
"_bins", TString(v2->GetName())+
"_bins", *v2) );
679 RooArgSet binCategories2D;
680 TIterator* v3it = binnedVariables.createIterator();
681 for(RooRealVar* v3 = (RooRealVar*)v3it->Next(); v3!=0; v3 = (RooRealVar*)v3it->Next() ){
682 if(v3 == v1 || v3 == v2)
continue;
683 binCategories2D.addClone( RooBinningCategory(TString(v3->GetName())+
"_bins", TString(v3->GetName())+
"_bins", *v3) );
685 RooMultiCategory allCats2D(
"allCats2D",
"allCats2D", RooArgSet(binCategories2D, mappedCategories));
686 if(allCats2D.numTypes()==0){
687 makeEfficiencyPlot2D(eff, *v1, *v2, TString::Format(
"%s_%s_PLOT", v1->GetName(), v2->GetName()),
"", effName);
689 RooDataSet myEff(eff);
690 myEff.addColumn(allCats2D);
691 TIterator* catIt = allCats2D.typeIterator();
692 for(RooCatType*
t = (RooCatType*)catIt->Next();
t!=0;
t = (RooCatType*)catIt->Next() ){
693 TString catName =
t->GetName();
694 if(catName.Contains(
"NotMapped"))
continue;
695 catName.ReplaceAll(
"{",
"").ReplaceAll(
"}",
"").ReplaceAll(
";",
"_&_");
696 RooDataSet* eff_bin = (RooDataSet*) myEff.reduce( Cut(TString::Format(
"allCats2D==%d",
t->getVal())) );
697 makeEfficiencyPlot2D(*eff_bin, *v1, *v2, TString::Format(
"%s_%s_PLOT_%s",v1->GetName(), v2->GetName(), catName.Data()), catName, effName);
702 RooMultiCategory allCats1D(
"allCats1D",
"allCats1D", RooArgSet(binCategories1D, mappedCategories));
703 if(allCats1D.numTypes()==0){
704 makeEfficiencyPlot1D(eff, *v1, TString::Format(
"%s_PLOT", v1->GetName()),
"", effName);
706 RooDataSet myEff(eff);
707 myEff.addColumn(allCats1D);
708 TIterator* catIt = allCats1D.typeIterator();
709 for(RooCatType*
t = (RooCatType*)catIt->Next();
t!=0;
t = (RooCatType*)catIt->Next() ){
710 TString catName =
t->GetName();
711 if(catName.Contains(
"NotMapped"))
continue;
712 catName.ReplaceAll(
"{",
"").ReplaceAll(
"}",
"").ReplaceAll(
";",
"_&_");
713 RooDataSet* eff_bin = (RooDataSet*) myEff.reduce( Cut(TString::Format(
"allCats1D==%d",
t->getVal())) );
714 makeEfficiencyPlot1D(*eff_bin, *v1, TString::Format(
"%s_PLOT_%s", v1->GetName(), catName.Data()), catName, effName);
723 const RooArgSet*
set = eff.get();
724 RooRealVar*
e = (RooRealVar*) set->find(
"efficiency");
725 RooPlot*
p = v.frame(Name(plotName), Title(plotTitle));
726 eff.plotOnXY(p,YVar(*e));
727 p->SetYTitle(TString(
"Efficiency of ")+effName);
728 p->SetAxisRange(0,1,
"Y");
736 canvas.SetRightMargin(0.15);
737 TH2F*
h =
new TH2F(plotName, plotName, v1.getBinning().numBins(), v1.getBinning().array(), v2.getBinning().numBins(), v2.getBinning().array());
738 const RooArgSet*
set = eff.get();
739 RooRealVar*
e = (RooRealVar*) set->find(
"efficiency");
740 RooRealVar* v1_ = (RooRealVar*) set->find(v1.GetName());
741 RooRealVar* v2_ = (RooRealVar*) set->find(v2.GetName());
742 h->SetTitle(TString::Format(
"%s;%s%s;%s%s;Efficiency of %s", plotTitle.Data(),
743 v1.GetTitle(), TString(v1.getUnit()).Length()==0?
"":TString::Format(
" (%s)", v1.getUnit()).
Data(),
744 v2.GetTitle(), TString(v2.getUnit()).Length()==0?
"":TString::Format(
" (%s)", v2.getUnit()).
Data(), effName.
Data()));
745 h->SetOption(
"colztexte");
746 h->GetZaxis()->SetRangeUser(-0.001,1.001);
748 for(
int i=0;
i<eff.numEntries();
i++){
750 h->SetBinContent(h->FindBin(v1_->getVal(), v2_->getVal()), e->getVal());
751 h->SetBinError(h->FindBin(v1_->getVal(), v2_->getVal()), (e->getErrorHi()-e->getErrorLo())/2.);
762 int pass = w->data(
"data")->sumEntries(
"_efficiencyCategory_==_efficiencyCategory_::Passed");
763 int fail = w->data(
"data")->sumEntries(
"_efficiencyCategory_==_efficiencyCategory_::Failed");
764 double e = (pass+fail == 0) ? 0 : pass/
double(pass+fail);
766 double alpha = (1.0 - .68540158589942957)/2;
767 double lo = (pass == 0) ? 0.0 : ROOT::Math::beta_quantile( alpha, pass, fail+1 );
768 double hi = (fail == 0) ? 1.0 : ROOT::Math::beta_quantile( 1-alpha, pass+1, fail );
773 efficiency.setVal(e);
774 efficiency.setAsymError(lo-e, hi-e);
778 std::vector<std::string>::const_iterator it;
779 for(it=fixVars.begin(); it<fixVars.end(); it++){
780 if(w->var((*it).c_str()))
781 w->var((*it).c_str())->setAttribute(
"Constant",fix);
783 std::cout <<
"TagProbeFitter: " <<
"Can't find a variable to fix: " << *it;
789 if(!fixVarValues.empty()){
790 std::cout <<
"attempt to save variables more than once!" << std::endl;
793 std::vector<std::string>::const_iterator it;
794 for(it=fixVars.begin(); it<fixVars.end(); it++){
795 fixVarValues.push_back(w->var((*it).c_str())->getVal());
801 if(fixVarValues.size()==fixVars.size())
802 for(
unsigned int i=0;
i< fixVars.size();
i++){
803 std::cout <<
"setting variable " << fixVars[
i].c_str() << std::endl;
804 w->var(fixVars[
i].c_str())->setVal(fixVarValues[
i]);
807 std::cout <<
"fixVars and fixVarValues are not of the same size!" << std::endl;
void doCntEfficiency(RooWorkspace *w, RooRealVar &efficiency)
calculate the efficiecny by counting in the dataset found in the workspace
bool addExpression(std::string expressionName, std::string title, std::string expression, std::vector< std::string > arguments)
adds a new category based on a cut
void makeEfficiencyPlot1D(RooDataSet &eff, RooRealVar &v, TString plotName, TString plotTitle, TString effName)
makes the 1D plot
void addPdf(std::string pdfName, std::vector< std::string > &pdfCommands)
static PFTauRenderPlugin instance
TagProbeFitter(std::vector< std::string > inputFileNames, std::string inputDirectoryName, std::string inputTreeName, std::string outputFileName, int numCPU=1, bool saveWorkspace_=false, bool floatShapeParameters=true, std::vector< std::string > fixVars_=std::vector< std::string >())
construct the fitter with the inputFileName, inputDirectoryName, inputTreeName, outputFileName and sp...
void setWeightVar(const std::string &weight)
set a variable to be used as weight for a dataset. empty string means no weights. ...
std::string calculateEfficiency(std::string dirName, std::string efficiencyCategory, std::string efficiencyState, std::vector< std::string > &unbinnedVariables, std::map< std::string, std::vector< double > > &binnedReals, std::map< std::string, std::vector< std::string > > &binnedCategories, std::vector< std::string > &binToPDFmap)
calculate the efficiency for a particular binning of the data; it saves everything in the directory "...
bool addThresholdCategory(std::string categoryName, std::string title, std::string varName, double cutValue)
adds a new category based on a cut
void setQuiet(bool quiet_=true)
suppress most of the output from RooFit and Minuit
void varSaver(RooWorkspace *w)
store values in the vector
void varRestorer(RooWorkspace *w)
restore variables's values for fit starting point
void saveFitPlot(RooWorkspace *w)
saves the fit canvas
~TagProbeFitter()
destructor closes the files
void saveEfficiencyPlots(RooDataSet &eff, TString effName, RooArgSet &binnedVariables, RooArgSet &mappedCategories)
saves the efficiency plots
void setInitialValues(RooWorkspace *w)
sets initial values of the PDF parameters based on the data available in the workspace ...
void saveDistributionsPlot(RooWorkspace *w)
saves the distributions canvas
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
void makeEfficiencyPlot2D(RooDataSet &eff, RooRealVar &v1, RooRealVar &v2, TString plotName, TString plotTitle, TString effName)
makes the 2D plot
void varFixer(RooWorkspace *w, bool fix)
fix or release variables selected by user
std::vector< std::vector< double > > tmp
Double_t mean(Double_t mH, TString proc)
TString units(TString variable, Char_t axis)
char data[epos_bytes_allocation]
void setBinsForMassPlots(int bins)
set number of bins to use when making the plots; 0 = automatic
void doSBSEfficiency(RooWorkspace *w, RooRealVar &efficiency)
calculate the efficiecny with side band substraction in the dataset found in the workspace ...
void createPdf(RooWorkspace *w, std::vector< std::string > &pdfCommands)
creates the simultaneous PDF in the workspace according to the "pdfCommands"
void doFitEfficiency(RooWorkspace *w, std::string pdfName, RooRealVar &efficiency)
calculate the efficiecny with a simulataneous maximum likelihood fit in the dataset found in the work...
bool addCategory(std::string categoryName, std::string title, std::string expression)
adds a new category variable to the set of variables describing the data in the tree; "expression" is...
bool addVariable(std::string variableName, std::string title, double low, double hi, std::string units)
adds a new real variable to the set of variables describing the data in the tree
void set(const std::string &name, int value)
set the flag, with a run-time name