00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef RooStats_FeldmanCousins
00033 #include "RooStats/FeldmanCousins.h"
00034 #endif
00035
00036 #ifndef RooStats_RooStatsUtils
00037 #include "RooStats/RooStatsUtils.h"
00038 #endif
00039
00040 #ifndef RooStats_PointSetInterval
00041 #include "RooStats/PointSetInterval.h"
00042 #endif
00043
00044 #include "RooStats/ModelConfig.h"
00045
00046 #include "RooStats/SamplingDistribution.h"
00047
00048 #include "RooStats/ProfileLikelihoodTestStat.h"
00049 #include "RooStats/NeymanConstruction.h"
00050 #include "RooStats/RooStatsUtils.h"
00051
00052 #include "RooDataSet.h"
00053 #include "RooDataHist.h"
00054 #include "RooGlobalFunc.h"
00055 #include "RooMsgService.h"
00056 #include "TFile.h"
00057 #include "TTree.h"
00058
00059 ClassImp(RooStats::FeldmanCousins) ;
00060
00061 using namespace RooFit;
00062 using namespace RooStats;
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 FeldmanCousins::FeldmanCousins(RooAbsData& data, ModelConfig& model) :
00089 fSize(0.05),
00090 fModel(model),
00091 fData(data),
00092 fTestStatSampler(0),
00093 fPointsToTest(0),
00094 fPOIToTest(0),
00095 fConfBelt(0),
00096 fAdaptiveSampling(false),
00097 fAdditionalNToysFactor(1.),
00098 fNbins(10),
00099 fFluctuateData(true),
00100 fDoProfileConstruction(true),
00101 fSaveBeltToFile(false),
00102 fCreateBelt(false)
00103 {
00104
00105 }
00106
00107
00108 FeldmanCousins::~FeldmanCousins() {
00109
00110
00111 if(fPointsToTest) delete fPointsToTest;
00112 if(fPOIToTest) delete fPOIToTest;
00113 if(fTestStatSampler) delete fTestStatSampler;
00114 }
00115
00116
00117
00118 void FeldmanCousins::SetModel(const ModelConfig & model) {
00119
00120 fModel = model;
00121 }
00122
00123
00124 TestStatSampler* FeldmanCousins::GetTestStatSampler() const{
00125 if(!fTestStatSampler)
00126 this->CreateTestStatSampler();
00127 return fTestStatSampler;
00128 }
00129
00130
00131 void FeldmanCousins::CreateTestStatSampler() const{
00132
00133
00134
00135 ProfileLikelihoodTestStat* testStatistic = new ProfileLikelihoodTestStat(*fModel.GetPdf());
00136
00137
00138 fTestStatSampler = new ToyMCSampler(*testStatistic,int(fAdditionalNToysFactor*50./fSize)) ;
00139 fTestStatSampler->SetParametersForTestStat(*fModel.GetParametersOfInterest() );
00140 if(fModel.GetObservables())
00141 fTestStatSampler->SetObservables(*fModel.GetObservables());
00142 fTestStatSampler->SetPdf(*fModel.GetPdf());
00143
00144 if(!fAdaptiveSampling){
00145 ooccoutP(&fModel,Generation) << "FeldmanCousins: ntoys per point = " << (int) (fAdditionalNToysFactor*50./fSize) << endl;
00146 } else{
00147 ooccoutP(&fModel,Generation) << "FeldmanCousins: ntoys per point: adaptive" << endl;
00148 }
00149 if(fFluctuateData){
00150 ooccoutP(&fModel,Generation) << "FeldmanCousins: nEvents per toy will fluctuate about expectation" << endl;
00151 } else{
00152 ooccoutP(&fModel,Generation) << "FeldmanCousins: nEvents per toy will not fluctuate, will always be " << fData.numEntries() << endl;
00153 fTestStatSampler->SetNEventsPerToy(fData.numEntries());
00154 }
00155 }
00156
00157
00158
00159 void FeldmanCousins::CreateParameterPoints() const{
00160
00161
00162
00163
00164 RooAbsPdf* pdf = fModel.GetPdf();
00165 if (!pdf ){
00166 ooccoutE(&fModel,Generation) << "FeldmanCousins: ModelConfig has no PDF" << endl;
00167 return;
00168 }
00169
00170
00171 RooArgSet* parameters = new RooArgSet(*fModel.GetParametersOfInterest());
00172 if(fModel.GetNuisanceParameters())
00173 parameters->add(*fModel.GetNuisanceParameters());
00174
00175
00176 if( fModel.GetNuisanceParameters() && ! fModel.GetParametersOfInterest()->equals(*parameters) && fDoProfileConstruction) {
00177
00178 ooccoutP(&fModel,Generation) << "FeldmanCousins: Model has nuisance parameters, will do profile construction" << endl;
00179
00180
00181 TIter it2 = fModel.GetParametersOfInterest()->createIterator();
00182 RooRealVar *myarg2;
00183 while ((myarg2 = dynamic_cast<RooRealVar*>(it2.Next()))) {
00184 myarg2->setBins(fNbins);
00185 }
00186
00187
00188
00189 RooAbsData* parameterScan = NULL;
00190 if(fPOIToTest)
00191 parameterScan = fPOIToTest;
00192 else
00193 parameterScan = new RooDataHist("parameterScan", "", *fModel.GetParametersOfInterest());
00194
00195
00196 ooccoutP(&fModel,Generation) << "FeldmanCousins: # points to test = " << parameterScan->numEntries() << endl;
00197
00198 RooFit::MsgLevel previous = RooMsgService::instance().globalKillBelow();
00199 RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL) ;
00200 RooAbsReal* nll = pdf->createNLL(fData,RooFit::CloneData(false));
00201 RooAbsReal* profile = nll->createProfile(*fModel.GetParametersOfInterest());
00202
00203 RooDataSet* profileConstructionPoints = new RooDataSet("profileConstruction",
00204 "profileConstruction",
00205 *parameters);
00206
00207
00208 for(Int_t i=0; i<parameterScan->numEntries(); ++i){
00209
00210 *parameters = *parameterScan->get(i);
00211 profile->getVal();
00212 profileConstructionPoints->add(*parameters);
00213 }
00214 RooMsgService::instance().setGlobalKillBelow(previous) ;
00215 delete profile;
00216 delete nll;
00217 if(!fPOIToTest) delete parameterScan;
00218
00219
00220 fPointsToTest = profileConstructionPoints;
00221
00222 } else{
00223
00224 ooccoutP(&fModel,Generation) << "FeldmanCousins: Model has no nuisance parameters" << endl;
00225
00226 TIter it = parameters->createIterator();
00227 RooRealVar *myarg;
00228 while ((myarg = dynamic_cast<RooRealVar*>(it.Next()))) {
00229 myarg->setBins(fNbins);
00230 }
00231
00232 RooDataHist* parameterScan = new RooDataHist("parameterScan", "", *parameters);
00233 ooccoutP(&fModel,Generation) << "FeldmanCousins: # points to test = " << parameterScan->numEntries() << endl;
00234
00235 fPointsToTest = parameterScan;
00236 }
00237
00238 delete parameters;
00239
00240 }
00241
00242
00243
00244 PointSetInterval* FeldmanCousins::GetInterval() const {
00245
00246
00247
00248
00249
00250
00251
00252 fModel.GuessObsAndNuisance(fData);
00253
00254
00255 if(!fTestStatSampler)
00256 this->CreateTestStatSampler();
00257
00258 fTestStatSampler->SetObservables(*fModel.GetObservables());
00259
00260 if(!fFluctuateData)
00261 fTestStatSampler->SetNEventsPerToy(fData.numEntries());
00262
00263
00264 this->CreateParameterPoints();
00265
00266
00267 NeymanConstruction nc(fData,fModel);
00268
00269 nc.SetTestStatSampler(*fTestStatSampler);
00270 nc.SetTestSize(fSize);
00271
00272 nc.SetParameterPointsToTest( *fPointsToTest );
00273 nc.SetLeftSideTailFraction(0.);
00274 nc.SetData(fData);
00275 nc.UseAdaptiveSampling(fAdaptiveSampling);
00276 nc.AdditionalNToysFactor(fAdditionalNToysFactor);
00277 nc.SaveBeltToFile(fSaveBeltToFile);
00278 nc.CreateConfBelt(fCreateBelt);
00279 fConfBelt = nc.GetConfidenceBelt();
00280
00281 return nc.GetInterval();
00282 }