10 #include <Math/MinimizerOptions.h> 12 #include "RooAbsPdf.h" 13 #include "RooDataSet.h" 14 #include "RooRealVar.h" 15 #include "RooRandom.h" 16 #include "RooCategory.h" 17 #include "RooSimultaneous.h" 19 #include "RooChi2Var.h" 20 #include "RooDataHist.h" 21 #include "RooAbsData.h" 22 #include "RooWorkspace.h" 23 #include "RooMCStudy.h" 24 #include "RooFitResult.h" 25 #include "RooMultiVarGaussian.h" 26 #include "RooArgList.h" 27 #include "RooArgSet.h" 29 #include "RooStats/ModelConfig.h" 30 #include "RooStats/ProfileLikelihoodTestStat.h" 31 #include "RooStats/ToyMCSampler.h" 32 #include "RooStats/HistFactory/Measurement.h" 39 _algorithm = TString(ROOT::Math::MinimizerOptions::DefaultMinimizerAlgo().c_str());
51 _algorithm = TString(ROOT::Math::MinimizerOptions::DefaultMinimizerAlgo().c_str());
69 std::cerr <<
"ERROR::NULL workspace. No dataset generated!" <<
std::endl;
74 RooStats::ModelConfig *combined_config = (RooStats::ModelConfig*)ws->obj(
"ModelConfig");
76 std::cerr <<
"ERROR::No model config " <<
" ModelConfig " <<
" in workspace!" <<
std::endl;
81 RooAbsPdf* pdf = combined_config->GetPdf();
83 std::cerr <<
"ERROR::No pdf found in ModelConfig. No dataset generated!" <<
std::endl;
87 RooAbsData*
data = ws->data(
"obsData");
89 std::cerr <<
"ERROR::No dataset found in workspace. No dataset generated!" <<
std::endl;
93 const RooArgSet* obs = combined_config->GetObservables();
95 std::cerr <<
"ERROR::No observables found in ModelConfig. No dataset generated!" <<
std::endl;
104 Int_t NEventsToGenerate = (Int_t)(data->sumEntries());
105 mctoy = pdf->generate(*obs, RooFit::NumEvents(NEventsToGenerate), RooFit::AutoBinned(
false));
108 mctoy = pdf->generate(*obs, RooFit::AutoBinned(
false), RooFit::Extended());
123 std::cerr <<
"ERROR::NULL Workspace. Return empty fit!!" <<
std::endl;
128 RooStats::ModelConfig *combined_config = (RooStats::ModelConfig*)ws->obj(
"ModelConfig");
129 if(!combined_config){
130 std::cerr <<
"ERROR::No model config " <<
" ModelConfig " <<
" in workspace. Return empty fit!!" <<
std::endl;
137 RooAbsPdf* pdf = combined_config->GetPdf();
138 const RooArgSet* obs_set = combined_config->GetObservables();
141 RooMCStudy* mcstudy =
new RooMCStudy( *pdf, *obs_set, RooFit::FitOptions(
"r"));
144 RooStats::ProfileLikelihoodTestStat ts(*combined_config->GetPdf());
147 RooStats::ToyMCSampler sampler(ts,nexp);
148 sampler.SetPdf(*combined_config->GetPdf());
149 sampler.SetObservables(*combined_config->GetObservables());
150 sampler.SetGlobalObservables(*combined_config->GetGlobalObservables());
151 sampler.SetParametersForTestStat(*combined_config->GetParametersOfInterest());
153 RooArgSet poiAndNuisance;
154 poiAndNuisance.add(*combined_config->GetParametersOfInterest());
155 if(combined_config->GetNuisanceParameters())
156 poiAndNuisance.add(*combined_config->GetNuisanceParameters());
157 RooArgSet* nullParams = (RooArgSet*) poiAndNuisance.snapshot();
161 ws->saveSnapshot(
"paramsToFit",poiAndNuisance);
163 for(
int i=0; i<nexp; ++i) {
165 std::cout <<
"INFO::Running on toy " << i <<
std::endl;
168 ws->loadSnapshot(
"paramsToFit");
170 RooAbsData* toyMC = sampler.GenerateToyData( *nullParams );
173 RooFitResult* fresult = combined_config->GetPdf()->fitTo(*toyMC, RooFit::Minimizer(
_minimizer.Data(),
_algorithm.Data()), RooFit::Save(), RooFit::Strategy(
_fitstrategy), RooFit::Minos(
_minoserror), RooFit::PrintLevel(-1), RooFit::Constrain(*combined_config->GetPdf()->getParameters(*toyMC)), RooFit::Extended(),
RooFit::Offset(
true));
175 mcstudy->addFitResult(*fresult);
197 std::cerr <<
"ERROR::NULL Workspace. Return empty fit!!" <<
std::endl;
202 RooStats::ModelConfig *combined_config = (RooStats::ModelConfig*)ws->obj(
"ModelConfig");
203 if(!combined_config){
204 std::cerr <<
"ERROR::No model config " <<
" ModelConfig " <<
" in workspace. Return empty fit!!" <<
std::endl;
218 RooStats::ProfileLikelihoodTestStat ts(*combined_config->GetPdf());
221 RooStats::ToyMCSampler sampler(ts, 1);
222 sampler.SetPdf(*combined_config->GetPdf());
223 sampler.SetObservables(*combined_config->GetObservables());
224 sampler.SetGlobalObservables(*combined_config->GetGlobalObservables());
225 sampler.SetParametersForTestStat(*combined_config->GetParametersOfInterest());
227 RooArgSet poiAndNuisance;
228 poiAndNuisance.add(*combined_config->GetParametersOfInterest());
229 if(combined_config->GetNuisanceParameters())
230 poiAndNuisance.add(*combined_config->GetNuisanceParameters());
231 RooArgSet* nullParams = (RooArgSet*) poiAndNuisance.snapshot();
235 ws->saveSnapshot(
"paramsToFit",poiAndNuisance);
242 ws->loadSnapshot(
"paramsToFit");
244 RooAbsData* toyMC = sampler.GenerateToyData( *nullParams );
247 RooFitResult* fresult = combined_config->GetPdf()->fitTo(*toyMC, RooFit::Minimizer(
_minimizer.Data(),
_algorithm.Data()), RooFit::Save(), RooFit::Strategy(
_fitstrategy), RooFit::Minos(
_minoserror), RooFit::PrintLevel(-1), RooFit::Constrain(*combined_config->GetPdf()->getParameters(*toyMC)), RooFit::Extended(),
RooFit::Offset(
true));
263 std::cerr <<
"ERROR::NULL Workspace. Return empty fit!!" <<
std::endl;
268 RooStats::ModelConfig *combined_config = (RooStats::ModelConfig*)ws->obj(
"ModelConfig");
269 if(!combined_config){
270 std::cerr <<
"ERROR::No model config " <<
"ModelConfig " <<
" in workspace. Return empty fit!!" <<
std::endl;
278 RooAbsData *obsdata = ws->data(
"obsData");
280 RooFitResult* fresult = NULL;
282 fresult = combined_config->GetPdf()->fitTo(*obsdata, RooFit::Minimizer(
_minimizer.Data(),
_algorithm.Data()), RooFit::Save(), RooFit::Strategy(
_fitstrategy), RooFit::Minos(
_minoserror), RooFit::PrintLevel(1), RooFit::Constrain(*combined_config->GetPdf()->getParameters(*obsdata)), RooFit::Extended(),
RooFit::Offset(
true) );
284 fresult = combined_config->GetPdf()->fitTo(*obsdata, RooFit::Minimizer(
_minimizer.Data(),
_algorithm.Data()), RooFit::Save(), RooFit::Strategy(
_fitstrategy), RooFit::Minos(
_minoserror), RooFit::Constrain(*combined_config->GetPdf()->getParameters(*obsdata)), RooFit::Extended(),
RooFit::Offset(
true), RooFit::SumW2Error(
true) );
298 std::cerr <<
"ERROR::NULL Workspace. Return empty fit!!" <<
std::endl;
303 RooStats::ModelConfig *combined_config = (RooStats::ModelConfig*)ws->obj(
"ModelConfig");
304 if(!combined_config){
305 std::cerr <<
"ERROR::No model config " <<
"ModelConfig " <<
" in workspace. Return empty fit!!" <<
std::endl;
310 RooAbsData *obsdata = ws->data(
"asimovData");
312 RooFitResult* fresult = combined_config->GetPdf()->fitTo(*obsdata, RooFit::Minimizer(
_minimizer.Data(),
_algorithm.Data()), RooFit::Save(), RooFit::Strategy(
_fitstrategy), RooFit::Minos(
_minoserror), RooFit::Constrain(*combined_config->GetPdf()->getParameters(*obsdata)), RooFit::Extended(),
RooFit::Offset(
true));
323 std::cerr <<
"ERROR::NULL Workspace. Return empty fit!!" <<
std::endl;
328 RooStats::ModelConfig *combined_config = (RooStats::ModelConfig*)ws->obj(
"ModelConfig");
329 if(!combined_config){
330 std::cerr <<
"ERROR::No model config " <<
"ModelConfig " <<
" in workspace. Return empty fit!!" <<
std::endl;
334 RooFitResult* fresult = combined_config->GetPdf()->fitTo(*obsdata, RooFit::Minimizer(
_minimizer.Data(),
_algorithm.Data()), RooFit::Save(), RooFit::Strategy(
_fitstrategy), RooFit::Minos(
_minoserror), RooFit::PrintLevel(1), RooFit::Constrain(*combined_config->GetPdf()->getParameters(*obsdata)), RooFit::Extended(),
RooFit::Offset(
true) );
345 TList *list =
new TList;
347 for(UInt_t j = 0 ; j < wsvec.size(); j++){
348 RooWorkspace*
ws = wsvec.at(j);
364 TTree *newtree = TTree::MergeTrees(list);
365 newtree->SetName(
"MCToysFromWorkspace");
376 std::cerr <<
"ERROR::NULL Workspace. Return empty fit!!" <<
std::endl;
381 RooStats::ModelConfig *combined_config = (RooStats::ModelConfig*)ws->obj(
"ModelConfig");
382 if(!combined_config){
383 std::cerr <<
"ERROR::No model config " <<
"ModelConfig " <<
" in workspace. Return empty fit!!" <<
std::endl;
387 RooAbsPdf* pdf = combined_config->GetPdf();
388 const RooArgSet* obs_set = combined_config->GetObservables();
391 RooMCStudy* mcstudy =
new RooMCStudy( *pdf, *obs_set, RooFit::FitOptions(
"r"));
392 mcstudy->addFitResult(*res);
406 TTree* myTree =
new TTree(
"mcstree",
"mcstree");
408 const RooDataSet& toydata = mc->fitParDataSet();
442 Int_t covQual=0,
status=0, numInvalidNLL=0;
443 Float_t minNll=0, edm=0;
444 myTree->Branch(
"covQual", &covQual,
"covQual/I" );
445 myTree->Branch(
"status", &
status,
"status/I" );
446 myTree->Branch(
"numInvalidNLL", &numInvalidNLL,
"numInvalidNLL/I" );
447 myTree->Branch(
"minNll", &minNll,
"minNll/F" );
448 myTree->Branch(
"edm", &edm,
"edm/F" );
450 std::vector<Float_t> varVals;
451 const RooArgSet*
args = toydata.get();
452 varVals.resize( args->getSize(), -999. );
455 TIterator* Itr = args->createIterator();
456 for(Int_t i=0; (var = (RooRealVar*)Itr->Next()); ++i){
457 TString
varName = var->GetName();
458 TString varNameF = TString(var->GetName()) +
"/F";
459 myTree->Branch( varName.Data(), &varVals[i], varNameF.Data() );
464 std::vector<Float_t> corrVals;
465 const RooArgList& fitparams = mc->fitResult(0)->floatParsFinal();
466 corrVals.resize( fitparams.getSize()*fitparams.getSize(), -999. );
469 for(Int_t i=0; i < fitparams.getSize(); ++i){
470 RooRealVar* fvar = (RooRealVar*)fitparams.at(i);
471 TString
varName = fvar->GetName();
472 for(Int_t j=0; j < fitparams.getSize(); ++j){
474 RooRealVar* fvar2 = (RooRealVar*)fitparams.
at(j);
475 TString varName2 = varName + TString(
"_corr_") + TString(fvar2->GetName());
476 TString varNameF = varName2 +
"/F";
477 myTree->Branch( varName2.Data(), &corrVals[
counter], varNameF.Data() );
483 for(
int iToy=0; iToy<toydata.numEntries(); iToy++){
484 const RooFitResult*
result = mc->fitResult(iToy);
486 covQual = result->covQual();
487 status = result->status();
488 minNll = result->minNll();
489 numInvalidNLL = result->numInvalidNLL();
492 Itr = args->createIterator();
493 for (Int_t i=0; (var=(RooRealVar*)Itr->Next()); ++i) { varVals[i] = var->getVal(); }
496 const RooArgList& fparams = result->floatParsFinal();
498 for(Int_t i=0; i < fparams.getSize(); ++i){
499 RooRealVar* fvar = (RooRealVar*)fparams.at(i);
500 for(Int_t j=0; j < fparams.getSize(); ++j){
502 RooRealVar* fvar2 = (RooRealVar*)fparams.at(j);
503 corrVals[
counter] = result->correlation(fvar->GetName(), fvar2->GetName());
520 std::cerr <<
"ERROR::No RooAbsData. Will return NULL TTree!" <<
std::endl;
524 TTree* myTree =
new TTree(treename, treename);
526 std::vector<Float_t> varVals;
527 const RooArgSet*
args = data->get();
528 varVals.resize( args->getSize(), -999. );
531 TIterator* Itr = args->createIterator();
532 for(Int_t i=0; (var = (RooRealVar*)Itr->Next()); ++i) {
533 TString
varName = var->GetName();
534 TString varNameF = TString(var->GetName()) +
"/F";
535 myTree->Branch( varName.Data(), &varVals[i], varNameF.Data() );
540 for(
int iToy=0; iToy<data->numEntries(); iToy++){
542 TIterator* Itr2 = args->createIterator();
543 for (Int_t i=0; (var=(RooRealVar*)Itr2->Next()); ++i) {varVals[i] = var->getVal();}
558 std::cerr <<
"ERROR::NULL workspace. Will return empty vector!" <<
std::endl;
563 RooSimultaneous* pdf = (RooSimultaneous*)work->pdf(
"simPdf");
565 std::cerr <<
"ERROR::No pdf found in workspace. Will return empty vector!" <<
std::endl;
571 data = work->data(
"obsData");
574 RooCategory* categories = work->cat(
"channelCat");
580 std::vector<TString> categoriesName;
581 TIterator* iter = categories->typeIterator();
583 while( (catType = (RooCatType*) iter->Next())) {
584 TString catname = catType->GetName();
585 categoriesName.push_back(catname);
588 RooArgSet* chi2varset =
new RooArgSet(
"chi2varset");
589 for(UInt_t i = 0; i < categoriesName.size(); i++){
590 TString catname = categoriesName[i];
592 if(categories->setLabel(catname)){
593 std::cout <<
"WARNING::Category " << catname.Data() <<
" is not a member of channelCat" <<
std::endl;
597 RooAbsPdf* subpdf = (RooAbsPdf*)pdf->getPdf(catname.Data());
599 std::cout <<
"WARNING::Can't find sub-pdf for region " << catname.Data() <<
std::endl;
603 TString subdataset_str = Form(
"channelCat==channelCat::%s",catname.Data());
604 RooAbsData* subdataset = (RooAbsData*) data->reduce(subdataset_str.Data());
606 std::cout <<
"WARNING::Can't find sub-dataset for region " << catname.Data() <<
std::endl;
610 RooRealVar*
var =(RooRealVar*) ((RooArgSet*) subpdf->getObservables(*subdataset))->find(Form(
"obs_x_%s", catname.Data()));
612 RooPlot* frame_dummy = var->frame();
613 subdataset->plotOn(frame_dummy,RooFit::Cut(subdataset_str),RooFit::DataError(RooAbsData::Poisson));
614 subpdf->plotOn(frame_dummy,RooFit::Normalization(1,RooAbsReal::RelativeExpected),RooFit::Precision(1
e-5));
616 RooRealVar* chi2var =
new RooRealVar(Form(
"%s_var",subpdf->GetName()), Form(
"%s_var",subpdf->GetTitle()), frame_dummy->chiSquare());
617 chi2varset->add(*chi2var);
629 std::cerr <<
"ERROR::NULL Workspace. Return empty fit!!" <<
std::endl;
634 RooStats::ModelConfig *combined_config = (RooStats::ModelConfig*)ws->obj(
"ModelConfig");
635 if(!combined_config){
636 std::cerr <<
"ERROR::No model config " <<
" ModelConfig " <<
" in workspace. Return empty fit!!" <<
std::endl;
640 RooAbsPdf* pdf = combined_config->GetPdf();
642 std::cerr <<
"ERROR::No pdf found in ModelConfig. No dataset generated!" <<
std::endl;
646 const RooArgSet* obs = combined_config->GetObservables();
647 const RooArgSet* globalobs = combined_config->GetGlobalObservables();
649 std::cerr <<
"ERROR::No observables found in ModelConfig. No dataset generated!" <<
std::endl;
653 const RooArgSet* pars = pdf->getParameters(obs);
654 RooArgList floatParList;
656 TIterator* iter = pars->createIterator();
658 while((arg=(RooAbsArg*)iter->Next())){
659 if(arg->InheritsFrom(
"RooRealVar") && !arg->isConstant()){
660 floatParList.add(*arg);
674 RooDataSet* chi2dataset = NULL;
676 for(
int i=0; i<nexp; ++i) {
678 std::cout <<
"INFO::Generating toy " << i <<
std::endl;
680 RooRandom::randomGenerator()->SetSeed(seed + i);
682 RooAbsData* toyMC = pdf->generate(*obs, RooFit::AutoBinned(
false), RooFit::Extended());
685 RooFitResult* toyfitresult =
FitToyData(ws,toyMC);
689 chi2dataset =
new RooDataSet(
"chi2dataset",
"chi2dataset",*chi2set);
693 if(toyfitresult->status() == 0)
694 chi2dataset->add(*chi2set);
void ResetValues(RooWorkspace *ws, const RooArgList &parList)
TTree * RooMCStudyToTTree(RooMCStudy *mc)
RooFitResult * FitData(RooWorkspace *ws, bool isWeighted=false)
const std::string instance
TTree * RooDataSetToTTree(RooAbsData *data, TString treename)
RooFitResult * FitAsimovData(RooWorkspace *w)
TTree * GenerateChi2Tree(RooWorkspace *ws, int nexp, int seed)
void ResetValuesToNominal(RooWorkspace *ws, const RooArgSet &parSet)
RooArgSet * GetChi2Set(RooWorkspace *work, RooAbsData *data)
TTree * FitToyMCFromWorkspace(std::vector< RooWorkspace * > wsvec, bool fitdata)
TTree * GenerateAndFit(RooWorkspace *ws, int nexp)
void ResetError(RooWorkspace *ws, const RooArgList &parList)
RooAbsData * GenerateToyMC(RooWorkspace *ws, bool datanorm=true)
TTree * RooFitResultToTTree(RooWorkspace *ws, RooFitResult *res)
RooFitResult * GenerateAndFitOneToy(RooWorkspace *ws)
RooFitResult * FitToyData(RooWorkspace *w, RooAbsData *obsdata)
QTextStream & endl(QTextStream &s)