CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Types | Public Member Functions | Private Member Functions | Private Attributes
ProfiledLikelihoodTestStatOpt Class Reference

#include <ProfiledLikelihoodRatioTestStatExt.h>

Inheritance diagram for ProfiledLikelihoodTestStatOpt:

Public Types

enum  OneSidedness { twoSidedDef = 0, oneSidedDef = 1, signFlipDef = 2 }
 

Public Member Functions

virtual Double_t Evaluate (RooAbsData &data, RooArgSet &nullPOI)
 
virtual std::vector< Double_t > Evaluate (RooAbsData &data, RooArgSet &nullPOI, const std::vector< Double_t > &rVals)
 
virtual const TString GetVarName () const
 
 ProfiledLikelihoodTestStatOpt (const RooArgSet &observables, RooAbsPdf &pdf, const RooArgSet *nuisances, const RooArgSet &params, const RooArgSet &poi, const RooArgList &gobsParams, const RooArgList &gobs, int verbosity=0, OneSidedness oneSided=oneSidedDef)
 
void SetOneSided (OneSidedness oneSided)
 
void setPrintLevel (Int_t level)
 

Private Member Functions

bool createNLL (RooAbsPdf &pdf, RooAbsData &data)
 
double minNLL (bool constrained, RooRealVar *r=0)
 

Private Attributes

RooArgList gobs_
 
RooArgList gobsParams_
 
std::auto_ptr< RooAbsReal > nll_
 
RooArgSet nuisances_
 
OneSidedness oneSided_
 
std::auto_ptr< RooArgSet > params_
 
RooAbsPdf * pdf_
 
RooArgSet poi_
 
RooArgSet poiParams_
 
RooArgSet snap_
 
Int_t verbosity_
 

Detailed Description

Definition at line 49 of file ProfiledLikelihoodRatioTestStatExt.h.

Member Enumeration Documentation

Constructor & Destructor Documentation

ProfiledLikelihoodTestStatOpt::ProfiledLikelihoodTestStatOpt ( const RooArgSet &  observables,
RooAbsPdf &  pdf,
const RooArgSet *  nuisances,
const RooArgSet &  params,
const RooArgSet &  poi,
const RooArgList &  gobsParams,
const RooArgList &  gobs,
int  verbosity = 0,
OneSidedness  oneSided = oneSidedDef 
)

Definition at line 117 of file ProfiledLikelihoodRatioTestStatExt.cc.

References a, dtNoiseDBValidation_cfg::cerr, gather_cfg::cout, DBG, nuisances_, params_, pdf_, poi_, poiParams_, createTree::pp, and snap_.

127 :
128  pdf_(&pdf),
129  gobsParams_(gobsParams),
130  gobs_(gobs),
132  oneSided_(oneSided)
133 {
134  DBG(DBG_PLTestStat_main, (std::cout << "Created for " << pdf.GetName() << "." << std::endl))
135 
136  params.snapshot(snap_,false);
137  ((RooRealVar*)snap_.find(params.first()->GetName()))->setConstant(false);
138  if (nuisances) { nuisances_.add(*nuisances); snap_.addClone(*nuisances, /*silent=*/true); }
139  params_.reset(pdf_->getParameters(observables));
140  DBG(DBG_PLTestStat_ctor, (std::cout << "Observables: " << std::endl)) DBG(DBG_PLTestStat_ctor, (observables.Print("V")))
141  DBG(DBG_PLTestStat_ctor, (std::cout << "All params: " << std::endl)) DBG(DBG_PLTestStat_ctor, (params_->Print("V")))
142  DBG(DBG_PLTestStat_ctor, (std::cout << "Snapshot: " << std::endl)) DBG(DBG_PLTestStat_ctor, (snap_.Print("V")))
143  DBG(DBG_PLTestStat_ctor, (std::cout << "POI: " << std::endl)) DBG(DBG_PLTestStat_ctor, (poi_.Print("V")))
144  RooLinkedListIter it = poi.iterator();
145  for (RooAbsArg *a = (RooAbsArg*) it.Next(); a != 0; a = (RooAbsArg*) it.Next()) {
146  // search for this poi in the parameters and in the snapshot
147  RooAbsArg *ps = snap_.find(a->GetName());
148  RooAbsArg *pp = params_->find(a->GetName());
149  if (pp == 0) { std::cerr << "WARNING: NLL does not depend on POI " << a->GetName() << ", cannot profile" << std::endl; continue; }
150  if (ps == 0) { std::cerr << "WARNING: no snapshot for POI " << a->GetName() << ", cannot profile" << std::endl; continue; }
151  poi_.add(*ps);
152  poiParams_.add(*pp);
153  }
154 }
tuple pp
Definition: createTree.py:15
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
bool first
Definition: L1TdeRCT.cc:94
double a
Definition: hdecay.h:121
perl if(1 lt scalar(@::datatypes))
Definition: edlooper.cc:31
#define DBG(X, Z)
tuple cout
Definition: gather_cfg.py:121

