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. :
1021 // -1.));
1022 } else {
1023 // up->SetPoint(up->GetN(), out->GetPointX(i), out->GetPointY(i) - out->GetErrorY(i) * (above ? 1. : -1.));
1024 down->SetPoint(down->GetN(), out->GetPointX(i), out->GetPointY(i) + out->GetErrorY(i) * (above ? 1. : -1.));
1025 }
1026 }
1027 // now go back round in reverse
1028 for (int i = out->GetN() - 1; i >= 0; i--) {
1029 if (i < out->GetN() - nPointsDown) {
1030 up->SetPoint(up->GetN(), out->GetPointX(i), out->GetPointY(i) - out->GetErrorY(i) * (above ? 1. : -1.));
1031 // down->SetPoint(down->GetN(), out->GetPointX(i), out->GetPointY(i) - out->GetErrorY(i) * (above ? 1. :
1032 // -1.));
1033 } else {
1034 // up->SetPoint(up->GetN(), out->GetPointX(i), out->GetPointY(i) - out->GetErrorY(i) * (above ? 1. : -1.));
1035 down->SetPoint(down->GetN(), out->GetPointX(i), out->GetPointY(i) - out->GetErrorY(i) * (above ? 1. : -1.));
1036 }
1037 }
1038 }
1039
1040 if (visualize) {
1041 // draw result
1042 if (!gPad && gROOT->GetSelectedPad())
1043 gROOT->GetSelectedPad()->cd();
1044 if (gPad)
1045 gPad->Clear();
1046 out->DrawClone(expBand ? "AF" : "ALP")->SetBit(kCanDelete);
1047#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 30, 00)
1048 if (auto pad = gROOT->GetSelectedPad()) {
1049 pad->GetCanvas()->ResetUpdated(); // stops previous canvas being replaced in a jupyter notebook
1050 }
1051#endif
1053 }
1054
1055 return out;
1056}
1057
1058std::shared_ptr<TMultiGraph> xRooNLLVar::xRooHypoSpace::graphs(const char *opt)
1059{
1060 TString sOpt(opt);
1061 sOpt.ToLower();
1062 std::shared_ptr<TMultiGraph> out;
1063 if (sOpt.Contains("pcls") || sOpt.Contains("pnull") || sOpt.Contains("ts")) {
1064
1065 bool visualize = sOpt.Contains("visualize");
1066 sOpt.ReplaceAll("visualize", "");
1067
1068 auto exp2 = graph(sOpt + " exp2");
1069 auto exp1 = graph(sOpt + " exp1");
1070 auto exp = graph(sOpt + " exp");
1071 bool doObs = true;
1072 // for(auto& hp : *this) { if(hp.isExpected) {doObs=false; break;} }
1073 auto obs = (doObs) ? graph(sOpt) : nullptr;
1074
1075 out = std::make_shared<TMultiGraph>(GetName(), GetTitle());
1076 if (exp2 && exp2->GetN() > 1)
1077 out->Add(static_cast<TGraph *>(exp2->Clone()), "FP");
1078 if (exp1 && exp1->GetN() > 1)
1079 out->Add(static_cast<TGraph *>(exp1->Clone()), "FP");
1080 if (exp && exp->GetN() > 1)
1081 out->Add(static_cast<TGraph *>(exp->Clone()), "LP");
1082 if (obs && obs->GetN() > 1)
1083 out->Add(static_cast<TGraph *>(obs->Clone()), "LP");
1084
1085 if (!out->GetListOfGraphs()) {
1086 return nullptr;
1087 }
1088
1089 TGraph *testedPoints = nullptr;
1090 if (sOpt.Contains("pcls")) {
1091 TGraph *line = new TGraph;
1092 line->SetName("alpha");
1093 line->SetLineStyle(2);
1094 line->SetEditable(false);
1095 line->SetPoint(line->GetN(), out->GetHistogram()->GetXaxis()->GetXmin() - 10, 0.05);
1096 testedPoints = new TGraph;
1097 testedPoints->SetName("hypoPoints");
1098 testedPoints->SetEditable(false);
1099 testedPoints->SetMarkerStyle(24);
1100 testedPoints->SetMarkerSize(0.4); // use line to indicate tested points
1101 if (exp) {
1102 for (int i = 0; i < exp->GetN(); i++) {
1103 testedPoints->SetPoint(testedPoints->GetN(), exp->GetPointX(i), 0.05);
1104 }
1105 }
1106 line->SetPoint(line->GetN(), out->GetHistogram()->GetXaxis()->GetXmax() + 10, 0.05);
1108 out->GetListOfFunctions()->Add(line, "L");
1109 }
1110 if (exp) {
1111 out->GetHistogram()->GetXaxis()->SetTitle(exp->GetHistogram()->GetXaxis()->GetTitle());
1112 out->GetHistogram()->GetYaxis()->SetTitle(exp->GetHistogram()->GetYaxis()->GetTitle());
1113 }
1114 TLegend *leg = nullptr;
1115 if (out->GetListOfGraphs()->GetEntries() > 1) {
1116 leg = new TLegend(1. - gStyle->GetPadRightMargin() - 0.3, 1. - gStyle->GetPadTopMargin() - 0.35,
1117 1. - gStyle->GetPadRightMargin() - 0.05, 1. - gStyle->GetPadTopMargin() - 0.05);
1118 leg->SetName("legend");
1119 leg->SetBit(kCanDelete);
1120
1121 out->GetListOfFunctions()->Add(leg);
1122 // out->GetListOfFunctions()->Add(out->GetHistogram()->Clone(".axis"),"sameaxis"); // redraw axis
1123
1124 for (auto g : *out->GetListOfGraphs()) {
1125 if (dynamic_cast<TGraph *>(g)->GetListOfFunctions()->FindObject("down")) {
1126 leg->AddEntry(g, "", "F");
1127 } else {
1128 leg->AddEntry(g, "", "LPE");
1129 }
1130 }
1131 }
1132
1133 auto addToLegend = [](TLegend *l, const char *label, const std::pair<double, double> val) {
1134 if (l) {
1135 l->AddEntry((TObject *)nullptr,
1136 TString::Format("%s%s: %g #pm %g%s", std::isfinite(val.second) ? "" : "#color[2]{", label,
1137 val.first, val.second, std::isfinite(val.second) ? "" : "}"),
1138 "");
1139 }
1140 };
1141
1142 if (sOpt.Contains("pcls")) {
1143 // add current limit estimates to legend
1144 if (exp2 && exp2->GetN() > 1) {
1145 auto l = xRooFit::matchPrecision(GetLimit(*graph(sOpt + "exp-2")));
1146 addToLegend(leg, "-2#sigma", l);
1147 }
1148 if (exp1 && exp1->GetN() > 1) {
1149 auto l = xRooFit::matchPrecision(GetLimit(*graph(sOpt + "exp-1")));
1150 addToLegend(leg, "-1#sigma", l);
1151 }
1152 if (exp && exp->GetN() > 1) {
1153 auto l = xRooFit::matchPrecision(GetLimit(*exp));
1154 addToLegend(leg, "0#sigma", l);
1155 }
1156 if (exp1 && exp1->GetN() > 1) {
1157 auto l = xRooFit::matchPrecision(GetLimit(*graph(sOpt + "exp+1")));
1158 addToLegend(leg, "+1#sigma", l);
1159 }
1160 if (exp2 && exp2->GetN() > 1) {
1161 auto l = xRooFit::matchPrecision(GetLimit(*graph(sOpt + "exp+2")));
1162 addToLegend(leg, "+2#sigma", l);
1163 }
1164 if (obs && obs->GetN() > 1) {
1165 auto l = xRooFit::matchPrecision(GetLimit(*obs));
1166 addToLegend(leg, "Observed", l);
1167 }
1168 }
1169 if (testedPoints)
1170 out->Add(testedPoints, "P");
1171
1172 if (visualize) {
1173 if (!gPad && gROOT->GetSelectedPad())
1174 gROOT->GetSelectedPad()->cd();
1175 if (gPad)
1176 gPad->Clear();
1177 auto gra2 = static_cast<TMultiGraph *>(out->DrawClone("A"));
1178 gra2->SetBit(kCanDelete);
1179 if (sOpt.Contains("pcls") || sOpt.Contains("pnull")) {
1180 gra2->SetMinimum(1e-6);
1181 gra2->SetMaximum(1);
1182 }
1183 if (gPad) {
1184 gPad->RedrawAxis();
1185 gPad->GetCanvas()->Paint();
1186 gPad->GetCanvas()->Update();
1187#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 30, 00)
1188 gPad->GetCanvas()->ResetUpdated(); // stops previous canvas being replaced in a jupyter notebook
1189#endif
1190 }
1192 }
1193 }
1194
1195 return out;
1196}
1197
1199{
1200
1201 if (std::isnan(target)) {
1202 target = 1. - gEnv->GetValue("xRooHypoSpace.CL", 95.) / 100.;
1203 }
1204
1205 auto gr = std::make_shared<TGraph>(pValues);
1206 // remove any nan points and duplicates
1207 int i = 0;
1208 std::set<double> existingX;
1209 while (i < gr->GetN()) {
1210 if (std::isnan(gr->GetPointY(i))) {
1211 gr->RemovePoint(i);
1212 } else if (existingX.find(gr->GetPointX(i)) != existingX.end()) {
1213 gr->RemovePoint(i);
1214 } else {
1215 existingX.insert(gr->GetPointX(i));
1216 // convert to log ....
1217 gr->SetPointY(i, log(std::max(gr->GetPointY(i), 1e-10)));
1218 i++;
1219 }
1220 }
1221
1222 gr->Sort();
1223
1224 // simple linear extrapolation to critical value ... return nan if problem
1225 if (gr->GetN() < 2) {
1226 return std::pair<double, double>(std::numeric_limits<double>::quiet_NaN(), 0);
1227 }
1228
1229 double alpha = log(target);
1230
1231 bool above = gr->GetPointY(0) > alpha;
1232 for (int ii = 1; ii < gr->GetN(); ii++) {
1233 if ((above && (gr->GetPointY(ii) <= alpha)) || (!above && (gr->GetPointY(ii) >= alpha))) {
1234 // found the limit ... return linearly extrapolated point
1235 double lim = gr->GetPointX(ii - 1) + (gr->GetPointX(ii) - gr->GetPointX(ii - 1)) *
1236 (alpha - gr->GetPointY(ii - 1)) /
1237 (gr->GetPointY(ii) - gr->GetPointY(ii - 1));
1238 // use points either side as error
1239 double err = std::max(lim - gr->GetPointX(ii - 1), gr->GetPointX(ii) - lim);
1240 // give err negative sign to indicate if error due to negative side
1241 if ((lim - gr->GetPointX(ii - 1)) > (gr->GetPointX(ii) - lim))
1242 err *= -1;
1243 return std::pair(lim, err);
1244 }
1245 }
1246 // if reach here need to extrapolate ...
1247 if ((above && gr->GetPointY(gr->GetN() - 1) <= gr->GetPointY(0)) ||
1248 (!above && gr->GetPointY(gr->GetN() - 1) >= gr->GetPointY(0))) {
1249 // extrapolating above based on last two points
1250 // in fact, if 2nd last point is a p=1 (log(p)=0) then go back
1251 int offset = 2;
1252 while (offset < gr->GetN() && gr->GetPointY(gr->GetN() - offset) == 0)
1253 offset++;
1254 double x1 = gr->GetPointX(gr->GetN() - offset);
1255 double y1 = gr->GetPointY(gr->GetN() - offset);
1256 double m = (gr->GetPointY(gr->GetN() - 1) - y1) / (gr->GetPointX(gr->GetN() - 1) - x1);
1257 if (m == 0.)
1258 return std::pair(std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity());
1259 return std::pair((alpha - y1) / m + x1, std::numeric_limits<double>::infinity());
1260 } else {
1261 // extrapolating below based on first two points
1262 double x1 = gr->GetPointX(0);
1263 double y1 = gr->GetPointY(0);
1264 double m = (gr->GetPointY(1) - y1) / (gr->GetPointX(1) - x1);
1265 if (m == 0.)
1266 return std::pair(-std::numeric_limits<double>::infinity(), -std::numeric_limits<double>::infinity());
1267 return std::pair((alpha - y1) / m + x1, -std::numeric_limits<double>::infinity());
1268 }
1269}
1270
1272{
1273 TString sOpt = TString::Format("p%s", type);
1274 if (std::isnan(nSigma)) {
1275 sOpt += "obs";
1276 } else {
1277 sOpt += TString::Format("exp%s%d", nSigma > 0 ? "+" : "", int(nSigma));
1278 }
1279 return GetLimit(*graph(sOpt + " readonly"));
1280}
1281
1283xRooNLLVar::xRooHypoSpace::findlimit(const char *opt, double relUncert, unsigned int maxTries)
1284{
1285 TString sOpt(opt);
1286 bool visualize = sOpt.Contains("visualize");
1287 sOpt.ReplaceAll("visualize", "");
1288 std::shared_ptr<TGraphErrors> gr = graph(sOpt + " readonly");
1289 if (visualize) {
1290 auto gra = graphs(sOpt.Contains("toys") ? "pcls readonly toys" : "pcls readonly");
1291 if (gra) {
1292 if (!gPad)
1293 gra->Draw(); // in 6.28 DrawClone wont make the gPad defined :( ... so Draw then clear and Draw Clone
1294 gPad->Clear();
1295 auto gra2 = static_cast<TMultiGraph *>(gra->DrawClone("A"));
1296 gra2->SetBit(kCanDelete);
1297 gra2->SetMinimum(1e-9);
1298 gra2->SetMaximum(1);
1299 gPad->RedrawAxis();
1300 gPad->GetCanvas()->Paint();
1301 gPad->GetCanvas()->Update();
1302#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 30, 00)
1303 gPad->GetCanvas()->ResetUpdated(); // stops previous canvas being replaced in a jupyter notebook
1304#endif
1306 }
1307 }
1308
1309 // resync parameter boundaries from nlls (may have been modified by fits)
1310 for (auto p : axes()) {
1311 for (auto &[pdf, nll] : fNlls) {
1312 if (auto _v = dynamic_cast<RooRealVar *>(nll->pars()->find(*p))) {
1313 dynamic_cast<RooRealVar *>(p)->setRange(_v->getMin(), _v->getMax());
1314 }
1315 }
1316 }
1317
1318 if (!gr || gr->GetN() < 2) {
1319 auto v = (axes().empty()) ? nullptr : dynamic_cast<RooRealVar *>(*axes().rbegin());
1320 if (!v)
1321 return std::pair(std::numeric_limits<double>::quiet_NaN(), 0.);
1322 double muMax = std::min(std::min(v->getMax("physical"), v->getMax()), v->getMax("scan"));
1323 double muMin = std::max(std::max(v->getMin("physical"), v->getMin()), v->getMin("scan"));
1324 if (!gr || gr->GetN() < 1) {
1325 if (maxTries == 0 || std::isnan(AddPoint(TString::Format("%s=%g", v->GetName(), muMin)).getVal(sOpt).first)) {
1326 // first point failed ... give up
1327 ::Error("findlimit", "Problem evaluating First Point %s @ %s=%g", sOpt.Data(), v->GetName(), muMin);
1328 return std::pair(std::numeric_limits<double>::quiet_NaN(), 0.);
1329 }
1330 gr.reset();
1331 return findlimit(opt, relUncert, maxTries - 1); // do this to resync parameter limits
1332 }
1333
1334 // can approximate expected limit using
1335 // mu_hat + sigma_mu*ROOT::Math::gaussian_quantile(1.-alpha/2.,1) for cls
1336 // or mu_hat + sigma_mu*ROOT::Math::gaussian_quantile((1.-alpha),1) for cls+b
1337 // get a very first estimate of sigma_mu from ufit to expected data, take error on mu as sigma_mu
1338 double nextPoint = muMin + (muMax - muMin) / 50;
1339
1340 // if done an expected limit, assume data is like expected and choose expected limit point as first test point
1341 if (sOpt.Contains("obs")) {
1342 TString sOpt2 = sOpt;
1343 sOpt2.ReplaceAll("obs", "exp");
1344 auto expLim = findlimit(sOpt2, std::numeric_limits<double>::infinity(), 0);
1345 if (!std::isnan(expLim.first) && expLim.first < nextPoint)
1346 nextPoint = expLim.first;
1347 }
1348
1349 auto point =
1350 (sOpt.Contains("exp")) ? back().asimov() : std::shared_ptr<xRooHypoPoint>(&back(), [](xRooHypoPoint *) {});
1351 point = nullptr;
1352 if (point && point->ufit()) {
1353 double rough_sigma_mu = point->mu_hat().getError();
1354 double another_estimate = point->mu_hat().getVal() + rough_sigma_mu * ROOT::Math::gaussian_quantile(0.95, 1);
1355 // if (another_estimate < nextPoint) {
1357 ::Info("xRooHypoSpace::findlimit", "Guessing %g based on rough sigma_mu = %g", nextPoint, rough_sigma_mu);
1358 //}
1359 }
1360
1361 if (maxTries == 0 || std::isnan(AddPoint(TString::Format("%s=%g", v->GetName(), nextPoint)).getVal(sOpt).first)) {
1362 // second point failed ... give up
1363 ::Error("xRooHypoSpace::findlimit", "Problem evaluating Second Point %s @ %s=%g", sOpt.Data(), v->GetName(),
1364 nextPoint);
1365 return std::pair(std::numeric_limits<double>::quiet_NaN(), 0.);
1366 }
1367 gr.reset();
1368 return findlimit(opt, relUncert, maxTries - 1);
1369 }
1370
1371 auto lim = GetLimit(*gr);
1372
1373 if (std::isnan(lim.first)) {
1374 return lim;
1375 }
1376
1377 auto v = dynamic_cast<RooRealVar *>(*axes().rbegin());
1378 double maxMu = std::min(std::min(v->getMax("physical"), v->getMax()), v->getMax("scan"));
1379 double minMu = std::max(std::max(v->getMin("physical"), v->getMin()), v->getMin("scan"));
1380
1381 // static double MIN_LIMIT_UNCERT = 1e-4; // stop iterating once uncert gets this small
1382 if (lim.first > -std::numeric_limits<double>::infinity() && lim.first < std::numeric_limits<double>::infinity() &&
1383 (std::abs(lim.second) <= relUncert * std::abs(lim.first) /* || std::abs(lim.second)<MIN_LIMIT_UNCERT*/))
1384 return lim;
1385
1386 double nextPoint;
1387
1388 if (lim.second == std::numeric_limits<double>::infinity()) {
1389 // limit was found by extrapolating to right
1390 nextPoint = lim.first;
1391 if (nextPoint == std::numeric_limits<double>::infinity() || nextPoint > maxMu) {
1392 nextPoint = gr->GetPointX(gr->GetN() - 1) + (maxMu - minMu) / 50;
1393 }
1394
1395 // prefer extrapolation with sigma_mu, if available, if it takes us further
1396 // as shape of p-value curve is usually
1397 auto point =
1398 (sOpt.Contains("exp")) ? back().asimov() : std::shared_ptr<xRooHypoPoint>(&back(), [](xRooHypoPoint *) {});
1399 point = nullptr;
1400 if (point && point->ufit()) {
1401 double rough_sigma_mu = point->mu_hat().getError();
1402 double another_estimate = point->mu_hat().getVal() + rough_sigma_mu * ROOT::Math::gaussian_quantile(0.95, 1);
1403 // if (another_estimate < nextPoint) {
1405 ::Info("xRooHypoSpace::findlimit", "Guessing %g based on rough sigma_mu = %g", nextPoint, rough_sigma_mu);
1406 //}
1407 }
1408 nextPoint = std::min(nextPoint + nextPoint * relUncert * 0.99, maxMu); // ensure we step over location if possible
1409
1410 if (nextPoint > maxMu)
1411 return lim;
1412 } else if (lim.second == -std::numeric_limits<double>::infinity()) {
1413 // limit from extrapolating to left
1414 nextPoint = lim.first;
1415 if (nextPoint < minMu)
1416 nextPoint = gr->GetPointX(0) - (maxMu - minMu) / 50;
1417 if (nextPoint < minMu)
1418 return lim;
1419 } else {
1420 nextPoint = lim.first + lim.second * relUncert * 0.99;
1421 }
1422
1423 // got here need a new point .... evaluate the estimated lim location +/- the relUncert (signed error takes care of
1424 // direction)
1425
1426 if (maxTries == 0) {
1427 ::Warning("xRooHypoSpace::findlimit", "Reached max number of point evaluations");
1428 return lim;
1429 }
1430
1431 ::Info("xRooHypoSpace::findlimit", "%s -- Testing new point @ %s=%g (delta=%g)", sOpt.Data(), v->GetName(),
1432 nextPoint, lim.second);
1433 if (std::isnan(AddPoint(TString::Format("%s=%g", v->GetName(), nextPoint)).getVal(sOpt).first)) {
1434 ::Error("xRooHypoSpace::findlimit", "Problem evaluating %s @ %s=%g", sOpt.Data(), v->GetName(), nextPoint);
1435 return lim;
1436 }
1437 gr.reset();
1438 return findlimit(opt, relUncert, maxTries - 1);
1439}
1440
1442{
1443
1444 TString sOpt(opt);
1445 sOpt.ToLower();
1446
1447 if ((sOpt == "" || sOpt == "same") && !empty()) {
1448 if (front().fPllType == xRooFit::Asymptotics::OneSidedPositive) {
1449 sOpt += "pcls"; // default to showing cls p-value scan if drawing a limit
1450 for (auto &hp : *this) {
1451 if (!hp.nullToys.empty() || !hp.altToys.empty()) {
1452 sOpt += " toys";
1453 break; // default to toys if done toys
1454 }
1455 }
1456 } else if (front().fPllType == xRooFit::Asymptotics::TwoSided) {
1457 sOpt += "ts";
1458 }
1459 }
1460
1461 // split up by ; and call Draw for each (with 'same' appended)
1462 auto _axes = axes();
1463 if (_axes.empty())
1464 return;
1465
1466 if (sOpt == "status") {
1467 // draw the points in the space
1468 if (_axes.size() <= 2) {
1469 TGraphErrors *out = new TGraphErrors;
1470 out->SetBit(kCanDelete);
1471 out->SetName("points");
1472 out->SetMarkerSize(0.5);
1473 TGraph *tsAvail = new TGraph;
1474 tsAvail->SetName("ts");
1475 tsAvail->SetBit(kCanDelete);
1476 tsAvail->SetMarkerStyle(20);
1477 TGraph *expAvail = new TGraph;
1478 expAvail->SetName("exp");
1479 expAvail->SetBit(kCanDelete);
1480 expAvail->SetMarkerStyle(25);
1481 expAvail->SetMarkerSize(out->GetMarkerSize() * 1.5);
1482 TGraph *badPoints = new TGraph;
1483 badPoints->SetName("bad_ufit");
1484 badPoints->SetBit(kCanDelete);
1485 badPoints->SetMarkerStyle(5);
1486 badPoints->SetMarkerColor(kRed);
1487 badPoints->SetMarkerSize(out->GetMarkerSize());
1488 TGraph *badPoints2 = new TGraph;
1489 badPoints2->SetName("bad_cfit_null");
1490 badPoints2->SetBit(kCanDelete);
1491 badPoints2->SetMarkerStyle(2);
1492 badPoints2->SetMarkerColor(kRed);
1493 badPoints2->SetMarkerSize(out->GetMarkerSize());
1494
1495 out->SetTitle(TString::Format("%s;%s;%s", GetTitle(), _axes.at(0)->GetTitle(),
1496 (_axes.size() == 1) ? "" : _axes.at(1)->GetTitle()));
1497 for (auto &p : *this) {
1498 bool _readOnly = p.nllVar ? p.nllVar->get()->getAttribute("readOnly") : false;
1499 if (p.nllVar)
1500 p.nllVar->get()->setAttribute("readOnly", true);
1501 double x = p.coords->getRealValue(_axes.at(0)->GetName());
1502 double y = _axes.size() == 1 ? p.ts_asymp().first : p.coords->getRealValue(_axes.at(1)->GetName());
1503 out->SetPoint(out->GetN(), x, y);
1504 if (!std::isnan(p.ts_asymp().first)) {
1505 if (_axes.size() == 1)
1506 out->SetPointError(out->GetN() - 1, 0, p.ts_asymp().second);
1507 tsAvail->SetPoint(tsAvail->GetN(), x, y);
1508 } else if (p.fUfit && (std::isnan(p.fUfit->minNll()) ||
1509 xRooNLLVar::xRooHypoPoint::allowedStatusCodes.find(p.fUfit->status()) ==
1511 badPoints->SetPoint(badPoints->GetN(), x, y);
1512 } else if (p.fNull_cfit && (std::isnan(p.fNull_cfit->minNll()) ||
1513 xRooNLLVar::xRooHypoPoint::allowedStatusCodes.find(p.fNull_cfit->status()) ==
1515 badPoints2->SetPoint(badPoints2->GetN(), x, y);
1516 }
1517 if (!std::isnan(p.ts_asymp(0).first)) {
1518 expAvail->SetPoint(expAvail->GetN(), x, y);
1519 } else if (p.asimov() && p.asimov()->fUfit &&
1520 (std::isnan(p.asimov()->fUfit->minNll()) ||
1521 xRooNLLVar::xRooHypoPoint::allowedStatusCodes.find(p.asimov()->fUfit->status()) ==
1523
1524 } else if (p.asimov() && p.asimov()->fNull_cfit &&
1525 (std::isnan(p.asimov()->fNull_cfit->minNll()) ||
1526 xRooNLLVar::xRooHypoPoint::allowedStatusCodes.find(p.asimov()->fNull_cfit->status()) ==
1528 }
1529 if (p.nllVar)
1530 p.nllVar->get()->setAttribute("readOnly", _readOnly);
1531 }
1532
1533 if (_axes.size() == 1) {
1534 TGraph tmp;
1535 for (int i = 0; i < out->GetN(); i++) {
1536 if (!std::isnan(out->GetPointY(i)))
1537 tmp.SetPoint(tmp.GetN(), out->GetPointX(i), out->GetPointY(i));
1538 }
1539 auto fixPoints = [&](TGraph *g) {
1540 for (int i = 0; i < g->GetN(); i++) {
1541 if (std::isnan(g->GetPointY(i)))
1542 g->SetPointY(i, std::isnan(tmp.Eval(g->GetPointX(i))) ? 0. : tmp.Eval(g->GetPointX(i)));
1543 }
1544 };
1545 fixPoints(out);
1550 }
1551
1552 out->SetMarkerStyle(4);
1553 out->Draw("AP");
1554 auto leg = new TLegend(1. - gPad->GetRightMargin() - 0.3, 1. - gPad->GetTopMargin() - 0.35,
1555 1. - gPad->GetRightMargin() - 0.05, 1. - gPad->GetTopMargin() - 0.05);
1556 leg->SetName("legend");
1557 leg->AddEntry(out, "Uncomputed", "P");
1558
1559 if (tsAvail->GetN()) {
1560 out->GetListOfFunctions()->Add(tsAvail, "P");
1561 leg->AddEntry(tsAvail, "Computed", "P");
1562 } else {
1563 delete tsAvail;
1564 }
1565 if (expAvail->GetN()) {
1566 out->GetListOfFunctions()->Add(expAvail, "P");
1567 leg->AddEntry(expAvail, "Expected computed", "P");
1568 } else {
1569 delete expAvail;
1570 }
1571 if (badPoints->GetN()) {
1572 out->GetListOfFunctions()->Add(badPoints, "P");
1573 leg->AddEntry(badPoints, "Bad ufit", "P");
1574 } else {
1575 delete badPoints;
1576 }
1577 if (badPoints2->GetN()) {
1578 out->GetListOfFunctions()->Add(badPoints2, "P");
1579 leg->AddEntry(badPoints2, "Bad null cfit", "P");
1580 } else {
1581 delete badPoints2;
1582 }
1583 leg->SetBit(kCanDelete);
1584 leg->Draw();
1585 // if(_axes.size()==1) out->GetHistogram()->GetYaxis()->SetRangeUser(0,1);
1586 gPad->SetGrid(true, _axes.size() > 1);
1587 if (_axes.size() == 1)
1588 gPad->SetLogy(false);
1589 }
1590
1592
1593 return;
1594 }
1595
1596 if (sOpt.Contains("pcls") || sOpt.Contains("pnull") || sOpt.Contains("ts")) {
1597 auto gra = graphs(sOpt + " readonly");
1598 if (!gPad && gROOT->GetSelectedPad())
1599 gROOT->GetSelectedPad()->cd();
1600 if (!sOpt.Contains("same") && gPad) {
1601 gPad->Clear();
1602 }
1603 if (gra) {
1604 if (!gPad)
1606 auto gra2 = static_cast<TMultiGraph *>(gra->DrawClone(sOpt.Contains("same") ? "" : "A"));
1607 gra2->SetBit(kCanDelete);
1608 if (sOpt.Contains("pcls") || sOpt.Contains("pnull")) {
1609 gra2->SetMinimum(1e-6);
1610 gra2->SetMaximum(1);
1611 }
1612 if (gPad) {
1613 gPad->RedrawAxis();
1614 gPad->GetCanvas()->Paint();
1615 gPad->GetCanvas()->Update();
1616#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 30, 00)
1617 gPad->GetCanvas()->ResetUpdated(); // stops previous canvas being replaced in a jupyter notebook
1618#endif
1620 }
1621 }
1622 if (!sOpt.Contains("same") && gPad) {
1623 // auto mg = static_cast<TMultiGraph*>(gPad->GetPrimitive(gra->GetName()));
1624 // mg->GetHistogram()->SetMinimum(1e-9);
1625 // mg->GetHistogram()->GetYaxis()->SetRangeUser(1e-9,1);
1626 // gPad->SetGrid(0, 0);
1627 // gPad->SetLogy(1);
1628 }
1629
1631
1632 return;
1633 }
1634
1635 // graphs("ts visualize");return;
1636
1637 TGraphErrors *out = new TGraphErrors;
1638 out->SetName(GetName());
1639
1640 TString title = (!axes().empty()) ? TString::Format(";%s", axes().first()->GetTitle()) : "";
1641
1643 if (!empty() && axes().size() == 1) {
1644 for (auto &p : *this) {
1645 if (p.fPllType != xRooFit::Asymptotics::TwoSided) {
1646 pllType = p.fPllType;
1647 }
1648 }
1649 title += ";";
1650 title += front().tsTitle(true);
1651 }
1652
1653 out->SetTitle(title);
1654 *dynamic_cast<TAttFill *>(out) = *this;
1655 *dynamic_cast<TAttLine *>(out) = *this;
1656 *dynamic_cast<TAttMarker *>(out) = *this;
1657 out->SetBit(kCanDelete);
1658
1659 if (!gPad)
1662 if (!sOpt.Contains("same"))
1663 basePad->Clear();
1664
1665 bool doFits = false;
1666 if (sOpt.Contains("fits")) {
1667 doFits = true;
1668 sOpt.ReplaceAll("fits", "");
1669 }
1670
1671 auto mainPad = gPad;
1672
1673 out->SetEditable(false);
1674
1675 if (doFits) {
1676 gPad->Divide(1, 2);
1677 gPad->cd(1);
1678 gPad->SetBottomMargin(gPad->GetBottomMargin() * 2.); // increase margin to be same as before
1679 gPad->SetGrid();
1680 out->Draw(sOpt);
1681 basePad->cd(2);
1682 mainPad = basePad->GetPad(1);
1683 } else {
1684 gPad->SetGrid();
1685 out->Draw("ALP");
1686 }
1687
1688 std::pair<double, double> minMax(std::numeric_limits<double>::infinity(), -std::numeric_limits<double>::infinity());
1689 for (auto &p : *this) {
1690 if (p.fPllType != pllType)
1691 continue; // must all have same pll type
1692 auto val = p.pll(true).first;
1693 if (std::isnan(val))
1694 continue;
1695 minMax.first = std::min(minMax.first, val);
1696 minMax.second = std::max(minMax.second, val);
1697 }
1698 if (minMax.first < std::numeric_limits<double>::infinity())
1699 out->SetMinimum(minMax.first);
1700 if (minMax.second > -std::numeric_limits<double>::infinity())
1701 out->SetMaximum(minMax.second);
1702
1703 TGraph *badPoints = nullptr;
1704
1705 TStopwatch s;
1706 s.Start();
1707 std::shared_ptr<const RooFitResult> ufr;
1708 for (auto &p : *this) {
1709 if (p.fPllType != pllType)
1710 continue; // must all have same pll type
1711 auto val = p.pll().first;
1712 if (!ufr)
1713 ufr = p.ufit();
1714 if (out->GetN() == 0 && ufr && ufr->status() == 0) {
1715 out->SetPoint(out->GetN(),
1716 ufr->floatParsFinal().getRealValue(axes().first()->GetName(),
1717 ufr->constPars().getRealValue(axes().first()->GetName())),
1718 0.);
1719 out->SetPointError(out->GetN() - 1, 0, ufr->edm());
1720 }
1721 if (auto fr = p.fNull_cfit;
1722 fr && doFits) { // access member to avoid unnecessarily creating fit result if wasnt needed
1723 // create a new subpad and draw fitResult on it
1724 auto _pad = gPad;
1725 auto pad =
1726 new TPad(fr->GetName(), TString::Format("%s = %g", poi().first()->GetTitle(), p.fNullVal()), 0, 0, 1., 1);
1727 pad->SetNumber(out->GetN() + 1); // can't use "0" for a subpad
1728 pad->cd();
1729 xRooNode(fr).Draw("goff");
1730 _pad->cd();
1731 //_pad->GetListOfPrimitives()->AddFirst(pad);
1732 pad->AppendPad();
1733 }
1734 if (std::isnan(val) && p.status() != 0) {
1735 if (!badPoints) {
1736 badPoints = new TGraph;
1737 badPoints->SetBit(kCanDelete);
1738 badPoints->SetName("badPoints");
1739 badPoints->SetMarkerStyle(5);
1740 badPoints->SetMarkerColor(kRed);
1741 badPoints->SetMarkerSize(1);
1742 out->GetListOfFunctions()->Add(badPoints, "P");
1743 }
1744 badPoints->SetPoint(badPoints->GetN(), p.fNullVal(), out->Eval(p.fNullVal()));
1745 mainPad->Modified();
1746 } else if (!std::isnan(val)) {
1747 out->SetPoint(out->GetN(), p.coords->getRealValue(axes().first()->GetName()), p.pll().first);
1748 out->SetPointError(out->GetN() - 1, 0, p.pll().second);
1749 out->Sort();
1750
1751 // reposition bad points
1752 if (badPoints) {
1753 for (int i = 0; i < badPoints->GetN(); i++)
1754 badPoints->SetPointY(i, out->Eval(badPoints->GetPointX(i)));
1755 }
1756
1757 mainPad->Modified();
1758 }
1759 if (s.RealTime() > 3) { // stops the clock
1760 basePad->GetCanvas()->Paint();
1761 basePad->GetCanvas()->Update();
1763 s.Reset();
1764 s.Start();
1765 }
1766 s.Continue();
1767 }
1768 basePad->GetCanvas()->Paint();
1769 basePad->GetCanvas()->Update();
1770#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 30, 00)
1771 basePad->GetCanvas()->ResetUpdated(); // stops previous canvas being replaced in a jupyter notebook
1772#endif
1774
1775 // finish by overlaying ufit
1776 if (ufr && doFits) {
1777 auto _pad = gPad;
1778 auto pad = new TPad(ufr->GetName(), "unconditional fit", 0, 0, 1., 1.);
1779 pad->SetNumber(-1);
1780 pad->cd();
1781 xRooNode(ufr).Draw("goff");
1782 _pad->cd();
1783 pad->AppendPad();
1784
1785 // draw one more pad to represent the selected, and draw the ufit pad onto that pad
1786 pad = new TPad("selected", "selected", 0, 0, 1, 1);
1787 pad->Draw();
1788 pad->cd();
1789 basePad->GetPad(2)->GetPad(-1)->AppendPad();
1790 pad->Modified();
1791 pad->Update();
1793
1794 basePad->cd();
1795 }
1796
1797 if (doFits) {
1798 if (!xRooNode::gIntObj) {
1800 }
1801 gPad->GetCanvas()->Connect("Highlighted(TVirtualPad*,TObject*,Int_t,Int_t)", "xRooNode::InteractiveObject",
1802 xRooNode::gIntObj, "Interactive_PLLPlot(TVirtualPad*,TObject*,Int_t,Int_t)");
1803 }
1804
1805 return;
1806}
1807
1809{
1810
1811 RooStats::HypoTestInverterResult *out = nullptr;
1812
1813 auto _axes = axes();
1814 if (_axes.empty())
1815 return out;
1816
1817 out = new RooStats::HypoTestInverterResult(GetName(), *dynamic_cast<RooRealVar *>(_axes.at(0)), 0.95);
1818 out->SetTitle(GetTitle());
1819
1820 for (auto &p : *this) {
1821 double _x = p.coords->getRealValue(_axes.at(0)->GetName(), std::numeric_limits<double>::quiet_NaN());
1822 out->Add(_x, p.result());
1823 }
1824
1825 return out;
1826}
1827
#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:148
@ kCanDelete
Definition TObject.h:375
#define gROOT
Definition TROOT.h:417
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:1519
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:1576
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition TGraph.cxx:2389
virtual void SetMaximum(Double_t maximum=-1111)
Set the maximum of the graph.
Definition TGraph.cxx:2371
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:2533
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:2069
void SetName(const char *name="") override
Set graph name.
Definition TGraph.cxx:2428
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:1587
void SetTitle(const char *title="") override
Change (i.e.
Definition TGraph.cxx:2444
virtual void SetPointY(Int_t i, Double_t y)
Set y value for point i.
Definition TGraph.cxx:2421
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:2341
virtual void SetMinimum(Double_t minimum=-1111)
Set the minimum of the graph.
Definition TGraph.cxx:2380
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:888
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:427
Double_t Atof() const
Return floating-point value contained in string.
Definition TString.cxx:2134
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:2459
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:662
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