Go to the documentation of this file.00001 #include <RooGlobalFunc.h>
00002 #include <RooMsgService.h>
00003 #include <RooProdPdf.h>
00004 #include <RooWorkspace.h>
00005 #include <TFile.h>
00006
00007 #include "BCMath.h"
00008 #include <iostream>
00009
00010 #include "BCRooInterface.h"
00011
00012 #include <RooUniform.h>
00013
00014
00015 #include "RooRealVar.h"
00016 #include "RooAbsReal.h"
00017 #include "RooRandom.h"
00018
00019
00020
00021
00022
00023 void BCRooInterface::Initialize( RooAbsData& data,
00024 RooAbsPdf& model,
00025 RooAbsPdf& prior_trans,
00026 const RooArgSet* params,
00027 const RooArgSet& listPOI )
00028 {
00029
00030 fData = &data;
00031 fModel = &model;
00032
00033
00034 RooAbsPdf* prior_total = &prior_trans;
00035 if (prior_total!=0 ) {
00036 fPrior = prior_total;
00037 }
00038 else {
00039 std::cout << "No prior PDF: without taking action the program would crash\n";
00040 std::cout << "No prior PDF: adding dummy uniform pdf on the interval [0..1]\n";
00041 priorhelpvar = new RooRealVar("priorhelpvar","",0.0, 1.0 );
00042 _addeddummyprior = true;
00043 RooUniform* priorhelpdist = new RooUniform("priorhelpdist","", *priorhelpvar);
00044 fPrior = priorhelpdist;
00045 }
00046
00047 std::cout << "Imported parameters:\n";
00048 fParams = new RooArgList(listPOI);
00049 const RooArgSet* paramsTmp = params;
00050 if (paramsTmp!=0)
00051 fParams->add(*paramsTmp);
00052 fParams->Print("v");
00053
00054 fParamsPOI = new RooArgList(listPOI);
00055
00056
00057
00058 RooArgSet* constrainedParams = fModel->getParameters(*fData);
00059 fNll = fModel->createNLL(*fData, RooFit::Constrain(*constrainedParams) );
00060
00061 DefineParameters();
00062
00063 if(_fillChain){
00064 SetupRooStatsMarkovChain();
00065 }
00066 }
00067
00068
00069
00070 void BCRooInterface::Initialize( const char* rootFile,
00071 const char* wsName,
00072 const char* dataName,
00073 const char* modelName,
00074 const char* priorName,
00075 const char* priorNuisanceName,
00076 const char* paramsName,
00077 const char* listPOIName )
00078 {
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 std::cout << "Opening " << rootFile << std::endl;
00094 TFile* file = new TFile(rootFile);
00095 std::cout << "content :\n";
00096 file->ls();
00097
00098 RooWorkspace* bat_ws = (RooWorkspace*) file->Get(wsName);
00099 bat_ws->Print("v");
00100
00101 fData = (RooAbsData*) bat_ws->data(dataName);
00102 fModel = (RooAbsPdf*) bat_ws->function(modelName);
00103
00104
00105 RooAbsPdf* priorPOI = (RooAbsPdf*) bat_ws->function(priorName);
00106 RooAbsPdf* priorNuisance = (RooAbsPdf*) bat_ws->pdf(priorNuisanceName);
00107 if (priorNuisance!=0 && priorPOI!=0) {
00108 fPrior = new RooProdPdf("fPrior","complete prior",*priorPOI,*priorNuisance);
00109 }
00110 else {
00111 if ( priorNuisance!=0 )
00112 fPrior=priorNuisance;
00113 else if ( priorPOI!=0 )
00114 fPrior = priorPOI;
00115 else
00116 std::cout << "No prior PDF: without taking action the program would crash\n";
00117 std::cout << "No prior PDF: adding dummy uniform pdf on the interval [0..1]\n";
00118 priorhelpvar = new RooRealVar("priorhelpvar","",0.0, 1.0 );
00119 _addeddummyprior = true;
00120 RooUniform* priorhelpdist = new RooUniform("priorhelpdist","", *priorhelpvar);
00121 fPrior = priorhelpdist;
00122 }
00123
00124 std::cout << "Imported parameters:\n";
00125 fParams = new RooArgList(*(bat_ws->set(listPOIName)));
00126 RooArgSet* paramsTmp = (RooArgSet*) bat_ws->set(paramsName);
00127 if (paramsTmp!=0) {
00128 fParams->add(*paramsTmp);
00129 }
00130 if (_addeddummyprior == true ){
00131 fParams->add(*priorhelpvar);
00132 }
00133 fParams->Print("v");
00134
00135
00136
00137 RooArgSet* constrainedParams = fModel->getParameters(*fData);
00138 fNll = fModel->createNLL(*fData, RooFit::Constrain(*constrainedParams) );
00139
00140 file->Close();
00141
00142 DefineParameters();
00143 }
00144
00145
00146
00147 BCRooInterface::BCRooInterface() : BCModel()
00148 {
00149 _default_nbins = 500;
00150 _fillChain = false;
00151 _addeddummyprior = false;
00152 }
00153
00154
00155 BCRooInterface::BCRooInterface(const char* name, bool fillChain) : BCModel(name)
00156 {
00157 _default_nbins = 500;
00158 _fillChain = fillChain;
00159 }
00160
00161
00162 BCRooInterface::~BCRooInterface()
00163 {
00164 if(_fillChain){
00165 delete _roostatsMarkovChain;
00166 }
00167
00168
00169
00170 }
00171
00172
00173 void BCRooInterface::DefineParameters()
00174 {
00175
00176 int nParams = fParams->getSize();
00177 for (int iParam=0; iParam<nParams; iParam++) {
00178 RooRealVar* ipar = (RooRealVar*) fParams->at(iParam);
00179 this->AddParameter(ipar->GetName(),ipar->getMin(),ipar->getMax());
00180 this->SetNbins(ipar->GetName(),_default_nbins);
00181 std::cout << "added parameter: " << ipar->GetName() << " defined in range [ " << ipar->getMin() << " - " << ipar->getMax() << " ] with number of bins: " << _default_nbins << " \n";
00182 }
00183
00184 for(list< pair<const char*,int> >::iterator listiter = _nbins_list.begin(); listiter != _nbins_list.end(); listiter++){
00185 this->SetNbins((*listiter).first,(*listiter).second);
00186 std::cout << "adjusted parameter: " << (*listiter).first << " to number of bins: " << (*listiter).second << " \n";
00187 }
00188
00189 }
00190
00191
00192 double BCRooInterface::LogLikelihood(std::vector <double> parameters)
00193 {
00194
00195
00196 int nParams = fParams->getSize();
00197 for (int iParam=0; iParam<nParams; iParam++) {
00198 RooRealVar* ipar = (RooRealVar*) fParams->at(iParam);
00199 ipar->setVal(parameters.at(iParam));
00200 }
00201
00202
00203 double logprob = -fNll->getVal();
00204 return logprob;
00205 }
00206
00207
00208 double BCRooInterface::LogAPrioriProbability(std::vector <double> parameters)
00209 {
00210
00211 int nParams = fParams->getSize();
00212 for (int iParam=0; iParam<nParams; iParam++) {
00213 RooRealVar* ipar = (RooRealVar*) fParams->at(iParam);
00214 ipar->setVal(parameters.at(iParam));
00215 }
00216
00217 RooArgSet* tmpArgSet = new RooArgSet(*fParams);
00218 double prob = fPrior->getVal(tmpArgSet);
00219 delete tmpArgSet;
00220 if (prob<1e-300)
00221 prob = 1e-300;
00222 return log(prob);
00223 }
00224
00225 void BCRooInterface::SetNumBins(const char * parname, int nbins)
00226 {
00227 for(list< pair<const char*,int> >::iterator listiter = _nbins_list.begin(); listiter != _nbins_list.end(); listiter++){
00228 if(!strcmp((*listiter).first, parname)){
00229 (*listiter).second = nbins;
00230 return;
00231 }
00232 }
00233 _nbins_list.push_back( make_pair(parname,nbins) );
00234 }
00235
00236 void BCRooInterface::SetNumBins(int nbins)
00237 {
00238 _default_nbins = nbins;
00239 }
00240
00241 void BCRooInterface::SetupRooStatsMarkovChain()
00242 {
00243
00244
00245
00246 _parametersForMarkovChainPrevious.add(*fParamsPOI);
00247 _parametersForMarkovChainCurrent.add(*fParamsPOI);
00248
00249 cout << "size of _parametersForMarkovChain: " << _parametersForMarkovChainCurrent.getSize() << endl;
00250 cout << "size of fParamsPOI: " << fParamsPOI->getSize() << endl;
00251
00252
00253 _roostatsMarkovChain = new RooStats::MarkovChain();
00254
00255
00256
00257
00258 cout << "setting up parameters for RooStats markov chain" << endl;
00259 _parametersForMarkovChainPrevious.writeToStream(cout, false);
00260
00261
00262 int nchains = MCMCGetNChains();
00263 for(int countChains = 1; countChains<=nchains ; countChains++ ){
00264 double tempweight = 1.0;
00265 fVecWeights.push_back(tempweight);
00266 vector<double> tempvec;
00267 TIterator* setiter = fParamsPOI->createIterator();
00268 double tempval = 0.;
00269 while(setiter->Next()){
00270 tempvec.push_back(tempval);
00271 }
00272 fPreviousStep.push_back(tempvec);
00273 fCurrentStep.push_back(tempvec);
00274 }
00275
00276 fFirstComparison = true;
00277
00278
00279
00280
00281
00282
00283
00284
00285 }
00286
00287
00288 void BCRooInterface::MCMCIterationInterface()
00289 {
00290
00291
00292 if(_fillChain){
00293
00294
00295 int nchains = MCMCGetNChains();
00296
00297
00298 int npar = GetNParameters();
00299
00300
00301
00302 for (int i = 0; i < nchains; ++i) {
00303
00304
00305
00306 TIterator* setiter = fParamsPOI->createIterator();
00307 int j = 0;
00308
00309 while(setiter->Next()){
00310
00311
00312 BCParameter * tempBCparam = GetParameter(j);
00313
00314
00315 const char * paramnamepointer = (tempBCparam->GetName()).c_str();
00316
00317 double xij = fMCMCx.at(i * npar + j);
00318 AddToCurrentChainElement(xij, i, j);
00319 RooRealVar* parampointer = (RooRealVar*) &(_parametersForMarkovChainCurrent[paramnamepointer]);
00320 parampointer->setVal(xij);
00321 j++;
00322 }
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332 if( !(EqualsLastChainElement(i)) ){
00333 double weight = GetWeightForChain(i);
00334 _roostatsMarkovChain->Add(_parametersForMarkovChainPrevious, -1.* MCMCGetLogProbx(j), weight);
00335 _parametersForMarkovChainPrevious = _parametersForMarkovChainCurrent;
00336 }
00337 }
00338 }
00339 }
00340
00341 void BCRooInterface::AddToCurrentChainElement(double xij, int chainNum, int poiNum)
00342 {
00343 fCurrentStep[chainNum][poiNum] = xij;
00344 }
00345
00346 bool BCRooInterface::EqualsLastChainElement(int chainNum)
00347 {
00348 bool equals = true;
00349 vector<double>::iterator itPrevious = fPreviousStep[chainNum].begin();
00350
00351 if(fFirstComparison == true){
00352 fFirstComparison = false;
00353 _parametersForMarkovChainPrevious = _parametersForMarkovChainCurrent;
00354 return true;
00355 }
00356
00357
00358 for (vector<double>::iterator itCurrent = fCurrentStep[chainNum].begin(); itCurrent != fCurrentStep[chainNum].end(); ++itCurrent) {
00359 if(*itCurrent != *itPrevious){
00360 equals = false;
00361 fPreviousStep[chainNum] = fCurrentStep[chainNum];
00362 break;
00363 }
00364 ++itPrevious;
00365 }
00366
00367 if(equals == true){
00368 fVecWeights[chainNum] += 1.0;
00369 }
00370
00371 return equals;
00372
00373 }
00374
00375 double BCRooInterface::GetWeightForChain(int chainNum)
00376 {
00377 double retval = fVecWeights[chainNum];
00378 fVecWeights[chainNum]= 1.0 ;
00379 return retval;
00380 }
00381