Member Function Documentation

bool ProfiledLikelihoodTestStatOpt::createNLL ( RooAbsPdf &  pdf,
RooAbsData &  data 
)
private

Definition at line 362 of file ProfiledLikelihoodRatioTestStatExt.cc.

References nll_, and nuisances_.

Referenced by Evaluate().

363 {
364  if (typeid(pdf) == typeid(RooSimultaneousOpt)) {
365  if (nll_.get() == 0) nll_.reset(pdf.createNLL(data, RooFit::Constrain(nuisances_)));
366  else ((cacheutils::CachingSimNLL&)(*nll_)).setData(data);
367  return true;
368  } else {
369  nll_.reset(pdf.createNLL(data, RooFit::Constrain(nuisances_)));
370  return false;
371  }
372 }
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
Double_t ProfiledLikelihoodTestStatOpt::Evaluate ( RooAbsData &  data,
RooArgSet &  nullPOI 
)
virtual

Definition at line 157 of file ProfiledLikelihoodRatioTestStatExt.cc.

References gather_cfg::cout, createNLL(), DBG, runtimedef::get(), gobs_, gobsParams_, i, minNLL(), nll_, nuisances_, oneSided_, oneSidedDef, params_, pdf_, poi_, poiParams_, alignCSCRings::r, utils::setAllConstant(), signFlipDef, snap_, and std::swap().

Referenced by ProfileLikelihood::runSignificance().

