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