1 #include "BCRooInterface.h" 2 #include "../../BAT/BCMath.h" 3 #include "../../BAT/BCParameter.h" 5 #include <RooGlobalFunc.h> 6 #include <RooMsgService.h> 7 #include <RooProdPdf.h> 8 #include <RooUniform.h> 9 #include <RooWorkspace.h> 13 #include "RooRealVar.h" 14 #include "RooAbsReal.h" 15 #include "RooRandom.h" 20 void BCRooInterface::Initialize( RooAbsData& data,
22 RooAbsPdf& prior_trans,
23 const RooArgSet* params,
24 const RooArgSet& listPOI )
31 RooAbsPdf* prior_total = &prior_trans;
32 if (prior_total != 0 ) {
35 std::cout <<
"No prior PDF: without taking action the program would crash\n";
36 std::cout <<
"No prior PDF: adding dummy uniform pdf on the interval [0..1]\n";
37 priorhelpvar =
new RooRealVar(
"priorhelpvar",
"", 0.0, 1.0 );
38 _addeddummyprior =
true;
39 RooUniform* priorhelpdist =
new RooUniform(
"priorhelpdist",
"", *priorhelpvar);
40 fPrior = priorhelpdist;
43 std::cout <<
"Imported parameters:\n";
44 fParams =
new RooArgList(listPOI);
45 const RooArgSet* paramsTmp = params;
47 fParams->add(*paramsTmp);
50 fParamsPOI =
new RooArgList(listPOI);
54 RooArgSet* constrainedParams = fModel->getParameters(*fData);
55 fNll = fModel->createNLL(*fData, RooFit::Constrain(*constrainedParams) );
66 void BCRooInterface::Initialize(
const char* rootFile,
69 const char* modelName,
70 const char* priorName,
71 const char* priorNuisanceName,
72 const char* paramsName,
73 const char* listPOIName )
89 std::cout <<
"Opening " << rootFile << std::endl;
90 TFile* file = TFile::Open(rootFile);
92 throw std::runtime_error(std::string(
"Could not open ") + rootFile);
93 std::cout <<
"content :\n";
96 RooWorkspace* bat_ws = (RooWorkspace*) file->Get(wsName);
99 fData = (RooAbsData*) bat_ws->data(dataName);
100 fModel = (RooAbsPdf*) bat_ws->function(modelName);
103 RooAbsPdf* priorPOI = (RooAbsPdf*) bat_ws->function(priorName);
104 RooAbsPdf* priorNuisance = (RooAbsPdf*) bat_ws->pdf(priorNuisanceName);
105 if (priorNuisance != 0 && priorPOI != 0) {
106 fPrior =
new RooProdPdf(
"fPrior",
"complete prior", *priorPOI, *priorNuisance);
108 if ( priorNuisance != 0 )
109 fPrior = priorNuisance;
110 else if ( priorPOI != 0 )
113 std::cout <<
"No prior PDF: without taking action the program would crash\n";
114 std::cout <<
"No prior PDF: adding dummy uniform pdf on the interval [0..1]\n";
115 priorhelpvar =
new RooRealVar(
"priorhelpvar",
"", 0.0, 1.0 );
116 _addeddummyprior =
true;
117 RooUniform* priorhelpdist =
new RooUniform(
"priorhelpdist",
"", *priorhelpvar);
118 fPrior = priorhelpdist;
122 std::cout <<
"Imported parameters:\n";
123 fParams =
new RooArgList(*(bat_ws->set(listPOIName)));
124 RooArgSet* paramsTmp = (RooArgSet*) bat_ws->set(paramsName);
125 if (paramsTmp != 0) {
126 fParams->add(*paramsTmp);
128 if (_addeddummyprior ==
true ) {
129 fParams->add(*priorhelpvar);
135 RooArgSet* constrainedParams = fModel->getParameters(*fData);
136 fNll = fModel->createNLL(*fData, RooFit::Constrain(*constrainedParams) );
144 BCRooInterface::BCRooInterface(
const std::string& name,
bool fillChain) :
154 _addeddummyprior(false),
155 _fillChain(fillChain),
156 fFirstComparison(false),
157 _roostatsMarkovChain(NULL)
164 BCRooInterface::~BCRooInterface()
168 delete _roostatsMarkovChain;
173 void BCRooInterface::DefineParameters()
177 for (
unsigned iParam = 0; iParam < unsigned(fParams->getSize()); ++iParam) {
178 RooRealVar* ipar = (RooRealVar*) fParams->at(iParam);
179 this->
AddParameter(ipar->GetName(), ipar->getMin(), ipar->getMax());
181 p.SetNbins(_default_nbins);
183 std::cout <<
"added parameter: " << p.GetName() <<
" defined in range [ " << p.GetLowerLimit() <<
" - " 184 << p.GetUpperLimit() <<
" ] with number of bins: " << p.GetNbins() <<
" \n";
187 for (std::list< std::pair<const char*, int> >::iterator listiter = _nbins_list.begin(); listiter != _nbins_list.end(); ++listiter) {
188 GetParameter((*listiter).first).SetNbins((*listiter).second);
189 std::cout <<
"adjusted parameter: " << (*listiter).first <<
" to number of bins: " << (*listiter).second <<
" \n";
199 int nParams = fParams->getSize();
200 for (
int iParam = 0; iParam < nParams; iParam++) {
201 RooRealVar* ipar = (RooRealVar*) fParams->at(iParam);
202 ipar->setVal(parameters.at(iParam));
206 double logprob = -fNll->getVal();
215 int nParams = fParams->getSize();
216 for (
int iParam = 0; iParam < nParams; iParam++) {
217 RooRealVar* ipar = (RooRealVar*) fParams->at(iParam);
218 ipar->setVal(parameters.at(iParam));
221 RooArgSet* tmpArgSet =
new RooArgSet(*fParams);
222 double prob = fPrior->getVal(tmpArgSet);
231 for (std::list< std::pair<const char*, int> >::iterator listiter = _nbins_list.begin(); listiter != _nbins_list.end(); listiter++) {
232 if (!strcmp((*listiter).first, parname)) {
233 (*listiter).second = nbins;
237 _nbins_list.push_back( std::make_pair(parname, nbins) );
242 _default_nbins = nbins;
255 _parametersForMarkovChainPrevious.add(*fParams);
256 _parametersForMarkovChainCurrent.add(*fParams);
258 std::cout <<
"size of _parametersForMarkovChain: " << _parametersForMarkovChainCurrent.getSize() << std::endl;
259 std::cout <<
"size of fParamsPOI: " << fParamsPOI->getSize() << std::endl;
262 _roostatsMarkovChain =
new RooStats::MarkovChain();
267 std::cout <<
"setting up parameters for RooStats markov chain" << std::endl;
268 _parametersForMarkovChainPrevious.writeToStream(std::cout,
false);
272 for (
int countChains = 1; countChains <= nchains ; countChains++ ) {
273 double tempweight = 1.0;
274 fVecWeights.push_back(tempweight);
275 std::vector<double> tempvec;
276 TIterator* setiter = fParamsPOI->createIterator();
278 while (setiter->Next()) {
279 tempvec.push_back(tempval);
281 fPreviousStep.push_back(tempvec);
282 fCurrentStep.push_back(tempvec);
285 fFirstComparison =
true;
307 for (
int i = 0; i < nchains; ++i) {
312 TIterator* setiter = fParams->createIterator();
319 while (setiter->Next()) {
326 const char* paramnamepointer = (tempBCparam.
GetName()).c_str();
327 double xij =
Getx(i, j);
328 AddToCurrentChainElement(xij, i, j);
329 RooRealVar* parampointer = (RooRealVar*) & (_parametersForMarkovChainCurrent[paramnamepointer]);
330 parampointer->setVal(xij);
344 if ( !(EqualsLastChainElement(i)) ) {
345 double weight = GetWeightForChain(i);
346 _roostatsMarkovChain->Add(_parametersForMarkovChainPrevious, -1.*
GetLogProbx(j), weight);
347 _parametersForMarkovChainPrevious = _parametersForMarkovChainCurrent;
353 void BCRooInterface::AddToCurrentChainElement(
double xij,
int chainNum,
int poiNum)
355 fCurrentStep[chainNum][poiNum] = xij;
358 bool BCRooInterface::EqualsLastChainElement(
int chainNum)
361 std::vector<double>::iterator itPrevious = fPreviousStep[chainNum].begin();
363 if (fFirstComparison ==
true) {
364 fFirstComparison =
false;
365 _parametersForMarkovChainPrevious = _parametersForMarkovChainCurrent;
370 for (std::vector<double>::iterator itCurrent = fCurrentStep[chainNum].begin(); itCurrent != fCurrentStep[chainNum].end(); ++itCurrent) {
371 if (*itCurrent != *itPrevious) {
373 fPreviousStep[chainNum] = fCurrentStep[chainNum];
379 if (equals ==
true) {
380 fVecWeights[chainNum] += 1.0;
387 double BCRooInterface::GetWeightForChain(
int chainNum)
389 double retval = fVecWeights[chainNum];
390 fVecWeights[chainNum] = 1.0 ;
void SetupRooStatsMarkovChain()
setup RooStats Markov Chain
void SetMarginalizationMethod(BCIntegrate::BCMarginalizationMethod method)
unsigned GetNChains() const
virtual bool AddParameter(const std::string &name, double min, double max, const std::string &latexname="", const std::string &unitstring="")
void MCMCIterationInterface()
overloaded function from BCIntegrate to fill RooStats Markov Chain with every accepted step ...
BCParameter & GetParameter(unsigned index)
void SetNumBins(const char *parname, int nbins)
set the number of histogram bins for a specific parameter
The base class for all user-defined models.
A class representing a parameter of a model.
double LogLikelihood(const std::vector< double > ¶meters)
Calculates natural logarithm of the likelihood.
double LogAPrioriProbability(const std::vector< double > ¶meters)
Returns natural logarithm of the prior probability.
const std::vector< double > & Getx(unsigned c) const
double GetLogProbx(unsigned c) const
virtual const std::string & GetName() const