MCMCIntervalPlot.cxx

Go to the documentation of this file.
00001 // @(#)root/roostats:$Id: MCMCIntervalPlot.cxx 36222 2010-10-09 18:27:06Z wouter $
00002 // Authors: Kevin Belasco        17/06/2009
00003 // Authors: Kyle Cranmer         17/06/2009
00004 /*************************************************************************
00005  * Project: RooStats                                                     *
00006  * Package: RooFit/RooStats                                              *
00007  *************************************************************************
00008  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
00009  * All rights reserved.                                                  *
00010  *                                                                       *
00011  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00012  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00013  *************************************************************************/
00014 
00015 //_________________________________________________
00016 /*
00017 BEGIN_HTML
00018 <p>
00019 This class provides simple and straightforward utilities to plot a MCMCInterval
00020 object.  Basic use only requires a few lines once you have an MCMCInterval*:
00021 </p>
00022 <p>
00023 MCMCIntervalPlot plot(*interval);
00024 plot.Draw();
00025 </p>
00026 <p>
00027 The standard Draw() function will currently draw the confidence interval
00028 range with bars if 1-D and a contour if 2-D.  The MCMC posterior will also be
00029 plotted for the 1-D case.
00030 </p>
00031 END_HTML
00032 */
00033 //_________________________________________________
00034 
00035 #ifndef ROOSTATS_MCMCIntervalPlot
00036 #include "RooStats/MCMCIntervalPlot.h"
00037 #endif
00038 #include <iostream>
00039 #ifndef ROOT_TROOT
00040 #include "TROOT.h"
00041 #endif
00042 #ifndef ROOT_TMath
00043 #include "TMath.h"
00044 #endif
00045 #ifndef ROOT_TLine
00046 #include "TLine.h"
00047 #endif
00048 #ifndef ROOT_TObjArray
00049 #include "TObjArray.h"
00050 #endif
00051 #ifndef ROOT_TList
00052 #include "TList.h"
00053 #endif
00054 #ifndef ROOT_TGraph
00055 #include "TGraph.h"
00056 #endif
00057 #ifndef ROOT_TPad
00058 #include "TPad.h"
00059 #endif
00060 #ifndef ROO_REAL_VAR
00061 #include "RooRealVar.h"
00062 #endif
00063 #ifndef ROO_PLOT
00064 #include "RooPlot.h"
00065 #endif
00066 #ifndef ROOT_TH2
00067 #include "TH2.h"
00068 #endif
00069 #ifndef ROOT_TH1F
00070 #include "TH1F.h"
00071 #endif
00072 #ifndef ROO_ARG_LIST
00073 #include "RooArgList.h"
00074 #endif
00075 #ifndef ROOT_TAxis
00076 #include "TAxis.h"
00077 #endif
00078 #ifndef ROO_GLOBAL_FUNC
00079 #include "RooGlobalFunc.h"
00080 #endif
00081 
00082 // Extra draw commands
00083 //static const char* POSTERIOR_HIST = "posterior_hist";
00084 //static const char* POSTERIOR_KEYS_PDF = "posterior_keys_pdf";
00085 //static const char* POSTERIOR_KEYS_PRODUCT = "posterior_keys_product";
00086 //static const char* HIST_INTERVAL = "hist_interval";
00087 //static const char* KEYS_PDF_INTERVAL = "keys_pdf_interval";
00088 //static const char* TAIL_FRACTION_INTERVAL = "tail_fraction_interval";
00089 //static const char* OPTION_SEP = ":";
00090 
00091 ClassImp(RooStats::MCMCIntervalPlot);
00092 
00093 using namespace std;
00094 using namespace RooStats;
00095 
00096 MCMCIntervalPlot::MCMCIntervalPlot()
00097 {
00098    fInterval = NULL;
00099    fParameters = NULL;
00100    fPosteriorHist = NULL;
00101    fPosteriorKeysPdf = NULL;
00102    fPosteriorKeysProduct = NULL;
00103    fDimension = 0;
00104    fLineColor = kBlack;
00105    fShadeColor = kGray;
00106    fLineWidth = 1;
00107    //fContourColor = kBlack;
00108    fShowBurnIn = kTRUE;
00109    fWalk = NULL;
00110    fBurnIn = NULL;
00111    fFirst = NULL;
00112    fParamGraph = NULL;
00113    fNLLGraph = NULL;
00114    fNLLHist = NULL;
00115    fWeightHist = NULL;
00116    fPosteriorHistHistCopy = NULL;
00117    fPosteriorHistTFCopy = NULL;
00118 }
00119 
00120 MCMCIntervalPlot::MCMCIntervalPlot(MCMCInterval& interval)
00121 {
00122    SetMCMCInterval(interval);
00123    fPosteriorHist = NULL;
00124    fPosteriorKeysPdf = NULL;
00125    fPosteriorKeysProduct = NULL;
00126    fLineColor = kBlack;
00127    fShadeColor = kGray;
00128    fLineWidth = 1;
00129    //fContourColor = kBlack;
00130    fShowBurnIn = kTRUE;
00131    fWalk = NULL;
00132    fBurnIn = NULL;
00133    fFirst = NULL;
00134    fParamGraph = NULL;
00135    fNLLGraph = NULL;
00136    fNLLHist = NULL;
00137    fWeightHist = NULL;
00138    fPosteriorHistHistCopy = NULL;
00139    fPosteriorHistTFCopy = NULL;
00140 }
00141 
00142 MCMCIntervalPlot::~MCMCIntervalPlot()
00143 {
00144    delete fParameters;
00145    // kbelasco: why does deleting fPosteriorHist remove the graphics
00146    // but deleting TGraphs doesn't?
00147    //delete fPosteriorHist;
00148    // can we delete fNLLHist and fWeightHist?
00149    //delete fNLLHist;
00150    //delete fWeightHist;
00151 
00152    // kbelasco: should we delete fPosteriorKeysPdf and fPosteriorKeysProduct?
00153    delete fPosteriorKeysPdf;
00154    delete fPosteriorKeysProduct;
00155 
00156    delete fWalk;
00157    delete fBurnIn;
00158    delete fFirst;
00159    delete fParamGraph;
00160    delete fNLLGraph;
00161 }
00162 
00163 void MCMCIntervalPlot::SetMCMCInterval(MCMCInterval& interval)
00164 {
00165    fInterval = &interval;
00166    fDimension = fInterval->GetDimension();
00167    fParameters = fInterval->GetParameters();
00168 }
00169 
00170 void MCMCIntervalPlot::Draw(const Option_t* options)
00171 {
00172    DrawInterval(options);
00173 }
00174 
00175 void MCMCIntervalPlot::DrawPosterior(const Option_t* options)
00176 {
00177    if (fInterval->GetUseKeys())
00178       DrawPosteriorKeysPdf(options);
00179    else
00180       DrawPosteriorHist(options);
00181 }
00182 
00183 void* MCMCIntervalPlot::DrawPosteriorHist(const Option_t* options,
00184       const char* title, Bool_t scale)
00185 {
00186    if (fPosteriorHist == NULL)
00187       fPosteriorHist = fInterval->GetPosteriorHist();
00188 
00189    if (fPosteriorHist == NULL) {
00190       coutE(InputArguments) << "MCMCIntervalPlot::DrawPosteriorHist: "
00191          << "Couldn't get posterior histogram." << endl;
00192       return NULL;
00193    }
00194 
00195    // kbelasco: annoying hack because histogram drawing fails when it sees
00196    // an unrecognized option like POSTERIOR_HIST, etc.
00197    const Option_t* myOpt = NULL;
00198 
00199    TString tmpOpt(options);
00200    if (tmpOpt.Contains("same"))
00201       myOpt = "same";
00202 
00203    // scale so highest bin has height 1
00204    if (scale)
00205       fPosteriorHist->Scale(1/fPosteriorHist->GetBinContent(
00206                fPosteriorHist->GetMaximumBin()));
00207 
00208    TString ourTitle(GetTitle());
00209    if (ourTitle.CompareTo("") == 0) {
00210       if (title)
00211          fPosteriorHist->SetTitle(title);
00212    } else
00213       fPosteriorHist->SetTitle(GetTitle());
00214 
00215    //fPosteriorHist->Draw(myOpt);
00216 
00217    return (void*)fPosteriorHist;
00218 }
00219 
00220 void* MCMCIntervalPlot::DrawPosteriorKeysPdf(const Option_t* options)
00221 {
00222    if (fPosteriorKeysPdf == NULL)
00223       fPosteriorKeysPdf = fInterval->GetPosteriorKeysPdf();
00224 
00225    if (fPosteriorKeysPdf == NULL) {
00226       coutE(InputArguments) << "MCMCIntervalPlot::DrawPosteriorKeysPdf: "
00227          << "Couldn't get posterior Keys PDF." << endl;
00228       return NULL;
00229    }
00230 
00231    TString title(GetTitle());
00232    Bool_t isEmpty = (title.CompareTo("") == 0);
00233 
00234    if (fDimension == 1) {
00235       RooRealVar* v = (RooRealVar*)fParameters->first();
00236       RooPlot* frame = v->frame();
00237       if (isEmpty)
00238          frame->SetTitle(Form("Posterior Keys PDF for %s", v->GetName()));
00239       else
00240          frame->SetTitle(GetTitle());
00241       //fPosteriorKeysPdf->plotOn(frame);
00242       //fPosteriorKeysPdf->plotOn(frame,
00243       //      RooFit::Normalization(1, RooAbsReal::Raw));
00244       //frame->Draw(options);
00245       return (void*)frame;
00246    } else if (fDimension == 2) {
00247       RooArgList* axes = fInterval->GetAxes();
00248       RooRealVar* xVar = (RooRealVar*)axes->at(0);
00249       RooRealVar* yVar = (RooRealVar*)axes->at(1);
00250       TH2F* keysHist = (TH2F*)fPosteriorKeysPdf->createHistogram(
00251             "keysPlot2D", *xVar, RooFit::YVar(*yVar), RooFit::Scaling(kFALSE));
00252       if (isEmpty)
00253          keysHist->SetTitle(
00254                Form("MCMC histogram of posterior Keys PDF for %s, %s",
00255                   axes->at(0)->GetName(), axes->at(1)->GetName()));
00256       else
00257          keysHist->SetTitle(GetTitle());
00258 
00259       keysHist->Draw(options);
00260       delete axes;
00261       return NULL;
00262    }
00263    return NULL;
00264 }
00265 
00266 void MCMCIntervalPlot::DrawInterval(const Option_t* options)
00267 {
00268    switch (fInterval->GetIntervalType()) {
00269       case MCMCInterval::kShortest:
00270          DrawShortestInterval(options);
00271          break;
00272       case MCMCInterval::kTailFraction:
00273          DrawTailFractionInterval(options);
00274          break;
00275       default:
00276          coutE(InputArguments) << "MCMCIntervalPlot::DrawInterval(): " <<
00277             "Interval type not supported" << endl;
00278          break;
00279    }
00280 }
00281 
00282 void MCMCIntervalPlot::DrawShortestInterval(const Option_t* options)
00283 {
00284    if (fInterval->GetUseKeys())
00285       DrawKeysPdfInterval(options);
00286    else
00287       DrawHistInterval(options);
00288 }
00289 
00290 void MCMCIntervalPlot::DrawKeysPdfInterval(const Option_t* options)
00291 {
00292    TString title(GetTitle());
00293    Bool_t isEmpty = (title.CompareTo("") == 0);
00294 
00295    if (fDimension == 1) {
00296       // Draw the posterior keys PDF as well so the user can see where the
00297       // limit bars line up
00298       // fDimension == 1, so we know we will receive a RooPlot
00299       RooPlot* frame = (RooPlot*)DrawPosteriorKeysPdf(options);
00300 
00301       //Double_t height = 1;
00302       //Double_t height = 2.0 * fInterval->GetKeysPdfCutoff();
00303       Double_t height = fInterval->GetKeysMax();
00304 
00305       RooRealVar* p = (RooRealVar*)fParameters->first();
00306       Double_t ul = fInterval->UpperLimitByKeys(*p);
00307       Double_t ll = fInterval->LowerLimitByKeys(*p);
00308 
00309       if (frame != NULL && fPosteriorKeysPdf != NULL) {
00310          // draw shading in interval
00311          if (isEmpty)
00312             frame->SetTitle(NULL);
00313          else
00314             frame->SetTitle(GetTitle());
00315          frame->GetYaxis()->SetTitle(Form("Posterior for parameter %s",
00316                   p->GetName()));
00317          fPosteriorKeysPdf->plotOn(frame,
00318                RooFit::Normalization(1, RooAbsReal::Raw),
00319                RooFit::Range(ll, ul, kFALSE),
00320                RooFit::VLines(),
00321                RooFit::DrawOption("F"),
00322                RooFit::MoveToBack(),
00323                RooFit::FillColor(fShadeColor));
00324 
00325          // hack - this is drawn twice now:
00326          // once by DrawPosteriorKeysPdf (which also configures things and sets
00327          // the title), and once again here so the shading shows up behind.
00328          fPosteriorKeysPdf->plotOn(frame,
00329                RooFit::Normalization(1, RooAbsReal::Raw));
00330       }
00331       if (frame) {
00332         frame->Draw(options);
00333       }
00334 
00335       TLine* llLine = new TLine(ll, 0, ll, height);
00336       TLine* ulLine = new TLine(ul, 0, ul, height);
00337       llLine->SetLineColor(fLineColor);
00338       ulLine->SetLineColor(fLineColor);
00339       llLine->SetLineWidth(fLineWidth);
00340       ulLine->SetLineWidth(fLineWidth);
00341       llLine->Draw(options);
00342       ulLine->Draw(options);
00343    } else if (fDimension == 2) {
00344       if (fPosteriorKeysPdf == NULL)
00345          fPosteriorKeysPdf = fInterval->GetPosteriorKeysPdf();
00346 
00347       if (fPosteriorKeysPdf == NULL) {
00348          coutE(InputArguments) << "MCMCIntervalPlot::DrawKeysPdfInterval: "
00349             << "Couldn't get posterior Keys PDF." << endl;
00350          return;
00351       }
00352 
00353       RooArgList* axes = fInterval->GetAxes();
00354       RooRealVar* xVar = (RooRealVar*)axes->at(0);
00355       RooRealVar* yVar = (RooRealVar*)axes->at(1);
00356       TH2F* contHist = (TH2F*)fPosteriorKeysPdf->createHistogram(
00357           "keysContour2D", *xVar, RooFit::YVar(*yVar), RooFit::Scaling(kFALSE));
00358       //if (isEmpty)
00359       //   contHist->SetTitle(Form("MCMC Keys conf. interval for %s, %s",
00360       //            axes->at(0)->GetName(), axes->at(1)->GetName()));
00361       //else
00362       //   contHist->SetTitle(GetTitle());
00363       if (!isEmpty)
00364          contHist->SetTitle(GetTitle());
00365       else
00366          contHist->SetTitle(NULL);
00367 
00368       contHist->SetStats(kFALSE);
00369 
00370       TString tmpOpt(options);
00371       if (!tmpOpt.Contains("CONT2")) tmpOpt.Append("CONT2");
00372 
00373       Double_t cutoff = fInterval->GetKeysPdfCutoff();
00374       contHist->SetContour(1, &cutoff);
00375       contHist->SetLineColor(fLineColor);
00376       contHist->SetLineWidth(fLineWidth);
00377       contHist->Draw(tmpOpt.Data());
00378       delete axes;
00379    } else {
00380       coutE(InputArguments) << "MCMCIntervalPlot::DrawKeysPdfInterval: "
00381          << " Sorry: " << fDimension << "-D plots not currently supported" << endl;
00382    }
00383 }
00384 
00385 void MCMCIntervalPlot::DrawHistInterval(const Option_t* options)
00386 {
00387    TString title(GetTitle());
00388    Bool_t isEmpty = (title.CompareTo("") == 0);
00389 
00390    if (fDimension == 1) {
00391       // draw lower and upper limits
00392       RooRealVar* p = (RooRealVar*)fParameters->first();
00393       Double_t ul = fInterval->UpperLimitByHist(*p);
00394       Double_t ll = fInterval->LowerLimitByHist(*p);
00395 
00396       // Draw the posterior histogram as well so the user can see where the
00397       // limit bars line up
00398       // fDimension == 1, so we know will get a TH1F*
00399       TH1F* hist = (TH1F*)DrawPosteriorHist(options, NULL, false);
00400       if (isEmpty)
00401          hist->SetTitle(NULL);
00402       else
00403          hist->SetTitle(GetTitle());
00404       hist->GetYaxis()->SetTitle(Form("Posterior for parameter %s",
00405                p->GetName()));
00406       hist->SetStats(kFALSE);
00407       TH1F* copy = (TH1F*)hist->Clone(Form("%s_copy", hist->GetTitle()));
00408       Double_t histCutoff = fInterval->GetHistCutoff();
00409 
00410       Int_t i;
00411       Int_t nBins = copy->GetNbinsX();
00412       Double_t height;
00413       for (i = 1; i <= nBins; i++) {
00414          // remove bins with height < cutoff
00415          height = copy->GetBinContent(i);
00416          if (height < histCutoff)
00417             copy->SetBinContent(i, 0);
00418       }
00419 
00420       hist->Scale(1/hist->GetBinContent(hist->GetMaximumBin()));
00421       copy->Scale(1/copy->GetBinContent(hist->GetMaximumBin()));
00422 
00423       copy->SetFillStyle(1001);
00424       copy->SetFillColor(fShadeColor);
00425       hist->Draw(options);
00426       copy->Draw("same");
00427 
00428       fPosteriorHistHistCopy = copy;
00429 
00430       TLine* llLine = new TLine(ll, 0, ll, 1);
00431       TLine* ulLine = new TLine(ul, 0, ul, 1);
00432       llLine->SetLineColor(fLineColor);
00433       ulLine->SetLineColor(fLineColor);
00434       llLine->SetLineWidth(fLineWidth);
00435       ulLine->SetLineWidth(fLineWidth);
00436       llLine->Draw(options);
00437       ulLine->Draw(options);
00438 
00439    } else if (fDimension == 2) {
00440       if (fPosteriorHist == NULL)
00441          fPosteriorHist = fInterval->GetPosteriorHist();
00442 
00443       if (fPosteriorHist == NULL) {
00444          coutE(InputArguments) << "MCMCIntervalPlot::DrawHistInterval: "
00445             << "Couldn't get posterior histogram." << endl;
00446          return;
00447       }
00448 
00449       RooArgList* axes = fInterval->GetAxes();
00450       //if (isEmpty)
00451       //   fPosteriorHist->SetTitle(
00452       //         Form("MCMC histogram conf. interval for %s, %s",
00453       //            axes->at(0)->GetName(), axes->at(1)->GetName()));
00454       //else
00455       //   fPosteriorHist->SetTitle(GetTitle());
00456       if (!isEmpty)
00457          fPosteriorHist->SetTitle(GetTitle());
00458       else
00459          fPosteriorHist->SetTitle(NULL);
00460       delete axes;
00461 
00462       fPosteriorHist->SetStats(kFALSE);
00463 
00464       TString tmpOpt(options);
00465       if (!tmpOpt.Contains("CONT2")) tmpOpt.Append("CONT2");
00466 
00467       Double_t cutoff = fInterval->GetHistCutoff();
00468       fPosteriorHist->SetContour(1, &cutoff);
00469       fPosteriorHist->SetLineColor(fLineColor);
00470       fPosteriorHist->SetLineWidth(fLineWidth);
00471       fPosteriorHist->Draw(tmpOpt.Data());
00472    } else {
00473       coutE(InputArguments) << "MCMCIntervalPlot::DrawHistInterval: "
00474          << " Sorry: " << fDimension << "-D plots not currently supported" << endl;
00475    }
00476 }
00477 
00478 void MCMCIntervalPlot::DrawTailFractionInterval(const Option_t* options)
00479 {
00480    TString title(GetTitle());
00481    Bool_t isEmpty = (title.CompareTo("") == 0);
00482 
00483    if (fDimension == 1) {
00484       // Draw the posterior histogram as well so the user can see where the
00485       // limit bars line up
00486       RooRealVar* p = (RooRealVar*)fParameters->first();
00487       Double_t ul = fInterval->UpperLimitTailFraction(*p);
00488       Double_t ll = fInterval->LowerLimitTailFraction(*p);
00489 
00490       TH1F* hist = (TH1F*)DrawPosteriorHist(options, NULL, false);
00491       if (isEmpty)
00492          hist->SetTitle(NULL);
00493       else
00494          hist->SetTitle(GetTitle());
00495       hist->GetYaxis()->SetTitle(Form("Posterior for parameter %s",
00496                p->GetName()));
00497       hist->SetStats(kFALSE);
00498       TH1F* copy = (TH1F*)hist->Clone(Form("%s_copy", hist->GetTitle()));
00499 
00500       Int_t i;
00501       Int_t nBins = copy->GetNbinsX();
00502       Double_t center;
00503       for (i = 1; i <= nBins; i++) {
00504          // remove bins outside interval
00505          center = copy->GetBinCenter(i);
00506          if (center < ll || center > ul)
00507             copy->SetBinContent(i, 0);
00508       }
00509 
00510       hist->Scale(1/hist->GetBinContent(hist->GetMaximumBin()));
00511       copy->Scale(1/copy->GetBinContent(hist->GetMaximumBin()));
00512 
00513       copy->SetFillStyle(1001);
00514       copy->SetFillColor(fShadeColor);
00515       hist->Draw(options);
00516       copy->Draw("same");
00517 
00518       // draw lower and upper limits
00519       TLine* llLine = new TLine(ll, 0, ll, 1);
00520       TLine* ulLine = new TLine(ul, 0, ul, 1);
00521       llLine->SetLineColor(fLineColor);
00522       ulLine->SetLineColor(fLineColor);
00523       llLine->SetLineWidth(fLineWidth);
00524       ulLine->SetLineWidth(fLineWidth);
00525       llLine->Draw(options);
00526       ulLine->Draw(options);
00527    } else {
00528       coutE(InputArguments) << "MCMCIntervalPlot::DrawTailFractionInterval: "
00529          << " Sorry: " << fDimension << "-D plots not currently supported"
00530          << endl;
00531    }
00532 }
00533 
00534 void* MCMCIntervalPlot::DrawPosteriorKeysProduct(const Option_t* options)
00535 {
00536    if (fPosteriorKeysProduct == NULL)
00537       fPosteriorKeysProduct = fInterval->GetPosteriorKeysProduct();
00538 
00539    if (fPosteriorKeysProduct == NULL) {
00540       coutE(InputArguments) << "MCMCIntervalPlot::DrawPosteriorKeysProduct: "
00541          << "Couldn't get posterior Keys product." << endl;
00542       return NULL;
00543    }
00544 
00545    RooArgList* axes = fInterval->GetAxes();
00546 
00547    TString title(GetTitle());
00548    Bool_t isEmpty = (title.CompareTo("") == 0);
00549 
00550    if (fDimension == 1) {
00551       RooPlot* frame = ((RooRealVar*)fParameters->first())->frame();
00552       if (isEmpty)
00553          frame->SetTitle(Form("Posterior Keys PDF * Heaviside product for %s",
00554                   axes->at(0)->GetName()));
00555       else
00556          frame->SetTitle(GetTitle());
00557       //fPosteriorKeysProduct->plotOn(frame);
00558       fPosteriorKeysProduct->plotOn(frame,
00559             RooFit::Normalization(1, RooAbsReal::Raw));
00560       frame->Draw(options);
00561       return (void*)frame;
00562    } else if (fDimension == 2) {
00563       RooRealVar* xVar = (RooRealVar*)axes->at(0);
00564       RooRealVar* yVar = (RooRealVar*)axes->at(1);
00565       TH2F* productHist = (TH2F*)fPosteriorKeysProduct->createHistogram(
00566             "prodPlot2D", *xVar, RooFit::YVar(*yVar), RooFit::Scaling(kFALSE));
00567       if (isEmpty)
00568          productHist->SetTitle(
00569                Form("MCMC Posterior Keys Product Hist. for %s, %s",
00570                   axes->at(0)->GetName(), axes->at(1)->GetName()));
00571       else
00572          productHist->SetTitle(GetTitle());
00573       productHist->Draw(options);
00574       return NULL;
00575    }
00576    delete axes;
00577    return NULL;
00578 }
00579 
00580 void MCMCIntervalPlot::DrawChainScatter(RooRealVar& xVar, RooRealVar& yVar)
00581 {
00582    const MarkovChain* markovChain = fInterval->GetChain();
00583 
00584    Int_t size = markovChain->Size();
00585    Int_t burnInSteps;
00586    if (fShowBurnIn)
00587       burnInSteps = fInterval->GetNumBurnInSteps();
00588    else
00589       burnInSteps = 0;
00590 
00591    Double_t* x = new Double_t[size - burnInSteps];
00592    Double_t* y = new Double_t[size - burnInSteps];
00593    Double_t* burnInX = NULL;
00594    Double_t* burnInY = NULL;
00595    if (burnInSteps > 0) {
00596       burnInX = new Double_t[burnInSteps];
00597       burnInY = new Double_t[burnInSteps];
00598    }
00599    Double_t firstX;
00600    Double_t firstY;
00601 
00602    for (Int_t i = burnInSteps; i < size; i++) {
00603       x[i - burnInSteps] = markovChain->Get(i)->getRealValue(xVar.GetName());
00604       y[i - burnInSteps] = markovChain->Get(i)->getRealValue(yVar.GetName());
00605    }
00606 
00607    for (Int_t i = 0; i < burnInSteps; i++) {
00608       burnInX[i] = markovChain->Get(i)->getRealValue(xVar.GetName());
00609       burnInY[i] = markovChain->Get(i)->getRealValue(yVar.GetName());
00610    }
00611 
00612    firstX = markovChain->Get(0)->getRealValue(xVar.GetName());
00613    firstY = markovChain->Get(0)->getRealValue(yVar.GetName());
00614 
00615    TString title(GetTitle());
00616    Bool_t isEmpty = (title.CompareTo("") == 0);
00617 
00618    TGraph* walk = new TGraph(size - burnInSteps, x, y);
00619    if (isEmpty)
00620       walk->SetTitle(Form("2-D Scatter Plot of Markov chain for %s, %s",
00621                xVar.GetName(), yVar.GetName()));
00622    else
00623       walk->SetTitle(GetTitle());
00624    // kbelasco: figure out how to set TGraph variable ranges
00625    walk->GetXaxis()->Set(xVar.numBins(), xVar.getMin(), xVar.getMax());
00626    walk->GetXaxis()->SetTitle(xVar.GetName());
00627    walk->GetYaxis()->Set(yVar.numBins(), yVar.getMin(), yVar.getMax());
00628    walk->GetYaxis()->SetTitle(yVar.GetName());
00629    walk->SetLineColor(kGray);
00630    walk->SetMarkerStyle(6);
00631    walk->SetMarkerColor(kViolet);
00632    walk->Draw("A,L,P,same");
00633 
00634    TGraph* burnIn = NULL;
00635    if (burnInX != NULL && burnInY != NULL) {
00636       burnIn = new TGraph(burnInSteps - 1, burnInX, burnInY);
00637       burnIn->SetLineColor(kPink);
00638       burnIn->SetMarkerStyle(6);
00639       burnIn->SetMarkerColor(kPink);
00640       burnIn->Draw("L,P,same");
00641    }
00642 
00643    TGraph* first = new TGraph(1, &firstX, &firstY);
00644    first->SetLineColor(kGreen);
00645    first->SetMarkerStyle(3);
00646    first->SetMarkerSize(2);
00647    first->SetMarkerColor(kGreen);
00648    first->Draw("L,P,same");
00649 
00650    //walkCanvas->Update();
00651    delete [] x;
00652    delete [] y;
00653    if (burnInX != NULL) delete [] burnInX;
00654    if (burnInY != NULL) delete [] burnInY;
00655    //delete walk;
00656    //delete burnIn;
00657    //delete first;
00658 }
00659 
00660 void MCMCIntervalPlot::DrawParameterVsTime(RooRealVar& param)
00661 {
00662    const MarkovChain* markovChain = fInterval->GetChain();
00663    Int_t size = markovChain->Size();
00664    Int_t numEntries = 2 * size;
00665    Double_t* value = new Double_t[numEntries];
00666    Double_t* time = new Double_t[numEntries];
00667    Double_t val;
00668    Int_t weight;
00669    Int_t t = 0;
00670    for (Int_t i = 0; i < size; i++) {
00671       val = markovChain->Get(i)->getRealValue(param.GetName());
00672       weight = (Int_t)markovChain->Weight();
00673       value[2*i] = val;
00674       value[2*i + 1] = val;
00675       time[2*i] = t;
00676       t += weight;
00677       time[2*i + 1] = t;
00678    }
00679 
00680    TString title(GetTitle());
00681    Bool_t isEmpty = (title.CompareTo("") == 0);
00682 
00683    TGraph* paramGraph = new TGraph(numEntries, time, value);
00684    if (isEmpty)
00685       paramGraph->SetTitle(Form("%s vs. time in Markov chain",param.GetName()));
00686    else
00687       paramGraph->SetTitle(GetTitle());
00688    paramGraph->GetXaxis()->SetTitle("Time (discrete steps)");
00689    paramGraph->GetYaxis()->SetTitle(param.GetName());
00690    //paramGraph->SetLineColor(fLineColor);
00691    paramGraph->Draw("A,L,same");
00692    delete [] value; 
00693    delete [] time; 
00694    //gPad->Update();
00695 }
00696 
00697 void MCMCIntervalPlot::DrawNLLVsTime()
00698 {
00699    const MarkovChain* markovChain = fInterval->GetChain();
00700    Int_t size = markovChain->Size();
00701    Int_t numEntries = 2 * size;
00702    Double_t* nllValue = new Double_t[numEntries];
00703    Double_t* time = new Double_t[numEntries];
00704    Double_t nll;
00705    Int_t weight;
00706    Int_t t = 0;
00707    for (Int_t i = 0; i < size; i++) {
00708       nll = markovChain->NLL(i);
00709       weight = (Int_t)markovChain->Weight();
00710       nllValue[2*i] = nll;
00711       nllValue[2*i + 1] = nll;
00712       time[2*i] = t;
00713       t += weight;
00714       time[2*i + 1] = t;
00715    }
00716 
00717    TString title(GetTitle());
00718    Bool_t isEmpty = (title.CompareTo("") == 0);
00719 
00720    TGraph* nllGraph = new TGraph(numEntries, time, nllValue);
00721    if (isEmpty)
00722       nllGraph->SetTitle("NLL value vs. time in Markov chain");
00723    else
00724       nllGraph->SetTitle(GetTitle());
00725    nllGraph->GetXaxis()->SetTitle("Time (discrete steps)");
00726    nllGraph->GetYaxis()->SetTitle("NLL (-log(likelihood))");
00727    //nllGraph->SetLineColor(fLineColor);
00728    nllGraph->Draw("A,L,same");
00729    //gPad->Update();
00730    delete [] nllValue; 
00731    delete [] time; 
00732 }
00733 
00734 void MCMCIntervalPlot::DrawNLLHist(const Option_t* options)
00735 {
00736    if (fNLLHist == NULL) {
00737       const MarkovChain* markovChain = fInterval->GetChain();
00738       // find the max NLL value
00739       Double_t maxNLL = 0;
00740       Int_t size = markovChain->Size();
00741       for (Int_t i = 0; i < size; i++)
00742          if (markovChain->NLL(i) > maxNLL)
00743             maxNLL = markovChain->NLL(i);
00744       RooRealVar* nllVar = fInterval->GetNLLVar();
00745       fNLLHist = new TH1F("mcmc_nll_hist", "MCMC NLL Histogram",
00746             nllVar->getBins(), 0, maxNLL);
00747       TString title(GetTitle());
00748       Bool_t isEmpty = (title.CompareTo("") == 0);
00749       if (!isEmpty)
00750          fNLLHist->SetTitle(GetTitle());
00751       fNLLHist->GetXaxis()->SetTitle("-log(likelihood)");
00752       for (Int_t i = 0; i < size; i++)
00753          fNLLHist->Fill(markovChain->NLL(i), markovChain->Weight());
00754    }
00755    fNLLHist->Draw(options);
00756 }
00757 
00758 void MCMCIntervalPlot::DrawWeightHist(const Option_t* options)
00759 {
00760    if (fWeightHist == NULL) {
00761       const MarkovChain* markovChain = fInterval->GetChain();
00762       // find the max weight value
00763       Double_t maxWeight = 0;
00764       Int_t size = markovChain->Size();
00765       for (Int_t i = 0; i < size; i++)
00766          if (markovChain->Weight(i) > maxWeight)
00767             maxWeight = markovChain->Weight(i);
00768       fWeightHist = new TH1F("mcmc_weight_hist", "MCMC Weight Histogram",
00769             (Int_t)(maxWeight + 1), 0, maxWeight * 1.02);
00770       for (Int_t i = 0; i < size; i++)
00771          fWeightHist->Fill(markovChain->Weight(i));
00772    }
00773    fWeightHist->Draw(options);
00774 }
00775 
00776 /*
00777 /////////////////////////////////////////////////////////////////////
00778   // 3-d plot of the parameter points
00779   dataCanvas->cd(2);
00780   // also plot the points in the markov chain
00781   RooDataSet* markovChainData = ((MCMCInterval*)mcmcint)->GetChainAsDataSet();
00782 
00783   TTree& chain =  ((RooTreeDataStore*) markovChainData->store())->tree();
00784   chain.SetMarkerStyle(6);
00785   chain.SetMarkerColor(kRed);
00786   chain.Draw("s:ratioSigEff:ratioBkgEff","","box"); // 3-d box proporional to posterior
00787 
00788   // the points used in the profile construction
00789   TTree& parameterScan =  ((RooTreeDataStore*) fc.GetPointsToScan()->store())->tree();
00790   parameterScan.SetMarkerStyle(24);
00791   parameterScan.Draw("s:ratioSigEff:ratioBkgEff","","same");
00792 
00793   chain.SetMarkerStyle(6);
00794   chain.SetMarkerColor(kRed);
00795   //chain.Draw("s:ratioSigEff:ratioBkgEff", "_MarkovChain_local_nll","box");
00796   //chain.Draw("_MarkovChain_local_nll");
00797 ////////////////////////////////////////////////////////////////////
00798 */

Generated on Tue Jul 5 15:14:35 2011 for ROOT_528-00b_version by  doxygen 1.5.1