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

#include <ToyMCSamplerOpt.h>

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

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.

56  :
57  mode_(pdf.canBeExtended() ? (preferBinned ? Binned : Unbinned) : Counting),
58  pdf_(&pdf),
60 {
61  if (pdf.canBeExtended()) {
62  if (pdf.getAttribute("forceGenBinned")) mode_ = Binned;
63  else if (pdf.getAttribute("forceGenPoisson")) mode_ = Poisson;
64  else if (pdf.getAttribute("forceGenUnbinned")) mode_ = Unbinned;
65  }
66 
67  RooArgSet *obs = pdf.getObservables(observables);
68  observables_.add(*obs);
69  delete obs;
70  if (mode_ == Binned) {
71  if (runtimedef::get("TMCSO_GenBinned")) mode_ = BinnedNoWorkaround;
72  else if (runtimedef::get("TMCSO_GenBinnedWorkaround")) mode_ = Binned;
73  else mode_ = Poisson;
74  } else if (mode_ == Unbinned) {
75  //if (!runtimedef::get("TMCSO_NoPrepareMultiGen")) {
76  // spec_ = protoData ? pdf.prepareMultiGen(observables_, RooFit::Extended(), RooFit::ProtoData(*protoData, true, true))
77  // : pdf.prepareMultiGen(observables_, RooFit::Extended());
78  //}
79  }
80 }
int get(const char *name)
const RooAbsPdf * pdf() const
toymcoptutils::SinglePdfGenInfo::~SinglePdfGenInfo ( )

Definition at line 82 of file ToyMCSamplerOpt.cc.

83 {
84  delete spec_;
85  delete weightVar_;
86  delete histoSpec_;
87 }

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.

92 {
93  assert(forceEvents == 0 && "SinglePdfGenInfo: forceEvents must be zero at least for now");
94  RooAbsData *ret = 0;
95  switch (mode_) {
96  case Unbinned:
97  if (spec_ == 0) spec_ = protoData ? pdf_->prepareMultiGen(observables_, RooFit::Extended(), RooFit::ProtoData(*protoData, true, true))
98  : pdf_->prepareMultiGen(observables_, RooFit::Extended());
99  if (spec_) ret = pdf_->generate(*spec_);
100  else ret = pdf_->generate(observables_, RooFit::Extended());
101  break;
102  case Binned:
103  { // aka generateBinnedWorkaround
104  RooDataSet *data = pdf_->generate(observables_, RooFit::Extended());
105  ret = new RooDataHist(data->GetName(), "", *data->get(), *data);
106  delete data;
107  }
108  break;
109  case BinnedNoWorkaround:
110  ret = protoData ? pdf_->generateBinned(observables_, RooFit::Extended(), RooFit::ProtoData(*protoData, true, true))
111  : pdf_->generateBinned(observables_, RooFit::Extended());
112  break;
113  case Poisson:
114  ret = generateWithHisto(weightVar_, false);
115  break;
116  case Counting:
117  ret = pdf_->generate(observables_, 1);
118  break;
119  default:
120  throw std::logic_error("Mode not foreseen in SinglePdfGenInfo::generate");
121  }
122  //std::cout << "Dataset generated from " << pdf_->GetName() << " (weighted? " << ret->isWeighted() << ")" << std::endl;
123  //utils::printRAD(ret);
124  return ret;
125 }
RooDataSet * generateWithHisto(RooRealVar *&weightVar, bool asimov)
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
RooDataSet * toymcoptutils::SinglePdfGenInfo::generateAsimov ( RooRealVar *&  weightVar)

Definition at line 128 of file ToyMCSamplerOpt.cc.

References runtimedef::get().

129 {
130  static int nPA = runtimedef::get("TMCSO_PseudoAsimov");
131  if (nPA) return generatePseudoAsimov(weightVar, nPA);
132  return generateWithHisto(weightVar, true);
133 }
int get(const char *name)
RooDataSet * generatePseudoAsimov(RooRealVar *&weightVar, int nPoints)
RooDataSet * generateWithHisto(RooRealVar *&weightVar, bool asimov)
RooDataSet * toymcoptutils::SinglePdfGenInfo::generateCountingAsimov ( )
private

Definition at line 219 of file ToyMCSamplerOpt.cc.

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

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

Definition at line 136 of file ToyMCSamplerOpt.cc.

References data, and i.

