#ifndef RooStats_FeldmanCousins
#include "RooStats/FeldmanCousins.h"
#endif
#ifndef RooStats_RooStatsUtils
#include "RooStats/RooStatsUtils.h"
#endif
#ifndef RooStats_PointSetInterval
#include "RooStats/PointSetInterval.h"
#endif
#include "RooStats/SamplingDistribution.h"
#include "RooStats/ProfileLikelihoodTestStat.h"
#include "RooStats/NeymanConstruction.h"
#include "RooStats/RooStatsUtils.h"
#include "RooDataSet.h"
#include "RooDataHist.h"
#include "RooGlobalFunc.h"
#include "RooDataHist.h"
#include "TFile.h"
#include "TTree.h"
ClassImp(RooStats::FeldmanCousins) ;
using namespace RooFit;
using namespace RooStats;
FeldmanCousins::FeldmanCousins() {
fWS = new RooWorkspace("FeldmanCousinsWS");
fOwnsWorkspace = true;
fDataName = "";
fPdfName = "";
fAdaptiveSampling=false;
fPointsToTest = 0;
fNbins = 10;
fFluctuateData=true;
fDoProfileConstruction=true;
}
FeldmanCousins::~FeldmanCousins() {
if(fOwnsWorkspace && fWS) delete fWS;
if(fPointsToTest) delete fPointsToTest;
if(fTestStatSampler) delete fTestStatSampler;
}
void FeldmanCousins::CreateTestStatSampler() const{
RooAbsPdf* pdf = fWS->pdf(fPdfName);
RooAbsData* data = fWS->data(fDataName);
if (data && pdf ) {
RooArgSet* parameters = pdf->getParameters(data);
RemoveConstantParameters(parameters);
ProfileLikelihoodTestStat* testStatistic = new ProfileLikelihoodTestStat(*pdf);
fTestStatSampler = new ToyMCSampler(*testStatistic) ;
fTestStatSampler->SetPdf(*pdf);
fTestStatSampler->SetParameters(*parameters);
fTestStatSampler->SetNEventsPerToy(data->numEntries());
fTestStatSampler->SetNToys((int) (50./fSize));
fTestStatSampler->SetExtended(fFluctuateData);
if(!fAdaptiveSampling){
cout << "ntoys per point = " << (int) 50./fSize << endl;
} else{
cout << "ntoys per point: adaptive" << endl;
}
if(fFluctuateData)
cout << "nEvents per toy will fluctuate about expectation" << endl;
else
cout << "nEvents per toy will not fluctuate, will always be " << data->numEntries() << endl;
}
}
void FeldmanCousins::CreateParameterPoints() const{
RooAbsPdf* pdf = fWS->pdf(fPdfName);
RooAbsData* data = fWS->data(fDataName);
if (data && pdf ){
RooArgSet* parameters = pdf->getParameters(data);
RemoveConstantParameters(parameters);
TIter it = parameters->createIterator();
RooRealVar *myarg;
while ((myarg = (RooRealVar *)it.Next())) {
if(!myarg) continue;
myarg->setBins(fNbins);
}
if( ! fPOI->equals(*parameters) && fDoProfileConstruction ) {
cout << " nuisance parameters, will do profile construction" << endl;
TIter it2 = fPOI->createIterator();
RooRealVar *myarg2;
while ((myarg2 = (RooRealVar *)it2.Next())) {
if(!myarg2) continue;
myarg2->setBins(fNbins);
}
RooDataHist* parameterScan = new RooDataHist("parameterScan", "", *fPOI);
cout << "# points to test = " << parameterScan->numEntries() << endl;
RooArgSet* tmpPoint;
RooFit::MsgLevel previous = RooMsgService::instance().globalKillBelow();
RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL) ;
RooAbsReal* nll = pdf->createNLL(*data, Constrain(*parameters));
RooAbsReal* profile = nll->createProfile(*fPOI);
RooDataSet* profileConstructionPoints = new RooDataSet("profileConstruction",
"profileConstruction",
*parameters);
for(Int_t i=0; i<parameterScan->numEntries(); ++i){
tmpPoint = (RooArgSet*) parameterScan->get(i)->clone("temp");
RooStats::SetParameters(tmpPoint, parameters);
profile->getVal();
profileConstructionPoints->add(*parameters);
delete tmpPoint;
}
RooMsgService::instance().setGlobalKillBelow(previous) ;
delete profile;
delete nll;
fPointsToTest = profileConstructionPoints;
cout << "# points to test = " << fPointsToTest->numEntries() << endl;
delete parameterScan;
} else{
cout << " no nuisance parameters" << endl;
RooDataHist* parameterScan = new RooDataHist("parameterScan", "", *parameters);
cout << "# points to test = " << parameterScan->numEntries() << endl;
fPointsToTest = parameterScan;
}
delete parameters;
}
}
ConfInterval* FeldmanCousins::GetInterval() const {
RooAbsData* data = fWS->data(fDataName);
if(!data) {
cout << "Data is not set, FeldmanCousins not initialized" << endl;
return 0;
}
this->CreateTestStatSampler();
this->CreateParameterPoints();
RooStats::NeymanConstruction nc;
nc.SetTestStatSampler(*fTestStatSampler);
nc.SetTestSize(fSize);
nc.SetParameterPointsToTest( *fPointsToTest );
nc.SetLeftSideTailFraction(0.);
nc.SetData(*data);
nc.UseAdaptiveSampling(fAdaptiveSampling);
nc.SaveBeltToFile(fSaveBeltToFile);
nc.CreateConfBelt(fCreateBelt);
fConfBelt = nc.GetConfidenceBelt();
return nc.GetInterval();
}