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
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
00041
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
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
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
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
00107
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 {
00120
00121 }
00122
00123
00124 BCRooInterface::BCRooInterface(const char* name) : BCModel(name)
00125 {
00126
00127 }
00128
00129
00130 BCRooInterface::~BCRooInterface()
00131 {
00132
00133 }
00134
00135
00136 void BCRooInterface::DefineParameters()
00137 {
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 {
00153
00154
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
00162 double logprob = -fNll->getVal();
00163 return logprob;
00164 }
00165
00166
00167 double BCRooInterface::LogAPrioriProbability(std::vector <double> parameters)
00168 {
00169
00170
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
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