59 ClassImp(RooStats::FeldmanCousins); ;
61 using namespace RooFit;
62 using namespace RooStats;
68 FeldmanCousins::FeldmanCousins(RooAbsData& data, ModelConfig& model) :
76 fAdaptiveSampling(false),
77 fAdditionalNToysFactor(1.),
80 fDoProfileConstruction(true),
81 fSaveBeltToFile(false),
89 FeldmanCousins::~FeldmanCousins() {
90 if(fPointsToTest)
delete fPointsToTest;
91 if(fPOIToTest)
delete fPOIToTest;
92 if(fTestStatSampler)
delete fTestStatSampler;
98 void FeldmanCousins::SetModel(
const ModelConfig & model) {
104 TestStatSampler* FeldmanCousins::GetTestStatSampler()
const{
105 if(!fTestStatSampler)
106 this->CreateTestStatSampler();
107 return fTestStatSampler;
113 void FeldmanCousins::CreateTestStatSampler()
const{
115 ProfileLikelihoodTestStat* testStatistic =
new ProfileLikelihoodTestStat(*fModel.GetPdf());
118 fTestStatSampler =
new ToyMCSampler(*testStatistic,
int(fAdditionalNToysFactor*50./fSize)) ;
119 fTestStatSampler->SetParametersForTestStat(*fModel.GetParametersOfInterest() );
120 if(fModel.GetObservables())
121 fTestStatSampler->SetObservables(*fModel.GetObservables());
122 fTestStatSampler->SetPdf(*fModel.GetPdf());
124 if(!fAdaptiveSampling){
125 ooccoutP(&fModel,Generation) <<
"FeldmanCousins: ntoys per point = " << (int) (fAdditionalNToysFactor*50./fSize) << endl;
127 ooccoutP(&fModel,Generation) <<
"FeldmanCousins: ntoys per point: adaptive" << endl;
130 ooccoutP(&fModel,Generation) <<
"FeldmanCousins: nEvents per toy will fluctuate about expectation" << endl;
132 ooccoutP(&fModel,Generation) <<
"FeldmanCousins: nEvents per toy will not fluctuate, will always be " << fData.numEntries() << endl;
133 fTestStatSampler->SetNEventsPerToy(fData.numEntries());
141 void FeldmanCousins::CreateParameterPoints()
const{
143 RooAbsPdf* pdf = fModel.GetPdf();
145 ooccoutE(&fModel,Generation) <<
"FeldmanCousins: ModelConfig has no PDF" << endl;
150 RooArgSet* parameters =
new RooArgSet(*fModel.GetParametersOfInterest());
151 if(fModel.GetNuisanceParameters())
152 parameters->add(*fModel.GetNuisanceParameters());
155 if( fModel.GetNuisanceParameters() && ! fModel.GetParametersOfInterest()->equals(*parameters) && fDoProfileConstruction) {
157 ooccoutP(&fModel,Generation) <<
"FeldmanCousins: Model has nuisance parameters, will do profile construction" << endl;
160 TIter it2 = fModel.GetParametersOfInterest()->createIterator();
162 while ((myarg2 = dynamic_cast<RooRealVar*>(it2.Next()))) {
163 myarg2->setBins(fNbins);
168 RooAbsData* parameterScan = NULL;
170 parameterScan = fPOIToTest;
172 parameterScan =
new RooDataHist(
"parameterScan",
"", *fModel.GetParametersOfInterest());
175 ooccoutP(&fModel,Generation) <<
"FeldmanCousins: # points to test = " << parameterScan->numEntries() << endl;
177 RooFit::MsgLevel previous = RooMsgService::instance().globalKillBelow();
178 RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL) ;
179 RooAbsReal* nll = pdf->createNLL(fData,RooFit::CloneData(
false));
180 RooAbsReal* profile = nll->createProfile(*fModel.GetParametersOfInterest());
182 RooDataSet* profileConstructionPoints =
new RooDataSet(
"profileConstruction",
183 "profileConstruction",
187 for(Int_t i=0; i<parameterScan->numEntries(); ++i){
189 *parameters = *parameterScan->get(i);
191 profileConstructionPoints->add(*parameters);
193 RooMsgService::instance().setGlobalKillBelow(previous) ;
196 if(!fPOIToTest)
delete parameterScan;
199 fPointsToTest = profileConstructionPoints;
203 ooccoutP(&fModel,Generation) <<
"FeldmanCousins: Model has no nuisance parameters" << endl;
205 TIter it = parameters->createIterator();
207 while ((myarg = dynamic_cast<RooRealVar*>(it.Next()))) {
208 myarg->setBins(fNbins);
211 RooDataHist* parameterScan =
new RooDataHist(
"parameterScan",
"", *parameters);
212 ooccoutP(&fModel,Generation) <<
"FeldmanCousins: # points to test = " << parameterScan->numEntries() << endl;
214 fPointsToTest = parameterScan;
225 PointSetInterval* FeldmanCousins::GetInterval()
const {
230 fModel.GuessObsAndNuisance(fData);
233 if(!fTestStatSampler)
234 this->CreateTestStatSampler();
236 fTestStatSampler->SetObservables(*fModel.GetObservables());
239 fTestStatSampler->SetNEventsPerToy(fData.numEntries());
242 this->CreateParameterPoints();
245 NeymanConstruction nc(fData,fModel);
247 nc.SetTestStatSampler(*fTestStatSampler);
248 nc.SetTestSize(fSize);
250 nc.SetParameterPointsToTest( *fPointsToTest );
251 nc.SetLeftSideTailFraction(0.);
253 nc.UseAdaptiveSampling(fAdaptiveSampling);
254 nc.AdditionalNToysFactor(fAdditionalNToysFactor);
255 nc.SaveBeltToFile(fSaveBeltToFile);
256 nc.CreateConfBelt(fCreateBelt);
257 fConfBelt = nc.GetConfidenceBelt();
259 return nc.GetInterval();