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 Attributes
toymcoptutils::SimPdfGenInfo Class Reference

#include <ToyMCSamplerOpt.h>

Public Member Functions

RooAbsData * generate (RooRealVar *&weightVar, const RooDataSet *protoData=NULL, int forceEvents=0)
 
RooAbsData * generateAsimov (RooRealVar *&weightVar)
 
void setCacheTemplates (bool cache)
 
void setCopyData (bool copyData)
 
 SimPdfGenInfo (RooAbsPdf &pdf, const RooArgSet &observables, bool preferBinned, const RooDataSet *protoData=NULL, int forceEvents=0)
 
 ~SimPdfGenInfo ()
 

Private Attributes

RooAbsCategoryLValue * cat_
 
bool copyData_
 
std::map< std::string,
RooAbsData * > 
datasetPieces_
 
RooArgSet observables_
 
RooArgSet ownedCrap_
 
RooAbsPdf * pdf_
 
std::vector< SinglePdfGenInfo * > pdfs_
 

Detailed Description

Definition at line 32 of file ToyMCSamplerOpt.h.

Constructor & Destructor Documentation

toymcoptutils::SimPdfGenInfo::SimPdfGenInfo ( RooAbsPdf &  pdf,
const RooArgSet &  observables,
bool  preferBinned,
const RooDataSet *  protoData = NULL,
int  forceEvents = 0 
)

Definition at line 273 of file ToyMCSamplerOpt.cc.

References cat_, utils::factorizePdf(), pileupCalc::nbins, ownedCrap_, and pdfs_.

273  :
274  pdf_(&pdf),
275  cat_(0),
276  observables_(observables),
277  copyData_(true)
278 {
279  assert(forceEvents == 0 && "SimPdfGenInfo: forceEvents must be zero at least for now");
280  RooSimultaneous *simPdf = dynamic_cast<RooSimultaneous *>(&pdf);
281  if (simPdf) {
282  cat_ = const_cast<RooAbsCategoryLValue *>(&simPdf->indexCat());
283  int nbins = cat_->numBins((const char *)0);
284  pdfs_.resize(nbins, 0);
285  RooArgList dummy;
286  for (int ic = 0; ic < nbins; ++ic) {
287  cat_->setBin(ic);
288  RooAbsPdf *pdfi = simPdf->getPdf(cat_->getLabel());
289  if (pdfi == 0) throw std::logic_error(std::string("Unmapped category state: ") + cat_->getLabel());
290  RooAbsPdf *newpdf = utils::factorizePdf(observables, *pdfi, dummy);
291  pdfs_[ic] = new SinglePdfGenInfo(*newpdf, observables, preferBinned);
292  if (newpdf != 0 && newpdf != pdfi) {
293  ownedCrap_.addOwned(*newpdf);
294  }
295  }
296  } else {
297  pdfs_.push_back(new SinglePdfGenInfo(pdf, observables, preferBinned, protoData, forceEvents));
298  }
299 }
RooAbsCategoryLValue * cat_
RooAbsPdf * factorizePdf(const RooArgSet &observables, RooAbsPdf &pdf, RooArgList &constraints)
Definition: utils.cc:80
std::vector< SinglePdfGenInfo * > pdfs_
toymcoptutils::SimPdfGenInfo::~SimPdfGenInfo ( )

Definition at line 301 of file ToyMCSamplerOpt.cc.

302 {
303  for (std::vector<SinglePdfGenInfo *>::iterator it = pdfs_.begin(), ed = pdfs_.end(); it != ed; ++it) {
304  delete *it;
305  }
306  pdfs_.clear();
307  //for (std::map<std::string,RooDataSet*>::iterator it = datasetPieces_.begin(), ed = datasetPieces_.end(); it != ed; ++it) {
308  for (std::map<std::string,RooAbsData*>::iterator it = datasetPieces_.begin(), ed = datasetPieces_.end(); it != ed; ++it) {
309  delete it->second;
310  }
311  datasetPieces_.clear();
312 }
std::map< std::string, RooAbsData * > datasetPieces_
std::vector< SinglePdfGenInfo * > pdfs_

