• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

BCSummaryTool.cxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008-2012, Daniel Kollar and Kevin Kroeninger.
00003  * All rights reserved.
00004  *
00005  * For the licensing terms see doc/COPYING.
00006  */
00007 
00008 // ---------------------------------------------------------
00009 
00010 #include <TCanvas.h>
00011 #include <TPostScript.h>
00012 #include <TLegend.h>
00013 #include <TH2D.h>
00014 #include <TGraphErrors.h>
00015 #include <TGraphAsymmErrors.h>
00016 #include <TStyle.h>
00017 #include <TLatex.h>
00018 #include <TMarker.h>
00019 #include <TArrow.h>
00020 #include <TGaxis.h>
00021 #include <TF1.h>
00022 #include <TLine.h>
00023 
00024 #include <iostream>
00025 #include <fstream>
00026 
00027 #include "BCModel.h"
00028 #include "BCSummaryPriorModel.h"
00029 #include "BCH1D.h"
00030 #include "BCH2D.h"
00031 #include "BCLog.h"
00032 #include "BCMath.h"
00033 
00034 #include "BCSummaryTool.h"
00035 
00036 unsigned int BCSummaryTool::fHCounter=0;
00037 
00038 // ---------------------------------------------------------
00039 BCSummaryTool::BCSummaryTool()
00040    : fModel(0)
00041    , fPriorModel(0)
00042    , fFlagInfoMarg(false)
00043    , fFlagInfoOpt(false)
00044 {
00045    // define sum of probabilities for quantiles
00046    fSumProb.push_back(0.05);
00047    fSumProb.push_back(0.10);
00048    fSumProb.push_back(0.1587);
00049    fSumProb.push_back(0.50);
00050    fSumProb.push_back(0.8413);
00051    fSumProb.push_back(0.90);
00052    fSumProb.push_back(0.95);
00053 
00054    // set text style
00055    gStyle->SetPaintTextFormat(".2g");
00056 }
00057 
00058 // ---------------------------------------------------------
00059 BCSummaryTool::BCSummaryTool(BCModel * model)
00060    : fModel(model)
00061    , fPriorModel(0)
00062    , fFlagInfoMarg(false)
00063    , fFlagInfoOpt(false)
00064 {
00065    // define sum of probabilities for quantiles
00066    fSumProb.push_back(0.05);
00067    fSumProb.push_back(0.10);
00068    fSumProb.push_back(0.1587);
00069    fSumProb.push_back(0.50);
00070    fSumProb.push_back(0.8413);
00071    fSumProb.push_back(0.90);
00072    fSumProb.push_back(0.95);
00073 
00074    // set text style
00075    gStyle->SetPaintTextFormat(".2g");
00076 }
00077 
00078 // ---------------------------------------------------------
00079 BCSummaryTool::~BCSummaryTool()
00080 {
00081    delete fPriorModel;
00082 }
00083 
00084 // ---------------------------------------------------------
00085 int BCSummaryTool::CopySummaryData()
00086 {
00087    // check if model exists
00088    if (!fModel)
00089       return 0;
00090 
00091    // clear information
00092    fParName.clear();
00093    fParMin.clear();
00094    fParMax.clear();
00095    fMean.clear();
00096    fMargMode.clear();
00097    fGlobalMode.clear();
00098    fQuantiles.clear();
00099    fSmallInt.clear();
00100    fRMS.clear();
00101    fCorrCoeff.clear();
00102 
00103    // get number of parameters and quantiles
00104    int npar = fModel->GetNParameters();
00105    int nquantiles = int( fSumProb.size() );
00106 
00107    // copy information from marginalized distributions
00108    for (int i = 0; i < npar; ++i) {
00109 
00110       // copy parameter information
00111       fParName.push_back( (fModel->GetParameter(i)->GetName()) );
00112       fParMin.push_back( fModel->GetParameter(i)->GetLowerLimit() );
00113       fParMax.push_back( fModel->GetParameter(i)->GetUpperLimit() );
00114 
00115       // copy 1D marginalized information
00116       if (fModel->MCMCGetFlagRun()) {
00117          fFlagInfoMarg = true;
00118          BCH1D * bch1d_temp = fModel->GetMarginalized( fModel->GetParameter(i) );
00119          if (bch1d_temp) {
00120             fMean.push_back( bch1d_temp->GetMean() );
00121             fRMS.push_back( bch1d_temp->GetRMS() );
00122             fMargMode.push_back( bch1d_temp->GetMode() );
00123             for (int j = 0; j < nquantiles; ++j)
00124                fQuantiles.push_back( bch1d_temp->GetQuantile( fSumProb.at(j) ) );
00125             std::vector<double> intervals = bch1d_temp->GetSmallestIntervals();
00126             int nintervals = int(intervals.size() / 5);
00127             fSmallInt.push_back(nintervals);
00128             fSmallInt.insert( fSmallInt.end(), intervals.begin(), intervals.end() );
00129          }
00130          else {
00131             double tmpval = fModel->GetParameter(i)->GetUpperLimit() - fModel->GetParameter(i)->GetLowerLimit();
00132             tmpval = fModel->GetParameter(i)->GetLowerLimit() - 2. * tmpval;
00133             fMean.push_back( tmpval );
00134             fRMS.push_back( tmpval );
00135             fMargMode.push_back( tmpval );
00136             for (int j = 0; j < nquantiles; ++j)
00137                fQuantiles.push_back( tmpval );
00138             fSmallInt.push_back( 0 );
00139 //            fSmallInt.insert( fSmallInt.end(), intervals.begin(), intervals.end() );
00140          }
00141 
00142          // copy 2D margnialized information
00143          for (int j = 0; j < npar; ++j) {
00144             if (i!=j) {
00145                BCH2D * bch2d_temp = fModel->GetMarginalized(fModel->GetParameter(i),fModel->GetParameter(j));
00146                if ( bch2d_temp )
00147                   fCorrCoeff.push_back( bch2d_temp->GetHistogram()->GetCorrelationFactor() );
00148                else
00149                   fCorrCoeff.push_back( 0. );
00150             }
00151             else
00152                fCorrCoeff.push_back(1.);
00153          }
00154       }
00155       else {
00156          //         BCLog::OutWarning("BCSummaryTool::CopySummaryData : No information on marginalized distributions present.");
00157       }
00158 
00159       // copy optimization information
00160       if ((fModel->GetBestFitParameters()).size() > 0) {
00161          fFlagInfoOpt = true;
00162          fGlobalMode.push_back ( (fModel->GetBestFitParameters()).at(i) );
00163       }
00164       else {
00165          //         BCLog::OutWarning("BCSummaryTool::CopySummaryData : No information on optimization present.");
00166       }
00167    }
00168 
00169    // no error
00170    return 1;
00171 }
00172 
00173 // ---------------------------------------------------------
00174 int BCSummaryTool::PrintParameterPlot(const char * filename)
00175 {
00176    // copy summary data
00177    if (!CopySummaryData())
00178       return 0;
00179 
00180    // get number of parameters and quantiles
00181    int npar = fModel->GetNParameters();
00182    int nquantiles = int( fSumProb.size() );
00183 
00184    // create histogram
00185    TH1D * hist_axes = new TH1D(
00186          TString::Format("hist_axes_par_%d",getNextIndex()),
00187          ";;Scaled parameter range [a.u.]",npar, -0.5, npar-0.5);
00188    hist_axes->SetStats(kFALSE);
00189    for (int i = 0; i < npar; ++i)
00190       hist_axes->GetXaxis()->SetBinLabel( i+1, fParName.at(i).c_str() );
00191 //   hist_axes->GetXaxis()->SetLabelOffset(0.03);
00192    hist_axes->GetXaxis()->SetLabelSize(0.06);
00193    hist_axes->GetXaxis()->SetTickLength(0.0);
00194    hist_axes->GetYaxis()->SetRangeUser(-0.1, 1.1);
00195    hist_axes->GetYaxis()->SetTickLength(0.0);
00196 
00197    // create graphs
00198    TGraphErrors * graph_quantiles = new TGraphErrors(npar*nquantiles);
00199    graph_quantiles->SetMarkerSize(0);
00200    graph_quantiles->SetLineColor(38);
00201    graph_quantiles->SetLineStyle(2);
00202 
00203    TGraphErrors * graph_mean = new TGraphErrors(npar);
00204    graph_mean->SetMarkerColor(kBlack);
00205    graph_mean->SetMarkerStyle(20);
00206 
00207    TGraphErrors * graph_mode = new TGraphErrors(npar);
00208    graph_mode->SetMarkerColor(kRed);
00209    graph_mode->SetMarkerStyle(20);
00210 
00211    TGraphAsymmErrors * graph_intervals = new TGraphAsymmErrors(0);
00212    graph_intervals->SetFillColor(kYellow);
00213    graph_intervals->SetLineStyle(2);
00214    graph_intervals->SetLineColor(kRed);
00215    graph_intervals->SetMarkerSize(0);
00216 
00217    // fill graphs
00218    int indexintervals = 0;
00219 
00220    // fill graph quantiles
00221    if (fFlagInfoMarg) {
00222       for (int i = 0; i < npar; ++i) {
00223          for (int j = 0; j < nquantiles; ++j) {
00224             graph_quantiles->SetPoint(i*nquantiles+j,double(i),
00225                   (fQuantiles.at(i*nquantiles+j) - fParMin.at(i))/(fParMax.at(i)-fParMin.at(i)));
00226             graph_quantiles->SetPointError(i*nquantiles+j, 0.5, 0.0);
00227          }
00228       }
00229    }
00230 
00231    // fill graph mean and rms
00232    if (fFlagInfoMarg) {
00233       for (int i = 0; i < npar; ++i) {
00234          // fill graph mean
00235          graph_mean->SetPoint(i, double(i), (fMean.at(i) - fParMin.at(i))/(fParMax.at(i)-fParMin.at(i)));
00236          graph_mean->SetPointError(i, 0.0, fRMS.at(i)/(fParMax.at(i)-fParMin.at(i)));
00237       }
00238    }
00239 
00240    // fill graph mode
00241    if (fFlagInfoOpt) {
00242       for (int i = 0; i < npar; ++i)
00243          graph_mode->SetPoint(i, double(i), (fGlobalMode.at(i) - fParMin.at(i))/(fParMax.at(i)-fParMin.at(i)));
00244    }
00245 
00246    // fill graph smallest intervals
00247    if (fFlagInfoMarg) {
00248       for (int i = 0; i < npar; ++i) {
00249          int nintervals = int(fSmallInt.at(indexintervals++));
00250          for (int j = 0; j < nintervals; ++j) {
00251             double xmin = fSmallInt.at(indexintervals++);
00252             double xmax = fSmallInt.at(indexintervals++);
00253             indexintervals++;
00254             double xlocalmaxpos = fSmallInt.at(indexintervals++);
00255             indexintervals++;
00256             int npoints = graph_intervals->GetN();
00257             graph_intervals->SetPoint(npoints,double(i),
00258                   (xlocalmaxpos - fParMin.at(i))/(fParMax.at(i)-fParMin.at(i)));
00259             graph_intervals->SetPointError(npoints,0.5, 0.5,
00260                   (xlocalmaxpos - xmin)/(fParMax.at(i)-fParMin.at(i)),
00261                   (xmax - xlocalmaxpos)/(fParMax.at(i)-fParMin.at(i)));
00262          }
00263       }
00264    }
00265 
00266    // create legend
00267    TLegend * legend = new TLegend(0.15, 0.88, 0.85, 0.99);
00268    legend->SetBorderSize(0);
00269    legend->SetFillColor(0);
00270 
00271    // create latex
00272    TLatex * latex = new TLatex();
00273    latex->SetTextSize(0.02);
00274 
00275    // create lines
00276    TLine * line_top = new TLine(-0.5, 1.0, npar-0.5, 1.0);
00277    line_top->SetLineColor(kBlack);
00278    line_top->SetLineStyle(1);
00279    line_top->SetLineWidth(2);
00280 
00281    TLine * line_bot = new TLine(-0.5, 0.0, npar-0.5, 0.0);
00282    line_bot->SetLineColor(kBlack);
00283    line_bot->SetLineStyle(1);
00284    line_bot->SetLineWidth(2);
00285 
00286    // print to file
00287    TCanvas * c_par = new TCanvas(TString::Format("c_par_%d",getNextIndex()));
00288    c_par->cd();
00289    hist_axes->Draw();
00290    line_top->Draw();
00291    line_bot->Draw();
00292    if (fFlagInfoMarg) {
00293       graph_intervals->DrawClone("SAME2");
00294       for (int i = 0; i < graph_intervals->GetN(); ++i)
00295          graph_intervals->SetPointError(i, 0.5, 0.5, 0.0, 0.0);
00296       graph_intervals->Draw("SAMEPZ");
00297       graph_quantiles->Draw("SAMEPZ");
00298       graph_mean->Draw("SAMEP");
00299       legend->AddEntry(graph_quantiles, "Quantiles (5%, 10%, 16%, 50%, 84%, 90%, 95%)", "L");
00300       legend->AddEntry(graph_mean,      "Mean and RMS", "LEP");
00301       legend->AddEntry(graph_intervals, "Smallest 68% intervals and local modes", "FL");
00302    }
00303    if (fFlagInfoOpt) {
00304       graph_mode->Draw("SAMEP");
00305       legend->AddEntry(graph_mode,      "Global mode", "P");
00306    }
00307    for (int i = 0; i < npar;++i) {
00308       //      latex->DrawLatex(double(i)-0.1, 0.010, Form("%+3.3f", fParMin.at(i)));
00309       //      latex->DrawLatex(double(i)-0.1, 0.965, Form("%+3.3f", fParMax.at(i)));
00310       latex->DrawLatex(double(i)-0.1, 0.010-0.07, Form("%+3.3f", fParMin.at(i)));
00311       latex->DrawLatex(double(i)-0.1, 0.965+0.07, Form("%+3.3f", fParMax.at(i)));
00312    }
00313    latex->SetNDC();
00314    latex->DrawLatex(0.9, 0.175, "Par. min.");
00315    latex->DrawLatex(0.9, 0.83, "Par. max.");
00316    legend->Draw("SAME");
00317    gPad->RedrawAxis();
00318    c_par->Print(filename);
00319 
00320    // no error
00321    return 1;
00322 }
00323 
00324 // ---------------------------------------------------------
00325 int BCSummaryTool::PrintCorrelationMatrix(const char * filename)
00326 {
00327    // copy summary data
00328    if (!CopySummaryData())
00329       return 0;
00330 
00331    // check if marginalized information is there
00332    if (!fFlagInfoMarg)
00333       return 0;
00334 
00335    // get number of parameters
00336    int npar = fModel->GetNParameters();
00337 
00338    // create histogram
00339    TH2D * hist_corr = new TH2D(
00340          TString::Format("hist_corr_%d",getNextIndex()),
00341          ";;",npar, -0.5, npar-0.5,npar, -0.5, npar-0.5);
00342    hist_corr->SetStats(kFALSE);
00343    hist_corr->GetXaxis()->SetTickLength(0.0);
00344 //   hist_corr->GetXaxis()->SetLabelOffset(0.03);
00345    hist_corr->GetYaxis()->SetTickLength(0.0);
00346 //   hist_corr->GetYaxis()->SetLabelOffset(0.03);
00347    hist_corr->GetZaxis()->SetRangeUser(-1.0, 1.0);
00348 
00349    for (int i = 0; i < npar; ++i) {
00350       hist_corr->GetXaxis()->SetLabelSize(0.06);
00351       hist_corr->GetYaxis()->SetLabelSize(0.06);
00352       if (npar < 5) {
00353          hist_corr->GetXaxis()->SetBinLabel( i+1, fParName.at(i).c_str() );
00354          hist_corr->GetYaxis()->SetBinLabel( npar-i, fParName.at(i).c_str() );
00355       }
00356       else {
00357          hist_corr->GetXaxis()->SetBinLabel( i+1, TString::Format("%d",i) );
00358          hist_corr->GetYaxis()->SetBinLabel( npar-i, TString::Format("%d",i) );
00359       }
00360    }
00361 
00362    // fill plot
00363    for (int i = 0; i < npar; ++i)
00364       for (int j = 0; j < npar; ++j) {
00365          int index = i * npar + j;
00366          double corr = fCorrCoeff.at(index);
00367          hist_corr->SetBinContent(i+1, npar-j, corr);
00368       }
00369 
00370    // print to file
00371    TCanvas * c_corr = new TCanvas(TString::Format("c_corr_matrix_%d",getNextIndex()));
00372    c_corr->cd();
00373    hist_corr->Draw("colz text");
00374 
00375    TF1 * f = new TF1("fUp","x",-0.5,npar-0.5);
00376    TGaxis * A1 = new TGaxis(-0.5,npar-0.5,npar-0.5,npar-0.5,"fUp",100,"-");
00377    A1->ImportAxisAttributes(hist_corr->GetXaxis());
00378    A1->Draw();
00379 
00380    // redraw the histogram to overlay thetop axis tick marks since
00381    // we don't know how to make them disappear
00382    hist_corr->GetXaxis()->SetLabelSize(0.);
00383    hist_corr->Draw("colz text same");
00384 
00385    for (int i = 0; i < npar; ++i)
00386       for (int j = 0; j < npar; ++j) {
00387          BCH2D * bch2d_temp = fModel->GetMarginalized(fModel->GetParameter(i),fModel->GetParameter(j));
00388          if ( bch2d_temp || i==j )
00389             continue;
00390 
00391          TBox * bempty = new TBox(
00392             hist_corr->GetXaxis()->GetBinLowEdge(i+1),
00393             hist_corr->GetYaxis()->GetBinLowEdge(npar-j),
00394             hist_corr->GetXaxis()->GetBinLowEdge(i+2),
00395             hist_corr->GetYaxis()->GetBinLowEdge(npar-j+1)
00396          );
00397          bempty->SetLineStyle(0);
00398          bempty->SetLineWidth(0);
00399          bempty->SetFillColor(kWhite);
00400          bempty->Draw();
00401       }
00402 
00403    // redraw top and right axes
00404    TLine * lA1 = new TLine(-0.5,npar-0.5,npar-0.5,npar-0.5);
00405    lA1->Draw("same");
00406    TLine * lA2 = new TLine(npar-0.5,npar-0.5,npar-0.5,-0.5);
00407    lA2->Draw("same");
00408 
00409    gPad->RedrawAxis();
00410    c_corr->Print(filename);
00411 
00412    delete f;
00413    delete A1;
00414    delete lA1;
00415    delete lA2;
00416    delete hist_corr;
00417    delete c_corr;
00418 
00419    // no error
00420    return 1;
00421 }
00422 
00423 // ---------------------------------------------------------
00424 int BCSummaryTool::PrintCorrelationPlot(const char * filename)
00425 {
00426    // check if marginalized information is there
00427    if (!fModel->MCMCGetFlagRun()) {
00428       BCLog::OutError("BCSummaryTool::PrintCorrelationPlot : MCMC was not run. Marginalized distributions not available.");
00429       return 0;
00430    }
00431 
00432    // get number of parameters
00433    int npar = fModel->GetNParameters();
00434 
00435    TCanvas * c = new TCanvas(TString::Format("c_corr_%d",getNextIndex()));
00436    c->cd();
00437 
00438    double margin = .1;
00439    double padsize = (1. - 2.*margin) / (double)npar;
00440 
00441    // array with pads holding the histograms
00442    TPad ** pad = new TPad*[npar*npar];
00443 
00444    // position of pads
00445    double xlow, xup, ylow, yup;
00446    double marginleft, marginright, margintop, marginbottom;
00447    marginleft=marginright=margintop=marginbottom=0.01;
00448 
00449    // drawing all histograms
00450    for (int i=npar-1;i>=0;--i) {
00451       xlow = (double)i*padsize + margin;
00452       xup = xlow+padsize;
00453 
00454       for (int j=i;j<=npar-1;j++) {
00455          yup = 1. - (double)j*padsize - margin;
00456          ylow = yup-padsize;
00457 
00458          // preparing the pad
00459          int ipad=i*npar+j;
00460          pad[ipad]=new TPad(TString::Format("pad_%d_%d",ipad,getNextIndex()), "", xlow, ylow, xup, yup);
00461          pad[ipad]->SetTopMargin(margintop);
00462          pad[ipad]->SetBottomMargin(marginbottom);
00463          pad[ipad]->SetLeftMargin(marginleft);
00464          pad[ipad]->SetRightMargin(marginright);
00465          pad[ipad]->SetFillColor(kWhite);
00466 
00467          pad[ipad]->Draw();
00468          pad[ipad]->cd();
00469 
00470 
00471          TH1 * hh = 0;
00472          // get the histogram
00473          if(i==j) {
00474             BCH1D * bh1 = fModel->GetMarginalized(fModel->GetParameter(i));
00475             if (bh1)
00476                hh = (TH1D*)bh1->GetHistogram()->Clone();
00477          }
00478          else {
00479             BCH2D * bh2 = fModel->GetMarginalized(fModel->GetParameter(i),fModel->GetParameter(j));
00480             if (bh2)
00481                hh = (TH2D*)bh2->GetHistogram()->Clone();
00482          }
00483 
00484          // if the histogram is not available, draw N/A
00485          if (!hh) {
00486             pad[ipad]->SetFillColor(kGray);
00487             TBox * box = new TBox(marginleft,marginbottom,1.-marginright,1.-margintop);
00488             box->SetLineWidth(1);
00489             box->SetLineColor(kGray+1);
00490             box->SetFillColor(kWhite);
00491             box->Draw();
00492 
00493             TText * text_na = new TText(.5,.5,"N/A");
00494             text_na->SetTextFont(42);
00495             text_na->SetTextAlign(22);
00496             text_na->SetTextSize(.8/(double)npar);
00497             text_na->SetTextColor(kGray+1);
00498             text_na->Draw();
00499          }
00500          // otherwise draw the histogram
00501          else {
00502             if(i==j) {
00503                hh->SetFillStyle(1001);
00504                hh->SetFillColor(kYellow);
00505                hh->Draw("hist");
00506             }
00507             else {
00508                hh->SetContour(20);
00509                hh->Draw("col");
00510             }
00511 
00512             hh->GetXaxis()->SetLabelOffset(999);
00513             hh->GetYaxis()->SetLabelOffset(999);
00514             hh->GetXaxis()->SetTitleSize(0.);
00515             hh->GetYaxis()->SetTitleSize(0.);
00516          }
00517 
00518          c->cd();
00519 
00520          // draw axis label
00521          double labelsize = .8/(double)npar/5.;
00522          double xtext, ytext;
00523 
00524          // y axis
00525          if(i==0) {
00526             TText * label = new TText;
00527             label->SetTextFont(62);
00528             label->SetTextSize(labelsize);
00529             label->SetTextAlign(22);
00530             label->SetNDC();
00531 
00532             label->SetTextAngle(90);
00533 
00534             xtext = margin * (1. - 8. * labelsize);
00535             ytext = yup - padsize / 2.;
00536 
00537             label->DrawText(xtext,ytext,fModel->GetParameter(j)->GetName().c_str());
00538          }
00539 
00540          // x axis
00541          if(j==npar-1) {
00542             TText * label = new TText;
00543             label->SetTextFont(62);
00544             label->SetTextSize(labelsize);
00545             label->SetTextAlign(22);
00546             label->SetNDC();
00547 
00548             xtext = xlow + padsize / 2.;
00549             ytext = margin * (1. - 6. * labelsize);
00550 
00551             label->DrawText(xtext,ytext,fModel->GetParameter(i)->GetName().c_str());
00552          }
00553       }
00554    }
00555 
00556    gPad->RedrawAxis();
00557    c->Print(filename);
00558 
00559    return 1;
00560 }
00561 
00562 // ---------------------------------------------------------
00563 int BCSummaryTool::PrintKnowledgeUpdatePlot1D(int index, const char * filename, double min, double max)
00564 {
00565    // create canvas
00566    TCanvas * c = new TCanvas();
00567    c->cd();
00568 
00569    // draw
00570    DrawKnowledgeUpdatePlot1D(index, min, max);
00571 
00572    // print
00573    c->Print(filename);
00574 
00575    // no error
00576    return 1;
00577 
00578 }
00579 
00580 // ---------------------------------------------------------
00581 int BCSummaryTool::DrawKnowledgeUpdatePlot1D(int index, double min, double max)
00582 {
00583    // create legend
00584    TLegend * legend1d = new TLegend(0.50, 0.88, 0.85, 0.99);
00585    legend1d->SetBorderSize(0);
00586    legend1d->SetFillColor(0);
00587 
00588    // get histograms;
00589    BCParameter * par = fModel->GetParameter(index);
00590    TH1D * hist_prior = fPriorModel->GetMarginalized(par)->GetHistogram();
00591    hist_prior->SetLineColor(kRed);
00592    TH1D * hist_posterior = fModel->GetMarginalized(par)->GetHistogram();
00593 
00594    legend1d->AddEntry(hist_prior, "Prior probability", "L");
00595    legend1d->AddEntry(hist_posterior, "Posterior probability", "L");
00596 
00597    // scale histogram
00598    hist_prior->Scale(hist_posterior->Integral()/hist_prior->Integral());
00599 
00600    // get maximum
00601    double max_prior = hist_prior->GetMaximum();
00602    double max_posterior = hist_posterior->GetMaximum();
00603    double maxy = 1.1 * TMath::Max(max_prior, max_posterior);
00604 
00605    // plot
00606    hist_prior->GetXaxis()->SetNdivisions(508);
00607    hist_posterior->GetXaxis()->SetNdivisions(508);
00608    // debugKK
00609    if (min != max) {
00610       double qmin = fModel->GetMarginalized(par)->GetQuantile(min);
00611       double qmax = fModel->GetMarginalized(par)->GetQuantile(max);
00612       hist_posterior->Draw();
00613       TH1D * hist_shaded = fModel->GetMarginalized(par)->GetSubHisto(qmin,qmax,"");
00614       hist_shaded->SetFillStyle(1001);
00615       hist_shaded->SetFillColor(kYellow);
00616       legend1d->AddEntry(hist_shaded, Form("%.0f%% - %.0f%% prob.", min*100., max*100.), "F");
00617       hist_shaded->Draw("same");
00618       hist_prior->Draw("SAME");
00619       hist_posterior->Draw("SAME");
00620       gPad->RedrawAxis();
00621    }
00622    else {
00623       hist_prior->Draw();
00624       hist_posterior->Draw("SAME");
00625    }
00626 
00627    legend1d->Draw("SAME");
00628 
00629    // scale axes
00630    hist_prior->GetYaxis()->SetRangeUser(0.0, maxy);
00631    hist_posterior->GetYaxis()->SetRangeUser(0.0, maxy);
00632 
00633    return 1;
00634 }
00635 
00636 // ---------------------------------------------------------
00637 int BCSummaryTool::PrintKnowledgeUpdatePlots(const char * filename)
00638 {
00639    // perform analysis
00640    CalculatePriorModel();
00641 
00642    // create postscript
00643    TPostScript * ps = new TPostScript(filename);
00644 
00645    // create canvas and prepare postscript
00646    TCanvas * c_update = new TCanvas(TString::Format("c_update_%d",getNextIndex()));
00647 
00648    c_update->Update();
00649    ps->NewPage();
00650    c_update->cd();
00651 
00652    // create legend
00653    //   TLegend * legend1d = new TLegend(0.50, 0.88, 0.85, 0.94);
00654    //   legend1d->SetBorderSize(0);
00655    //   legend1d->SetFillColor(0);
00656 
00657    // loop over all parameters
00658    int npar = fModel->GetNParameters();
00659    for (int i = 0; i < npar; ++i) {
00660       // update post script
00661       c_update->Update();
00662       ps->NewPage();
00663       c_update->cd();
00664 
00665       /*
00666       // get histograms;
00667       BCParameter * par = fModel->GetParameter(i);
00668       TH1D * hist_prior = fPriorModel->GetMarginalized(par)->GetHistogram();
00669       hist_prior->SetLineColor(kRed);
00670       TH1D * hist_posterior = fModel->GetMarginalized(par)->GetHistogram();
00671 
00672       // add entries
00673       if (!i) {
00674          legend1d->AddEntry(hist_prior, "Prior probability", "L");
00675          legend1d->AddEntry(hist_posterior, "Posterior probability", "L");
00676       }
00677 
00678       // scale histogram
00679       hist_prior->Scale(hist_posterior->Integral()/hist_prior->Integral());
00680 
00681       // get maximum
00682       double max_prior = hist_prior->GetMaximum();
00683       double max_posterior = hist_posterior->GetMaximum();
00684       double max = 1.1 * TMath::Max(max_prior, max_posterior);
00685 
00686       // plot
00687       c_update->cd();
00688       hist_prior->GetXaxis()->SetNdivisions(508);
00689       hist_posterior->GetXaxis()->SetNdivisions(508);
00690       // debugKK
00691       if (min != max) {
00692          double qmin = fModel->GetMarginalized(par)->GetQuantile(min);
00693          double qmax = fModel->GetMarginalized(par)->GetQuantile(max);
00694          hist_posterior->Draw("SAME");
00695          TH1D * hist_shaded = fModel->GetMarginalized(par)->GetSubHisto(qmin,qmax,"");
00696          hist_shaded->SetFillStyle(1001);
00697          hist_shaded->SetFillColor(kYellow);
00698          hist_shaded->Draw("same");
00699          hist_prior->Draw("SAME");
00700          hist_posterior->Draw("SAME");
00701       }
00702       // debugKK
00703       //      hist_prior->Draw();
00704       //      hist_posterior->Draw("SAME");
00705       legend1d->Draw("SAME");
00706 
00707       // scale axes
00708       hist_prior->GetYaxis()->SetRangeUser(0.0, max);
00709       hist_posterior->GetYaxis()->SetRangeUser(0.0, max);
00710       */
00711       c_update->cd();
00712       DrawKnowledgeUpdatePlot1D(i, 0., 0.);
00713    }
00714 
00715    // create legend
00716    TLegend * legend2d = new TLegend(0.50, 0.88, 0.85, 0.99);
00717    legend2d->SetBorderSize(0);
00718    legend2d->SetFillColor(0);
00719 
00720    // create markers and arrows
00721    TMarker * marker_prior = new TMarker();
00722    marker_prior->SetMarkerStyle(24);
00723    marker_prior->SetMarkerColor(kRed);
00724 
00725    TMarker * marker_posterior = new TMarker();
00726    marker_posterior->SetMarkerStyle(24);
00727    marker_posterior->SetMarkerColor(kBlack);
00728 
00729    TArrow * arrow = new TArrow();
00730    arrow->SetArrowSize(0.02);
00731    arrow->SetLineColor(kBlue);
00732    arrow->SetLineStyle(2);
00733 
00734    // loop over all parameters
00735    for (int i = 0; i < npar; ++i) {
00736       for (int j = 0; j < i; ++j) {
00737          // update post script
00738          c_update->Update();
00739          ps->NewPage();
00740          c_update->cd();
00741 
00742          // get parameters
00743          BCParameter * par1 = fModel->GetParameter(i);
00744          BCParameter * par2 = fModel->GetParameter(j);
00745 
00746          // get 2-d histograms
00747          BCH2D * bch2d_2dprior = fPriorModel->GetMarginalized(par1, par2);
00748          BCH2D * bch2d_2dposterior = fModel->GetMarginalized(par1, par2);
00749 
00750          // get histograms
00751          TH2D * hist_2dprior = bch2d_2dprior->GetHistogram();
00752          hist_2dprior->SetLineColor(kRed);
00753          TH2D * hist_2dposterior = bch2d_2dposterior->GetHistogram();
00754 
00755          // scale histograms
00756          hist_2dprior->Scale(1.0/hist_2dprior->Integral("width"));
00757          hist_2dposterior->Scale(1.0/hist_2dposterior->Integral("width"));
00758 
00759          // calculate contours
00760          bch2d_2dprior->CalculateIntegratedHistogram();
00761          bch2d_2dposterior->CalculateIntegratedHistogram();
00762 
00763          double level[1] = {bch2d_2dprior->GetLevel(0.32)};
00764          hist_2dprior->SetContour(1, level);
00765          hist_2dprior->Draw("CONT3");
00766          level[0] = bch2d_2dposterior->GetLevel(0.32);
00767          hist_2dposterior->SetContour(1, level);
00768          hist_2dposterior->Draw("CONT3 SAME");
00769 
00770          std::vector<double> mode_prior = fPriorModel->GetBestFitParameters();
00771          std::vector<double> mode_posterior = fModel->GetBestFitParameters();
00772 
00773          marker_prior->DrawMarker(mode_prior.at(j), mode_prior.at(i));
00774          marker_posterior->DrawMarker(mode_posterior.at(j), mode_posterior.at(i));
00775          arrow->DrawArrow(mode_prior.at(j), mode_prior.at(i), mode_posterior.at(j), mode_posterior.at(i));
00776 
00777          if (i==1 && j == 0) {
00778             legend2d->AddEntry(hist_2dprior, "68% prior contour", "L");
00779             legend2d->AddEntry(hist_2dposterior, "68% posterior contour", "L");
00780             legend2d->AddEntry(marker_prior, "Prior mode", "P");
00781             legend2d->AddEntry(marker_posterior, "Posterior mode", "P");
00782             legend2d->AddEntry(arrow, "Change in mode", "L");
00783          }
00784          legend2d->Draw();
00785       }
00786    }
00787 
00788    // close ps
00789    c_update->Update();
00790    ps->Close();
00791 
00792    // free memory
00793    delete legend2d;
00794    delete marker_prior;
00795    delete marker_posterior;
00796    delete arrow;
00797    delete ps;
00798    delete c_update;
00799 
00800    // no error
00801    return 1;
00802 }
00803 
00804 // // ---------------------------------------------------------
00805 // int BCSummaryTool::Print2DOverviewPlots(const char* filename)
00806 // {
00807 //    // copy summary data
00808 //    if (!CopySummaryData())
00809 //       return 0;
00810 
00811 //    // get number of parameters
00812 //    int npar = fModel->GetNParameters();
00813 
00814 //    TCanvas * c_2doverview = new TCanvas("c_2doverview");
00815 //    c_2doverview->Divide(npar+1, npar+1, 0.005, 0.005);
00816 
00817 //       for (int i = 0; i < npar; ++i) {
00818 //       for (int j = 1; j < npar+1; ++j) {
00819 //          c_2doverview->cd(1+i*(npar+1)+j);
00820 //          gPad->SetBottomMargin(0);
00821 //          gPad->SetTopMargin(0);
00822 //          gPad->SetLeftMargin(0);
00823 //          gPad->SetRightMargin(0);
00824 //          ->DrawClone();
00825 //       }
00826 //    }
00827 //    for (int i = 1; i < npar+1; ++i) {
00828 //       int index = (npar+1) * npar + i + 1;
00829 //       c_2doverview->cd(index);
00830 //       TPaveText * pt = new TPaveText(0.0, 0.0, 1.0, 1.0, "NDC");
00831 //       pt->SetTextAlign(22);
00832 //       pt->SetTextSize(0.1);
00833 //       pt->SetBorderSize(0);
00834 //       pt->SetFillStyle(0);
00835 //       pt->AddText(fParName.at(i-1));
00836 //       pt->Draw();
00837 //    }
00838 
00839 
00840 //    // no error
00841 //    return 1;
00842 // }
00843 
00844 // ---------------------------------------------------------
00845 int BCSummaryTool::PrintParameterLatex(const char * filename)
00846 {
00847    // open file
00848    std::ofstream ofi(filename);
00849    ofi.precision(3);
00850 
00851    // check if file is open
00852    if(!ofi.is_open()) {
00853       std::cerr << "Couldn't open file " << filename <<std::endl;
00854       return 0;
00855    }
00856 
00857    // get number of parameters and quantiles
00858    int npar = fModel->GetNParameters();
00859 
00860    // print table
00861    ofi
00862       << "\\documentclass[11pt, a4paper]{article}" << std::endl
00863       << std::endl
00864       << "\\begin{document}" << std::endl
00865       << std::endl
00866       << "\\begin{table}[ht!]" << std::endl
00867       << "\\begin{center}" << std::endl
00868       <<"\\begin{tabular}{llllllll}" << std::endl
00869       << "\\hline" << std::endl
00870       << "Parameter & Mean & RMS & Gl. mode & Mode & Median & 16\\% quant. & 84\\% quant. \\\\" << std::endl
00871       << "\\hline" << std::endl;
00872 
00873    for (int i = 0; i < npar; ++i) {
00874       BCParameter * par = fModel->GetParameter(i);
00875       BCH1D * bch1d = fModel->GetMarginalized(par);
00876       ofi
00877          << par->GetName() << " & "
00878          << bch1d->GetMean() << " & "
00879          << bch1d->GetRMS() << " & "
00880          << fModel->GetBestFitParameters().at(i) << " & "
00881          << bch1d->GetMode() << " & "
00882          << bch1d->GetMedian() << " & "
00883          << bch1d->GetQuantile(0.16) << " & "
00884          << bch1d->GetQuantile(0.84) << " \\\\" << std::endl;
00885    }
00886    ofi
00887       << "\\hline" << std::endl
00888       << "\\end{tabular}" << std::endl
00889       << "\\caption{Summary of the parameter estimates.}" << std::endl
00890       << "\\end{center}" << std::endl
00891       << "\\end{table}" << std::endl
00892       << std::endl
00893       << "\\end{document}" << std::endl;
00894 
00895    // close file
00896    ofi.close();
00897 
00898    // no error
00899    return 1;
00900 }
00901 
00902 // ---------------------------------------------------------
00903 int BCSummaryTool::CalculatePriorModel()
00904 {
00905    // create new prior model
00906    delete fPriorModel;
00907 
00908    fPriorModel = new BCSummaryPriorModel();
00909 
00910    // set model
00911    fPriorModel->SetModel(fModel);
00912 
00913    // perform marginalization
00914    fPriorModel->MarginalizeAll(); 
00915 
00916    // perform minimization
00917    fPriorModel->FindMode( fPriorModel->GetBestFitParameters() );
00918 
00919    // no error
00920    return 1;
00921 }
00922 
00923 // ---------------------------------------------------------

Generated by  doxygen 1.7.1