UpperLimitMCSModule.cxx

Go to the documentation of this file.
00001 // @(#)root/roostats:$Id: UpperLimitMCSModule.cxx 31316 2009-11-19 15:21:34Z moneta $
00002 // Author: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke, Nils Ruthmann
00003 /*************************************************************************
00004  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
00005  * All rights reserved.                                                  *
00006  *                                                                       *
00007  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00008  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00009  *************************************************************************/
00010 
00011 
00012 
00013 
00014 //////////////////////////////////////////////////////////////////////////////
00015 // 
00016 // BEGIN_HTML
00017 // This modules allow to compute in the ToyMcStudy framework the ProfileLikelihood upper limit for each toy-MC sample generated
00018 
00019 // END_HTML
00020 //
00021 //
00022 
00023 #include "Riostream.h"
00024 
00025 #include "RooDataSet.h"
00026 //#include "RooRealVar.h"
00027 #include "TString.h"
00028 //#include "RooFit.h"
00029 #include "RooFitResult.h"
00030 #include "RooStats/UpperLimitMCSModule.h"
00031 #include "RooMsgService.h"
00032 #include "RooStats/ConfInterval.h"
00033 #include "RooStats/PointSetInterval.h"
00034 #include "RooStats/LikelihoodInterval.h"
00035 #include "RooStats/LikelihoodIntervalPlot.h"
00036 #include "RooStats/ProfileLikelihoodCalculator.h"
00037 #include "TCanvas.h"
00038 #include "RooMinuit.h"
00039 #include "RooNLLVar.h"
00040 #include "RooCmdArg.h"
00041 #include "RooRealVar.h"
00042 
00043 
00044 
00045 ClassImp(RooStats::UpperLimitMCSModule);
00046 
00047 
00048 using namespace RooStats ;
00049 
00050 
00051 
00052 //_____________________________________________________________________________
00053 UpperLimitMCSModule::UpperLimitMCSModule(const RooArgSet* poi, Double_t CL) : 
00054   RooAbsMCStudyModule(Form("UpperLimitMCSModule_%s",poi->first()->GetName()),Form("UpperLimitMCSModule_%s",poi->first()->GetName())),
00055   _parName(poi->first()->GetName()), 
00056   _plc(0),_ul(0),_poi(0), _data(0),_cl(CL), _model(0)
00057 {
00058   std::cout<<"RooUpperLimitConstructor ParName:"<<_parName<<std::endl;
00059   std::cout<<"RooUpperLimitConstructor CL:"<<_cl<<std::endl;
00060   // Constructor of module with parameter to be interpreted as nSignal and the value of the
00061   // null hypothesis for nSignal (usually zero)
00062 }
00063 
00064 
00065 
00066 //_____________________________________________________________________________
00067 UpperLimitMCSModule::UpperLimitMCSModule(const UpperLimitMCSModule& other) : 
00068   RooAbsMCStudyModule(other), 
00069   _parName(other._poi->first()->GetName()),
00070   _plc(0),_ul(0),_poi(other._poi), _data(0), _cl(other._cl), _model(other._model)
00071 {
00072   // Copy constructor
00073 }
00074 
00075 
00076 
00077 //_____________________________________________________________________________
00078 UpperLimitMCSModule:: ~UpperLimitMCSModule() 
00079 {
00080   // Destructor
00081 
00082  
00083   if (_plc) {
00084     delete _plc ;
00085   }
00086   if (_data) {
00087     delete _data ;
00088   }
00089   if(_ul){
00090     delete _ul;
00091   }
00092   if(_poi){
00093      delete _poi;
00094   }
00095   if (_model){
00096     delete _model;
00097   }
00098 }
00099 
00100 
00101 
00102 //_____________________________________________________________________________
00103 Bool_t UpperLimitMCSModule::initializeInstance()
00104 {
00105   // Initialize module after attachment to RooMCStudy object
00106 
00107   // Check that parameter is also present in fit parameter list of RooMCStudy object
00108   if (!fitParams()->find(_parName.c_str())) {
00109     coutE(InputArguments) << "UpperLimitMCSModule::initializeInstance:: ERROR: No parameter named " << _parName << " in RooMCStudy!" << endl ;
00110     return kFALSE ;
00111   }
00112   
00113   //Construct the ProfileLikelihoodCalculator
00114   _poi=new RooArgSet(*(fitParams()->find(_parName.c_str())));
00115   std::cout<<"RooUpperLimit Initialize Instance: POI Set:"<<std::endl;
00116   _poi->Print("v");
00117   std::cout<<"RooUpperLimit Initialize Instance: End:"<<std::endl;
00118 
00119 
00120   
00121   TString ulName = Form("ul_%s",_parName.c_str()) ;
00122   TString ulTitle = Form("UL for parameter %s",_parName.c_str()) ;
00123   _ul = new RooRealVar(ulName.Data(),ulTitle.Data(),0) ;
00124 
00125 
00126   // Create new dataset to be merged with RooMCStudy::fitParDataSet
00127   _data = new RooDataSet("ULSigData","Additional data for UL study",RooArgSet(*_ul)) ;
00128 
00129   return kTRUE ;
00130 }
00131 
00132 
00133 
00134 //_____________________________________________________________________________
00135 Bool_t UpperLimitMCSModule::initializeRun(Int_t /*numSamples*/) 
00136 {
00137   // Initialize module at beginning of RooCMStudy run
00138 
00139   _data->reset() ;
00140   return kTRUE ;
00141 }
00142 
00143 
00144 
00145 //_____________________________________________________________________________
00146 RooDataSet* UpperLimitMCSModule::finalizeRun() 
00147 {
00148   // Return auxiliary dataset with results of delta(-log(L))
00149   // calculations of this module so that it is merged with
00150   // RooMCStudy::fitParDataSet() by RooMCStudy
00151 
00152   return _data ;
00153 }
00154 
00155 
00156 
00157 //_____________________________________________________________________________
00158 
00159 // Bool_t UpperLimitMCSModule::processAfterFit(Int_t /*sampleNum*/)  
00160 // {
00161 //   // Save likelihood from nominal fit, fix chosen parameter to its
00162 //   // null hypothesis value and rerun fit Save difference in likelihood
00163 //   // and associated Gaussian significance in auxilary dataset
00164 
00165 //   RooRealVar* par = static_cast<RooRealVar*>(fitParams()->find(_parName.c_str())) ;
00166 //   par->setVal(_nullValue) ;
00167 //   par->setConstant(kTRUE) ;
00168 //   RooFitResult* frnull = refit() ;
00169 //   par->setConstant(kFALSE) ;
00170   
00171 //   _nll0h->setVal(frnull->minNll()) ;
00172 
00173 //   Double_t deltaLL = (frnull->minNll() - nllVar()->getVal()) ;
00174 //   Double_t signif = deltaLL>0 ? sqrt(2*deltaLL) : -sqrt(-2*deltaLL) ;
00175 //   _sig0h->setVal(signif) ;
00176 //   _dll0h->setVal(deltaLL) ;
00177 
00178 
00179 //   _data->add(RooArgSet(*_nll0h,*_dll0h,*_sig0h)) ;
00180 
00181 //   delete frnull ;
00182 //   return kTRUE ;
00183 
00184 // }
00185 
00186 
00187 //_____________________________________________________________________________
00188 Bool_t UpperLimitMCSModule::processBetweenGenAndFit(Int_t /*sampleNum*/) {
00189 
00190   std::cout<<"after generation Test"<<std::endl;
00191 
00192 
00193   static_cast<RooRealVar*>(_poi->first())->setVal(static_cast<RooRealVar*>(fitInitParams()->find(_parName.c_str()))->getVal());
00194   
00195   //_poi->first()->Print();
00196   static_cast<RooRealVar*>(_poi->first())->setBins(1000);
00197   //fitModel()->Print("v");
00198   std::cout<<"generated Entries:"<<genSample()->numEntries()<<std::endl;
00199 
00200   RooStats::ProfileLikelihoodCalculator plc( *(genSample()), *(fitModel()), *_poi);
00201    
00202   //PLC calculates intervals. for one sided ul multiply testsize by two
00203   plc.SetTestSize(2*(1-_cl));
00204   RooStats::ConfInterval* pllint=plc.GetInterval();
00205 
00206 
00207   std::cout<<"poi value: "<<((RooRealVar*)( _poi->first()))->getVal()<<std::endl;
00208   std::cout<<(static_cast<RooRealVar*>((fitParams()->find(_parName.c_str()))))->getVal()<<std::endl;
00209   std::cout<<((RooStats::LikelihoodInterval*)pllint)->UpperLimit((RooRealVar&)*(_poi->first()))<<std::endl;
00210 
00211 
00212   //Go to the fit Value for zour POI to make sure upperlimit works correct.
00213   //fitModel()->fitTo(*genSample());
00214   
00215 
00216 
00217   _ul->setVal(((RooStats::LikelihoodInterval*)pllint)->UpperLimit(static_cast<RooRealVar&>(*(fitParams()->find(_parName.c_str())))));
00218   
00219   _data->add(RooArgSet(*_ul));
00220   std::cout<<"UL:"<<_ul->getVal()<<std::endl;
00221 //   if (_ul->getVal()<1){
00222     
00223 //   RooStats::LikelihoodIntervalPlot plotpll((RooStats::LikelihoodInterval*) pllint);
00224 //   TCanvas c1;
00225 //   plotpll.Draw();
00226 //   c1.Print("test.ps");
00227 //   std::cout<<" UL<1 whats going on here?"<<std::endl;
00228 //   abort();
00229 //   }
00230   
00231   delete pllint;
00232   
00233   
00234   return kTRUE;
00235 }

Generated on Tue Jul 5 15:14:37 2011 for ROOT_528-00b_version by  doxygen 1.5.1