Member Function Documentation

RooAbsData * toymcoptutils::SimPdfGenInfo::generate ( RooRealVar *&  weightVar,
const RooDataSet *  protoData = NULL,
int  forceEvents = 0 
)

Definition at line 316 of file ToyMCSamplerOpt.cc.

References data, i, n, and run_regression::ret.

Referenced by ToyMCSamplerOpt::Generate(), and Combine::run().

317 {
318  RooAbsData *ret = 0;
319  TString retName = TString::Format("%sData", pdf_->GetName());
320  if (cat_ != 0) {
321  //bool needsWeights = false;
322  for (int i = 0, n = cat_->numBins((const char *)0); i < n; ++i) {
323  if (pdfs_[i] == 0) continue;
324  cat_->setBin(i);
325  RooAbsData *&data = datasetPieces_[cat_->getLabel()]; delete data;
326  assert(protoData == 0);
327  data = pdfs_[i]->generate(protoData); // I don't really know if protoData != 0 would make sense here
328  if (data->isWeighted()) {
329  if (weightVar == 0) weightVar = new RooRealVar("_weight_","",1.0);
330  RooArgSet obs(*data->get());
331  obs.add(*weightVar);
332  RooDataSet *wdata = new RooDataSet(data->GetName(), "", obs, "_weight_");
333  for (int i = 0, n = data->numEntries(); i < n; ++i) {
334  obs = *data->get(i);
335  if (data->weight()) wdata->add(obs, data->weight());
336  }
337  //std::cout << "DataHist was " << std::endl; utils::printRAD(data);
338  delete data;
339  data = wdata;
340  //std::cout << "DataSet is " << std::endl; utils::printRAD(data);
341  }
342  //if (data->isWeighted()) needsWeights = true;
343  }
344  if (copyData_) {
346  RooArgSet vars(observables_), varsPlusWeight(observables_);
347  if (weightVar) varsPlusWeight.add(*weightVar);
348  ret = new RooDataSet(retName, "", varsPlusWeight, (weightVar ? weightVar->GetName() : 0));
349  for (std::map<std::string,RooAbsData*>::iterator it = datasetPieces_.begin(), ed = datasetPieces_.end(); it != ed; ++it) {
350  cat_->setLabel(it->first.c_str());
351  for (unsigned int i = 0, n = it->second->numEntries(); i < n; ++i) {
352  vars = *it->second->get(i);
353  ret->add(vars, it->second->weight());
354  }
355  }
356  } else {
357  // not copyData is the "fast" mode used when generating toys as a ToyMCSampler.
358  // this doesn't copy the data, so the toys cannot outlive this class and each new
359  // toy over-writes the memory of the previous one.
360  ret = new RooDataSet(retName, "", observables_, RooFit::Index((RooCategory&)*cat_), RooFit::Link(datasetPieces_) /*, RooFit::OwnLinked()*/);
361  }
362  } else ret = pdfs_[0]->generate(protoData, forceEvents);
363  //std::cout << "Dataset generated from sim pdf (weighted? " << ret->isWeighted() << ")" << std::endl; utils::printRAD(ret);
364  return ret;
365 }
RooAbsCategoryLValue * cat_
int i
Definition: DBlmapReader.cc:9
std::map< std::string, RooAbsData * > datasetPieces_
std::vector< SinglePdfGenInfo * > pdfs_
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
RooAbsData * toymcoptutils::SimPdfGenInfo::generateAsimov ( RooRealVar *&  weightVar)

Definition at line 368 of file ToyMCSamplerOpt.cc.

References data, i, n, and run_regression::ret.

Referenced by asimovutils::asimovDatasetNominal(), asimovutils::asimovDatasetWithFit(), and Combine::run().

