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