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

BCRooInterface.cxx

Go to the documentation of this file.
00001 #include <RooGlobalFunc.h>
00002 #include <RooMsgService.h>
00003 #include <RooProdPdf.h>
00004 #include <RooRealVar.h>
00005 #include <RooWorkspace.h>
00006 #include <TFile.h>
00007 
00008 #include <BAT/BCMath.h>
00009 #include <iostream>
00010 
00011 #include "BCRooInterface.h"
00012 
00013 // ---------------------------------------------------------
00014 void BCRooInterface::Initialize( RooAbsData& data,
00015              RooAbsPdf& model,
00016              RooAbsPdf& prior_trans,
00017              const RooArgSet* params,
00018              const RooArgSet& listPOI )
00019 {
00020 
00021   fData = &data;
00022   fModel = &model;
00023 
00024   // make the product of both priors to get the full prior probability function
00025   RooAbsPdf* prior_total = &prior_trans;
00026   if (prior_total!=0 ) {
00027     fPrior = prior_total;
00028   }
00029   else {
00030       std::cout << "No prior PDF: the program will crash\n";
00031   }
00032 
00033   std::cout << "Imported parameters:\n";
00034   fParams  = new RooArgList(listPOI);
00035   const RooArgSet* paramsTmp = params;
00036   if (paramsTmp!=0)
00037     fParams->add(*paramsTmp);
00038   fParams->Print("v");
00039 
00040   // create the log-likelihood function
00041 //   fNll = new RooNLLVar("fNll","",*fModel,*fData,true/*extended*/);
00042 
00043   RooArgSet* constrainedParams = fModel->getParameters(*fData);
00044   fNll = fModel->createNLL(*fData, RooFit::Constrain(*constrainedParams) );
00045 
00046   DefineParameters();
00047 }
00048 
00049 // ---------------------------------------------------------
00050 
00051 void BCRooInterface::Initialize( const char* rootFile,
00052              const char* wsName,
00053              const char* dataName,
00054              const char* modelName,
00055              const char* priorName,
00056              const char* priorNuisanceName,
00057              const char* paramsName,
00058              const char* listPOIName )
00059 {
00060   // retrieve the RooFit inputs from the ROOT file
00061 
00062   /*
00063   // hard coded names in the workspace
00064   char* rootFile = "bat_workspace.root";
00065   char* wsName= "batWS";
00066   char* dataName= "data";
00067   char* modelName= "model";
00068   char* priorName= "priorPOI";
00069   char* priorNuisanceName= "priorNuisance";
00070   char* paramsName= "parameters";
00071   char* listPOIName= "POI";
00072   */
00073 
00074    std::cout << "Opening " << rootFile << std::endl;
00075    TFile* file = new TFile(rootFile);
00076    std::cout << "content :\n";
00077    file->ls();
00078 
00079    RooWorkspace* bat_ws = (RooWorkspace*) file->Get(wsName);
00080    bat_ws->Print("v");
00081 
00082    fData = (RooAbsData*) bat_ws->data(dataName);
00083    fModel = (RooAbsPdf*) bat_ws->function(modelName);
00084 
00085    // make the product of both priors to get the full prior probability function
00086    RooAbsPdf* priorPOI = (RooAbsPdf*) bat_ws->function(priorName);
00087    RooAbsPdf* priorNuisance = (RooAbsPdf*) bat_ws->pdf(priorNuisanceName);
00088    if (priorNuisance!=0 && priorPOI!=0) {
00089       fPrior = new RooProdPdf("fPrior","complete prior",*priorPOI,*priorNuisance);
00090    }
00091    else {
00092       if ( priorNuisance!=0 )
00093          fPrior=priorNuisance;
00094       else if ( priorPOI!=0 )
00095          fPrior = priorPOI;
00096       else
00097          std::cout << "No prior PDF: the program will crash\n";
00098    }
00099 
00100    std::cout << "Imported parameters:\n";
00101    fParams  = new RooArgList(*(bat_ws->set(listPOIName)));
00102    RooArgSet* paramsTmp = (RooArgSet*) bat_ws->set(paramsName);
00103    if (paramsTmp!=0) fParams->add(*paramsTmp);
00104    fParams->Print("v");
00105 
00106    // create the log-likelihood function
00107    //fNll = new RooNLLVar("fNll","",*fModel,*fData,true/*extended*/);
00108    RooArgSet* constrainedParams = fModel->getParameters(*fData);
00109    fNll = fModel->createNLL(*fData, RooFit::Constrain(*constrainedParams) );
00110 
00111    file->Close();
00112 
00113    DefineParameters();
00114 }
00115 
00116 
00117 // ---------------------------------------------------------
00118 BCRooInterface::BCRooInterface() : BCModel()
00119 {  // default constructor
00120 
00121 }
00122 
00123 // ---------------------------------------------------------
00124 BCRooInterface::BCRooInterface(const char* name) : BCModel(name)
00125 {  // another constructor
00126 
00127 }
00128 
00129 // ---------------------------------------------------------
00130 BCRooInterface::~BCRooInterface()
00131 {  // default destructor
00132 
00133 }
00134 
00135 // ---------------------------------------------------------
00136 void BCRooInterface::DefineParameters()
00137 {  // define for BAT the list of parameters, range and plot binning
00138 
00139   int default_nbins = 500;
00140 
00141   int nParams = fParams->getSize();
00142   for (int iParam=0; iParam<nParams; iParam++) {
00143     RooRealVar* ipar = (RooRealVar*) fParams->at(iParam);
00144     this->AddParameter(ipar->GetName(),ipar->getMin(),ipar->getMax());
00145     this->SetNbins(ipar->GetName(),default_nbins);
00146     std::cout << "added parameter: " << ipar->GetName() << " defined in range [ " << ipar->getMin() << " - " << ipar->getMax() << " ]\n";
00147   }
00148 }
00149 
00150 // ---------------------------------------------------------
00151 double BCRooInterface::LogLikelihood(std::vector <double> parameters)
00152 {  // this methods returns the logarithm of the conditional probability: p(data|parameters)
00153 
00154    // retrieve the values of the parameters to be tested
00155   int nParams = fParams->getSize();
00156   for (int iParam=0; iParam<nParams; iParam++) {
00157     RooRealVar* ipar = (RooRealVar*) fParams->at(iParam);
00158     ipar->setVal(parameters.at(iParam));
00159   }
00160 
00161   // compute the log of the likelihood function
00162   double logprob = -fNll->getVal();
00163   return logprob;
00164 }
00165 
00166 // ---------------------------------------------------------
00167 double BCRooInterface::LogAPrioriProbability(std::vector <double> parameters)
00168 {  // this method returs the logarithm of the prior probability for the parameters: p(parameters).
00169 
00170    // retrieve the values of the parameters to be tested
00171   int nParams = fParams->getSize();
00172   for (int iParam=0; iParam<nParams; iParam++) {
00173     RooRealVar* ipar = (RooRealVar*) fParams->at(iParam);
00174     ipar->setVal(parameters.at(iParam));
00175   }
00176 
00177   // compute the log of the prior function
00178   RooArgSet* tmpArgSet = new RooArgSet(*fParams);
00179   double prob = fPrior->getVal(tmpArgSet);
00180   delete tmpArgSet;
00181   if (prob<1e-300)
00182     prob = 1e-300;
00183   return log(prob);
00184 }
00185 

Generated on Mon Aug 30 2010 22:14:54 for Bayesian Analysis Toolkit by  doxygen 1.7.1