00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef ROOSTATS_ProfileLikelihoodTestStat
00012 #define ROOSTATS_ProfileLikelihoodTestStat
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef ROOT_Rtypes
00032 #include "Rtypes.h"
00033 #endif
00034
00035 #include <vector>
00036
00037 #include "RooStats/RooStatsUtils.h"
00038
00039
00040 #include "RooStats/SamplingDistribution.h"
00041 #include "RooStats/TestStatistic.h"
00042
00043 #include "RooStats/RooStatsUtils.h"
00044
00045 #include "RooRealVar.h"
00046 #include "RooProfileLL.h"
00047 #include "RooNLLVar.h"
00048
00049 #include "RooMinuit.h"
00050
00051 namespace RooStats {
00052
00053 class ProfileLikelihoodTestStat : public TestStatistic{
00054
00055 public:
00056 ProfileLikelihoodTestStat() {
00057
00058 fPdf = 0;
00059 fProfile = 0;
00060 fNll = 0;
00061 fCachedBestFitParams = 0;
00062 fLastData = 0;
00063 fOneSided = false;
00064 }
00065 ProfileLikelihoodTestStat(RooAbsPdf& pdf) {
00066 fPdf = &pdf;
00067 fProfile = 0;
00068 fNll = 0;
00069 fCachedBestFitParams = 0;
00070 fLastData = 0;
00071 fOneSided = false;
00072 }
00073 virtual ~ProfileLikelihoodTestStat() {
00074
00075
00076 if(fProfile) delete fProfile;
00077 if(fNll) delete fNll;
00078 if(fCachedBestFitParams) delete fCachedBestFitParams;
00079 }
00080 void SetOneSided(Bool_t flag=true) {fOneSided = flag;}
00081
00082
00083 virtual Double_t Evaluate(RooAbsData& data, RooArgSet& paramsOfInterest) {
00084 if (!&data) {
00085 cout << "problem with data" << endl;
00086 return 0 ;
00087 }
00088
00089 RooRealVar* firstPOI = (RooRealVar*) paramsOfInterest.first();
00090 double initial_mu_value = firstPOI->getVal();
00091
00092
00093 RooFit::MsgLevel msglevel = RooMsgService::instance().globalKillBelow();
00094 RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
00095
00096
00097
00098 RooAbsReal* nll = fPdf->createNLL(data, RooFit::CloneData(kFALSE));
00099 RooAbsReal* profile = nll->createProfile(paramsOfInterest);
00100
00101 RooArgSet* attachedSet = nll->getVariables();
00102 *attachedSet = paramsOfInterest;
00103
00104
00105
00106
00107 double ret = profile->getVal();
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 if(fOneSided){
00118 double fit_favored_mu = ((RooProfileLL*) profile)->bestFitObs().getRealValue(firstPOI->GetName()) ;
00119
00120 if( fit_favored_mu > initial_mu_value)
00121
00122 ret = 0 ;
00123 }
00124 delete attachedSet;
00125 delete nll;
00126 nll = 0;
00127 delete profile;
00128 RooMsgService::instance().setGlobalKillBelow(msglevel);
00129
00130
00131
00132 return ret;
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 bool needToRebuild = true;
00145
00146 if (fLastData == &data)
00147 needToRebuild = false;
00148 else fLastData = &data;
00149
00150
00151
00152 needToRebuild = true;
00153
00154
00155
00156
00157
00158 if (needToRebuild) {
00159 if (fProfile) delete fProfile;
00160 if (fNll) delete fNll;
00161
00162
00163
00164
00165
00166
00167 RooArgSet* constrainedParams = fPdf->getParameters(data);
00168 RemoveConstantParameters(constrainedParams);
00169
00170
00171
00172 RooNLLVar * nll2 = (RooNLLVar*) fPdf->createNLL(
00173 data, RooFit::CloneData(kFALSE), RooFit::Constrain(*constrainedParams)
00174 );
00175 fNll = nll2;
00176 fProfile = (RooProfileLL*) nll2->createProfile(paramsOfInterest);
00177 delete constrainedParams;
00178
00179
00180
00181
00182
00183 if (fCachedBestFitParams) {
00184
00185 RooArgSet* origParamVals = (RooArgSet*) paramsOfInterest.snapshot();
00186
00187
00188 SetParameters(fCachedBestFitParams, fProfile->getParameters(data));
00189
00190
00191 fProfile->getVal();
00192
00193
00194
00195
00196
00197 SetParameters(origParamVals, ¶msOfInterest);
00198
00199
00200 delete origParamVals;
00201
00202 } else {
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 RooArgSet* origParamVals = (RooArgSet*) paramsOfInterest.snapshot();
00213
00214
00215 RooMinuit minuit(*nll);
00216 minuit.setPrintLevel(-999);
00217 minuit.setNoWarn();
00218 minuit.migrad();
00219
00220
00221 fCachedBestFitParams = (RooArgSet*) (nll->getParameters(data)->snapshot());
00222
00223
00224 SetParameters(origParamVals, ¶msOfInterest);
00225
00226
00227
00228 fProfile->getVal();
00229
00230
00231 delete origParamVals;
00232
00233 }
00234
00235 }
00236
00237 if (!fProfile) {
00238 cout << "problem making profile" << endl;
00239 }
00240
00241
00242 SetParameters(¶msOfInterest, fProfile->getParameters(data));
00243
00244 Double_t value = fProfile->getVal();
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 if (value < 0) {
00260
00261
00262 delete fNll;
00263 delete fProfile;
00264
00265
00266
00267
00268
00269
00270 RooArgSet* constrainedParams = fPdf->getParameters(data);
00271 RemoveConstantParameters(constrainedParams);
00272
00273 RooNLLVar * nll2 = (RooNLLVar*) fPdf->createNLL(data, RooFit::CloneData(kFALSE), RooFit::Constrain(
00274 *constrainedParams));
00275 fNll = nll2;
00276 fProfile = (RooProfileLL*) nll2->createProfile(paramsOfInterest);
00277 delete constrainedParams;
00278
00279
00280 SetParameters(¶msOfInterest, fProfile->getParameters(data));
00281
00282 value = fProfile->getVal();
00283
00284 }
00285
00286 RooMsgService::instance().setGlobalKillBelow(RooFit::DEBUG);
00287 return value;
00288 }
00289
00290
00291 virtual const TString GetVarName() const {return "Profile Likelihood Ratio";}
00292
00293
00294
00295
00296 private:
00297 RooProfileLL* fProfile;
00298 RooAbsPdf* fPdf;
00299 RooNLLVar* fNll;
00300 const RooArgSet* fCachedBestFitParams;
00301 RooAbsData* fLastData;
00302
00303 Bool_t fOneSided;
00304
00305 protected:
00306 ClassDef(ProfileLikelihoodTestStat,2)
00307 };
00308 }
00309
00310
00311 #endif