158 {
159  bool do_debug = runtimedef::get("DEBUG_PLTSO");
160  //static bool do_rescan = runtimedef::get("PLTSO_FULL_RESCAN");
161  DBG(DBG_PLTestStat_main, (std::cout << "Being evaluated on " << data.GetName() << std::endl))
162 
163  // Take snapshot of initial state, to restore it at the end
164  RooArgSet initialState; params_->snapshot(initialState);
165 
166  DBG(DBG_PLTestStat_pars, std::cout << "Being evaluated on " << data.GetName() << ": params before snapshot are " << std::endl)
167  DBG(DBG_PLTestStat_pars, params_->Print("V"))
168  // Initialize parameters
169  *params_ = snap_;
170 
171  //set value of "globalConstrained" nuisances to the value of the corresponding global observable and set them constant
172  for (int i=0; i<gobsParams_.getSize(); ++i) {
173  RooRealVar *nuis = (RooRealVar*)gobsParams_.at(i);
174  RooRealVar *gobs = (RooRealVar*)gobs_.at(i);
175  nuis->setVal(gobs->getVal());
176  nuis->setConstant();
177  }
178 
179  DBG(DBG_PLTestStat_pars, std::cout << "Being evaluated on " << data.GetName() << ": params after snapshot are " << std::endl)
180  DBG(DBG_PLTestStat_pars, params_->Print("V"))
181  // Initialize signal strength (or the first parameter)
182  RooRealVar *rIn = (RooRealVar *) poi_.first();
183  RooRealVar *r = (RooRealVar *) params_->find(rIn->GetName());
184  bool canKeepNLL = createNLL(*pdf_, data);
185 
186  double initialR = rIn->getVal();
187 
188  // Perform unconstrained minimization (denominator)
189  if (poi_.getSize() == 1) {
190  r->setMin(0); if (initialR == 0 || (oneSided_ != oneSidedDef)) r->removeMax(); else r->setMax(1.1*initialR);
191  r->setVal(initialR == 0 ? 0.5 : 0.5*initialR); //best guess
192  r->setConstant(false);
193  } else {
195  }
196  DBG(DBG_PLTestStat_pars, (std::cout << "r In: ")) DBG(DBG_PLTestStat_pars, (rIn->Print(""))) DBG(DBG_PLTestStat_pars, std::cout << std::endl)
197  DBG(DBG_PLTestStat_pars, std::cout << "r before the fit: ") DBG(DBG_PLTestStat_pars, r->Print("")) DBG(DBG_PLTestStat_pars, std::cout << std::endl)
198 
199  //std::cout << "PERFORMING UNCONSTRAINED FIT " << r->GetName() << " [ " << r->getMin() << " - " << r->getMax() << " ] "<< std::endl;
200  double nullNLL = minNLL(/*constrained=*/false, r);
201  double bestFitR = r->getVal();
202 
203  DBG(DBG_PLTestStat_pars, (std::cout << "r after the fit: ")) DBG(DBG_PLTestStat_pars, (r->Print(""))) DBG(DBG_PLTestStat_pars, std::cout << std::endl)
204  DBG(DBG_PLTestStat_pars, std::cout << "Was evaluated on " << data.GetName() << ": params before snapshot are " << std::endl)
205  DBG(DBG_PLTestStat_pars, params_->Print("V"))
206 
207  // Prepare for constrained minimization (numerator)
208  if (poi_.getSize() == 1) {
209  r->setVal(initialR);
210  r->setConstant(true);
211  } else {
212  poiParams_.assignValueOnly(poi_);
214  }
215  double thisNLL = nullNLL;
216  if (initialR == 0 || oneSided_ != oneSidedDef || bestFitR < initialR) {
217  // must do constrained fit (if there's something to fit besides XS)
218  //std::cout << "PERFORMING CONSTRAINED FIT " << r->GetName() << " == " << r->getVal() << std::endl;
219  thisNLL = (nuisances_.getSize() > 0 ? minNLL(/*constrained=*/true, r) : nll_->getVal());
220  if (thisNLL - nullNLL < -0.02) {
221  DBG(DBG_PLTestStat_main, (printf(" --> constrained fit is better... will repeat unconstrained fit\n")))
222  utils::setAllConstant(poiParams_,false);
223  nullNLL = minNLL(/*constrained=*/false, r);
224  bestFitR = r->getVal();
225  if (bestFitR > initialR && oneSided_ == oneSidedDef) {
226  DBG(DBG_PLTestStat_main, (printf(" after re-fit, signal %7.4f > %7.4f, test statistics will be zero.\n", bestFitR, initialR)))
227  thisNLL = nullNLL;
228  }
229  }
230  /* This piece of debug code was added to see if we were finding a local minimum at zero instead of the global minimum.
231  It doesn't seem to be the case, however
232  if (do_rescan && fabs(thisNLL - nullNLL) < 0.2 && initialR == 0) {
233  if (do_debug) printf("Doing full rescan. best fit r = %.4f, -lnQ = %.5f\n", bestFitR, thisNLL-nullNLL);
234  for (double rx = 0; rx <= 5; rx += 0.01) {
235  r->setVal(rx); double y = nll_->getVal();
236  if (y < nullNLL) {
237  printf("%4.2f\t%8.5f\t<<==== ALERT\n", rx, y - nullNLL);
238  } else {
239  printf("%4.2f\t%8.5f\n", rx, y - nullNLL);
240  }
241  }
242  } */
243  if (bestFitR > initialR && oneSided_ == signFlipDef) {
244  DBG(DBG_PLTestStat_main, (printf(" fitted signal %7.4f > %7.4f, test statistics will be negative.\n", bestFitR, initialR)))
245  std::swap(thisNLL, nullNLL);
246  }
247  } else {
248  DBG(DBG_PLTestStat_main, (printf(" signal fit %7.4f > %7.4f: don't need to compute numerator\n", bestFitR, initialR)))
249  }
250 
251  //Restore initial state, to avoid issues with ToyMCSampler
252  *params_ = initialState;
253 
254  if (!canKeepNLL) nll_.reset();
255 
256  DBG(DBG_PLTestStat_main, (printf("\nNLLs: num % 10.4f, den % 10.4f (signal %7.4f), test stat % 10.4f\n", thisNLL, nullNLL, bestFitR, thisNLL-nullNLL)))
257  if (do_debug) printf("\nNLLs: num % 10.4f, den % 10.4f (signal %7.4f), test stat % 10.4f\n", thisNLL, nullNLL, bestFitR, thisNLL-nullNLL);
258  //return std::min(thisNLL-nullNLL, 0.);
259  return thisNLL-nullNLL;
260 }
void swap(ora::Record &rh, ora::Record &lh)
Definition: Record.h:70
int i
Definition: DBlmapReader.cc:9
bool setAllConstant(const RooAbsCollection &coll, bool constant=true)
set all RooRealVars to constants. return true if at least one changed status
Definition: utils.cc:248
int get(const char *name)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
double minNLL(bool constrained, RooRealVar *r=0)
double f[11][100]
bool first
Definition: L1TdeRCT.cc:94
long long int num
Definition: procUtils.cc:71
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
perl if(1 lt scalar(@::datatypes))
Definition: edlooper.cc:31
#define DBG(X, Z)
tuple cout
Definition: gather_cfg.py:121
bool createNLL(RooAbsPdf &pdf, RooAbsData &data)
std::vector< Double_t > ProfiledLikelihoodTestStatOpt::Evaluate ( RooAbsData &  data,
RooArgSet &  nullPOI,
const std::vector< Double_t > &  rVals 
)
virtual

