CMS 3D CMS Logo

Public Member Functions | Private Attributes

toymcoptutils::SimPdfGenInfo Class Reference

#include <ToyMCSamplerOpt.h>

List of all members.

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_.

                                                                                                                                                       :
    pdf_(&pdf),
    cat_(0),
    observables_(observables),
    copyData_(true)
{
    assert(forceEvents == 0 && "SimPdfGenInfo: forceEvents must be zero at least for now");
    RooSimultaneous *simPdf = dynamic_cast<RooSimultaneous *>(&pdf);
    if (simPdf) {
        cat_ = const_cast<RooAbsCategoryLValue *>(&simPdf->indexCat());
        int nbins = cat_->numBins((const char *)0);
        pdfs_.resize(nbins, 0);
        RooArgList dummy;
        for (int ic = 0; ic < nbins; ++ic) {
            cat_->setBin(ic);
            RooAbsPdf *pdfi = simPdf->getPdf(cat_->getLabel());
            if (pdfi == 0) throw std::logic_error(std::string("Unmapped category state: ") + cat_->getLabel());
            RooAbsPdf *newpdf = utils::factorizePdf(observables, *pdfi, dummy);
            pdfs_[ic] = new SinglePdfGenInfo(*newpdf, observables, preferBinned);
            if (newpdf != 0 && newpdf != pdfi) {
                ownedCrap_.addOwned(*newpdf); 
            }
        }
    } else {
        pdfs_.push_back(new SinglePdfGenInfo(pdf, observables, preferBinned, protoData, forceEvents));
    }
}
toymcoptutils::SimPdfGenInfo::~SimPdfGenInfo ( )

Definition at line 301 of file ToyMCSamplerOpt.cc.

{
    for (std::vector<SinglePdfGenInfo *>::iterator it = pdfs_.begin(), ed = pdfs_.end(); it != ed; ++it) {
        delete *it;
    }
    pdfs_.clear();
    //for (std::map<std::string,RooDataSet*>::iterator it = datasetPieces_.begin(), ed = datasetPieces_.end(); it != ed; ++it) {
    for (std::map<std::string,RooAbsData*>::iterator it = datasetPieces_.begin(), ed = datasetPieces_.end(); it != ed; ++it) {
        delete it->second;
    }
    datasetPieces_.clear();
}

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().

{
    RooAbsData *ret = 0;
    TString retName =  TString::Format("%sData", pdf_->GetName());
    if (cat_ != 0) {
        //bool needsWeights = false;
        for (int i = 0, n = cat_->numBins((const char *)0); i < n; ++i) {
            if (pdfs_[i] == 0) continue;
            cat_->setBin(i);
            RooAbsData *&data =  datasetPieces_[cat_->getLabel()]; delete data;
            assert(protoData == 0);
            data = pdfs_[i]->generate(protoData); // I don't really know if protoData != 0 would make sense here
            if (data->isWeighted()) {
                if (weightVar == 0) weightVar = new RooRealVar("_weight_","",1.0);
                RooArgSet obs(*data->get()); 
                obs.add(*weightVar);
                RooDataSet *wdata = new RooDataSet(data->GetName(), "", obs, "_weight_");
                for (int i = 0, n = data->numEntries(); i < n; ++i) {
                    obs = *data->get(i);
                    if (data->weight()) wdata->add(obs, data->weight());
                }
                //std::cout << "DataHist was " << std::endl; utils::printRAD(data);
                delete data;
                data = wdata;
                //std::cout << "DataSet is " << std::endl; utils::printRAD(data);
            } 
            //if (data->isWeighted()) needsWeights = true;
        }
        if (copyData_) {
            RooArgSet vars(observables_), varsPlusWeight(observables_); 
            if (weightVar) varsPlusWeight.add(*weightVar);
            ret = new RooDataSet(retName, "", varsPlusWeight, (weightVar ? weightVar->GetName() : 0));
            for (std::map<std::string,RooAbsData*>::iterator it = datasetPieces_.begin(), ed = datasetPieces_.end(); it != ed; ++it) {
                cat_->setLabel(it->first.c_str());
                for (unsigned int i = 0, n = it->second->numEntries(); i < n; ++i) {
                    vars = *it->second->get(i);
                    ret->add(vars, it->second->weight());
                }
            }
        } else {
            // not copyData is the "fast" mode used when generating toys as a ToyMCSampler.
            // this doesn't copy the data, so the toys cannot outlive this class and each new
            // toy over-writes the memory of the previous one.
            ret = new RooDataSet(retName, "", observables_, RooFit::Index((RooCategory&)*cat_), RooFit::Link(datasetPieces_) /*, RooFit::OwnLinked()*/);
        }
    } else ret = pdfs_[0]->generate(protoData, forceEvents);
    //std::cout << "Dataset generated from sim pdf (weighted? " << ret->isWeighted() << ")" << std::endl; utils::printRAD(ret);
    return ret;
}
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().

