Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
SimpleLikelihoodRatioTestStat.cxx
Go to the documentation of this file.
1 // @(#)root/roostats:$Id$
2 // Author: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke
3 /*************************************************************************
4  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
5  * All rights reserved. *
6  * *
7  * For the licensing terms see $ROOTSYS/LICENSE. *
8  * For the list of contributors see $ROOTSYS/README/CREDITS. *
9  *************************************************************************/
10 
11 /** \class RooStats::SimpleLikelihoodRatioTestStat
12  \ingroup Roostats
13 
14 TestStatistic class that returns -log(L[null] / L[alt]) where
15 L is the likelihood.
16 It is often called as the LEP Test statistic.
17 
18 
19 */
20 
22 #include "RooStats/RooStatsUtils.h"
23 
24 Bool_t RooStats::SimpleLikelihoodRatioTestStat::fgAlwaysReuseNll = kTRUE ;
25 
26 ////////////////////////////////////////////////////////////////////////////////
27 
28 void RooStats::SimpleLikelihoodRatioTestStat::SetAlwaysReuseNLL(Bool_t flag) { fgAlwaysReuseNll = flag ; }
29 
30 Double_t RooStats::SimpleLikelihoodRatioTestStat::Evaluate(RooAbsData& data, RooArgSet& nullPOI) {
31 
32  if (fFirstEval && ParamsAreEqual()) {
33  oocoutW(fNullParameters,InputArguments)
34  << "Same RooArgSet used for null and alternate, so you must explicitly SetNullParameters and SetAlternateParameters or the likelihood ratio will always be 1."
35  << std::endl;
36  }
37 
38  // strip pdfs of constraints (which cancel out in the ratio) to avoid unnecessary computations and computational errors
39  if (fFirstEval) {
40  fNullPdf = RooStats::MakeUnconstrainedPdf(*fNullPdf, *fNullPdf->getObservables(data));
41  fAltPdf = RooStats::MakeUnconstrainedPdf(*fAltPdf , *fAltPdf->getObservables(data) );
42  }
43 
44  fFirstEval = false;
45 
46  RooFit::MsgLevel msglevel = RooMsgService::instance().globalKillBelow();
47  RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
48 
49  Bool_t reuse = (fReuseNll || fgAlwaysReuseNll) ;
50 
51  Bool_t created = kFALSE ;
52  if (!fNllNull) {
53  RooArgSet* allParams = fNullPdf->getParameters(data);
54  fNllNull = fNullPdf->createNLL(data, RooFit::CloneData(kFALSE),RooFit::Constrain(*allParams),RooFit::GlobalObservables(fGlobalObs),RooFit::ConditionalObservables(fConditionalObs));
55  delete allParams;
56  created = kTRUE ;
57  }
58  if (reuse && !created) {
59  fNllNull->setData(data, kFALSE) ;
60  }
61 
62  // make sure we set the variables attached to this nll
63  RooArgSet* attachedSet = fNllNull->getVariables();
64  *attachedSet = *fNullParameters;
65  *attachedSet = nullPOI;
66  double nullNLL = fNllNull->getVal();
67 
68  //std::cout << std::endl << "SLRTS: null params:" << std::endl;
69  //attachedSet->Print("v");
70 
71 
72  if (!reuse) {
73  delete fNllNull ; fNllNull = NULL ;
74  }
75  delete attachedSet;
76 
77  created = kFALSE ;
78  if (!fNllAlt) {
79  RooArgSet* allParams = fAltPdf->getParameters(data);
80  fNllAlt = fAltPdf->createNLL(data, RooFit::CloneData(kFALSE),RooFit::Constrain(*allParams),RooFit::GlobalObservables(fGlobalObs),RooFit::ConditionalObservables(fConditionalObs));
81  delete allParams;
82  created = kTRUE ;
83  }
84  if (reuse && !created) {
85  fNllAlt->setData(data, kFALSE) ;
86  }
87  // make sure we set the variables attached to this nll
88  attachedSet = fNllAlt->getVariables();
89  *attachedSet = *fAltParameters;
90  double altNLL = fNllAlt->getVal();
91 
92  //std::cout << std::endl << "SLRTS: alt params:" << std::endl;
93  //attachedSet->Print("v");
94 
95 
96 // std::cout << std::endl << "SLRTS null NLL: " << nullNLL << " alt NLL: " << altNLL << std::endl << std::endl;
97 
98 
99  if (!reuse) {
100  delete fNllAlt ; fNllAlt = NULL ;
101  }
102  delete attachedSet;
103 
104 
105 
106  // save this snapshot
107  if( fDetailedOutputEnabled ) {
108  if( !fDetailedOutput ) {
109  fDetailedOutput = new RooArgSet( *(new RooRealVar("nullNLL","null NLL",0)), "detailedOut_SLRTS" );
110  fDetailedOutput->add( *(new RooRealVar("altNLL","alternate NLL",0)) );
111  }
112  fDetailedOutput->setRealValue( "nullNLL", nullNLL );
113  fDetailedOutput->setRealValue( "altNLL", altNLL );
114 
115 // std::cout << std::endl << "STORING THIS AS DETAILED OUTPUT:" << std::endl;
116 // fDetailedOutput->Print("v");
117 // std::cout << std::endl;
118  }
119 
120 
121  RooMsgService::instance().setGlobalKillBelow(msglevel);
122  return nullNLL - altNLL;
123 }