62 ClassImp(RooStats::MCMCIntervalPlot);
65 using namespace RooStats;
69 MCMCIntervalPlot::MCMCIntervalPlot()
73 fPosteriorHist = NULL;
74 fPosteriorKeysPdf = NULL;
75 fPosteriorKeysProduct = NULL;
89 fPosteriorHistHistCopy = NULL;
90 fPosteriorHistTFCopy = NULL;
95 MCMCIntervalPlot::MCMCIntervalPlot(MCMCInterval& interval)
97 SetMCMCInterval(interval);
98 fPosteriorHist = NULL;
99 fPosteriorKeysPdf = NULL;
100 fPosteriorKeysProduct = NULL;
113 fPosteriorHistHistCopy = NULL;
114 fPosteriorHistTFCopy = NULL;
119 MCMCIntervalPlot::~MCMCIntervalPlot()
130 delete fPosteriorKeysPdf;
131 delete fPosteriorKeysProduct;
142 void MCMCIntervalPlot::SetMCMCInterval(MCMCInterval& interval)
144 fInterval = &interval;
145 fDimension = fInterval->GetDimension();
146 fParameters = fInterval->GetParameters();
151 void MCMCIntervalPlot::Draw(
const Option_t* options)
153 DrawInterval(options);
158 void MCMCIntervalPlot::DrawPosterior(
const Option_t* options)
160 if (fInterval->GetUseKeys())
161 DrawPosteriorKeysPdf(options);
163 DrawPosteriorHist(options);
168 void* MCMCIntervalPlot::DrawPosteriorHist(
const Option_t* ,
169 const char* title, Bool_t scale)
171 if (fPosteriorHist == NULL)
172 fPosteriorHist = fInterval->GetPosteriorHist();
174 if (fPosteriorHist == NULL) {
175 coutE(InputArguments) <<
"MCMCIntervalPlot::DrawPosteriorHist: "
176 <<
"Couldn't get posterior histogram." << endl;
190 fPosteriorHist->Scale(1/fPosteriorHist->GetBinContent(
191 fPosteriorHist->GetMaximumBin()));
193 TString ourTitle(GetTitle());
194 if (ourTitle.CompareTo(
"") == 0) {
196 fPosteriorHist->SetTitle(title);
198 fPosteriorHist->SetTitle(GetTitle());
202 return (
void*)fPosteriorHist;
207 void* MCMCIntervalPlot::DrawPosteriorKeysPdf(
const Option_t* options)
209 if (fPosteriorKeysPdf == NULL)
210 fPosteriorKeysPdf = fInterval->GetPosteriorKeysPdf();
212 if (fPosteriorKeysPdf == NULL) {
213 coutE(InputArguments) <<
"MCMCIntervalPlot::DrawPosteriorKeysPdf: "
214 <<
"Couldn't get posterior Keys PDF." << endl;
218 TString title(GetTitle());
219 Bool_t isEmpty = (title.CompareTo(
"") == 0);
221 if (fDimension == 1) {
222 RooRealVar* v = (RooRealVar*)fParameters->first();
223 RooPlot* frame = v->frame();
225 coutE(InputArguments) <<
"MCMCIntervalPlot::DrawPosteriorKeysPdf: "
226 <<
"Invalid parameter" << endl;
230 frame->SetTitle(Form(
"Posterior Keys PDF for %s", v->GetName()));
232 frame->SetTitle(GetTitle());
238 }
else if (fDimension == 2) {
239 RooArgList* axes = fInterval->GetAxes();
240 RooRealVar* xVar = (RooRealVar*)axes->at(0);
241 RooRealVar* yVar = (RooRealVar*)axes->at(1);
242 TH2F* keysHist = (TH2F*)fPosteriorKeysPdf->createHistogram(
243 "keysPlot2D", *xVar, RooFit::YVar(*yVar), RooFit::Scaling(kFALSE));
246 Form(
"MCMC histogram of posterior Keys PDF for %s, %s",
247 axes->at(0)->GetName(), axes->at(1)->GetName()));
249 keysHist->SetTitle(GetTitle());
251 keysHist->Draw(options);
260 void MCMCIntervalPlot::DrawInterval(
const Option_t* options)
262 switch (fInterval->GetIntervalType()) {
263 case MCMCInterval::kShortest:
264 DrawShortestInterval(options);
266 case MCMCInterval::kTailFraction:
267 DrawTailFractionInterval(options);
270 coutE(InputArguments) <<
"MCMCIntervalPlot::DrawInterval(): " <<
271 "Interval type not supported" << endl;
278 void MCMCIntervalPlot::DrawShortestInterval(
const Option_t* options)
280 if (fInterval->GetUseKeys())
281 DrawKeysPdfInterval(options);
283 DrawHistInterval(options);
288 void MCMCIntervalPlot::DrawKeysPdfInterval(
const Option_t* options)
290 TString title(GetTitle());
291 Bool_t isEmpty = (title.CompareTo(
"") == 0);
293 if (fDimension == 1) {
297 RooPlot* frame = (RooPlot*)DrawPosteriorKeysPdf(options);
301 Double_t height = fInterval->GetKeysMax();
303 RooRealVar* p = (RooRealVar*)fParameters->first();
304 Double_t ul = fInterval->UpperLimitByKeys(*p);
305 Double_t ll = fInterval->LowerLimitByKeys(*p);
307 if (frame != NULL && fPosteriorKeysPdf != NULL) {
310 frame->SetTitle(NULL);
312 frame->SetTitle(GetTitle());
313 frame->GetYaxis()->SetTitle(Form(
"Posterior for parameter %s",
315 fPosteriorKeysPdf->plotOn(frame,
316 RooFit::Normalization(1, RooAbsReal::Raw),
317 RooFit::Range(ll, ul, kFALSE),
319 RooFit::DrawOption(
"F"),
320 RooFit::MoveToBack(),
321 RooFit::FillColor(fShadeColor));
326 fPosteriorKeysPdf->plotOn(frame,
327 RooFit::Normalization(1, RooAbsReal::Raw));
330 frame->Draw(options);
333 TLine* llLine =
new TLine(ll, 0, ll, height);
334 TLine* ulLine =
new TLine(ul, 0, ul, height);
335 llLine->SetLineColor(fLineColor);
336 ulLine->SetLineColor(fLineColor);
337 llLine->SetLineWidth(fLineWidth);
338 ulLine->SetLineWidth(fLineWidth);
339 llLine->Draw(options);
340 ulLine->Draw(options);
341 }
else if (fDimension == 2) {
342 if (fPosteriorKeysPdf == NULL)
343 fPosteriorKeysPdf = fInterval->GetPosteriorKeysPdf();
345 if (fPosteriorKeysPdf == NULL) {
346 coutE(InputArguments) <<
"MCMCIntervalPlot::DrawKeysPdfInterval: "
347 <<
"Couldn't get posterior Keys PDF." << endl;
351 RooArgList* axes = fInterval->GetAxes();
352 RooRealVar* xVar = (RooRealVar*)axes->at(0);
353 RooRealVar* yVar = (RooRealVar*)axes->at(1);
354 TH2F* contHist = (TH2F*)fPosteriorKeysPdf->createHistogram(
355 "keysContour2D", *xVar, RooFit::YVar(*yVar), RooFit::Scaling(kFALSE));
362 contHist->SetTitle(GetTitle());
364 contHist->SetTitle(NULL);
366 contHist->SetStats(kFALSE);
368 TString tmpOpt(options);
369 if (!tmpOpt.Contains(
"CONT2")) tmpOpt.Append(
"CONT2");
371 Double_t cutoff = fInterval->GetKeysPdfCutoff();
372 contHist->SetContour(1, &cutoff);
373 contHist->SetLineColor(fLineColor);
374 contHist->SetLineWidth(fLineWidth);
375 contHist->Draw(tmpOpt.Data());
378 coutE(InputArguments) <<
"MCMCIntervalPlot::DrawKeysPdfInterval: "
379 <<
" Sorry: " << fDimension <<
"-D plots not currently supported" << endl;
385 void MCMCIntervalPlot::DrawHistInterval(
const Option_t* options)
387 TString title(GetTitle());
388 Bool_t isEmpty = (title.CompareTo(
"") == 0);
390 if (fDimension == 1) {
392 RooRealVar* p = (RooRealVar*)fParameters->first();
393 Double_t ul = fInterval->UpperLimitByHist(*p);
394 Double_t ll = fInterval->LowerLimitByHist(*p);
399 TH1F* hist = (TH1F*)DrawPosteriorHist(options, NULL,
false);
400 if (hist == NULL)
return;
402 hist->SetTitle(NULL);
404 hist->SetTitle(GetTitle());
405 hist->GetYaxis()->SetTitle(Form(
"Posterior for parameter %s",
407 hist->SetStats(kFALSE);
408 TH1F* copy = (TH1F*)hist->Clone(Form(
"%s_copy", hist->GetTitle()));
409 Double_t histCutoff = fInterval->GetHistCutoff();
412 Int_t nBins = copy->GetNbinsX();
414 for (i = 1; i <= nBins; i++) {
416 height = copy->GetBinContent(i);
417 if (height < histCutoff) {
418 copy->SetBinContent(i, 0);
419 copy->SetBinError(i, 0);
423 hist->Scale(1/hist->GetBinContent(hist->GetMaximumBin()));
424 copy->Scale(1/copy->GetBinContent(hist->GetMaximumBin()));
426 copy->SetFillStyle(1001);
427 copy->SetFillColor(fShadeColor);
430 copy->Draw(
"HIST SAME");
432 fPosteriorHistHistCopy = copy;
434 TLine* llLine =
new TLine(ll, 0, ll, 1);
435 TLine* ulLine =
new TLine(ul, 0, ul, 1);
436 llLine->SetLineColor(fLineColor);
437 ulLine->SetLineColor(fLineColor);
438 llLine->SetLineWidth(fLineWidth);
439 ulLine->SetLineWidth(fLineWidth);
440 llLine->Draw(options);
441 ulLine->Draw(options);
443 }
else if (fDimension == 2) {
444 if (fPosteriorHist == NULL)
445 fPosteriorHist = fInterval->GetPosteriorHist();
447 if (fPosteriorHist == NULL) {
448 coutE(InputArguments) <<
"MCMCIntervalPlot::DrawHistInterval: "
449 <<
"Couldn't get posterior histogram." << endl;
453 RooArgList* axes = fInterval->GetAxes();
461 fPosteriorHist->SetTitle(GetTitle());
463 fPosteriorHist->SetTitle(NULL);
466 fPosteriorHist->SetStats(kFALSE);
468 TString tmpOpt(options);
469 if (!tmpOpt.Contains(
"CONT2")) tmpOpt.Append(
"CONT2");
471 Double_t cutoff = fInterval->GetHistCutoff();
472 fPosteriorHist->SetContour(1, &cutoff);
473 fPosteriorHist->SetLineColor(fLineColor);
474 fPosteriorHist->SetLineWidth(fLineWidth);
475 fPosteriorHist->Draw(tmpOpt.Data());
477 coutE(InputArguments) <<
"MCMCIntervalPlot::DrawHistInterval: "
478 <<
" Sorry: " << fDimension <<
"-D plots not currently supported" << endl;
484 void MCMCIntervalPlot::DrawTailFractionInterval(
const Option_t* options)
486 TString title(GetTitle());
487 Bool_t isEmpty = (title.CompareTo(
"") == 0);
489 if (fDimension == 1) {
492 RooRealVar* p = (RooRealVar*)fParameters->first();
493 Double_t ul = fInterval->UpperLimitTailFraction(*p);
494 Double_t ll = fInterval->LowerLimitTailFraction(*p);
496 TH1F* hist = (TH1F*)DrawPosteriorHist(options, NULL,
false);
497 if (hist == NULL)
return;
499 hist->SetTitle(NULL);
501 hist->SetTitle(GetTitle());
502 hist->GetYaxis()->SetTitle(Form(
"Posterior for parameter %s",
504 hist->SetStats(kFALSE);
505 TH1F* copy = (TH1F*)hist->Clone(Form(
"%s_copy", hist->GetTitle()));
508 Int_t nBins = copy->GetNbinsX();
510 for (i = 1; i <= nBins; i++) {
512 center = copy->GetBinCenter(i);
513 if (center < ll || center > ul) {
514 copy->SetBinContent(i, 0);
515 copy->SetBinError(i, 0);
519 hist->Scale(1/hist->GetBinContent(hist->GetMaximumBin()));
520 copy->Scale(1/copy->GetBinContent(hist->GetMaximumBin()));
522 copy->SetFillStyle(1001);
523 copy->SetFillColor(fShadeColor);
525 copy->Draw(
"hist same");
528 TLine* llLine =
new TLine(ll, 0, ll, 1);
529 TLine* ulLine =
new TLine(ul, 0, ul, 1);
530 llLine->SetLineColor(fLineColor);
531 ulLine->SetLineColor(fLineColor);
532 llLine->SetLineWidth(fLineWidth);
533 ulLine->SetLineWidth(fLineWidth);
534 llLine->Draw(options);
535 ulLine->Draw(options);
537 coutE(InputArguments) <<
"MCMCIntervalPlot::DrawTailFractionInterval: "
538 <<
" Sorry: " << fDimension <<
"-D plots not currently supported"
545 void* MCMCIntervalPlot::DrawPosteriorKeysProduct(
const Option_t* options)
547 if (fPosteriorKeysProduct == NULL)
548 fPosteriorKeysProduct = fInterval->GetPosteriorKeysProduct();
550 if (fPosteriorKeysProduct == NULL) {
551 coutE(InputArguments) <<
"MCMCIntervalPlot::DrawPosteriorKeysProduct: "
552 <<
"Couldn't get posterior Keys product." << endl;
556 RooArgList* axes = fInterval->GetAxes();
558 TString title(GetTitle());
559 Bool_t isEmpty = (title.CompareTo(
"") == 0);
561 if (fDimension == 1) {
562 RooPlot* frame = ((RooRealVar*)fParameters->first())->frame();
563 if (!frame)
return NULL;
565 frame->SetTitle(Form(
"Posterior Keys PDF * Heaviside product for %s",
566 axes->at(0)->GetName()));
568 frame->SetTitle(GetTitle());
570 fPosteriorKeysProduct->plotOn(frame,
571 RooFit::Normalization(1, RooAbsReal::Raw));
572 frame->Draw(options);
574 }
else if (fDimension == 2) {
575 RooRealVar* xVar = (RooRealVar*)axes->at(0);
576 RooRealVar* yVar = (RooRealVar*)axes->at(1);
577 TH2F* productHist = (TH2F*)fPosteriorKeysProduct->createHistogram(
578 "prodPlot2D", *xVar, RooFit::YVar(*yVar), RooFit::Scaling(kFALSE));
580 productHist->SetTitle(
581 Form(
"MCMC Posterior Keys Product Hist. for %s, %s",
582 axes->at(0)->GetName(), axes->at(1)->GetName()));
584 productHist->SetTitle(GetTitle());
585 productHist->Draw(options);
594 void MCMCIntervalPlot::DrawChainScatter(RooRealVar& xVar, RooRealVar& yVar)
596 const MarkovChain* markovChain = fInterval->GetChain();
598 Int_t size = markovChain->Size();
601 burnInSteps = fInterval->GetNumBurnInSteps();
605 Double_t* x =
new Double_t[size - burnInSteps];
606 Double_t* y =
new Double_t[size - burnInSteps];
607 Double_t* burnInX = NULL;
608 Double_t* burnInY = NULL;
609 if (burnInSteps > 0) {
610 burnInX =
new Double_t[burnInSteps];
611 burnInY =
new Double_t[burnInSteps];
616 for (Int_t i = burnInSteps; i < size; i++) {
617 x[i - burnInSteps] = markovChain->Get(i)->getRealValue(xVar.GetName());
618 y[i - burnInSteps] = markovChain->Get(i)->getRealValue(yVar.GetName());
621 for (Int_t i = 0; i < burnInSteps; i++) {
622 burnInX[i] = markovChain->Get(i)->getRealValue(xVar.GetName());
623 burnInY[i] = markovChain->Get(i)->getRealValue(yVar.GetName());
626 firstX = markovChain->Get(0)->getRealValue(xVar.GetName());
627 firstY = markovChain->Get(0)->getRealValue(yVar.GetName());
629 TString title(GetTitle());
630 Bool_t isEmpty = (title.CompareTo(
"") == 0);
632 TGraph* walk =
new TGraph(size - burnInSteps, x, y);
634 walk->SetTitle(Form(
"2-D Scatter Plot of Markov chain for %s, %s",
635 xVar.GetName(), yVar.GetName()));
637 walk->SetTitle(GetTitle());
639 walk->GetXaxis()->Set(xVar.numBins(), xVar.getMin(), xVar.getMax());
640 walk->GetXaxis()->SetTitle(xVar.GetName());
641 walk->GetYaxis()->Set(yVar.numBins(), yVar.getMin(), yVar.getMax());
642 walk->GetYaxis()->SetTitle(yVar.GetName());
643 walk->SetLineColor(kGray);
644 walk->SetMarkerStyle(6);
645 walk->SetMarkerColor(kViolet);
646 walk->Draw(
"A,L,P,same");
648 TGraph* burnIn = NULL;
649 if (burnInX != NULL && burnInY != NULL) {
650 burnIn =
new TGraph(burnInSteps - 1, burnInX, burnInY);
651 burnIn->SetLineColor(kPink);
652 burnIn->SetMarkerStyle(6);
653 burnIn->SetMarkerColor(kPink);
654 burnIn->Draw(
"L,P,same");
657 TGraph* first =
new TGraph(1, &firstX, &firstY);
658 first->SetLineColor(kGreen);
659 first->SetMarkerStyle(3);
660 first->SetMarkerSize(2);
661 first->SetMarkerColor(kGreen);
662 first->Draw(
"L,P,same");
667 if (burnInX != NULL)
delete [] burnInX;
668 if (burnInY != NULL)
delete [] burnInY;
676 void MCMCIntervalPlot::DrawParameterVsTime(RooRealVar& param)
678 const MarkovChain* markovChain = fInterval->GetChain();
679 Int_t size = markovChain->Size();
680 Int_t numEntries = 2 * size;
681 Double_t* value =
new Double_t[numEntries];
682 Double_t* time =
new Double_t[numEntries];
686 for (Int_t i = 0; i < size; i++) {
687 val = markovChain->Get(i)->getRealValue(param.GetName());
688 weight = (Int_t)markovChain->Weight();
690 value[2*i + 1] = val;
696 TString title(GetTitle());
697 Bool_t isEmpty = (title.CompareTo(
"") == 0);
699 TGraph* paramGraph =
new TGraph(numEntries, time, value);
701 paramGraph->SetTitle(Form(
"%s vs. time in Markov chain",param.GetName()));
703 paramGraph->SetTitle(GetTitle());
704 paramGraph->GetXaxis()->SetTitle(
"Time (discrete steps)");
705 paramGraph->GetYaxis()->SetTitle(param.GetName());
707 paramGraph->Draw(
"A,L,same");
715 void MCMCIntervalPlot::DrawNLLVsTime()
717 const MarkovChain* markovChain = fInterval->GetChain();
718 Int_t size = markovChain->Size();
719 Int_t numEntries = 2 * size;
720 Double_t* nllValue =
new Double_t[numEntries];
721 Double_t* time =
new Double_t[numEntries];
725 for (Int_t i = 0; i < size; i++) {
726 nll = markovChain->NLL(i);
727 weight = (Int_t)markovChain->Weight();
729 nllValue[2*i + 1] = nll;
735 TString title(GetTitle());
736 Bool_t isEmpty = (title.CompareTo(
"") == 0);
738 TGraph* nllGraph =
new TGraph(numEntries, time, nllValue);
740 nllGraph->SetTitle(
"NLL value vs. time in Markov chain");
742 nllGraph->SetTitle(GetTitle());
743 nllGraph->GetXaxis()->SetTitle(
"Time (discrete steps)");
744 nllGraph->GetYaxis()->SetTitle(
"NLL (-log(likelihood))");
746 nllGraph->Draw(
"A,L,same");
754 void MCMCIntervalPlot::DrawNLLHist(
const Option_t* options)
756 if (fNLLHist == NULL) {
757 const MarkovChain* markovChain = fInterval->GetChain();
760 Int_t size = markovChain->Size();
761 for (Int_t i = 0; i < size; i++)
762 if (markovChain->NLL(i) > maxNLL)
763 maxNLL = markovChain->NLL(i);
764 RooRealVar* nllVar = fInterval->GetNLLVar();
765 fNLLHist =
new TH1F(
"mcmc_nll_hist",
"MCMC NLL Histogram",
766 nllVar->getBins(), 0, maxNLL);
767 TString title(GetTitle());
768 Bool_t isEmpty = (title.CompareTo(
"") == 0);
770 fNLLHist->SetTitle(GetTitle());
771 fNLLHist->GetXaxis()->SetTitle(
"-log(likelihood)");
772 for (Int_t i = 0; i < size; i++)
773 fNLLHist->Fill(markovChain->NLL(i), markovChain->Weight());
775 fNLLHist->Draw(options);
780 void MCMCIntervalPlot::DrawWeightHist(
const Option_t* options)
782 if (fWeightHist == NULL) {
783 const MarkovChain* markovChain = fInterval->GetChain();
785 Double_t maxWeight = 0;
786 Int_t size = markovChain->Size();
787 for (Int_t i = 0; i < size; i++)
788 if (markovChain->Weight(i) > maxWeight)
789 maxWeight = markovChain->Weight(i);
790 fWeightHist =
new TH1F(
"mcmc_weight_hist",
"MCMC Weight Histogram",
791 (Int_t)(maxWeight + 1), 0, maxWeight * 1.02);
792 for (Int_t i = 0; i < size; i++)
793 fWeightHist->Fill(markovChain->Weight(i));
795 fWeightHist->Draw(options);
800 // 3-d plot of the parameter points
802 // also plot the points in the markov chain
803 RooDataSet* markovChainData = ((MCMCInterval*)mcmcint)->GetChainAsDataSet();
805 TTree& chain = ((RooTreeDataStore*) markovChainData->store())->tree();
806 chain.SetMarkerStyle(6);
807 chain.SetMarkerColor(kRed);
808 chain.Draw("s:ratioSigEff:ratioBkgEff","","box"); // 3-d box proportional to posterior
810 // the points used in the profile construction
811 TTree& parameterScan = ((RooTreeDataStore*) fc.GetPointsToScan()->store())->tree();
812 parameterScan.SetMarkerStyle(24);
813 parameterScan.Draw("s:ratioSigEff:ratioBkgEff","","same");
815 chain.SetMarkerStyle(6);
816 chain.SetMarkerColor(kRed);
817 //chain.Draw("s:ratioSigEff:ratioBkgEff", "_MarkovChain_local_nll","box");
818 //chain.Draw("_MarkovChain_local_nll");