BAT  0.9.4
The Bayesian analysis toolkit
 All Classes Namespaces Functions Variables Enumerations
BATCalculator.cxx
1 // Author: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke, Stefan A. Schmitz
2 
3 #include <TAxis.h>
4 #include <TFile.h>
5 #include <TCanvas.h>
6 #include <TVector.h>
7 
8 #include <RooAbsFunc.h>
9 //#include <RooAbsReal.h>
10 #include <RooRealVar.h>
11 //#include <RooArgSet.h>
12 #include <RooBrentRootFinder.h>
13 #include <RooFormulaVar.h>
14 #include <RooGenericPdf.h>
15 //#include <RooPlot.h>
16 #include <RooProdPdf.h>
17 #include <RooDataHist.h>
18 //#include <RooAbsPdf.h>
19 #include <RooHistPdf.h>
20 
21 #include <RooStats/MarkovChain.h>
22 
23 #include "../../BAT/BCLog.h"
24 #include "../../BAT/BCAux.h"
25 #include "../../BAT/BCH1D.h"
26 
27 #include "BCRooInterface.h"
28 #include "BATCalculator.h"
29 
30 #include <algorithm>
31 #include <cassert>
32 
33 
35 
36 namespace RooStats {
37 
38 // ---------------------------------------------------------
39 BATCalculator::BATCalculator()
40  : fData(0)
41  , fPdf(0)
42  , fPrior(0)
43  , fparams(0)
44  , _posteriorTH1D(0)
45  , fProductPdf(0)
46  , fLogLike(0)
47  , fLikelihood(0)
48  , fIntegratedLikelihood(0)
49  , fPosteriorPdf(0)
50  , fLower(0)
51  , fUpper(0)
52  , fBrfPrecision(0.00005)
53  , fValidInterval(false)
54  , fValidMCMCInterval(false)
55  , fConnectedInterval(false)
56  , _nMCMC(0)
57  , fSize(0.05)
58  , fLeftSideFraction(0.5)
59 {
60  // default constructor
61  _myRooInterface = new BCRooInterface();
62 }
63 
64 // ---------------------------------------------------------
65 BATCalculator::BATCalculator( /* const char * name, const char * title, */
66  RooAbsData & data,
67  RooAbsPdf & pdf,
68  RooArgSet & POI,
69  RooAbsPdf & prior,
70  RooArgSet * params,
71  bool fillChain)
72  : fData(&data)
73  , fPdf(&pdf)
74  , fPOI(POI)
75  , fPrior(&prior)
76  , fparams(params)
77  , _posteriorTH1D(0)
78  , fProductPdf(0)
79  , fLogLike(0)
80  , fLikelihood(0)
81  , fIntegratedLikelihood(0)
82  , fPosteriorPdf(0)
83  , fLower(0)
84  , fUpper(0)
85  , fBrfPrecision(0.00005)
86  , fValidInterval(false)
87  , fValidMCMCInterval(false)
88  , fConnectedInterval(false)
89  , _nMCMC(1000000)
90  , fSize(0.05)
91  , fLeftSideFraction(0.5)
92  //, TNamed( TString(name), TString(title) )
93 {
94  // constructor
95  //if (nuisanceParameters)
96  // fNuisanceParameters.add(*nuisanceParameters);
97  _myRooInterface = new BCRooInterface("BCRooInterfaceForBAT",fillChain);
98 }
99 
100 // ---------------------------------------------------------
101 BATCalculator::BATCalculator( RooAbsData & data, ModelConfig & model, bool fillChain)
102  : fData(&data)
103  , fPdf(model.GetPdf())
104  , fPOI(*model.GetParametersOfInterest())
105  , fPrior(model.GetPriorPdf())
106  , fparams(model.GetNuisanceParameters())
107  , _posteriorTH1D(0)
108  , fProductPdf(0)
109  , fLogLike(0)
110  , fLikelihood(0)
111  , fIntegratedLikelihood(0)
112  , fPosteriorPdf(0)
113  , fLower(0)
114  , fUpper(0)
115  , fBrfPrecision(0.00005)
116  , fValidInterval(false)
117  , fValidMCMCInterval(false)
118  , fConnectedInterval(false)
119  , _nMCMC(1000000)
120  , fSize(0.05)
121  , fLeftSideFraction(0.5)
122 {
123  std::cout << "BATCalculator calling constructor ..." << std::endl;
124  // constructor from Model Config
125  // SetModel(model);
126  _myRooInterface = new BCRooInterface("BCRooInterfaceForBAT",fillChain);
127  std::cout << "BATCalculator constructed" << std::endl;
128 }
129 
130 // ---------------------------------------------------------
131 BATCalculator::~BATCalculator()
132 {
133  std::cout << "BATCalculator calling destructor ..." << std::endl;
134  // destructor
135  ClearAll();
136  delete _myRooInterface;
137 }
138 
139 // ---------------------------------------------------------
140 void BATCalculator::ClearAll() const
141 {
142  // clear cached pdf objects
143  if (fProductPdf)
144  delete fProductPdf;
145  if (fLogLike)
146  delete fLogLike;
147  if (fLikelihood)
148  delete fLikelihood;
149  if (fIntegratedLikelihood)
150  delete fIntegratedLikelihood;
151  if (fPosteriorPdf)
152  delete fPosteriorPdf;
153  fPosteriorPdf = 0;
154  fProductPdf = 0;
155  fLogLike = 0;
156  fLikelihood = 0;
157  fIntegratedLikelihood = 0;
158  fLower = 0;
159  fUpper = 0;
160  fValidInterval = false;
161  fValidMCMCInterval = false;
162 }
163 
164 // ---------------------------------------------------------
165 void BATCalculator::SetModel(const ModelConfig & /* model */)
166 {
167  // set the model
168  //fPdf = model.GetPdf();
169  //fPriorPOI = model.GetPriorPdf();
170  // assignment operator = does not do a real copy the sets (must use add method)
171  //fPOI.removeAll();
172  //*fparams.removeAll();
173  //if (model.GetParametersOfInterest())
174  // fPOI.add( *(model.GetParametersOfInterest()) );
175  //if (model.GetNuisanceParameters())
176  // fNuisanceParameters.add( *(model.GetNuisanceParameters() ) );
177 
178  // invalidate the cached pointers
179  //ClearAll();
180 }
181 
182 // ---------------------------------------------------------
183 RooArgSet * BATCalculator::GetMode(RooArgSet * /* parameters */) const
184 {
187  // Should cover multi-dimensional cases...
188  // How do we do if there are points that are equi-probable?
189 
190  return 0;
191 }
192 
193 // ---------------------------------------------------------
194 //returns posterior with respect to first entry in the POI ArgSet
195 RooAbsPdf * BATCalculator::GetPosteriorPdf1D() const
196 {
197  const char * POIname = fPOI.first()->GetName();
198  return GetPosteriorPdf1D(POIname);
199 }
200 
201 // ---------------------------------------------------------
202 // returns posterior with respect to provided name in the POI ArgSet
203 RooAbsPdf * BATCalculator::GetPosteriorPdf1D(const char * POIname) const
204 {
205 
206  // run some sanity checks
207  if (!fPdf ) {
208  std::cerr << "BATCalculator::GetPosteriorPdf - missing pdf model" << std::endl;
209  return 0;
210  }
211 
212  if (!fPrior) {
213  std::cerr << "BATCalculator::GetPosteriorPdf - missing prior pdf" << std::endl;
214  }
215 
216  if (fPOI.getSize() == 0) {
217  std::cerr << "BATCalculator::GetPosteriorPdf - missing parameter of interest" << std::endl;
218  return 0;
219  }
220 
221  if (fPOI.getSize() > 1) {
222  std::cerr << "BATCalculator::GetPosteriorPdf - current implementation works only on 1D intervals" << std::endl;
223  return 0;
224  }
225 
226  //initialize RooInterface object
227  _myRooInterface->Initialize(*fData,*fPdf,*fPrior,fparams,fPOI);
228  _myRooInterface->MCMCSetNIterationsRun(_nMCMC);
229  _myRooInterface->MarginalizeAll();
230  _myRooInterface->FindMode();
231  const BCParameter * myPOI = _myRooInterface->GetParameter(POIname);
232  BCH1D * myPosterior =_myRooInterface->GetMarginalized(myPOI);
233  TH1D * posteriorTH1D = myPosterior->GetHistogram();
234  _posteriorTH1D = static_cast<TH1D *>(posteriorTH1D->Clone("_posteriorTH1D"));
235  RooDataHist * posteriorRooDataHist = new RooDataHist("posteriorhist","", fPOI,posteriorTH1D);
236  fPosteriorPdf = new RooHistPdf("posteriorPdf","",fPOI,*posteriorRooDataHist);
237 
238  /*
239  // plots for debugging
240  TFile * debugfile = TFile::Open( "debug_posterior.root" ,"RECREATE");
241  if ( debugfile->IsOpen() )
242  std::cout << "File debug_posterior opened successfully" << std::endl;
243  debugfile->cd();
244  //posteriorTH1D->Write("posteriorTH1D");
245  //posteriorRooDataHist->Write("posteriorRooDataHist");
246  fPosteriorPdf->Write("fPosteriorPdf");
247  TCanvas myCanvas("canvasforfposterior","canvas for fposterior");
248  myCanvas.cd();
249  //fPosteriorPdf->Draw();
250  //myCanvas.Write();
251  RooRealVar nSig("nSig","",0.0001,200.);
252  RooPlot * pl = nSig.frame();
253  fPosteriorPdf->plotOn(pl);
254  pl->Draw();
255  pl->Write();
256  myCanvas.Write();
257  debugfile->Close();
258 
259  // just for testing: create plots with BAT
260  char * outputFile = "bat_plots_debugging.ps";
261  _myRooInterface -> PrintAllMarginalized(outputFile);
262  //*/
263 
264  return fPosteriorPdf; // is of type RooAbsPdf*
265 }
266 
267 // ---------------------------------------------------------
268 // return a RooPlot with the posterior PDF and the credibility region
269 RooPlot * BATCalculator::GetPosteriorPlot1D() const
270 {
271 
272 
273  if (!fPosteriorPdf){
274  std::cout << "BATCalculator::GetPosteriorPlot1D:"
275  << "Warning : posterior not available" << std::endl;
276  GetPosteriorPdf1D();
277  }
278  if ((!fValidInterval) && (!fValidMCMCInterval)){
279  std::cout << "BATCalculator::GetPosteriorPlot1D:"
280  << "Warning : interval not available" << std::endl;
281  GetInterval1D();
282  }
283 
284  RooAbsRealLValue * poi = dynamic_cast<RooAbsRealLValue *>( fPOI.first() );
285  assert(poi);
286 
287  RooPlot* plot = poi->frame();
288 
289  plot->SetTitle(TString("Posterior probability of parameter \"")+TString(poi->GetName())+TString("\""));
290  fPosteriorPdf->plotOn(plot,RooFit::Range(fLower,fUpper,kFALSE),RooFit::VLines(),RooFit::DrawOption("F"),RooFit::MoveToBack(),RooFit::FillColor(kGray));
291  fPosteriorPdf->plotOn(plot);
292  plot->GetYaxis()->SetTitle("posterior probability");
293 
294  return plot;
295 }
296 
297 // ---------------------------------------------------------
298 // returns central interval for first POI in the POI ArgSet
299 SimpleInterval * BATCalculator::GetInterval1D() const
300 {
301  const char * POIname = fPOI.first()->GetName();
302  return GetInterval1D(POIname); //is of type RooAbsPdf *
303 }
304 
305 
306 // ---------------------------------------------------------
307 // returns central interval for the defined POI in the POI ArgSet -> test code because orginal version is not working anymore
308 SimpleInterval * BATCalculator::GetInterval1D(const char * POIname) const
309 {
310  //const char * POIname = fPOI.first()->GetName();
311 
312  if (fValidInterval)
313  std::cout << "BATCalculator::GetShortestInterval1D:"
314  << "Warning : recomputing interval for the same CL and same model" << std::endl;
315 
316  // get pointer to selected parameter of interest
317  RooRealVar * poi = dynamic_cast<RooRealVar*>( fPOI.find(POIname) );
318  assert(poi);
319 
320  // get pointer to poserior pdf
321  if (!fPosteriorPdf)
322  fPosteriorPdf = (RooAbsPdf*) GetPosteriorPdf1D();
323  //RooAbsReal * cdf = fPosteriorPdf->createCdf(fPOI,RooFit::ScanParameters(100,2));
324 
325  // range of poi
326  Double_t minpoi = poi->getMin();
327  Double_t maxpoi = poi->getMax();
328 
329  // bin number of histogram for posterior
330  Int_t stepnumber = _posteriorTH1D->GetNbinsX();
331  std::cout << "stepnumber is: " << stepnumber << std::endl;
332 
333  // width of one bin in histogram of posterior
334  Double_t stepsize = (maxpoi-minpoi)/stepnumber;
335  std::cout << "stepsize is: " << stepsize << std::endl;
336 
337  // pair: first entry: bin number , second entry: value of posterior
338  std::vector< std::pair< Int_t,Double_t > > posteriorVector;
339 
340  // for normalization
341  Double_t histoIntegral = 0;
342  // give posteriorVector the right length
343  posteriorVector.resize(stepnumber);
344 
345  // see in BayesianCalculator for details about this "feature"
346  // FB: comment in because 'unused variable'
347  // Double_t tmpVal = poi->getVal();
348 
349  // initialize elements of posteriorVector
350  int i = 0;
351  std::vector< std::pair< Int_t,Double_t > >::iterator vecit = posteriorVector.begin();
352  std::vector< std::pair< Int_t,Double_t > >::iterator vecit_end = posteriorVector.end();
353  for( ; vecit != vecit_end ; ++vecit) {
354  poi->setVal(poi->getMin()+i*stepsize);
355  posteriorVector[i] = std::make_pair(i, _posteriorTH1D->GetBinContent(i+1) ); // hope this is working, +1 necessary, because GetBinContent(0) returns the underflow bin
356  histoIntegral+=_posteriorTH1D->GetBinContent(i); // better to get this directly from the histogram ?!
357  //std::cout << "pair with binnumber: " << i << " and postriorprob: " << _posteriorTH1D->GetBinContent(i+1) << std::endl;
358  i++;
359  }
360 
361  std::cout << "histoIntegral is: " << histoIntegral << std::endl;
362 
363  double lowerProbLim = (1.-ConfidenceLevel())*fLeftSideFraction;
364  double upperProbLim = 1. -( (1.-ConfidenceLevel())*(1.-fLeftSideFraction) );
365  std::cout << "lowerProbLim is: " << lowerProbLim << "upperProbLim is: " << histoIntegral << std::endl;
366 
367  fLower = -1.;
368  fUpper = -1.;
369  bool lowerlimset = false;
370  bool upperlimset = false;
371 
372 
373  if(fLeftSideFraction == 0){
374  fLower = minpoi;
375  lowerlimset = true;
376  }
377 
378  if(fLeftSideFraction == 1){
379  fUpper = maxpoi;
380  upperlimset = true;
381  }
382 
383  // keep track of integrated posterior in the interval
384  Double_t integratedposterior = 0.;
385 
386  i = 0;
387  vecit = posteriorVector.begin();
388  for( ; vecit != vecit_end ; ++vecit) {
389  integratedposterior += posteriorVector[i].second;
390  if( (lowerlimset != true) && ((integratedposterior/histoIntegral) >= lowerProbLim) ){
391  fLower = poi->getMin()+i*stepsize;
392  lowerlimset = true;
393  }
394  if( (upperlimset != true) && ((integratedposterior/histoIntegral) >= upperProbLim) ){
395  fUpper = poi->getMin()+i*stepsize;
396  upperlimset = true;
397  break;
398  }
399  i++;
400  }
401 
402  TString interval_name = TString("CentralBayesianInterval_a") + TString(this->GetName());
403  SimpleInterval * interval = new SimpleInterval(interval_name,*poi,fLower,fUpper,ConfidenceLevel());
404  interval->SetTitle("SimpleInterval from BATCalculator");
405 
406  return interval;
407 
408 }
409 
410 // ---------------------------------------------------------
411 // returns central interval for requested POI -> no longer working since root v5_32
412 // SimpleInterval * BATCalculator::GetInterval1Dv0(const char * POIname) const
413 // {
414 // /// returns a SimpleInterval with lower and upper bounds on the
415 // /// parameter of interest. Applies the central ordering rule to
416 // /// compute the credibility interval. Covers only the case with one
417 // /// single parameter of interest
418 //
419 // if (fValidInterval)
420 // std::cout << "BATCalculator::GetInterval1D:"
421 // << "Warning : recomputing interval for the same CL and same model" << std::endl;
422 //
423 // RooRealVar * poi = dynamic_cast<RooRealVar *>( fPOI.find(POIname) );
424 // assert(poi);
425 //
426 // if (!fPosteriorPdf)
427 // fPosteriorPdf = (RooAbsPdf*) GetPosteriorPdf1D();
428 //
429 // Double_t* myarr = new Double_t[1];
430 // myarr[0]= 0.001;
431 // std::cout << "fPosteriorPdf: " << fPosteriorPdf->maxVal(1) << std::endl;
432 // std::cout << "fPosteriorPdf: " << fPosteriorPdf->getVal() << std::endl;
433 // // std::cout << "fPosteriorPdf: " << fPosteriorPdf->evaluate() << std::endl;
434 //
435 // RooAbsReal * cdf = fPosteriorPdf->createCdf(fPOI,RooFit::ScanParameters(100,2));
436 // //RooAbsReal* cdf = fPosteriorPdf->createCdf(fPOI,RooFit::ScanNoCdf());
437 //
438 // RooAbsFunc * cdf_bind = cdf->bindVars(fPOI,&fPOI);
439 // std::cout << "cdf_bind: " << std::endl;
440 // std::cout << "cdf_bind: " << cdf_bind->isValid() << std::endl;
441 // std::cout << "cdf_bind: " << cdf_bind->getDimension() << std::endl;
442 // //Double_t test = (*_function)(&a) - value;
443 // std::cout << "cdf_bind: " << (*cdf_bind)(myarr) << std::endl;
444 // std::cout << "cdf_bind: " << cdf_bind->getMaxLimit(1) << std::endl;
445 //
446 // RooBrentRootFinder brf(*cdf_bind);
447 // brf.setTol(fBrfPrecision); // set the brf precision
448 //
449 // double tmpVal = poi->getVal(); // patch used because findRoot changes the value of poi
450 //
451 // double y = fSize*fLeftSideFraction; //adjust lower tail prob. according to fLeftSideFraction
452 //
453 // brf.findRoot(fLower,poi->getMin(),poi->getMax(),y);
454 //
455 // y = 1.-(fSize*(1.-fLeftSideFraction) ); //adjust upper tail prob. according to fLeftSideFraction
456 //
457 // bool ret = brf.findRoot(fUpper,poi->getMin(),poi->getMax(),y);
458 // std::cout << " brf.findRoot: " << fUpper << " " << poi->getMin() << " " << poi->getMax() << " " << y << std::endl;
459 // if (!ret)
460 // std::cout << "BATCalculator::GetInterval1D: Warning:"
461 // << "Error returned from Root finder, estimated interval is not fully correct"
462 // << std::endl;
463 //
464 // poi->setVal(tmpVal); // patch: restore the original value of poi
465 //
466 // delete cdf_bind;
467 // delete cdf;
468 // fValidInterval = true;
469 // fConnectedInterval = true;
470 //
471 // TString interval_name = TString("CentralBayesianInterval_a") + TString(this->GetName());
472 // SimpleInterval * interval = new SimpleInterval(interval_name,*poi,fLower,fUpper,ConfidenceLevel());
473 // interval->SetTitle("SimpleInterval from BATCalculator");
474 //
475 // return interval;
476 // }
477 
478 // ---------------------------------------------------------
479 SimpleInterval * BATCalculator::GetShortestInterval1D() const
480 {
481  const char * POIname = fPOI.first()->GetName();
482  bool checkConnected = true;
483  return GetShortestInterval1D(POIname, checkConnected);
484 }
485 
486 // ---------------------------------------------------------
487 // returns a SimpleInterval with lower and upper bounds on the
488 // parameter of interest. Applies the shortest interval rule to
489 // compute the credibility interval. The resulting interval is not necessarily connected.
490 // Methods for constructing the shortest region with respect given CL are now available in
491 // different places (here; MCMCCalculator, BAT)-> this might require cleaning at some point.
492 // Result is approximate as CL is not reached exactly (due to finite bin number/bin size in posterior histogram)-> make CL/interval a bit smaller or bigger ?
493 SimpleInterval * BATCalculator::GetShortestInterval1D(const char * POIname, bool & checkConnected) const
494 {
495 
496  if (fValidInterval)
497  std::cout << "BATCalculator::GetShortestInterval1D:"
498  << "Warning : recomputing interval for the same CL and same model" << std::endl;
499 
500  // get pointer to selected parameter of interest
501  RooRealVar * poi = dynamic_cast<RooRealVar*>( fPOI.find(POIname) );
502  assert(poi);
503 
504  // get pointer to poserior pdf
505  if (!fPosteriorPdf)
506  fPosteriorPdf = (RooAbsPdf*) GetPosteriorPdf1D();
507  //RooAbsReal * cdf = fPosteriorPdf->createCdf(fPOI,RooFit::ScanParameters(100,2));
508 
509  // range of poi
510  Double_t minpoi = poi->getMin();
511  Double_t maxpoi = poi->getMax();
512 
513  // bin number of histogram for posterior
514  Int_t stepnumber = _posteriorTH1D->GetNbinsX();
515  std::cout << "stepnumber is: " << stepnumber << std::endl;
516 
517  // width of one bin in histogram of posterior
518  Double_t stepsize = (maxpoi-minpoi)/stepnumber;
519  std::cout << "stepsize is: " << stepsize << std::endl;
520 
521  // pair: first entry: bin number , second entry: value of posterior
522  std::vector< std::pair< Int_t,Double_t > > posteriorVector;
523 
524  // for normalization
525  Double_t histoIntegral = 0;
526  // give posteriorVector the right length
527  posteriorVector.resize(stepnumber);
528 
529  // see in BayesianCalculator for details about this "feature"
530  Double_t tmpVal = poi->getVal();
531 
532  // initialize elements of posteriorVector
533  int i = 0;
534  std::vector< std::pair< Int_t,Double_t > >::iterator vecit = posteriorVector.begin();
535  std::vector< std::pair< Int_t,Double_t > >::iterator vecit_end = posteriorVector.end();
536  for( ; vecit != vecit_end ; ++vecit) {
537  poi->setVal(poi->getMin()+i*stepsize);
538  posteriorVector[i] = std::make_pair(i, _posteriorTH1D->GetBinContent(i+1) ); // hope this is working, +1 necessary, because GetBinContent(0) returns the underflow bin
539  histoIntegral+=_posteriorTH1D->GetBinContent(i); // better to get this directly from the histogram ?!
540  //std::cout << "pair with binnumber: " << i << " and postriorprob: " << _posteriorTH1D->GetBinContent(i+1) << std::endl;
541  i++;
542  }
543 
544  std::cout << "histoIntegral is: " << histoIntegral << std::endl;
545 
546  // sort posteriorVector with respect to posterior pdf
547  std::sort(posteriorVector.begin(), posteriorVector.end(), sortbyposterior);
548 
549  // keep track of integrated posterior in the interval
550  Double_t integratedposterior = 0.;
551 
552  // keep track of lowerLim and upperLim
553  Double_t lowerLim=posteriorVector.size();
554  Double_t upperLim=0;
555 
556  // store the bins in the intervall
557  std::vector<bool> inInterval;
558  inInterval.resize(posteriorVector.size());
559 
560  // set all values in inInterval to false
561  for (unsigned int k = 0; k < inInterval.size(); k++)
562  inInterval[k] = false;
563 
564  unsigned int j = 0;
565  // add bins to interval while CL not reached
566  //std::cout << "integratedposterior: " << integratedposterior << "histoIntegral: " << histoIntegral << std::endl;
567  //std::cout << " integratedposterior/histoIntegral : " << integratedposterior/histoIntegral << std::endl;
568  //std::cout << " 1-fSize : " << 1-fSize << std::endl;
569  //std::cout << " posteriorVector.size(): " << posteriorVector.size() << std::endl;
570  while(((integratedposterior/histoIntegral) < (1-fSize)) && (j < posteriorVector.size())) {
571  //std::cout << "j is: " << j << " , int prob: " << integratedposterior/histoIntegral << std::endl;
572  integratedposterior+=posteriorVector[j].second;
573 
574  //std::cout << "bin number: " << posteriorVector[j].first << " with posterior prob.: " << posteriorVector[j].second << std::endl;
575 
576  // update vector with bins included in the interval
577  inInterval[posteriorVector[j].first] = true;
578 
579  if(posteriorVector[j].first < lowerLim) {
580  lowerLim = posteriorVector[j].first; // update lowerLim
581  std::cout << "updating lower lim to: " << lowerLim << std::endl;
582  }
583  if(posteriorVector[j].first > upperLim) {
584  upperLim = posteriorVector[j].first; // update upperLim
585  std::cout << "updating upper lim to: " << upperLim << std::endl;
586  }
587 
588  fLower = lowerLim * stepsize; //
589  fUpper = upperLim * stepsize;
590  j++;
591  }
592 
593  // initialize vector with interval borders
594 
595  bool runInside = false;
596  for (unsigned int l = 0; l < inInterval.size(); l++) {
597  if ( (runInside == false) && (inInterval[l] == true) ) {
598  _intervalBorders1D.push_back(static_cast<double>(l)* stepsize);
599  runInside = true;
600  }
601  if ( ( runInside == true) && (l < (inInterval.size()-1) ) && (inInterval[l+1] == false) ) {
602  _intervalBorders1D.push_back(static_cast<double>(l)* stepsize);
603  runInside = false;
604  }
605  if ( ( runInside == true) && (l == (inInterval.size()-1)) ) {
606  _intervalBorders1D.push_back(static_cast<double>(l)* stepsize);
607  }
608  }
609 
610  // check if the intervall is connected
611  if(checkConnected) {
612  if (_intervalBorders1D.size() > 2) {
613  fConnectedInterval = false;
614  }
615  else {
616  fConnectedInterval = true;
617  }
618  }
619 
620  poi->setVal(tmpVal); // patch: restore the original value of poi
621 
622  fValidInterval = true;
623 
624  TString interval_name = TString("ShortestBayesianInterval_a") + TString(this->GetName());
625  SimpleInterval * interval = new SimpleInterval(interval_name,*poi,fLower,fUpper,ConfidenceLevel());
626  interval->SetTitle("Shortest SimpleInterval from BATCalculator");
627 
628  //if(assert)
629  return interval;
630 }
631 
632 // ---------------------------------------------------------
633 MCMCInterval* BATCalculator::GetInterval() const{
634 
635  // run some sanity checks
636  if (!fPdf ) {
637  std::cerr << "BATCalculator::GetInterval - missing pdf model" << std::endl;
638  return 0;
639  }
640 
641  if (!fPrior) {
642  std::cerr << "BATCalculator::GetInterval - missing prior pdf" << std::endl;
643  }
644 
645  if (fPOI.getSize() == 0) {
646  std::cerr << "BATCalculator::GetInterval - missing parameter of interest" << std::endl;
647  return 0;
648  }
649 
650  if (!fPosteriorPdf){
651  //initialize RooInterface object
652  _myRooInterface->Initialize(*fData,*fPdf,*fPrior,fparams,fPOI);
653  _myRooInterface->MCMCSetNIterationsRun(_nMCMC);
654  _myRooInterface->MarginalizeAll();
655  _myRooInterface->FindMode();
656  }
657 
658  MarkovChain * roostats_chain = GetBCRooInterface()->GetRooStatsMarkovChain();
659  MCMCInterval * mcmcInterval = new MCMCInterval("roostatsmcmcinterval", fPOI , *roostats_chain);
660  //MCMCInterval * mcmcInterval = new MCMCInterval("roostatsmcmcinterval", *(GetBCRooInterface()->GetArgSetForMarkovChain()) , *roostats_chain);
661  mcmcInterval->SetUseKeys(false);
662  mcmcInterval->SetConfidenceLevel(1.-fSize);
663  fValidMCMCInterval = true;
664  return mcmcInterval;
665 }
666 
667 // ---------------------------------------------------------
668 void BATCalculator::SetNumBins(const char * parname, int nbins)
669 {
670  _myRooInterface->SetNumBins(parname, nbins);
671 }
672 
673 void BATCalculator::SetNumBins(int nbins)
674 {
675  _myRooInterface->SetNumBins(nbins);
676 }
677 
678 void BATCalculator::SetLeftSideTailFraction(Double_t leftSideFraction ){
679  if( (leftSideFraction >= 0.) && (leftSideFraction <= 1.) ){
680  fLeftSideFraction = leftSideFraction;
681  }
682  else{
683  std::cout << "BATCalculator::SetLeftSideTailFraction(Double_t leftSideFraction ) - value needs to be in the interval [0.,1.] to be meaningful, your value: " << leftSideFraction << " ,left side tail fraction has not been changed!" << std::endl;
684  }
685 
686 }
687 
688 
689 // ---------------------------------------------------------
690 Double_t BATCalculator::GetOneSidedUperLim()
691 {
692 // double safeVal = fSize;
693 // fSize = safeVal/2.;
694  std::cout << "calculating " << (1.-fSize/2) << "upper limit" << std::endl;
695  return GetInterval1D()->UpperLimit();
696 }
697 
698 /*
699 // ---------------------------------------------------------
700 int BATCalculator::GetNbins(const char * parname)
701 {
702  ;
703 }
704 */
705 
706 // ---------------------------------------------------------
707 bool sortbyposterior(std::pair< Int_t,Double_t > pair1, std::pair< Int_t,Double_t > pair2)
708 {
709  return (pair1.second > pair2.second);
710 }
711 
712 
713 
714 } // end namespace RooStats
A class representing a parameter of a model.
Definition: BCParameter.h:28
TH1D * GetHistogram()
Definition: BCH1D.h:53
A class for handling 1D distributions.
Definition: BCH1D.h:31