Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
xRooNLLVar.cxx
Go to the documentation of this file.
1/*
2 * Project: xRooFit
3 * Author:
4 * Will Buttinger, RAL 2022
5 *
6 * Copyright (c) 2022, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13/** \class ROOT::Experimental::XRooFit::xRooNLLVar
14\ingroup xroofit
15
16This xRooNLLVar object has several special methods, e.g. for fitting and toy dataset generation.
17
18 */
19
20#include "RVersion.h"
21
22#if ROOT_VERSION_CODE < ROOT_VERSION(6, 27, 00)
23#define protected public
24#endif
25
26#include "RooFitResult.h"
27
28#if ROOT_VERSION_CODE < ROOT_VERSION(6, 33, 00)
29#include "RooNLLVar.h"
30#endif
31
32#ifdef protected
33#undef protected
34#endif
35
36#include "xRooFit/xRooFit.h"
37
38#include "RooCmdArg.h"
39#include "RooAbsPdf.h"
40#include "RooAbsData.h"
41
42#include "RooConstraintSum.h"
43#include "RooSimultaneous.h"
45#include "TPRegexp.h"
46#include "TEfficiency.h"
47
48#include "RooRealVar.h"
49#include "Math/ProbFunc.h"
50#include "RooRandom.h"
51
52#include "TPad.h"
53#include "TSystem.h"
54
55#include "coutCapture.h"
56
57#include <chrono>
58
59#include "Math/GenAlgoOptions.h"
60
61#if ROOT_VERSION_CODE < ROOT_VERSION(6, 27, 00)
62#define private public
63#define GETWS(a) a->_myws
64#define GETWSSETS(w) w->_namedSets
65#else
66#define GETWS(a) a->workspace()
67#define GETWSSETS(w) w->sets()
68#endif
69#include "RooWorkspace.h"
70#ifdef private
71#undef private
72#endif
73
74#if ROOT_VERSION_CODE < ROOT_VERSION(6, 27, 00)
75#define protected public
76#endif
78#ifdef protected
79#undef protected
80#endif
81
82#include "TMultiGraph.h"
83#include "TCanvas.h"
84#include "TArrow.h"
85#include "RooStringVar.h"
86#include "TDirectory.h"
87#include "TStyle.h"
88#include "TH1D.h"
89#include "TLegend.h"
90#include "RooCategory.h"
91#include "TTree.h"
92#include "TGraph2D.h"
93
94#include "RooGaussian.h"
95#include "RooPoisson.h"
96
97#include "TROOT.h"
98#include "TKey.h"
99#include "TRegexp.h"
100#include "TStopwatch.h"
101
103
104std::set<int> xRooNLLVar::xRooHypoPoint::allowedStatusCodes = {0};
105
107public:
108 AutoRestorer(const RooAbsCollection &s, xRooNLLVar *nll = nullptr) : fSnap(s.snapshot()), fNll(nll)
109 {
110 fPars.add(s);
111 if (fNll) {
112 // if (!fNll->kReuseNLL) fOldNll = *fNll;
113 fOldData = fNll->getData();
114 fOldName = fNll->get()->GetName();
115 fOldTitle = fNll->get()->getStringAttribute("fitresultTitle");
116 }
117 }
119 {
121 if (fNll) {
122 // commented out code was attempt to speed up things avoid unnecessarily reinitializing things over and over
123 // if (!fNll->kReuseNLL) {
124 // // can be faster just by putting back in old nll
125 // fNll->std::shared_ptr<RooAbsReal>::operator=(fOldNll);
126 // fNll->fData = fOldData.first;
127 // fNll->fGlobs = fOldData.second;
128 // } else {
129 // fNll->setData(fOldData);
130 // fNll->get()->SetName(fOldName);
131 // fNll->get()->setStringAttribute("fitresultTitle", (fOldTitle == "") ? nullptr : fOldTitle);
132 // }
133 fNll->fGlobs = fOldData.second; // will mean globs matching checks are skipped in setData
134 fNll->setData(fOldData);
135 fNll->get()->SetName(fOldName);
136 fNll->get()->setStringAttribute("fitresultTitle", (fOldTitle == "") ? nullptr : fOldTitle);
137 }
138 }
140 std::unique_ptr<RooAbsCollection> fSnap;
141 xRooNLLVar *fNll = nullptr;
142 // std::shared_ptr<RooAbsReal> fOldNll;
143 std::pair<std::shared_ptr<RooAbsData>, std::shared_ptr<const RooAbsCollection>> fOldData;
145};
146
147xRooNLLVar::~xRooNLLVar() {}
148
149xRooNLLVar::xRooNLLVar(RooAbsPdf &pdf, const std::pair<RooAbsData *, const RooAbsCollection *> &data,
150 const RooLinkedList &nllOpts)
151 : xRooNLLVar(std::shared_ptr<RooAbsPdf>(&pdf, [](RooAbsPdf *) {}),
152 std::make_pair(std::shared_ptr<RooAbsData>(data.first, [](RooAbsData *) {}),
153 std::shared_ptr<const RooAbsCollection>(data.second, [](const RooAbsCollection *) {})),
154 nllOpts)
155{
156}
157
158xRooNLLVar::xRooNLLVar(const std::shared_ptr<RooAbsPdf> &pdf,
159 const std::pair<std::shared_ptr<RooAbsData>, std::shared_ptr<const RooAbsCollection>> &data,
160 const RooLinkedList &opts)
161 : fPdf(pdf), fData(data.first), fGlobs(data.second)
162{
163
164#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 37, 00)
166#else
168#endif
169 fOpts = std::shared_ptr<RooLinkedList>(new RooLinkedList, [](RooLinkedList *l) {
170 if (l)
171 l->Delete();
172 delete l;
173 });
174 fOpts->SetName("");
175
176 // we *must* take global observables from the model even if they are included in the dataset
177 // this is because the way xRooNLLVar is coded up it assumes the globs in the funcVars *ARE*
178 // part of the model
179 fOpts->Add(RooFit::GlobalObservablesSource("model").Clone(nullptr));
180
181 for (int i = 0; i < opts.GetSize(); i++) {
182 if (strlen(opts.At(i)->GetName()) == 0)
183 continue; // skipping "none" cmds
184 if (strcmp(opts.At(i)->GetName(), "GlobalObservables") == 0) {
185 // will skip here to add with the obs from the function below
186 // must match global observables
187 auto gl = dynamic_cast<RooCmdArg *>(opts.At(i))->getSet(0);
188 if (!fGlobs || !fGlobs->equals(*gl)) {
189 throw std::runtime_error("GlobalObservables mismatch");
190 }
191 } else {
192 SetOption(dynamic_cast<RooCmdArg &>(*opts.At(i)));
193 }
194 }
195 if (fGlobs) {
196 // add global observables opt with function obs
197 auto _vars = std::unique_ptr<RooArgSet>(fPdf->getVariables());
198 if (auto extCon = dynamic_cast<RooCmdArg *>(fOpts->find("ExternalConstraints"))) {
199 for (auto con : *extCon->getSet(0)) {
200 _vars->add(*std::unique_ptr<RooArgSet>(con->getVariables()));
201 }
202 }
203 auto _funcGlobs = std::unique_ptr<RooArgSet>(dynamic_cast<RooArgSet *>(_vars->selectCommon(*fGlobs)));
204 fOpts->Add(RooFit::GlobalObservables(*_funcGlobs).Clone());
205 }
206
207 if (auto flag = dynamic_cast<RooCmdArg *>(fOpts->find("ReuseNLL"))) {
208 kReuseNLL = flag->getInt(0);
209 }
210
211 // if fit range specified, and pdf is a RooSimultaneous, may need to 'reduce' the model if some of the pdfs are in
212 // range and others are not
213 if (auto range = dynamic_cast<RooCmdArg *>(fOpts->find("RangeWithName"))) {
214 TString rangeName = range->getString(0);
215
216 // reduce the data here for convenience, not really necessary because will happen inside RooNLLVar but still
217 // fData.reset( fData->reduce(RooFit::SelectVars(*fData->get()),RooFit::CutRange(rangeName)) );
218
219 if (auto s = dynamic_cast<RooSimultaneous *>(fPdf.get()); s) {
220 auto &_cat = const_cast<RooAbsCategoryLValue &>(s->indexCat());
221 std::vector<TString> chanPatterns;
222 TStringToken pattern(rangeName, ",");
223 bool hasRange(false);
224 std::string noneCatRanges;
225 while (pattern.NextToken()) {
226 chanPatterns.emplace_back(pattern);
227 if (_cat.hasRange(chanPatterns.back())) {
228 hasRange = true;
229 } else {
230 if (!noneCatRanges.empty())
231 noneCatRanges += ",";
232 noneCatRanges += chanPatterns.back();
233 }
234 }
235 if (hasRange) {
236 // must remove the ranges that referred to selections on channel category
237 // otherwise RooFit will incorrectly evaluate the NLL (it creates a partition for each range given in the
238 // list, which all end up being equal) the NLL would become scaled by the number of ranges given
239 if (noneCatRanges.empty()) {
240 fOpts->Remove(range);
242 } else {
243 range->setString(0, noneCatRanges.c_str());
244 }
245 // must reduce because category var has one of the ranges
246 auto newPdf =
247 std::make_shared<RooSimultaneous>(TString::Format("%s_reduced", s->GetName()), "Reduced model", _cat);
248 for (auto &c : _cat) {
249 auto _pdf = s->getPdf(c.first.c_str());
250 if (!_pdf)
251 continue;
252 _cat.setIndex(c.second);
253 bool matchAny = false;
254 for (auto &p : chanPatterns) {
255 if (_cat.hasRange(p) && _cat.inRange(p)) {
256 matchAny = true;
257 break;
258 }
259 }
260 if (matchAny) {
261 newPdf->addPdf(*_pdf, c.first.c_str());
262 }
263 }
264 fPdf = newPdf;
265 }
266 }
267 }
268
269 // if (fGlobs) {
270 // // must check GlobalObservables is in the list
271 // }
272 //
273 // if (auto globs = dynamic_cast<RooCmdArg*>(fOpts->find("GlobalObservables"))) {
274 // // first remove any obs the pdf doesnt depend on
275 // auto _vars = std::unique_ptr<RooAbsCollection>( fPdf->getVariables() );
276 // auto _funcGlobs = std::unique_ptr<RooAbsCollection>(_vars->selectCommon(*globs->getSet(0)));
277 // fGlobs.reset( std::unique_ptr<RooAbsCollection>(globs->getSet(0)->selectCommon(*_funcGlobs))->snapshot() );
278 // globs->setSet(0,dynamic_cast<const RooArgSet&>(*_funcGlobs)); // globs in linked list has its own argset
279 // but args need to live as long as the func
280 // /*RooArgSet toRemove;
281 // for(auto a : *globs->getSet(0)) {
282 // if (!_vars->find(*a)) toRemove.add(*a);
283 // }
284 // const_cast<RooArgSet*>(globs->getSet(0))->remove(toRemove);
285 // fGlobs.reset( globs->getSet(0)->snapshot() );
286 // fGlobs->setAttribAll("Constant",true);
287 // const_cast<RooArgSet*>(globs->getSet(0))->replace(*fGlobs);*/
288 // }
289}
290
291xRooNLLVar::xRooNLLVar(const std::shared_ptr<RooAbsPdf> &pdf, const std::shared_ptr<RooAbsData> &data,
292 const RooLinkedList &opts)
293 : xRooNLLVar(
294 pdf,
295 std::make_pair(data, std::shared_ptr<const RooAbsCollection>(
296 (opts.find("GlobalObservables"))
297 ? dynamic_cast<RooCmdArg *>(opts.find("GlobalObservables"))->getSet(0)->snapshot()
298 : nullptr)),
299 opts)
300{
301}
302
304{
305 std::cout << "PDF: ";
306 if (fPdf) {
307 fPdf->Print();
308 } else {
309 std::cout << "<null>" << std::endl;
310 }
311 std::cout << "Data: ";
312 if (fData) {
313 fData->Print();
314 } else {
315 std::cout << "<null>" << std::endl;
316 }
317 std::cout << "NLL Options: " << std::endl;
318 for (int i = 0; i < fOpts->GetSize(); i++) {
319 auto c = dynamic_cast<RooCmdArg *>(fOpts->At(i));
320 if (!c)
321 continue;
322 std::cout << " " << c->GetName() << " : ";
323 if (c->getString(0)) {
324 std::cout << c->getString(0);
325 } else if (c->getSet(0) && !c->getSet(0)->empty()) {
326 std::cout << (c->getSet(0)->contentsString());
327 } else {
328 std::cout << c->getInt(0);
329 }
330 std::cout << std::endl;
331 }
332 if (fFitConfig) {
333 std::cout << "Fit Config: " << std::endl;
334 std::cout << " UseParabErrors: " << (fFitConfig->ParabErrors() ? "True" : "False")
335 << " [toggles HESSE algorithm]" << std::endl;
336 std::cout << " MinimizerOptions: " << std::endl;
337 fFitConfig->MinimizerOptions().Print();
338 }
339 std::cout << "Last Rebuild Log Output: " << fFuncCreationLog << std::endl;
340}
341
343{
344 TString oldName = "";
345 if (std::shared_ptr<RooAbsReal>::get())
346 oldName = std::shared_ptr<RooAbsReal>::get()->GetName();
347 if (fPdf) {
349 // need to find all RooRealSumPdf nodes and mark them binned or unbinned as required
350 RooArgSet s;
351 fPdf->treeNodeServerList(&s, nullptr, true, false);
352 s.add(*fPdf); // ensure include self in case fitting a RooRealSumPdf
353 bool isBinned = false;
354 bool hasBinned = false; // if no binned option then 'auto bin' ...
355 if (auto a = dynamic_cast<RooCmdArg *>(fOpts->find("Binned")); a) {
356 hasBinned = true;
357 isBinned = a->getInt(0);
358 }
359 std::map<RooAbsArg *, bool> origValues;
360 if (hasBinned) {
361 for (auto a : s) {
362 if (a->InheritsFrom("RooRealSumPdf")) {
363 // since RooNLLVar will assume binBoundaries available (not null), we should check bin boundaries
364 // available
365 bool setBinned = false;
366 if (isBinned) {
367 std::unique_ptr<RooArgSet> obs(a->getObservables(fData->get()));
368 if (obs->size() == 1) { // RooNLLVar requires exactly 1 obs
369 auto *var = static_cast<RooRealVar *>(obs->first());
370 std::unique_ptr<std::list<double>> boundaries{dynamic_cast<RooAbsReal *>(a)->binBoundaries(
371 *var, -std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity())};
372 if (boundaries) {
373 if (!std::shared_ptr<RooAbsReal>::get()) {
374 Info("xRooNLLVar", "%s will be evaluated as a Binned PDF (%d bins)", a->GetName(),
375 int(boundaries->size() - 1));
376 }
377 setBinned = true;
378 }
379 }
380 }
381 origValues[a] = a->getAttribute("BinnedLikelihood");
382 a->setAttribute("BinnedLikelihood", setBinned);
383 }
384 }
385 }
386 std::map<RooAbsPdf *, std::string> normRanges;
387 std::set<TObject *> removedOpts;
388 if (auto range = dynamic_cast<RooCmdArg *>(fOpts->find("RangeWithName"))) {
389 TString rangeName = range->getString(0);
390 if (auto sr = dynamic_cast<RooCmdArg *>(fOpts->find("SplitRange"));
391 sr && sr->getInt(0) && dynamic_cast<RooSimultaneous *>(fPdf.get())) {
392 if (auto special = fOpts->find("RangeOptimize")) {
393 removedOpts.insert(sr);
394 fOpts->Remove(sr);
395 removedOpts.insert(range);
396 fOpts->Remove(range);
397 removedOpts.insert(special);
398 fOpts->Remove(special);
399 }
400 // doing split range ... need to loop over categories of simpdf and apply range to each
401 auto simPdf = dynamic_cast<RooSimultaneous *>(fPdf.get());
402 for (auto cat : simPdf->indexCat()) {
403 auto subpdf = simPdf->getPdf(cat.first.c_str());
404 if (!subpdf)
405 continue; // state not in pdf
407 srangeName.ReplaceAll(",", "_" + cat.first + ",");
408 srangeName += "_" + cat.first;
410 subpdf->treeNodeServerList(&ss, nullptr, true, false);
411 ss.add(*subpdf);
412 for (auto a : ss) {
413 if (a->InheritsFrom("RooAddPdf")) {
414 auto p = dynamic_cast<RooAbsPdf *>(a);
415 normRanges[p] = p->normRange() ? p->normRange() : "";
416 p->setNormRange(srangeName);
417 }
418 }
419 }
420 } else {
421 // set range on all AddPdfs before creating - needed in cases where coefs are present and need fractioning
422 // based on fit range bugfix needed: roofit needs to propagate the normRange to AddPdfs child nodes (used in
423 // createExpectedEventsFunc)
424 for (auto a : s) {
425 if (a->InheritsFrom("RooAddPdf")) {
426 auto p = dynamic_cast<RooAbsPdf *>(a);
427 normRanges[p] = p->normRange() ? p->normRange() : "";
428 p->setNormRange(rangeName);
429 }
430 }
431 }
432 }
433 // before creating, clear away caches if any if pdf is in ws
434 if (GETWS(fPdf)) {
435 std::set<std::string> setNames;
436 for (auto &a : GETWSSETS(GETWS(fPdf))) {
437 if (TString(a.first.c_str()).BeginsWith("CACHE_")) {
438 setNames.insert(a.first);
439 }
440 }
441 for (auto &a : setNames) {
442 GETWS(fPdf)->removeSet(a.c_str());
443 }
444 }
445 std::set<std::string> attribs;
446 if (std::shared_ptr<RooAbsReal>::get())
447 attribs = std::shared_ptr<RooAbsReal>::get()->attributes();
448 this->reset(std::unique_ptr<RooAbsReal>{fPdf->createNLL(*fData, *fOpts)}.release());
449 std::shared_ptr<RooAbsReal>::get()->SetName(TString::Format("nll_%s/%s", fPdf->GetName(), fData->GetName()));
450 for (auto o : removedOpts)
451 fOpts->Add(o);
452 // RooFit only swaps in what it calls parameters, this misses out the RooConstVars which we treat as pars as well
453 // so swap those in ... question: is recursiveRedirectServers usage in RooAbsOptTestStatic (and here) a memory
454 // leak?? where do the replaced servers get deleted??
455
456 for (auto &[k, v] : normRanges)
457 k->setNormRange(v == "" ? nullptr : v.c_str());
458
459 for (auto &a : attribs)
460 std::shared_ptr<RooAbsReal>::get()->setAttribute(a.c_str());
461 // create parent on next line to avoid triggering workspace initialization code in constructor of xRooNode
462 if (GETWS(fPdf)) {
463 xRooNode(*GETWS(fPdf), std::make_shared<xRooNode>()).sterilize();
464 } // there seems to be a nasty bug somewhere that can make the cache become invalid, so clear it here
465 if (oldName != "")
466 std::shared_ptr<RooAbsReal>::get()->SetName(oldName);
467 if (!origValues.empty()) {
468 // need to evaluate NOW so that slaves are created while the BinnedLikelihood settings are in place
469 std::shared_ptr<RooAbsReal>::get()->getVal();
470 for (auto &[o, v] : origValues)
471 o->setAttribute("BinnedLikelihood", v);
472 }
473 }
474
475 fFuncVars = std::unique_ptr<RooArgSet>{std::shared_ptr<RooAbsReal>::get()->getVariables()};
476 if (fGlobs) {
477 fFuncGlobs.reset(fFuncVars->selectCommon(*fGlobs));
478 fFuncGlobs->setAttribAll("Constant", true);
479 }
480 fConstVars.reset(fFuncVars->selectByAttrib("Constant", true)); // will check if any of these have floated
481}
482
483std::pair<std::shared_ptr<RooAbsData>, std::shared_ptr<const RooAbsCollection>>
485{
486 if (!fPdf)
487 return std::pair(nullptr, nullptr);
488 auto fr = std::make_shared<RooFitResult>(TUUID().AsString());
489 fr->setFinalParList(RooArgList());
491 l.add((fFuncVars) ? *fFuncVars : *std::unique_ptr<RooAbsCollection>(fPdf->getParameters(*fData)));
492 fr->setConstParList(l);
493 const_cast<RooArgList &>(fr->constPars()).setAttribAll("global", false);
494 if (fGlobs)
495 std::unique_ptr<RooAbsCollection>(fr->constPars().selectCommon(*fGlobs))->setAttribAll("global", true);
496 return xRooFit::generateFrom(*fPdf, *fr, expected, seed);
497}
498
500
501xRooNLLVar::xRooFitResult::xRooFitResult(const std::shared_ptr<xRooNode> &in, const std::shared_ptr<xRooNLLVar> &nll)
502 : std::shared_ptr<const RooFitResult>(std::dynamic_pointer_cast<const RooFitResult>(in->fComp)),
503 fNode(in),
504 fNll(nll),
505 fCfits(std::make_shared<std::map<std::string, xRooFitResult>>())
506{
507}
509{
510 return fNode->get<RooFitResult>();
511}
512// xRooNLLVar::xRooFitResult::operator std::shared_ptr<const RooFitResult>() const { return
513// std::dynamic_pointer_cast<const RooFitResult>(fNode->fComp); }
514xRooNLLVar::xRooFitResult::operator const RooFitResult *() const
515{
516 return fNode->get<const RooFitResult>();
517}
519{
520 fNode->Draw(opt);
521}
522
524{
525
526 // create a hypoPoint with ufit equal to this fit
527 // and poi equal to given poi
528 if (!fNll)
529 throw std::runtime_error("xRooFitResult::cfit: Cannot create cfit without nll");
530
531 // see if fit already done
532 if (alias) {
533 if (auto res = fCfits->find(alias); res != fCfits->end()) {
534 return res->second;
535 }
536 }
537 if (auto res = fCfits->find(poiValues); res != fCfits->end()) {
538 return res->second;
539 }
540
541 AutoRestorer s(*fNll->fFuncVars);
542 *fNll->fFuncVars = get()->floatParsFinal();
543 fNll->fFuncVars->assignValueOnly(get()->constPars());
544 std::unique_ptr<RooAbsCollection>(fNll->fFuncVars->selectCommon(get()->floatParsFinal()))
545 ->setAttribAll("Constant", false);
546 std::unique_ptr<RooAbsCollection>(fNll->fFuncVars->selectCommon(get()->constPars()))->setAttribAll("Constant", true);
547
548 auto hp = fNll->hypoPoint(poiValues, std::numeric_limits<double>::quiet_NaN(), xRooFit::Asymptotics::Unknown);
549 hp.fUfit = *this;
550 auto out = xRooNLLVar::xRooFitResult(std::make_shared<xRooNode>(hp.cfit_null(), fNode->fParent), fNll);
551 fCfits->insert(std::pair((alias) ? alias : poiValues, out));
552 return out;
553}
555{
556 RooRealVar *npVar = dynamic_cast<RooRealVar *>((prefit ? get()->floatParsInit() : get()->floatParsFinal()).find(np));
557 if (!npVar)
558 throw std::runtime_error("xRooFitResult::ifit: par not found");
559 return cfit(TString::Format("%s=%f", np, npVar->getVal() + (up ? npVar->getErrorHi() : npVar->getErrorLo())));
560}
561double xRooNLLVar::xRooFitResult::impact(const char *poi, const char *np, bool up, bool prefit, bool covApprox)
562{
563 if (!covApprox) {
564 // get the ifit and get the difference between the postFit poi values
565 RooRealVar *poiHat = dynamic_cast<RooRealVar *>((get()->floatParsFinal()).find(poi));
566 if (!poiHat)
567 throw std::runtime_error("xRooFitResult::impact: poi not found");
568 auto _ifit = ifit(np, up, prefit);
569 if (!_ifit)
570 throw std::runtime_error("xRooFitResult::impact: null ifit");
571 if (_ifit->status() != 0)
572 fNode->Warning("impact", "ifit status code is %d", _ifit->status());
573 return _ifit->floatParsFinal().getRealValue(poi) - poiHat->getVal();
574 } else {
575 // estimate impact from the covariance matrix ....
576 int iPoi = get()->floatParsFinal().index(poi);
577 int iNp = get()->floatParsFinal().index(np);
578 if (iPoi == -1)
579 throw std::runtime_error("xRooFitResult::impact: poi not found");
580 if (iNp == -1)
581 throw std::runtime_error("xRooFitResult::impact: np not found");
583 dynamic_cast<RooRealVar *>((prefit ? get()->floatParsInit() : get()->floatParsFinal()).find(np));
584 return get()->covarianceMatrix()(iPoi, iNp) / (up ? npVar->getErrorHi() : npVar->getErrorLo());
585 }
586 return std::numeric_limits<double>::quiet_NaN();
587}
588
589double xRooNLLVar::xRooFitResult::conditionalError(const char *poi, const char *nps, bool up, bool covApprox)
590{
591 // run a fit with given NPs held constant, return quadrature difference
592
594 RooArgList vars;
595 RooAbsArg *poiVar = nullptr;
596 for (auto p : get()->floatParsFinal()) {
597 if (strcmp(p->GetName(), poi) == 0) {
598 vars.add(*p);
599 poiVar = p;
600 continue;
601 }
602 TStringToken pattern(nps, ",");
603 bool matches = false;
604 while (pattern.NextToken()) {
605 TString s(pattern);
606 if ((p->getStringAttribute("group") && s == p->getStringAttribute("group")) ||
607 TString(p->GetName()).Contains(TRegexp(s, true)) || p->getAttribute(s)) {
608 matches = true;
609 break;
610 }
611 }
612 if (matches) {
613 if (npNames.Length())
614 npNames += ",";
615 npNames += p->GetName();
616 } else {
617 vars.add(*p); // keeping in reduced cov matrix
618 }
619 }
620 if (!poiVar) {
621 throw std::runtime_error(TString::Format("Could not find poi: %s", poi));
622 }
623 if (npNames == "") {
624 fNode->Warning("conditionalError", "No parameters selected by: %s", nps);
625 return (up) ? static_cast<RooRealVar *>(poiVar)->getErrorHi() : static_cast<RooRealVar *>(poiVar)->getErrorLo();
626 }
627
628 if (covApprox) {
629 int idx = vars.index(poi);
630 return sqrt(get()->conditionalCovarianceMatrix(vars)(idx, idx));
631 }
632
633 auto _cfit = cfit(npNames.Data(), nps);
634
635 auto _poi = _cfit->floatParsFinal().find(poi);
636
637 return (up) ? static_cast<RooRealVar *>(_poi)->getErrorHi() : static_cast<RooRealVar *>(_poi)->getErrorLo();
638}
639
641{
642
643 RooRealVar *poiHat = dynamic_cast<RooRealVar *>((get()->floatParsFinal()).find(poi));
644 if (!poiHat)
645 throw std::runtime_error("xRooFitResult::ranknp: poi not found");
646
647 std::vector<std::pair<std::string, double>> ranks;
648 // first do with the covariance approximation, since that's always available
649 for (auto par : get()->floatParsFinal()) {
650 if (par == poiHat)
651 continue;
652 ranks.emplace_back(std::pair(par->GetName(), impact(poi, par->GetName(), up, prefit, true)));
653 }
654
655 std::sort(ranks.begin(), ranks.end(), [](auto &left, auto &right) {
656 if (std::isnan(left.second) && !std::isnan(right.second))
657 return false;
658 if (!std::isnan(left.second) && std::isnan(right.second))
659 return true;
660 return fabs(left.second) > fabs(right.second);
661 });
662
663 // now redo the ones above the threshold
664 for (auto &[n, v] : ranks) {
665 if (v >= approxThreshold) {
666 try {
667 v = impact(poi, n.c_str(), up, prefit);
668 } catch (...) {
669 v = std::numeric_limits<double>::quiet_NaN();
670 };
671 }
672 }
673
674 // resort
675 std::sort(ranks.begin(), ranks.end(), [](auto &left, auto &right) {
676 if (std::isnan(left.second) && !std::isnan(right.second))
677 return false;
678 if (!std::isnan(left.second) && std::isnan(right.second))
679 return true;
680 return fabs(left.second) > fabs(right.second);
681 });
682
683 RooArgList out;
684 out.setName("rankings");
685 for (auto &[n, v] : ranks) {
686 out.addClone(*get()->floatParsFinal().find(n.c_str()));
687 auto vv = static_cast<RooRealVar *>(out.at(out.size() - 1));
688 vv->setVal(v);
689 vv->removeError();
690 vv->removeRange();
691 }
692 return out;
693}
694
695xRooNLLVar::xRooFitResult xRooNLLVar::minimize(const std::shared_ptr<ROOT::Fit::FitConfig> &_config)
696{
697 auto &nll = *get();
698 auto out = xRooFit::minimize(nll, (_config) ? _config : fitConfig(), fOpts);
699 // add any pars that are const here that aren't in constPars list because they may have been
700 // const-optimized and their values cached with the dataset, so if subsequently floated the
701 // nll wont evaluate correctly
702 // fConstVars.reset( fFuncVars->selectByAttrib("Constant",true) );
703
704 // before returning, flag which of the constPars were actually global observables
705 if (out) {
706 const_cast<RooArgList &>(out->constPars()).setAttribAll("global", false);
707 if (fGlobs)
708 std::unique_ptr<RooAbsCollection>(out->constPars().selectCommon(*fGlobs))->setAttribAll("global", true);
709 }
710
711 if (fOpts->find("GoF")) {
712 // add pgof to the fit result
713 const_cast<RooArgList &>(out->constPars()).addClone(RooRealVar(".pgof", "GoF p-value", pgof()));
714 }
715
716 return xRooFitResult(std::make_shared<xRooNode>(out, fPdf), std::make_shared<xRooNLLVar>(*this));
717}
718
719std::shared_ptr<ROOT::Fit::FitConfig> xRooNLLVar::fitConfig()
720{
721 if (!fFitConfig)
723 return fFitConfig;
724}
725
727{
728 if (auto conf = fitConfig(); conf)
729 return const_cast<ROOT::Math::IOptions *>(conf->MinimizerOptions().ExtraOptions());
730 return nullptr;
731}
732
733double xRooNLLVar::getEntryVal(size_t entry) const
734{
735 auto _data = data();
736 if (!_data)
737 return 0;
738 if (size_t(_data->numEntries()) <= entry)
739 return 0;
740 auto _pdf = pdf();
741 *std::unique_ptr<RooAbsCollection>(_pdf->getObservables(_data)) = *_data->get(entry);
742 // if (auto s = dynamic_cast<RooSimultaneous*>(_pdf.get());s) return
743 // -_data->weight()*s->getPdf(s->indexCat().getLabel())->getLogVal(_data->get());
744 return -_data->weight() * _pdf->getLogVal(_data->get());
745}
746
747std::set<std::string> xRooNLLVar::binnedChannels() const
748{
749 std::set<std::string> out;
750
751 auto binnedOpt = dynamic_cast<RooCmdArg *>(fOpts->find("Binned")); // the binned option, if explicitly specified
752
753 if (auto s = dynamic_cast<RooSimultaneous *>(pdf().get())) {
754 xRooNode simPdf(*s);
755 bool allChannels = true;
756 for (auto c : simPdf.bins()) {
757 // see if there's a RooRealSumPdf in the channel - if there is, if it has BinnedLikelihood set
758 // then assume is a BinnedLikelihood channel
759 RooArgSet nodes;
760 c->get<RooAbsArg>()->treeNodeServerList(&nodes, nullptr, true, false);
761 bool isBinned = false;
762 for (auto a : nodes) {
763 if (a->InheritsFrom("RooRealSumPdf") &&
764 ((binnedOpt && binnedOpt->getInt(0)) || (!binnedOpt && a->getAttribute("BinnedLikelihood")))) {
765 TString chanName(c->GetName());
766 out.insert(chanName(chanName.Index("=") + 1, chanName.Length()).Data());
767 isBinned = true;
768 break;
769 }
770 }
771 if (!isBinned) {
772 allChannels = false;
773 }
774 }
775 if (allChannels) {
776 out.clear();
777 out.insert("*");
778 }
779 } else {
780 RooArgSet nodes;
781 pdf()->treeNodeServerList(&nodes, nullptr, true, false);
782 for (auto a : nodes) {
783 if (a->InheritsFrom("RooRealSumPdf") &&
784 ((binnedOpt && binnedOpt->getInt(0)) || (!binnedOpt && a->getAttribute("BinnedLikelihood")))) {
785 out.insert("*");
786 break;
787 }
788 }
789 }
790 return out;
791}
792
794{
795
796 auto _data = data();
797 if (!_data)
798 return 0;
799 if (size_t(_data->numEntries()) <= entry)
800 return 0;
801 auto _pdf = pdf().get();
802 std::unique_ptr<RooAbsCollection> _robs(_pdf->getObservables(_data->get()));
803 *_robs = *_data->get(entry); // only set robs
804 if (auto s = dynamic_cast<RooSimultaneous *>(_pdf); s) {
805 _pdf = s->getPdf(s->indexCat().getCurrentLabel());
806 }
807 double volume = 1.;
808 for (auto o : *_robs) {
809
810 if (auto a = dynamic_cast<RooAbsRealLValue *>(o);
811 a && _pdf->dependsOn(*a)) { // dependsOn check needed until ParamHistFunc binBoundaries method fixed
812 std::unique_ptr<std::list<double>> bins(
813 _pdf->binBoundaries(*a, -std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity()));
814 if (bins) {
815 double lowEdge = -std::numeric_limits<double>::infinity();
816 for (auto b : *bins) {
817 if (b > a->getVal()) {
818 volume *= (b - lowEdge);
819 break;
820 }
821 lowEdge = b;
822 }
823 }
824 }
825 }
826
827 return volume;
828}
829
831{
832 // for each global observable in the dataset, determine which constraint term is associated to it
833 // and given its type, add the necessary saturated term...
834
835 double out = 0;
836
837 if (!fGlobs)
838 return 0;
839
840 auto cTerm = constraintTerm();
841 if (!cTerm)
842 return 0;
843
844 for (auto c : cTerm->list()) {
845 if (std::string(c->ClassName()) == "RooAbsPdf" ||
846 std::string(c->ClassName()).find("RooNormalizedPdf") != std::string::npos) {
847 // in ROOT 6.32 the constraintTerm is full of RooNormalizedPdfs which aren't public
848 // became public in 6.34, hence now also check for RooNormalizedPdf explicitly
849 // in this case use the first server
850 c = c->servers()[0];
851 }
852 if (auto gaus = dynamic_cast<RooGaussian *>(c)) {
853 auto v = dynamic_cast<RooAbsReal *>(fGlobs->find(gaus->getX().GetName()));
854 if (!v) {
855 v = dynamic_cast<RooAbsReal *>(fGlobs->find(
856 gaus->getMean().GetName())); // shouldn't really happen but does for at least ws made by pyhf
857 }
858 if (!v)
859 continue;
860 out -= std::log(ROOT::Math::gaussian_pdf(v->getVal(), gaus->getSigma().getVal(), v->getVal()));
861 } else if (auto pois = dynamic_cast<RooPoisson *>(c)) {
862 auto v = dynamic_cast<RooAbsReal *>(fGlobs->find(pois->getX().GetName()));
863 if (!v)
864 continue;
865 out -= std::log(TMath::Poisson(v->getVal(), v->getVal()));
866 }
867 }
868
869 return out;
870}
871
872double xRooNLLVar::ndof() const
873{
874 return data()->numEntries() + (fFuncGlobs ? fFuncGlobs->size() : 0) -
875 std::unique_ptr<RooAbsCollection>(pars()->selectByAttrib("Constant", false))->size();
876}
877
878double xRooNLLVar::pgof() const
879{
880 // note that if evaluating this for a single channel, until 6.30 is available if you are using Binned mode the pdf
881 // will need to be part of a Simultaneous
882 return TMath::Prob(2. * (get()->getVal() - saturatedVal()), ndof());
883}
884
886{
887 // need to count number of floating unconstrained parameters
888 // which are floating parameters not featured in the constraintTerm
889 std::unique_ptr<RooAbsCollection> _floats(pars()->selectByAttrib("Constant", false));
890 if (auto _constraintTerm = constraintTerm()) {
891 _floats->remove(*std::unique_ptr<RooAbsCollection>(_constraintTerm->getVariables()));
892 }
893 return data()->numEntries() - _floats->size();
894}
895
897{
898 // using totVal - constraintTerm while new evalbackend causes mainTerm() to return nullptr
899 return get()->getVal() - constraintTermVal();
900}
901
903{
904 if (auto _constraintTerm = constraintTerm()) {
905 return _constraintTerm->getVal();
906 }
907 return 0;
908}
909
911{
913}
914
919
921{
922
923 // Use this term to create a goodness-of-fit metric, which is approx chi2 distributed with numEntries (data) d.o.f:
924 // prob = TMath::Prob( 2.*(nll.mainTerm()->getVal() - nll.saturatedNllTerm()), nll.data()->numEntries() )
925
926 // note that need to construct nll with explicit Binned(1 or 0) option otherwise will pick up nll eval
927 // from attributes in model already, so many get binned mainTerm eval when thinking not binned because didnt specify
928 // Binned(1)
929
930 auto _data = data();
931 if (!_data)
932 return std::numeric_limits<double>::quiet_NaN();
933
934 std::set<std::string> _binnedChannels = binnedChannels();
935
936 // for binned case each entry is: -(-N + Nlog(N) - TMath::LnGamma(N+1))
937 // for unbinned case each entry is: -(N*log(N/(sumN*binW))) = -N*logN + N*log(sumN) + N*log(binW)
938 // but unbinned gets extendedTerm = sumN - sumN*log(sumN)
939 // so resulting sum is just sumN - sum[ N*logN - N*log(binW) ]
940 // which is the same as the binned case without the LnGamma part and with the extra sum[N*log(binW)] part
941
942 const RooAbsCategoryLValue *cat = (dynamic_cast<RooSimultaneous *>(pdf().get()))
943 ? &dynamic_cast<RooSimultaneous *>(pdf().get())->indexCat()
944 : nullptr;
945
946 double out = _data->sumEntries();
947 for (int i = 0; i < _data->numEntries(); i++) {
948 _data->get(i);
949 double w = _data->weight();
950 if (w == 0)
951 continue;
952 out -= w * std::log(w);
953 if (_binnedChannels.count("*")) {
954 out += TMath::LnGamma(w + 1);
955 } else if (_binnedChannels.empty()) {
956 out += w * std::log(getEntryBinWidth(i));
957 } else if (cat) {
958 // need to determine which channel we are in for this entry to decide if binned or unbinned active
959 if (_binnedChannels.count(_data->get()->getCatLabel(cat->GetName()))) {
960 out += TMath::LnGamma(w + 1);
961 } else {
962 out += w * std::log(getEntryBinWidth(i));
963 }
964 } else {
965 throw std::runtime_error("Cannot determine category of RooSimultaneous pdf");
966 }
967 }
968
969 out += simTermVal();
970
971 return out;
972}
973
974std::shared_ptr<RooArgSet> xRooNLLVar::pars(bool stripGlobalObs) const
975{
976 auto out = std::shared_ptr<RooArgSet>(get()->getVariables());
977 if (stripGlobalObs && fGlobs) {
978 out->remove(*fGlobs, true, true);
979 }
980 return out;
981}
982
983TObject *
984xRooNLLVar::Scan(const char *scanPars, const std::vector<std::vector<double>> &coords, const RooArgList &profilePars)
985{
986 return Scan(*std::unique_ptr<RooAbsCollection>(get()->getVariables()->selectByName(scanPars)), coords, profilePars);
987}
988
989TObject *xRooNLLVar::Scan(const RooArgList &scanPars, const std::vector<std::vector<double>> &coords,
990 const RooArgList &profilePars)
991{
992
993 if (scanPars.size() > 2 || scanPars.empty())
994 return nullptr;
995
996 TGraph2D *out2d = (scanPars.size() == 2) ? new TGraph2D() : nullptr;
997 TGraph *out1d = (out2d) ? nullptr : new TGraph();
998 TNamed *out = (out2d) ? static_cast<TNamed *>(out2d) : static_cast<TNamed *>(out1d);
999 out->SetName(get()->GetName());
1000 out->SetTitle(TString::Format("%s;%s%s%s", get()->GetTitle(), scanPars.first()->GetTitle(), out2d ? ";" : "",
1001 out2d ? scanPars.at(1)->GetTitle() : ""));
1002
1003 std::unique_ptr<RooAbsCollection> funcVars(get()->getVariables());
1005
1006 for (auto &coord : coords) {
1007 if (coord.size() != scanPars.size()) {
1008 throw std::runtime_error("Invalid coordinate");
1009 }
1010 for (size_t i = 0; i < coord.size(); i++) {
1011 static_cast<RooAbsRealLValue &>(scanPars[i]).setVal(coord[i]);
1012 }
1013
1014 if (profilePars.empty()) {
1015 // just evaluate
1016 if (out2d) {
1017 out2d->SetPoint(out2d->GetN(), coord[0], coord[1], get()->getVal());
1018 } else {
1019 out1d->SetPoint(out1d->GetN(), coord[0], get()->getVal());
1020 }
1021 }
1022 }
1023
1024 return out;
1025}
1026
1028{
1029 TString sOpt(opt);
1030
1031 auto _pars = pars();
1032
1033 if (sOpt == "sensitivity") {
1034
1035 // will make a plot of DeltaNLL
1036 }
1037
1038 if (sOpt == "floating") {
1039 // start scanning floating pars
1040 auto floats = std::unique_ptr<RooAbsCollection>(_pars->selectByAttrib("Constant", false));
1041 TVirtualPad *pad = gPad;
1042 if (!pad) {
1044 pad = gPad;
1045 }
1046 TMultiGraph *gr = new TMultiGraph;
1047 gr->SetName("multigraph");
1048 gr->SetTitle(TString::Format("%s;Normalized Parameter Value;#Delta NLL", get()->GetTitle()));
1049 /*((TPad*)pad)->DivideSquare(floats->size());
1050 int i=0;
1051 for(auto a : *floats) {
1052 i++;
1053 pad->cd(i);
1054 Draw(a->GetName());
1055 }*/
1056 return;
1057 }
1058
1059 RooArgList vars;
1060 TStringToken pattern(sOpt, ":");
1061 while (pattern.NextToken()) {
1062 TString s(pattern);
1063 if (auto a = _pars->find(s); a)
1064 vars.add(*a);
1065 }
1066
1067 if (vars.size() == 1) {
1068 TGraph *out = new TGraph;
1069 out->SetBit(kCanDelete);
1070 TGraph *bad = new TGraph;
1071 bad->SetBit(kCanDelete);
1072 bad->SetMarkerColor(kRed);
1073 bad->SetMarkerStyle(5);
1074 TMultiGraph *gr = (gPad) ? dynamic_cast<TMultiGraph *>(gPad->GetPrimitive("multigraph")) : nullptr;
1075 bool normRange = false;
1076 if (!gr) {
1077 gr = new TMultiGraph;
1078 gr->Add(out, "LP");
1080 } else {
1081 normRange = true;
1082 }
1083 out->SetName(get()->GetName());
1084 gr->SetTitle(TString::Format("%s;%s;#Delta NLL", get()->GetTitle(), vars.at(0)->GetTitle()));
1085 // scan outwards from current value towards limits
1086 auto v = dynamic_cast<RooRealVar *>(vars.at(0));
1087 double low = v->getVal();
1088 double high = low;
1089 double step = (v->getMax() - v->getMin()) / 100;
1090 double init = v->getVal();
1091 double initVal = func()->getVal();
1092 // double xscale = (normRange) ? (2.*(v->getMax() - v->getMin())) : 1.;
1093 auto currTime = std::chrono::steady_clock::now();
1094 while (out->GetN() < 100 && (low > v->getMin() || high < v->getMax())) {
1095 if (out->GetN() == 0) {
1096 out->SetPoint(out->GetN(), low, 0);
1097 low -= step;
1098 high += step;
1099 if (!normRange) {
1100 gr->Draw("A");
1101 gPad->SetGrid();
1102 }
1103 continue;
1104 }
1105 if (low > v->getMin()) {
1106 v->setVal(low);
1107 auto _v = func()->getVal();
1108 if (std::isnan(_v) || std::isinf(_v)) {
1109 if (bad->GetN() == 0)
1110 gr->Add(bad, "P");
1111 bad->SetPoint(bad->GetN(), low, out->GetPointY(0));
1112 } else {
1113 out->SetPoint(out->GetN(), low, _v - initVal);
1114 }
1115 low -= step;
1116 }
1117 if (high < v->getMax()) {
1118 v->setVal(high);
1119 auto _v = func()->getVal();
1120 if (std::isnan(_v) || std::isinf(_v)) {
1121 if (bad->GetN() == 0)
1122 gr->Add(bad, "P");
1123 bad->SetPoint(bad->GetN(), high, out->GetPointY(0));
1124 } else {
1125 out->SetPoint(out->GetN(), high, _v - initVal);
1126 }
1127 high += step;
1128 }
1129 out->Sort();
1130 // should only do processEvents once every second in case using x11 (which is slow)
1131 gPad->Modified();
1132 if (std::chrono::steady_clock::now() - currTime > std::chrono::seconds(1)) {
1133 currTime = std::chrono::steady_clock::now();
1134 gPad->Update();
1136 }
1137 }
1138 // add arrow to show current par value
1139 TArrow a;
1140 a.DrawArrow(init, 0, init, -0.1);
1141 gPad->Update();
1142#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 30, 00)
1143 gPad->GetCanvas()->ResetUpdated(); // stops previous canvas being replaced in a jupyter notebook
1144#endif
1146 v->setVal(init);
1147 } else {
1148 Error("Draw", "Name a parameter to scan over: Draw(<name>) , choose from: %s",
1149 _pars->empty() ? "" : _pars->contentsString().c_str());
1150 }
1151}
1152
1153std::pair<std::shared_ptr<RooAbsData>, std::shared_ptr<const RooAbsCollection>> xRooNLLVar::getData() const
1154{
1155 return std::make_pair(fData, fGlobs);
1156}
1157
1159{
1160 if (data.fComp && !data.get<RooAbsData>()) {
1161 return false;
1162 }
1163 return setData(std::dynamic_pointer_cast<RooAbsData>(data.fComp),
1164 std::shared_ptr<const RooAbsCollection>(data.globs().argList().snapshot()));
1165}
1166
1167bool xRooNLLVar::setData(const std::pair<std::shared_ptr<RooAbsData>, std::shared_ptr<const RooAbsCollection>> &_data)
1168{
1169
1170 if (fData == _data.first && fGlobs == _data.second)
1171 return true;
1172
1173 auto _globs = fGlobs; // done to keep globs alive while NLL might still be alive.
1174
1175 auto _dglobs = (_data.second) ? _data.second
1176 : std::shared_ptr<const RooAbsCollection>(_data.first->getGlobalObservables(),
1177 [](const RooAbsCollection *) {});
1178
1179 if (fGlobs && !(fGlobs->empty() && !_dglobs) && _data.first &&
1180 fGlobs != _dglobs) { // second condition allows for no globs being a nullptr, third allow globs to remain if
1181 // nullifying data
1182 if (!_dglobs)
1183 throw std::runtime_error("Missing globs");
1184 // ignore 'extra' globs
1185 RooArgSet s;
1186 s.add(*fGlobs);
1187 std::unique_ptr<RooAbsCollection> _actualGlobs(fPdf->getObservables(s));
1188 RooArgSet s2;
1189 s2.add(*_dglobs);
1190 std::unique_ptr<RooAbsCollection> _actualGlobs2(fPdf->getObservables(s2));
1191 if (!_actualGlobs->equals(*_actualGlobs2)) {
1192 RooArgSet rC;
1193 rC.add(*_actualGlobs2);
1194 rC.remove(*std::unique_ptr<RooAbsCollection>(rC.selectCommon(*_actualGlobs)));
1195 TString r = (!rC.empty()) ? rC.contentsString() : "";
1196 RooArgSet lC;
1197 lC.add(*_actualGlobs);
1198 lC.remove(*std::unique_ptr<RooAbsCollection>(lC.selectCommon(*_actualGlobs2)));
1199 TString l = (!lC.empty()) ? lC.contentsString() : "";
1200 throw std::runtime_error(TString::Format("globs mismatch: adding %s removing %s", r.Data(), l.Data()));
1201 }
1202 fGlobs = _dglobs;
1203 }
1204
1205 if (!std::shared_ptr<RooAbsReal>::get()) {
1206 fData = _data.first;
1207 return true; // not loaded yet so nothing to do
1208 }
1209
1210 try {
1211 if (!kReuseNLL /*|| !mainTerm()*/
1212 /*|| mainTerm()->operMode() == RooAbsTestStatistic::MPMaster*/) { // lost access to RooAbsTestStatistic
1213 // in 6.34, but MP-mode will still throw
1214 // exception, so we will still catch it
1215 // happens when using MP need to rebuild the nll instead
1216 // also happens if there's no mainTerm(), which is the case in 6.32 where RooNLLVar is partially deprecated
1218 // ensure the const state is back where it was at nll construction time;
1219 fFuncVars->setAttribAll("Constant", false);
1220 fConstVars->setAttribAll("Constant", true);
1221 std::shared_ptr<RooAbsData> __data = fData; // do this just to keep fData alive while killing previous NLLVar
1222 // (can't kill data while NLL constructed with it)
1223 fData = _data.first;
1224 reinitialize();
1225 return true;
1226 }
1227 bool out = false;
1228 if (_data.first) {
1229 // replace in all terms
1230 out = get()->setData(*_data.first, false /* clone data */);
1231 // get()->setValueDirty();
1232 // if (_data.first->getGlobalObservables()) {
1233 // // replace in all terms
1234 // out = get()->setData(*_data.first, false);
1235 // get()->setValueDirty();
1236 // } else {
1237 // // replace just in mainTerm ... note to self: why not just replace in all like above? should
1238 // test! auto _mainTerm = mainTerm(); out = _mainTerm->setData(*_data.first, false /* clone data?
1239 // */); _mainTerm->setValueDirty();
1240 // }
1241 } else {
1242 reset();
1243 }
1244 fData = _data.first;
1245 return out;
1246 } catch (std::runtime_error &) {
1247 // happens when using MP need to rebuild the nll instead
1248 // also happens if there's no mainTerm(), which is the case in 6.32 where RooNLLVar is partially deprecated
1250 // ensure the const state is back where it was at nll construction time;
1251 fFuncVars->setAttribAll("Constant", false);
1252 fConstVars->setAttribAll("Constant", true);
1253 std::shared_ptr<RooAbsData> __data = fData; // do this just to keep fData alive while killing previous NLLVar
1254 // (can't kill data while NLL constructed with it)
1255 fData = _data.first;
1256 reinitialize();
1257 return true;
1258 }
1259 throw std::runtime_error("Unable to setData");
1260}
1261
1262std::shared_ptr<RooAbsReal> xRooNLLVar::func() const
1263{
1264 if (!(*this)) {
1265 const_cast<xRooNLLVar *>(this)->reinitialize();
1266 } else if (auto f = std::unique_ptr<RooAbsCollection>(fConstVars->selectByAttrib("Constant", false)); !f->empty()) {
1267 // have to reinitialize if const par values have changed - const optimization forces this
1268 // TODO: currently changes to globs also triggers this since the vars includes globs (vars are the non-obs pars)
1269 // std::cout << "Reinitializing because of change of const parameters:" << f->contentsString() << std::endl;
1270 const_cast<xRooNLLVar *>(this)->reinitialize();
1271
1272 // note ... it may be sufficient here to do:
1273 // nll.constOptimizeTestStatistic(RooAbsArg::ConfigChange, constOptimize>1 /* do tracking too if >1 */); //
1274 // trigger a re-evaluate of which nodes to cache-and-track nll.constOptimizeTestStatistic(RooAbsArg::ValueChange,
1275 // constOptimize>1); // update the cache values -- is this needed??
1276 // this forces the optimization to be redone
1277 // for now leave as a reinitialize though, until had a chance to test this properly
1278 }
1279 if (fGlobs && fFuncGlobs) {
1280 *fFuncGlobs = *fGlobs;
1281 fFuncGlobs->setAttribAll("Constant", true);
1282 }
1283 return *this;
1284}
1285
1287{
1288
1289 if (strcmp(opt.GetName(), "Hesse") == 0) {
1290 fitConfig()->SetParabErrors(opt.getInt(0)); // controls hesse
1291 } else if (strcmp(opt.GetName(), "Minos") == 0) {
1292 fitConfig()->SetMinosErrors(opt.getInt(0)); // controls minos
1293 } else if (strcmp(opt.GetName(), "Strategy") == 0) {
1294 fitConfig()->MinimizerOptions().SetStrategy(opt.getInt(0));
1295 } else if (strcmp(opt.GetName(), "StrategySequence") == 0) {
1296 fitConfigOptions()->SetNamedValue("StrategySequence", opt.getString(0));
1297 } else if (strcmp(opt.GetName(), "Tolerance") == 0) {
1298 fitConfig()->MinimizerOptions().SetTolerance(opt.getDouble(0));
1299 } else if (strcmp(opt.GetName(), "MaxCalls") == 0) {
1300 fitConfig()->MinimizerOptions().SetMaxFunctionCalls(opt.getInt(0));
1301 } else if (strcmp(opt.GetName(), "MaxIterations") == 0) {
1302 fitConfig()->MinimizerOptions().SetMaxIterations(opt.getInt(0));
1303 } else if (strcmp(opt.GetName(), "PrintLevel") == 0) {
1304 fitConfig()->MinimizerOptions().SetPrintLevel(opt.getInt(0));
1305 } else {
1306 if (strcmp(opt.GetName(), "Optimize") == 0) {
1307 // this flag will trigger constOptimizeTestStatistic to be called on the nll in createNLL method
1308 // we should ensure that the fitconfig setting is consistent with it ...
1309 fitConfigOptions()->SetValue("OptimizeConst", opt.getInt(0));
1310 }
1311 if (auto prevObject = fOpts->FindObject(opt.GetName()); prevObject) {
1312 // replace previous option
1313 fOpts->Replace(prevObject, opt.Clone(nullptr));
1314 delete prevObject;
1315 } else {
1316 fOpts->Add(opt.Clone(nullptr)); // nullptr needed because accessing Clone via TObject base class puts
1317 // "" instead, so doesnt copy names
1318 }
1319 if (std::shared_ptr<RooAbsReal>::get()) {
1320 reinitialize(); // do this way to keep name of nll if user set
1321 } else {
1322 reset(); // will trigger reinitialize
1323 }
1324 }
1325}
1326
1328{
1329 return fData.get();
1330 /*
1331#if ROOT_VERSION_CODE < ROOT_VERSION(6, 33, 00)
1332 auto _nll = mainTerm();
1333 if (!_nll)
1334 return fData.get();
1335 RooAbsData *out = &static_cast<RooAbsOptTestStatistic*>(_nll)->data();
1336#else
1337 RooAbsData* out = nullptr; // new backends not conducive to having a reference to a RooAbsData in them (they use
1338buffers instead) #endif if (!out) return fData.get(); return out;
1339 */
1340}
1341
1342/*
1343RooAbsReal *xRooNLLVar::mainTerm() const
1344{
1345 return nullptr;
1346 // the main term is the "other term" in a RooAddition alongside a ConstraintSum
1347 // if can't find the ConstraintSum, just return the function
1348
1349 RooAbsArg* _func = func().get();
1350 if(!_func->InheritsFrom("RooAddition")) {
1351 _func = nullptr;
1352 // happens with new 6.32 backend, where the top-level function is an EvaluatorWrapper
1353 for (auto s : func()->servers()) {
1354 if(s->InheritsFrom("RooAddition")) {
1355 _func = s; break;
1356 }
1357 }
1358 if(!_func) {
1359 return func().get();
1360 }
1361 }
1362 std::set<RooAbsArg*> others,constraints;
1363 for (auto s : _func->servers()) {
1364 if(s->InheritsFrom("RooConstraintSum")) {
1365 constraints.insert(s);
1366 } else {
1367 others.insert(s);
1368 }
1369 }
1370 if(constraints.size()==1 && others.size()==1) {
1371 return static_cast<RooAbsReal*>(*others.begin());
1372 }
1373 return nullptr; // failed to find the right term?
1374
1375
1376}
1377 */
1378
1380{
1381 // returns Nexp - Nobs*log(Nexp)
1382 return fPdf->extendedTerm(fData->sumEntries(), fData->get());
1383}
1384
1386{
1387 // comes from the _simCount code inside RooNLLVar
1388 // is this actually only appropriate if the roosimultaneous is not extended?
1389 // i.e. then this term represents the probability the entry belongs to a given state, and given
1390 // all the states are normalized to 1, this probability is assumed to just be 1/N_states
1391 if (auto s = dynamic_cast<RooSimultaneous *>(fPdf.get()); s) {
1392 return fData->sumEntries() * log(1.0 * (s->servers().size() - 1)); // one of the servers is the cat
1393 }
1394 return 0;
1395}
1396
1398{
1399 // this is only relevant if BinnedLikelihood active
1400 // = sum[ N_i! ] since LnGamma(N_i+1) ~= N_i!
1401 // need to also subtract off sum[ N_i*log(width_i) ] in order to have formula: binnedLL = unbinnedLL + binnedDataTerm
1402 // note this is 0 if all the bin widths are 1
1403 double out = 0;
1404 for (int i = 0; i < fData->numEntries(); i++) {
1405 fData->get(i);
1406 out += TMath::LnGamma(fData->weight() + 1) - fData->weight() * std::log(getEntryBinWidth(i));
1407 }
1408
1409 return out;
1410}
1411
1413{
1414 auto _func = func();
1415 if (auto a = dynamic_cast<RooConstraintSum *>(_func.get()); a)
1416 return a;
1417 for (auto s : _func->servers()) {
1418 if (auto a = dynamic_cast<RooConstraintSum *>(s); a)
1419 return a;
1420 // allow one more depth to support 6.32 (where sum is hidden inside the first server)
1421 for (auto s2 : s->servers()) {
1422 if (auto a2 = dynamic_cast<RooConstraintSum *>(s2); a2)
1423 return a2;
1424 }
1425 }
1426 return nullptr;
1427}
1428
1429/*xRooNLLVar::operator RooAbsReal &() const {
1430 // this works in c++ but not in python
1431 std::cout << "implicit conversion" << std::endl;
1432 return *fFunc;
1433}*/
1434
1436{
1438 sWhat.ToLower();
1439 sWhat.ReplaceAll(" =", "=").ReplaceAll("= ", "="); // remove spaces around equals signs
1440 bool doTS = sWhat.Contains("ts");
1441 bool doCLs = sWhat.Contains("pcls");
1442 bool doNull = sWhat.Contains("pnull");
1443 bool doAlt = sWhat.Contains("palt");
1444 double nSigma = (sWhat.Contains("exp"))
1445 ? (TString(sWhat(sWhat.Index("exp") + 3, sWhat.Index(" ", sWhat.Index("exp")) == -1
1446 ? sWhat.Length()
1447 : sWhat.Index(" ", sWhat.Index("exp"))))
1448 .Atof())
1449 : std::numeric_limits<double>::quiet_NaN();
1450
1451 bool toys = sWhat.Contains("toys");
1452
1453 // bool asymp = sWhat.Contains("asymp");
1454
1455 bool readOnly = sWhat.Contains("readonly");
1456
1457 if (!readOnly) {
1458 if (toys) {
1459 sigma_mu(); // means we will be able to evaluate the asymptotic values too
1460 }
1461 // only add toys if actually required
1462 if (getVal(sWhat + " readonly").second != 0) {
1463 if (sWhat.Contains("toys=")) {
1464 // extract number of toys required ... format is "nullToys.altToysFraction" if altToysFraction=0 then use
1465 // same for both, unless explicitly set (i.e. N.0) then means we want no alt toys
1466 // e.g. if doing just pnull significance
1467 TString toyNum = sWhat(sWhat.Index("toys=") + 5, sWhat.Length());
1468 size_t nToys = toyNum.Atoi();
1469 size_t nToysAlt = (toyNum.Atof() - nToys) * nToys;
1470 if (nToysAlt == 0 && !toyNum.Contains('.'))
1471 nToysAlt = nToys;
1472 if (nullToys.size() < nToys) {
1473 addNullToys(nToys - nullToys.size());
1474 }
1475 if (altToys.size() < nToysAlt) {
1476 addAltToys(nToysAlt - altToys.size());
1477 }
1478 } else if (doCLs && toys) {
1479 // auto toy-generating for limits .. do in blocks of 100
1480 addCLsToys(100, 0, 0.05, nSigma);
1481 } else if (toys) {
1482 throw std::runtime_error("Auto-generating toys for anything other than CLs not yet supported, please "
1483 "specify number of toys with 'toys=N' ");
1484 }
1485 }
1486 }
1487
1488 struct RestoreNll {
1489 RestoreNll(std::shared_ptr<xRooNLLVar> &v, bool r) : rr(r), var(v)
1490 {
1491 if (rr && var && var->get()) {
1492 _readOnly = var->get()->getAttribute("readOnly");
1493 var->get()->setAttribute("readOnly", rr);
1494 } else {
1495 rr = false;
1496 }
1497 };
1498 ~RestoreNll()
1499 {
1500 if (rr)
1501 var->get()->setAttribute("readOnly", _readOnly);
1502 };
1503
1504 bool rr = false;
1505 bool _readOnly = false;
1506
1507 std::shared_ptr<xRooNLLVar> &var;
1508 };
1509
1510 RestoreNll rest(nllVar, readOnly);
1511
1512 if (doTS)
1513 return (toys) ? ts_toys(nSigma) : ts_asymp(nSigma);
1514 if (doNull)
1515 return (toys) ? pNull_toys(nSigma) : pNull_asymp(nSigma);
1516 if (doAlt)
1517 return (toys) ? pAlt_toys(nSigma) : pAlt_asymp(nSigma);
1518 if (doCLs)
1519 return (toys) ? pCLs_toys(nSigma) : pCLs_asymp(nSigma);
1520
1521 throw std::runtime_error(std::string("Unknown: ") + what);
1522}
1523
1525{
1526 RooArgList out;
1527 out.setName("poi");
1528 out.add(*std::unique_ptr<RooAbsCollection>(coords->selectByAttrib("poi", true)));
1529 return out;
1530}
1531
1533{
1534 RooArgList out;
1535 out.setName("alt_poi");
1536 out.addClone(*std::unique_ptr<RooAbsCollection>(coords->selectByAttrib("poi", true)));
1537 for (auto a : out) {
1538 auto v = dynamic_cast<RooAbsRealLValue *>(a);
1539 if (!v)
1540 continue;
1541 if (auto s = a->getStringAttribute("altVal"); s && strlen(s)) {
1542 v->setVal(TString(s).Atof());
1543 } else {
1544 v->setVal(std::numeric_limits<double>::quiet_NaN());
1545 }
1546 }
1547 return out;
1548}
1549
1551{
1552 auto &me = const_cast<xRooHypoPoint &>(*this);
1553 int out = 0;
1554 if (me.ufit(true) && !allowedStatusCodes.count(me.ufit(true)->status()))
1555 out += 1;
1556 if (me.cfit_null(true) && !allowedStatusCodes.count(me.cfit_null(true)->status()))
1557 out += 1 << 1;
1558 if (me.cfit_alt(true) && !allowedStatusCodes.count(me.cfit_alt(true)->status()))
1559 out += 1 << 2;
1560 if (me.asimov(true))
1561 out += me.asimov(true)->status() << 3;
1562 return out;
1563}
1564
1566{
1567 auto _poi = const_cast<xRooHypoPoint *>(this)->poi();
1568 auto _alt_poi = const_cast<xRooHypoPoint *>(this)->alt_poi();
1569 std::cout << "POI: " << _poi.contentsString() << " , null: ";
1570 bool first = true;
1571 for (auto a : _poi) {
1572 auto v = dynamic_cast<RooAbsReal *>(a);
1573 if (!a)
1574 continue;
1575 if (!first)
1576 std::cout << ",";
1577 std::cout << v->getVal();
1578 first = false;
1579 }
1580 std::cout << " , alt: ";
1581 first = true;
1582 bool any_alt = false;
1583 for (auto a : _alt_poi) {
1584 auto v = dynamic_cast<RooAbsReal *>(a);
1585 if (!a)
1586 continue;
1587 if (!first)
1588 std::cout << ",";
1589 std::cout << v->getVal();
1590 first = false;
1591 if (!std::isnan(v->getVal()))
1592 any_alt = true;
1593 }
1594 std::cout << " , pllType: " << fPllType << std::endl;
1595
1596 if (fPllType == xRooFit::Asymptotics::Unknown) {
1597 std::cout << " obs ts: " << obs_ts << " +/- " << obs_ts_err << std::endl;
1598 }
1599
1600 std::cout << " - ufit: ";
1601 if (fUfit) {
1602 std::cout << fUfit->GetName() << " " << fUfit->minNll() << " (status=" << fUfit->status() << ") (";
1603 first = true;
1604 for (auto a : _poi) {
1605 auto v = dynamic_cast<RooRealVar *>(fUfit->floatParsFinal().find(a->GetName()));
1606 if (!v)
1607 continue;
1608 if (!first)
1609 std::cout << ",";
1610 std::cout << v->GetName() << "_hat: " << v->getVal() << " +/- " << v->getError();
1611 first = false;
1612 }
1613 std::cout << ")" << std::endl;
1614 } else {
1615 std::cout << "Not calculated" << std::endl;
1616 }
1617 std::cout << " - cfit_null: ";
1618 if (fNull_cfit) {
1619 std::cout << fNull_cfit->GetName() << " " << fNull_cfit->minNll() << " (status=" << fNull_cfit->status() << ")";
1620 } else {
1621 std::cout << "Not calculated";
1622 }
1623 if (any_alt) {
1624 std::cout << std::endl << " - cfit_alt: ";
1625 if (fAlt_cfit) {
1626 std::cout << fAlt_cfit->GetName() << " " << fAlt_cfit->minNll() << " (status=" << fAlt_cfit->status() << ")"
1627 << std::endl;
1628 } else {
1629 std::cout << "Not calculated" << std::endl;
1630 }
1631 std::cout << " sigma_mu: ";
1632 const_cast<xRooHypoPoint *>(this)->asimov(true); // will trigger construction of fAsimov hypoPoint if possible
1633 if (!fAsimov || !fAsimov->fUfit || !fAsimov->fNull_cfit) {
1634 std::cout << "Not calculated";
1635 } else {
1636 std::cout << const_cast<xRooHypoPoint *>(this)->sigma_mu().first << " +/- "
1637 << const_cast<xRooHypoPoint *>(this)->sigma_mu().second;
1638 }
1639 if (fAsimov) {
1640 std::cout << std::endl;
1641 std::cout << " - asimov ufit: ";
1642 if (fAsimov->fUfit) {
1643 std::cout << fAsimov->fUfit->GetName() << " " << fAsimov->fUfit->minNll()
1644 << " (status=" << fAsimov->fUfit->status() << ")";
1645 } else {
1646 std::cout << "Not calculated";
1647 }
1648 std::cout << std::endl << " - asimov cfit_null: ";
1649 if (fAsimov->fNull_cfit) {
1650 std::cout << fAsimov->fNull_cfit->GetName() << " " << fAsimov->fNull_cfit->minNll()
1651 << " (status=" << fAsimov->fNull_cfit->status() << ")";
1652 } else {
1653 std::cout << "Not calculated";
1654 }
1655 }
1656 std::cout << std::endl;
1657 } else {
1658 std::cout << std::endl;
1659 }
1660 if (fLbound_cfit) {
1661 std::cout << " - cfit_lbound: " << fLbound_cfit->GetName() << " " << fLbound_cfit->minNll()
1662 << " (status=" << fLbound_cfit->status() << ")" << std::endl;
1663 }
1664 if (fGenFit)
1665 std::cout << " - gfit: " << fGenFit->GetName() << std::endl;
1666 if (!nullToys.empty() || !altToys.empty()) {
1667 std::cout << " * null toys: " << nullToys.size();
1668 size_t firstToy = 0;
1669 while (firstToy < nullToys.size() && std::isnan(std::get<1>(nullToys[firstToy])))
1670 firstToy++;
1671 if (firstToy > 0)
1672 std::cout << " [ of which " << firstToy << " are bad]";
1673 std::cout << " , alt toys: " << altToys.size();
1674 firstToy = 0;
1675 while (firstToy < altToys.size() && std::isnan(std::get<1>(altToys[firstToy])))
1676 firstToy++;
1677 if (firstToy > 0)
1678 std::cout << " [ of which " << firstToy << " are bad]";
1679 std::cout << std::endl;
1680 }
1681 // std::cout << " nllVar: " << nllVar << std::endl;
1682}
1683
1685{
1686 if (ufit()) {
1687 auto var = dynamic_cast<RooRealVar *>(ufit()->floatParsFinal().find(fPOIName()));
1688 if (var) {
1689 return *var;
1690 } else {
1691 throw std::runtime_error(TString::Format("Cannot find POI: %s", fPOIName()));
1692 }
1693 }
1694 throw std::runtime_error("Unconditional fit unavailable");
1695}
1696
1697std::pair<std::shared_ptr<RooAbsData>, std::shared_ptr<const RooAbsCollection>> xRooNLLVar::xRooHypoPoint::data()
1698{
1699 if (fData.first)
1700 return fData;
1701 if (fGenFit && isExpected) {
1702 // std::cout << "Generating asimov" << std::endl;poi().Print("v");
1703 fData = xRooFit::generateFrom(*nllVar->fPdf, *fGenFit, true);
1704 }
1705 return fData;
1706}
1707
1708xRooNLLVar::xRooHypoPoint::xRooHypoPoint(std::shared_ptr<RooStats::HypoTestResult> htr, const RooAbsCollection *_coords)
1709 : hypoTestResult(htr)
1710{
1711 if (hypoTestResult) {
1712 // load the pllType
1713 fPllType =
1714 xRooFit::Asymptotics::PLLType(hypoTestResult->GetFitInfo()->getGlobalObservables()->getCatIndex("pllType"));
1715 isExpected = hypoTestResult->GetFitInfo()->getGlobalObservables()->getRealValue("isExpected");
1716
1717 // load obsTS value
1718 obs_ts = hypoTestResult->GetTestStatisticData();
1719 obs_ts_err = hypoTestResult->GetFitInfo()->getGlobalObservables()->getRealValue("obs_ts_err");
1720
1721 // load the toys
1722 auto toys = hypoTestResult->GetNullDetailedOutput();
1723 if (toys) {
1724 // load coords from the nullDist globs list
1725 if (toys->getGlobalObservables()) {
1726 coords = std::shared_ptr<RooAbsCollection>(toys->getGlobalObservables()->snapshot());
1727 }
1728 for (int i = 0; i < toys->numEntries(); i++) {
1729 auto toy = toys->get(i);
1730 nullToys.emplace_back(
1731 std::make_tuple(int(toy->getRealValue("seed")), toy->getRealValue("ts"), toys->weight()));
1732 }
1733 }
1734 toys = hypoTestResult->GetAltDetailedOutput();
1735 if (toys) {
1736 for (int i = 0; i < toys->numEntries(); i++) {
1737 auto toy = toys->get(i);
1738 altToys.emplace_back(
1739 std::make_tuple(int(toy->getRealValue("seed")), toy->getRealValue("ts"), toys->weight()));
1740 }
1741 }
1742 }
1743 if (!coords && _coords)
1744 coords.reset(_coords->snapshot());
1745}
1746
1747std::shared_ptr<xRooNLLVar::xRooHypoPoint> xRooNLLVar::xRooHypoPoint::asimov(bool readOnly)
1748{
1749
1750 if (!fAsimov && (nllVar || hypoTestResult)) {
1751 auto theFit = (!fData.first && fGenFit && !isExpected)
1752 ? fGenFit
1753 : cfit_alt(readOnly); // first condition allows genFit to be used as the altFit *if* the data is
1754 // entirely absent, provided not expected data because we postpone data
1755 // creation til later in that case (see below)
1756 if (!theFit || allowedStatusCodes.find(theFit->status()) == allowedStatusCodes.end())
1757 return fAsimov;
1758 fAsimov = std::make_shared<xRooHypoPoint>(*this);
1759 fAsimov->coords.reset(fAsimov->coords->snapshot()); // create a copy so can remove the physical range below
1760 fAsimov->hypoTestResult.reset();
1761 fAsimov->fPllType = xRooFit::Asymptotics::TwoSided;
1762 for (auto p : fAsimov->poi()) {
1763 // dynamic_cast<RooRealVar *>(p)->removeRange("physical"); -- can't use this as will modify shared property
1764 if (auto v = dynamic_cast<RooRealVar *>(p)) {
1765 v->deleteSharedProperties(); // effectively removes all custom ranges
1766 }
1767 }
1768
1769 fAsimov->nullToys.clear();
1770 fAsimov->altToys.clear();
1771 fAsimov->fUfit = retrieveFit(3);
1772 fAsimov->fNull_cfit = retrieveFit(4);
1773 fAsimov->fAlt_cfit.reset();
1774 fAsimov->fData =
1775 std::make_pair(nullptr, nullptr); // postpone generating expected data until we definitely need it
1776 fAsimov->fGenFit = theFit;
1777 fAsimov->isExpected = true;
1778 }
1779
1780 return fAsimov;
1781}
1782
1784{
1785 if (fPllType != xRooFit::Asymptotics::Uncapped && ts_asymp(nSigma).first == 0)
1786 return std::pair<double, double>(1, 0);
1787 auto first_poi = dynamic_cast<RooRealVar *>(poi().first());
1788 if (!first_poi)
1789 return std::pair<double, double>(std::numeric_limits<double>::quiet_NaN(), 0);
1790 auto _sigma_mu = sigma_mu();
1791 double nom = xRooFit::Asymptotics::PValue(fPllType, ts_asymp(nSigma).first, fNullVal(), fNullVal(), _sigma_mu.first,
1792 first_poi->getMin("physical"), first_poi->getMax("physical"));
1793 double up =
1794 xRooFit::Asymptotics::PValue(fPllType, ts_asymp(nSigma).first + ts_asymp(nSigma).second, fNullVal(), fNullVal(),
1795 _sigma_mu.first, first_poi->getMin("physical"), first_poi->getMax("physical"));
1796 double down =
1797 xRooFit::Asymptotics::PValue(fPllType, ts_asymp(nSigma).first - ts_asymp(nSigma).second, fNullVal(), fNullVal(),
1798 _sigma_mu.first, first_poi->getMin("physical"), first_poi->getMax("physical"));
1799 return std::pair(nom, std::max(std::abs(up - nom), std::abs(down - nom)));
1800}
1801
1803{
1804 if (fPllType != xRooFit::Asymptotics::Uncapped && ts_asymp(nSigma).first == 0)
1805 return std::pair<double, double>(1, 0);
1806 auto first_poi = dynamic_cast<RooRealVar *>(poi().first());
1807 if (!first_poi)
1808 return std::pair<double, double>(std::numeric_limits<double>::quiet_NaN(), 0);
1809 auto _sigma_mu = sigma_mu();
1810 double nom = xRooFit::Asymptotics::PValue(fPllType, ts_asymp(nSigma).first, fNullVal(), fAltVal(), _sigma_mu.first,
1811 first_poi->getMin("physical"), first_poi->getMax("physical"));
1812 double up =
1813 xRooFit::Asymptotics::PValue(fPllType, ts_asymp(nSigma).first + ts_asymp(nSigma).second, fNullVal(), fAltVal(),
1814 _sigma_mu.first, first_poi->getMin("physical"), first_poi->getMax("physical"));
1815 double down =
1816 xRooFit::Asymptotics::PValue(fPllType, ts_asymp(nSigma).first - ts_asymp(nSigma).second, fNullVal(), fAltVal(),
1817 _sigma_mu.first, first_poi->getMin("physical"), first_poi->getMax("physical"));
1818
1819 return std::pair(nom, std::max(std::abs(up - nom), std::abs(down - nom)));
1820}
1821
1823{
1824 if (fNullVal() == fAltVal())
1825 return std::pair<double, double>(1, 0); // by construction
1826
1827 if (fPllType != xRooFit::Asymptotics::Uncapped && ts_asymp(nSigma).first == 0)
1828 return std::pair<double, double>(1, 0);
1829 auto first_poi = dynamic_cast<RooRealVar *>(poi().first());
1830 if (!first_poi)
1831 return std::pair<double, double>(std::numeric_limits<double>::quiet_NaN(), 0);
1832
1833 auto _ts_asymp = ts_asymp(nSigma);
1834 auto _sigma_mu = sigma_mu();
1835 double nom1 = xRooFit::Asymptotics::PValue(fPllType, _ts_asymp.first, fNullVal(), fNullVal(), _sigma_mu.first,
1836 first_poi->getMin("physical"), first_poi->getMax("physical"));
1837 double up1 =
1838 xRooFit::Asymptotics::PValue(fPllType, _ts_asymp.first + _ts_asymp.second, fNullVal(), fNullVal(),
1839 _sigma_mu.first, first_poi->getMin("physical"), first_poi->getMax("physical"));
1840 double down1 =
1841 xRooFit::Asymptotics::PValue(fPllType, _ts_asymp.first - _ts_asymp.second, fNullVal(), fNullVal(),
1842 _sigma_mu.first, first_poi->getMin("physical"), first_poi->getMax("physical"));
1843 double nom2 = xRooFit::Asymptotics::PValue(fPllType, _ts_asymp.first, fNullVal(), fAltVal(), _sigma_mu.first,
1844 first_poi->getMin("physical"), first_poi->getMax("physical"));
1845 double up2 =
1846 xRooFit::Asymptotics::PValue(fPllType, _ts_asymp.first + _ts_asymp.second, fNullVal(), fAltVal(), _sigma_mu.first,
1847 first_poi->getMin("physical"), first_poi->getMax("physical"));
1848 double down2 =
1849 xRooFit::Asymptotics::PValue(fPllType, _ts_asymp.first - _ts_asymp.second, fNullVal(), fAltVal(), _sigma_mu.first,
1850 first_poi->getMin("physical"), first_poi->getMax("physical"));
1851
1852 auto nom = (nom1 == 0) ? 0 : nom1 / nom2;
1853 auto up = (up1 == 0) ? 0 : up1 / up2;
1854 auto down = (down1 == 0) ? 0 : down1 / down2;
1855
1856 return std::pair(nom, std::max(std::abs(up - nom), std::abs(down - nom)));
1857}
1858
1860{
1861 if (std::isnan(nSigma)) {
1862 return (fPllType == xRooFit::Asymptotics::Unknown) ? std::make_pair(obs_ts, obs_ts_err) : pll();
1863 }
1864
1865 auto first_poi = dynamic_cast<RooRealVar *>(poi().first());
1866 auto _sigma_mu = sigma_mu();
1867 if (!first_poi || (!std::isnan(nSigma) && std::isnan(_sigma_mu.first)))
1868 return std::pair<double, double>(std::numeric_limits<double>::quiet_NaN(), 0);
1869 double nom = xRooFit::Asymptotics::k(fPllType, ROOT::Math::gaussian_cdf(nSigma), fNullVal(), fAltVal(),
1870 _sigma_mu.first, first_poi->getMin("physical"), first_poi->getMax("physical"));
1871 double up = xRooFit::Asymptotics::k(fPllType, ROOT::Math::gaussian_cdf(nSigma), fNullVal(), fAltVal(),
1872 _sigma_mu.first + _sigma_mu.second, first_poi->getMin("physical"),
1873 first_poi->getMax("physical"));
1874 double down = xRooFit::Asymptotics::k(fPllType, ROOT::Math::gaussian_cdf(nSigma), fNullVal(), fAltVal(),
1875 _sigma_mu.first - _sigma_mu.second, first_poi->getMin("physical"),
1876 first_poi->getMax("physical"));
1877 return std::pair<double, double>(nom, std::max(std::abs(nom - up), std::abs(nom - down)));
1878}
1879
1881{
1882 if (std::isnan(nSigma)) {
1883 return ts_asymp();
1884 }
1885 // nans should appear in the alt toys first ... so loop until past nans
1886 size_t firstToy = 0;
1887 while (firstToy < altToys.size() && std::isnan(std::get<1>(altToys[firstToy])))
1888 firstToy++;
1889 if (firstToy >= altToys.size())
1890 return std::pair(std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN());
1891 int targetIdx =
1892 (altToys.size() - firstToy) * ROOT::Math::gaussian_cdf_c(nSigma) + firstToy; // TODO: Account for weights
1893 return std::pair(std::get<1>(altToys[targetIdx]), (std::get<1>(altToys[std::min(int(altToys.size()), targetIdx)]) -
1894 std::get<1>(altToys[std::max(0, targetIdx)])) /
1895 2.);
1896}
1897
1899{
1900 auto _ufit = ufit(readOnly);
1901 if (!_ufit) {
1902 if (hypoTestResult)
1903 return std::pair<double, double>(hypoTestResult->GetTestStatisticData(), 0);
1904 return std::pair<double, double>(std::numeric_limits<double>::quiet_NaN(), 0);
1905 }
1906 if (allowedStatusCodes.find(_ufit->status()) == allowedStatusCodes.end()) {
1907 return std::pair<double, double>(std::numeric_limits<double>::quiet_NaN(), 0);
1908 }
1909 if (auto _first_poi = dynamic_cast<RooRealVar *>(poi().first());
1910 _first_poi && _first_poi->getMin("physical") > _first_poi->getMin() &&
1911 mu_hat().getVal() < _first_poi->getMin("physical")) {
1912 // replace _ufit with fit "boundary" conditional fit
1913 _ufit = cfit_lbound(readOnly);
1914 if (!_ufit) {
1915 return std::pair<double, double>(std::numeric_limits<double>::quiet_NaN(), 0);
1916 }
1917 }
1918 auto cFactor = (fPllType == xRooFit::Asymptotics::TwoSided)
1919 ? 1.
1920 : xRooFit::Asymptotics::CompatFactor(fPllType, fNullVal(), mu_hat().getVal());
1921 if (cFactor == 0)
1922 return std::pair<double, double>(0, 0);
1923 auto _cfit_null = cfit_null(readOnly);
1924 if (!_cfit_null || allowedStatusCodes.find(_cfit_null->status()) == allowedStatusCodes.end())
1925 return std::pair<double, double>(std::numeric_limits<double>::quiet_NaN(), 0);
1926 // std::cout << cfit->minNll() << ":" << cfit->edm() << " " << ufit->minNll() << ":" << ufit->edm() << std::endl;
1927 double diff = _cfit_null->minNll() - _ufit->minNll();
1928 if (diff < 0) {
1929 // use edm to attempt a small correction to the diff, i.e. assume edm is an additional correction required to
1930 // minNll
1931 diff += (_ufit->edm() - _cfit_null->edm());
1932 }
1933 return std::pair<double, double>(2. * cFactor * diff,
1934 2. * cFactor * sqrt(pow(cfit_null(readOnly)->edm(), 2) + pow(_ufit->edm(), 2)));
1935 // return 2.*cFactor*(cfit->minNll()+cfit->edm() - ufit->minNll()+ufit->edm());
1936}
1937
1938std::shared_ptr<const RooFitResult> xRooNLLVar::xRooHypoPoint::retrieveFit(int type)
1939{
1940 if (!hypoTestResult)
1941 return nullptr;
1942 // see if can retrieve from that ....
1943 if (auto fits = hypoTestResult->GetFitInfo()) {
1944 for (int i = 0; i < fits->numEntries(); i++) {
1945 auto fit = fits->get(i);
1946 if (fit->getCatIndex("type") != type)
1947 continue;
1948 // found ufit ... construct
1949 std::string _name =
1950 fits->getGlobalObservables()->getStringValue(TString::Format("%s.name", fit->getCatLabel("type")));
1951 // see if can retrieve from any open file ....
1952 TDirectory *tmp = gDirectory;
1953 for (auto file : *gROOT->GetListOfFiles()) {
1954 if (auto k = static_cast<TDirectory *>(file)->FindKeyAny(_name.c_str())) {
1955 // use pre-retrieved fits if available
1957 k->GetMotherDir()->GetList()
1958 ? dynamic_cast<xRooFit::StoredFitResult *>(k->GetMotherDir()->GetList()->FindObject(k->GetName()))
1959 : nullptr;
1960 if (auto cachedFit = (storedFr) ? storedFr->fr.get() : k->ReadObject<RooFitResult>(); cachedFit) {
1961 if (!storedFr) {
1963 k->GetMotherDir()->Add(storedFr);
1964 }
1965 gDirectory = tmp; // one of the above calls moves to key's directory ... i didn't check which
1966 return storedFr->fr;
1967 }
1968 }
1969 }
1970 auto rfit = std::make_shared<RooFitResult>(_name.c_str(), TUUID(_name.c_str()).GetTime().AsString());
1971 rfit->setStatus(fit->getRealValue("status"));
1972 rfit->setMinNLL(fit->getRealValue("minNll"));
1973 rfit->setEDM(fit->getRealValue("edm"));
1974 if (type == 0) {
1975 std::unique_ptr<RooAbsCollection> par_hats(
1976 hypoTestResult->GetFitInfo()->getGlobalObservables()->selectByName(coords->contentsString().c_str()));
1977 par_hats->setName("floatParsFinal");
1978 rfit->setFinalParList(*par_hats);
1979 } else {
1980 rfit->setFinalParList(RooArgList());
1981 }
1982 rfit->setConstParList(RooArgList());
1983 rfit->setInitParList(RooArgList());
1984 TMatrixDSym cov(0);
1985 rfit->setCovarianceMatrix(cov);
1986 rfit->setCovQual(fit->getRealValue("covQual"));
1987 return rfit;
1988 }
1989 }
1990 return nullptr;
1991}
1992
1993std::shared_ptr<const RooFitResult> xRooNLLVar::xRooHypoPoint::ufit(bool readOnly)
1994{
1995 if (fUfit)
1996 return fUfit;
1997 if (auto rfit = retrieveFit(0)) {
1998 return fUfit = rfit;
1999 }
2000 if (!nllVar || (readOnly && nllVar->get() && !nllVar->get()->getAttribute("readOnly")))
2001 return nullptr;
2002 if (!nllVar->fFuncVars)
2003 nllVar->reinitialize();
2004 AutoRestorer snap(*nllVar->fFuncVars, nllVar.get());
2005 if (!fData.first) {
2006 if (!readOnly && isExpected && fGenFit) {
2007 // can try to do a readOnly in case can load from cache
2008 bool tmp = nllVar->get()->getAttribute("readOnly");
2009 nllVar->get()->setAttribute("readOnly");
2010 auto out = ufit(true);
2011 nllVar->get()->setAttribute("readOnly", tmp);
2012 if (out) {
2013 // retrieve from cache worked, no need to generate dataset
2014 return out;
2015 } else if (!tmp) { // don't need to setData if doing a readOnly fit
2016 nllVar->setData(data());
2017 }
2018 }
2019 } else if (!nllVar->get()->getAttribute("readOnly")) { // don't need to setData if doing a readOnly fit
2020 nllVar->setData(fData);
2021 }
2022 nllVar->fFuncVars->setAttribAll("Constant", false);
2023 *nllVar->fFuncVars = *coords; // will reconst the coords
2024 if (nllVar->fFuncGlobs)
2025 nllVar->fFuncGlobs->setAttribAll("Constant", true);
2026 std::unique_ptr<RooAbsCollection>(nllVar->fFuncVars->selectCommon(poi()))
2027 ->setAttribAll("Constant", false); // float the poi
2028 if (fGenFit) {
2029 // make initial guess same as pars we generated with
2030 nllVar->fFuncVars->assignValueOnly(fGenFit->constPars());
2031 nllVar->fFuncVars->assignValueOnly(fGenFit->floatParsFinal());
2032 // rename nll so if caching fit results will cache into subdir
2033 nllVar->get()->SetName(
2034 TString::Format("%s/%s_%s", nllVar->get()->GetName(), fGenFit->GetName(), (isExpected) ? "asimov" : "toys"));
2035 if (!isExpected)
2036 nllVar->get()->SetName(TString::Format("%s/%s", nllVar->get()->GetName(), fData.first->GetName()));
2037
2038 } else if (!std::isnan(fAltVal())) {
2039 // guess data given is expected to align with alt value, unless initVal attribute specified
2040 for (auto _poiCoord : poi()) {
2041 auto _poi = dynamic_cast<RooRealVar *>(nllVar->fFuncVars->find(_poiCoord->GetName()));
2042 if (_poi) {
2043 _poi->setVal(_poi->getStringAttribute("initVal") ? TString(_poi->getStringAttribute("initVal")).Atof()
2044 : fAltVal());
2045 }
2046 }
2047 }
2048 return (fUfit = nllVar->minimize());
2049}
2050
2052{
2053 std::string out;
2054 for (auto &c : coll) {
2055 if (!out.empty())
2056 out += ",";
2057 out += c->GetName();
2058 if (auto v = dynamic_cast<RooAbsReal *>(c); v) {
2059 out += TString::Format("=%g", v->getVal());
2060 } else if (auto cc = dynamic_cast<RooAbsCategory *>(c); cc) {
2061 out += TString::Format("=%s", cc->getLabel());
2062 } else if (auto s = dynamic_cast<RooStringVar *>(c); v) {
2063 out += TString::Format("=%s", s->getVal());
2064 }
2065 }
2066 return out;
2067}
2068
2069std::shared_ptr<const RooFitResult> xRooNLLVar::xRooHypoPoint::cfit_null(bool readOnly)
2070{
2071 if (fNull_cfit)
2072 return fNull_cfit;
2073 if (auto rfit = retrieveFit(1)) {
2074 return fNull_cfit = rfit;
2075 }
2076 if (!nllVar || (readOnly && nllVar->get() && !nllVar->get()->getAttribute("readOnly")))
2077 return nullptr;
2078 if (!nllVar->fFuncVars)
2079 nllVar->reinitialize();
2080 AutoRestorer snap(*nllVar->fFuncVars, nllVar.get());
2081 if (!fData.first) {
2082 if (!readOnly && isExpected && fGenFit) {
2083 // can try to do a readOnly in case can load from cache
2084 bool tmp = nllVar->get()->getAttribute("readOnly");
2085 nllVar->get()->setAttribute("readOnly");
2086 auto out = cfit_null(true);
2087 nllVar->get()->setAttribute("readOnly", tmp);
2088 if (out) {
2089 // retrieve from cache worked, no need to generate dataset
2090 return out;
2091 } else if (!tmp) { // don't need to setData if doing a readOnly fit
2092 nllVar->setData(data());
2093 }
2094 }
2095 } else if (!nllVar->get()->getAttribute("readOnly")) { // don't need to setData if doing a readOnly fit
2096 nllVar->setData(fData);
2097 }
2098 if (fUfit) {
2099 // move to ufit coords before evaluating
2100 *nllVar->fFuncVars = fUfit->floatParsFinal();
2101 }
2102 nllVar->fFuncVars->setAttribAll("Constant", false);
2103 *nllVar->fFuncVars = *coords; // will reconst the coords
2104 if (nllVar->fFuncGlobs)
2105 nllVar->fFuncGlobs->setAttribAll("Constant", true);
2106 if (fPOIName()) {
2107 nllVar->fFuncVars->find(fPOIName())
2108 ->setStringAttribute("altVal", (!std::isnan(fAltVal())) ? TString::Format("%g", fAltVal()) : nullptr);
2109 }
2110 if (fGenFit) {
2111 nllVar->get()->SetName(
2112 TString::Format("%s/%s_%s", nllVar->get()->GetName(), fGenFit->GetName(), (isExpected) ? "asimov" : "toys"));
2113 if (!isExpected)
2114 nllVar->get()->SetName(TString::Format("%s/%s", nllVar->get()->GetName(), fData.first->GetName()));
2115 }
2116 nllVar->get()->setStringAttribute("fitresultTitle", collectionContents(poi()).c_str());
2117 return (fNull_cfit = nllVar->minimize());
2118}
2119
2120std::shared_ptr<const RooFitResult> xRooNLLVar::xRooHypoPoint::cfit_lbound(bool readOnly)
2121{
2122 auto _first_poi = dynamic_cast<RooRealVar *>(poi().first());
2123 if (!_first_poi)
2124 return nullptr;
2125 if (_first_poi->getMin("physical") <= _first_poi->getMin())
2126 return nullptr;
2127 if (fLbound_cfit)
2128 return fLbound_cfit;
2129 if (auto rfit = retrieveFit(6)) {
2130 return fLbound_cfit = rfit;
2131 }
2132 if (!nllVar || (readOnly && nllVar->get() && !nllVar->get()->getAttribute("readOnly")))
2133 return nullptr;
2134 if (!nllVar->fFuncVars)
2135 nllVar->reinitialize();
2136 AutoRestorer snap(*nllVar->fFuncVars, nllVar.get());
2137 if (!fData.first) {
2138 if (!readOnly && isExpected && fGenFit) {
2139 // can try to do a readOnly in case can load from cache
2140 bool tmp = nllVar->get()->getAttribute("readOnly");
2141 nllVar->get()->setAttribute("readOnly");
2142 auto out = cfit_lbound(true);
2143 nllVar->get()->setAttribute("readOnly", tmp);
2144 if (out) {
2145 // retrieve from cache worked, no need to generate dataset
2146 return out;
2147 } else if (!tmp) { // don't need to setData if doing a readOnly fit
2148 nllVar->setData(data());
2149 }
2150 }
2151 } else if (!nllVar->get()->getAttribute("readOnly")) { // don't need to setData if doing a readOnly fit
2152 nllVar->setData(fData);
2153 }
2154 if (fUfit) {
2155 // move to ufit coords before evaluating
2156 *nllVar->fFuncVars = fUfit->floatParsFinal();
2157 }
2158 nllVar->fFuncVars->setAttribAll("Constant", false);
2159 *nllVar->fFuncVars = *coords; // will reconst the coords
2160 nllVar->fFuncVars->setRealValue(_first_poi->GetName(), _first_poi->getMin("physical"));
2161 if (nllVar->fFuncGlobs)
2162 nllVar->fFuncGlobs->setAttribAll("Constant", true);
2163 if (fPOIName()) {
2164 nllVar->fFuncVars->find(fPOIName())
2165 ->setStringAttribute("altVal", (!std::isnan(fAltVal())) ? TString::Format("%g", fAltVal()) : nullptr);
2166 }
2167 if (fGenFit) {
2168 nllVar->get()->SetName(
2169 TString::Format("%s/%s_%s", nllVar->get()->GetName(), fGenFit->GetName(), (isExpected) ? "asimov" : "toys"));
2170 if (!isExpected)
2171 nllVar->get()->SetName(TString::Format("%s/%s", nllVar->get()->GetName(), fData.first->GetName()));
2172 }
2173 nllVar->get()->setStringAttribute(
2174 "fitresultTitle",
2175 collectionContents(*std::unique_ptr<RooAbsCollection>(nllVar->fFuncVars->selectCommon(poi()))).c_str());
2176 return (fLbound_cfit = nllVar->minimize());
2177}
2178
2179std::shared_ptr<const RooFitResult> xRooNLLVar::xRooHypoPoint::cfit_alt(bool readOnly)
2180{
2181 if (std::isnan(fAltVal()))
2182 return nullptr;
2183 if (fAlt_cfit)
2184 return fAlt_cfit;
2185 if (auto rfit = retrieveFit(2)) {
2186 return fAlt_cfit = rfit;
2187 }
2188 if (!nllVar || (readOnly && nllVar->get() && !nllVar->get()->getAttribute("readOnly")))
2189 return nullptr;
2190 if (!nllVar->fFuncVars)
2191 nllVar->reinitialize();
2192 AutoRestorer snap(*nllVar->fFuncVars, nllVar.get());
2193 if (!fData.first) {
2194 if (!readOnly && isExpected && fGenFit) {
2195 // can try to do a readOnly in case can load from cache
2196 bool tmp = nllVar->get()->getAttribute("readOnly");
2197 nllVar->get()->setAttribute("readOnly");
2198 auto out = cfit_alt(true);
2199 nllVar->get()->setAttribute("readOnly", tmp);
2200 if (out) {
2201 // retrieve from cache worked, no need to generate dataset
2202 return out;
2203 } else if (!tmp) { // don't need to setData if doing a readOnly fit
2204 nllVar->setData(data());
2205 }
2206 }
2207 } else if (!nllVar->get()->getAttribute("readOnly")) { // don't need to setData if doing a readOnly fit
2208 nllVar->setData(fData);
2209 }
2210 if (fUfit) {
2211 // move to ufit coords before evaluating
2212 *nllVar->fFuncVars = fUfit->floatParsFinal();
2213 }
2214 nllVar->fFuncVars->setAttribAll("Constant", false);
2215 *nllVar->fFuncVars = *coords; // will reconst the coords
2216 if (nllVar->fFuncGlobs)
2217 nllVar->fFuncGlobs->setAttribAll("Constant", true);
2218 *nllVar->fFuncVars = alt_poi();
2219 if (fGenFit) {
2220 nllVar->get()->SetName(
2221 TString::Format("%s/%s_%s", nllVar->get()->GetName(), fGenFit->GetName(), (isExpected) ? "asimov" : "toys"));
2222 if (!isExpected)
2223 nllVar->get()->SetName(TString::Format("%s/%s", nllVar->get()->GetName(), fData.first->GetName()));
2224 }
2225 nllVar->get()->setStringAttribute("fitresultTitle", collectionContents(alt_poi()).c_str());
2226 return (fAlt_cfit = nllVar->minimize());
2227}
2228
2230{
2231
2232 auto asi = asimov(readOnly);
2233
2234 if (!asi) {
2235 return std::pair<double, double>(std::numeric_limits<double>::quiet_NaN(), 0);
2236 }
2237
2238 auto out = asi->pll(readOnly);
2239 return std::pair<double, double>(std::abs(fNullVal() - fAltVal()) / sqrt(out.first),
2240 out.second * 0.5 * std::abs(fNullVal() - fAltVal()) /
2241 (out.first * sqrt(out.first)));
2242}
2243
2245{
2246 auto _ts = ts_toys(nSigma);
2247 if (std::isnan(_ts.first))
2248 return _ts;
2249 if (fPllType != xRooFit::Asymptotics::Uncapped && _ts.first == 0)
2250 return std::pair<double, double>(1, 0); // don't need toys to compute this point!
2251
2252 TEfficiency eff("", "", 1, 0, 1);
2253
2254 auto &_theToys = (alt) ? altToys : nullToys;
2255
2256 if (_theToys.empty()) {
2257 return std::pair(0.5, std::numeric_limits<double>::infinity());
2258 }
2259
2260 // loop over toys, count how many are > ts value
2261 // nans (mean bad ts evaluations) will count towards uncertainty
2262 int nans = 0;
2263 double result = 0;
2264 double result_err_up = 0;
2265 double result_err_down = 0;
2266 for (auto &toy : _theToys) {
2267 if (std::isnan(std::get<1>(toy))) {
2268 nans++;
2269 } else {
2270 bool res = std::get<1>(toy) >= _ts.first;
2271 if (std::get<2>(toy) != 1) {
2272 eff.FillWeighted(res, 0.5, std::get<2>(toy));
2273 } else {
2274 eff.Fill(res, 0.5);
2275 }
2276 if (res)
2277 result += std::get<2>(toy);
2278 if (std::get<1>(toy) >= _ts.first - _ts.second)
2279 result_err_up += std::get<2>(toy);
2280 if (std::get<1>(toy) >= _ts.first - _ts.second)
2281 result_err_down += std::get<2>(toy);
2282 }
2283 }
2284 // symmetrize the error
2287 double result_err = std::max(std::abs(result_err_up), std::abs(result_err_down));
2288 // assume the nans would "add" to the p-value, conservative scenario
2289 result_err += nans;
2290 result_err /= _theToys.size();
2291
2292 // don't include the nans for the central value though
2293 result /= (_theToys.size() - nans);
2294
2295 // add to the result_err (in quadrature) the uncert due to limited stats
2297 return std::pair<double, double>(result, result_err);
2298}
2299
2304
2306{
2307 if (!std::isnan(nSigma)) {
2308 return std::pair<double, double>(ROOT::Math::gaussian_cdf(nSigma), 0); // by construction
2309 }
2310 return pX_toys(true, nSigma);
2311}
2312
2314{
2315 xRooHypoPoint out;
2316 out.coords = coords;
2317 out.fPllType = fPllType; // out.fPOIName = fPOIName; out.fNullVal=fNullVal; out.fAltVal = fAltVal;
2318 out.nllVar = nllVar;
2319 if (!nllVar)
2320 return out;
2321 auto _cfit = cfit_null();
2322 if (!_cfit)
2323 return out;
2324 if (!nllVar->fFuncVars)
2325 nllVar->reinitialize();
2326 //*nllVar->fFuncVars = cfit_null()->floatParsFinal();
2327 //*nllVar->fFuncVars = cfit_null()->constPars();
2328 out.fData = xRooFit::generateFrom(*nllVar->fPdf, *_cfit, false, seed); // nllVar->generate(false,seed);
2329 out.fGenFit = _cfit;
2330 return out;
2331}
2332
2334{
2335 xRooHypoPoint out;
2336 out.coords = coords;
2337 out.fPllType = fPllType; // out.fPOIName = fPOIName; out.fNullVal=fNullVal; out.fAltVal = fAltVal;
2338 out.nllVar = nllVar;
2339 if (!nllVar)
2340 return out;
2341 if (!cfit_alt())
2342 return out;
2343 if (!nllVar->fFuncVars)
2344 nllVar->reinitialize();
2345 //*nllVar->fFuncVars = cfit_alt()->floatParsFinal();
2346 //*nllVar->fFuncVars = cfit_alt()->constPars();
2347 out.fData =
2348 xRooFit::generateFrom(*nllVar->fPdf, *cfit_alt(), false, seed); // out.data = nllVar->generate(false,seed);
2349 out.fGenFit = cfit_alt();
2350 return out;
2351}
2352
2354 bool targetCLs, double relErrThreshold, size_t maxToys)
2355{
2356 if ((alt && !cfit_alt()) || (!alt && !cfit_null())) {
2357 throw std::runtime_error("Cannot add toys, invalid conditional fit");
2358 }
2359
2360 auto condition = [&]() { // returns true if need more toys
2361 if (std::isnan(target))
2362 return false;
2363 auto obs = targetCLs ? pCLs_toys(target_nSigma) : (alt ? pAlt_toys(target_nSigma) : pNull_toys(target_nSigma));
2364 if (!std::isnan(obs.first)) {
2365 double diff = (target < 0) ? obs.first : std::abs(obs.first - target);
2366 double err = obs.second;
2367 if (err > 1e-4 && diff <= relErrThreshold * obs.second) {
2368 // return true; // more toys needed
2369 if (targetCLs) {
2370 // decide which type we'd want to generate and update alt flag
2371 auto pNull = pNull_toys(target_nSigma);
2372 auto pAlt = pAlt_toys(target_nSigma);
2373 // std::cout << obs.first << " +/- " << obs.second << ": " << pNull.first << " +/- " << pNull.second << "
2374 // , " << pAlt.first << " +/- " << pAlt.second << std::endl;
2375 alt = (pAlt.second * pNull.first > pNull.second * pAlt.first);
2376 if ((alt ? pAlt.second : pNull.second) < 1e-4)
2377 return false; // stop if error gets too small
2378 }
2379 return true;
2380 }
2381 }
2382 return false;
2383 };
2384
2385 if (!std::isnan(target) && std::isnan(ts_toys(target_nSigma).first)) {
2386 if (std::isnan(target_nSigma)) {
2387 throw std::runtime_error("Cannot target obs p-value because ts value unavailable");
2388 }
2389 if (targetCLs && pCLs_toys(target_nSigma).second == 0) {
2390 // this happens if the mu_test=mu_alt ... no toys needed
2391 return 0;
2392 }
2393
2394 // try generating 100 alt toys
2395 Info("addToys", "First generating 100 alt toys in order to determine expected ts value");
2396 addToys(true, 100, initialSeed);
2397 // if still null then exit
2398 if (std::isnan(ts_toys(target_nSigma).first)) {
2399 throw std::runtime_error("Unable to determine expected ts value");
2400 }
2401 }
2402
2403 size_t nans = 0;
2404 float lastTime = 0;
2405 int lasti = 0;
2406 auto g = gROOT->Get<TGraph>("toyTime");
2407 if (!g) {
2408 g = new TGraph;
2409 g->SetNameTitle("toyTime", "Time per toy;Toy;time [s]");
2410 gROOT->Add(g);
2411 }
2412 g->Set(0);
2413 TStopwatch s2;
2414 s2.Start();
2415 TStopwatch s;
2416 s.Start();
2417
2418 size_t toysAdded(0);
2419 size_t altToysAdded(0);
2420 if (initialSeed) {
2422 }
2423 do {
2424 auto &toys = (alt) ? altToys : nullToys;
2425 if (toys.size() >= maxToys) {
2426 // cannot generate more toys, reached limit already
2427 break;
2428 }
2429 // don't generate toys if reached target
2430 if (!std::isnan(target) && !condition()) {
2431 break;
2432 }
2433 auto currVal = std::isnan(target) ? std::pair(0., 0.)
2434 : (targetCLs ? pCLs_toys(target_nSigma)
2435 : (alt ? pAlt_toys(target_nSigma) : pNull_toys(target_nSigma)));
2436 size_t nnToys = std::min(size_t(nToys), (maxToys - toys.size()));
2437
2438 for (size_t i = 0; i < nnToys; i++) {
2439 int seed = RooRandom::randomGenerator()->Integer(std::numeric_limits<uint32_t>::max());
2440 auto toy = ((alt) ? generateAlt(seed) : generateNull(seed));
2441 TDirectory *tmp = gDirectory;
2442 gDirectory = nullptr; // disables any saving of fit results for toys
2443 toys.push_back(std::make_tuple(seed, toy.pll().first, 1.));
2444 gDirectory = tmp;
2445 (alt ? altToysAdded : toysAdded)++;
2446 if (std::isnan(std::get<1>(toys.back())))
2447 nans++;
2448 g->SetPoint(g->GetN(), g->GetN(), s.RealTime() - lastTime); // stops the clock
2449 lastTime = s.RealTime();
2450 if (s.RealTime() > 10) {
2451 std::cout << "\r"
2452 << TString::Format("Generated %d/%d %s hypothesis toys [%.2f toys/s]",
2453 int(alt ? altToysAdded : toysAdded), int(nnToys), alt ? "alt" : "null",
2455 if (!std::isnan(target)) {
2456 std::cout << " [current=" << currVal.first << "+/-" << currVal.second << " target=" << target
2457 << " nSigma=" << target_nSigma << "]";
2458 }
2459 std::cout << "..." << std::flush;
2461 s.Reset();
2462 if (!gROOT->IsBatch()) {
2463 Draw();
2464 if (gPad) {
2465 gPad->Update();
2466#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 30, 00)
2467 gPad->GetCanvas()->ResetUpdated(); // stops previous canvas being replaced in a jupyter notebook
2468#endif
2470 }
2471 }
2472 s.Start();
2473 // std::cout << "Generated " << i << "/" << nToys << (alt ? " alt " : " null ") << " hypothesis toys " ..."
2474 // << std::endl;
2475 }
2476 s.Continue();
2477 }
2478 // sort the toys ... put nans first - do by setting all as negative inf (is that still necessary with the custom
2479 // sort below??)
2480 for (auto &t : toys) {
2481 if (std::isnan(std::get<1>(t)))
2482 std::get<1>(t) = -std::numeric_limits<double>::infinity();
2483 }
2484 std::sort(toys.begin(), toys.end(),
2485 [](const decltype(nullToys)::value_type &a, const decltype(nullToys)::value_type &b) -> bool {
2486 if (std::isnan(std::get<1>(a)))
2487 return true;
2488 if (std::isnan(std::get<1>(b)))
2489 return false;
2490 return std::get<1>(a) < std::get<1>(b);
2491 });
2492 for (auto &t : toys) {
2493 if (std::isinf(std::get<1>(t)))
2494 std::get<1>(t) = std::numeric_limits<double>::quiet_NaN();
2495 }
2496 if (std::isnan(target)) {
2497 break; // no more toys if not doing a target
2498 }
2499 // if(condition()) {
2500 // Info("addToys","Generating more toys to determine p-value ... currently: %f +/-
2501 // %f",pNull_toys(target_nSigma).first,pNull_toys(target_nSigma).second);
2502 // }
2503 } while (condition());
2504 if (lasti) {
2505 std::cout << "\r"
2506 << "Finished Generating ";
2507 if (toysAdded) {
2508 std::cout << toysAdded << " null ";
2509 }
2510 if (altToysAdded) {
2511 std::cout << altToysAdded << " alt ";
2512 }
2513 std::cout << "toys " << TString::Format("[%.2f toys/s overall]", double(toysAdded + altToysAdded) / s2.RealTime())
2514 << std::endl;
2515 if (!gROOT->IsBatch()) {
2516 Draw();
2517 if (gPad) {
2518 gPad->Update();
2519#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 30, 00)
2520 gPad->GetCanvas()->ResetUpdated(); // stops previous canvas being replaced in a jupyter notebook
2521#endif
2523 }
2524 }
2525 }
2526
2527 if (nans > 0) {
2528 std::cout << "Warning: " << nans << " toys were bad" << std::endl;
2529 }
2530 return toysAdded;
2531}
2532
2534{
2535 addToys(false, nToys, seed, target, target_nSigma);
2536}
2538{
2539 addToys(true, nToys, seed, target, target_nSigma);
2540}
2541
2543{
2544 addToys(false, nToys, seed, target, target_nSigma, true);
2545 return;
2546 //
2547 // auto condition = [&](bool doingAlt=false) { // returns true if need more toys
2548 // if(std::isnan(target)) return false;
2549 // auto pval = pCLs_toys(target_nSigma);
2550 // if (!std::isnan(pval.first)) {
2551 // double diff = std::abs(pval.first - target);
2552 // double err = pval.second;
2553 // if (err > 1e-4 && diff <= 2 * pval.second) {
2554 // return true; // more toys needed
2555 // // decide which type we'd want to generate
2556 // // if it matches the type we are generating, then return true
2557 // auto pNull = pNull_toys(target_nSigma);
2558 // auto pAlt = pAlt_toys(target_nSigma);
2559 // if ((doingAlt ? pAlt.second : pNull.second) < 1e-4) return false; // stop if error gets too small
2560 // bool doAlt = (pAlt.second * pNull.first > pNull.second * pAlt.first);
2561 // return doAlt == doingAlt;
2562 // }
2563 // }
2564 // return false;
2565 // };
2566 // while(condition()) {
2567 // bool doAlt = false;
2568 // double relErrThreshold = 2;
2569 // if(nullToys.size()<size_t(nToys)) {
2570 // addToys(false,nToys);continue;
2571 // } else if(altToys.size()<size_t(nToys)) {
2572 // addToys(true,nToys);continue;
2573 // } else {
2574 // // see which have bigger errors ... generate more of that ...
2575 // auto pNull = pNull_toys(target_nSigma);
2576 // auto pAlt = pAlt_toys(target_nSigma);
2577 // doAlt = (pAlt.second*pNull.first > pNull.second*pAlt.first);
2578 // if( (doAlt ? pAlt.second : pNull.second) < 1e-4 ) break; // stop if error gets too small
2579 // auto pCLs = pCLs_toys(target_nSigma);
2580 // relErrThreshold = (doAlt) ? (pNull.second/pNull.first) : (pAlt.second/pAlt.first);
2581 // relErrThreshold = std::min(2.,std::abs(relErrThreshold));
2582 // std::cout << "Current pCLs = " << pCLs.first << " +/- " << pCLs.second
2583 // << " (pNull = " << pNull.first << " +/- " << pNull.second
2584 // << " , pAlt = " << pAlt.first << " +/- " << pAlt.second << ") ... generating more " << (doAlt ?
2585 // "alt" : "null") << " toys " << relErrThreshold << std::endl;
2586 //
2587 // }
2588 // if( addToys(doAlt, nToys/*, seed, -1, target_nSigma,relErrThreshold*/) == 0) {
2589 // break; // no toys got added, so stop looping
2590 // }
2591 // }
2592}
2593
2596{
2597 xRooHypoPoint out;
2598 // out.fPOIName = parName; out.fNullVal = value; out.fAltVal = alt_value;
2599
2600 if (!fFuncVars) {
2601 reinitialize();
2602 }
2604
2605 out.nllVar = std::make_shared<xRooNLLVar>(*this);
2606 out.fData = getData();
2607
2608 TStringToken pattern(poiValues, ",");
2610 while (pattern.NextToken()) {
2611 TString s = pattern.Data();
2612 TString cName = s;
2613 double val = std::numeric_limits<double>::quiet_NaN();
2614 auto i = s.Index("=");
2615 if (i != -1) {
2616 cName = s(0, i);
2617 TString cVal = s(i + 1, s.Length());
2618 if (!cVal.IsFloat())
2619 throw std::runtime_error("poiValues must contain value");
2620 val = cVal.Atof();
2621 }
2622 auto v = dynamic_cast<RooRealVar *>(fFuncVars->find(cName));
2623 if (!v)
2624 throw std::runtime_error("Cannot find poi");
2625 if (!std::isnan(val))
2626 v->setVal(val);
2627 v->setConstant(); // because will select constants as coords
2628 if (poiNames != "") {
2629 poiNames += ",";
2630 }
2631 poiNames += cName;
2632 }
2633 if (poiNames == "") {
2634 throw std::runtime_error("No poi");
2635 }
2636 if (!std::isnan(alt_value)) {
2637 std::unique_ptr<RooAbsCollection> thePoi(fFuncVars->selectByName(poiNames));
2638 for (auto b : *thePoi) {
2639 if (!static_cast<RooRealVar *>(b)->hasRange("physical")) {
2640 static_cast<RooRealVar *>(b)->setRange("physical", 0, std::numeric_limits<double>::infinity());
2641 }
2642 }
2643 }
2644 auto _snap = std::unique_ptr<RooAbsCollection>(fFuncVars->selectByAttrib("Constant", true))->snapshot();
2645 _snap->setAttribAll("poi", false);
2646 std::unique_ptr<RooAbsCollection> _poi(_snap->selectByName(poiNames));
2647 _poi->setAttribAll("poi", true);
2648 if (std::isnan(alt_value)) {
2649 for (auto a : *_poi)
2650 a->setStringAttribute("altVal", nullptr);
2651 } else {
2652 for (auto a : *_poi)
2653 a->setStringAttribute("altVal", TString::Format("%g", alt_value));
2654 }
2655 if (fGlobs)
2656 _snap->remove(*fGlobs, true, true);
2657 out.coords.reset(_snap);
2658
2659 auto _type = pllType;
2660 if (_type == xRooFit::Asymptotics::Unknown) {
2661 // decide based on values
2662 if (std::isnan(alt_value)) {
2664 } else if (dynamic_cast<RooRealVar *>(_poi->first())->getVal() >= alt_value) {
2666 } else {
2668 }
2669 }
2670
2671 out.fPllType = _type;
2672
2673 return out;
2674}
2675
2676xRooNLLVar::xRooHypoPoint
2678{
2679 if (!fFuncVars) {
2680 reinitialize();
2681 }
2682 std::unique_ptr<RooAbsCollection> _poi(fFuncVars->selectByAttrib("poi", true));
2683 if (_poi->empty()) {
2684 throw std::runtime_error("No POI specified in model");
2685 } else if (_poi->size() != 1) {
2686 throw std::runtime_error("Multiple POI specified in model");
2687 }
2688 return hypoPoint(_poi->first()->GetName(), value, alt_value, pllType);
2689}
2690
2696
2698{
2699
2700 if (!nllVar && !hypoTestResult)
2701 return;
2702
2703 TString sOpt(opt);
2704 sOpt.ToLower();
2705 bool hasSame = sOpt.Contains("same");
2706 sOpt.ReplaceAll("same", "");
2707
2708 TVirtualPad *pad = gPad;
2709
2710 TH1 *hAxis = nullptr;
2711
2712 auto clearPad = []() {
2713 gPad->Clear();
2714 if (gPad->GetNumber() == 0) {
2715 gPad->SetBottomMargin(gStyle->GetPadBottomMargin());
2716 gPad->SetTopMargin(gStyle->GetPadTopMargin());
2717 gPad->SetLeftMargin(gStyle->GetPadLeftMargin());
2718 gPad->SetRightMargin(gStyle->GetPadRightMargin());
2719 }
2720 };
2721
2722 if (!hasSame || !pad) {
2723 if (!pad) {
2725 pad = gPad;
2726 }
2727 clearPad();
2728 } else {
2729 // get the histogram representing the axes
2730 hAxis = dynamic_cast<TH1 *>(pad->GetPrimitive(".axis"));
2731 if (!hAxis) {
2732 for (auto o : *pad->GetListOfPrimitives()) {
2733 if (hAxis = dynamic_cast<TH1 *>(o); hAxis)
2734 break;
2735 }
2736 }
2737 }
2738
2739 // get min and max values
2740 double _min = std::numeric_limits<double>::quiet_NaN();
2741 double _max = -std::numeric_limits<double>::quiet_NaN();
2742
2743 for (auto &p : nullToys) {
2744 if (std::get<2>(p) == 0)
2745 continue;
2746 if (std::isnan(std::get<1>(p)))
2747 continue;
2748 _min = std::min(std::get<1>(p), _min);
2749 _max = std::max(std::get<1>(p), _max);
2750 }
2751 for (auto &p : altToys) {
2752 if (std::get<2>(p) == 0)
2753 continue;
2754 if (std::isnan(std::get<1>(p)))
2755 continue;
2756 _min = std::min(std::get<1>(p), _min);
2757 _max = std::max(std::get<1>(p), _max);
2758 }
2759
2760 auto obs = ts_asymp();
2761 if (!std::isnan(obs.first)) {
2762 _min = std::min(obs.first - std::abs(obs.first) * 0.1, _min);
2763 _max = std::max(obs.first + std::abs(obs.first) * 0.1, _max);
2764 }
2765 // these are used down below to add obs p-values to legend, but up here because can trigger fits that create asimov
2766 auto pNull = pNull_toys();
2767 auto pAlt = pAlt_toys();
2768 auto pNullA = (fPllType == xRooFit::Asymptotics::Unknown)
2769 ? std::pair(std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN())
2770 : pNull_asymp();
2771 auto pAltA = (fPllType == xRooFit::Asymptotics::Unknown)
2772 ? std::pair(std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN())
2773 : pAlt_asymp();
2774 sigma_mu(true);
2775 auto asi = (fPllType != xRooFit::Asymptotics::Unknown && fAsimov && fAsimov->fUfit && fAsimov->fNull_cfit)
2776 ? fAsimov->pll().first
2777 : std::numeric_limits<double>::quiet_NaN();
2778 if (!std::isnan(asi) && asi > 0) {
2779 // can calculate asymptotic distributions,
2780 _min = std::min(asi - std::abs(asi), _min);
2781 _max = std::max(asi + std::abs(asi), _max);
2782 }
2783 if (_min > 0)
2784 _min = 0;
2785
2786 auto _poi = dynamic_cast<RooRealVar *>(poi().first());
2787
2788 auto makeHist = [&](bool isAlt) {
2789 TString title;
2790 auto h = new TH1D((isAlt) ? "alt_toys" : "null_toys", "", 100, _min, _max + (_max - _min) * 0.01);
2791 h->SetDirectory(nullptr);
2792 size_t nBadOrZero = 0;
2793 for (auto &p : (isAlt) ? altToys : nullToys) {
2794 double w = std::isnan(std::get<1>(p)) ? 0 : std::get<2>(p);
2795 if (w == 0)
2796 nBadOrZero++;
2797 if (!std::isnan(std::get<1>(p)))
2798 h->Fill(std::get<1>(p), w);
2799 }
2800 if (h->GetEntries() > 0)
2801 h->Scale(1. / h->Integral(0, h->GetNbinsX() + 1));
2802
2803 // add POI values to identify hypos
2804 // for(auto p : *fPOI) {
2805 // if (auto v = dynamic_cast<RooRealVar*>(p)) {
2806 // if (auto v2 = dynamic_cast<RooRealVar*>(fAltPoint->fCoords->find(*v)); v2 &&
2807 // v2->getVal()!=v->getVal()) {
2808 // // found point that differs in poi and altpoint value, so print my coords value for this
2809 // title += TString::Format("%s' = %g,
2810 // ",v->GetTitle(),dynamic_cast<RooRealVar*>(fCoords->find(*v))->getVal());
2811 // }
2812 // }
2813 // }
2814 if (fPOIName())
2815 title += TString::Format("%s' = %g", fPOIName(), (isAlt) ? fAltVal() : fNullVal());
2816 title += TString::Format(" , N_{toys}=%d", int((isAlt) ? altToys.size() : nullToys.size()));
2817 if (nBadOrZero > 0)
2818 title += TString::Format(" (N_{bad/0}=%d)", int(nBadOrZero));
2819 title += ";";
2820 title += tsTitle();
2821 title += TString::Format(";Probability Mass");
2822 h->SetTitle(title);
2823 h->SetLineColor(isAlt ? kRed : kBlue);
2824 h->SetLineWidth(2);
2825 h->SetMarkerSize(0);
2826 h->SetBit(kCanDelete);
2827 return h;
2828 };
2829
2830 auto nullHist = makeHist(false);
2831 auto altHist = makeHist(true);
2832
2833 TLegend *l = nullptr;
2834 auto h = (nullHist->GetEntries()) ? nullHist : altHist;
2835 if (!hasSame) {
2836 gPad->SetLogy();
2837 auto axis = static_cast<TH1 *>(h->Clone(".axis"));
2838 axis->SetBit(kCanDelete);
2839 axis->SetStats(false);
2840 axis->Reset("ICES");
2841 axis->SetTitle(TString::Format("%s HypoPoint", collectionContents(poi()).c_str()));
2842 axis->SetLineWidth(0);
2843 axis->Draw(""); // h->Draw("axis"); cant use axis option if want title drawn
2844 axis->SetMinimum(1e-7);
2845 axis->GetYaxis()->SetRangeUser(1e-7, 10);
2846 axis->SetMaximum(h->GetMaximum());
2847 hAxis = axis;
2848 l = new TLegend(0.4, 0.7, 1. - gPad->GetRightMargin(), 1. - gPad->GetTopMargin());
2849 l->SetName("legend");
2850 l->SetFillStyle(0);
2851 l->SetBorderSize(0);
2853 l->Draw();
2854 l->ConvertNDCtoPad();
2855 } else {
2856 for (auto o : *gPad->GetListOfPrimitives()) {
2857 l = dynamic_cast<TLegend *>(o);
2858 if (l)
2859 break;
2860 }
2861 }
2862
2863 if (h->GetEntries() > 0) {
2864 h->Draw("esame");
2865 } else {
2866 h->Draw("axissame"); // for unknown reason if second histogram empty it still draws with two weird bars???
2867 }
2868 h = altHist;
2869 if (h->GetEntries() > 0) {
2870 h->Draw("esame");
2871 } else {
2872 h->Draw("axissame"); // for unknown reason if second histogram empty it still draws with two weird bars???
2873 }
2874
2875 if (l) {
2876 l->AddEntry(nullHist);
2877 l->AddEntry(altHist);
2878 }
2879
2880 if (fAsimov && fAsimov->fUfit && fAsimov->fNull_cfit && !std::isnan(sigma_mu().first) && !std::isnan(fAltVal())) {
2881 auto hh = static_cast<TH1 *>(nullHist->Clone("null_asymp"));
2882 hh->SetBit(kCanDelete);
2883 hh->SetStats(false);
2884 hh->SetLineStyle(2);
2885 hh->Reset();
2886 for (int i = 1; i <= hh->GetNbinsX(); i++) {
2887 hh->SetBinContent(
2888 i, xRooFit::Asymptotics::PValue(fPllType, hh->GetBinLowEdge(i), fNullVal(), fNullVal(), sigma_mu().first,
2889 _poi->getMin("physical"), _poi->getMax("physical")) -
2890 xRooFit::Asymptotics::PValue(fPllType, hh->GetBinLowEdge(i + 1), fNullVal(), fNullVal(),
2891 sigma_mu().first, _poi->getMin("physical"), _poi->getMax("physical")));
2892 }
2893 hh->Draw("lsame");
2894 hh = static_cast<TH1 *>(altHist->Clone("alt_asymp"));
2895 hh->SetBit(kCanDelete);
2896 hh->SetStats(false);
2897 hh->SetLineStyle(2);
2898 hh->Reset();
2899 for (int i = 1; i <= hh->GetNbinsX(); i++) {
2900 hh->SetBinContent(
2901 i, xRooFit::Asymptotics::PValue(fPllType, hh->GetBinLowEdge(i), fNullVal(), fAltVal(), sigma_mu().first,
2902 _poi->getMin("physical"), _poi->getMax("physical")) -
2903 xRooFit::Asymptotics::PValue(fPllType, hh->GetBinLowEdge(i + 1), fNullVal(), fAltVal(),
2904 sigma_mu().first, _poi->getMin("physical"), _poi->getMax("physical")));
2905 }
2906 hh->Draw("lsame");
2907 }
2908
2909 // draw observed points
2910 TLine ll;
2911 ll.SetLineStyle(1);
2912 ll.SetLineWidth(3);
2913 // for(auto p : fObs) {
2914 auto tl = ll.DrawLine(obs.first, hAxis->GetMinimum(), obs.first, 0.1);
2915 auto label = TString::Format("obs ts = %.4f", obs.first);
2916 if (obs.second)
2917 label += TString::Format(" #pm %.4f", obs.second);
2918
2919 l->AddEntry(tl, label, "l");
2920 label = "";
2921 if (nullHist->GetEntries() || altHist->GetEntries()) {
2922 auto pCLs = pCLs_toys();
2923 label += " p_{toy}=(";
2924 label += (std::isnan(pNull.first)) ? "-" : TString::Format("%.4f #pm %.4f", pNull.first, pNull.second);
2925 label += (std::isnan(pAlt.first)) ? ",-" : TString::Format(",%.4f #pm %.4f", pAlt.first, pAlt.second);
2926 label += (std::isnan(pCLs.first)) ? ",-)" : TString::Format(",%.4f #pm %.4f)", pCLs.first, pCLs.second);
2927 }
2928 if (label.Length() > 0)
2929 l->AddEntry("", label, "");
2930 label = "";
2931 if (!std::isnan(pNullA.first) || !std::isnan(pAltA.first)) {
2932 auto pCLs = pCLs_asymp();
2933 label += " p_{asymp}=(";
2934 label += (std::isnan(pNullA.first)) ? "-" : TString::Format("%.4f #pm %.4f", pNullA.first, pNullA.second);
2935 label += (std::isnan(pAltA.first)) ? ",-" : TString::Format(",%.4f #pm %.4f", pAltA.first, pAltA.second);
2936 label += (std::isnan(pCLs.first)) ? ",-)" : TString::Format(",%.4f #pm %.4f)", pCLs.first, pCLs.second);
2937 }
2938 if (label.Length() > 0)
2939 l->AddEntry("", label, "");
2940
2941 if (auto ax = dynamic_cast<TH1 *>(gPad->GetPrimitive(".axis")))
2942 ax->GetYaxis()->SetRangeUser(1e-7, 1);
2943}
2944
2946{
2947 auto v = dynamic_cast<RooRealVar *>(poi().empty() ? nullptr : poi().first());
2949 if (v && v->hasRange("physical") && v->getMin("physical") != -std::numeric_limits<double>::infinity()) {
2950 return (inWords) ? TString::Format("Lower-Bound One-Sided Limit PLR")
2951 : TString::Format("#tilde{q}_{%s=%g}", v->GetTitle(), v->getVal());
2952 } else if (v) {
2953 return (inWords) ? TString::Format("One-Sided Limit PLR")
2954 : TString::Format("q_{%s=%g}", v->GetTitle(), v->getVal());
2955 } else {
2956 return "q";
2957 }
2958 } else if (fPllType == xRooFit::Asymptotics::TwoSided) {
2959 if (v && v->hasRange("physical") && v->getMin("physical") != -std::numeric_limits<double>::infinity()) {
2960 return (inWords) ? TString::Format("Lower-Bound PLR")
2961 : TString::Format("#tilde{t}_{%s=%g}", v->GetTitle(), v->getVal());
2962 } else if (v) {
2963 return (inWords) ? TString::Format("-2log[L(%s,#hat{#hat{#theta}})/L(#hat{%s},#hat{#theta})]", v->GetTitle(),
2964 v->GetTitle())
2965 : TString::Format("t_{%s=%g}", v->GetTitle(), v->getVal());
2966 } else
2967 return "t";
2968 } else if (fPllType == xRooFit::Asymptotics::OneSidedNegative) {
2969 if (v && v->hasRange("physical") && v->getMin("physical") != -std::numeric_limits<double>::infinity()) {
2970 return (inWords) ? TString::Format("Lower-Bound One-Sided Discovery PLR")
2971 : TString::Format("#tilde{r}_{%s=%g}", v->GetTitle(), v->getVal());
2972 } else if (v) {
2973 return (inWords) ? TString::Format("One-Sided Discovery PLR")
2974 : TString::Format("r_{%s=%g}", v->GetTitle(), v->getVal());
2975 } else {
2976 return "r";
2977 }
2978 } else if (fPllType == xRooFit::Asymptotics::Uncapped) {
2979 if (v && v->hasRange("physical") && v->getMin("physical") != -std::numeric_limits<double>::infinity()) {
2980 return (inWords) ? TString::Format("Lower-Bound Uncapped PLR")
2981 : TString::Format("#tilde{u}_{%s=%g}", v->GetTitle(), v->getVal());
2982 } else if (v) {
2983 return (inWords) ? TString::Format("Uncapped PLR") : TString::Format("u_{%s=%g}", v->GetTitle(), v->getVal());
2984 } else {
2985 return "u";
2986 }
2987 } else {
2988 return "Test Statistic";
2989 }
2990}
2991
2993{
2994 return (poi().empty()) ? nullptr : (poi().first())->GetName();
2995}
2997{
2998 auto first_poi = dynamic_cast<RooAbsReal *>(poi().first());
2999 return (first_poi == nullptr) ? std::numeric_limits<double>::quiet_NaN() : first_poi->getVal();
3000}
3002{
3003 auto _alt_poi = alt_poi(); // need to keep alive as alt_poi owns its contents
3004 auto first_poi = dynamic_cast<RooAbsReal *>(_alt_poi.first());
3005 return (first_poi == nullptr) ? std::numeric_limits<double>::quiet_NaN() : first_poi->getVal();
3006}
3007
3009{
3010 auto first_poi = dynamic_cast<RooAbsRealLValue *>(poi().first());
3011 if (!first_poi) {
3012 throw std::runtime_error("HypoPoint has no POI, cannot set null value");
3013 }
3014 first_poi->setVal(val);
3015}
3017{
3018 auto first_poi = dynamic_cast<RooAbsArg *>(poi().first());
3019 if (!first_poi) {
3020 throw std::runtime_error("HypoPoint has no POI, cannot set alt value");
3021 }
3022 first_poi->setStringAttribute("altVal", TString::Format("%g", val).Data());
3023}
3024
3025xRooNLLVar::xRooHypoSpace xRooNLLVar::hypoSpace(const char *parName, int nPoints, double low, double high,
3027 int tsType)
3028{
3029 if (nPoints < 0 || tsType < 0) {
3030 // nPoints<0 catches case where pyROOT has converted TestStatistic enum to int
3031 if (nPoints < 0) {
3032 tsType = nPoints;
3033 nPoints = int(low + 0.5);
3034 low = high;
3035 high = alt_value;
3036 }
3037 if (alt_value == std::numeric_limits<double>::quiet_NaN()) {
3039 alt_value = 0;
3041 alt_value = 1;
3042 }
3043 }
3044
3045 auto out = hypoSpace(parName, pllType, alt_value);
3046
3047 // TODO: things like the physical range and alt value can't be stored on the poi
3048 // because if they change they will change for all hypoSpaces at once, so cannot have
3049 // two hypoSpace with e.g. different physical ranges.
3050 // the hypoSpace should make a copy of them at some point
3051 for (auto p : out.poi()) {
3053 dynamic_cast<RooRealVar *>(p)->setRange("physical", 0, std::numeric_limits<double>::infinity());
3054 Info("xRooNLLVar::hypoSpace", "Setting physical range of %s to [0,inf]", p->GetName());
3055 } else if (dynamic_cast<RooRealVar *>(p)->hasRange("physical")) {
3056 dynamic_cast<RooRealVar *>(p)->removeRange("physical");
3057 Info("xRooNLLVar::hypoSpace", "Removing physical range of %s", p->GetName());
3058 }
3059 }
3060
3061 // ensure pll type is set explicitly if known at this point
3063 out.fTestStatType = xRooFit::Asymptotics::OneSidedPositive;
3065 out.fTestStatType = xRooFit::Asymptotics::Uncapped;
3066 } else if (tsType == xRooFit::TestStatistic::q0) {
3067 out.fTestStatType = xRooFit::Asymptotics::OneSidedNegative;
3068 }
3069
3070 if (nPoints > 0) {
3071 out.AddPoints(parName, nPoints, low, high);
3072 } else {
3073 if (!std::isnan(low) && !std::isnan(high) && !(std::isinf(low) && std::isinf(high))) {
3074 for (auto p : out.poi()) {
3075 dynamic_cast<RooRealVar *>(p)->setRange("scan", low, high);
3076 }
3077 }
3078 }
3079 return out;
3080 }
3081
3083 if (nPoints > 0)
3084 hs.AddPoints(parName, nPoints, low, high);
3085 else {
3086 if (!std::isnan(low) && !std::isnan(high) && !(std::isinf(low) && std::isinf(high))) {
3087 for (auto p : hs.poi()) {
3088 dynamic_cast<RooRealVar *>(p)->setRange("scan", low, high);
3089 }
3090 }
3091 }
3092 return hs;
3093}
3094
3097{
3098 auto _poi = std::unique_ptr<RooAbsCollection>(
3099 std::unique_ptr<RooAbsCollection>(pdf()->getVariables())->selectByAttrib("poi", true));
3100 if (_poi->empty())
3101 throw std::runtime_error("You must specify a POI for the hypoSpace");
3102 return hypoSpace(_poi->first()->GetName(), nPoints, low, high, alt_value, pllType);
3103}
3104
3107{
3109
3110 s.AddModel(pdf());
3111 if (strlen(parName)) {
3112 std::unique_ptr<RooAbsCollection> axes(s.pars()->selectByName(parName));
3113 if (axes->empty())
3114 throw std::runtime_error("parameter not found");
3115 axes->setAttribAll("axis", true);
3116 }
3117 /*if (std::unique_ptr<RooAbsCollection>(s.pars()->selectByAttrib("poi", true))->empty()) {
3118 throw std::runtime_error("You must specify at least one POI for the hypoSpace");
3119 }*/
3120 s.fNlls[s.fPdfs.begin()->second] = std::make_shared<xRooNLLVar>(*this);
3122
3123 for (auto poi : s.poi()) {
3124 poi->setStringAttribute("altVal", std::isnan(alt_value) ? nullptr : TString::Format("%f", alt_value));
3125 }
3126
3127 return s;
3128}
3129
3131{
3132 if (hypoTestResult) {
3133 return *hypoTestResult;
3134 }
3136 out.SetBackgroundAsAlt(true);
3137 out.SetName(TUUID().AsString());
3138 out.SetTitle(TString::Format("%s HypoPoint", collectionContents(poi()).c_str()));
3139
3140 bool setReadonly = false;
3141 if (nllVar && !nllVar->get()->getAttribute("readOnly")) {
3142 setReadonly = true;
3143 nllVar->get()->setAttribute("readOnly");
3144 }
3145
3146 out.SetTestStatisticData(ts_asymp().first);
3147
3148 // build a ds to hold all fits ... store coords in the globs list of the nullDist
3149 // also need to store at least mu_hat value(s)
3152 fitMeta.addClone(RooCategory("pllType", "test statistic type",
3153 {{"TwoSided", 0},
3154 {"OneSidedPositive", 1},
3155 {"OneSidedNegative", 2},
3156 {"OneSidedAbsolute", 3},
3157 {"Uncapped", 4},
3158 {"Unknown", 5}}));
3159 if (ufit()) {
3160 fitMeta.addClone(ufit()->floatParsFinal());
3161 }
3162 fitMeta.setCatIndex("pllType", int(fPllType));
3163 fitMeta.addClone(RooRealVar("isExpected", "isExpected", int(isExpected)));
3164 fitMeta.addClone(RooRealVar("obs_ts_err", "obs_ts_err", obs_ts_err));
3165 fitDetails.addClone(RooCategory("type", "fit type",
3166 {{"ufit", 0},
3167 {"cfit_null", 1},
3168 {"cfit_alt", 2},
3169 {"asimov_ufit", 3},
3170 {"asimov_cfit_null", 4},
3171 {"gen", 5},
3172 {"cfit_lbound", 6}}));
3173 // fitDetails.addClone(RooStringVar("name", "Fit Name", "")); -- not supported properly in ROOT yet
3174 fitDetails.addClone(RooRealVar("status", "status", 0));
3175 fitDetails.addClone(RooRealVar("covQual", "covQual", 0));
3176 fitDetails.addClone(RooRealVar("minNll", "minNll", 0));
3177 fitDetails.addClone(RooRealVar("edm", "edm", 0));
3178 auto fitDS = new RooDataSet("fits", "fit summary data", fitDetails);
3179 // fitDS->convertToTreeStore(); // strings not stored properly in vector store, so do convert! - not needed since
3180 // string var storage not properly supported - storing in globs list instead
3181
3182 for (int i = 0; i < 7; i++) {
3183 std::shared_ptr<const RooFitResult> fit;
3184 switch (i) {
3185 case 0: fit = ufit(); break;
3186 case 1: fit = cfit_null(); break;
3187 case 2: fit = cfit_alt(); break;
3188 case 3: fit = asimov() ? asimov()->ufit(true) : nullptr; break;
3189 case 4: fit = asimov() ? asimov()->cfit_null(true) : nullptr; break;
3190 case 5: fit = fGenFit; break;
3191 case 6: fit = cfit_lbound(); break;
3192 }
3193 if (fit) {
3194 fitDetails.setCatIndex("type", i);
3195 fitMeta.addClone(RooStringVar(TString::Format("%s.name", fitDetails.getCatLabel("type")),
3196 fitDetails.getCatLabel("type"), fit->GetName()));
3197 // fitDetails.setStringValue("name",fit->GetName());
3198 fitDetails.setRealValue("status", fit->status());
3199 fitDetails.setRealValue("minNll", fit->minNll());
3200 fitDetails.setRealValue("edm", fit->edm());
3201 fitDetails.setRealValue("covQual", fit->covQual());
3202 fitDS->add(fitDetails);
3203 }
3204 }
3205 fitDS->setGlobalObservables(fitMeta);
3206
3207 out.SetFitInfo(fitDS);
3208
3211 nullMeta.addClone(*coords);
3212 nullDetails.addClone(RooRealVar("seed", "Toy Seed", 0));
3213 nullDetails.addClone(RooRealVar("ts", "test statistic value", 0));
3214 nullDetails.addClone(RooRealVar("weight", "weight", 1));
3215 auto nullToyDS = new RooDataSet("nullToys", "nullToys", nullDetails, RooFit::WeightVar("weight"));
3216 nullToyDS->setGlobalObservables(nullMeta);
3217 if (!nullToys.empty()) {
3218
3219 std::vector<double> values;
3220 std::vector<double> weights;
3221 values.reserve(nullToys.size());
3222 weights.reserve(nullToys.size());
3223
3224 for (auto &t : nullToys) {
3225 values.push_back(std::get<1>(t));
3226 weights.push_back(std::get<2>(t));
3227 nullDetails.setRealValue("seed", std::get<0>(t));
3228 nullDetails.setRealValue("ts", std::get<1>(t));
3229 nullToyDS->add(nullDetails, std::get<2>(t));
3230 }
3231 out.SetNullDistribution(new RooStats::SamplingDistribution("null", "Null dist", values, weights, tsTitle()));
3232#if ROOT_VERSION_CODE < ROOT_VERSION(6, 27, 00)
3233 out.fNullPValue = pNull_toys().first; // technically set above
3234 out.fNullPValueError =
3235 pNull_toys().second; // overrides binomial error used in SamplingDistribution::IntegralAndError
3236#else
3237 out.SetNullPValue(pNull_toys().first); // technically set above
3238 out.SetNullPValueError(
3239 pNull_toys().second); // overrides binomial error used in SamplingDistribution::IntegralAndError
3240#endif
3241 } else {
3242#if ROOT_VERSION_CODE < ROOT_VERSION(6, 27, 00)
3243 out.fNullPValue = pNull_asymp().first;
3244 out.fNullPValueError = pNull_asymp().second;
3245#else
3246 out.SetNullPValue(pNull_asymp().first);
3247 out.SetNullPValueError(pNull_asymp().second);
3248#endif
3249 }
3250 out.SetNullDetailedOutput(nullToyDS);
3251
3252 if (!altToys.empty()) {
3253 std::vector<double> values;
3254 std::vector<double> weights;
3255 values.reserve(altToys.size());
3256 weights.reserve(altToys.size());
3259 altDetails.addClone(RooRealVar("seed", "Toy Seed", 0));
3260 altDetails.addClone(RooRealVar("ts", "test statistic value", 0));
3261 altDetails.addClone(RooRealVar("weight", "weight", 1));
3262 auto altToyDS = new RooDataSet("altToys", "altToys", altDetails, RooFit::WeightVar("weight"));
3263 altToyDS->setGlobalObservables(altMeta);
3264 for (auto &t : altToys) {
3265 values.push_back(std::get<1>(t));
3266 weights.push_back(std::get<2>(t));
3267 altDetails.setRealValue("seed", std::get<0>(t));
3268 altDetails.setRealValue("ts", std::get<1>(t));
3269 altToyDS->add(altDetails, std::get<2>(t));
3270 }
3271 out.SetAltDistribution(new RooStats::SamplingDistribution("alt", "Alt dist", values, weights, tsTitle()));
3272 out.SetAltDetailedOutput(altToyDS);
3273#if ROOT_VERSION_CODE < ROOT_VERSION(6, 27, 00)
3274 out.fAlternatePValue = pAlt_toys().first; // technically set above
3275 out.fAlternatePValueError =
3276 pAlt_toys().second; // overrides binomial error used in SamplingDistribution::IntegralAndError
3277#else
3278 out.SetAltPValue(pAlt_toys().first); // technically set above
3279 out.SetAltPValueError(
3280 pAlt_toys().second); // overrides binomial error used in SamplingDistribution::IntegralAndError
3281#endif
3282
3283 } else if (fPllType != xRooFit::Asymptotics::Unknown) {
3284#if ROOT_VERSION_CODE < ROOT_VERSION(6, 27, 00)
3285 out.fAlternatePValue = pAlt_asymp().first;
3286 out.fAlternatePValueError = pAlt_asymp().second;
3287#else
3288 out.SetAltPValue(pAlt_asymp().first);
3289 out.SetAltPValueError(pAlt_asymp().second);
3290#endif
3291 }
3292
3293 if (setReadonly) {
3294 nllVar->get()->setAttribute("readOnly", false);
3295 }
3296
3297 return out;
3298}
3299
3300std::string cling::printValue(const xRooNLLVar::xValueWithError *v)
3301{
3302 if (!v)
3303 return "xValueWithError: nullptr\n";
3304 return Form("%f +/- %f", v->first, v->second);
3305}
3306std::string cling::printValue(const std::map<std::string, xRooNLLVar::xValueWithError> *m)
3307{
3308 if (!m)
3309 return "nullptr\n";
3310 std::string out = "{\n";
3311 for (auto [k, v] : *m) {
3312 out += "\"" + k + "\" => " + printValue(&v) + "\n";
3313 }
3314 out += "}\n";
3315 return out;
3316}
3317
#define SafeDelete(p)
Definition RConfig.hxx:533
#define b(i)
Definition RSha256.hxx:100
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
#define g(i)
Definition RSha256.hxx:105
#define a(i)
Definition RSha256.hxx:99
#define h(i)
Definition RSha256.hxx:106
#define e(i)
Definition RSha256.hxx:103
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
@ kRed
Definition Rtypes.h:67
@ kBlue
Definition Rtypes.h:67
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gDirectory
Definition TDirectory.h:385
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
Definition TError.cxx:241
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:208
winID h TVirtualViewer3D TVirtualGLPainter p
winID h TVirtualViewer3D vv
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t target
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t np
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
@ kCanDelete
Definition TObject.h:370
#define gROOT
Definition TROOT.h:411
static char * Format(const char *format, va_list ap)
Format a string in a circular formatting buffer (using a printf style format descriptor).
Definition TString.cxx:2448
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2495
R__EXTERN TStyle * gStyle
Definition TStyle.h:442
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
#define gPad
AutoRestorer(const RooAbsCollection &s, xRooNLLVar *nll=nullptr)
RooArgSet fPars
TString fOldTitle
TString fOldName
std::pair< std::shared_ptr< RooAbsData >, std::shared_ptr< const RooAbsCollection > > fOldData
xRooNLLVar * fNll
std::unique_ptr< RooAbsCollection > fSnap
static double k(const IncompatFunc &compatRegions, double pValue, double poiVal, double poiPrimeVal, double sigma_mu=0, double mu_low=-std::numeric_limits< double >::infinity(), double mu_high=std::numeric_limits< double >::infinity())
static int CompatFactor(const IncompatFunc &func, double mu_hat)
static double PValue(const IncompatFunc &compatRegions, double k, double mu, double mu_prime, double sigma_mu=0, double mu_low=-std::numeric_limits< double >::infinity(), double mu_high=std::numeric_limits< double >::infinity())
static std::shared_ptr< const RooFitResult > minimize(RooAbsReal &nll, const std::shared_ptr< ROOT::Fit::FitConfig > &fitConfig=nullptr, const std::shared_ptr< RooLinkedList > &nllOpts=nullptr)
Definition xRooFit.cxx:722
static std::pair< std::shared_ptr< RooAbsData >, std::shared_ptr< const RooAbsCollection > > generateFrom(RooAbsPdf &pdf, const RooFitResult &fr, bool expected=false, int seed=0)
Definition xRooFit.cxx:150
static std::shared_ptr< ROOT::Fit::FitConfig > createFitConfig()
Definition xRooFit.cxx:487
double impact(const char *poi, const char *np, bool up=true, bool prefit=false, bool approx=false)
xRooFitResult ifit(const char *np, bool up, bool prefit=false)
double conditionalError(const char *poi, const char *nps, bool up=true, bool approx=false)
RooArgList ranknp(const char *poi, bool up=true, bool prefit=false, double approxThreshold=std::numeric_limits< double >::infinity())
xRooFitResult cfit(const char *poiValues, const char *alias=nullptr)
std::shared_ptr< RooStats::HypoTestResult > hypoTestResult
Definition xRooNLLVar.h:280
std::shared_ptr< const RooFitResult > retrieveFit(int type)
std::vector< std::tuple< int, double, double > > altToys
Definition xRooNLLVar.h:277
std::shared_ptr< const RooAbsCollection > coords
Definition xRooNLLVar.h:263
std::shared_ptr< const RooFitResult > cfit_lbound(bool readOnly=false)
void Draw(Option_t *opt="") override
Default Draw method for all objects.
xValueWithError ts_toys(double nSigma=std::numeric_limits< double >::quiet_NaN())
std::shared_ptr< const RooFitResult > fUfit
Definition xRooNLLVar.h:265
xRooHypoPoint(std::shared_ptr< RooStats::HypoTestResult > htr=nullptr, const RooAbsCollection *_coords=nullptr)
xValueWithError sigma_mu(bool readOnly=false)
xValueWithError pAlt_asymp(double nSigma=std::numeric_limits< double >::quiet_NaN())
std::vector< std::tuple< int, double, double > > nullToys
Definition xRooNLLVar.h:275
std::shared_ptr< xRooHypoPoint > asimov(bool readOnly=false)
xValueWithError pAlt_toys(double nSigma=std::numeric_limits< double >::quiet_NaN())
std::shared_ptr< const RooFitResult > ufit(bool readOnly=false)
void Print(Option_t *opt="") const override
Print TNamed name and title.
std::shared_ptr< const RooFitResult > cfit_null(bool readOnly=false)
std::pair< std::shared_ptr< RooAbsData >, std::shared_ptr< const RooAbsCollection > > data()
xValueWithError pCLs_asymp(double nSigma=std::numeric_limits< double >::quiet_NaN())
std::shared_ptr< const RooFitResult > cfit_alt(bool readOnly=false)
size_t addToys(bool alt, int nToys, int initialSeed=0, double target=std::numeric_limits< double >::quiet_NaN(), double target_nSigma=std::numeric_limits< double >::quiet_NaN(), bool targetCLs=false, double relErrThreshold=2., size_t maxToys=10000)
void addAltToys(int nToys=1, int seed=0, double target=std::numeric_limits< double >::quiet_NaN(), double target_nSigma=std::numeric_limits< double >::quiet_NaN())
void addCLsToys(int nToys=1, int seed=0, double target=std::numeric_limits< double >::quiet_NaN(), double target_nSigma=std::numeric_limits< double >::quiet_NaN())
xValueWithError pX_toys(bool alt, double nSigma=std::numeric_limits< double >::quiet_NaN())
void addNullToys(int nToys=1, int seed=0, double target=std::numeric_limits< double >::quiet_NaN(), double target_nSigma=std::numeric_limits< double >::quiet_NaN())
xValueWithError ts_asymp(double nSigma=std::numeric_limits< double >::quiet_NaN())
xValueWithError pNull_asymp(double nSigma=std::numeric_limits< double >::quiet_NaN())
xValueWithError pNull_toys(double nSigma=std::numeric_limits< double >::quiet_NaN())
std::shared_ptr< RooArgSet > pars() const
Definition xRooNLLVar.h:325
bool AddModel(const xRooNode &pdf, const char *validity="")
std::map< std::shared_ptr< xRooNode >, std::shared_ptr< xRooNLLVar > > fNlls
Definition xRooNLLVar.h:390
std::set< std::pair< std::shared_ptr< RooArgList >, std::shared_ptr< xRooNode > > > fPdfs
Definition xRooNLLVar.h:392
This xRooNLLVar object has several special methods, e.g.
Definition xRooNLLVar.h:59
std::shared_ptr< RooAbsCollection > fFuncGlobs
Definition xRooNLLVar.h:508
std::shared_ptr< const RooAbsCollection > fGlobs
Definition xRooNLLVar.h:501
std::shared_ptr< RooLinkedList > fOpts
Definition xRooNLLVar.h:503
std::shared_ptr< RooAbsReal > func() const
std::set< std::string > binnedChannels() const
ROOT::Math::IOptions * fitConfigOptions()
RooConstraintSum * constraintTerm() const
std::shared_ptr< ROOT::Fit::FitConfig > fFitConfig
Definition xRooNLLVar.h:504
xRooHypoSpace hypoSpace(const char *parName, int nPoints, double low, double high, double alt_value=std::numeric_limits< double >::quiet_NaN(), const xRooFit::Asymptotics::PLLType &pllType=xRooFit::Asymptotics::Unknown, int tsType=0)
TObject * Scan(const RooArgList &scanPars, const std::vector< std::vector< double > > &coords, const RooArgList &profilePars=RooArgList())
std::shared_ptr< RooAbsCollection > fConstVars
Definition xRooNLLVar.h:507
xRooNLLVar(RooAbsPdf &pdf, const std::pair< RooAbsData *, const RooAbsCollection * > &data, const RooLinkedList &nllOpts=RooLinkedList())
std::shared_ptr< RooAbsPdf > pdf() const
Definition xRooNLLVar.h:452
std::pair< std::shared_ptr< RooAbsData >, std::shared_ptr< const RooAbsCollection > > generate(bool expected=false, int seed=0)
std::pair< std::shared_ptr< RooAbsData >, std::shared_ptr< const RooAbsCollection > > getData() const
double getEntryVal(size_t entry) const
std::shared_ptr< RooAbsCollection > fFuncVars
Definition xRooNLLVar.h:506
double getEntryBinWidth(size_t entry) const
std::shared_ptr< ROOT::Fit::FitConfig > fitConfig()
std::shared_ptr< RooArgSet > pars(bool stripGlobalObs=true) const
void SetOption(const RooCmdArg &opt)
std::shared_ptr< RooAbsData > fData
Definition xRooNLLVar.h:500
std::shared_ptr< RooAbsPdf > fPdf
Definition xRooNLLVar.h:499
bool setData(const std::pair< std::shared_ptr< RooAbsData >, std::shared_ptr< const RooAbsCollection > > &_data)
xRooHypoPoint hypoPoint(const char *parName, double value, double alt_value=std::numeric_limits< double >::quiet_NaN(), const xRooFit::Asymptotics::PLLType &pllType=xRooFit::Asymptotics::Unknown)
xRooFitResult minimize(const std::shared_ptr< ROOT::Fit::FitConfig > &=nullptr)
The xRooNode class is designed to wrap over a TObject and provide functionality to aid with interacti...
Definition xRooNode.h:52
Generic interface for defining configuration options of a numerical algorithm.
Definition IOptions.h:28
void SetValue(const char *name, double val)
generic methods for retrieving options
Definition IOptions.h:42
virtual void SetNamedValue(const char *, const char *)
Definition IOptions.cxx:50
const_iterator begin() const
const_iterator end() const
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:76
bool dependsOn(const RooAbsCollection &serverList, const RooAbsArg *ignoreArg=nullptr, bool valueOnly=false) const
Test whether we depend on (ie, are served by) any object in the specified collection.
Abstract base class for objects that represent a discrete value that can be set from the outside,...
A space to attach TBranches.
Abstract container object that can hold multiple RooAbsArg objects.
Storage_t const & get() const
Const access to the underlying stl container.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
void setAttribAll(const Text_t *name, bool value=true)
Set given attribute in each element of the collection by calling each elements setAttribute() functio...
Int_t index(const RooAbsArg *arg) const
Returns index of given arg, or -1 if arg is not in the collection.
Storage_t::size_type size() const
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
virtual const RooArgSet * get() const
Definition RooAbsData.h:101
virtual Int_t numEntries() const
Return number of entries in dataset, i.e., count unweighted entries.
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:32
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
virtual void setVal(double value)=0
Set the current value of the object. Needs to be overridden by implementations.
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:107
virtual bool setData(RooAbsData &, bool=true)
Definition RooAbsReal.h:373
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition RooArgList.h:110
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Object to represent discrete states.
Definition RooCategory.h:28
Named container for two doubles, two integers two object points and three string pointers that can be...
Definition RooCmdArg.h:26
double getDouble(Int_t idx) const
Return double stored in slot idx.
Definition RooCmdArg.h:92
Int_t getInt(Int_t idx) const
Definition RooCmdArg.h:87
TObject * Clone(const char *newName=nullptr) const override
Make a clone of an object using the Streamer facility.
Definition RooCmdArg.h:58
const char * getString(Int_t idx) const
Return string stored in slot idx.
Definition RooCmdArg.h:96
Calculates the sum of the -(log) likelihoods of a set of RooAbsPfs that represent constraint function...
Container class to hold unbinned data.
Definition RooDataSet.h:32
RooFitResult is a container class to hold the input and output of a PDF fit to a dataset.
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
Collection class for internal use, storing a collection of RooAbsArg pointers in a doubly linked list...
static RooMsgService & instance()
Return reference to singleton instance.
Poisson pdf.
Definition RooPoisson.h:19
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition RooRandom.cxx:47
Variable that can be changed from the outside.
Definition RooRealVar.h:37
void setVal(double value) override
Set value of variable to 'value'.
Facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset.
HypoTestResult is a base class for results from hypothesis tests.
This class simply holds a sampling distribution of some test statistic.
A RooAbsArg implementing string values.
Draw all kinds of Arrows.
Definition TArrow.h:29
virtual TArrow * DrawArrow(Double_t x1, Double_t y1, Double_t x2, Double_t y2, Float_t arrowsize=0, Option_t *option="")
Draw this arrow with new coordinates.
Definition TArrow.cxx:133
static TCanvas * MakeDefCanvas()
Static function to build a default canvas.
Definition TCanvas.cxx:1514
const char * AsString() const
Return the date & time as a string (ctime() format).
Definition TDatime.cxx:101
Describe directory structure in memory.
Definition TDirectory.h:45
virtual TKey * FindKeyAny(const char *) const
Definition TDirectory.h:199
Class to handle efficiency histograms.
Definition TEfficiency.h:29
void FillWeighted(Bool_t bPassed, Double_t weight, Double_t x, Double_t y=0, Double_t z=0)
This function is used for filling the two histograms with a weight.
Double_t GetEfficiencyErrorUp(Int_t bin) const
Returns the upper error on the efficiency in the given global bin.
void Fill(Bool_t bPassed, Double_t x, Double_t y=0, Double_t z=0)
This function is used for filling the two histograms.
Graphics object made of three arrays X, Y and Z with the same number of points each.
Definition TGraph2D.h:41
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
virtual void Add(TF1 *f, Double_t c1=1)
Performs the operation: y = y + c1*f(x,y) Errors are not recalculated.
Definition TGraph.cxx:623
void SetName(const char *name="") override
Set graph name.
Definition TGraph.cxx:2329
void Draw(Option_t *chopt="") override
Draw this graph with its current attributes.
Definition TGraph.cxx:832
void SetTitle(const char *title="") override
Change (i.e.
Definition TGraph.cxx:2345
void SetNameTitle(const char *name="", const char *title="") override
Set graph name and title.
Definition TGraph.cxx:2365
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:927
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:109
This class displays a legend box (TPaveText) containing several legend entries.
Definition TLegend.h:23
void Draw(Option_t *option="") override
Draw this legend with its current attributes.
Definition TLegend.cxx:424
Use the TLine constructor to create a simple line.
Definition TLine.h:22
A TMultiGraph is a collection of TGraph (or derived) objects.
Definition TMultiGraph.h:34
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
Mother of all ROOT objects.
Definition TObject.h:41
virtual void Delete(Option_t *option="")
Delete this object.
Definition TObject.cxx:267
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:864
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:293
Regular expression class.
Definition TRegexp.h:31
Stopwatch class.
Definition TStopwatch.h:28
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
void Continue()
Resume a stopped stopwatch.
void Reset()
Definition TStopwatch.h:52
Provides iteration through tokens of a given string.
Definition TPRegexp.h:143
Bool_t NextToken()
Get the next token, it is stored in this TString.
Basic string class.
Definition TString.h:138
Ssiz_t Length() const
Definition TString.h:425
Double_t Atof() const
Return floating-point value contained in string.
Definition TString.cxx:2060
const char * Data() const
Definition TString.h:384
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition TString.h:631
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2384
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:640
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:659
Float_t GetPadRightMargin() const
Definition TStyle.h:216
Float_t GetPadLeftMargin() const
Definition TStyle.h:215
Float_t GetPadBottomMargin() const
Definition TStyle.h:213
Float_t GetPadTopMargin() const
Definition TStyle.h:214
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition TSystem.cxx:414
This class defines a UUID (Universally Unique IDentifier), also known as GUIDs (Globally Unique IDent...
Definition TUUID.h:42
TDatime GetTime() const
Get time from UUID.
Definition TUUID.cxx:669
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
RooCmdArg WeightVar(const char *name="weight", bool reinterpretAsWeight=false)
RooCmdArg GlobalObservables(Args_t &&... argsOrArgSet)
RooCmdArg GlobalObservablesSource(const char *sourceName)
double gaussian_pdf(double x, double sigma=1, double x0=0)
Probability density function of the normal (Gaussian) distribution with mean x0 and standard deviatio...
const Int_t n
Definition legend1.C:16
TGraphErrors * gr
Definition legend1.C:25
double gaussian_cdf(double x, double sigma=1, double x0=0)
Alternative name for same function.
double gaussian_cdf_c(double x, double sigma=1, double x0=0)
Alternative name for same function.
static constexpr auto NumIntegration
Alias of MsgLevel::NumericIntegration for backwards compatibility.
@ NumericIntegration
Double_t Prob(Double_t chi2, Int_t ndf)
Computation of the probability for a certain Chi-squared (chi2) and number of degrees of freedom (ndf...
Definition TMath.cxx:637
Double_t Poisson(Double_t x, Double_t par)
Computes the Poisson distribution function for (x,par).
Definition TMath.cxx:587
Double_t LnGamma(Double_t z)
Computation of ln[gamma(z)] for all z.
Definition TMath.cxx:509
#define BEGIN_XROOFIT_NAMESPACE
Definition Config.h:24
#define END_XROOFIT_NAMESPACE
Definition Config.h:25
static const char * what
Definition stlLoader.cc:5
th1 Draw()
TMarker m
Definition textangle.C:8
TLine l
Definition textangle.C:4
std::string collectionContents(const RooAbsCollection &coll)
#define GETWS(a)
#define GETWSSETS(w)