137 {
138  if (mode_ == Unbinned) {
139  double expEvents = pdf_->expectedEvents(observables_);
140  std::auto_ptr<RooDataSet> data(pdf_->generate(observables_, nPoints));
141  if (weightVar == 0) weightVar = new RooRealVar("_weight_","",1.0);
142  RooArgSet obsPlusW(observables_); obsPlusW.add(*weightVar);
143  RooDataSet *rds = new RooDataSet(data->GetName(), "", obsPlusW, weightVar->GetName());
144  for (int i = 0; i < nPoints; ++i) {
145  observables_ = *data->get(i);
146  rds->add(observables_, expEvents/nPoints);
147  }
148  return rds;
149  } else {
150  return generateWithHisto(weightVar, true);
151  }
152 }
int i
Definition: DBlmapReader.cc:9
RooDataSet * generateWithHisto(RooRealVar *&weightVar, bool asimov)
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
RooDataSet * toymcoptutils::SinglePdfGenInfo::generateWithHisto ( RooRealVar *&  weightVar,
bool  asimov 
)
private

Definition at line 156 of file ToyMCSamplerOpt.cc.

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

157 {
158  if (mode_ == Counting) return generateCountingAsimov();
159  if (observables_.getSize() > 3) throw std::invalid_argument(std::string("ERROR in SinglePdfGenInfo::generateWithHisto for ") + pdf_->GetName() + ", more than 3 observable");
160  RooArgList obs(observables_);
161  RooRealVar *x = (RooRealVar*)obs.at(0);
162  RooRealVar *y = obs.getSize() > 1 ? (RooRealVar*)obs.at(1) : 0;
163  RooRealVar *z = obs.getSize() > 2 ? (RooRealVar*)obs.at(2) : 0;
164  if (weightVar == 0) weightVar = new RooRealVar("_weight_","",1.0);
165 
166  RooCmdArg ay = (y ? RooFit::YVar(*y) : RooCmdArg::none());
167  RooCmdArg az = (z ? RooFit::ZVar(*z) : RooCmdArg::none());
168 
169  if (histoSpec_ == 0) {
170  histoSpec_ = pdf_->createHistogram("htemp", *x, ay, az);
171  histoSpec_->SetDirectory(0);
172  }
173 
174  double expectedEvents = pdf_->expectedEvents(observables_);
175  histoSpec_->Scale(expectedEvents/ histoSpec_->Integral());
176  RooArgSet obsPlusW(obs); obsPlusW.add(*weightVar);
177  RooDataSet *data = new RooDataSet(TString::Format("%sData", pdf_->GetName()), "", obsPlusW, weightVar->GetName());
178  RooAbsArg::setDirtyInhibit(true); // don't propagate dirty flags while filling histograms
179  switch (obs.getSize()) {
180  case 1:
181  for (int i = 1, n = histoSpec_->GetNbinsX(); i <= n; ++i) {
182  x->setVal(histoSpec_->GetXaxis()->GetBinCenter(i));
183  data->add(observables_, asimov ? histoSpec_->GetBinContent(i) : RooRandom::randomGenerator()->Poisson(histoSpec_->GetBinContent(i)) );
184  }
185  break;
186  case 2:
187  {
188  TH2& h2 = dynamic_cast<TH2&>(*histoSpec_);
189  for (int ix = 1, nx = h2.GetNbinsX(); ix <= nx; ++ix) {
190  for (int iy = 1, ny = h2.GetNbinsY(); iy <= ny; ++iy) {
191  x->setVal(h2.GetXaxis()->GetBinCenter(ix));
192  y->setVal(h2.GetYaxis()->GetBinCenter(iy));
193  data->add(observables_, asimov ? h2.GetBinContent(ix,iy) : RooRandom::randomGenerator()->Poisson(h2.GetBinContent(ix,iy)) );
194  } }
195  }
196  break;
197  case 3:
198  {
199  TH3& h3 = dynamic_cast<TH3&>(*histoSpec_);
200  for (int ix = 1, nx = h3.GetNbinsX(); ix <= nx; ++ix) {
201  for (int iy = 1, ny = h3.GetNbinsY(); iy <= ny; ++iy) {
202  for (int iz = 1, nz = h3.GetNbinsZ(); iz <= nz; ++iz) {
203  x->setVal(h3.GetXaxis()->GetBinCenter(ix));
204  y->setVal(h3.GetYaxis()->GetBinCenter(iy));
205  z->setVal(h3.GetYaxis()->GetBinCenter(iz));
206  data->add(observables_, asimov ? h3.GetBinContent(ix,iy,iz) : RooRandom::randomGenerator()->Poisson(h3.GetBinContent(ix,iy,iz)) );
207  } } }
208  }
209  }
210  RooAbsArg::setDirtyInhibit(false); // restore proper propagation of dirty flags
211  if (!keepHistoSpec_) { delete histoSpec_; histoSpec_ = 0; }
212  //std::cout << "Asimov dataset generated from " << pdf_->GetName() << " (sumw? " << data->sumEntries() << ", expected events " << expectedEvents << ")" << std::endl;
213  //utils::printRDH(data);
214  return data;
215 }
int i
Definition: DBlmapReader.cc:9
double double double z
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
x
Definition: VDTMath.h:216
const RooAbsPdf* toymcoptutils::SinglePdfGenInfo::pdf ( ) const
inline