{
    RooAbsData *ret = 0;
    TString retName =  TString::Format("%sData", pdf_->GetName());
    if (cat_ != 0) {
        //bool needsWeights = false;
        for (int i = 0, n = cat_->numBins((const char *)0); i < n; ++i) {
            if (pdfs_[i] == 0) continue;
            cat_->setBin(i);
            RooAbsData *&data =  datasetPieces_[cat_->getLabel()]; delete data;
            data = pdfs_[i]->generateAsimov(weightVar); 
        }
        if (copyData_) { 
            RooArgSet vars(observables_), varsPlusWeight(observables_); varsPlusWeight.add(*weightVar);
            ret = new RooDataSet(retName, "", varsPlusWeight, (weightVar ? weightVar->GetName() : 0));
            for (std::map<std::string,RooAbsData*>::iterator it = datasetPieces_.begin(), ed = datasetPieces_.end(); it != ed; ++it) {
                cat_->setLabel(it->first.c_str());
                for (unsigned int i = 0, n = it->second->numEntries(); i < n; ++i) {
                    vars = *it->second->get(i);
                    ret->add(vars, it->second->weight());
                }
            }
        } else {
            // not copyData is the "fast" mode used when generating toys as a ToyMCSampler.
            // this doesn't copy the data, so the toys cannot outlive this class and each new
            // toy over-writes the memory of the previous one.
            ret = new RooDataSet(retName, "", observables_, RooFit::Index((RooCategory&)*cat_), RooFit::Link(datasetPieces_) /*, RooFit::OwnLinked()*/);
        }
    } else ret = pdfs_[0]->generateAsimov(weightVar);
    //std::cout << "Asimov dataset generated from sim pdf " << pdf_->GetName() << " (sumw? " << ret->sumEntries() << ")" << std::endl; 
    //utils::printRAD(ret);
    return ret;
}
void toymcoptutils::SimPdfGenInfo::setCacheTemplates ( bool  cache)

Definition at line 403 of file ToyMCSamplerOpt.cc.

Referenced by ToyMCSamplerOpt::Generate().

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

Definition at line 38 of file ToyMCSamplerOpt.h.

References copyData_.

Referenced by ToyMCSamplerOpt::Generate().

{ copyData_ = copyData; }

Member Data Documentation

RooAbsCategoryLValue* toymcoptutils::SimPdfGenInfo::cat_ [private]

Definition at line 42 of file ToyMCSamplerOpt.h.

Referenced by SimPdfGenInfo().

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.

Definition at line 43 of file ToyMCSamplerOpt.h.

Definition at line 45 of file ToyMCSamplerOpt.h.

Referenced by SimPdfGenInfo().

RooAbsPdf* toymcoptutils::SimPdfGenInfo::pdf_ [private]

Definition at line 41 of file ToyMCSamplerOpt.h.

Definition at line 44 of file ToyMCSamplerOpt.h.

Referenced by SimPdfGenInfo().