MarkovChain.cxx

Go to the documentation of this file.
00001 // @(#)root/roostats:$Id: MarkovChain.cxx 35820 2010-09-28 07:42:50Z moneta $
00002 // Authors: Kevin Belasco        17/06/2009
00003 // Authors: Kyle Cranmer         17/06/2009
00004 /*************************************************************************
00005  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //_________________________________________________
00013 /*
00014 BEGIN_HTML
00015 <p>
00016 Stores the steps in a Markov Chain of points.  Allows user to access the
00017 weight and NLL value (if applicable) with which a point was added to the
00018 MarkovChain.
00019 </p>
00020 END_HTML
00021 */
00022 //_________________________________________________
00023 
00024 #ifndef ROOT_Rtypes
00025 #include "Rtypes.h"
00026 #endif
00027 
00028 #ifndef ROOT_TNamed
00029 #include "TNamed.h"
00030 #endif
00031 #ifndef ROOSTATS_MarkovChain
00032 #include "RooStats/MarkovChain.h"
00033 #endif
00034 #ifndef ROO_DATA_SET
00035 #include "RooDataSet.h"
00036 #endif
00037 #ifndef ROO_ARG_SET
00038 #include "RooArgSet.h"
00039 #endif
00040 #ifndef ROO_REAL_VAR
00041 #include "RooRealVar.h"
00042 #endif
00043 #ifndef RooStats_RooStatsUtils
00044 #include "RooStats/RooStatsUtils.h"
00045 #endif
00046 #ifndef ROO_DATA_HIST
00047 #include "RooDataHist.h"
00048 #endif
00049 #ifndef ROOT_THnSparse
00050 #include "THnSparse.h"
00051 #endif
00052 
00053 ClassImp(RooStats::MarkovChain);
00054 
00055 using namespace RooFit;
00056 using namespace RooStats;
00057 
00058 static const char* WEIGHT_NAME = "weight_MarkovChain_local_";
00059 static const char* NLL_NAME = "nll_MarkovChain_local_";
00060 static const char* DATASET_NAME = "dataset_MarkovChain_local_";
00061 static const char* DEFAULT_NAME = "_markov_chain";
00062 static const char* DEFAULT_TITLE = "Markov Chain";
00063 
00064 MarkovChain::MarkovChain() :
00065    TNamed(DEFAULT_NAME, DEFAULT_TITLE)
00066 {
00067    fParameters = NULL;
00068    fDataEntry = NULL;
00069    fChain = NULL;
00070    fNLL = NULL;
00071    fWeight = NULL;
00072 }
00073 
00074 MarkovChain::MarkovChain(RooArgSet& parameters) :
00075    TNamed(DEFAULT_NAME, DEFAULT_TITLE)
00076 {
00077    fParameters = NULL;
00078    fDataEntry = NULL;
00079    fChain = NULL;
00080    fNLL = NULL;
00081    fWeight = NULL;
00082    SetParameters(parameters);
00083 }
00084 
00085 MarkovChain::MarkovChain(const char* name, const char* title,
00086       RooArgSet& parameters) : TNamed(name, title)
00087 {
00088    fParameters = NULL;
00089    fDataEntry = NULL;
00090    fChain = NULL;
00091    fNLL = NULL;
00092    fWeight = NULL;
00093    SetParameters(parameters);
00094 }
00095 
00096 void MarkovChain::SetParameters(RooArgSet& parameters)
00097 {
00098    delete fChain;
00099    delete fParameters;
00100    delete fDataEntry;
00101 
00102    fParameters = new RooArgSet();
00103    fParameters->addClone(parameters);
00104 
00105    // kbelasco: consider setting fDataEntry = fChain->get()
00106    // to see if that makes it possible to get values of variables without
00107    // doing string comparison
00108    RooRealVar nll(NLL_NAME, "-log Likelihood", 0);
00109    RooRealVar weight(WEIGHT_NAME, "weight", 0);
00110 
00111    fDataEntry = new RooArgSet();
00112    fDataEntry->addClone(parameters);
00113    fDataEntry->addClone(nll);
00114    fDataEntry->addClone(weight);
00115    fNLL = (RooRealVar*)fDataEntry->find(NLL_NAME);
00116    fWeight = (RooRealVar*)fDataEntry->find(WEIGHT_NAME);
00117 
00118    fChain = new RooDataSet(DATASET_NAME, "Markov Chain", *fDataEntry,WEIGHT_NAME);
00119 }
00120 
00121 void MarkovChain::Add(RooArgSet& entry, Double_t nllValue, Double_t weight)
00122 {
00123    if (fParameters == NULL)
00124       SetParameters(entry);
00125    RooStats::SetParameters(&entry, fDataEntry);
00126    fNLL->setVal(nllValue);
00127    //kbelasco: this is stupid, but some things might require it, so be doubly sure
00128    fWeight->setVal(weight);
00129    fChain->add(*fDataEntry, weight);
00130    //fChain->add(*fDataEntry);
00131 }
00132 
00133 void MarkovChain::AddFast(RooArgSet& entry, Double_t nllValue, Double_t weight)
00134 {
00135    RooStats::SetParameters(&entry, fDataEntry);
00136    fNLL->setVal(nllValue);
00137    //kbelasco: this is stupid, but some things might require it, so be doubly sure
00138    fWeight->setVal(weight);
00139    fChain->addFast(*fDataEntry, weight);
00140    //fChain->addFast(*fDataEntry);
00141 }
00142 
00143 RooDataSet* MarkovChain::GetAsDataSet(RooArgSet* whichVars) const
00144 {
00145    RooArgSet args;
00146    if (whichVars == NULL) {
00147       //args.add(*fParameters);
00148       //args.add(*fNLL);
00149       args.add(*fDataEntry);
00150    } else {
00151       args.add(*whichVars);
00152    }
00153 
00154    RooDataSet* data;
00155    //data = dynamic_cast<RooDataSet*>(fChain->reduce(args));
00156    data = (RooDataSet*)fChain->reduce(args);
00157 
00158    return data;
00159 }
00160 
00161 RooDataSet* MarkovChain::GetAsDataSet(const RooCmdArg& arg1, const RooCmdArg& arg2, 
00162                                       const RooCmdArg& arg3, const RooCmdArg& arg4, const RooCmdArg& arg5, 
00163                                       const RooCmdArg& arg6, const RooCmdArg& arg7, const RooCmdArg& arg8) const
00164 {
00165    RooDataSet* data;
00166    data = (RooDataSet*)fChain->reduce(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8);
00167    return data;
00168 }
00169 
00170 RooDataHist* MarkovChain::GetAsDataHist(RooArgSet* whichVars) const
00171 {
00172    RooArgSet args;
00173    if (whichVars == NULL) {
00174       args.add(*fParameters);
00175       //args.add(*fNLL);
00176       //args.add(*fDataEntry);
00177    } else {
00178       args.add(*whichVars);
00179    }
00180 
00181    RooDataSet* data = (RooDataSet*)fChain->reduce(args);
00182    RooDataHist* hist = data->binnedClone();
00183    delete data;
00184 
00185    return hist;
00186 }
00187 
00188 RooDataHist* MarkovChain::GetAsDataHist(const RooCmdArg& arg1, const RooCmdArg& arg2, 
00189                                         const RooCmdArg& arg3, const RooCmdArg& arg4, const RooCmdArg& arg5, 
00190                                         const RooCmdArg& arg6, const RooCmdArg& arg7, const RooCmdArg& arg8) const
00191 {
00192    RooDataSet* data;
00193    RooDataHist* hist;
00194    data = (RooDataSet*)fChain->reduce(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8);
00195    hist = data->binnedClone();
00196    delete data;
00197 
00198    return hist;
00199 }
00200 
00201 THnSparse* MarkovChain::GetAsSparseHist(RooAbsCollection* whichVars) const
00202 {
00203    RooArgList axes;
00204    if (whichVars == NULL)
00205       axes.add(*fParameters);
00206    else 
00207       axes.add(*whichVars);
00208 
00209    Int_t dim = axes.getSize();
00210    Double_t* min = new Double_t[dim];
00211    Double_t* max = new Double_t[dim];
00212    Int_t* bins = new Int_t[dim];
00213    TIterator* it = axes.createIterator();
00214    for (Int_t i = 0; i < dim; i++) {
00215       min[i] = ((RooRealVar*)it->Next())->getMin();
00216       max[i] = ((RooRealVar*)it->Next())->getMax();
00217       bins[i] = ((RooRealVar*)it->Next())->numBins();
00218    }
00219    THnSparseF* sparseHist = new THnSparseF("posterior", "MCMC Posterior Histogram",
00220          dim, bins, min, max);
00221    delete[] min;
00222    delete[] max;
00223    delete[] bins;
00224 
00225    // kbelasco: it appears we need to call Sumw2() just to get the
00226    // histogram to keep a running total of the weight so that Getsumw doesn't
00227    // just return 0
00228    sparseHist->Sumw2();
00229 
00230    // Fill histogram
00231    Int_t size = fChain->numEntries();
00232    const RooArgSet* entry;
00233    Double_t* x = new Double_t[dim];
00234    for (Int_t i = 0; i < size; i++) {
00235       entry = fChain->get(i);
00236       it->Reset();
00237       for (Int_t ii = 0; ii < dim; ii++)
00238          x[ii] = entry->getRealValue(it->Next()->GetName());
00239       sparseHist->Fill(x, fChain->weight());
00240    }
00241    delete[] x;
00242    delete it;
00243 
00244    return sparseHist;
00245 }
00246 
00247 Double_t MarkovChain::NLL(Int_t i) const
00248 {
00249    // kbelasco: how to do this?
00250    //fChain->get(i);
00251    //return fNLL->getVal();
00252    return fChain->get(i)->getRealValue(NLL_NAME);
00253 }
00254 
00255 Double_t MarkovChain::NLL() const
00256 {
00257    // kbelasco: how to do this?
00258    //fChain->get();
00259    //return fNLL->getVal();
00260    return fChain->get()->getRealValue(NLL_NAME);
00261 }
00262 
00263 Double_t MarkovChain::Weight() const
00264 {
00265    return fChain->weight();
00266 }
00267 
00268 Double_t MarkovChain::Weight(Int_t i) const
00269 {
00270    fChain->get(i);
00271    return fChain->weight();
00272 }

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