CMS 3D CMS Logo

Public Types | Public Member Functions | Private Member Functions | Private Attributes

toymcoptutils::SinglePdfGenInfo Class Reference

#include <ToyMCSamplerOpt.h>

List of all members.

Public Types

enum  Mode {
  Binned, BinnedNoWorkaround, Poisson, Unbinned,
  Counting
}

Public Member Functions

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

Private Member Functions

RooDataSet * generateCountingAsimov ()
RooDataSet * generateWithHisto (RooRealVar *&weightVar, bool asimov)
void setToExpected (RooProdPdf &prod, RooArgSet &obs)
void setToExpected (RooPoisson &pois, RooArgSet &obs)

Private Attributes

TH1 * histoSpec_
bool keepHistoSpec_
Mode mode_
RooArgSet observables_
RooAbsPdf * pdf_
RooAbsPdf::GenSpec * spec_
RooRealVar * weightVar_

Detailed Description

Definition at line 9 of file ToyMCSamplerOpt.h.


Member Enumeration Documentation

Enumerator:
Binned 
BinnedNoWorkaround 
Poisson 
Unbinned 
Counting 

Definition at line 11 of file ToyMCSamplerOpt.h.


Constructor & Destructor Documentation

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

Definition at line 56 of file ToyMCSamplerOpt.cc.

References Binned, BinnedNoWorkaround, runtimedef::get(), mode_, observables_, Poisson, and Unbinned.

                                                                                                                                                             :
   mode_(pdf.canBeExtended() ? (preferBinned ? Binned : Unbinned) : Counting),
   pdf_(&pdf),
   spec_(0),histoSpec_(0),keepHistoSpec_(0),weightVar_(0)
{
   if (pdf.canBeExtended()) {
       if (pdf.getAttribute("forceGenBinned")) mode_ = Binned;
       else if (pdf.getAttribute("forceGenPoisson")) mode_ = Poisson;
       else if (pdf.getAttribute("forceGenUnbinned")) mode_ = Unbinned;
   }

   RooArgSet *obs = pdf.getObservables(observables);
   observables_.add(*obs);
   delete obs;
   if (mode_ == Binned) {
      if (runtimedef::get("TMCSO_GenBinned")) mode_ = BinnedNoWorkaround;
      else if (runtimedef::get("TMCSO_GenBinnedWorkaround")) mode_ = Binned;
      else mode_ = Poisson;
   } else if (mode_ == Unbinned) {
       //if (!runtimedef::get("TMCSO_NoPrepareMultiGen")) {
       //    spec_ = protoData ? pdf.prepareMultiGen(observables_, RooFit::Extended(), RooFit::ProtoData(*protoData, true, true)) 
       //                      : pdf.prepareMultiGen(observables_, RooFit::Extended());
       //}
   }
}
toymcoptutils::SinglePdfGenInfo::~SinglePdfGenInfo ( )

Definition at line 82 of file ToyMCSamplerOpt.cc.

{
    delete spec_;
    delete weightVar_;
    delete histoSpec_;
}

Member Function Documentation

RooAbsData * toymcoptutils::SinglePdfGenInfo::generate ( const RooDataSet *  protoData = NULL,
int  forceEvents = 0 
)

Definition at line 91 of file ToyMCSamplerOpt.cc.

References data, and run_regression::ret.

{
    assert(forceEvents == 0 && "SinglePdfGenInfo: forceEvents must be zero at least for now");
    RooAbsData *ret = 0;
    switch (mode_) {
        case Unbinned:
            if (spec_ == 0) spec_ = protoData ? pdf_->prepareMultiGen(observables_, RooFit::Extended(), RooFit::ProtoData(*protoData, true, true))
                                              : pdf_->prepareMultiGen(observables_, RooFit::Extended());
            if (spec_) ret = pdf_->generate(*spec_);
            else ret = pdf_->generate(observables_, RooFit::Extended());
            break;
        case Binned:
            { // aka generateBinnedWorkaround
                RooDataSet *data =  pdf_->generate(observables_, RooFit::Extended());
                ret = new RooDataHist(data->GetName(), "", *data->get(), *data);
                delete data;
            }
            break;
        case BinnedNoWorkaround:
            ret = protoData ? pdf_->generateBinned(observables_, RooFit::Extended(), RooFit::ProtoData(*protoData, true, true))
                            : pdf_->generateBinned(observables_, RooFit::Extended());
            break;
        case Poisson:
            ret = generateWithHisto(weightVar_, false);
            break;
        case Counting:
            ret = pdf_->generate(observables_, 1);
            break;
        default:
            throw std::logic_error("Mode not foreseen in SinglePdfGenInfo::generate");
    } 
    //std::cout << "Dataset generated from " << pdf_->GetName() << " (weighted? " << ret->isWeighted() << ")" << std::endl;
    //utils::printRAD(ret);
    return ret;
}
RooDataSet * toymcoptutils::SinglePdfGenInfo::generateAsimov ( RooRealVar *&  weightVar)

