1 #include "../interface/utils.h"
2 #include "../interface/RooSimultaneousOpt.h"
14 #include <TIterator.h>
17 #include <RooAbsData.h>
18 #include <RooAbsPdf.h>
19 #include <RooArgSet.h>
20 #include <RooDataHist.h>
21 #include <RooDataSet.h>
22 #include <RooRealVar.h>
23 #include <RooProdPdf.h>
24 #include <RooProduct.h>
25 #include <RooSimultaneous.h>
26 #include <RooWorkspace.h>
28 #include <RooStats/ModelConfig.h>
31 std::vector<std::string> varnames, catnames;
32 const RooArgSet *b0 = data->get();
33 TIterator *iter = b0->createIterator();
34 for (RooAbsArg *
a = 0; (
a = (RooAbsArg *)iter->Next()) != 0; ) {
35 if (
a->InheritsFrom(
"RooRealVar")) {
36 varnames.push_back(
a->GetName());
37 }
else if (
a->InheritsFrom(
"RooCategory")) {
38 catnames.push_back(
a->GetName());
42 size_t nv = varnames.size(), nc = catnames.size();
44 for (
size_t j = 0;
j < nv; ++
j) { printf(
"%16.16s ", varnames[
j].c_str()); }
45 for (
size_t j = 0;
j < nc; ++
j) { printf(
"%16.16s ", catnames[
j].c_str()); }
47 for (
int i = 0, nb = data->numEntries();
i < nb; ++
i) {
48 const RooArgSet *
bin = data->get(
i);
50 for (
size_t j = 0;
j < nv; ++
j) { printf(
"%16g ", bin->getRealValue(varnames[
j].c_str())); }
51 for (
size_t j = 0;
j < nc; ++
j) { printf(
"%16.16s ", bin->getCatLabel(catnames[
j].c_str())); }
52 printf(
"%8.3f\n", data->weight());
57 if (d->InheritsFrom(
"RooDataHist") || d->numEntries() != 1)
printRDH(const_cast<RooAbsData*>(d));
58 else d->get(0)->Print(
"V");
62 std::cout <<
"Pdf " << pdf->GetName() <<
" parameters." << std::endl;
63 std::auto_ptr<RooArgSet> params(pdf->getVariables());
69 std::cout <<
"ModelConfig " << mc.GetName() <<
" (" << mc.GetTitle() <<
"): pdf parameters." << std::endl;
70 std::auto_ptr<RooArgSet> params(mc.GetPdf()->getVariables());
75 std::cout <<
"PDF " << pdfName <<
" parameters." << std::endl;
76 std::auto_ptr<RooArgSet> params(w->pdf(pdfName)->getVariables());
82 const std::type_info &
id =
typeid(pdf);
83 if (
id ==
typeid(RooProdPdf)) {
85 RooProdPdf *
prod =
dynamic_cast<RooProdPdf *
>(&pdf);
86 RooArgList newFactors; RooArgSet newOwned;
87 RooArgList
list(prod->pdfList());
89 for (
int i = 0,
n =
list.getSize();
i <
n; ++
i) {
90 RooAbsPdf *pdfi = (RooAbsPdf *)
list.at(
i);
91 RooAbsPdf *newpdf =
factorizePdf(observables, *pdfi, constraints);
93 if (newpdf == 0) { needNew =
true;
continue; }
94 if (newpdf != pdfi) { needNew =
true; newOwned.add(*newpdf); }
95 newFactors.add(*newpdf);
98 else if (newFactors.getSize() == 0)
return 0;
99 else if (newFactors.getSize() == 1) {
100 RooAbsPdf *
ret = (RooAbsPdf *) newFactors.first()->Clone(TString::Format(
"%s_obsOnly", pdf.GetName()));
104 RooProdPdf *
ret =
new RooProdPdf(TString::Format(
"%s_obsOnly", pdf.GetName()),
"", newFactors);
105 ret->addOwnedComponents(newOwned);
109 RooSimultaneous *
sim =
dynamic_cast<RooSimultaneous *
>(&pdf);
110 RooAbsCategoryLValue *cat = (RooAbsCategoryLValue *) sim->indexCat().Clone();
111 int nbins = cat->numBins((
const char *)0);
112 TObjArray factorizedPdfs(nbins); RooArgSet newOwned;
113 bool needNew =
false;
114 for (
int ic = 0, nc = nbins; ic < nc; ++ic) {
116 RooAbsPdf *pdfi = sim->getPdf(cat->getLabel());
117 RooAbsPdf *newpdf =
factorizePdf(observables, *pdfi, constraints);
118 factorizedPdfs[ic] = newpdf;
119 if (newpdf == 0) {
throw std::runtime_error(std::string(
"ERROR: channel ") + cat->getLabel() +
" factorized to zero."); }
120 if (newpdf != pdfi) { needNew =
true; newOwned.add(*newpdf); }
122 RooSimultaneous *
ret = sim;
124 ret =
new RooSimultaneous(TString::Format(
"%s_obsOnly", pdf.GetName()),
"", (RooAbsCategoryLValue&) sim->indexCat());
125 for (
int ic = 0, nc = nbins; ic < nc; ++ic) {
127 RooAbsPdf *newpdf = (RooAbsPdf *) factorizedPdfs[ic];
128 if (newpdf) ret->addPdf(*newpdf, cat->getLabel());
130 ret->addOwnedComponents(newOwned);
135 newret->addOwnedComponents(RooArgSet(*ret));
140 }
else if (pdf.dependsOn(observables)) {
143 if (!constraints.contains(pdf)) constraints.add(pdf);
154 const std::type_info &
id =
typeid(pdf);
155 if (
id ==
typeid(RooProdPdf)) {
156 RooProdPdf *
prod =
dynamic_cast<RooProdPdf *
>(&pdf);
157 RooArgList
list(prod->pdfList());
158 for (
int i = 0,
n =
list.getSize();
i <
n; ++
i) {
159 RooAbsPdf *pdfi = (RooAbsPdf *)
list.at(
i);
160 factorizePdf(observables, *pdfi, obsTerms, constraints);
163 RooSimultaneous *
sim =
dynamic_cast<RooSimultaneous *
>(&pdf);
164 RooAbsCategoryLValue *cat = (RooAbsCategoryLValue *) sim->indexCat().Clone();
165 for (
int ic = 0, nc = cat->numBins((
const char *)0); ic < nc; ++ic) {
167 RooAbsPdf *pdfi = sim->getPdf(cat->getLabel());
168 if (pdfi != 0)
factorizePdf(observables, *pdfi, obsTerms, constraints);
171 }
else if (pdf.dependsOn(observables)) {
172 if (!obsTerms.contains(pdf)) obsTerms.add(pdf);
174 if (!constraints.contains(pdf)) constraints.add(pdf);
178 RooAbsPdf *pdf =
dynamic_cast<RooAbsPdf *
>(&func);
180 factorizePdf(observables, *pdf, obsTerms, constraints, debug);
183 const std::type_info &
id =
typeid(func);
184 if (
id ==
typeid(RooProduct)) {
185 RooProduct *
prod =
dynamic_cast<RooProduct *
>(&func);
188 std::auto_ptr<TIterator> iter(
components.createIterator());
189 for (RooAbsReal *funci = (RooAbsReal *) iter->Next(); funci != 0; funci = (RooAbsReal *) iter->Next()) {
193 }
else if (func.dependsOn(observables)) {
194 if (!obsTerms.contains(func)) obsTerms.add(func);
196 if (!constraints.contains(func)) constraints.add(func);
208 if (constraints.getSize() == 0)
return 0;
209 return new RooProdPdf(name,
"", constraints);
215 return new RooProdPdf(name,
"", obsTerms);
220 RooArgSet
tmp(
"RealBranchNodeList") ;
221 pdf->branchNodeServerList(&tmp);
222 tmp.snapshot(holder, cloneLeafNodes);
224 return (RooAbsPdf*) holder.find(pdf->GetName());
228 RooArgSet
tmp(
"RealBranchNodeList") ;
229 pdf->branchNodeServerList(&tmp);
230 tmp.snapshot(holder, cloneLeafNodes);
232 return (RooAbsReal*) holder.find(pdf->GetName());
237 std::auto_ptr<TIterator> iterAll(allObjects.createIterator());
238 std::auto_ptr<TIterator> iterVal(values.createIterator());
239 for (RooAbsArg *
v = (RooAbsArg *) iterVal->Next();
v != 0;
v = (RooAbsArg *) iterVal->Next()) {
240 if (
typeid(*
v) !=
typeid(RooRealVar))
continue;
241 std::auto_ptr<TIterator> clientIter(
v->clientIterator());
242 for (RooAbsArg *
a = (RooAbsArg *) clientIter->Next();
a != 0;
a = (RooAbsArg *) clientIter->Next()) {
243 if (allObjects.containsInstance(*
a) && !clients.containsInstance(*
a)) clients.add(*
a);
249 bool changed =
false;
250 std::auto_ptr<TIterator> iter(coll.createIterator());
251 for (RooAbsArg *
a = (RooAbsArg *) iter->Next();
a != 0;
a = (RooAbsArg *) iter->Next()) {
252 RooRealVar *
v =
dynamic_cast<RooRealVar *
>(
a);
253 if (v && (v->isConstant() != constant)) {
255 v->setConstant(constant);
262 bool ok =
true; std::ostringstream
errors;
263 std::auto_ptr<TIterator> iter;
264 RooAbsPdf *pdf = model.GetPdf();
if (pdf == 0)
throw std::invalid_argument(
"Model without Pdf");
265 RooArgSet allowedToFloat;
266 if (model.GetObservables() == 0) {
267 ok =
false; errors <<
"ERROR: model does not define observables.\n";
269 if (throwOnFail)
throw std::invalid_argument(errors.str());
else return false;
271 allowedToFloat.add(*model.GetObservables());
273 if (model.GetParametersOfInterest() == 0) {
274 ok =
false; errors <<
"ERROR: model does not define parameters of interest.\n";
276 iter.reset(model.GetParametersOfInterest()->createIterator());
277 for (RooAbsArg *
a = (RooAbsArg *) iter->Next();
a != 0;
a = (RooAbsArg *) iter->Next()) {
278 RooRealVar *
v =
dynamic_cast<RooRealVar *
>(
a);
279 if (!v) { ok =
false; errors <<
"ERROR: parameter of interest " <<
a->GetName() <<
" is a " <<
a->ClassName() <<
" and not a RooRealVar\n";
continue; }
280 if (v->isConstant()) { ok =
false; errors <<
"ERROR: parameter of interest " <<
a->GetName() <<
" is constant\n";
continue; }
281 if (!pdf->dependsOn(*v)) { ok =
false; errors <<
"ERROR: pdf does not depend on parameter of interest " <<
a->GetName() <<
"\n";
continue; }
282 allowedToFloat.add(*v);
285 if (model.GetNuisanceParameters() != 0) {
286 iter.reset(model.GetNuisanceParameters()->createIterator());
287 for (RooAbsArg *
a = (RooAbsArg *) iter->Next();
a != 0;
a = (RooAbsArg *) iter->Next()) {
288 RooRealVar *
v =
dynamic_cast<RooRealVar *
>(
a);
289 if (!v) { ok =
false; errors <<
"ERROR: nuisance parameter " <<
a->GetName() <<
" is a " <<
a->ClassName() <<
" and not a RooRealVar\n";
continue; }
290 if (v->isConstant()) { ok =
false; errors <<
"ERROR: nuisance parameter " <<
a->GetName() <<
" is constant\n";
continue; }
291 if (!pdf->dependsOn(*v)) { errors <<
"WARNING: pdf does not depend on nuisance parameter " <<
a->GetName() <<
"\n";
continue; }
292 allowedToFloat.add(*v);
295 if (model.GetGlobalObservables() != 0) {
296 iter.reset(model.GetGlobalObservables()->createIterator());
297 for (RooAbsArg *
a = (RooAbsArg *) iter->Next();
a != 0;
a = (RooAbsArg *) iter->Next()) {
298 RooRealVar *
v =
dynamic_cast<RooRealVar *
>(
a);
299 if (!v) { ok =
false; errors <<
"ERROR: global observable " <<
a->GetName() <<
" is a " <<
a->ClassName() <<
" and not a RooRealVar\n";
continue; }
300 if (!v->isConstant()) { ok =
false; errors <<
"ERROR: global observable " <<
a->GetName() <<
" is not constant\n";
continue; }
301 if (!pdf->dependsOn(*v)) { errors <<
"WARNING: pdf does not depend on global observable " <<
a->GetName() <<
"\n";
continue; }
304 std::auto_ptr<RooArgSet> params(pdf->getParameters(*model.GetObservables()));
305 iter.reset(params->createIterator());
306 for (RooAbsArg *
a = (RooAbsArg *) iter->Next();
a != 0;
a = (RooAbsArg *) iter->Next()) {
307 if (
a->getAttribute(
"flatParam") &&
a->isConstant()) {
308 ok =
false; errors <<
"ERROR: parameter " <<
a->GetName() <<
" is declared as flatParam but is constant.\n";
310 if (
a->isConstant() || allowedToFloat.contains(*
a))
continue;
311 if (
a->getAttribute(
"flatParam")) {
312 RooRealVar *rrv =
dynamic_cast<RooRealVar *
>(
a);
313 if (rrv->getVal() > rrv->getMax() || rrv->getVal() < rrv->getMin()) {
314 ok =
false; errors <<
"ERROR: flatParam " << rrv->GetName() <<
" has a value " << rrv->getVal() <<
315 " outside of the defined range [" << rrv->getMin() <<
", " << rrv->getMax() <<
"]\n";
318 errors <<
"WARNING: pdf parameter " <<
a->GetName() <<
" (type " <<
a->ClassName() <<
") is not allowed to float (it's not nuisance, poi, observable or global observable\n";
323 if (!ok && throwOnFail)
throw std::invalid_argument(errors.str());
329 RooAbsCategoryLValue *cat = (RooAbsCategoryLValue *) sim->indexCat().Clone();
330 int nbins = cat->numBins((
const char *)0);
331 TObjArray factorizedPdfs(nbins);
333 for (
int ic = 0, nc = nbins; ic < nc; ++ic) {
335 RooAbsPdf *pdfi = sim->getPdf(cat->getLabel());
336 if (pdfi == 0) { factorizedPdfs[ic] = 0;
continue; }
337 RooAbsPdf *newpdf =
factorizePdf(observables, *pdfi, constraints);
338 factorizedPdfs[ic] = newpdf;
339 if (newpdf == 0) {
continue; }
340 if (newpdf != pdfi) { newOwned.add(*newpdf); }
342 RooSimultaneous *
ret =
new RooSimultaneous(TString::Format(
"%s_reloaded", sim->GetName()),
"", (RooAbsCategoryLValue&) sim->indexCat());
343 for (
int ic = 0, nc = nbins; ic < nc; ++ic) {
345 RooAbsPdf *newpdf = (RooAbsPdf *) factorizedPdfs[ic];
347 if (constraints.getSize() > 0) {
348 RooArgList allfactors(constraints); allfactors.add(*newpdf);
349 RooProdPdf *newerpdf =
new RooProdPdf(TString::Format(
"%s_plus_constr", newpdf->GetName()),
"", allfactors);
350 ret->addPdf(*newerpdf, cat->getLabel());
352 newOwned.add(*newerpdf);
354 ret->addPdf(*newpdf, cat->getLabel());
358 ret->addOwnedComponents(newOwned);
365 if (&from == &to)
return;
366 const std::set<std::string> attribs = from.attributes();
367 if (!attribs.empty()) {
368 for (std::set<std::string>::const_iterator it = attribs.begin(), ed = attribs.end(); it != ed; ++it) to.setAttribute(it->c_str());
370 const std::map<std::string, std::string> strattribs = from.stringAttributes();
371 if (!strattribs.empty()) {
372 for (std::map<std::string,std::string>::const_iterator it = strattribs.begin(), ed = strattribs.end(); it != ed; ++it) to.setStringAttribute(it->first.c_str(), it->second.c_str());
378 RooAbsCategoryLValue &cat =
const_cast<RooAbsCategoryLValue &
>(simPdf.indexCat());
379 TList *
split = simData.split(cat, kTRUE);
380 for (
int i = 0,
n = cat.numBins((
const char *)0);
i <
n; ++
i) {
382 RooAbsPdf *pdf = simPdf.getPdf(cat.getLabel());
383 if (pdf->getAttribute(
"forceGenBinned") || pdf->getAttribute(
"forceGenUnbinned")) {
384 if (verbose)
std::cout <<
" - " << cat.getLabel() <<
" has generation mode already set" << std::endl;
387 RooAbsData *spl = (RooAbsData *) split->FindObject(cat.getLabel());
389 if (verbose)
std::cout <<
" - " << cat.getLabel() <<
" has no dataset, cannot guess" << std::endl;
392 if (spl->numEntries() != spl->sumEntries()) {
393 if (verbose)
std::cout <<
" - " << cat.getLabel() <<
" has " << spl->numEntries() <<
" num entries of sum " << spl->sumEntries() <<
", mark as binned" << std::endl;
394 pdf->setAttribute(
"forceGenBinned");
396 if (verbose)
std::cout <<
" - " << cat.getLabel() <<
" has " << spl->numEntries() <<
" num entries of sum " << spl->sumEntries() <<
", mark as unbinned" << std::endl;
397 pdf->setAttribute(
"forceGenUnbinned");
402 std::vector<RooPlot *>
403 utils::makePlots(
const RooAbsPdf &pdf,
const RooAbsData &
data,
const char *signalSel,
const char *backgroundSel,
float rebinFactor) {
404 std::vector<RooPlot *>
ret;
406 RooAbsPdf *facpdf =
factorizePdf(*data.get(0),
const_cast<RooAbsPdf &
>(pdf), constraints);
408 const std::type_info &
id =
typeid(*facpdf);
410 const RooSimultaneous *
sim =
dynamic_cast<const RooSimultaneous *
>(&pdf);
411 const RooAbsCategoryLValue &cat = (RooAbsCategoryLValue &) sim->indexCat();
412 TList *
datasets = data.split(cat,
true);
413 TIter
next(datasets);
414 for (RooAbsData *ds = (RooAbsData *)
next(); ds != 0; ds = (RooAbsData *)
next()) {
415 RooAbsPdf *pdfi = sim->getPdf(ds->GetName());
416 std::auto_ptr<RooArgSet> obs(pdfi->getObservables(ds));
417 if (obs->getSize() == 0)
break;
418 RooRealVar *
x =
dynamic_cast<RooRealVar *
>(obs->first());
419 if (x == 0)
continue;
420 int nbins = x->numBins();
if (nbins == 0) nbins = 100;
421 if (nbins/rebinFactor > 6) nbins = ceil(nbins/rebinFactor);
422 ret.push_back(x->frame(RooFit::Title(ds->GetName()), RooFit::Bins(nbins)));
423 ret.back()->SetName(ds->GetName());
424 ds->plotOn(ret.back());
425 if (signalSel && strlen(signalSel)) pdfi->plotOn(ret.back(), RooFit::LineColor(209), RooFit::Components(signalSel));
426 if (backgroundSel && strlen(backgroundSel)) pdfi->plotOn(ret.back(), RooFit::LineColor(206), RooFit::Components(backgroundSel));
427 pdfi->plotOn(ret.back());
431 }
else if (pdf.canBeExtended()) {
432 std::auto_ptr<RooArgSet> obs(pdf.getObservables(&data));
433 RooRealVar *
x =
dynamic_cast<RooRealVar *
>(obs->first());
435 ret.push_back(x->frame());
436 ret.back()->SetName(
"data");
437 data.plotOn(ret.back());
438 if (signalSel && strlen(signalSel)) pdf.plotOn(ret.back(), RooFit::LineColor(209), RooFit::Components(signalSel));
439 if (backgroundSel && strlen(backgroundSel)) pdf.plotOn(ret.back(), RooFit::LineColor(206), RooFit::Components(backgroundSel));
440 pdf.plotOn(ret.back());
443 if (facpdf != &pdf) {
delete facpdf; }
453 RooLinkedListIter iter = src.iterator();
int i = 0;
454 for (RooAbsArg *
a = (RooAbsArg *) iter.Next();
a != 0;
a = (RooAbsArg *) iter.Next(), ++
i) {
455 RooRealVar *rrv =
dynamic_cast<RooRealVar *
>(
a);
456 if (rrv == 0)
throw std::invalid_argument(
"Collection to read from contains a non-RooRealVar");
463 RooLinkedListIter iter = src.iterator();
int i = 0;
464 for (RooAbsArg *
a = (RooAbsArg *) iter.Next();
a != 0;
a = (RooAbsArg *) iter.Next(), ++
i) {
465 RooRealVar *rrv =
dynamic_cast<RooRealVar *
>(
a);
466 rrv->setVal(values_[i]);
469 RooLinkedListIter iter = src_->iterator();
int i = 0;
470 for (RooAbsArg *
a = (RooAbsArg *) iter.Next();
a != 0;
a = (RooAbsArg *) iter.Next(), ++
i) {
471 RooAbsArg *a2 = src.find(
a->GetName());
if (a2 == 0)
continue;
472 RooRealVar *rrv =
dynamic_cast<RooRealVar *
>(a2);
473 rrv->setVal(values_[i]);
479 if (src_ == 0) { printf(
"<NIL>\n");
return; }
481 RooLinkedListIter iter = src_->iterator();
int i = 0;
482 for (RooAbsArg *
a = (RooAbsArg *) iter.Next();
a != 0;
a = (RooAbsArg *) iter.Next(), ++
i) {
483 printf(
" %3d) %-30s = %9.6g\n", i,
a->GetName(), values_[
i]);
const RooAbsCollection * src_
std::vector< RooPlot * > makePlots(const RooAbsPdf &pdf, const RooAbsData &data, const char *signalSel=0, const char *backgroundSel=0, float rebinFactor=1.0)
make plots, if possible
bool setAllConstant(const RooAbsCollection &coll, bool constant=true)
set all RooRealVars to constants. return true if at least one changed status
void guessChannelMode(RooSimultaneous &simPdf, RooAbsData &simData, bool verbose=false)
void factorizeFunc(const RooArgSet &observables, RooAbsReal &pdf, RooArgList &obsTerms, RooArgList &otherTerms, bool debug=false)
factorize a RooAbsReal
std::vector< double > values_
void getClients(const RooAbsCollection &values, const RooAbsCollection &allObjects, RooAbsCollection &clients)
add to 'clients' all object within allObjects that directly depend on values
void writeTo(const RooAbsCollection &) const
RooAbsPdf * makeNuisancePdf(RooStats::ModelConfig &model, const char *name="nuisancePdf")
const RooAbsCollection & src() const
RooSimultaneous * rebuildSimPdf(const RooArgSet &observables, RooSimultaneous *pdf)
void printPdf(RooAbsPdf *pdf)
RooAbsPdf * fullClonePdf(const RooAbsPdf *pdf, RooArgSet &holder, bool cloneLeafNodes=false)
RooAbsPdf * factorizePdf(const RooArgSet &observables, RooAbsPdf &pdf, RooArgList &constraints)
void printRDH(RooAbsData *data)
RooAbsPdf * makeObsOnlyPdf(RooStats::ModelConfig &model, const char *name="obsPdf")
Note: doesn't recompose Simultaneous pdfs properly, for that use factorizePdf method.
RooAbsReal * fullCloneFunc(const RooAbsReal *pdf, RooArgSet &holder, bool cloneLeafNodes=false)
static std::string from(" from ")
bool checkModel(const RooStats::ModelConfig &model, bool throwOnFail=false)
void readFrom(const RooAbsCollection &)
std::vector< std::vector< double > > tmp
void printRAD(const RooAbsData *d)
char data[epos_bytes_allocation]
void Print(const char *fmt) const
void copyAttributes(const RooAbsArg &from, RooAbsArg &to)
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger list("!*","!HLTx*"if it matches 2 triggers or more) will accept the event if all the matching triggers are FAIL.It will reject the event if any of the triggers are PASS or EXCEPTION(this matches the behavior of"!*"before the partial wildcard feature was incorporated).Triggers which are in the READY state are completely ignored.(READY should never be returned since the trigger paths have been run