Definition at line 262 of file ProfiledLikelihoodRatioTestStatExt.cc.

References gather_cfg::cout, createNLL(), DBG, EPS, runtimedef::get(), gobs_, gobsParams_, i, minNLL(), nll_, nuisances_, oneSided_, oneSidedDef, params_, pdf_, poi_, alignCSCRings::r, run_regression::ret, signFlipDef, and snap_.

263 {
264  static bool do_debug = runtimedef::get("DEBUG_PLTSO");
265  std::vector<Double_t> ret(rVals.size(), 0);
266 
267  DBG(DBG_PLTestStat_main, (std::cout << "Being evaluated on " << data.GetName() << std::endl))
268 
269  // Take snapshot of initial state, to restore it at the end
270  RooArgSet initialState; params_->snapshot(initialState);
271 
272  DBG(DBG_PLTestStat_pars, std::cout << "Being evaluated on " << data.GetName() << ": params before snapshot are " << std::endl)
273  DBG(DBG_PLTestStat_pars, params_->Print("V"))
274  // Initialize parameters
275  *params_ = snap_;
276 
277  //set value of "globalConstrained" nuisances to the value of the corresponding global observable and set them constant
278  for (int i=0; i<gobsParams_.getSize(); ++i) {
279  RooRealVar *nuis = (RooRealVar*)gobsParams_.at(i);
280  RooRealVar *gobs = (RooRealVar*)gobs_.at(i);
281  nuis->setVal(gobs->getVal());
282  nuis->setConstant();
283  }
284 
285  DBG(DBG_PLTestStat_pars, std::cout << "Being evaluated on " << data.GetName() << ": params after snapshot are " << std::endl)
286  DBG(DBG_PLTestStat_pars, params_->Print("V"))
287  // Initialize signal strength
288  RooRealVar *rIn = (RooRealVar *) poi_.first();
289  RooRealVar *r = (RooRealVar *) params_->find(rIn->GetName());
290  bool canKeepNLL = createNLL(*pdf_, data);
291 
292  double initialR = rVals.back();
293 
294  // Perform unconstrained minimization (denominator)
295  r->setMin(0); if (initialR == 0 || (oneSided_ != oneSidedDef)) r->removeMax(); else r->setMax(1.1*initialR);
296  r->setVal(initialR == 0 ? 0.5 : 0.5*initialR); //best guess
297  r->setConstant(false);
298  DBG(DBG_PLTestStat_pars, (std::cout << "r In: ")) DBG(DBG_PLTestStat_pars, (rIn->Print(""))) DBG(DBG_PLTestStat_pars, std::cout << std::endl)
299  DBG(DBG_PLTestStat_pars, std::cout << "r before the fit: ") DBG(DBG_PLTestStat_pars, r->Print("")) DBG(DBG_PLTestStat_pars, std::cout << std::endl)
300 
301  if (do_debug) std::cout << "PERFORMING UNCONSTRAINED FIT " << r->GetName() << " [ " << r->getMin() << " - " << r->getVal() << " - " << r->getMax() << " ] "<< std::endl;
302  double nullNLL = minNLL(/*constrained=*/false, r);
303  double bestFitR = r->getVal();
304  // Take snapshot of initial state, to restore it at the end
305  RooArgSet bestFitState; params_->snapshot(bestFitState);
306 
307 
308  DBG(DBG_PLTestStat_pars, (std::cout << "r after the fit: ")) DBG(DBG_PLTestStat_pars, (r->Print(""))) DBG(DBG_PLTestStat_pars, std::cout << std::endl)
309  DBG(DBG_PLTestStat_pars, std::cout << "Was evaluated on " << data.GetName() << ": params before snapshot are " << std::endl)
310  DBG(DBG_PLTestStat_pars, params_->Print("V"))
311 
312  double EPS = 0.25*ROOT::Math::MinimizerOptions::DefaultTolerance();
313  for (int iR = 0, nR = rVals.size(); iR < nR; ++iR) {
314  if (fabs(ret[iR]) > 10*EPS) continue; // don't bother re-update points which were too far from zero anyway.
315  *params_ = bestFitState;
316  initialR = rVals[iR];
317  // Prepare for constrained minimization (numerator)
318  r->setVal(initialR);
319  r->setConstant(true);
320  double thisNLL = nullNLL, sign = +1.0;
321  if (initialR == 0 || oneSided_ != oneSidedDef || bestFitR < initialR) {
322  // must do constrained fit (if there's something to fit besides XS)
323  //std::cout << "PERFORMING CONSTRAINED FIT " << r->GetName() << " == " << r->getVal() << std::endl;
324  thisNLL = (nuisances_.getSize() > 0 ? minNLL(/*constrained=*/true, r) : nll_->getVal());
325  if (thisNLL - nullNLL < 0 && thisNLL - nullNLL >= -EPS) {
326  thisNLL = nullNLL;
327  } else if (thisNLL - nullNLL < 0) {
328  DBG(DBG_PLTestStat_main, (printf(" --> constrained fit is better... will repeat unconstrained fit\n")))
329  r->setConstant(false);
330  r->setVal(bestFitR);
331  double oldNullNLL = nullNLL;
332  nullNLL = minNLL(/*constrained=*/false, r);
333  bestFitR = r->getVal();
334  bestFitState.removeAll(); params_->snapshot(bestFitState);
335  for (int iR2 = 0; iR2 < iR; ++iR2) {
336  ret[iR2] -= (nullNLL - oldNullNLL); // fixup already computed test statistics
337  }
338  iR = -1; continue; // restart over again, refitting those close to zero :-(
339  }
340  if (bestFitR > initialR && oneSided_ == signFlipDef) {
341  DBG(DBG_PLTestStat_main, (printf(" fitted signal %7.4f > %7.4f, test statistics will be negative.\n", bestFitR, initialR)))
342  sign = -1.0;
343  }
344  } else {
345  DBG(DBG_PLTestStat_main, (printf(" signal fit %7.4f > %7.4f: don't need to compute numerator\n", bestFitR, initialR)))
346  }
347 
348  ret[iR] = sign * (thisNLL-nullNLL);
349  DBG(DBG_PLTestStat_main, (printf("\nNLLs for %7.4f: num % 10.4f, den % 10.4f (signal %7.4f), test stat % 10.4f\n", initialR, thisNLL, nullNLL, bestFitR, ret[iR])))
350  if (do_debug) {
351  printf(" Q(%.4f) = %.4f (best fit signal %.4f), from num %.4f, den %.4f\n", rVals[iR], ret[iR], bestFitR, thisNLL, nullNLL); fflush(stdout);
352  }
353  }
354  //Restore initial state, to avoid issues with ToyMCSampler
355  *params_ = initialState;
356 
357  if (!canKeepNLL) nll_.reset();
358 
359  //return std::min(thisNLL-nullNLL, 0.);
360  return ret;
361 }
int i
Definition: DBlmapReader.cc:9
int get(const char *name)
#define EPS
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
double minNLL(bool constrained, RooRealVar *r=0)
bool first
Definition: L1TdeRCT.cc:94
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
perl if(1 lt scalar(@::datatypes))
Definition: edlooper.cc:31
#define DBG(X, Z)
tuple cout
Definition: gather_cfg.py:121
bool createNLL(RooAbsPdf &pdf, RooAbsData &data)
tuple size
Write out results.
virtual const TString ProfiledLikelihoodTestStatOpt::GetVarName ( ) const
inlinevirtual