Definition at line 128 of file ToyMCSamplerOpt.cc.

References reco::get().

{
    static int nPA = runtimedef::get("TMCSO_PseudoAsimov");
    if (nPA) return generatePseudoAsimov(weightVar, nPA);
    return generateWithHisto(weightVar, true);
}
RooDataSet * toymcoptutils::SinglePdfGenInfo::generateCountingAsimov ( ) [private]

Definition at line 219 of file ToyMCSamplerOpt.cc.

References parseEventContent::prod, and run_regression::ret.

{
    RooArgSet obs(observables_);
    RooProdPdf *prod = dynamic_cast<RooProdPdf *>(pdf_);
    RooPoisson *pois = 0;
    if (prod != 0) {
        setToExpected(*prod, observables_);
    } else if ((pois = dynamic_cast<RooPoisson *>(pdf_)) != 0) {
        setToExpected(*pois, observables_);
    } else throw std::logic_error("A counting model pdf must be either a RooProdPdf or a RooPoisson");
    RooDataSet *ret = new RooDataSet(TString::Format("%sData", pdf_->GetName()), "", obs);
    ret->add(obs);
    return ret;
}
RooDataSet * toymcoptutils::SinglePdfGenInfo::generatePseudoAsimov ( RooRealVar *&  weightVar,
int  nPoints 
)

Definition at line 136 of file ToyMCSamplerOpt.cc.

References data, and i.

{
    if (mode_ == Unbinned) {
        double expEvents = pdf_->expectedEvents(observables_);
        std::auto_ptr<RooDataSet> data(pdf_->generate(observables_, nPoints));
        if (weightVar == 0) weightVar = new RooRealVar("_weight_","",1.0);
        RooArgSet obsPlusW(observables_); obsPlusW.add(*weightVar);
        RooDataSet *rds = new RooDataSet(data->GetName(), "", obsPlusW, weightVar->GetName());
        for (int i = 0; i < nPoints; ++i) {
            observables_ = *data->get(i);
            rds->add(observables_, expEvents/nPoints);
        }
        return rds; 
    } else {
        return generateWithHisto(weightVar, true);
    }
}
RooDataSet * toymcoptutils::SinglePdfGenInfo::generateWithHisto ( RooRealVar *&  weightVar,
bool  asimov 
) [private]

Definition at line 156 of file ToyMCSamplerOpt.cc.

References data, i, n, none, x, detailsBasic3DVector::y, and z.

{
    if (mode_ == Counting) return generateCountingAsimov();
    if (observables_.getSize() > 3) throw std::invalid_argument(std::string("ERROR in SinglePdfGenInfo::generateWithHisto for ") + pdf_->GetName() + ", more than 3 observable");
    RooArgList obs(observables_);
    RooRealVar *x = (RooRealVar*)obs.at(0);
    RooRealVar *y = obs.getSize() > 1 ? (RooRealVar*)obs.at(1) : 0;
    RooRealVar *z = obs.getSize() > 2 ? (RooRealVar*)obs.at(2) : 0;
    if (weightVar == 0) weightVar = new RooRealVar("_weight_","",1.0);

    RooCmdArg ay = (y ? RooFit::YVar(*y) : RooCmdArg::none());
    RooCmdArg az = (z ? RooFit::ZVar(*z) : RooCmdArg::none());

    if (histoSpec_ == 0) {
        histoSpec_ = pdf_->createHistogram("htemp", *x, ay, az); 
        histoSpec_->SetDirectory(0);
    } 

    double expectedEvents = pdf_->expectedEvents(observables_);
    histoSpec_->Scale(expectedEvents/ histoSpec_->Integral()); 
    RooArgSet obsPlusW(obs); obsPlusW.add(*weightVar);
    RooDataSet *data = new RooDataSet(TString::Format("%sData", pdf_->GetName()), "", obsPlusW, weightVar->GetName());
    RooAbsArg::setDirtyInhibit(true); // don't propagate dirty flags while filling histograms 
    switch (obs.getSize()) {
        case 1:
            for (int i = 1, n = histoSpec_->GetNbinsX(); i <= n; ++i) {
                x->setVal(histoSpec_->GetXaxis()->GetBinCenter(i));
                data->add(observables_, asimov ? histoSpec_->GetBinContent(i) : RooRandom::randomGenerator()->Poisson(histoSpec_->GetBinContent(i)) );
            }
            break;
        case 2:
            {
            TH2& h2 = dynamic_cast<TH2&>(*histoSpec_);
            for (int ix = 1, nx = h2.GetNbinsX(); ix <= nx; ++ix) {
            for (int iy = 1, ny = h2.GetNbinsY(); iy <= ny; ++iy) {
                x->setVal(h2.GetXaxis()->GetBinCenter(ix));
                y->setVal(h2.GetYaxis()->GetBinCenter(iy));
                data->add(observables_, asimov ? h2.GetBinContent(ix,iy) : RooRandom::randomGenerator()->Poisson(h2.GetBinContent(ix,iy)) );
            } }
            }
            break;
        case 3:
            {
            TH3& h3 = dynamic_cast<TH3&>(*histoSpec_);
            for (int ix = 1, nx = h3.GetNbinsX(); ix <= nx; ++ix) {
            for (int iy = 1, ny = h3.GetNbinsY(); iy <= ny; ++iy) {
            for (int iz = 1, nz = h3.GetNbinsZ(); iz <= nz; ++iz) {
                x->setVal(h3.GetXaxis()->GetBinCenter(ix));
                y->setVal(h3.GetYaxis()->GetBinCenter(iy));
                z->setVal(h3.GetYaxis()->GetBinCenter(iz));
                data->add(observables_, asimov ? h3.GetBinContent(ix,iy,iz) : RooRandom::randomGenerator()->Poisson(h3.GetBinContent(ix,iy,iz)) );
            } } }
            }
    }
    RooAbsArg::setDirtyInhibit(false); // restore proper propagation of dirty flags
    if (!keepHistoSpec_) { delete histoSpec_; histoSpec_ = 0; }
    //std::cout << "Asimov dataset generated from " << pdf_->GetName() << " (sumw? " << data->sumEntries() << ", expected events " << expectedEvents << ")" << std::endl;
    //utils::printRDH(data);
    return data;
}
const RooAbsPdf* toymcoptutils::SinglePdfGenInfo::pdf ( ) const [inline]

