// @(#)root/roostats:$Id$
// Author: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke
/*************************************************************************
 * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "RooStats/RatioOfProfiledLikelihoodsTestStat.h"

#include "RooArgSet.h"
#include "RooAbsData.h"
#include "TMath.h"
#include "RooMsgService.h"
#include "RooGlobalFunc.h"


Bool_t RooStats::RatioOfProfiledLikelihoodsTestStat::fgAlwaysReuseNll = kTRUE ;

void RooStats::RatioOfProfiledLikelihoodsTestStat::SetAlwaysReuseNLL(Bool_t flag) { fgAlwaysReuseNll = flag ; }

    //__________________________________________
Double_t RooStats::RatioOfProfiledLikelihoodsTestStat::ProfiledLikelihood(RooAbsData& data, RooArgSet& poi, RooAbsPdf& pdf) {
   // returns -logL(poi, conditonal MLE of nuisance params)
   // subtract off the global MLE or not depending on the option
   // It is the numerator or the denominator of the ratio (depending on the pdf)
   //L.M. : not sure why this method is needed now

   int type = (fSubtractMLE) ? 0 : 2; 
       
   // null
   if ( &pdf == fNullProfile.GetPdf() )
      return fNullProfile.EvaluateProfileLikelihood(type, data, poi);
   else if (&pdf == fAltProfile.GetPdf() )
      return fAltProfile.EvaluateProfileLikelihood(type, data, poi);
   
   oocoutE((TObject*)NULL,InputArguments) << "RatioOfProfiledLikelihoods::ProfileLikelihood - invalid pdf used for computing the profiled likelihood - return NaN" 
                         << std::endl;

   return TMath::QuietNaN(); 
      
}
    
//__________________________________________
Double_t  RooStats::RatioOfProfiledLikelihoodsTestStat::Evaluate(RooAbsData& data, RooArgSet& nullParamsOfInterest) {
   // evaluate the ratio of profile likelihood
   
   
   int type = (fSubtractMLE) ? 0 : 2; 
       
   // null
   double nullNLL = fNullProfile.EvaluateProfileLikelihood(type, data, nullParamsOfInterest);
   const RooArgSet *nullset = fNullProfile.GetDetailedOutput();
   
   // alt 
   double altNLL = fAltProfile.EvaluateProfileLikelihood(type, data, *fAltPOI);
   const RooArgSet *altset = fAltProfile.GetDetailedOutput();
   
   if (fDetailedOutput != NULL) {
      delete fDetailedOutput;
      fDetailedOutput = NULL;
   }
   if (fDetailedOutputEnabled) {
      fDetailedOutput = new RooArgSet();
      RooRealVar* var(0);
      for(TIterator *it = nullset->createIterator();(var = dynamic_cast<RooRealVar*>(it->Next()));) {
         RooRealVar* cloneVar = new RooRealVar(TString::Format("nullprof_%s", var->GetName()),
                                               TString::Format("%s for null", var->GetTitle()), var->getVal());
         fDetailedOutput->addOwned(*cloneVar);
      }
      for(TIterator *it = altset->createIterator();(var = dynamic_cast<RooRealVar*>(it->Next()));) {
         RooRealVar* cloneVar = new RooRealVar(TString::Format("altprof_%s", var->GetName()),
                                               TString::Format("%s for null", var->GetTitle()), var->getVal());
         fDetailedOutput->addOwned(*cloneVar);
      }
   }
   
/*
// set variables back to where they were
nullParamsOfInterest = *saveNullPOI;
*allVars = *saveAll;
delete saveAll;
delete allVars;
*/

   return nullNLL -altNLL;
}
 RatioOfProfiledLikelihoodsTestStat.cxx:1
 RatioOfProfiledLikelihoodsTestStat.cxx:2
 RatioOfProfiledLikelihoodsTestStat.cxx:3
 RatioOfProfiledLikelihoodsTestStat.cxx:4
 RatioOfProfiledLikelihoodsTestStat.cxx:5
 RatioOfProfiledLikelihoodsTestStat.cxx:6
 RatioOfProfiledLikelihoodsTestStat.cxx:7
 RatioOfProfiledLikelihoodsTestStat.cxx:8
 RatioOfProfiledLikelihoodsTestStat.cxx:9
 RatioOfProfiledLikelihoodsTestStat.cxx:10
 RatioOfProfiledLikelihoodsTestStat.cxx:11
 RatioOfProfiledLikelihoodsTestStat.cxx:12
 RatioOfProfiledLikelihoodsTestStat.cxx:13
 RatioOfProfiledLikelihoodsTestStat.cxx:14
 RatioOfProfiledLikelihoodsTestStat.cxx:15
 RatioOfProfiledLikelihoodsTestStat.cxx:16
 RatioOfProfiledLikelihoodsTestStat.cxx:17
 RatioOfProfiledLikelihoodsTestStat.cxx:18
 RatioOfProfiledLikelihoodsTestStat.cxx:19
 RatioOfProfiledLikelihoodsTestStat.cxx:20
 RatioOfProfiledLikelihoodsTestStat.cxx:21
 RatioOfProfiledLikelihoodsTestStat.cxx:22
 RatioOfProfiledLikelihoodsTestStat.cxx:23
 RatioOfProfiledLikelihoodsTestStat.cxx:24
 RatioOfProfiledLikelihoodsTestStat.cxx:25
 RatioOfProfiledLikelihoodsTestStat.cxx:26
 RatioOfProfiledLikelihoodsTestStat.cxx:27
 RatioOfProfiledLikelihoodsTestStat.cxx:28
 RatioOfProfiledLikelihoodsTestStat.cxx:29
 RatioOfProfiledLikelihoodsTestStat.cxx:30
 RatioOfProfiledLikelihoodsTestStat.cxx:31
 RatioOfProfiledLikelihoodsTestStat.cxx:32
 RatioOfProfiledLikelihoodsTestStat.cxx:33
 RatioOfProfiledLikelihoodsTestStat.cxx:34
 RatioOfProfiledLikelihoodsTestStat.cxx:35
 RatioOfProfiledLikelihoodsTestStat.cxx:36
 RatioOfProfiledLikelihoodsTestStat.cxx:37
 RatioOfProfiledLikelihoodsTestStat.cxx:38
 RatioOfProfiledLikelihoodsTestStat.cxx:39
 RatioOfProfiledLikelihoodsTestStat.cxx:40
 RatioOfProfiledLikelihoodsTestStat.cxx:41
 RatioOfProfiledLikelihoodsTestStat.cxx:42
 RatioOfProfiledLikelihoodsTestStat.cxx:43
 RatioOfProfiledLikelihoodsTestStat.cxx:44
 RatioOfProfiledLikelihoodsTestStat.cxx:45
 RatioOfProfiledLikelihoodsTestStat.cxx:46
 RatioOfProfiledLikelihoodsTestStat.cxx:47
 RatioOfProfiledLikelihoodsTestStat.cxx:48
 RatioOfProfiledLikelihoodsTestStat.cxx:49
 RatioOfProfiledLikelihoodsTestStat.cxx:50
 RatioOfProfiledLikelihoodsTestStat.cxx:51
 RatioOfProfiledLikelihoodsTestStat.cxx:52
 RatioOfProfiledLikelihoodsTestStat.cxx:53
 RatioOfProfiledLikelihoodsTestStat.cxx:54
 RatioOfProfiledLikelihoodsTestStat.cxx:55
 RatioOfProfiledLikelihoodsTestStat.cxx:56
 RatioOfProfiledLikelihoodsTestStat.cxx:57
 RatioOfProfiledLikelihoodsTestStat.cxx:58
 RatioOfProfiledLikelihoodsTestStat.cxx:59
 RatioOfProfiledLikelihoodsTestStat.cxx:60
 RatioOfProfiledLikelihoodsTestStat.cxx:61
 RatioOfProfiledLikelihoodsTestStat.cxx:62
 RatioOfProfiledLikelihoodsTestStat.cxx:63
 RatioOfProfiledLikelihoodsTestStat.cxx:64
 RatioOfProfiledLikelihoodsTestStat.cxx:65
 RatioOfProfiledLikelihoodsTestStat.cxx:66
 RatioOfProfiledLikelihoodsTestStat.cxx:67
 RatioOfProfiledLikelihoodsTestStat.cxx:68
 RatioOfProfiledLikelihoodsTestStat.cxx:69
 RatioOfProfiledLikelihoodsTestStat.cxx:70
 RatioOfProfiledLikelihoodsTestStat.cxx:71
 RatioOfProfiledLikelihoodsTestStat.cxx:72
 RatioOfProfiledLikelihoodsTestStat.cxx:73
 RatioOfProfiledLikelihoodsTestStat.cxx:74
 RatioOfProfiledLikelihoodsTestStat.cxx:75
 RatioOfProfiledLikelihoodsTestStat.cxx:76
 RatioOfProfiledLikelihoodsTestStat.cxx:77
 RatioOfProfiledLikelihoodsTestStat.cxx:78
 RatioOfProfiledLikelihoodsTestStat.cxx:79
 RatioOfProfiledLikelihoodsTestStat.cxx:80
 RatioOfProfiledLikelihoodsTestStat.cxx:81
 RatioOfProfiledLikelihoodsTestStat.cxx:82
 RatioOfProfiledLikelihoodsTestStat.cxx:83
 RatioOfProfiledLikelihoodsTestStat.cxx:84
 RatioOfProfiledLikelihoodsTestStat.cxx:85
 RatioOfProfiledLikelihoodsTestStat.cxx:86
 RatioOfProfiledLikelihoodsTestStat.cxx:87
 RatioOfProfiledLikelihoodsTestStat.cxx:88
 RatioOfProfiledLikelihoodsTestStat.cxx:89