Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
xRooHypoSpace.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
14
15#include "RooArgSet.h"
16#include "RooArgList.h"
17#include "RooRealVar.h"
18#include "RooFitResult.h"
19#include "RooConstVar.h"
20#include "RooAbsPdf.h"
21
22#include "TCanvas.h"
23#include "TStopwatch.h"
24#include "TSystem.h"
25#include "TPRegexp.h"
26#include "TMemFile.h"
27#include "TROOT.h"
28#include "RooDataSet.h"
29#include "TKey.h"
30#include "TFile.h"
31#include "TGraphErrors.h"
32#include "TMultiGraph.h"
33#include "TH1F.h"
34#include "TStyle.h"
35#include "TLegend.h"
36#include "TLine.h"
38#include "TEnv.h"
39
40#include "./PythonInterface.h"
41
43
44xRooNLLVar::xRooHypoSpace::xRooHypoSpace(const char *name, const char *title)
45 : TNamed(name, title), fPars(std::make_shared<RooArgSet>())
46{
47 if (name == nullptr || strlen(name) == 0) {
48 SetName(TUUID().AsString());
49 }
50}
51
53 : fPars(std::make_shared<RooArgSet>())
54{
55 if (!result)
56 return;
57
59
60 fPars->addClone(*std::unique_ptr<RooAbsCollection>(result->GetParameters()));
61 double spaceSize = 1;
62 for (auto p : *fPars) {
63 auto v = dynamic_cast<RooRealVar *>(p);
64 if (!v)
65 continue;
66 spaceSize *= (v->getMax() - v->getMin());
67 }
68 for (int i = 0; i < result->ArraySize(); i++) {
69 auto point = result->GetResult(i);
70 double xVal = result->GetXValue(i);
71 double ssize = spaceSize;
72 for (auto p : *fPars) {
73 auto v = dynamic_cast<RooRealVar *>(p);
74 if (!v)
75 continue;
76 ssize /= (v->getMax() - v->getMin());
77 double remain = std::fmod(xVal, ssize);
78 v->setVal((xVal - remain) / ssize);
79 xVal = remain;
80 }
81 emplace_back(xRooHypoPoint(std::make_shared<RooStats::HypoTestResult>(*point), fPars.get()));
82 }
83 // add any pars we might have missed
84 for (auto &p : *this) {
85 for (auto a : *p.coords) {
86 if (!fPars->find(a->GetName()))
87 fPars->addClone(*a);
88 }
89 }
90}
91
92std::shared_ptr<xRooNode> xRooNLLVar::xRooHypoSpace::pdf(const char *parValues) const
93{
94 return pdf(toArgs(parValues));
95}
96
97std::shared_ptr<xRooNode> xRooNLLVar::xRooHypoSpace::pdf(const RooAbsCollection &parValues) const
98{
100 rhs.add(parValues);
101 rhs.sort();
102
103 std::shared_ptr<xRooNode> out = nullptr;
104
105 for (auto &[_range, _pdf] : fPdfs) {
106 // any pars not in rhs are assumed to have infinite range in rhs
107 // and vice versa
108 bool collision = true;
109 for (auto &_lhs : *_range) {
110 auto _rhs = rhs.find(*_lhs);
111 if (!_rhs)
112 continue;
113 if (auto v = dynamic_cast<RooRealVar *>(_rhs); v) {
114 if (auto v2 = dynamic_cast<RooRealVar *>(_lhs)) {
115 if (!(v->getMin() <= v2->getMax() && v2->getMin() <= v->getMax())) {
116 collision = false;
117 break;
118 }
119 } else if (auto c2 = dynamic_cast<RooConstVar *>(_lhs)) {
120 if (!(v->getMin() <= c2->getVal() && c2->getVal() <= v->getMax())) {
121 collision = false;
122 break;
123 }
124 }
125 } else if (auto c = dynamic_cast<RooConstVar *>(_rhs); c) {
126 if (auto v2 = dynamic_cast<RooRealVar *>(_lhs)) {
127 if (!(c->getVal() <= v2->getMax() && v2->getMin() <= c->getVal())) {
128 collision = false;
129 break;
130 }
131 } else if (auto c2 = dynamic_cast<RooConstVar *>(_lhs)) {
132 if (!(c->getVal() == c2->getVal())) {
133 collision = false;
134 break;
135 }
136 }
137 }
138 }
139 if (collision) {
140 if (out) {
141 throw std::runtime_error("Multiple pdf possibilities");
142 }
143 out = _pdf;
144 }
145 }
146
147 return out;
148}
149
151{
152
153 RooArgList out;
154
155 TStringToken pattern(str, ";");
156 while (pattern.NextToken()) {
157 TString s = pattern;
158 // split by "=" sign
159 auto _idx = s.Index('=');
160 if (_idx == -1)
161 continue;
162 TString _name = s(0, _idx);
163 TString _val = s(_idx + 1, s.Length());
164
165 if (_val.IsFloat()) {
166 out.addClone(RooConstVar(_name, _name, _val.Atof()));
167 } else if (_val.BeginsWith('[')) {
168 _idx = _val.Index(',');
169 if (_idx == -1)
170 continue;
171 TString _min = _val(0, _idx);
172 TString _max = _val(_idx + 1, _val.Length() - _idx - 2);
173 out.addClone(RooRealVar(_name, _name, _min.Atof(), _max.Atof()));
174 }
175 }
176
177 return out;
178}
179
180int xRooNLLVar::xRooHypoSpace::AddPoints(const char *parName, size_t nPoints, double low, double high)
181{
182 if (nPoints == 0)
183 return nPoints;
184
185 auto _par = dynamic_cast<RooAbsRealLValue *>(fPars->find(parName));
186 if (!_par)
187 throw std::runtime_error("Unknown parameter");
188 _par->setAttribute("axis");
189
190 if (low < _par->getMin()) {
191 Warning("AddPoints", "low edge of hypoSpace %g below lower bound of parameter: %g. Changing to lower bound", low,
192 _par->getMin());
193 low = _par->getMin();
194 }
195 if (high > _par->getMax()) {
196 Warning("AddPoints", "high edge of hypoSpace %g above upper bound of parameter: %g. Changing to upper bound",
197 high, _par->getMax());
198 high = _par->getMax();
199 }
200
201 if (nPoints == 1) {
202 _par->setVal((high + low) * 0.5);
203 AddPoint();
204 return nPoints;
205 }
206
207 double step = (high - low) / (nPoints - 1);
208 if (step <= 0)
209 throw std::runtime_error("Invalid steps");
210
211 for (size_t i = 0; i < nPoints; i++) {
212 _par->setVal((i == nPoints - 1) ? high : (low + step * i));
213 AddPoint();
214 }
215 return nPoints;
216}
217
219{
220 if (axes().empty()) {
221 // set the first poi as the axis variable to scan
222 if (poi().empty()) {
223 throw std::runtime_error("No POI to scan");
224 } else {
225 poi().first()->setAttribute("axis");
226 }
227 }
228
229 if (empty()) {
230 // promote all axes to being poi and demote all non-axes to non-poi
231 poi().setAttribAll("poi", false);
232 axes().setAttribAll("poi");
233 }
234
235 return AddPoint(TString::Format("%s=%f", axes().first()->GetName(), value));
236}
237
238int xRooNLLVar::xRooHypoSpace::scan(const char *type, size_t nPoints, double low, double high,
239 const std::vector<double> &nSigmas, double relUncert)
240{
241
243 sType.ToLower();
244 if (sType.Contains("cls") && !sType.Contains("pcls"))
245 sType.ReplaceAll("cls", "pcls");
246 if (!sType.Contains("pcls") && !sType.Contains("ts") && !sType.Contains("pnull") && !sType.Contains("plr")) {
247 throw std::runtime_error("scan type must be equal to one of: plr, cls, ts, pnull");
248 }
249
250 // will scan the first axes variable ... if there is none, specify the first poi as the axis var
251 if (axes().empty()) {
252 // set the first poi as the axis variable to scan
253 if (poi().empty()) {
254 throw std::runtime_error("No POI to scan");
255 } else {
256 poi().first()->setAttribute("axis");
257 }
258 }
259
260 // promote all axes to being poi and demote all non-axes to non-poi
261 poi().setAttribAll("poi", false);
262 axes().setAttribAll("poi");
263
264 auto p = dynamic_cast<RooRealVar *>(axes().first());
265 if (!p) {
266 throw std::runtime_error(TString::Format("%s not scannable", axes().first()->GetName()));
267 }
268
269 if (sType.Contains("cls")) {
270 if (empty() && relUncert == std::numeric_limits<double>::infinity()) {
271 // use default uncertainty precision of 10%
272 ::Info("xRooHypoSpace::scan", "Using default precision of 10%% for auto-scan");
273 relUncert = 0.1;
274 }
275 for (auto a : axes()) {
276 if (!a->hasRange("physical")) {
277 ::Info("xRooHypoSpace::scan", "No physical range set for %s, setting to [0,inf]", p->GetName());
278 dynamic_cast<RooRealVar *>(a)->setRange("physical", 0, std::numeric_limits<double>::infinity());
279 }
280 if (!a->getStringAttribute("altVal") || !strlen(p->getStringAttribute("altVal"))) {
281 ::Info("xRooHypoSpace::scan", "No altVal set for %s, setting to 0", a->GetName());
282 a->setStringAttribute("altVal", "0");
283 }
284 // ensure range straddles altVal
285 double altVal = TString(a->getStringAttribute("altVal")).Atof();
286 auto v = dynamic_cast<RooRealVar *>(a);
287 if (v->getMin() >= altVal) {
288 ::Info("xRooHypoSpace::scan", "range of POI does not straddle alt value, adjusting minimum to %g",
289 altVal - 1e-5);
290 v->setMin(altVal - 1e-5);
291 }
292 if (v->getMax() <= altVal) {
293 ::Info("xRooHypoSpace::scan", "range of POI does not straddle alt value, adjusting maximum to %g",
294 altVal + 1e-5);
295 v->setMax(altVal + 1e-5);
296 }
297 for (auto &[pdf, nll] : fNlls) {
298 if (auto _v = dynamic_cast<RooRealVar *>(nll->pars()->find(*a))) {
299 _v->setRange(v->getMin(), v->getMax());
300 }
301 }
302 }
303 } else if (sType.Contains("plr")) {
304 // force use of two-sided test statistic for any new points
305 fTestStatType = xRooFit::Asymptotics::TwoSided;
306 sType.ReplaceAll("plr", "ts");
307 } else if (sType.Contains("pnull") && fTestStatType == xRooFit::Asymptotics::Unknown) {
308 // for pnull (aka discovery) "scan" (may just be a single point) default to use of
309 // uncapped test stat
310 fTestStatType = xRooFit::Asymptotics::Uncapped;
311 // and ensure altVal is set
312 for (auto a : axes()) {
313 if (!a->getStringAttribute("altVal") || !strlen(p->getStringAttribute("altVal"))) {
314 ::Info("xRooHypoSpace::scan", "No altVal set for %s, setting to 1", a->GetName());
315 a->setStringAttribute("altVal", "1");
316 }
317 }
318 }
319
320 if (/*high < low ||*/ (high == low && nPoints != 1)) {
321 // take from parameter (will be either the defaults from the construction of the hypoSpace or whatever last scan
322 // was
323 low = p->getMin("scan");
324 high = p->getMax("scan");
325 }
326 if (!std::isnan(low) && !std::isnan(high) && !(std::isinf(low) && std::isinf(high))) {
327 p->setRange("scan", std::min(low, high), std::max(low, high));
328 }
329
330 bool doObs = false;
331 for (auto nSigma : nSigmas) {
332 if (std::isnan(nSigma)) {
333 doObs = true;
334 break;
335 }
336 }
337
338 if (fNlls.empty()) {
339 // this happens when loaded hypoSpace from a hypoSpaceInverterResult
340 // set relUncert to infinity so that we don't test any new points
341 relUncert = std::numeric_limits<double>::infinity(); // no NLL available so just get whatever limit we can
342
343 // if any of the defined points are 'expected' data don't do obs
344 // for(auto& hp : *this) {
345 // if(hp.isExpected) {
346 // doObs = false; break;
347 // }
348 // }
349 } else {
350#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 26, 00)
351// bool allGen = true;
352// for (auto &[pdf, nll]: fNlls) {
353// auto _d = dynamic_cast<RooDataSet *>(nll->data());
354// if (!_d || !_d->weightVar() || !_d->weightVar()->getStringAttribute("fitResult") ||
355// !_d->weightVar()->getAttribute("expected")) {
356// allGen = false;
357// break;
358// }
359// }
360// if (allGen)
361// doObs = false;
362#endif
363 }
364
365 // create a fitDatabase if required
367 if (fFitDb) {
368 // move to the db, and unlock it if this is a TMemFile
369 fFitDb->cd();
370 if (auto myDb = dynamic_cast<TMemFile *>(fFitDb.get())) {
371 // need to unlock the database
372 myDb->SetWritable(true);
373 }
374 }
375 if (!gDirectory || !gDirectory->IsWritable()) {
376 // locate a TMemFile in the open list of files and move to that
377 // or create one if cannot find
378 /*for (auto file : *gROOT->GetListOfFiles()) {
379 if (auto f = dynamic_cast<TMemFile *>(file)) {
380 f->cd();
381 break;
382 }
383 }
384 if (!gDirectory || !gDirectory->IsWritable()) {
385 new TMemFile("fitDatabase", "RECREATE");
386 }*/
387 // now we create a TMemFile of our own, so that we don't get in the way of other hypoSpaces
388 fFitDb = std::shared_ptr<TDirectory>(
389 new TMemFile(TString::Format("fitDatabase_%s", TUUID().AsString()), "RECREATE"), [](TDirectory *) {});
390 // db can last longer than the hypoSpace, so that the fits are fully available in the browser
391 // if a scan was initiated through the browser. If user wants to cleanup they can do manually
392 // through root's GetListOfFiles()
393 // would like to clean it up ourself when the hypoSpace is destroyed, but would need way to keep alive for the
394 // browser
395 }
396
397 int out = 0;
398
399 // enable visualizing by default if scanning in non-batch mode
400 if (!gROOT->IsBatch() && !sType.Contains("visualize"))
401 sType += " visualize";
402
403 if (nPoints == 0) {
404 // automatic scan
405 if (sType.Contains("cls")) {
406 if (p->hasRange("scan")) {
407 ::Info("xRooHypoSpace::scan", "Using %s scan range: %g - %g", p->GetName(), p->getMin("scan"),
408 p->getMax("scan"));
409 }
410 for (double nSigma : nSigmas) {
411 xValueWithError res(std::make_pair(0., 0.));
412 if (std::isnan(nSigma)) {
413 if (!doObs)
414 continue;
415 res = findlimit(TString::Format("%s obs", sType.Data()), relUncert);
416 } else {
417 res =
418 findlimit(TString::Format("%s exp%s%d", sType.Data(), nSigma > 0 ? "+" : "", int(nSigma)), relUncert);
419 }
420 if (std::isnan(res.first) || std::isnan(res.second)) {
421 out = 1;
422 } else if (std::isinf(res.second)) {
423 out = 2;
424 }
425 }
426 } else if (sType.Contains("ts")) {
427 // add a point at the current poi value
428 // use that to determine ufit value and use uncertainty to set an approx range
429 AddPoint(TString::Format("%s=%g", poi().first()->GetName(), poi().getRealValue(poi().first()->GetName())));
430 graphs(sType); // triggers computation
431 if (back().status() != 0) {
432 out += 1;
433 } else {
434 // fit was ok, so use the values to determine an appropriate range
435 low = std::max(low, back().mu_hat().getVal() - back().mu_hat().getError() * 3);
436 high = std::min(high, back().mu_hat().getVal() + back().mu_hat().getError() * 3);
437 nPoints = 20;
438 double step = (high - low) / (nPoints - 1);
439 for (size_t i = 0; i < nPoints; i++) {
440 AddPoint(TString::Format("%s=%g", poi().first()->GetName(), low + step * i));
441 graphs(sType); // triggers computation
442 if (back().status() != 0)
443 out += 1;
444 }
445 }
446 } else {
447 throw std::runtime_error(TString::Format("Automatic scanning not yet supported for %s", type));
448 }
449 } else {
450 // add the required points and then compute the required value
451 if (nPoints == 1) {
452 AddPoint(TString::Format("%s=%g", poi().first()->GetName(), (high + low) / 2.));
453 graphs(sType); // triggers computation
454 if (back().status() != 0)
455 out += 1;
456 } else {
457 double step = (high - low) / (nPoints - 1);
458 for (size_t i = 0; i < nPoints; i++) {
459 AddPoint(TString::Format("%s=%g", poi().first()->GetName(), low + step * i));
460 graphs(sType); // triggers computation
461 if (back().status() != 0)
462 out += 1;
463 }
464 }
465 }
466
467 if (origDir)
468 origDir->cd();
469
470 if (auto myDb = dynamic_cast<TMemFile *>(fFitDb.get())) {
471 // need to lock the database, because if its writable when pyroot closes it causes a crash
472 myDb->SetWritable(false);
473 }
474 return out;
475}
476
477std::map<std::string, xRooNLLVar::xValueWithError>
478xRooNLLVar::xRooHypoSpace::limits(const char *opt, const std::vector<double> &nSigmas, double relUncert)
479{
480
481 if (fNlls.empty()) {
482 // this happens when loaded hypoSpace from a hypoSpaceInverterResult
483 // set relUncert to infinity so that we don't test any new points
484 relUncert = std::numeric_limits<double>::infinity(); // no NLL available so just get whatever limit we can
485 }
486
487 scan(opt, nSigmas, relUncert);
488
489 std::map<std::string, xRooNLLVar::xValueWithError> out;
490 for (auto nSigma : nSigmas) {
491 auto lim = limit(opt, nSigma);
492 if (lim.second < 0)
493 lim.second = -lim.second; // make errors positive for this method
494 out[std::isnan(nSigma) ? "obs" : TString::Format("%d", int(nSigma)).Data()] = xRooFit::matchPrecision(lim);
495 }
496 return out;
497}
498
500{
501 // move to given coords, if any ... will mark them const too
502 std::unique_ptr<RooAbsCollection, std::function<void(RooAbsCollection *)>> _snap(fPars->snapshot(),
503 [&](RooAbsCollection *c) {
504 *fPars = *c;
505 delete c;
506 });
507 TStringToken pattern(coords, ";");
508 while (pattern.NextToken()) {
509 TString s = pattern;
510 // split by "=" sign
511 auto _idx = s.Index('=');
512 if (_idx == -1)
513 continue;
514 TString _name = s(0, _idx);
515 TString _val = s(_idx + 1, s.Length());
516 auto _v = dynamic_cast<RooRealVar *>(fPars->find(_name));
517 if (!_v)
518 continue;
519
520 if (_val.IsFloat()) {
521 _v->setConstant();
522 _v->setVal(_val.Atof());
523 }
524 }
525
526 auto _pdf = pdf();
527
528 if (!_pdf)
529 throw std::runtime_error("no model at coordinates");
530
531 // if (std::unique_ptr<RooAbsCollection>(fPars->selectByAttrib("poi", true))->size() == 0) {
532 // throw std::runtime_error(
533 // "No pars designated as POI - set with pars()->find(<parName>)->setAttribute(\"poi\",true)");
534 // }
535
536 if (fNlls.find(_pdf) == fNlls.end()) {
537 fNlls[_pdf] = std::make_shared<xRooNLLVar>(_pdf->nll("" /*TODO:allow change dataset name and nll opts*/, {}));
538 }
539
540 xRooHypoPoint out;
541
542 out.nllVar = fNlls[_pdf];
543 out.fData = fNlls[_pdf]->getData();
544 out.isExpected = dynamic_cast<RooDataSet *>(out.fData.first.get()) &&
545 dynamic_cast<RooDataSet *>(out.fData.first.get())->weightVar()->getAttribute("expected");
546 // TODO: need to access the genfit of the data and add that to the point, somehow ...
547
548 out.coords.reset(fPars->snapshot()); // should already have altVal prop on poi, and poi labelled
549 // ensure all poi are marked const ... required by xRooHypoPoint behaviour
550 out.poi().setAttribAll("Constant");
551 // and now remove anything that's marked floating
552
553 // do to bug in remove have to ensure not using the hash map otherwise will be doing an invalid read after the
554 // deletion of the owned pars
555 const_cast<RooAbsCollection *>(out.coords.get())->useHashMapForFind(false);
556 const_cast<RooAbsCollection *>(out.coords.get())
557 ->remove(*std::unique_ptr<RooAbsCollection>(out.coords->selectByAttrib("Constant", false)), true, true);
558
559 double value = out.fNullVal();
560 double alt_value = out.fAltVal();
561
562 auto _type = fTestStatType;
563 if (_type == xRooFit::Asymptotics::Unknown) {
564 // decide based on values
565 if (std::isnan(alt_value)) {
567 } else if (value >= alt_value) {
569 } else {
571 }
572 }
573
574 out.fPllType = _type;
575
576 // look for a matching point
577 for (auto &p : *this) {
578 if (p.nllVar != out.nllVar)
579 continue;
580 if (p.fData != out.fData)
581 continue;
582 if (!p.alt_poi().equals(out.alt_poi()))
583 continue;
584 bool match = true;
585 for (auto c : p.alt_poi()) {
586 if (auto v = dynamic_cast<RooAbsReal *>(c);
587 v && std::abs(v->getVal() - out.alt_poi().getRealValue(v->GetName())) > 1e-12) {
588 match = false;
589 break;
590 } else if (auto cat = dynamic_cast<RooAbsCategory *>(c);
591 cat && cat->getCurrentIndex() ==
592 out.alt_poi().getCatIndex(cat->GetName(), std::numeric_limits<int>().max())) {
593 match = false;
594 break;
595 }
596 }
597 if (!match)
598 continue;
599 if (!p.coords->equals(*out.coords))
600 continue;
601 for (auto c : *p.coords) {
602 if (c->getAttribute("poi")) {
603 continue; // check poi below
604 }
605 if (auto v = dynamic_cast<RooAbsReal *>(c);
606 v && std::abs(v->getVal() - out.coords->getRealValue(v->GetName())) > 1e-12) {
607 match = false;
608 break;
609 } else if (auto cat = dynamic_cast<RooAbsCategory *>(c);
610 cat && cat->getCurrentIndex() ==
611 out.alt_poi().getCatIndex(cat->GetName(), std::numeric_limits<int>().max())) {
612 match = false;
613 break;
614 }
615 }
616 if (!match)
617 continue;
618 // if reached here we can copy over the asimov dataset to save re-generating it
619 // first copy over cfit_alt (if its there) because that is also the same since coords and alt_poi values same
620 if (auto cfit = p.cfit_alt(true)) {
621 out.fAlt_cfit = cfit;
622 }
623 if (p.asimov(true) && p.asimov(true)->fData.first && (!out.asimov(true) || !out.asimov(true)->fData.first)) {
624 out.asimov()->fData = p.asimov(true)->fData;
625 }
626 if (!p.poi().equals(out.poi()))
627 continue;
628 for (auto c : p.poi()) {
629 if (auto v = dynamic_cast<RooAbsReal *>(c);
630 v && std::abs(v->getVal() - out.poi().getRealValue(v->GetName())) > 1e-12) {
631 match = false;
632 break;
633 }
634 }
635 if (match) {
636 // found a duplicate point, return that!
637 return p;
638 }
639 }
640
641 std::string coordString;
642 for (auto a : axes()) {
643 coordString += TString::Format("%s=%g", a->GetName(), out.coords->getRealValue(a->GetName()));
644 coordString += ",";
645 }
646 coordString.erase(coordString.end() - 1);
647
649 auto s = TString::Format("Info in <xRooHypoSpace::AddPoint>: Added new point @ %s", coordString.c_str());
650 xPython::writeStdoutLine(s.Data());
651 // if (PyObject *sys_stdout = PySys_GetObject("stdout"); sys_stdout != nullptr) {
652 // Py_XDECREF(PyObject_CallMethod(sys_stdout, "flush", nullptr));
653 // }
654 } else {
655 ::Info("xRooHypoSpace::AddPoint", "Added new point @ %s", coordString.c_str());
656 }
657 return emplace_back(out);
658}
659
661{
662
663 if (!_pdf.get<RooAbsPdf>()) {
664 throw std::runtime_error("Not a pdf");
665 }
666
667 auto pars = _pdf.pars().argList();
668
669 // replace any pars with validity pars and add new pars
670 auto vpars = toArgs(validity);
671 pars.replace(vpars);
672 vpars.remove(pars, true, true);
673 pars.add(vpars);
674
675 if (auto existing = pdf(pars)) {
676 throw std::runtime_error(std::string("Clashing model: ") + existing->GetName());
677 }
678
679 auto myPars = std::shared_ptr<RooArgList>(dynamic_cast<RooArgList *>(pars.snapshot()));
680 myPars->sort();
681
682 pars.remove(*fPars, true, true);
683
684 fPars->addClone(pars);
685
686 fPdfs.insert(std::make_pair(myPars, std::make_shared<xRooNode>(_pdf)));
687
688 return true;
689}
690
692{
693 // determine which pars are the minimal set to distinguish all points in the space
694 RooArgList out;
695 out.setName("axes");
696
697 out.add(*std::unique_ptr<RooAbsCollection>(
698 fPars->selectByAttrib("axis", true))); // start with any pars explicitly designated as axes
699
700 bool clash;
701 do {
702 clash = false;
703
704 std::set<std::vector<double>> coords;
705 for (auto &p : *this) {
706 std::vector<double> p_coords;
707 for (auto o : out) {
708 auto _v = dynamic_cast<RooRealVar *>(p.coords->find(o->GetName()));
709 p_coords.push_back(
710 (_v && _v->isConstant())
711 ? _v->getVal()
712 : std::numeric_limits<double>::infinity()); // non-const coords are treating as non-existent
713 // p_coords.push_back(p.coords->getRealValue(o->GetName(), std::numeric_limits<double>::quiet_NaN()));
714 }
715 if (coords.find(p_coords) != coords.end()) {
716 clash = true;
717 break;
718 }
719 coords.insert(p_coords);
720 }
721
722 if (clash) {
723 // add next best coordinate
724 std::map<std::string, std::unordered_set<double>> values;
725 for (auto &par : *pars()) {
726 if (out.find(*par))
727 continue;
728 for (auto p : *this) {
729 auto _v = dynamic_cast<RooRealVar *>(p.coords->find(par->GetName()));
730 values[par->GetName()].insert(
731 (_v && _v->isConstant())
732 ? _v->getVal()
733 : std::numeric_limits<double>::infinity()); // non-const coords are treating as non-existent
734 // values[par->GetName()].insert(
735 // p.coords->getRealValue(par->GetName(), std::numeric_limits<double>::quiet_NaN()));
736 }
737 }
738
739 std::string bestVar;
740 size_t maxDiff = 0;
741 bool isPOI = false;
742 for (auto &[k, v] : values) {
743 if (v.size() > maxDiff || (v.size() == maxDiff && !isPOI && pars()->find(k.c_str())->getAttribute("poi"))) {
744 bestVar = k;
745 isPOI = pars()->find(k.c_str())->getAttribute("poi");
746 maxDiff = std::max(maxDiff, v.size());
747 }
748 }
749 if (bestVar.empty()) {
750 break;
751 }
752
753 out.add(*pars()->find(bestVar.c_str()));
754 }
755 } while (clash);
756
757 // ensure poi are at the end
758 std::unique_ptr<RooAbsCollection> poi(out.selectByAttrib("poi", true));
759 out.remove(*poi);
760 out.add(*poi);
761
762 return out;
763}
764
766{
767 RooArgList out;
768 out.setName("poi");
769 out.add(*std::unique_ptr<RooAbsCollection>(pars()->selectByAttrib("poi", true)));
770 return out;
771}
772
774{
775
776 auto _axes = axes();
777
778 size_t badFits = 0;
779
780 for (size_t i = 0; i < size(); i++) {
781 std::cout << i << ") ";
782 for (auto a : _axes) {
783 if (a != _axes.first())
784 std::cout << ",";
785 std::cout << a->GetName() << "="
786 << at(i).coords->getRealValue(a->GetName(), std::numeric_limits<double>::quiet_NaN());
787 }
788 std::cout << " status=[ufit:";
789 auto ufit = const_cast<xRooHypoPoint &>(at(i)).ufit(true);
790 if (!ufit) {
791 std::cout << "-";
792 } else {
793 std::cout << ufit->status();
794 badFits += (xRooNLLVar::xRooHypoPoint::allowedStatusCodes.count(ufit->status()) == 0);
795 }
796 std::cout << ",cfit_null:";
797 auto cfit = const_cast<xRooHypoPoint &>(at(i)).cfit_null(true);
798 if (!cfit) {
799 std::cout << "-";
800 } else {
801 std::cout << cfit->status();
802 badFits += (xRooNLLVar::xRooHypoPoint::allowedStatusCodes.count(cfit->status()) == 0);
803 }
804 std::cout << ",cfit_alt:";
805 auto afit = const_cast<xRooHypoPoint &>(at(i)).cfit_alt(true);
806 if (!afit) {
807 std::cout << "-";
808 } else {
809 std::cout << afit->status();
811 }
812 if (auto asiPoint = const_cast<xRooHypoPoint &>(at(i)).asimov(true)) {
813 std::cout << ",asimov.ufit:";
814 auto asi_ufit = asiPoint->ufit(true);
815 if (!asi_ufit) {
816 std::cout << "-";
817 } else {
818 std::cout << asi_ufit->status();
820 }
821 std::cout << ",asimov.cfit_null:";
822 auto asi_cfit = asiPoint->cfit_null(true);
823 if (!asi_cfit) {
824 std::cout << "-";
825 } else {
826 std::cout << asi_cfit->status();
828 }
829 auto cfit_lbound = const_cast<xRooHypoPoint &>(at(i)).cfit_lbound(true);
830 if (!cfit_lbound) {
831 } else {
832 std::cout << ",cfit_lbound:" << cfit_lbound->status();
833 badFits += (xRooNLLVar::xRooHypoPoint::allowedStatusCodes.count(cfit_lbound->status()) == 0);
834 }
835 }
836 std::cout << "]";
837 auto sigma_mu = const_cast<xRooHypoPoint &>(at(i)).sigma_mu(true);
838 if (!std::isnan(sigma_mu.first)) {
839 std::cout << " sigma_mu=" << sigma_mu.first;
840 if (sigma_mu.second)
841 std::cout << " +/- " << sigma_mu.second;
842 }
843 std::cout << std::endl;
844 }
845 std::cout << "--------------------------" << std::endl;
846 std::cout << "Number of bad fits: " << badFits << std::endl;
847}
848
849std::shared_ptr<TGraphErrors> xRooNLLVar::xRooHypoSpace::graph(
850 const char *opt /*, const std::function<void(xRooNLLVar::xRooHypoSpace*)>& progress*/) const
851{
852
853 TString sOpt(opt);
854 sOpt.ToLower();
855
856 bool doCLs = sOpt.Contains("cls");
857 bool readOnly = sOpt.Contains("readonly");
858 bool visualize = sOpt.Contains("visualize") && !readOnly;
859
860 double nSigma =
861 (sOpt.Contains("exp"))
862 ? (TString(sOpt(sOpt.Index("exp") + 3,
863 sOpt.Index(" ", sOpt.Index("exp")) == -1 ? sOpt.Length() : sOpt.Index(" ", sOpt.Index("exp"))))
864 .Atof())
865 : std::numeric_limits<double>::quiet_NaN();
866 bool expBand =
867 !std::isnan(nSigma) && nSigma && !(sOpt(sOpt.Index("exp") + 3) == '+' || sOpt(sOpt.Index("exp") + 3) == '-');
868
869 auto _axes = axes();
870 if (_axes.size() != 1)
871 return nullptr;
872
873 auto out = std::make_shared<TGraphErrors>();
874 out->SetName(GetName());
875 out->SetEditable(false);
876 const char *sCL = (doCLs) ? "CLs" : "null";
877
878 TString title =
879 TString::Format("%s;%s;p_{%s}",
880 (std::isnan(nSigma))
881 ? "Observed"
882 : (!nSigma ? "Expected"
883 : TString::Format("%s%d#sigma Expected",
884 expBand || !nSigma ? "" : ((nSigma < 0) ? "-" : "+"), int(nSigma))
885 .Data()),
886 _axes.at(0)->GetTitle(), sCL);
887
888 if (std::isnan(nSigma)) {
889 out->SetNameTitle(TString::Format("obs_p%s", sCL), title);
890 out->SetMarkerStyle(20);
891 out->SetMarkerSize(0.5);
892 if (sOpt.Contains("ts")) {
893 out->SetNameTitle("obs_ts", TString::Format("Observed;%s;%s", _axes.at(0)->GetTitle(),
894 (empty() ? "" : front().tsTitle(true).Data())));
895 }
896 } else {
897 out->SetNameTitle(TString::Format("exp%d_p%s", int(nSigma), sCL), title);
898 out->SetMarkerStyle(0);
899 out->SetMarkerSize(0);
900 out->SetLineStyle(2 + int(nSigma));
901 if (expBand && nSigma) {
902 out->SetFillColor((nSigma == 2) ? kYellow : kGreen);
903 // out->SetFillStyle(3005);
904 out->SetLineStyle(0);
905 out->SetLineWidth(0);
906 auto x = out->Clone("up");
907 x->SetBit(kCanDelete);
908 dynamic_cast<TAttFill *>(x)->SetFillStyle(nSigma == 2 ? 3005 : 3004);
909 dynamic_cast<TAttFill *>(x)->SetFillColor(kBlack);
910 out->GetListOfFunctions()->Add(x, "F");
911 x = out->Clone("down");
912 x->SetBit(kCanDelete);
913 dynamic_cast<TAttFill*>(x)->SetFillColor(kBlack);
914 dynamic_cast<TAttFill*>(x)->SetFillStyle(nSigma == 2 ? 3005 : 3004);
915 out->GetListOfFunctions()->Add(x, "F");
916 }
917 if (sOpt.Contains("ts")) {
918 out->SetNameTitle(TString::Format("exp_ts%d", int(nSigma)),
919 TString::Format("Expected;%s;%s", _axes.at(0)->GetTitle(), front().tsTitle(true).Data()));
920 }
921 }
922
923 auto badPoints = [&]() {
924 auto badPoints2 = dynamic_cast<TGraph *>(out->GetListOfFunctions()->FindObject("badPoints"));
925 if (!badPoints2) {
926 badPoints2 = new TGraph;
927 badPoints2->SetBit(kCanDelete);
928 badPoints2->SetName("badPoints");
929 badPoints2->SetMarkerStyle(5);
930 badPoints2->SetMarkerColor(std::isnan(nSigma) ? kRed : kBlue);
931 badPoints2->SetMarkerSize(1);
932 out->GetListOfFunctions()->Add(badPoints2, "P");
933 }
934 return badPoints2;
935 };
936 int nPointsDown = 0;
937 bool above = true;
938 TStopwatch s;
939 s.Start();
940 size_t nDone = 0;
941 for (auto &p : *this) {
942 if (s.RealTime() > 5) {
943 if (visualize) {
944 // draw readonly version of the graph
945 auto gra = graph(sOpt + " readOnly");
946 if (gra && gra->GetN()) {
947 if (!gPad && gROOT->GetSelectedPad())
948 gROOT->GetSelectedPad()->cd();
949 if (gPad)
950 gPad->Clear();
951 gra->DrawClone(expBand ? "AF" : "ALP")->SetBit(kCanDelete);
952#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 30, 00)
953 if (auto pad = gROOT->GetSelectedPad()) {
954 pad->GetCanvas()->ResetUpdated(); // stops previous canvas being replaced in a jupyter notebook
955 }
956#endif
958 }
959 } else {
960 ::Info("xRooHypoSpace::graph", "Completed %d/%d points for %s", int(nDone), int(size()), sOpt.Data());
961 }
962 s.Start();
963 } else {
964 s.Continue();
965 }
966 double _x = p.coords->getRealValue(_axes.at(0)->GetName(), std::numeric_limits<double>::quiet_NaN());
967 auto pval = const_cast<xRooHypoPoint &>(p).getVal(sOpt);
968 auto idx = out->GetN() - nPointsDown;
969
970 if (std::isnan(pval.first)) {
971 if (p.status() != 0) { // if status is 0 then bad pval is really just absence of fits, not bad fits
972 badPoints()->SetPoint(badPoints()->GetN(), _x, 0);
973 }
974 } else {
975 out->InsertPointBefore(idx, _x, pval.first);
976 out->SetPointError(idx, 0, pval.second);
977 }
978
979 if (expBand && nSigma) {
981 sOpt2.ReplaceAll("exp", "exp-");
982 pval = const_cast<xRooHypoPoint &>(p).getVal(sOpt2);
983 if (std::isnan(pval.first)) {
984 if (p.status() != 0) { // if status is 0 then bad pval is really just absence of fits, not bad fits
985 badPoints()->SetPoint(badPoints()->GetN(), _x, 0);
986 }
987 } else {
988 out->InsertPointBefore(idx + 1, _x, pval.first);
989 out->SetPointError(idx + 1, 0, pval.second);
990 nPointsDown++;
991 if (out->GetPointY(idx) < pval.first)
992 above = false; // the -sigma points are actually above +sigma
993 }
994 }
995 nDone++;
996 }
997
998 if (out->GetN() == 0)
999 return out;
1000
1001 if (!expBand) {
1002 out->Sort();
1003 if (out->GetListOfFunctions()->FindObject("badPoints")) {
1004 // try to interpolate the points
1005 for (int i = 0; i < badPoints()->GetN(); i++) {
1006 badPoints()->SetPointY(i, out->Eval(badPoints()->GetPointX(i)));
1007 }
1008 }
1009 } else {
1010 out->Sort(&TGraph::CompareX, true, 0, out->GetN() - nPointsDown - 1); // sort first half
1011 out->Sort(&TGraph::CompareX, false, out->GetN() - nPointsDown, out->GetN() - 1); // reverse sort second half
1012
1013 // now populate the up and down values
1014 auto up = dynamic_cast<TGraph *>(out->GetListOfFunctions()->FindObject("up"));
1015 auto down = dynamic_cast<TGraph *>(out->GetListOfFunctions()->FindObject("down"));
1016
1017 for (int i = 0; i < out->GetN(); i++) {
1018 if (i < out->GetN() - nPointsDown) {
1019 up->SetPoint(up->GetN(), out->GetPointX(i), out->GetPointY(i) + out->GetErrorY(i) * (above ? 1. : -1.));
1020 //down->SetPoint(down->GetN(), out->GetPointX(i), out->GetPointY(i) - out->GetErrorY(i) * (above ? 1. : -1.));
1021 } else {
1022 //up->SetPoint(up->GetN(), out->GetPointX(i), out->GetPointY(i) - out->GetErrorY(i) * (above ? 1. : -1.));
1023 down->SetPoint(down->GetN(), out->GetPointX(i), out->GetPointY(i) + out->GetErrorY(i) * (above ? 1. : -1.));
1024 }
1025 }
1026 // now go back round in reverse
1027 for (int i = out->GetN()-1; i >= 0; i--) {
1028 if (i < out->GetN() - nPointsDown) {
1029 up->SetPoint(up->GetN(), out->GetPointX(i), out->GetPointY(i) - out->GetErrorY(i) * (above ? 1. : -1.));
1030 //down->SetPoint(down->GetN(), out->GetPointX(i), out->GetPointY(i) - out->GetErrorY(i) * (above ? 1. : -1.));
1031 } else {
1032 //up->SetPoint(up->GetN(), out->GetPointX(i), out->GetPointY(i) - out->GetErrorY(i) * (above ? 1. : -1.));
1033 down->SetPoint(down->GetN(), out->GetPointX(i), out->GetPointY(i) - out->GetErrorY(i) * (above ? 1. : -1.));
1034 }
1035 }
1036 }
1037
1038 if (visualize) {
1039 // draw result
1040 if (!gPad && gROOT->GetSelectedPad())
1041 gROOT->GetSelectedPad()->cd();
1042 if (gPad)
1043 gPad->Clear();
1044 out->DrawClone(expBand ? "AF" : "ALP")->SetBit(kCanDelete);
1045#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 30, 00)
1046 if (auto pad = gROOT->GetSelectedPad()) {
1047 pad->GetCanvas()->ResetUpdated(); // stops previous canvas being replaced in a jupyter notebook
1048 }
1049#endif
1051 }
1052
1053 return out;
1054}
1055
1056std::shared_ptr<TMultiGraph> xRooNLLVar::xRooHypoSpace::graphs(const char *opt)
1057{
1058 TString sOpt(opt);
1059 sOpt.ToLower();
1060 std::shared_ptr<TMultiGraph> out;
1061 if (sOpt.Contains("pcls") || sOpt.Contains("pnull") || sOpt.Contains("ts")) {
1062
1063 bool visualize = sOpt.Contains("visualize");
1064 sOpt.ReplaceAll("visualize", "");
1065
1066 auto exp2 = graph(sOpt + " exp2");
1067 auto exp1 = graph(sOpt + " exp1");
1068 auto exp = graph(sOpt + " exp");
1069 bool doObs = true;
1070 // for(auto& hp : *this) { if(hp.isExpected) {doObs=false; break;} }
1071 auto obs = (doObs) ? graph(sOpt) : nullptr;
1072
1073 out = std::make_shared<TMultiGraph>(GetName(), GetTitle());
1074 if (exp2 && exp2->GetN() > 1)
1075 out->Add(static_cast<TGraph *>(exp2->Clone()), "FP");
1076 if (exp1 && exp1->GetN() > 1)
1077 out->Add(static_cast<TGraph *>(exp1->Clone()), "FP");
1078 if (exp && exp->GetN() > 1)
1079 out->Add(static_cast<TGraph *>(exp->Clone()), "LP");
1080 if (obs && obs->GetN() > 1)
1081 out->Add(static_cast<TGraph *>(obs->Clone()), "LP");
1082
1083 if (!out->GetListOfGraphs()) {
1084 return nullptr;
1085 }
1086
1087 TGraph *testedPoints = nullptr;
1088 if (sOpt.Contains("pcls")) {
1089 TGraph *line = new TGraph;
1090 line->SetName("alpha");
1091 line->SetLineStyle(2);
1092 line->SetEditable(false);
1093 line->SetPoint(line->GetN(), out->GetHistogram()->GetXaxis()->GetXmin() - 10, 0.05);
1094 testedPoints = new TGraph;
1095 testedPoints->SetName("hypoPoints");
1096 testedPoints->SetEditable(false);
1097 testedPoints->SetMarkerStyle(24);
1098 testedPoints->SetMarkerSize(0.4); // use line to indicate tested points
1099 if (exp) {
1100 for (int i = 0; i < exp->GetN(); i++) {
1101 testedPoints->SetPoint(testedPoints->GetN(), exp->GetPointX(i), 0.05);
1102 }
1103 }
1104 line->SetPoint(line->GetN(), out->GetHistogram()->GetXaxis()->GetXmax() + 10, 0.05);
1106 out->GetListOfFunctions()->Add(line, "L");
1107 }
1108 if (exp) {
1109 out->GetHistogram()->GetXaxis()->SetTitle(exp->GetHistogram()->GetXaxis()->GetTitle());
1110 out->GetHistogram()->GetYaxis()->SetTitle(exp->GetHistogram()->GetYaxis()->GetTitle());
1111 }
1112 TLegend *leg = nullptr;
1113 if (out->GetListOfGraphs()->GetEntries() > 1) {
1114 leg = new TLegend(1. - gStyle->GetPadRightMargin() - 0.3, 1. - gStyle->GetPadTopMargin() - 0.35,
1115 1. - gStyle->GetPadRightMargin() - 0.05, 1. - gStyle->GetPadTopMargin() - 0.05);
1116 leg->SetName("legend");
1117 leg->SetBit(kCanDelete);
1118
1119 out->GetListOfFunctions()->Add(leg);
1120 // out->GetListOfFunctions()->Add(out->GetHistogram()->Clone(".axis"),"sameaxis"); // redraw axis
1121
1122 for (auto g : *out->GetListOfGraphs()) {
1123 if (dynamic_cast<TGraph *>(g)->GetListOfFunctions()->FindObject("down")) {
1124 leg->AddEntry(g, "", "F");
1125 } else {
1126 leg->AddEntry(g, "", "LPE");
1127 }
1128 }
1129 }
1130
1131 auto addToLegend = [](TLegend *l, const char *label, const std::pair<double, double> val) {
1132 if (l) {
1133 l->AddEntry((TObject *)nullptr,
1134 TString::Format("%s%s: %g #pm %g%s", std::isfinite(val.second) ? "" : "#color[2]{", label,
1135 val.first, val.second, std::isfinite(val.second) ? "" : "}"),
1136 "");
1137 }
1138 };
1139
1140 if (sOpt.Contains("pcls")) {
1141 // add current limit estimates to legend
1142 if (exp2 && exp2->GetN() > 1) {
1143 auto l = xRooFit::matchPrecision(GetLimit(*graph(sOpt + "exp-2")));
1144 addToLegend(leg, "-2#sigma", l);
1145 }
1146 if (exp1 && exp1->GetN() > 1) {
1147 auto l = xRooFit::matchPrecision(GetLimit(*graph(sOpt + "exp-1")));
1148 addToLegend(leg, "-1#sigma", l);
1149 }
1150 if (exp && exp->GetN() > 1) {
1151 auto l = xRooFit::matchPrecision(GetLimit(*exp));
1152 addToLegend(leg, "0#sigma", l);
1153 }
1154 if (exp1 && exp1->GetN() > 1) {
1155 auto l = xRooFit::matchPrecision(GetLimit(*graph(sOpt + "exp+1")));
1156 addToLegend(leg, "+1#sigma", l);
1157 }
1158 if (exp2 && exp2->GetN() > 1) {
1159 auto l = xRooFit::matchPrecision(GetLimit(*graph(sOpt + "exp+2")));
1160 addToLegend(leg, "+2#sigma", l);
1161 }
1162 if (obs && obs->GetN() > 1) {
1163 auto l = xRooFit::matchPrecision(GetLimit(*obs));
1164 addToLegend(leg, "Observed", l);
1165 }
1166 }
1167 if (testedPoints)
1168 out->Add(testedPoints, "P");
1169
1170 if (visualize) {
1171 if (!gPad && gROOT->GetSelectedPad())
1172 gROOT->GetSelectedPad()->cd();
1173 if (gPad)
1174 gPad->Clear();
1175 auto gra2 = static_cast<TMultiGraph *>(out->DrawClone("A"));
1176 gra2->SetBit(kCanDelete);
1177 if (sOpt.Contains("pcls") || sOpt.Contains("pnull")) {
1178 gra2->SetMinimum(1e-6);
1179 gra2->SetMaximum(1);
1180 }
1181 if (gPad) {
1182 gPad->RedrawAxis();
1183 gPad->GetCanvas()->Paint();
1184 gPad->GetCanvas()->Update();
1185#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 30, 00)
1186 gPad->GetCanvas()->ResetUpdated(); // stops previous canvas being replaced in a jupyter notebook
1187#endif
1188 }
1190 }
1191 }
1192
1193 return out;
1194}
1195
1197{
1198
1199 if (std::isnan(target)) {
1200 target = 1. - gEnv->GetValue("xRooHypoSpace.CL", 95.) / 100.;
1201 }
1202
1203 auto gr = std::make_shared<TGraph>(pValues);
1204 // remove any nan points and duplicates
1205 int i = 0;
1206 std::set<double> existingX;
1207 while (i < gr->GetN()) {
1208 if (std::isnan(gr->GetPointY(i))) {
1209 gr->RemovePoint(i);
1210 } else if (existingX.find(gr->GetPointX(i)) != existingX.end()) {
1211 gr->RemovePoint(i);
1212 } else {
1213 existingX.insert(gr->GetPointX(i));
1214 // convert to log ....
1215 gr->SetPointY(i, log(std::max(gr->GetPointY(i), 1e-10)));
1216 i++;
1217 }
1218 }
1219
1220 gr->Sort();
1221
1222 // simple linear extrapolation to critical value ... return nan if problem
1223 if (gr->GetN() < 2) {
1224 return std::pair<double, double>(std::numeric_limits<double>::quiet_NaN(), 0);
1225 }
1226
1227 double alpha = log(target);
1228
1229 bool above = gr->GetPointY(0) > alpha;
1230 for (int ii = 1; ii < gr->GetN(); ii++) {
1231 if ((above && (gr->GetPointY(ii) <= alpha)) || (!above && (gr->GetPointY(ii) >= alpha))) {
1232 // found the limit ... return linearly extrapolated point
1233 double lim = gr->GetPointX(ii - 1) + (gr->GetPointX(ii) - gr->GetPointX(ii - 1)) *
1234 (alpha - gr->GetPointY(ii - 1)) /
1235 (gr->GetPointY(ii) - gr->GetPointY(ii - 1));
1236 // use points either side as error
1237 double err = std::max(lim - gr->GetPointX(ii - 1), gr->GetPointX(ii) - lim);
1238 // give err negative sign to indicate if error due to negative side
1239 if ((lim - gr->GetPointX(ii - 1)) > (gr->GetPointX(ii) - lim))
1240 err *= -1;
1241 return std::pair(lim, err);
1242 }
1243 }
1244 // if reach here need to extrapolate ...
1245 if ((above && gr->GetPointY(gr->GetN() - 1) <= gr->GetPointY(0)) ||
1246 (!above && gr->GetPointY(gr->GetN() - 1) >= gr->GetPointY(0))) {
1247 // extrapolating above based on last two points
1248 // in fact, if 2nd last point is a p=1 (log(p)=0) then go back
1249 int offset = 2;
1250 while (offset < gr->GetN() && gr->GetPointY(gr->GetN() - offset) == 0)
1251 offset++;
1252 double x1 = gr->GetPointX(gr->GetN() - offset);
1253 double y1 = gr->GetPointY(gr->GetN() - offset);
1254 double m = (gr->GetPointY(gr->GetN() - 1) - y1) / (gr->GetPointX(gr->GetN() - 1) - x1);
1255 if (m == 0.)
1256 return std::pair(std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity());
1257 return std::pair((alpha - y1) / m + x1, std::numeric_limits<double>::infinity());
1258 } else {
1259 // extrapolating below based on first two points
1260 double x1 = gr->GetPointX(0);
1261 double y1 = gr->GetPointY(0);
1262 double m = (gr->GetPointY(1) - y1) / (gr->GetPointX(1) - x1);
1263 if (m == 0.)
1264 return std::pair(-std::numeric_limits<double>::infinity(), -std::numeric_limits<double>::infinity());
1265 return std::pair((alpha - y1) / m + x1, -std::numeric_limits<double>::infinity());
1266 }
1267}
1268
1270{
1271 TString sOpt = TString::Format("p%s", type);
1272 if (std::isnan(nSigma)) {
1273 sOpt += "obs";
1274 } else {
1275 sOpt += TString::Format("exp%s%d", nSigma > 0 ? "+" : "", int(nSigma));
1276 }
1277 return GetLimit(*graph(sOpt + " readonly"));
1278}
1279
1281xRooNLLVar::xRooHypoSpace::findlimit(const char *opt, double relUncert, unsigned int maxTries)
1282{
1283 TString sOpt(opt);
1284 bool visualize = sOpt.Contains("visualize");
1285 sOpt.ReplaceAll("visualize", "");
1286 std::shared_ptr<TGraphErrors> gr = graph(sOpt + " readonly");
1287 if (visualize) {
1288 auto gra = graphs(sOpt.Contains("toys") ? "pcls readonly toys" : "pcls readonly");
1289 if (gra) {
1290 if (!gPad)
1291 gra->Draw(); // in 6.28 DrawClone wont make the gPad defined :( ... so Draw then clear and Draw Clone
1292 gPad->Clear();
1293 auto gra2 = static_cast<TMultiGraph *>(gra->DrawClone("A"));
1294 gra2->SetBit(kCanDelete);
1295 gra2->SetMinimum(1e-9);
1296 gra2->SetMaximum(1);
1297 gPad->RedrawAxis();
1298 gPad->GetCanvas()->Paint();
1299 gPad->GetCanvas()->Update();
1300#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 30, 00)
1301 gPad->GetCanvas()->ResetUpdated(); // stops previous canvas being replaced in a jupyter notebook
1302#endif
1304 }
1305 }
1306
1307 // resync parameter boundaries from nlls (may have been modified by fits)
1308 for (auto p : axes()) {
1309 for (auto &[pdf, nll] : fNlls) {
1310 if (auto _v = dynamic_cast<RooRealVar *>(nll->pars()->find(*p))) {
1311 dynamic_cast<RooRealVar *>(p)->setRange(_v->getMin(), _v->getMax());
1312 }
1313 }
1314 }
1315
1316 if (!gr || gr->GetN() < 2) {
1317 auto v = (axes().empty()) ? nullptr : dynamic_cast<RooRealVar *>(*axes().rbegin());
1318 if (!v)
1319 return std::pair(std::numeric_limits<double>::quiet_NaN(), 0.);
1320 double muMax = std::min(std::min(v->getMax("physical"), v->getMax()), v->getMax("scan"));
1321 double muMin = std::max(std::max(v->getMin("physical"), v->getMin()), v->getMin("scan"));
1322 if (!gr || gr->GetN() < 1) {
1323 if (maxTries == 0 || std::isnan(AddPoint(TString::Format("%s=%g", v->GetName(), muMin)).getVal(sOpt).first)) {
1324 // first point failed ... give up
1325 ::Error("findlimit", "Problem evaluating First Point %s @ %s=%g", sOpt.Data(), v->GetName(), muMin);
1326 return std::pair(std::numeric_limits<double>::quiet_NaN(), 0.);
1327 }
1328 gr.reset();
1329 return findlimit(opt, relUncert, maxTries - 1); // do this to resync parameter limits
1330 }
1331
1332 // can approximate expected limit using
1333 // mu_hat + sigma_mu*ROOT::Math::gaussian_quantile(1.-alpha/2.,1) for cls
1334 // or mu_hat + sigma_mu*ROOT::Math::gaussian_quantile((1.-alpha),1) for cls+b
1335 // get a very first estimate of sigma_mu from ufit to expected data, take error on mu as sigma_mu
1336 double nextPoint = muMin + (muMax - muMin) / 50;
1337
1338 // if done an expected limit, assume data is like expected and choose expected limit point as first test point
1339 if (sOpt.Contains("obs")) {
1340 TString sOpt2 = sOpt;
1341 sOpt2.ReplaceAll("obs", "exp");
1342 auto expLim = findlimit(sOpt2, std::numeric_limits<double>::infinity(), 0);
1343 if (!std::isnan(expLim.first) && expLim.first < nextPoint)
1344 nextPoint = expLim.first;
1345 }
1346
1347 auto point =
1348 (sOpt.Contains("exp")) ? back().asimov() : std::shared_ptr<xRooHypoPoint>(&back(), [](xRooHypoPoint *) {});
1349 point = nullptr;
1350 if (point && point->ufit()) {
1351 double rough_sigma_mu = point->mu_hat().getError();
1352 double another_estimate = point->mu_hat().getVal() + rough_sigma_mu * ROOT::Math::gaussian_quantile(0.95, 1);
1353 // if (another_estimate < nextPoint) {
1355 ::Info("xRooHypoSpace::findlimit", "Guessing %g based on rough sigma_mu = %g", nextPoint, rough_sigma_mu);
1356 //}
1357 }
1358
1359 if (maxTries == 0 || std::isnan(AddPoint(TString::Format("%s=%g", v->GetName(), nextPoint)).getVal(sOpt).first)) {
1360 // second point failed ... give up
1361 ::Error("xRooHypoSpace::findlimit", "Problem evaluating Second Point %s @ %s=%g", sOpt.Data(), v->GetName(),
1362 nextPoint);
1363 return std::pair(std::numeric_limits<double>::quiet_NaN(), 0.);
1364 }
1365 gr.reset();
1366 return findlimit(opt, relUncert, maxTries - 1);
1367 }
1368
1369 auto lim = GetLimit(*gr);
1370
1371 if (std::isnan(lim.first)) {
1372 return lim;
1373 }
1374
1375 auto v = dynamic_cast<RooRealVar *>(*axes().rbegin());
1376 double maxMu = std::min(std::min(v->getMax("physical"), v->getMax()), v->getMax("scan"));
1377 double minMu = std::max(std::max(v->getMin("physical"), v->getMin()), v->getMin("scan"));
1378
1379 // static double MIN_LIMIT_UNCERT = 1e-4; // stop iterating once uncert gets this small
1380 if (lim.first > -std::numeric_limits<double>::infinity() && lim.first < std::numeric_limits<double>::infinity() &&
1381 (std::abs(lim.second) <= relUncert * std::abs(lim.first) /* || std::abs(lim.second)<MIN_LIMIT_UNCERT*/))
1382 return lim;
1383
1384 double nextPoint;
1385
1386 if (lim.second == std::numeric_limits<double>::infinity()) {
1387 // limit was found by extrapolating to right
1388 nextPoint = lim.first;
1389 if (nextPoint == std::numeric_limits<double>::infinity() || nextPoint > maxMu) {
1390 nextPoint = gr->GetPointX(gr->GetN() - 1) + (maxMu - minMu) / 50;
1391 }
1392
1393 // prefer extrapolation with sigma_mu, if available, if it takes us further
1394 // as shape of p-value curve is usually
1395 auto point =
1396 (sOpt.Contains("exp")) ? back().asimov() : std::shared_ptr<xRooHypoPoint>(&back(), [](xRooHypoPoint *) {});
1397 point = nullptr;
1398 if (point && point->ufit()) {
1399 double rough_sigma_mu = point->mu_hat().getError();
1400 double another_estimate = point->mu_hat().getVal() + rough_sigma_mu * ROOT::Math::gaussian_quantile(0.95, 1);
1401 // if (another_estimate < nextPoint) {
1403 ::Info("xRooHypoSpace::findlimit", "Guessing %g based on rough sigma_mu = %g", nextPoint, rough_sigma_mu);
1404 //}
1405 }
1406 nextPoint = std::min(nextPoint + nextPoint * relUncert * 0.99, maxMu); // ensure we step over location if possible
1407
1408 if (nextPoint > maxMu)
1409 return lim;
1410 } else if (lim.second == -std::numeric_limits<double>::infinity()) {
1411 // limit from extrapolating to left
1412 nextPoint = lim.first;
1413 if (nextPoint < minMu)
1414 nextPoint = gr->GetPointX(0) - (maxMu - minMu) / 50;
1415 if (nextPoint < minMu)
1416 return lim;
1417 } else {
1418 nextPoint = lim.first + lim.second * relUncert * 0.99;
1419 }
1420
1421 // got here need a new point .... evaluate the estimated lim location +/- the relUncert (signed error takes care of
1422 // direction)
1423
1424 if (maxTries == 0) {
1425 ::Warning("xRooHypoSpace::findlimit", "Reached max number of point evaluations");
1426 return lim;
1427 }
1428
1429 ::Info("xRooHypoSpace::findlimit", "%s -- Testing new point @ %s=%g (delta=%g)", sOpt.Data(), v->GetName(),
1430 nextPoint, lim.second);
1431 if (std::isnan(AddPoint(TString::Format("%s=%g", v->GetName(), nextPoint)).getVal(sOpt).first)) {
1432 ::Error("xRooHypoSpace::findlimit", "Problem evaluating %s @ %s=%g", sOpt.Data(), v->GetName(), nextPoint);
1433 return lim;
1434 }
1435 gr.reset();
1436 return findlimit(opt, relUncert, maxTries - 1);
1437}
1438
1440{
1441
1442 TString sOpt(opt);
1443 sOpt.ToLower();
1444
1445 if ((sOpt == "" || sOpt == "same") && !empty()) {
1446 if (front().fPllType == xRooFit::Asymptotics::OneSidedPositive) {
1447 sOpt += "pcls"; // default to showing cls p-value scan if drawing a limit
1448 for (auto &hp : *this) {
1449 if (!hp.nullToys.empty() || !hp.altToys.empty()) {
1450 sOpt += " toys";
1451 break; // default to toys if done toys
1452 }
1453 }
1454 } else if (front().fPllType == xRooFit::Asymptotics::TwoSided) {
1455 sOpt += "ts";
1456 }
1457 }
1458
1459 // split up by ; and call Draw for each (with 'same' appended)
1460 auto _axes = axes();
1461 if (_axes.empty())
1462 return;
1463
1464 if (sOpt == "status") {
1465 // draw the points in the space
1466 if (_axes.size() <= 2) {
1467 TGraphErrors *out = new TGraphErrors;
1468 out->SetBit(kCanDelete);
1469 out->SetName("points");
1470 out->SetMarkerSize(0.5);
1471 TGraph *tsAvail = new TGraph;
1472 tsAvail->SetName("ts");
1473 tsAvail->SetBit(kCanDelete);
1474 tsAvail->SetMarkerStyle(20);
1475 TGraph *expAvail = new TGraph;
1476 expAvail->SetName("exp");
1477 expAvail->SetBit(kCanDelete);
1478 expAvail->SetMarkerStyle(25);
1479 expAvail->SetMarkerSize(out->GetMarkerSize() * 1.5);
1480 TGraph *badPoints = new TGraph;
1481 badPoints->SetName("bad_ufit");
1482 badPoints->SetBit(kCanDelete);
1483 badPoints->SetMarkerStyle(5);
1484 badPoints->SetMarkerColor(kRed);
1485 badPoints->SetMarkerSize(out->GetMarkerSize());
1486 TGraph *badPoints2 = new TGraph;
1487 badPoints2->SetName("bad_cfit_null");
1488 badPoints2->SetBit(kCanDelete);
1489 badPoints2->SetMarkerStyle(2);
1490 badPoints2->SetMarkerColor(kRed);
1491 badPoints2->SetMarkerSize(out->GetMarkerSize());
1492
1493 out->SetTitle(TString::Format("%s;%s;%s", GetTitle(), _axes.at(0)->GetTitle(),
1494 (_axes.size() == 1) ? "" : _axes.at(1)->GetTitle()));
1495 for (auto &p : *this) {
1496 bool _readOnly = p.nllVar ? p.nllVar->get()->getAttribute("readOnly") : false;
1497 if (p.nllVar)
1498 p.nllVar->get()->setAttribute("readOnly", true);
1499 double x = p.coords->getRealValue(_axes.at(0)->GetName());
1500 double y = _axes.size() == 1 ? p.ts_asymp().first : p.coords->getRealValue(_axes.at(1)->GetName());
1501 out->SetPoint(out->GetN(), x, y);
1502 if (!std::isnan(p.ts_asymp().first)) {
1503 if (_axes.size() == 1)
1504 out->SetPointError(out->GetN() - 1, 0, p.ts_asymp().second);
1505 tsAvail->SetPoint(tsAvail->GetN(), x, y);
1506 } else if (p.fUfit && (std::isnan(p.fUfit->minNll()) ||
1507 xRooNLLVar::xRooHypoPoint::allowedStatusCodes.find(p.fUfit->status()) ==
1509 badPoints->SetPoint(badPoints->GetN(), x, y);
1510 } else if (p.fNull_cfit && (std::isnan(p.fNull_cfit->minNll()) ||
1511 xRooNLLVar::xRooHypoPoint::allowedStatusCodes.find(p.fNull_cfit->status()) ==
1513 badPoints2->SetPoint(badPoints2->GetN(), x, y);
1514 }
1515 if (!std::isnan(p.ts_asymp(0).first)) {
1516 expAvail->SetPoint(expAvail->GetN(), x, y);
1517 } else if (p.asimov() && p.asimov()->fUfit &&
1518 (std::isnan(p.asimov()->fUfit->minNll()) ||
1519 xRooNLLVar::xRooHypoPoint::allowedStatusCodes.find(p.asimov()->fUfit->status()) ==
1521
1522 } else if (p.asimov() && p.asimov()->fNull_cfit &&
1523 (std::isnan(p.asimov()->fNull_cfit->minNll()) ||
1524 xRooNLLVar::xRooHypoPoint::allowedStatusCodes.find(p.asimov()->fNull_cfit->status()) ==
1526 }
1527 if (p.nllVar)
1528 p.nllVar->get()->setAttribute("readOnly", _readOnly);
1529 }
1530
1531 if (_axes.size() == 1) {
1532 TGraph tmp;
1533 for (int i = 0; i < out->GetN(); i++) {
1534 if (!std::isnan(out->GetPointY(i)))
1535 tmp.SetPoint(tmp.GetN(), out->GetPointX(i), out->GetPointY(i));
1536 }
1537 auto fixPoints = [&](TGraph *g) {
1538 for (int i = 0; i < g->GetN(); i++) {
1539 if (std::isnan(g->GetPointY(i)))
1540 g->SetPointY(i, std::isnan(tmp.Eval(g->GetPointX(i))) ? 0. : tmp.Eval(g->GetPointX(i)));
1541 }
1542 };
1543 fixPoints(out);
1548 }
1549
1550 out->SetMarkerStyle(4);
1551 out->Draw("AP");
1552 auto leg = new TLegend(1. - gPad->GetRightMargin() - 0.3, 1. - gPad->GetTopMargin() - 0.35,
1553 1. - gPad->GetRightMargin() - 0.05, 1. - gPad->GetTopMargin() - 0.05);
1554 leg->SetName("legend");
1555 leg->AddEntry(out, "Uncomputed", "P");
1556
1557 if (tsAvail->GetN()) {
1558 out->GetListOfFunctions()->Add(tsAvail, "P");
1559 leg->AddEntry(tsAvail, "Computed", "P");
1560 } else {
1561 delete tsAvail;
1562 }
1563 if (expAvail->GetN()) {
1564 out->GetListOfFunctions()->Add(expAvail, "P");
1565 leg->AddEntry(expAvail, "Expected computed", "P");
1566 } else {
1567 delete expAvail;
1568 }
1569 if (badPoints->GetN()) {
1570 out->GetListOfFunctions()->Add(badPoints, "P");
1571 leg->AddEntry(badPoints, "Bad ufit", "P");
1572 } else {
1573 delete badPoints;
1574 }
1575 if (badPoints2->GetN()) {
1576 out->GetListOfFunctions()->Add(badPoints2, "P");
1577 leg->AddEntry(badPoints2, "Bad null cfit", "P");
1578 } else {
1579 delete badPoints2;
1580 }
1581 leg->SetBit(kCanDelete);
1582 leg->Draw();
1583 // if(_axes.size()==1) out->GetHistogram()->GetYaxis()->SetRangeUser(0,1);
1584 gPad->SetGrid(true, _axes.size() > 1);
1585 if (_axes.size() == 1)
1586 gPad->SetLogy(false);
1587 }
1588
1590
1591 return;
1592 }
1593
1594 if (sOpt.Contains("pcls") || sOpt.Contains("pnull") || sOpt.Contains("ts")) {
1595 auto gra = graphs(sOpt + " readonly");
1596 if (!gPad && gROOT->GetSelectedPad())
1597 gROOT->GetSelectedPad()->cd();
1598 if (!sOpt.Contains("same") && gPad) {
1599 gPad->Clear();
1600 }
1601 if (gra) {
1602 if (!gPad)
1604 auto gra2 = static_cast<TMultiGraph *>(gra->DrawClone(sOpt.Contains("same") ? "" : "A"));
1605 gra2->SetBit(kCanDelete);
1606 if (sOpt.Contains("pcls") || sOpt.Contains("pnull")) {
1607 gra2->SetMinimum(1e-6);
1608 gra2->SetMaximum(1);
1609 }
1610 if (gPad) {
1611 gPad->RedrawAxis();
1612 gPad->GetCanvas()->Paint();
1613 gPad->GetCanvas()->Update();
1614#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 30, 00)
1615 gPad->GetCanvas()->ResetUpdated(); // stops previous canvas being replaced in a jupyter notebook
1616#endif
1618 }
1619 }
1620 if (!sOpt.Contains("same") && gPad) {
1621 // auto mg = static_cast<TMultiGraph*>(gPad->GetPrimitive(gra->GetName()));
1622 // mg->GetHistogram()->SetMinimum(1e-9);
1623 // mg->GetHistogram()->GetYaxis()->SetRangeUser(1e-9,1);
1624 // gPad->SetGrid(0, 0);
1625 // gPad->SetLogy(1);
1626 }
1627
1629
1630 return;
1631 }
1632
1633 // graphs("ts visualize");return;
1634
1635 TGraphErrors *out = new TGraphErrors;
1636 out->SetName(GetName());
1637
1638 TString title = (!axes().empty()) ? TString::Format(";%s", axes().first()->GetTitle()) : "";
1639
1641 if (!empty() && axes().size() == 1) {
1642 for (auto &p : *this) {
1643 if (p.fPllType != xRooFit::Asymptotics::TwoSided) {
1644 pllType = p.fPllType;
1645 }
1646 }
1647 title += ";";
1648 title += front().tsTitle(true);
1649 }
1650
1651 out->SetTitle(title);
1652 *dynamic_cast<TAttFill *>(out) = *this;
1653 *dynamic_cast<TAttLine *>(out) = *this;
1654 *dynamic_cast<TAttMarker *>(out) = *this;
1655 out->SetBit(kCanDelete);
1656
1657 if (!gPad)
1660 if (!sOpt.Contains("same"))
1661 basePad->Clear();
1662
1663 bool doFits = false;
1664 if (sOpt.Contains("fits")) {
1665 doFits = true;
1666 sOpt.ReplaceAll("fits", "");
1667 }
1668
1669 auto mainPad = gPad;
1670
1671 out->SetEditable(false);
1672
1673 if (doFits) {
1674 gPad->Divide(1, 2);
1675 gPad->cd(1);
1676 gPad->SetBottomMargin(gPad->GetBottomMargin() * 2.); // increase margin to be same as before
1677 gPad->SetGrid();
1678 out->Draw(sOpt);
1679 basePad->cd(2);
1680 mainPad = basePad->GetPad(1);
1681 } else {
1682 gPad->SetGrid();
1683 out->Draw("ALP");
1684 }
1685
1686 std::pair<double, double> minMax(std::numeric_limits<double>::infinity(), -std::numeric_limits<double>::infinity());
1687 for (auto &p : *this) {
1688 if (p.fPllType != pllType)
1689 continue; // must all have same pll type
1690 auto val = p.pll(true).first;
1691 if (std::isnan(val))
1692 continue;
1693 minMax.first = std::min(minMax.first, val);
1694 minMax.second = std::max(minMax.second, val);
1695 }
1696 if (minMax.first < std::numeric_limits<double>::infinity())
1697 out->SetMinimum(minMax.first);
1698 if (minMax.second > -std::numeric_limits<double>::infinity())
1699 out->SetMaximum(minMax.second);
1700
1701 TGraph *badPoints = nullptr;
1702
1703 TStopwatch s;
1704 s.Start();
1705 std::shared_ptr<const RooFitResult> ufr;
1706 for (auto &p : *this) {
1707 if (p.fPllType != pllType)
1708 continue; // must all have same pll type
1709 auto val = p.pll().first;
1710 if (!ufr)
1711 ufr = p.ufit();
1712 if (out->GetN() == 0 && ufr && ufr->status() == 0) {
1713 out->SetPoint(out->GetN(),
1714 ufr->floatParsFinal().getRealValue(axes().first()->GetName(),
1715 ufr->constPars().getRealValue(axes().first()->GetName())),
1716 0.);
1717 out->SetPointError(out->GetN() - 1, 0, ufr->edm());
1718 }
1719 if (auto fr = p.fNull_cfit;
1720 fr && doFits) { // access member to avoid unnecessarily creating fit result if wasnt needed
1721 // create a new subpad and draw fitResult on it
1722 auto _pad = gPad;
1723 auto pad =
1724 new TPad(fr->GetName(), TString::Format("%s = %g", poi().first()->GetTitle(), p.fNullVal()), 0, 0, 1., 1);
1725 pad->SetNumber(out->GetN() + 1); // can't use "0" for a subpad
1726 pad->cd();
1727 xRooNode(fr).Draw("goff");
1728 _pad->cd();
1729 //_pad->GetListOfPrimitives()->AddFirst(pad);
1730 pad->AppendPad();
1731 }
1732 if (std::isnan(val) && p.status() != 0) {
1733 if (!badPoints) {
1734 badPoints = new TGraph;
1735 badPoints->SetBit(kCanDelete);
1736 badPoints->SetName("badPoints");
1737 badPoints->SetMarkerStyle(5);
1738 badPoints->SetMarkerColor(kRed);
1739 badPoints->SetMarkerSize(1);
1740 out->GetListOfFunctions()->Add(badPoints, "P");
1741 }
1742 badPoints->SetPoint(badPoints->GetN(), p.fNullVal(), out->Eval(p.fNullVal()));
1743 mainPad->Modified();
1744 } else if (!std::isnan(val)) {
1745 out->SetPoint(out->GetN(), p.coords->getRealValue(axes().first()->GetName()), p.pll().first);
1746 out->SetPointError(out->GetN() - 1, 0, p.pll().second);
1747 out->Sort();
1748
1749 // reposition bad points
1750 if (badPoints) {
1751 for (int i = 0; i < badPoints->GetN(); i++)
1752 badPoints->SetPointY(i, out->Eval(badPoints->GetPointX(i)));
1753 }
1754
1755 mainPad->Modified();
1756 }
1757 if (s.RealTime() > 3) { // stops the clock
1758 basePad->GetCanvas()->Paint();
1759 basePad->GetCanvas()->Update();
1761 s.Reset();
1762 s.Start();
1763 }
1764 s.Continue();
1765 }
1766 basePad->GetCanvas()->Paint();
1767 basePad->GetCanvas()->Update();
1768#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 30, 00)
1769 basePad->GetCanvas()->ResetUpdated(); // stops previous canvas being replaced in a jupyter notebook
1770#endif
1772
1773 // finish by overlaying ufit
1774 if (ufr && doFits) {
1775 auto _pad = gPad;
1776 auto pad = new TPad(ufr->GetName(), "unconditional fit", 0, 0, 1., 1.);
1777 pad->SetNumber(-1);
1778 pad->cd();
1779 xRooNode(ufr).Draw("goff");
1780 _pad->cd();
1781 pad->AppendPad();
1782
1783 // draw one more pad to represent the selected, and draw the ufit pad onto that pad
1784 pad = new TPad("selected", "selected", 0, 0, 1, 1);
1785 pad->Draw();
1786 pad->cd();
1787 basePad->GetPad(2)->GetPad(-1)->AppendPad();
1788 pad->Modified();
1789 pad->Update();
1791
1792 basePad->cd();
1793 }
1794
1795 if (doFits) {
1796 if (!xRooNode::gIntObj) {
1798 }
1799 gPad->GetCanvas()->Connect("Highlighted(TVirtualPad*,TObject*,Int_t,Int_t)", "xRooNode::InteractiveObject",
1800 xRooNode::gIntObj, "Interactive_PLLPlot(TVirtualPad*,TObject*,Int_t,Int_t)");
1801 }
1802
1803 return;
1804}
1805
1807{
1808
1809 RooStats::HypoTestInverterResult *out = nullptr;
1810
1811 auto _axes = axes();
1812 if (_axes.empty())
1813 return out;
1814
1815 out = new RooStats::HypoTestInverterResult(GetName(), *dynamic_cast<RooRealVar *>(_axes.at(0)), 0.95);
1816 out->SetTitle(GetTitle());
1817
1818 for (auto &p : *this) {
1819 double _x = p.coords->getRealValue(_axes.at(0)->GetName(), std::numeric_limits<double>::quiet_NaN());
1820 out->Add(_x, p.result());
1821 }
1822
1823 return out;
1824}
1825
#define c(i)
Definition RSha256.hxx:101
#define g(i)
Definition RSha256.hxx:105
#define a(i)
Definition RSha256.hxx:99
#define e(i)
Definition RSha256.hxx:103
double _val
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
@ kBlack
Definition Rtypes.h:66
@ kGreen
Definition Rtypes.h:67
@ kBlue
Definition Rtypes.h:67
@ kYellow
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
R__EXTERN TEnv * gEnv
Definition TEnv.h:126
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
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:252
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t SetFillStyle
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 offset
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 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 x1
Option_t Option_t SetFillColor
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
Option_t Option_t TPoint TPoint const char y1
char name[80]
Definition TGX11.cxx:145
@ kCanDelete
Definition TObject.h:375
#define gROOT
Definition TROOT.h:426
R__EXTERN TStyle * gStyle
Definition TStyle.h:442
R__EXTERN TSystem * gSystem
Definition TSystem.h:582
#define gPad
static std::pair< double, double > matchPrecision(const std::pair< double, double > &in)
Definition xRooFit.cxx:2089
std::pair< std::shared_ptr< RooAbsData >, std::shared_ptr< const RooAbsCollection > > fData
Definition xRooNLLVar.h:181
std::shared_ptr< const RooAbsCollection > coords
Definition xRooNLLVar.h:264
std::shared_ptr< xRooHypoPoint > asimov(bool readOnly=false)
std::shared_ptr< const RooFitResult > fAlt_cfit
Definition xRooNLLVar.h:266
xValueWithError limit(const char *type="cls", double nSigma=std::numeric_limits< double >::quiet_NaN()) const
static xValueWithError GetLimit(const TGraph &pValues, double target=std::numeric_limits< double >::quiet_NaN())
std::map< std::string, xValueWithError > limits(const char *opt="cls", const std::vector< double > &nSigmas={0, 1, 2, -1, -2, std::numeric_limits< double >::quiet_NaN()}, double relUncert=std::numeric_limits< double >::infinity())
bool AddModel(const xRooNode &pdf, const char *validity="")
std::shared_ptr< TMultiGraph > graphs(const char *opt)
xValueWithError findlimit(const char *opt, double relUncert=std::numeric_limits< double >::infinity(), unsigned int maxTries=20)
int AddPoints(const char *parName, size_t nPoints, double low, double high)
std::shared_ptr< TGraphErrors > graph(const char *opt) const
void Print(Option_t *opt="") const override
Print TNamed name and title.
xRooHypoSpace(const char *name="", const char *title="")
int scan(const char *type, size_t nPoints, double low=std::numeric_limits< double >::quiet_NaN(), double high=std::numeric_limits< double >::quiet_NaN(), const std::vector< double > &nSigmas={0, 1, 2, -1, -2, std::numeric_limits< double >::quiet_NaN()}, double relUncert=0.1)
std::shared_ptr< xRooNode > pdf(const RooAbsCollection &parValues) const
void Draw(Option_t *opt="") override
Default Draw method for all objects.
std::shared_ptr< RooAbsPdf > pdf() const
Definition xRooNLLVar.h:453
std::shared_ptr< RooArgSet > pars(bool stripGlobalObs=true) const
The xRooNode class is designed to wrap over a TObject and provide functionality to aid with interacti...
Definition xRooNode.h:52
void Draw(Option_t *opt="") override
Default Draw method for all objects.
static InteractiveObject * gIntObj
Definition xRooNode.h:524
xRooNode pars() const
List of parameters (non-observables) of this node.
const_iterator end() const
A space to attach TBranches.
virtual value_type getCurrentIndex() const
Return index number of current state.
Abstract container object that can hold multiple RooAbsArg objects.
RooAbsCollection * selectByAttrib(const char *name, bool value) const
Create a subset of the current collection, consisting only of those elements with the specified attri...
double getRealValue(const char *name, double defVal=0.0, bool verbose=false) const
Get value of a RooAbsReal stored in set with given name.
Int_t getCatIndex(const char *name, Int_t defVal=0, bool verbose=false) const
Get index value of a RooAbsCategory stored in set with given name.
virtual bool remove(const RooAbsArg &var, bool silent=false, bool matchByNameOnly=false)
Remove the specified argument from our list.
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...
virtual RooAbsArg * addClone(const RooAbsArg &var, bool silent=false)
Add a clone of the specified argument to list.
void setName(const char *name)
RooAbsArg * find(const char *name) const
Find object with given name in list.
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 ...
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Represents a constant real-valued object.
Definition RooConstVar.h:23
Container class to hold unbinned data.
Definition RooDataSet.h:32
Variable that can be changed from the outside.
Definition RooRealVar.h:37
HypoTestInverterResult class holds the array of hypothesis test results and compute a confidence inte...
HypoTestResult * GetResult(int index) const
return a pointer to the i^th result object
int ArraySize() const
number of entries in the results array
double GetXValue(int index) const
function to return the value of the parameter of interest for the i^th entry in the results
bool Add(const HypoTestInverterResult &otherResult)
merge with the content of another HypoTestInverterResult object
RooArgSet * GetParameters() const override
return a cloned list with the parameter of interest
Fill Area Attributes class.
Definition TAttFill.h:21
Line Attributes class.
Definition TAttLine.h:21
virtual void SetLineStyle(Style_t lstyle)
Set the line style.
Definition TAttLine.h:46
Marker Attributes class.
Definition TAttMarker.h:21
virtual Size_t GetMarkerSize() const
Return the marker size.
Definition TAttMarker.h:35
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:43
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition TAttMarker.h:48
static TCanvas * MakeDefCanvas()
Static function to build a default canvas.
Definition TCanvas.cxx:1509
Describe directory structure in memory.
Definition TDirectory.h:45
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition TEnv.cxx:511
A TGraphErrors is a TGraph with error bars.
virtual void SetPointError(Double_t ex, Double_t ey)
Set ex and ey values for point pointed by the mouse.
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
virtual Double_t GetPointX(Int_t i) const
Get x value for point i.
Definition TGraph.cxx:1574
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition TGraph.cxx:2387
virtual void SetMaximum(Double_t maximum=-1111)
Set the maximum of the graph.
Definition TGraph.cxx:2369
static Bool_t CompareX(const TGraph *gr, Int_t left, Int_t right)
Return kTRUE if fX[left] > fX[right]. Can be used by Sort.
Definition TGraph.cxx:733
Int_t GetN() const
Definition TGraph.h:131
virtual void Sort(Bool_t(*greater)(const TGraph *, Int_t, Int_t)=&TGraph::CompareX, Bool_t ascending=kTRUE, Int_t low=0, Int_t high=-1111)
Sorts the points of this TGraph using in-place quicksort (see e.g.
Definition TGraph.cxx:2531
TList * GetListOfFunctions() const
Definition TGraph.h:125
virtual Double_t Eval(Double_t x, TSpline *spline=nullptr, Option_t *option="") const
Interpolate points in this graph at x using a TSpline.
Definition TGraph.cxx:981
virtual Int_t RemovePoint()
Delete point close to the mouse position Returns index of removed point (or -1 if nothing was changed...
Definition TGraph.cxx:2067
void SetName(const char *name="") override
Set graph name.
Definition TGraph.cxx:2426
void Draw(Option_t *chopt="") override
Draw this graph with its current attributes.
Definition TGraph.cxx:859
virtual Double_t GetPointY(Int_t i) const
Get y value for point i.
Definition TGraph.cxx:1585
void SetTitle(const char *title="") override
Change (i.e.
Definition TGraph.cxx:2442
virtual void SetPointY(Int_t i, Double_t y)
Set y value for point i.
Definition TGraph.cxx:2419
virtual void SetEditable(Bool_t editable=kTRUE)
if editable=kFALSE, the graph cannot be modified with the mouse by default a TGraph is editable
Definition TGraph.cxx:2339
virtual void SetMinimum(Double_t minimum=-1111)
Set the minimum of the graph.
Definition TGraph.cxx:2378
This class displays a legend box (TPaveText) containing several legend entries.
Definition TLegend.h:23
TObject * FindObject(const char *name) const override
Find an object in this list using its name.
Definition TList.cxx:708
void Add(TObject *obj) override
Definition TList.h:81
A TMemFile is like a normal TFile except that it reads and writes only from memory.
Definition TMemFile.h:27
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
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:173
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
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:149
virtual void SetNameTitle(const char *name, const char *title)
Set all the TNamed parameters (name and title).
Definition TNamed.cxx:163
Mother of all ROOT objects.
Definition TObject.h:42
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:885
The most important graphics class in the ROOT system.
Definition TPad.h:28
Bool_t Connect(const char *signal, const char *receiver_class, void *receiver, const char *slot)
Non-static method is used to connect from the signal of this object to the receiver slot.
Definition TQObject.cxx:865
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
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:2385
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:660
Float_t GetPadRightMargin() const
Definition TStyle.h:216
Float_t GetPadTopMargin() const
Definition TStyle.h:214
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition TSystem.cxx:418
This class defines a UUID (Universally Unique IDentifier), also known as GUIDs (Globally Unique IDent...
Definition TUUID.h:42
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
TLine * line
double gaussian_quantile(double z, double sigma)
Inverse ( ) of the cumulative distribution function of the lower tail of the normal (Gaussian) distri...
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
TGraphErrors * gr
Definition legend1.C:25
leg
Definition legend1.C:34
return c2
Definition legend2.C:14
void writeStdoutLine(const char *msg)
bool isPythonInitialized()
#define BEGIN_XROOFIT_NAMESPACE
Definition Config.h:24
#define END_XROOFIT_NAMESPACE
Definition Config.h:25
TMarker m
Definition textangle.C:8
TLine l
Definition textangle.C:4