Definition at line 61 of file ProfiledLikelihoodRatioTestStatExt.h.

61 { return "- log (#lambda)"; }
double ProfiledLikelihoodTestStatOpt::minNLL ( bool  constrained,
RooRealVar *  r = 0 
)
private
void ProfiledLikelihoodTestStatOpt::SetOneSided ( OneSidedness  oneSided)
inline

Definition at line 66 of file ProfiledLikelihoodRatioTestStatExt.h.

References oneSided_.

void ProfiledLikelihoodTestStatOpt::setPrintLevel ( Int_t  level)
inline

Member Data Documentation

RooArgList ProfiledLikelihoodTestStatOpt::gobs_
private

Definition at line 75 of file ProfiledLikelihoodRatioTestStatExt.h.

Referenced by Evaluate().

RooArgList ProfiledLikelihoodTestStatOpt::gobsParams_
private

Definition at line 75 of file ProfiledLikelihoodRatioTestStatExt.h.

Referenced by Evaluate().

std::auto_ptr<RooAbsReal> ProfiledLikelihoodTestStatOpt::nll_
private

Definition at line 74 of file ProfiledLikelihoodRatioTestStatExt.h.

Referenced by createNLL(), Evaluate(), and minNLL().

RooArgSet ProfiledLikelihoodTestStatOpt::nuisances_
private
OneSidedness ProfiledLikelihoodTestStatOpt::oneSided_
private

