CMS 3D CMS Logo

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

#include <CachingNLL.h>

Inheritance diagram for cacheutils::CachingAddNLL:

Public Member Functions

 CachingAddNLL (const char *name, const char *title, RooAbsPdf *pdf, RooAbsData *data)
 
 CachingAddNLL (const CachingAddNLL &other, const char *name=0)
 
void clearZeroPoint ()
 
virtual CachingAddNLLclone (const char *name=0) const
 
virtual Double_t defaultErrorLevel () const
 
virtual Double_t evaluate () const
 
virtual RooArgSet * getObservables (const RooArgSet *depList, Bool_t valueOnly=kTRUE) const
 
virtual RooArgSet * getParameters (const RooArgSet *depList, Bool_t stripDisconnected=kTRUE) const
 
virtual Bool_t isDerived () const
 
RooSetProxy & params ()
 
const RooAbsPdf * pdf () const
 
void setData (const RooAbsData &data)
 
void setZeroPoint ()
 
double sumWeights () const
 
virtual ~CachingAddNLL ()
 

Private Member Functions

void setup_ ()
 

Private Attributes

std::vector< RooAbsReal * > coeffs_
 
const RooAbsData * data_
 
std::vector< RooAbsReal * > integrals_
 
bool isRooRealSum_
 
RooSetProxy params_
 
std::vector< Double_t > partialSum_
 
RooAbsPdf * pdf_
 
std::vector< CachingPdfpdfs_
 
double sumWeights_
 
std::vector< Double_t > weights_
 
double zeroPoint_
 

Detailed Description

Definition at line 72 of file CachingNLL.h.

Constructor & Destructor Documentation

cacheutils::CachingAddNLL::CachingAddNLL ( const char *  name,
const char *  title,
RooAbsPdf *  pdf,
RooAbsData *  data 
)

Definition at line 238 of file CachingNLL.cc.

References setData(), and setup_().

238  :
239  RooAbsReal(name, title),
240  pdf_(pdf),
241  params_("params","parameters",this),
242  zeroPoint_(0)
243 {
244  if (pdf == 0) throw std::invalid_argument(std::string("Pdf passed to ")+name+" is null");
245  setData(*data);
246  setup_();
247 }
void setData(const RooAbsData &data)
Definition: CachingNLL.cc:415
const RooAbsPdf * pdf() const
Definition: CachingNLL.h:85
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
cacheutils::CachingAddNLL::CachingAddNLL ( const CachingAddNLL other,
const char *  name = 0 
)

Definition at line 249 of file CachingNLL.cc.

References data_, setData(), and setup_().

249  :
250  RooAbsReal(name ? name : (TString("nll_")+other.pdf_->GetName()).Data(), ""),
251  pdf_(other.pdf_),
252  params_("params","parameters",this),
253  zeroPoint_(0)
254 {
255  setData(*other.data_);
256  setup_();
257 }
void setData(const RooAbsData &data)
Definition: CachingNLL.cc:415
cacheutils::CachingAddNLL::~CachingAddNLL ( )
virtual

Definition at line 259 of file CachingNLL.cc.

References i, and n.

260 {
261  for (int i = 0, n = integrals_.size(); i < n; ++i) delete integrals_[i];
262  integrals_.clear();
263 }
int i
Definition: DBlmapReader.cc:9
std::vector< RooAbsReal * > integrals_
Definition: CachingNLL.h:98

Member Function Documentation

void cacheutils::CachingAddNLL::clearZeroPoint ( )
inline

Definition at line 87 of file CachingNLL.h.

References zeroPoint_.

87 { zeroPoint_ = 0.0; setValueDirty(); }
cacheutils::CachingAddNLL * cacheutils::CachingAddNLL::clone ( const char *  name = 0) const
virtual

Definition at line 266 of file CachingNLL.cc.

virtual Double_t cacheutils::CachingAddNLL::defaultErrorLevel ( ) const
inlinevirtual

Definition at line 80 of file CachingNLL.h.

80 { return 0.5; }
Double_t cacheutils::CachingAddNLL::evaluate ( ) const
virtual

Definition at line 343 of file CachingNLL.cc.

References PerfCounter::add(), dtNoiseDBValidation_cfg::cerr, alignCSCRings::e, lumiContext::fill, runtimedef::get(), cacheutils::CachingSimNLL::hasError_, create_public_lumi_plots::log, cacheutils::CachingSimNLL::noDeepLEE_, run_regression::ret, and TRACE_NLL.