369 {
370  RooAbsData *ret = 0;
371  TString retName = TString::Format("%sData", pdf_->GetName());
372  if (cat_ != 0) {
373  //bool needsWeights = false;
374  for (int i = 0, n = cat_->numBins((const char *)0); i < n; ++i) {
375  if (pdfs_[i] == 0) continue;
376  cat_->setBin(i);
377  RooAbsData *&data = datasetPieces_[cat_->getLabel()]; delete data;
378  data = pdfs_[i]->generateAsimov(weightVar);
379  }
380  if (copyData_) {
381  RooArgSet vars(observables_), varsPlusWeight(observables_); varsPlusWeight.add(*weightVar);
382  ret = new RooDataSet(retName, "", varsPlusWeight, (weightVar ? weightVar->GetName() : 0));
383  for (std::map<std::string,RooAbsData*>::iterator it = datasetPieces_.begin(), ed = datasetPieces_.end(); it != ed; ++it) {
384  cat_->setLabel(it->first.c_str());
385  for (unsigned int i = 0, n = it->second->numEntries(); i < n; ++i) {
386  vars = *it->second->get(i);
387  ret->add(vars, it->second->weight());
388  }
389  }
390  } else {
391  // not copyData is the "fast" mode used when generating toys as a ToyMCSampler.
392  // this doesn't copy the data, so the toys cannot outlive this class and each new
393  // toy over-writes the memory of the previous one.
394  ret = new RooDataSet(retName, "", observables_, RooFit::Index((RooCategory&)*cat_), RooFit::Link(datasetPieces_) /*, RooFit::OwnLinked()*/);
395  }
396  } else ret = pdfs_[0]->generateAsimov(weightVar);
397  //std::cout << "Asimov dataset generated from sim pdf " << pdf_->GetName() << " (sumw? " << ret->sumEntries() << ")" << std::endl;
398  //utils::printRAD(ret);
399  return ret;
400 }
RooAbsCategoryLValue * cat_
int i
Definition: DBlmapReader.cc:9
std::map< std::string, RooAbsData * > datasetPieces_
std::vector< SinglePdfGenInfo * > pdfs_
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
void toymcoptutils::SimPdfGenInfo::setCacheTemplates ( bool  cache)

Definition at line 403 of file ToyMCSamplerOpt.cc.

Referenced by ToyMCSamplerOpt::Generate().

404 {
405  for (std::vector<SinglePdfGenInfo *>::const_iterator it = pdfs_.begin(), ed = pdfs_.end(); it != ed; ++it) {
406  if (*it) (*it)->setCacheTemplates(cache);
407  }
408 }
std::vector< SinglePdfGenInfo * > pdfs_
void toymcoptutils::SimPdfGenInfo::setCopyData ( bool  copyData)
inline

Definition at line 38 of file ToyMCSamplerOpt.h.

References copyData_.

Referenced by ToyMCSamplerOpt::Generate().

38 { copyData_ = copyData; }

Member Data Documentation

RooAbsCategoryLValue* toymcoptutils::SimPdfGenInfo::cat_
private

Definition at line 42 of file ToyMCSamplerOpt.h.

Referenced by SimPdfGenInfo().

bool toymcoptutils::SimPdfGenInfo::copyData_
private

Definition at line 47 of file ToyMCSamplerOpt.h.

Referenced by setCopyData().

std::map<std::string,RooAbsData*> toymcoptutils::SimPdfGenInfo::datasetPieces_
private

Definition at line 46 of file ToyMCSamplerOpt.h.

RooArgSet toymcoptutils::SimPdfGenInfo::observables_
private

Definition at line 43 of file ToyMCSamplerOpt.h.

RooArgSet toymcoptutils::SimPdfGenInfo::ownedCrap_
private

Definition at line 45 of file ToyMCSamplerOpt.h.

Referenced by SimPdfGenInfo().

RooAbsPdf* toymcoptutils::SimPdfGenInfo::pdf_
private

Definition at line 41 of file ToyMCSamplerOpt.h.

std::vector<SinglePdfGenInfo *> toymcoptutils::SimPdfGenInfo::pdfs_
private

Definition at line 44 of file ToyMCSamplerOpt.h.

Referenced by SimPdfGenInfo().