Logo ROOT   6.10/09
Reference Guide
ToyMCSampler.cxx
Go to the documentation of this file.
1 // @(#)root/roostats:$Id$
2 // Author: Sven Kreiss June 2010
3 // Author: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke
4 /*************************************************************************
5  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /** \class RooStats::NuisanceParametersSampler
13  \ingroup Roostats
14 
15 Helper class for ToyMCSampler. Handles all of the nuisance parameter related
16 functions. Once instantiated, it gives a new nuisance parameter point
17 at each call to nextPoint(...).
18 Only used inside ToyMCSampler, ie "private" in the cxx file
19 */
20 
21 /** \class RooStats::ToyMCSampler
22  \ingroup Roostats
23 
24 ToyMCSampler is an implementation of the TestStatSampler interface.
25 It generates Toy Monte Carlo for a given parameter point and evaluates a
26 TestStatistic.
27 
28 For parallel runs, ToyMCSampler can be given an instance of ProofConfig
29 and then run in parallel using proof or proof-lite. Internally, it uses
30 ToyMCStudy with the RooStudyManager.
31 */
32 
33 #include "RooStats/ToyMCSampler.h"
34 
35 #include "RooMsgService.h"
36 
37 #include "RooDataHist.h"
38 
39 #include "RooRealVar.h"
40 
41 #include "TCanvas.h"
42 #include "RooPlot.h"
43 #include "RooRandom.h"
44 
45 #include "RooStudyManager.h"
46 #include "RooStats/ToyMCStudy.h"
48 #include "RooStats/RooStatsUtils.h"
49 #include "RooSimultaneous.h"
50 #include "RooCategory.h"
51 
52 #include "TMath.h"
53 
54 
55 using namespace RooFit;
56 using namespace std;
57 
58 
60 
61 namespace RooStats {
62 
63 ////////////////////////////////////////////////////////////////////////////////
64 /// Assigns new nuisance parameter point to members of nuisPoint.
65 /// nuisPoint can be more objects than just the nuisance
66 /// parameters.
67 
68 void NuisanceParametersSampler::NextPoint(RooArgSet& nuisPoint, Double_t& weight) {
69 
70  // check whether to get new set of nuisanceParPoints
71  if (fIndex >= fNToys) {
72  Refresh();
73  fIndex = 0;
74  }
75 
76  // get value
77  nuisPoint = *fPoints->get(fIndex++);
78  weight = fPoints->weight();
79 
80  // check whether result will have any influence
81  if(fPoints->weight() == 0.0) {
82  oocoutI((TObject*)NULL,Generation) << "Weight 0 encountered. Skipping." << endl;
83  NextPoint(nuisPoint, weight);
84  }
85 }
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 /// Creates the initial set of nuisance parameter points. It also refills the
89 /// set with new parameter points if called repeatedly. This helps with
90 /// adaptive sampling as the required number of nuisance parameter points
91 /// might increase during the run.
92 
93 void NuisanceParametersSampler::Refresh() {
94 
95  if (!fPrior || !fParams) return;
96 
97  if (fPoints) delete fPoints;
98 
99  if (fExpected) {
100  // UNDER CONSTRUCTION
101  oocoutI((TObject*)NULL,InputArguments) << "Using expected nuisance parameters." << endl;
102 
103  int nBins = fNToys;
104 
105  // From FeldmanCousins.cxx:
106  // set nbins for the POI
107  TIter it2 = fParams->createIterator();
108  RooRealVar *myarg2;
109  while ((myarg2 = dynamic_cast<RooRealVar*>(it2.Next()))) {
110  myarg2->setBins(nBins);
111  }
112 
113 
114  fPoints = fPrior->generate(
115  *fParams,
116  AllBinned(),
117  ExpectedData(),
118  NumEvents(1) // for Asimov set, this is only a scale factor
119  );
120  if(fPoints->numEntries() != fNToys) {
121  fNToys = fPoints->numEntries();
122  oocoutI((TObject*)NULL,InputArguments) <<
123  "Adjusted number of toys to number of bins of nuisance parameters: " << fNToys << endl;
124  }
125 
126 /*
127  // check
128  TCanvas *c1 = new TCanvas;
129  RooPlot *p = dynamic_cast<RooRealVar*>(fParams->first())->frame();
130  fPoints->plotOn(p);
131  p->Draw();
132  for(int x=0; x < fPoints->numEntries(); x++) {
133  fPoints->get(x)->Print("v");
134  cout << fPoints->weight() << endl;
135  }
136 */
137 
138  }else{
139  oocoutI((TObject*)NULL,InputArguments) << "Using randomized nuisance parameters." << endl;
140 
141  fPoints = fPrior->generate(*fParams, fNToys);
142  }
143 }
144 
145 Bool_t ToyMCSampler::fgAlwaysUseMultiGen = kFALSE ;
146 
147 ////////////////////////////////////////////////////////////////////////////////
148 
149 void ToyMCSampler::SetAlwaysUseMultiGen(Bool_t flag) { fgAlwaysUseMultiGen = flag ; }
150 
151 ////////////////////////////////////////////////////////////////////////////////
152 /// Proof constructor. Do not use.
153 
154 ToyMCSampler::ToyMCSampler() : fSamplingDistName("SD"), fNToys(1)
155 {
156 
157  fPdf = NULL;
161  fObservables = NULL;
163 
164  fSize = 0.05;
165  fNEvents = 0;
167  fGenerateBinnedTag = "";
170 
171  fToysInTails = 0.0;
175 
176  fProtoData = NULL;
177 
178  fProofConfig = NULL;
180 
181  _allVars = NULL ;
182  _gs1 = NULL ;
183  _gs2 = NULL ;
184  _gs3 = NULL ;
185  _gs4 = NULL ;
186 
187  //suppress messages for num integration of Roofit
189 
190  fUseMultiGen = kFALSE ;
191 }
192 
193 ////////////////////////////////////////////////////////////////////////////////
194 
196  fSamplingDistName(ts.GetVarName()), fNToys(ntoys)
197 {
198  fPdf = NULL;
202  fObservables = NULL;
204 
205  fSize = 0.05;
206  fNEvents = 0;
208  fGenerateBinnedTag = "";
211 
212  fToysInTails = 0.0;
216 
217  fProtoData = NULL;
218 
219  fProofConfig = NULL;
221 
222  _allVars = NULL ;
223  _gs1 = NULL ;
224  _gs2 = NULL ;
225  _gs3 = NULL ;
226  _gs4 = NULL ;
227 
228  //suppress messages for num integration of Roofit
230 
231  fUseMultiGen = kFALSE ;
232 
233  AddTestStatistic(&ts);
234 }
235 
236 ////////////////////////////////////////////////////////////////////////////////
237 
240 
241  ClearCache();
242 }
243 
244 ////////////////////////////////////////////////////////////////////////////////
245 /// only checks, no guessing/determination (do this in calculators,
246 /// e.g. using ModelConfig::GuessObsAndNuisance(...))
247 
249  bool goodConfig = true;
250 
251  if(fTestStatistics.size() == 0 || fTestStatistics[0] == NULL) { ooccoutE((TObject*)NULL,InputArguments) << "Test statistic not set." << endl; goodConfig = false; }
252  if(!fObservables) { ooccoutE((TObject*)NULL,InputArguments) << "Observables not set." << endl; goodConfig = false; }
253  if(!fParametersForTestStat) { ooccoutE((TObject*)NULL,InputArguments) << "Parameter values used to evaluate the test statistic are not set." << endl; goodConfig = false; }
254  if(!fPdf) { ooccoutE((TObject*)NULL,InputArguments) << "Pdf not set." << endl; goodConfig = false; }
255 
256 
257  //ooccoutI((TObject*)NULL,InputArguments) << "ToyMCSampler configuration:" << endl;
258  //ooccoutI((TObject*)NULL,InputArguments) << "Pdf from SetPdf: " << fPdf << endl;
259  // for( unsigned int i=0; i < fTestStatistics.size(); i++ ) {
260  // ooccoutI((TObject*)NULL,InputArguments) << "test statistics["<<i<<"]: " << fTestStatistics[i] << endl;
261  // }
262  //ooccoutI((TObject*)NULL,InputArguments) << endl;
263 
264  return goodConfig;
265 }
266 
267 ////////////////////////////////////////////////////////////////////////////////
268 /// Evaluate all test statistics, returning result and any detailed output.
269 /// PDF parameter values are saved in case they are modified by
270 /// TestStatistic::Evaluate (eg. SimpleLikelihoodRatioTestStat).
271 
273  DetailedOutputAggregator detOutAgg;
274  const RooArgList* allTS = EvaluateAllTestStatistics(data, poi, detOutAgg);
275  if (!allTS) return 0;
276  // no need to delete allTS, it is deleted in destructor of detOutAgg
277  return dynamic_cast<RooArgList*>(allTS->snapshot());
278 }
279 
280 ////////////////////////////////////////////////////////////////////////////////
281 
283  RooArgSet *allVars = fPdf ? fPdf->getVariables() : 0;
284  RooArgSet *saveAll = allVars ? dynamic_cast<RooArgSet*>(allVars->snapshot()) : 0;
285  for( unsigned int i = 0; i < fTestStatistics.size(); i++ ) {
286  if( fTestStatistics[i] == NULL ) continue;
287  TString name( TString::Format("%s_TS%u", fSamplingDistName.c_str(), i) );
288  RooArgSet* parForTS = dynamic_cast<RooArgSet*>(poi.snapshot());
289  RooRealVar ts( name, fTestStatistics[i]->GetVarName(), fTestStatistics[i]->Evaluate( data, *parForTS ) );
290  RooArgList tset(ts);
291  detOutAgg.AppendArgSet(&tset);
292  delete parForTS;
293  if (const RooArgSet* detOut = fTestStatistics[i]->GetDetailedOutput()) {
294  name.Append("_");
295  detOutAgg.AppendArgSet(detOut, name);
296  }
297  if (saveAll) *allVars = *saveAll; // restore values, perhaps modified by fTestStatistics[i]->Evaluate()
298  }
299  delete saveAll;
300  delete allVars;
301  return detOutAgg.GetAsArgList();
302 }
303 
304 ////////////////////////////////////////////////////////////////////////////////
305 
307  if(fTestStatistics.size() > 1) {
308  oocoutW((TObject*)NULL, InputArguments) << "Multiple test statistics defined, but only one distribution will be returned." << endl;
309  for( unsigned int i=0; i < fTestStatistics.size(); i++ ) {
310  oocoutW((TObject*)NULL, InputArguments) << " \t test statistic: " << fTestStatistics[i] << endl;
311  }
312  }
313 
314  RooDataSet* r = GetSamplingDistributions(paramPointIn);
315  if(r == NULL || r->numEntries() == 0) {
316  oocoutW((TObject*)NULL, Generation) << "no sampling distribution generated" << endl;
317  return NULL;
318  }
319 
320  SamplingDistribution* samp = new SamplingDistribution( r->GetName(), r->GetTitle(), *r );
321  delete r;
322  return samp;
323 }
324 
325 ////////////////////////////////////////////////////////////////////////////////
326 /// Use for serial and parallel runs.
327 
329 {
330 
331  // ======= S I N G L E R U N ? =======
332  if(!fProofConfig)
333  return GetSamplingDistributionsSingleWorker(paramPointIn);
334 
335  // ======= P A R A L L E L R U N =======
336  if (!CheckConfig()){
338  << "Bad COnfiguration in ToyMCSampler "
339  << endl;
340  return nullptr;
341  }
342 
343  // turn adaptive sampling off if given
344  if(fToysInTails) {
345  fToysInTails = 0;
347  << "Adaptive sampling in ToyMCSampler is not supported for parallel runs."
348  << endl;
349  }
350 
351  // adjust number of toys on the slaves to keep the total number of toys constant
352  Int_t totToys = fNToys;
353  fNToys = (int)ceil((double)fNToys / (double)fProofConfig->GetNExperiments()); // round up
354 
355  // create the study instance for parallel processing
356  ToyMCStudy* toymcstudy = new ToyMCStudy ;
357  toymcstudy->SetToyMCSampler(*this);
358  toymcstudy->SetParamPoint(paramPointIn);
360 
361  // temporary workspace for proof to avoid messing with TRef
363  RooStudyManager studymanager(w, *toymcstudy);
364  studymanager.runProof(fProofConfig->GetNExperiments(), fProofConfig->GetHost(), fProofConfig->GetShowGui());
365 
366  RooDataSet* output = toymcstudy->merge();
367 
368  // reset the number of toys
369  fNToys = totToys;
370 
371  delete toymcstudy;
372  return output;
373 }
374 
375 ////////////////////////////////////////////////////////////////////////////////
376 /// This is the main function for serial runs. It is called automatically
377 /// from inside GetSamplingDistribution when no ProofConfig is given.
378 /// You should not call this function yourself. This function should
379 /// be used by ToyMCStudy on the workers (ie. when you explicitly want
380 /// a serial run although ProofConfig is present).
381 ///
382 /// Make sure the cache is clear. It is important to clear it hear, because
383 /// the cache might be invalid even when just the firstPOI was changed, for which
384 /// no accessor has to be called. (Fixes a bug when ToyMCSampler is
385 /// used with the Neyman Construction)
386 
388 {
389  ClearCache();
390 
391  if (!CheckConfig()){
393  << "Bad COnfiguration in ToyMCSampler "
394  << endl;
395  return nullptr;
396  }
397 
398  // important to cache the paramPoint b/c test statistic might
399  // modify it from event to event
400  RooArgSet *paramPoint = (RooArgSet*) paramPointIn.snapshot();
401  RooArgSet *allVars = fPdf->getVariables();
402  RooArgSet *saveAll = (RooArgSet*) allVars->snapshot();
403 
404 
405  DetailedOutputAggregator detOutAgg;
406 
407  // counts the number of toys in the limits set for adaptive sampling
408  // (taking weights into account; always on first test statistic)
409  Double_t toysInTails = 0.0;
410 
411  for (Int_t i = 0; i < fMaxToys; ++i) {
412  // need to check at the beginning for case that zero toys are requested
413  if (toysInTails >= fToysInTails && i+1 > fNToys) break;
414 
415  // status update
416  if ( i% 500 == 0 && i>0 ) {
417  oocoutP((TObject*)0,Generation) << "generated toys: " << i << " / " << fNToys;
418  if (fToysInTails) ooccoutP((TObject*)0,Generation) << " (tails: " << toysInTails << " / " << fToysInTails << ")" << std::endl;
419  else ooccoutP((TObject*)0,Generation) << endl;
420  }
421 
422  // TODO: change this treatment to keep track of all values so that the threshold
423  // for adaptive sampling is counted for all distributions and not just the
424  // first one.
425  Double_t valueFirst = -999.0, weight = 1.0;
426 
427  // set variables to requested parameter point
428  *allVars = *saveAll; // important for example for SimpleLikelihoodRatioTestStat
429 
430  RooAbsData* toydata = GenerateToyData(*paramPoint, weight);
431 
432  *allVars = *fParametersForTestStat;
433 
434  const RooArgList* allTS = EvaluateAllTestStatistics(*toydata, *fParametersForTestStat, detOutAgg);
435  if (allTS->getSize() > Int_t(fTestStatistics.size()))
436  detOutAgg.AppendArgSet( fGlobalObservables, "globObs_" );
437  if (RooRealVar* firstTS = dynamic_cast<RooRealVar*>(allTS->first()))
438  valueFirst = firstTS->getVal();
439 
440  delete toydata;
441 
442  // check for nan
443  if(valueFirst != valueFirst) {
444  oocoutW((TObject*)NULL, Generation) << "skip: " << valueFirst << ", " << weight << endl;
445  continue;
446  }
447 
448  detOutAgg.CommitSet(weight);
449 
450  // adaptive sampling checks
451  if (valueFirst <= fAdaptiveLowLimit || valueFirst >= fAdaptiveHighLimit) {
452  if(weight >= 0.) toysInTails += weight;
453  else toysInTails += 1.;
454  }
455  }
456 
457  // clean up
458  *allVars = *saveAll;
459  delete saveAll;
460  delete allVars;
461  delete paramPoint;
462 
464 }
465 
466 ////////////////////////////////////////////////////////////////////////////////
467 
469 
470 
472  ooccoutE((TObject*)NULL,InputArguments) << "Global Observables not set." << endl;
473  return;
474  }
475 
476 
478 
479  // generate one set of global observables and assign it
480  // has problem for sim pdfs
481  RooSimultaneous* simPdf = dynamic_cast<RooSimultaneous*>( &pdf );
482  if (!simPdf) {
483  RooDataSet *one = pdf.generate(*fGlobalObservables, 1);
484 
485  const RooArgSet *values = one->get(0);
486  if (!_allVars) {
487  _allVars = pdf.getVariables();
488  }
489  *_allVars = *values;
490  delete one;
491 
492  } else {
493 
494  if (_pdfList.size() == 0) {
495  RooCategory& channelCat = (RooCategory&)simPdf->indexCat();
496  int nCat = channelCat.numTypes();
497  for (int i=0; i < nCat; ++i){
498  channelCat.setIndex(i);
499  RooAbsPdf* pdftmp = simPdf->getPdf(channelCat.getLabel());
500  assert(pdftmp);
501  RooArgSet* globtmp = pdftmp->getObservables(*fGlobalObservables);
502  RooAbsPdf::GenSpec* gs = pdftmp->prepareMultiGen(*globtmp, NumEvents(1));
503  _pdfList.push_back(pdftmp);
504  _obsList.push_back(globtmp);
505  _gsList.push_back(gs);
506  }
507  }
508 
509  list<RooArgSet*>::iterator oiter = _obsList.begin();
510  list<RooAbsPdf::GenSpec*>::iterator giter = _gsList.begin();
511  for (list<RooAbsPdf*>::iterator iter = _pdfList.begin(); iter != _pdfList.end(); ++iter, ++giter, ++oiter) {
512  //RooDataSet* tmp = (*iter)->generate(**oiter,1) ;
513  RooDataSet* tmp = (*iter)->generate(**giter);
514  **oiter = *tmp->get(0);
515  delete tmp;
516  }
517  }
518 
519 
520  } else {
521 
522  // not using multigen for global observables
524  const RooArgSet *values = one->get(0);
525  RooArgSet* allVars = pdf.getVariables();
526  *allVars = *values;
527  delete allVars;
528  delete one;
529 
530  }
531 }
532 
533 ////////////////////////////////////////////////////////////////////////////////
534 /// This method generates a toy data set for the given parameter point taking
535 /// global observables into account.
536 /// The values of the generated global observables remain in the pdf's variables.
537 /// They have to have those values for the subsequent evaluation of the
538 /// test statistics.
539 
540 RooAbsData* ToyMCSampler::GenerateToyData(RooArgSet& paramPoint, double& weight, RooAbsPdf& pdf) const {
541 
542  if(!fObservables) {
543  ooccoutE((TObject*)NULL,InputArguments) << "Observables not set." << endl;
544  return NULL;
545  }
546 
547  // assign input paramPoint
548  RooArgSet* allVars = fPdf->getVariables();
549  *allVars = paramPoint;
550 
551 
552  // create nuisance parameter points
556  oocoutI((TObject*)NULL,InputArguments) << "Cannot use multigen when nuisance parameters vary for every toy" << endl;
557  }
558 
559  // generate global observables
560  RooArgSet observables(*fObservables);
562  observables.remove(*fGlobalObservables);
564  }
565 
566  // save values to restore later.
567  // but this must remain after(!) generating global observables
568  const RooArgSet* saveVars = (const RooArgSet*)allVars->snapshot();
569 
570  if(fNuisanceParametersSampler) { // use nuisance parameters?
571  // Construct a set of nuisance parameters that has the parameters
572  // in the input paramPoint removed. Therefore, no parameter in
573  // paramPoint is randomized.
574  // Therefore when a parameter is given (should be held fixed),
575  // but is also in the list of nuisance parameters, the parameter
576  // will be held fixed. This is useful for debugging to hold single
577  // parameters fixed although under "normal" circumstances it is
578  // randomized.
579  RooArgSet allVarsMinusParamPoint(*allVars);
580  allVarsMinusParamPoint.remove(paramPoint, kFALSE, kTRUE); // match by name
581 
582  // get nuisance parameter point and weight
583  fNuisanceParametersSampler->NextPoint(allVarsMinusParamPoint, weight);
584 
585 
586  }else{
587  weight = 1.0;
588  }
589 
590  RooAbsData *data = Generate(pdf, observables);
591 
592  // We generated the data with the randomized nuisance parameter (if hybrid)
593  // but now we are setting the nuisance parameters back to where they were.
594  *allVars = *saveVars;
595  delete allVars;
596  delete saveVars;
597 
598  return data;
599 }
600 
601 ////////////////////////////////////////////////////////////////////////////////
602 /// This is the generate function to use in the context of the ToyMCSampler
603 /// instead of the standard RooAbsPdf::generate(...).
604 /// It takes into account whether the number of events is given explicitly
605 /// or whether it should use the expected number of events. It also takes
606 /// into account the option to generate a binned data set (ie RooDataHist).
607 
608 RooAbsData* ToyMCSampler::Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooDataSet* protoData, int forceEvents) const {
609 
610  if(fProtoData) {
611  protoData = fProtoData;
612  forceEvents = protoData->numEntries();
613  }
614 
615  RooAbsData *data = NULL;
616  int events = forceEvents;
617  if(events == 0) events = fNEvents;
618 
619  // cannot use multigen when the nuisance parameters change for every toy
620  bool useMultiGen = (fUseMultiGen || fgAlwaysUseMultiGen) && !fNuisanceParametersSampler;
621 
622  if(events == 0) {
623  if( pdf.canBeExtended() && pdf.expectedEvents(observables) > 0) {
624  if(fGenerateBinned) {
625  if(protoData) data = pdf.generate(observables, AllBinned(), Extended(), ProtoData(*protoData, true, true));
626  else data = pdf.generate(observables, AllBinned(), Extended());
627  }else{
628  if(protoData) {
629  if (useMultiGen) {
630  if (!_gs2) { _gs2 = pdf.prepareMultiGen(observables, Extended(), AutoBinned(fGenerateAutoBinned), GenBinned(fGenerateBinnedTag), ProtoData(*protoData, true, true)) ; }
631  data = pdf.generate(*_gs2) ;
632  } else {
633  data = pdf.generate (observables, Extended(), AutoBinned(fGenerateAutoBinned), GenBinned(fGenerateBinnedTag), ProtoData(*protoData, true, true));
634  }
635  }
636  else {
637  if (useMultiGen) {
639  data = pdf.generate(*_gs1) ;
640  } else {
642  }
643 
644  }
645  }
646  }else{
648  << "ToyMCSampler: Error : pdf is not extended and number of events per toy is zero"
649  << endl;
650  }
651  }else{
652  if(fGenerateBinned) {
653  if(protoData) data = pdf.generate(observables, events, AllBinned(), ProtoData(*protoData, true, true));
654  else data = pdf.generate(observables, events, AllBinned());
655  }else{
656  if(protoData) {
657  if (useMultiGen) {
658  if (!_gs3) { _gs3 = pdf.prepareMultiGen(observables, NumEvents(events), AutoBinned(fGenerateAutoBinned), GenBinned(fGenerateBinnedTag), ProtoData(*protoData, true, true)); }
659  data = pdf.generate(*_gs3) ;
660  } else {
661  data = pdf.generate (observables, NumEvents(events), AutoBinned(fGenerateAutoBinned), GenBinned(fGenerateBinnedTag), ProtoData(*protoData, true, true));
662  }
663  } else {
664  if (useMultiGen) {
666  data = pdf.generate(*_gs4) ;
667  } else {
668  data = pdf.generate (observables, NumEvents(events), AutoBinned(fGenerateAutoBinned), GenBinned(fGenerateBinnedTag));
669  }
670  }
671  }
672  }
673 
674  // in case of number counting print observables
675  // if (data->numEntries() == 1) {
676  // std::cout << "generate observables : ";
677  // RooStats::PrintListContent(*data->get(0), std::cout);
678  // }
679 
680  return data;
681 }
682 
683 ////////////////////////////////////////////////////////////////////////////////
684 /// Extended interface to append to sampling distribution more samples
685 
687  RooArgSet& allParameters,
688  SamplingDistribution* last,
689  Int_t additionalMC)
690 {
691  Int_t tmp = fNToys;
692  fNToys = additionalMC;
693  SamplingDistribution* newSamples = GetSamplingDistribution(allParameters);
694  fNToys = tmp;
695 
696  if(last){
697  last->Add(newSamples);
698  delete newSamples;
699  return last;
700  }
701 
702  return newSamples;
703 }
704 
705 ////////////////////////////////////////////////////////////////////////////////
706 /// clear the cache obtained from the pdf used for speeding the toy and global observables generation
707 /// needs to be called every time the model pdf (fPdf) changes
708 
710 
711  if (_gs1) delete _gs1;
712  _gs1 = 0;
713  if (_gs2) delete _gs2;
714  _gs2 = 0;
715  if (_gs3) delete _gs3;
716  _gs3 = 0;
717  if (_gs4) delete _gs4;
718  _gs4 = 0;
719 
720  // no need to delete the _pdfList since it is managed by the RooSimultaneous object
721  if (_pdfList.size() > 0) {
722  std::list<RooArgSet*>::iterator oiter = _obsList.begin();
723  for (std::list<RooAbsPdf::GenSpec*>::iterator giter = _gsList.begin(); giter != _gsList.end(); ++giter, ++oiter) {
724  delete *oiter;
725  delete *giter;
726  }
727  _pdfList.clear();
728  _obsList.clear();
729  _gsList.clear();
730  }
731 
732  //LM: is this set really needed ??
733  if (_allVars) delete _allVars;
734  _allVars = 0;
735 
736 }
737 
738 } // end namespace RooStats
RooArgSet * getVariables(Bool_t stripDisconnected=kTRUE) const
Return RooArgSet with all variables (tree leaf nodes of expresssion tree)
Definition: RooAbsArg.cxx:2082
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
RooCmdArg AllBinned()
RooArgSet * getObservables(const RooArgSet &set, Bool_t valueOnly=kTRUE) const
Definition: RooAbsArg.h:194
virtual SamplingDistribution * GetSamplingDistribution(RooArgSet &paramPoint)
const char * GetHost(void) const
Definition: ProofConfig.h:97
Bool_t GetShowGui(void) const
Definition: ProofConfig.h:101
virtual RooDataSet * GetSamplingDistributions(RooArgSet &paramPoint)
Use for serial and parallel runs.
virtual Bool_t setIndex(Int_t index, Bool_t printError=kTRUE)
Set value by specifying the index code of the desired state.
RooAbsData * Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooDataSet *protoData=NULL, int forceEvents=0) const
This is the generate function to use in the context of the ToyMCSampler instead of the standard RooAb...
#define oocoutI(o, a)
Definition: RooMsgService.h:44
RooAbsPdf::GenSpec * _gs1
Definition: ToyMCSampler.h:288
RooDataSet * merge()
Definition: ToyMCStudy.cxx:101
RooCmdArg NumEvents(Int_t numEvents)
RooWorkspace & GetWorkspace(void) const
Definition: ProofConfig.h:95
std::string fSamplingDistName
Definition: ToyMCSampler.h:253
int Int_t
Definition: RtypesCore.h:41
StreamConfig & getStream(Int_t id)
bool Bool_t
Definition: RtypesCore.h:59
GenSpec * prepareMultiGen(const RooArgSet &whatVars, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none())
Prepare GenSpec configuration object for efficient generation of multiple datasets from idetical spec...
Definition: RooAbsPdf.cxx:1855
NuisanceParametersSampler * fNuisanceParametersSampler
Definition: ToyMCSampler.h:281
void removeTopic(RooFit::MsgTopic oldTopic)
static RooMsgService & instance()
Return reference to singleton instance.
#define ooccoutP(o, a)
Definition: RooMsgService.h:52
STL namespace.
#define NULL
Definition: RtypesCore.h:88
RooCmdArg Extended(Bool_t flag=kTRUE)
void SetRandomSeed(unsigned int seed)
Definition: ToyMCStudy.h:60
This class is designed to aid in the construction of RooDataSets and RooArgSets, particularly those n...
#define ooccoutE(o, a)
Definition: RooMsgService.h:54
void SetParamPoint(const RooArgSet &paramPoint)
Definition: ToyMCStudy.h:58
#define oocoutP(o, a)
Definition: RooMsgService.h:45
ToyMCStudy is an implementation of RooAbsStudy for toy Monte Carlo sampling.
Definition: ToyMCStudy.h:32
Int_t numTypes(const char *=0) const
void NextPoint(RooArgSet &nuisPoint, Double_t &weight)
Assigns new nuisance parameter point to members of nuisPoint.
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
Definition: TString.cxx:2345
#define oocoutE(o, a)
Definition: RooMsgService.h:47
virtual SamplingDistribution * AppendSamplingDistribution(RooArgSet &allParameters, SamplingDistribution *last, Int_t additionalMC)
Extended interface to append to sampling distribution more samples.
virtual RooArgList * EvaluateAllTestStatistics(RooAbsData &data, const RooArgSet &poi)
Evaluate all test statistics, returning result and any detailed output.
virtual RooDataSet * GetSamplingDistributionsSingleWorker(RooArgSet &paramPoint)
This is the main function for serial runs.
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition: RooRandom.cxx:54
Bool_t CheckConfig(void)
only checks, no guessing/determination (do this in calculators, e.g.
void setBins(Int_t nBins, const char *name=0)
Definition: RooRealVar.h:77
void CommitSet(double weight=1.0)
Commit to the result RooDataSet.
virtual Double_t expectedEvents(const RooArgSet *nset) const
Return expected number of events from this p.d.f for use in extended likelihood calculations.
Definition: RooAbsPdf.cxx:2930
RooCmdArg ExpectedData(Bool_t flag=kTRUE)
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
std::list< RooArgSet * > _obsList
Definition: ToyMCSampler.h:286
Int_t getSize() const
void AppendArgSet(const RooAbsCollection *aset, TString prefix="")
For each variable in aset, prepend prefix to its name and add to the internal store.
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents: An owning collection is returned containing clones o...
TRandom2 r(17)
const RooAbsCategoryLValue & indexCat() const
RooAbsArg * first() const
const RooDataSet * fProtoData
Definition: ToyMCSampler.h:277
TObject * Next()
Definition: TCollection.h:153
Int_t GetNExperiments(void) const
Definition: ProofConfig.h:99
virtual void ClearCache()
clear the cache obtained from the pdf used for speeding the toy and global observables generation nee...
ProofConfig * fProofConfig
Definition: ToyMCSampler.h:279
static Double_t infinity()
Return internal infinity representation.
Definition: RooNumber.cxx:49
std::vector< TestStatistic * > fTestStatistics
Definition: ToyMCSampler.h:251
const RooArgSet * fGlobalObservables
Definition: ToyMCSampler.h:257
RooStudyManager is a utility class to manage studies that consist of repeated applications of generat...
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:37
ToyMCSampler is an implementation of the TestStatSampler interface.
Definition: ToyMCSampler.h:71
RooCmdArg GenBinned(const char *tag)
void Add(const SamplingDistribution *other)
merge two sampling distributions
RooDataSet * GetAsDataSet(TString name, TString title)
Returns all detailed output as a dataset.
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
This class simply holds a sampling distribution of some test statistic.
RooCategory represents a fundamental (non-derived) discrete value object.
Definition: RooCategory.h:24
Bool_t canBeExtended() const
Definition: RooAbsPdf.h:216
virtual const RooArgSet * get(Int_t index) const
Return RooArgSet with coordinates of event &#39;index&#39;.
const Bool_t kFALSE
Definition: RtypesCore.h:92
Namespace for the RooStats classes.
Definition: Asimov.h:20
RooAbsPdf::GenSpec * _gs4
GenSpec #3.
Definition: ToyMCSampler.h:291
#define ClassImp(name)
Definition: Rtypes.h:336
double Double_t
Definition: RtypesCore.h:55
RooCmdArg AutoBinned(Bool_t flag=kTRUE)
RooCmdArg ProtoData(const RooDataSet &protoData, Bool_t randomizeOrder=kFALSE, Bool_t resample=kFALSE)
std::list< RooAbsPdf::GenSpec * > _gsList
Definition: ToyMCSampler.h:287
void SetToyMCSampler(ToyMCSampler &t)
Definition: ToyMCStudy.h:57
virtual const char * getLabel() const
Return label string of current state.
Definition: RooCategory.h:39
static Bool_t fgAlwaysUseMultiGen
GenSpec #4.
Definition: ToyMCSampler.h:293
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, RooAbsPdf &pdf) const
Definition: ToyMCSampler.h:110
#define oocoutW(o, a)
Definition: RooMsgService.h:46
Mother of all ROOT objects.
Definition: TObject.h:37
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
RooAbsPdf * getPdf(const char *catName) const
Return the p.d.f associated with the given index category name.
ToyMCSampler()
Proof constructor. Do not use.
const RooArgSet * fObservables
Definition: ToyMCSampler.h:256
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
const RooArgSet * fParametersForTestStat
Definition: ToyMCSampler.h:250
RooDataSet * generate(const RooArgSet &whatVars, Int_t nEvents, const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none())
Generate a new dataset containing the specified variables with events sampled from our distribution...
Definition: RooAbsPdf.cxx:1702
double ceil(double)
RooAbsPdf::GenSpec * _gs3
GenSpec #2.
Definition: ToyMCSampler.h:290
virtual void GenerateGlobalObservables(RooAbsPdf &pdf) const
RooAbsPdf::GenSpec * _gs2
GenSpec #1.
Definition: ToyMCSampler.h:289
const RooArgSet * fNuisancePars
Definition: ToyMCSampler.h:255
RooSimultaneous facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset...
virtual RooDataSet * generateSimGlobal(const RooArgSet &whatVars, Int_t nEvents)
Special generator interface for generation of &#39;global observables&#39; – for RooStats tools...
Definition: RooAbsPdf.cxx:2383
TestStatistic is an interface class to provide a facility for construction test statistics distributi...
Definition: TestStatistic.h:31
const Bool_t kTRUE
Definition: RtypesCore.h:91
virtual void AddTestStatistic(TestStatistic *t=NULL)
Definition: ToyMCSampler.h:97
Helper class for ToyMCSampler.
Definition: ToyMCSampler.h:39
RooAbsPdf * fPriorNuisance
Definition: ToyMCSampler.h:254
std::list< RooAbsPdf * > _pdfList
Definition: ToyMCSampler.h:285
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:42
virtual Int_t numEntries() const
Definition: RooAbsData.cxx:269
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48