344 {
345 #ifdef DEBUG_CACHE
346  PerfCounter::add("CachingAddNLL::evaluate called");
347 #endif
348  std::fill( partialSum_.begin(), partialSum_.end(), 0.0 );
349 
350  std::vector<RooAbsReal*>::iterator itc = coeffs_.begin(), edc = coeffs_.end();
351  std::vector<CachingPdf>::iterator itp = pdfs_.begin();//, edp = pdfs_.end();
352  std::vector<Double_t>::const_iterator itw, bgw = weights_.begin();//, edw = weights_.end();
353  std::vector<Double_t>::iterator its, bgs = partialSum_.begin(), eds = partialSum_.end();
354  double sumCoeff = 0;
355  //std::cout << "Performing evaluation of " << GetName() << std::endl;
356  for ( ; itc != edc; ++itp, ++itc ) {
357  // get coefficient
358  Double_t coeff = (*itc)->getVal();
359  if (isRooRealSum_) {
360  sumCoeff += coeff * integrals_[itc - coeffs_.begin()]->getVal();
361  //std::cout << " coefficient = " << coeff << ", integral = " << integrals_[itc - coeffs_.begin()]->getVal() << std::endl;
362  } else {
363  sumCoeff += coeff;
364  }
365  // get vals
366  const std::vector<Double_t> &pdfvals = itp->eval(*data_);
367  // update running sum
368  std::vector<Double_t>::const_iterator itv = pdfvals.begin();
369  for (its = bgs; its != eds; ++its, ++itv) {
370  *its += coeff * (*itv); // sum (n_i * pdf_i)
371  }
372  }
373  // then get the final nll
374  double ret = 0;
375  for ( its = bgs, itw = bgw ; its != eds ; ++its, ++itw ) {
376  if (*itw == 0) continue;
377  if (!isnormal(*its) || *its <= 0) {
378  std::cerr << "WARNING: underflow to " << *its << " in " << GetName() << std::endl;
379  if (!CachingSimNLL::noDeepLEE_) logEvalError("Number of events is negative or error"); else CachingSimNLL::hasError_ = true;
380  }
381  double thispiece = (*itw) * (*its <= 0 ? -9e9 : log( ((*its) / sumCoeff) ));
382  #ifdef ADDNLL_KAHAN_SUM
383  static bool do_kahan = runtimedef::get("ADDNLL_KAHAN_SUM");
384  if (do_kahan) {
385  double kahan_y = thispiece - compensation;
386  double kahan_t = ret + kahan_y;
387  double kahan_d = (kahan_t - ret);
388  compensation = kahan_d - kahan_y;
389  ret = kahan_t;
390  } else {
391  ret += thispiece;
392  }
393  #else
394  ret += thispiece;
395  #endif
396  }
397  // then flip sign
398  ret = -ret;
399  // std::cout << "AddNLL for " << pdf_->GetName() << ": " << ret << std::endl;
400  // and add extended term: expected - observed*log(expected);
401  double expectedEvents = (isRooRealSum_ ? pdf_->getNorm(data_->get()) : sumCoeff);
402  if (expectedEvents <= 0) {
403  if (!CachingSimNLL::noDeepLEE_) logEvalError("Expected number of events is negative"); else CachingSimNLL::hasError_ = true;
404  expectedEvents = 1e-6;
405  }
406  //ret += expectedEvents - UInt_t(sumWeights_) * log(expectedEvents); // no, doesn't work with Asimov dataset
407  ret += expectedEvents - sumWeights_ * log(expectedEvents);
408  ret += zeroPoint_;
409  // std::cout << " plus extended term: " << ret << std::endl;
410  TRACE_NLL("AddNLL for " << pdf_->GetName() << ": " << ret)
411  return ret;
412 }
#define TRACE_NLL(x)
Definition: CachingNLL.cc:59
string fill
Definition: lumiContext.py:319
void add(double increment=1.0)
int get(const char *name)
std::vector< CachingPdf > pdfs_
Definition: CachingNLL.h:97
const RooAbsData * data_
Definition: CachingNLL.h:93
std::vector< Double_t > weights_
Definition: CachingNLL.h:94
std::vector< RooAbsReal * > integrals_
Definition: CachingNLL.h:98
std::vector< RooAbsReal * > coeffs_
Definition: CachingNLL.h:96
std::vector< Double_t > partialSum_
Definition: CachingNLL.h:99
RooArgSet * cacheutils::CachingAddNLL::getObservables ( const RooArgSet *  depList,
Bool_t  valueOnly = kTRUE 
) const
virtual

Definition at line 452 of file CachingNLL.cc.

453 {
454  return new RooArgSet();
455 }
RooArgSet * cacheutils::CachingAddNLL::getParameters ( const RooArgSet *  depList,
Bool_t  stripDisconnected = kTRUE 
) const
virtual

Definition at line 458 of file CachingNLL.cc.

459 {
460  return new RooArgSet(params_);
461 }
virtual Bool_t cacheutils::CachingAddNLL::isDerived ( ) const
inlinevirtual

Definition at line 79 of file CachingNLL.h.

79 { return kTRUE; }
RooSetProxy& cacheutils::CachingAddNLL::params ( )
inline