Definition at line 77 of file ProfiledLikelihoodRatioTestStatExt.h.

Referenced by Evaluate(), and SetOneSided().

std::auto_ptr<RooArgSet> ProfiledLikelihoodTestStatOpt::params_
private

Definition at line 71 of file ProfiledLikelihoodRatioTestStatExt.h.

Referenced by Evaluate(), and ProfiledLikelihoodTestStatOpt().

RooAbsPdf* ProfiledLikelihoodTestStatOpt::pdf_
private

Definition at line 69 of file ProfiledLikelihoodRatioTestStatExt.h.

Referenced by Evaluate(), and ProfiledLikelihoodTestStatOpt().

RooArgSet ProfiledLikelihoodTestStatOpt::poi_
private

Definition at line 70 of file ProfiledLikelihoodRatioTestStatExt.h.

Referenced by Evaluate(), and ProfiledLikelihoodTestStatOpt().

RooArgSet ProfiledLikelihoodTestStatOpt::poiParams_
private

Definition at line 73 of file ProfiledLikelihoodRatioTestStatExt.h.

Referenced by Evaluate(), and ProfiledLikelihoodTestStatOpt().

RooArgSet ProfiledLikelihoodTestStatOpt::snap_
private

Definition at line 70 of file ProfiledLikelihoodRatioTestStatExt.h.

Referenced by Evaluate(), and ProfiledLikelihoodTestStatOpt().

Int_t ProfiledLikelihoodTestStatOpt::verbosity_
private

Definition at line 76 of file ProfiledLikelihoodRatioTestStatExt.h.

Referenced by minNLL(), and setPrintLevel().