Definition at line 17 of file ToyMCSamplerOpt.h.

References pdf_.

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

Definition at line 18 of file ToyMCSamplerOpt.h.

References keepHistoSpec_.

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

Definition at line 235 of file ToyMCSamplerOpt.cc.

References a.

236 {
237  std::auto_ptr<TIterator> iter(prod.pdfList().createIterator());
238  for (RooAbsArg *a = (RooAbsArg *) iter->Next(); a != 0; a = (RooAbsArg *) iter->Next()) {
239  if (!a->dependsOn(obs)) continue;
240  RooPoisson *pois = 0;
241  if ((pois = dynamic_cast<RooPoisson *>(a)) != 0) {
242  setToExpected(*pois, obs);
243  } else {
244  RooProdPdf *subprod = dynamic_cast<RooProdPdf *>(a);
245  if (subprod) setToExpected(*subprod, obs);
246  else throw std::logic_error("Illegal term in counting model: depends on observables, but not Poisson or Product");
247  }
248  }
249 }
void setToExpected(RooProdPdf &prod, RooArgSet &obs)
double a
Definition: hdecay.h:121
void toymcoptutils::SinglePdfGenInfo::setToExpected ( RooPoisson &  pois,
RooArgSet &  obs 
)
private

Definition at line 252 of file ToyMCSamplerOpt.cc.

References a.

253 {
254  RooRealVar *myobs = 0;
255  RooAbsReal *myexp = 0;
256  std::auto_ptr<TIterator> iter(pois.serverIterator());
257  for (RooAbsArg *a = (RooAbsArg *) iter->Next(); a != 0; a = (RooAbsArg *) iter->Next()) {
258  if (obs.contains(*a)) {
259  assert(myobs == 0 && "SinglePdfGenInfo::setToExpected(RooPoisson): Two observables??");
260  myobs = dynamic_cast<RooRealVar *>(a);
261  assert(myobs != 0 && "SinglePdfGenInfo::setToExpected(RooPoisson): Observables is not a RooRealVar??");
262  } else {
263  assert(myexp == 0 && "SinglePdfGenInfo::setToExpected(RooPoisson): Two expecteds??");
264  myexp = dynamic_cast<RooAbsReal *>(a);
265  assert(myexp != 0 && "SinglePdfGenInfo::setToExpected(RooPoisson): Expectedis not a RooAbsReal??");
266  }
267  }
268  assert(myobs != 0 && "SinglePdfGenInfo::setToExpected(RooPoisson): No observable?");
269  assert(myexp != 0 && "SinglePdfGenInfo::setToExpected(RooPoisson): No expected?");
270  myobs->setVal(myexp->getVal());
271 }
double a
Definition: hdecay.h:121

Member Data Documentation

TH1* toymcoptutils::SinglePdfGenInfo::histoSpec_
private

Definition at line 24 of file ToyMCSamplerOpt.h.

bool toymcoptutils::SinglePdfGenInfo::keepHistoSpec_
private

Definition at line 25 of file ToyMCSamplerOpt.h.

Referenced by setCacheTemplates().

Mode toymcoptutils::SinglePdfGenInfo::mode_
private

Definition at line 20 of file ToyMCSamplerOpt.h.

Referenced by SinglePdfGenInfo().

RooArgSet toymcoptutils::SinglePdfGenInfo::observables_
private

Definition at line 22 of file ToyMCSamplerOpt.h.

Referenced by SinglePdfGenInfo().

RooAbsPdf* toymcoptutils::SinglePdfGenInfo::pdf_
private

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.

RooRealVar* toymcoptutils::SinglePdfGenInfo::weightVar_
private

Definition at line 26 of file ToyMCSamplerOpt.h.