Definition at line 88 of file CachingNLL.h.

References params_.

88 { return params_; }
const RooAbsPdf* cacheutils::CachingAddNLL::pdf ( ) const
inline

Definition at line 85 of file CachingNLL.h.

References pdf_.

85 { return pdf_; }
void cacheutils::CachingAddNLL::setData ( const RooAbsData &  data)

Definition at line 415 of file CachingNLL.cc.

References data, runtimedef::get(), i, and n.

Referenced by CachingAddNLL(), and cacheutils::CachingSimNLL::setData().

416 {
417  //std::cout << "Setting data for pdf " << pdf_->GetName() << std::endl;
418  //utils::printRAD(&data);
419  data_ = &data;
420  setValueDirty();
421  sumWeights_ = 0.0;
422  weights_.resize(data.numEntries());
423  partialSum_.resize(data.numEntries());
424  std::vector<Double_t>::iterator itw = weights_.begin();
425  #ifdef ADDNLL_KAHAN_SUM
426  double compensation = 0;
427  #endif
428  for (int i = 0, n = data.numEntries(); i < n; ++i, ++itw) {
429  data.get(i);
430  *itw = data.weight();
431  #ifdef ADDNLL_KAHAN_SUM
432  static bool do_kahan = runtimedef::get("ADDNLL_KAHAN_SUM");
433  if (do_kahan) {
434  double kahan_y = *itw - compensation;
435  double kahan_t = sumWeights_ + kahan_y;
436  double kahan_d = (kahan_t - sumWeights_);
437  compensation = kahan_d - kahan_y;
438  sumWeights_ = kahan_t;
439  } else {
440  sumWeights_ += *itw;
441  }
442  #else
443  sumWeights_ += *itw;
444  #endif
445  }
446  for (std::vector<CachingPdf>::iterator itp = pdfs_.begin(), edp = pdfs_.end(); itp != edp; ++itp) {
447  itp->setDataDirty();
448  }
449 }
int i
Definition: DBlmapReader.cc:9
int get(const char *name)
std::vector< CachingPdf > pdfs_
Definition: CachingNLL.h:97
const RooAbsData * data_
Definition: CachingNLL.h:93
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
std::vector< Double_t > weights_
Definition: CachingNLL.h:94
std::vector< Double_t > partialSum_
Definition: CachingNLL.h:99
void cacheutils::CachingAddNLL::setup_ ( )
private

Temporarily switch this off, it doesn't work. Don't know why, however.

Definition at line 272 of file CachingNLL.cc.

References a, gather_cfg::cout, utils::factorizeFunc(), i, and n.

Referenced by CachingAddNLL().

