1 #include "../interface/AsimovUtils.h"
6 #include <RooAbsData.h>
8 #include <RooProdPdf.h>
9 #include <RooUniform.h>
10 #include "../interface/utils.h"
11 #include "../interface/ToyMCSamplerOpt.h"
12 #include "../interface/CloseCoutSentry.h"
13 #include "../interface/CascadeMinimizer.h"
16 RooArgSet poi(*mc->GetParametersOfInterest());
17 RooRealVar *
r =
dynamic_cast<RooRealVar *
>(poi.first());
18 r->setConstant(
true); r->setVal(poiValue);
20 RooRealVar *weightVar = 0;
27 RooArgSet poi(*mc->GetParametersOfInterest());
28 RooRealVar *
r =
dynamic_cast<RooRealVar *
>(poi.first());
29 r->setConstant(
true); r->setVal(poiValue);
32 bool needsFit =
false;
33 if (mc->GetNuisanceParameters()) {
37 std::auto_ptr<RooArgSet> params(mc->GetPdf()->getParameters(realdata));
38 std::auto_ptr<TIterator> iter(params->createIterator());
39 for (RooAbsArg *
a = (RooAbsArg *) iter->Next();
a != 0;
a = (RooAbsArg *) iter->Next()) {
40 RooRealVar *rrv =
dynamic_cast<RooRealVar *
>(
a);
41 if ( rrv != 0 && rrv->isConstant() ==
false ) { needsFit =
true;
break; }
46 const RooCmdArg &constrain = (mc->GetNuisanceParameters() ? RooFit::Constrain(*mc->GetNuisanceParameters()) : RooCmdArg());
47 std::auto_ptr<RooAbsReal> nll(mc->GetPdf()->createNLL(realdata, constrain, RooFit::Extended(mc->GetPdf()->canBeExtended())));
50 minim.minimize(verbose-1);
53 if (mc->GetNuisanceParameters() && verbose > 1) {
54 std::cout <<
"Nuisance parameters after fit for asimov dataset: " << std::endl;
55 mc->GetNuisanceParameters()->Print(
"V");
58 RooRealVar *weightVar = 0;
63 if (mc->GetGlobalObservables() && mc->GetGlobalObservables()->getSize() > 0) {
64 RooArgSet gobs(*mc->GetGlobalObservables());
67 RooArgSet snapGlobalObsData;
69 gobs.snapshot(snapGlobalObsData);
71 RooArgSet nuis(*mc->GetNuisanceParameters());
73 RooProdPdf *
prod =
dynamic_cast<RooProdPdf *
>(nuispdf.get());
74 if (prod == 0)
throw std::runtime_error(
"AsimovUtils: the nuisance pdf is not a RooProdPdf!");
75 std::auto_ptr<TIterator> iter(prod->pdfList().createIterator());
76 for (RooAbsArg *
a = (RooAbsArg *) iter->Next();
a != 0;
a = (RooAbsArg *) iter->Next()) {
77 RooAbsPdf *cterm =
dynamic_cast<RooAbsPdf *
>(
a);
78 if (!cterm)
throw std::logic_error(
"AsimovUtils: a factor of the nuisance pdf is not a Pdf!");
79 if (!cterm->dependsOn(nuis))
continue;
80 if (
typeid(*cterm) ==
typeid(RooUniform))
continue;
81 std::auto_ptr<RooArgSet> cpars(cterm->getParameters(&gobs));
82 std::auto_ptr<RooArgSet> cgobs(cterm->getObservables(&gobs));
83 if (cgobs->getSize() != 1) {
84 throw std::runtime_error(Form(
"AsimovUtils: constraint term %s has multiple global observables", cterm->GetName()));
86 RooRealVar &rrv =
dynamic_cast<RooRealVar &
>(*cgobs->first());
88 RooAbsReal *
match = 0;
89 if (cpars->getSize() == 1) {
90 match =
dynamic_cast<RooAbsReal *
>(cpars->first());
92 std::auto_ptr<TIterator> iter2(cpars->createIterator());
93 for (RooAbsArg *a2 = (RooAbsArg *) iter2->Next(); a2 != 0; a2 = (RooAbsArg *) iter2->Next()) {
94 RooRealVar *rrv2 =
dynamic_cast<RooRealVar *
>(a2);
95 if (rrv2 != 0 && !rrv2->isConstant()) {
96 if (match != 0)
throw std::runtime_error(Form(
"AsimovUtils: constraint term %s has multiple floating params", cterm->GetName()));
102 std::cerr <<
"ERROR: AsimovUtils: can't find nuisance for constraint term " << cterm->GetName() << std::endl;
103 std::cerr <<
"Parameters: " << std::endl;
105 std::cerr <<
"Observables: " << std::endl;
107 throw std::runtime_error(Form(
"AsimovUtils: can't find nuisance for constraint term %s", cterm->GetName()));
109 std::string pdfName(cterm->ClassName());
110 if (pdfName ==
"RooGaussian" || pdfName ==
"RooPoisson") {
112 rrv.setVal(match->getVal());
113 }
else if (pdfName ==
"RooGamma") {
121 RooArgList leaves; cterm->leafNodeServerList(&leaves);
122 std::auto_ptr<TIterator> iter2(leaves.createIterator());
123 RooAbsReal *match2 = 0;
124 for (RooAbsArg *a2 = (RooAbsArg *) iter2->Next(); a2 != 0; a2 = (RooAbsArg *) iter2->Next()) {
125 RooAbsReal *rar =
dynamic_cast<RooAbsReal *
>(a2);
126 if (rar == 0 || rar == match || rar == &rrv)
continue;
127 if (!rar->isConstant())
throw std::runtime_error(Form(
"AsimovUtils: extra floating parameter %s of RooGamma %s.", rar->GetName(), cterm->GetName()));
128 if (rar->getVal() == 0)
continue;
129 if (match2 != 0)
throw std::runtime_error(Form(
"AsimovUtils: extra constant non-zero parameter %s of RooGamma %s.", rar->GetName(), cterm->GetName()));
132 if (match2 == 0)
throw std::runtime_error(Form(
"AsimovUtils: could not find the scaling term for RooGamma %s.", cterm->GetName()));
137 rrv.setVal(match->getVal()/match2->getVal() + 1.);
139 throw std::runtime_error(Form(
"AsimovUtils: can't handle constraint term %s of type %s", cterm->GetName(), cterm->ClassName()));
144 snapshot.removeAll();
146 gobs.snapshot(snapshot);
149 gobs = snapGlobalObsData;
152 std::cout <<
"Global observables for data: " << std::endl;
153 snapGlobalObsData.Print(
"V");
154 std::cout <<
"Global observables for asimov: " << std::endl;
bool setAllConstant(const RooAbsCollection &coll, bool constant=true)
set all RooRealVars to constants. return true if at least one changed status
RooAbsData * generateAsimov(RooRealVar *&weightVar)
RooAbsPdf * makeNuisancePdf(RooStats::ModelConfig &model, const char *name="nuisancePdf")
RooAbsData * asimovDatasetWithFit(RooStats::ModelConfig *mc, RooAbsData &realdata, RooAbsCollection &snapshot, double poiValue=0.0, int verbose=0)
Generate asimov dataset from best fit value of nuisance parameters, and fill in snapshot of correspon...
RooAbsData * asimovDatasetNominal(RooStats::ModelConfig *mc, double poiValue=0.0, int verbose=0)
Generate asimov dataset from nominal value of nuisance parameters.
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.