Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
MCMCIntervalPlot.cxx
Go to the documentation of this file.
1// @(#)root/roostats:$Id$
2// Authors: Kevin Belasco 17/06/2009
3// Authors: Kyle Cranmer 17/06/2009
4/*************************************************************************
5 * Project: RooStats *
6 * Package: RooFit/RooStats *
7 *************************************************************************
8 * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
9 * All rights reserved. *
10 * *
11 * For the licensing terms see $ROOTSYS/LICENSE. *
12 * For the list of contributors see $ROOTSYS/README/CREDITS. *
13 *************************************************************************/
14
15/** \class RooStats::MCMCIntervalPlot
16 \ingroup Roostats
17
18This class provides simple and straightforward utilities to plot a MCMCInterval
19object. Basic use only requires a few lines once you have an MCMCInterval*:
20
21~~~ {.cpp}
22 MCMCIntervalPlot plot(*interval);
23 plot.Draw();
24~~~
25
26The standard Draw() function will currently draw the confidence interval
27range with bars if 1-D and a contour if 2-D. The MCMC posterior will also be
28plotted for the 1-D case.
29
30*/
31
35
36#include "TLine.h"
37#include "TGraph.h"
38#include "RooRealVar.h"
39#include "RooPlot.h"
40#include "TH2.h"
41#include "TH1F.h"
42#include "RooArgList.h"
43#include "TAxis.h"
44#include "RooGlobalFunc.h"
45
46#include <iostream>
47
48// Extra draw commands
49//static const char* POSTERIOR_HIST = "posterior_hist";
50//static const char* POSTERIOR_KEYS_PDF = "posterior_keys_pdf";
51//static const char* POSTERIOR_KEYS_PRODUCT = "posterior_keys_product";
52//static const char* HIST_INTERVAL = "hist_interval";
53//static const char* KEYS_PDF_INTERVAL = "keys_pdf_interval";
54//static const char* TAIL_FRACTION_INTERVAL = "tail_fraction_interval";
55//static const char* OPTION_SEP = ":";
56
57
58using std::endl;
59using namespace RooStats;
60
61////////////////////////////////////////////////////////////////////////////////
62
64
65////////////////////////////////////////////////////////////////////////////////
66
71
72////////////////////////////////////////////////////////////////////////////////
73
75{
76 delete fParameters;
77 // kbelasco: why does deleting fPosteriorHist remove the graphics
78 // but deleting TGraphs doesn't?
79 //delete fPosteriorHist;
80 // can we delete fNLLHist and fWeightHist?
81 //delete fNLLHist;
82 //delete fWeightHist;
83
84 // kbelasco: should we delete fPosteriorKeysPdf and fPosteriorKeysProduct?
85 delete fPosteriorKeysPdf;
87
88 delete fWalk;
89 delete fBurnIn;
90 delete fFirst;
91 delete fParamGraph;
92 delete fNLLGraph;
93}
94
95////////////////////////////////////////////////////////////////////////////////
96
98{
99 fInterval = &interval;
100 fDimension = fInterval->GetDimension();
101 fParameters = fInterval->GetParameters();
102}
103
104////////////////////////////////////////////////////////////////////////////////
105
107{
108 DrawInterval(options);
109}
110
111////////////////////////////////////////////////////////////////////////////////
112
114{
115 if (fInterval->GetUseKeys()) {
116 DrawPosteriorKeysPdf(options);
117 } else {
118 DrawPosteriorHist(options);
119 }
120}
121
122////////////////////////////////////////////////////////////////////////////////
123
125 const char* title, bool scale)
126{
127 if (fPosteriorHist == nullptr)
128 fPosteriorHist = fInterval->GetPosteriorHist();
129
130 if (fPosteriorHist == nullptr) {
131 coutE(InputArguments) << "MCMCIntervalPlot::DrawPosteriorHist: "
132 << "Couldn't get posterior histogram." << std::endl;
133 return nullptr;
134 }
135
136 // kbelasco: annoying hack because histogram drawing fails when it sees
137 // an un-recognized option like POSTERIOR_HIST, etc.
138 //const Option_t* myOpt = nullptr;
139
140 //TString tmpOpt(options);
141 //if (tmpOpt.Contains("same"))
142 // myOpt = "same";
143
144 // scale so highest bin has height 1
145 if (scale) {
146 fPosteriorHist->Scale(1 / fPosteriorHist->GetBinContent(fPosteriorHist->GetMaximumBin()));
147 }
148
149 TString ourTitle(GetTitle());
150 if (ourTitle.CompareTo("") == 0) {
151 if (title)
152 fPosteriorHist->SetTitle(title);
153 } else
154 fPosteriorHist->SetTitle(GetTitle());
155
156 //fPosteriorHist->Draw(myOpt);
157
158 return (void*)fPosteriorHist;
159}
160
161////////////////////////////////////////////////////////////////////////////////
162
164{
165 if (fPosteriorKeysPdf == nullptr)
166 fPosteriorKeysPdf = fInterval->GetPosteriorKeysPdf();
167
168 if (fPosteriorKeysPdf == nullptr) {
169 coutE(InputArguments) << "MCMCIntervalPlot::DrawPosteriorKeysPdf: "
170 << "Couldn't get posterior Keys PDF." << std::endl;
171 return nullptr;
172 }
173
174 TString title(GetTitle());
175 bool isEmpty = (title.CompareTo("") == 0);
176
177 if (fDimension == 1) {
178 RooRealVar* v = static_cast<RooRealVar*>(fParameters->first());
179 RooPlot* frame = v->frame();
180 if (frame == nullptr) {
181 coutE(InputArguments) << "MCMCIntervalPlot::DrawPosteriorKeysPdf: "
182 << "Invalid parameter" << std::endl;
183 return nullptr;
184 }
185 if (isEmpty) {
186 frame->SetTitle(("Posterior Keys PDF for " + std::string(v->GetName())).c_str());
187 } else {
188 frame->SetTitle(GetTitle());
189 }
190 //fPosteriorKeysPdf->plotOn(frame);
191 //fPosteriorKeysPdf->plotOn(frame,
192 // RooFit::Normalization(1, RooAbsReal::Raw));
193 //frame->Draw(options);
194 return (void*)frame;
195 } else if (fDimension == 2) {
196 RooArgList* axes = fInterval->GetAxes();
197 RooRealVar* xVar = static_cast<RooRealVar*>(axes->at(0));
198 RooRealVar* yVar = static_cast<RooRealVar*>(axes->at(1));
199 TH2F* keysHist = static_cast<TH2F*>(fPosteriorKeysPdf->createHistogram(
200 "keysPlot2D", *xVar, RooFit::YVar(*yVar), RooFit::Scaling(false)));
201 if (isEmpty) {
202 keysHist->SetTitle(
203 Form("MCMC histogram of posterior Keys PDF for %s, %s",
204 axes->at(0)->GetName(), axes->at(1)->GetName()));
205 } else {
206 keysHist->SetTitle(GetTitle());
207 }
208
209 keysHist->Draw(options);
210 delete axes;
211 return nullptr;
212 }
213 return nullptr;
214}
215
216////////////////////////////////////////////////////////////////////////////////
217
219{
220 switch (fInterval->GetIntervalType()) {
222 DrawShortestInterval(options);
223 break;
226 break;
227 default:
228 coutE(InputArguments) << "MCMCIntervalPlot::DrawInterval(): " <<
229 "Interval type not supported" << std::endl;
230 break;
231 }
232}
233
234////////////////////////////////////////////////////////////////////////////////
235
237{
238 if (fInterval->GetUseKeys()) {
239 DrawKeysPdfInterval(options);
240 } else {
241 DrawHistInterval(options);
242 }
243}
244
245////////////////////////////////////////////////////////////////////////////////
246
248{
249 TString title(GetTitle());
250 bool isEmpty = (title.CompareTo("") == 0);
251
252 if (fDimension == 1) {
253 // Draw the posterior keys PDF as well so the user can see where the
254 // limit bars line up
255 // fDimension == 1, so we know we will receive a RooPlot
256 RooPlot* frame = reinterpret_cast<RooPlot*>(DrawPosteriorKeysPdf(options));
257
258 //double height = 1;
259 //double height = 2.0 * fInterval->GetKeysPdfCutoff();
260 double height = fInterval->GetKeysMax();
261
262 RooRealVar* p = static_cast<RooRealVar*>(fParameters->first());
263 double ul = fInterval->UpperLimitByKeys(*p);
264 double ll = fInterval->LowerLimitByKeys(*p);
265
266 if (frame != nullptr && fPosteriorKeysPdf != nullptr) {
267 // draw shading in interval
268 if (isEmpty) {
269 frame->SetTitle(nullptr);
270 } else {
271 frame->SetTitle(GetTitle());
272 }
273 frame->GetYaxis()->SetTitle(("Posterior for parameter " + std::string(p->GetName())).c_str());
274 fPosteriorKeysPdf->plotOn(frame,
276 RooFit::Range(ll, ul, false),
281
282 // hack - this is drawn twice now:
283 // once by DrawPosteriorKeysPdf (which also configures things and sets
284 // the title), and once again here so the shading shows up behind.
285 fPosteriorKeysPdf->plotOn(frame,
287 }
288 if (frame) {
289 frame->Draw(options);
290 }
291
292 TLine* llLine = new TLine(ll, 0, ll, height);
293 TLine* ulLine = new TLine(ul, 0, ul, height);
294 llLine->SetLineColor(fLineColor);
295 ulLine->SetLineColor(fLineColor);
296 llLine->SetLineWidth(fLineWidth);
297 ulLine->SetLineWidth(fLineWidth);
298 llLine->Draw(options);
299 ulLine->Draw(options);
300 } else if (fDimension == 2) {
301 if (fPosteriorKeysPdf == nullptr)
302 fPosteriorKeysPdf = fInterval->GetPosteriorKeysPdf();
303
304 if (fPosteriorKeysPdf == nullptr) {
305 coutE(InputArguments) << "MCMCIntervalPlot::DrawKeysPdfInterval: "
306 << "Couldn't get posterior Keys PDF." << std::endl;
307 return;
308 }
309
310 RooArgList* axes = fInterval->GetAxes();
311 RooRealVar* xVar = static_cast<RooRealVar*>(axes->at(0));
312 RooRealVar* yVar = static_cast<RooRealVar*>(axes->at(1));
313 TH2F* contHist = static_cast<TH2F*>(fPosteriorKeysPdf->createHistogram(
314 "keysContour2D", *xVar, RooFit::YVar(*yVar), RooFit::Scaling(false)));
315 //if (isEmpty)
316 // contHist->SetTitle(Form("MCMC Keys conf. interval for %s, %s",
317 // axes->at(0)->GetName(), axes->at(1)->GetName()));
318 //else
319 // contHist->SetTitle(GetTitle());
320 if (!isEmpty) {
321 contHist->SetTitle(GetTitle());
322 } else {
323 contHist->SetTitle(nullptr);
324 }
325
326 contHist->SetStats(false);
327
328 TString tmpOpt(options);
329 if (!tmpOpt.Contains("CONT2")) tmpOpt.Append("CONT2");
330
331 double cutoff = fInterval->GetKeysPdfCutoff();
332 contHist->SetContour(1, &cutoff);
333 contHist->SetLineColor(fLineColor);
334 contHist->SetLineWidth(fLineWidth);
335 contHist->Draw(tmpOpt.Data());
336 delete axes;
337 } else {
338 coutE(InputArguments) << "MCMCIntervalPlot::DrawKeysPdfInterval: "
339 << " Sorry: " << fDimension << "-D plots not currently supported" << std::endl;
340 }
341}
342
343////////////////////////////////////////////////////////////////////////////////
344
346{
347 TString title(GetTitle());
348 bool isEmpty = (title.CompareTo("") == 0);
349
350 if (fDimension == 1) {
351 // draw lower and upper limits
352 RooRealVar* p = static_cast<RooRealVar*>(fParameters->first());
353 double ul = fInterval->UpperLimitByHist(*p);
354 double ll = fInterval->LowerLimitByHist(*p);
355
356 // Draw the posterior histogram as well so the user can see where the
357 // limit bars line up
358 // fDimension == 1, so we know will get a TH1F*
359 TH1F* hist = reinterpret_cast<TH1F*>(DrawPosteriorHist(options, nullptr, false));
360 if (hist == nullptr) return;
361 if (isEmpty) {
362 hist->SetTitle(nullptr);
363 } else {
364 hist->SetTitle(GetTitle());
365 }
366 hist->GetYaxis()->SetTitle(("Posterior for parameter " + std::string(p->GetName())).c_str());
367 hist->SetStats(false);
368 TH1F* copy = static_cast<TH1F*>(hist->Clone((std::string(hist->GetTitle()) + "_copy").c_str()));
369 double histCutoff = fInterval->GetHistCutoff();
370
371 Int_t i;
372 Int_t nBins = copy->GetNbinsX();
373 double height;
374 for (i = 1; i <= nBins; i++) {
375 // remove bins with height < cutoff
376 height = copy->GetBinContent(i);
377 if (height < histCutoff) {
378 copy->SetBinContent(i, 0);
379 copy->SetBinError(i, 0);
380 }
381 }
382
383 hist->Scale(1/hist->GetBinContent(hist->GetMaximumBin()));
384 copy->Scale(1/copy->GetBinContent(hist->GetMaximumBin()));
385
386 copy->SetFillStyle(1001);
388 hist->Draw(options);
389 // to show the interval area
390 copy->Draw("HIST SAME");
391
393
394 TLine* llLine = new TLine(ll, 0, ll, 1);
395 TLine* ulLine = new TLine(ul, 0, ul, 1);
396 llLine->SetLineColor(fLineColor);
397 ulLine->SetLineColor(fLineColor);
398 llLine->SetLineWidth(fLineWidth);
399 ulLine->SetLineWidth(fLineWidth);
400 llLine->Draw(options);
401 ulLine->Draw(options);
402
403 } else if (fDimension == 2) {
404 if (fPosteriorHist == nullptr)
405 fPosteriorHist = fInterval->GetPosteriorHist();
406
407 if (fPosteriorHist == nullptr) {
408 coutE(InputArguments) << "MCMCIntervalPlot::DrawHistInterval: "
409 << "Couldn't get posterior histogram." << std::endl;
410 return;
411 }
412
413 RooArgList* axes = fInterval->GetAxes();
414 //if (isEmpty)
415 // fPosteriorHist->SetTitle(
416 // Form("MCMC histogram conf. interval for %s, %s",
417 // axes->at(0)->GetName(), axes->at(1)->GetName()));
418 //else
419 // fPosteriorHist->SetTitle(GetTitle());
420 if (!isEmpty) {
421 fPosteriorHist->SetTitle(GetTitle());
422 } else {
423 fPosteriorHist->SetTitle(nullptr);
424 }
425 delete axes;
426
427 fPosteriorHist->SetStats(false);
428
429 TString tmpOpt(options);
430 if (!tmpOpt.Contains("CONT2")) tmpOpt.Append("CONT2");
431
432 double cutoff = fInterval->GetHistCutoff();
433 fPosteriorHist->SetContour(1, &cutoff);
434 fPosteriorHist->SetLineColor(fLineColor);
435 fPosteriorHist->SetLineWidth(fLineWidth);
436 fPosteriorHist->Draw(tmpOpt.Data());
437 } else {
438 coutE(InputArguments) << "MCMCIntervalPlot::DrawHistInterval: "
439 << " Sorry: " << fDimension << "-D plots not currently supported" << std::endl;
440 }
441}
442
443////////////////////////////////////////////////////////////////////////////////
444
446{
447 TString title(GetTitle());
448 bool isEmpty = (title.CompareTo("") == 0);
449
450 if (fDimension == 1) {
451 // Draw the posterior histogram as well so the user can see where the
452 // limit bars line up
453 RooRealVar* p = static_cast<RooRealVar*>(fParameters->first());
454 double ul = fInterval->UpperLimitTailFraction(*p);
455 double ll = fInterval->LowerLimitTailFraction(*p);
456
457 TH1F* hist = reinterpret_cast<TH1F*>(DrawPosteriorHist(options, nullptr, false));
458 if (hist == nullptr) return;
459 if (isEmpty) {
460 hist->SetTitle(nullptr);
461 } else {
462 hist->SetTitle(GetTitle());
463 }
464 hist->GetYaxis()->SetTitle(Form("Posterior for parameter %s",
465 p->GetName()));
466 hist->SetStats(false);
467 TH1F* copy = static_cast<TH1F*>(hist->Clone(Form("%s_copy", hist->GetTitle())));
468
469 Int_t i;
470 Int_t nBins = copy->GetNbinsX();
471 double center;
472 for (i = 1; i <= nBins; i++) {
473 // remove bins outside interval
474 center = copy->GetBinCenter(i);
475 if (center < ll || center > ul) {
476 copy->SetBinContent(i, 0);
477 copy->SetBinError(i, 0);
478 }
479 }
480
481 hist->Scale(1/hist->GetBinContent(hist->GetMaximumBin()));
482 copy->Scale(1/copy->GetBinContent(hist->GetMaximumBin()));
483
484 copy->SetFillStyle(1001);
486 hist->Draw(options);
487 copy->Draw("hist same");
488
489 // draw lower and upper limits
490 TLine* llLine = new TLine(ll, 0, ll, 1);
491 TLine* ulLine = new TLine(ul, 0, ul, 1);
492 llLine->SetLineColor(fLineColor);
493 ulLine->SetLineColor(fLineColor);
494 llLine->SetLineWidth(fLineWidth);
495 ulLine->SetLineWidth(fLineWidth);
496 llLine->Draw(options);
497 ulLine->Draw(options);
498 } else {
499 coutE(InputArguments) << "MCMCIntervalPlot::DrawTailFractionInterval: "
500 << " Sorry: " << fDimension << "-D plots not currently supported"
501 << std::endl;
502 }
503}
504
505////////////////////////////////////////////////////////////////////////////////
506
508{
509 if (fPosteriorKeysProduct == nullptr)
510 fPosteriorKeysProduct = fInterval->GetPosteriorKeysProduct();
511
512 if (fPosteriorKeysProduct == nullptr) {
513 coutE(InputArguments) << "MCMCIntervalPlot::DrawPosteriorKeysProduct: "
514 << "Couldn't get posterior Keys product." << std::endl;
515 return nullptr;
516 }
517
518 RooArgList* axes = fInterval->GetAxes();
519
520 TString title(GetTitle());
521 bool isEmpty = (title.CompareTo("") == 0);
522
523 if (fDimension == 1) {
524 RooPlot* frame = (static_cast<RooRealVar*>(fParameters->first()))->frame();
525 if (!frame) return nullptr;
526 if (isEmpty) {
527 frame->SetTitle(Form("Posterior Keys PDF * Heaviside product for %s",
528 axes->at(0)->GetName()));
529 } else {
530 frame->SetTitle(GetTitle());
531 }
532 //fPosteriorKeysProduct->plotOn(frame);
533 fPosteriorKeysProduct->plotOn(frame,
535 frame->Draw(options);
536 return (void*)frame;
537 } else if (fDimension == 2) {
538 RooRealVar* xVar = static_cast<RooRealVar*>(axes->at(0));
539 RooRealVar* yVar = static_cast<RooRealVar*>(axes->at(1));
540 TH2F* productHist = static_cast<TH2F*>(fPosteriorKeysProduct->createHistogram(
541 "prodPlot2D", *xVar, RooFit::YVar(*yVar), RooFit::Scaling(false)));
542 if (isEmpty) {
543 productHist->SetTitle(
544 Form("MCMC Posterior Keys Product Hist. for %s, %s",
545 axes->at(0)->GetName(), axes->at(1)->GetName()));
546 } else {
547 productHist->SetTitle(GetTitle());
548 }
549 productHist->Draw(options);
550 return nullptr;
551 }
552 delete axes;
553 return nullptr;
554}
555
556////////////////////////////////////////////////////////////////////////////////
557
559{
560 const MarkovChain* markovChain = fInterval->GetChain();
561
562 Int_t size = markovChain->Size();
563 Int_t burnInSteps;
564 if (fShowBurnIn) {
565 burnInSteps = fInterval->GetNumBurnInSteps();
566 } else {
567 burnInSteps = 0;
568 }
569
570 double* x = new double[size - burnInSteps];
571 double* y = new double[size - burnInSteps];
572 double* burnInX = nullptr;
573 double* burnInY = nullptr;
574 if (burnInSteps > 0) {
575 burnInX = new double[burnInSteps];
576 burnInY = new double[burnInSteps];
577 }
578 double firstX;
579 double firstY;
580
581 for (Int_t i = burnInSteps; i < size; i++) {
582 x[i - burnInSteps] = markovChain->Get(i)->getRealValue(xVar.GetName());
583 y[i - burnInSteps] = markovChain->Get(i)->getRealValue(yVar.GetName());
584 }
585
586 for (Int_t i = 0; i < burnInSteps; i++) {
587 burnInX[i] = markovChain->Get(i)->getRealValue(xVar.GetName());
588 burnInY[i] = markovChain->Get(i)->getRealValue(yVar.GetName());
589 }
590
591 firstX = markovChain->Get(0)->getRealValue(xVar.GetName());
592 firstY = markovChain->Get(0)->getRealValue(yVar.GetName());
593
594 TString title(GetTitle());
595 bool isEmpty = (title.CompareTo("") == 0);
596
597 TGraph* walk = new TGraph(size - burnInSteps, x, y);
598 if (isEmpty) {
599 walk->SetTitle(Form("2-D Scatter Plot of Markov chain for %s, %s",
600 xVar.GetName(), yVar.GetName()));
601 } else {
602 walk->SetTitle(GetTitle());
603 }
604 // kbelasco: figure out how to set TGraph variable ranges
605 walk->GetXaxis()->Set(xVar.numBins(), xVar.getMin(), xVar.getMax());
606 walk->GetXaxis()->SetTitle(xVar.GetName());
607 walk->GetYaxis()->Set(yVar.numBins(), yVar.getMin(), yVar.getMax());
608 walk->GetYaxis()->SetTitle(yVar.GetName());
609 walk->SetLineColor(kGray);
610 walk->SetMarkerStyle(6);
611 walk->SetMarkerColor(kViolet);
612 walk->Draw("A,L,P,same");
613
614 TGraph* burnIn = nullptr;
615 if (burnInX != nullptr && burnInY != nullptr) {
616 burnIn = new TGraph(burnInSteps - 1, burnInX, burnInY);
617 burnIn->SetLineColor(kPink);
618 burnIn->SetMarkerStyle(6);
619 burnIn->SetMarkerColor(kPink);
620 burnIn->Draw("L,P,same");
621 }
622
623 TGraph* first = new TGraph(1, &firstX, &firstY);
624 first->SetLineColor(kGreen);
625 first->SetMarkerStyle(3);
626 first->SetMarkerSize(2);
627 first->SetMarkerColor(kGreen);
628 first->Draw("L,P,same");
629
630 //walkCanvas->Update();
631 delete [] x;
632 delete [] y;
633 if (burnInX != nullptr) delete [] burnInX;
634 if (burnInY != nullptr) delete [] burnInY;
635 //delete walk;
636 //delete burnIn;
637 //delete first;
638}
639
640////////////////////////////////////////////////////////////////////////////////
641
643{
644 const MarkovChain* markovChain = fInterval->GetChain();
645 Int_t size = markovChain->Size();
646 Int_t numEntries = 2 * size;
647 double* value = new double[numEntries];
648 double* time = new double[numEntries];
649 double val;
650 Int_t weight;
651 Int_t t = 0;
652 for (Int_t i = 0; i < size; i++) {
653 val = markovChain->Get(i)->getRealValue(param.GetName());
654 weight = (Int_t)markovChain->Weight();
655 value[2*i] = val;
656 value[2*i + 1] = val;
657 time[2*i] = t;
658 t += weight;
659 time[2*i + 1] = t;
660 }
661
662 TString title(GetTitle());
663 bool isEmpty = (title.CompareTo("") == 0);
664
665 TGraph* paramGraph = new TGraph(numEntries, time, value);
666 if (isEmpty) {
667 paramGraph->SetTitle(Form("%s vs. time in Markov chain",param.GetName()));
668 } else {
669 paramGraph->SetTitle(GetTitle());
670 }
671 paramGraph->GetXaxis()->SetTitle("Time (discrete steps)");
672 paramGraph->GetYaxis()->SetTitle(param.GetName());
673 //paramGraph->SetLineColor(fLineColor);
674 paramGraph->Draw("A,L,same");
675 delete [] value;
676 delete [] time;
677 //gPad->Update();
678}
679
680////////////////////////////////////////////////////////////////////////////////
681
683{
684 const MarkovChain* markovChain = fInterval->GetChain();
685 Int_t size = markovChain->Size();
686 Int_t numEntries = 2 * size;
687 double* nllValue = new double[numEntries];
688 double* time = new double[numEntries];
689 double nll;
690 Int_t weight;
691 Int_t t = 0;
692 for (Int_t i = 0; i < size; i++) {
693 nll = markovChain->NLL(i);
694 weight = (Int_t)markovChain->Weight();
695 nllValue[2*i] = nll;
696 nllValue[2*i + 1] = nll;
697 time[2*i] = t;
698 t += weight;
699 time[2*i + 1] = t;
700 }
701
702 TString title(GetTitle());
703 bool isEmpty = (title.CompareTo("") == 0);
704
705 TGraph* nllGraph = new TGraph(numEntries, time, nllValue);
706 if (isEmpty) {
707 nllGraph->SetTitle("NLL value vs. time in Markov chain");
708 } else {
709 nllGraph->SetTitle(GetTitle());
710 }
711 nllGraph->GetXaxis()->SetTitle("Time (discrete steps)");
712 nllGraph->GetYaxis()->SetTitle("NLL (-log(likelihood))");
713 //nllGraph->SetLineColor(fLineColor);
714 nllGraph->Draw("A,L,same");
715 //gPad->Update();
716 delete [] nllValue;
717 delete [] time;
718}
719
720////////////////////////////////////////////////////////////////////////////////
721
723{
724 if (fNLLHist == nullptr) {
725 const MarkovChain* markovChain = fInterval->GetChain();
726 // find the max NLL value
727 double maxNLL = 0;
728 Int_t size = markovChain->Size();
729 for (Int_t i = 0; i < size; i++) {
730 if (markovChain->NLL(i) > maxNLL)
731 maxNLL = markovChain->NLL(i);
732 }
733 RooRealVar* nllVar = fInterval->GetNLLVar();
734 fNLLHist = new TH1F("mcmc_nll_hist", "MCMC NLL Histogram",
735 nllVar->getBins(), 0, maxNLL);
736 TString title(GetTitle());
737 bool isEmpty = (title.CompareTo("") == 0);
738 if (!isEmpty)
739 fNLLHist->SetTitle(GetTitle());
740 fNLLHist->GetXaxis()->SetTitle("-log(likelihood)");
741 for (Int_t i = 0; i < size; i++)
742 fNLLHist->Fill(markovChain->NLL(i), markovChain->Weight());
743 }
744 fNLLHist->Draw(options);
745}
746
747////////////////////////////////////////////////////////////////////////////////
748
750{
751 if (fWeightHist == nullptr) {
752 const MarkovChain* markovChain = fInterval->GetChain();
753 // find the max weight value
754 double maxWeight = 0;
755 Int_t size = markovChain->Size();
756 for (Int_t i = 0; i < size; i++) {
757 if (markovChain->Weight(i) > maxWeight)
758 maxWeight = markovChain->Weight(i);
759 }
760 fWeightHist = new TH1F("mcmc_weight_hist", "MCMC Weight Histogram",
761 (Int_t)(maxWeight + 1), 0, maxWeight * 1.02);
762 for (Int_t i = 0; i < size; i++)
763 fWeightHist->Fill(markovChain->Weight(i));
764 }
765 fWeightHist->Draw(options);
766}
767
768/*
769/////////////////////////////////////////////////////////////////////
770 // 3-d plot of the parameter points
771 dataCanvas->cd(2);
772 // also plot the points in the markov chain
773 RooDataSet* markovChainData = ((MCMCInterval*)mcmcint)->GetChainAsDataSet();
774
775 TTree& chain = ((RooTreeDataStore*) markovChainData->store())->tree();
776 chain.SetMarkerStyle(6);
777 chain.SetMarkerColor(kRed);
778 chain.Draw("s:ratioSigEff:ratioBkgEff","","box"); // 3-d box proportional to posterior
779
780 // the points used in the profile construction
781 TTree& parameterScan = ((RooTreeDataStore*) fc.GetPointsToScan()->store())->tree();
782 parameterScan.SetMarkerStyle(24);
783 parameterScan.Draw("s:ratioSigEff:ratioBkgEff","","same");
784
785 chain.SetMarkerStyle(6);
786 chain.SetMarkerColor(kRed);
787 //chain.Draw("s:ratioSigEff:ratioBkgEff", "_MarkovChain_local_nll","box");
788 //chain.Draw("_MarkovChain_local_nll");
789////////////////////////////////////////////////////////////////////
790*/
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define coutE(a)
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
const char Option_t
Option string (const char).
Definition RtypesCore.h:80
@ kGray
Definition Rtypes.h:66
@ kPink
Definition Rtypes.h:68
@ kGreen
Definition Rtypes.h:67
@ kViolet
Definition Rtypes.h:68
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2496
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 numBins(const char *rangeName=nullptr) const override
virtual Int_t getBins(const char *name=nullptr) const
Get number of bins of currently defined range.
virtual double getMax(const char *name=nullptr) const
Get maximum of currently defined range.
virtual double getMin(const char *name=nullptr) const
Get minimum of currently defined range.
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition RooArgList.h:110
Plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:43
static RooPlot * frame(const RooAbsRealLValue &var, double xmin, double xmax, Int_t nBins)
Create a new frame for a given variable in x.
Definition RooPlot.cxx:185
void SetTitle(const char *name) override
Set the title of the RooPlot to 'title'.
Definition RooPlot.cxx:1209
TAxis * GetYaxis() const
Definition RooPlot.cxx:1230
void Draw(Option_t *options=nullptr) override
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:597
Variable that can be changed from the outside.
Definition RooRealVar.h:37
void DrawNLLHist(const Option_t *options=nullptr)
void Draw(const Option_t *options=nullptr) override
void * DrawPosteriorHist(const Option_t *options=nullptr, const char *title=nullptr, bool scale=true)
void DrawChainScatter(RooRealVar &xVar, RooRealVar &yVar)
void DrawTailFractionInterval(const Option_t *options=nullptr)
void SetMCMCInterval(MCMCInterval &interval)
~MCMCIntervalPlot() override
Destructor of SamplingDistribution.
void DrawPosterior(const Option_t *options=nullptr)
void DrawHistInterval(const Option_t *options=nullptr)
void * DrawPosteriorKeysProduct(const Option_t *options=nullptr)
void DrawWeightHist(const Option_t *options=nullptr)
void DrawInterval(const Option_t *options=nullptr)
void DrawParameterVsTime(RooRealVar &param)
void * DrawPosteriorKeysPdf(const Option_t *options=nullptr)
void DrawShortestInterval(const Option_t *options=nullptr)
void DrawKeysPdfInterval(const Option_t *options=nullptr)
MCMCInterval is a concrete implementation of the RooStats::ConfInterval interface.
Stores the steps in a Markov Chain of points.
Definition MarkovChain.h:26
virtual double NLL(Int_t i) const
get the NLL value of entry at position i
virtual const RooArgSet * Get(Int_t i) const
get the entry at position i
Definition MarkovChain.h:47
virtual double Weight() const
get the weight of the current (last indexed) entry
virtual Int_t Size() const
get the number of steps in the chain
Definition MarkovChain.h:45
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:40
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition TAttFill.h:42
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:47
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:44
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition TAttMarker.h:41
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
virtual void Set(Int_t nbins, Double_t xmin, Double_t xmax)
Initialize axis with fix bins.
Definition TAxis.cxx:790
void Draw(Option_t *chopt="") override
Default Draw method for all objects.
TAxis * GetXaxis() const
TAxis * GetYaxis() const
void SetTitle(const char *title="") override
Set the title of the TNamed.
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:878
virtual Double_t GetBinCenter(Int_t bin) const
Return bin center for 1D histogram.
Definition TH1.cxx:9275
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6836
virtual Int_t GetNbinsX() const
Definition TH1.h:541
virtual void SetBinError(Int_t bin, Double_t error)
Set the bin Error Note that this resets the bin eror option to be of Normal Type and for the non-empt...
Definition TH1.cxx:9340
TAxis * GetYaxis()
Definition TH1.h:572
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3097
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content see convention for numbering bins in TH1::GetBin In case the bin number is greater th...
Definition TH1.cxx:9356
virtual Int_t GetMaximumBin() const
Return location of bin with maximum value in the range.
Definition TH1.cxx:8714
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition TH1.cxx:5143
virtual void SetContour(Int_t nlevels, const Double_t *levels=nullptr)
Set the number and values of contour levels.
Definition TH1.cxx:8620
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition TH1.cxx:6719
TObject * Clone(const char *newname="") const override
Make a complete copy of the underlying object.
Definition TH1.cxx:2786
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition TH1.cxx:9127
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:345
Use the TLine constructor to create a simple line.
Definition TLine.h:22
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 Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:293
Basic string class.
Definition TString.h:138
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition TString.cxx:464
const char * Data() const
Definition TString.h:384
TString & Append(const char *cs)
Definition TString.h:581
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:641
RooCmdArg YVar(const RooAbsRealLValue &var, const RooCmdArg &arg={})
RooCmdArg Scaling(bool flag)
RooCmdArg FillColor(TColorNumber color)
RooCmdArg DrawOption(const char *opt)
RooCmdArg Range(const char *rangeName, bool adjustNorm=true)
RooCmdArg Normalization(double scaleFactor)
RooCmdArg MoveToBack()
RooCmdArg VLines()
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Namespace for the RooStats classes.
Definition CodegenImpl.h:66