273 {
274  const RooArgSet *obs = data_->get();
275  for (int i = 0, n = integrals_.size(); i < n; ++i) delete integrals_[i];
276  integrals_.clear();
277  RooAddPdf *addpdf = 0;
278  RooRealSumPdf *sumpdf = 0;
279  if ((addpdf = dynamic_cast<RooAddPdf *>(pdf_)) != 0) {
280  isRooRealSum_ = false;
281  int npdf = addpdf->coefList().getSize();
282  coeffs_.reserve(npdf);
283  pdfs_.reserve(npdf);
284  for (int i = 0; i < npdf; ++i) {
285  RooAbsReal * coeff = dynamic_cast<RooAbsReal*>(addpdf->coefList().at(i));
286  RooAbsPdf * pdfi = dynamic_cast<RooAbsPdf *>(addpdf->pdfList().at(i));
287  coeffs_.push_back(coeff);
288  pdfs_.push_back(CachingPdf(pdfi, obs));
289  }
290  } else if ((sumpdf = dynamic_cast<RooRealSumPdf *>(pdf_)) != 0) {
291  isRooRealSum_ = true;
292  int npdf = sumpdf->coefList().getSize();
293  coeffs_.reserve(npdf);
294  pdfs_.reserve(npdf);
295  integrals_.reserve(npdf);
296  for (int i = 0; i < npdf; ++i) {
297  RooAbsReal * coeff = dynamic_cast<RooAbsReal*>(sumpdf->coefList().at(i));
298  RooAbsReal * funci = dynamic_cast<RooAbsReal*>(sumpdf->funcList().at(i));
300  if (0 && typeid(*funci) == typeid(RooProduct)) {
301  RooArgList obsDep, obsInd;
302  obsInd.add(*coeff);
303  utils::factorizeFunc(*obs, *funci, obsDep, obsInd);
304  std::cout << "Entry " << i << ": coef name " << (coeff ? coeff->GetName() : "null") <<
305  " type " << (coeff ? coeff->ClassName() : "n/a") << std::endl;
306  std::cout << " " << "; func name " << (funci ? funci->GetName() : "null") <<
307  " type " << (funci ? funci->ClassName() : "n/a") << std::endl;
308  std::cout << "Terms depending on observables: " << std::endl; obsDep.Print("V");
309  std::cout << "Terms not depending on observables: " << std::endl; obsInd.Print("V");
310  if (obsInd.getSize() > 1) {
311  coeff = new RooProduct(TString::Format("%s_x_%s_obsIndep", coeff->GetName(), funci->GetName()), "", RooArgSet(obsInd));
312  addOwnedComponents(RooArgSet(*coeff));
313  }
314  if (obsDep.getSize() > 1) {
315  funci = new RooProduct(TString::Format("%s_obsDep", funci->GetName()), "", RooArgSet(obsInd));
316  addOwnedComponents(RooArgSet(*funci));
317  } else if (obsDep.getSize() == 1) {
318  funci = (RooAbsReal *) obsDep.first();
319  } else throw std::logic_error("No part of pdf depends on observables?");
320  }
321  coeffs_.push_back(coeff);
322  pdfs_.push_back(CachingPdf(funci, obs));
323  integrals_.push_back(funci->createIntegral(*obs));
324  }
325  } else {
326  std::string errmsg = "ERROR: CachingAddNLL: Pdf ";
327  errmsg += pdf_->GetName();
328  errmsg += " is neither a RooAddPdf nor a RooRealSumPdf, but a ";
329  errmsg += pdf_->ClassName();
330  throw std::invalid_argument(errmsg);
331  }
332 
333  std::auto_ptr<RooArgSet> params(pdf_->getParameters(*data_));
334  std::auto_ptr<TIterator> iter(params->createIterator());
335  for (RooAbsArg *a = (RooAbsArg *) iter->Next(); a != 0; a = (RooAbsArg *) iter->Next()) {
336  RooRealVar *rrv = dynamic_cast<RooRealVar *>(a);
337  //if (rrv != 0 && !rrv->isConstant()) params_.add(*rrv);
338  if (rrv != 0) params_.add(*rrv);
339  }
340 }
int i
Definition: DBlmapReader.cc:9
void factorizeFunc(const RooArgSet &observables, RooAbsReal &pdf, RooArgList &obsTerms, RooArgList &otherTerms, bool debug=false)
factorize a RooAbsReal
Definition: utils.cc:177
std::vector< CachingPdf > pdfs_
Definition: CachingNLL.h:97
const RooAbsData * data_
Definition: CachingNLL.h:93
double a
Definition: hdecay.h:121
tuple cout
Definition: gather_cfg.py:121
RooSetProxy & params()
Definition: CachingNLL.h:88
std::vector< RooAbsReal * > integrals_
Definition: CachingNLL.h:98
std::vector< RooAbsReal * > coeffs_
Definition: CachingNLL.h:96
void cacheutils::CachingAddNLL::setZeroPoint ( )
inline

Definition at line 86 of file CachingNLL.h.

References zeroPoint_.

86 { zeroPoint_ = -this->getVal(); setValueDirty(); }
double cacheutils::CachingAddNLL::sumWeights ( ) const
inline

Definition at line 84 of file CachingNLL.h.

References sumWeights_.

84 { return sumWeights_; }

Member Data Documentation

std::vector<RooAbsReal*> cacheutils::CachingAddNLL::coeffs_
mutableprivate

Definition at line 96 of file CachingNLL.h.

const RooAbsData* cacheutils::CachingAddNLL::data_
private

Definition at line 93 of file CachingNLL.h.

Referenced by CachingAddNLL().

std::vector<RooAbsReal*> cacheutils::CachingAddNLL::integrals_
mutableprivate

Definition at line 98 of file CachingNLL.h.

bool cacheutils::CachingAddNLL::isRooRealSum_
mutableprivate

Definition at line 100 of file CachingNLL.h.

RooSetProxy cacheutils::CachingAddNLL::params_
private

Definition at line 92 of file CachingNLL.h.

Referenced by params().

std::vector<Double_t> cacheutils::CachingAddNLL::partialSum_
mutableprivate

Definition at line 99 of file CachingNLL.h.

RooAbsPdf* cacheutils::CachingAddNLL::pdf_
private

Definition at line 91 of file CachingNLL.h.

Referenced by pdf().

std::vector<CachingPdf> cacheutils::CachingAddNLL::pdfs_
mutableprivate

Definition at line 97 of file CachingNLL.h.

double cacheutils::CachingAddNLL::sumWeights_
private

Definition at line 95 of file CachingNLL.h.

Referenced by sumWeights().

std::vector<Double_t> cacheutils::CachingAddNLL::weights_
private

Definition at line 94 of file CachingNLL.h.

double cacheutils::CachingAddNLL::zeroPoint_
private

Definition at line 101 of file CachingNLL.h.

Referenced by clearZeroPoint(), and setZeroPoint().