Definition at line 17 of file ToyMCSamplerOpt.h.

References pdf_.

{ return pdf_; }
void toymcoptutils::SinglePdfGenInfo::setCacheTemplates ( bool  cache) [inline]

Definition at line 18 of file ToyMCSamplerOpt.h.

References tests::test_CacheProxy::cache, and keepHistoSpec_.

void toymcoptutils::SinglePdfGenInfo::setToExpected ( RooProdPdf &  prod,
RooArgSet &  obs 
) [private]

Definition at line 235 of file ToyMCSamplerOpt.cc.

References a.

{
    std::auto_ptr<TIterator> iter(prod.pdfList().createIterator());
    for (RooAbsArg *a = (RooAbsArg *) iter->Next(); a != 0; a = (RooAbsArg *) iter->Next()) {
        if (!a->dependsOn(obs)) continue;
        RooPoisson *pois = 0;
        if ((pois = dynamic_cast<RooPoisson *>(a)) != 0) {
            setToExpected(*pois, obs);
        } else {
            RooProdPdf *subprod = dynamic_cast<RooProdPdf *>(a);
            if (subprod) setToExpected(*subprod, obs);
            else throw std::logic_error("Illegal term in counting model: depends on observables, but not Poisson or Product");
        }
    }
}
void toymcoptutils::SinglePdfGenInfo::setToExpected ( RooPoisson &  pois,
RooArgSet &  obs 
) [private]

Definition at line 252 of file ToyMCSamplerOpt.cc.

References a.

{
    RooRealVar *myobs = 0;
    RooAbsReal *myexp = 0;
    std::auto_ptr<TIterator> iter(pois.serverIterator());
    for (RooAbsArg *a = (RooAbsArg *) iter->Next(); a != 0; a = (RooAbsArg *) iter->Next()) {
        if (obs.contains(*a)) {
            assert(myobs == 0 && "SinglePdfGenInfo::setToExpected(RooPoisson): Two observables??");
            myobs = dynamic_cast<RooRealVar *>(a);
            assert(myobs != 0 && "SinglePdfGenInfo::setToExpected(RooPoisson): Observables is not a RooRealVar??");
        } else {
            assert(myexp == 0 && "SinglePdfGenInfo::setToExpected(RooPoisson): Two expecteds??");
            myexp = dynamic_cast<RooAbsReal *>(a);
            assert(myexp != 0 && "SinglePdfGenInfo::setToExpected(RooPoisson): Expectedis not a RooAbsReal??");
        }
    }
    assert(myobs != 0 && "SinglePdfGenInfo::setToExpected(RooPoisson): No observable?");
    assert(myexp != 0 && "SinglePdfGenInfo::setToExpected(RooPoisson): No expected?");
    myobs->setVal(myexp->getVal());
}

Member Data Documentation

Definition at line 24 of file ToyMCSamplerOpt.h.

Definition at line 25 of file ToyMCSamplerOpt.h.

Referenced by setCacheTemplates().

Definition at line 20 of file ToyMCSamplerOpt.h.

Referenced by SinglePdfGenInfo().

Definition at line 22 of file ToyMCSamplerOpt.h.

Referenced by SinglePdfGenInfo().

Definition at line 21 of file ToyMCSamplerOpt.h.

Referenced by pdf().

RooAbsPdf::GenSpec* toymcoptutils::SinglePdfGenInfo::spec_ [private]

Definition at line 23 of file ToyMCSamplerOpt.h.

Definition at line 26 of file ToyMCSamplerOpt.h.