Logo ROOT   6.12/07
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;
158  fParametersForTestStat = NULL;
159  fPriorNuisance = NULL;
160  fNuisancePars = NULL;
161  fObservables = NULL;
162  fGlobalObservables = 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 
195 ToyMCSampler::ToyMCSampler(TestStatistic &ts, Int_t ntoys) : fSamplingDistName(ts.GetVarName().Data()), fNToys(ntoys)
196 {
197  fPdf = NULL;
198  fParametersForTestStat = NULL;
199  fPriorNuisance = NULL;
200  fNuisancePars = NULL;
201  fObservables = NULL;
202  fGlobalObservables = NULL;
203 
204  fSize = 0.05;
205  fNEvents = 0;
207  fGenerateBinnedTag = "";
210 
211  fToysInTails = 0.0;
215 
216  fProtoData = NULL;
217 
218  fProofConfig = NULL;
220 
221  _allVars = NULL ;
222  _gs1 = NULL ;
223  _gs2 = NULL ;
224  _gs3 = NULL ;
225  _gs4 = NULL ;
226 
227  //suppress messages for num integration of Roofit
229 
230  fUseMultiGen = kFALSE ;
231 
232  AddTestStatistic(&ts);
233 }
234 
235 ////////////////////////////////////////////////////////////////////////////////
236 
239 
240  ClearCache();
241 }
242 
243 ////////////////////////////////////////////////////////////////////////////////
244 /// only checks, no guessing/determination (do this in calculators,
245 /// e.g. using ModelConfig::GuessObsAndNuisance(...))
246 
248  bool goodConfig = true;
249 
250  if(fTestStatistics.size() == 0 || fTestStatistics[0] == NULL) { ooccoutE((TObject*)NULL,InputArguments) << "Test statistic not set." << endl; goodConfig = false; }
251  if(!fObservables) { ooccoutE((TObject*)NULL,InputArguments) << "Observables not set." << endl; goodConfig = false; }
252  if(!fParametersForTestStat) { ooccoutE((TObject*)NULL,InputArguments) << "Parameter values used to evaluate the test statistic are not set." << endl; goodConfig = false; }
253  if(!fPdf) { ooccoutE((TObject*)NULL,InputArguments) << "Pdf not set." << endl; goodConfig = false; }
254 
255 
256  //ooccoutI((TObject*)NULL,InputArguments) << "ToyMCSampler configuration:" << endl;
257  //ooccoutI((TObject*)NULL,InputArguments) << "Pdf from SetPdf: " << fPdf << endl;
258  // for( unsigned int i=0; i < fTestStatistics.size(); i++ ) {
259  // ooccoutI((TObject*)NULL,InputArguments) << "test statistics["<<i<<"]: " << fTestStatistics[i] << endl;
260  // }
261  //ooccoutI((TObject*)NULL,InputArguments) << endl;
262 
263  return goodConfig;
264 }
265 
266 ////////////////////////////////////////////////////////////////////////////////
267 /// Evaluate all test statistics, returning result and any detailed output.
268 /// PDF parameter values are saved in case they are modified by
269 /// TestStatistic::Evaluate (eg. SimpleLikelihoodRatioTestStat).
270 
272  DetailedOutputAggregator detOutAgg;
273  const RooArgList* allTS = EvaluateAllTestStatistics(data, poi, detOutAgg);
274  if (!allTS) return 0;
275  // no need to delete allTS, it is deleted in destructor of detOutAgg
276  return dynamic_cast<RooArgList*>(allTS->snapshot());
277 }
278 
279 ////////////////////////////////////////////////////////////////////////////////
280 
282  RooArgSet *allVars = fPdf ? fPdf->getVariables() : 0;
283  RooArgSet *saveAll = allVars ? dynamic_cast<RooArgSet*>(allVars->snapshot()) : 0;
284  for( unsigned int i = 0; i < fTestStatistics.size(); i++ ) {
285  if( fTestStatistics[i] == NULL ) continue;
286  TString name( TString::Format("%s_TS%u", fSamplingDistName.c_str(), i) );
287  RooArgSet* parForTS = dynamic_cast<RooArgSet*>(poi.snapshot());
288  RooRealVar ts( name, fTestStatistics[i]->GetVarName(), fTestStatistics[i]->Evaluate( data, *parForTS ) );
289  RooArgList tset(ts);
290  detOutAgg.AppendArgSet(&tset);
291  delete parForTS;
292  if (const RooArgSet* detOut = fTestStatistics[i]->GetDetailedOutput()) {
293  name.Append("_");
294  detOutAgg.AppendArgSet(detOut, name);
295  }
296  if (saveAll) *allVars = *saveAll; // restore values, perhaps modified by fTestStatistics[i]->Evaluate()
297  }
298  delete saveAll;
299  delete allVars;
300  return detOutAgg.GetAsArgList();
301 }
302 
303 ////////////////////////////////////////////////////////////////////////////////
304 
306  if(fTestStatistics.size() > 1) {
307  oocoutW((TObject*)NULL, InputArguments) << "Multiple test statistics defined, but only one distribution will be returned." << endl;
308  for( unsigned int i=0; i < fTestStatistics.size(); i++ ) {
309  oocoutW((TObject*)NULL, InputArguments) << " \t test statistic: " << fTestStatistics[i] << endl;
310  }
311  }
312 
313  RooDataSet* r = GetSamplingDistributions(paramPointIn);
314  if(r == NULL || r->numEntries() == 0) {
315  oocoutW((TObject*)NULL, Generation) << "no sampling distribution generated" << endl;
316  return NULL;
317  }
318 
319  SamplingDistribution* samp = new SamplingDistribution( r->GetName(), r->GetTitle(), *r );
320  delete r;
321  return samp;
322 }
323 
324 ////////////////////////////////////////////////////////////////////////////////
325 /// Use for serial and parallel runs.
326 
328 {
329 
330  // ======= S I N G L E R U N ? =======
331  if(!fProofConfig)
332  return GetSamplingDistributionsSingleWorker(paramPointIn);
333 
334  // ======= P A R A L L E L R U N =======
335  if (!CheckConfig()){
337  << "Bad COnfiguration in ToyMCSampler "
338  << endl;
339  return nullptr;
340  }
341 
342  // turn adaptive sampling off if given
343  if(fToysInTails) {
344  fToysInTails = 0;
346  << "Adaptive sampling in ToyMCSampler is not supported for parallel runs."
347  << endl;
348  }
349 
350  // adjust number of toys on the slaves to keep the total number of toys constant
351  Int_t totToys = fNToys;
352  fNToys = (int)ceil((double)fNToys / (double)fProofConfig->GetNExperiments()); // round up
353 
354  // create the study instance for parallel processing
355  ToyMCStudy* toymcstudy = new ToyMCStudy ;
356  toymcstudy->SetToyMCSampler(*this);
357  toymcstudy->SetParamPoint(paramPointIn);
359 
360  // temporary workspace for proof to avoid messing with TRef
362  RooStudyManager studymanager(w, *toymcstudy);
363  studymanager.runProof(fProofConfig->GetNExperiments(), fProofConfig->GetHost(), fProofConfig->GetShowGui());
364 
365  RooDataSet* output = toymcstudy->merge();
366 
367  // reset the number of toys
368  fNToys = totToys;
369 
370  delete toymcstudy;
371  return output;
372 }
373 
374 ////////////////////////////////////////////////////////////////////////////////
375 /// This is the main function for serial runs. It is called automatically
376 /// from inside GetSamplingDistribution when no ProofConfig is given.
377 /// You should not call this function yourself. This function should
378 /// be used by ToyMCStudy on the workers (ie. when you explicitly want
379 /// a serial run although ProofConfig is present).
380 ///
381 /// Make sure the cache is clear. It is important to clear it hear, because
382 /// the cache might be invalid even when just the firstPOI was changed, for which
383 /// no accessor has to be called. (Fixes a bug when ToyMCSampler is
384 /// used with the Neyman Construction)
385 
387 {
388  ClearCache();
389 
390  if (!CheckConfig()){
392  << "Bad COnfiguration in ToyMCSampler "
393  << endl;
394  return nullptr;
395  }
396 
397  // important to cache the paramPoint b/c test statistic might
398  // modify it from event to event
399  RooArgSet *paramPoint = (RooArgSet*) paramPointIn.snapshot();
400  RooArgSet *allVars = fPdf->getVariables();
401  RooArgSet *saveAll = (RooArgSet*) allVars->snapshot();
402 
403 
404  DetailedOutputAggregator detOutAgg;
405 
406  // counts the number of toys in the limits set for adaptive sampling
407  // (taking weights into account; always on first test statistic)
408  Double_t toysInTails = 0.0;
409 
410  for (Int_t i = 0; i < fMaxToys; ++i) {
411  // need to check at the beginning for case that zero toys are requested
412  if (toysInTails >= fToysInTails && i+1 > fNToys) break;
413 
414  // status update
415  if ( i% 500 == 0 && i>0 ) {
416  oocoutP((TObject*)0,Generation) << "generated toys: " << i << " / " << fNToys;
417  if (fToysInTails) ooccoutP((TObject*)0,Generation) << " (tails: " << toysInTails << " / " << fToysInTails << ")" << std::endl;
418  else ooccoutP((TObject*)0,Generation) << endl;
419  }
420 
421  // TODO: change this treatment to keep track of all values so that the threshold
422  // for adaptive sampling is counted for all distributions and not just the
423  // first one.
424  Double_t valueFirst = -999.0, weight = 1.0;
425 
426  // set variables to requested parameter point
427  *allVars = *saveAll; // important for example for SimpleLikelihoodRatioTestStat
428 
429  RooAbsData* toydata = GenerateToyData(*paramPoint, weight);
430 
431  *allVars = *fParametersForTestStat;
432 
433  const RooArgList* allTS = EvaluateAllTestStatistics(*toydata, *fParametersForTestStat, detOutAgg);
434  if (allTS->getSize() > Int_t(fTestStatistics.size()))
435  detOutAgg.AppendArgSet( fGlobalObservables, "globObs_" );
436  if (RooRealVar* firstTS = dynamic_cast<RooRealVar*>(allTS->first()))
437  valueFirst = firstTS->getVal();
438 
439  delete toydata;
440 
441  // check for nan
442  if(valueFirst != valueFirst) {
443  oocoutW((TObject*)NULL, Generation) << "skip: " << valueFirst << ", " << weight << endl;
444  continue;
445  }
446 
447  detOutAgg.CommitSet(weight);
448 
449  // adaptive sampling checks
450  if (valueFirst <= fAdaptiveLowLimit || valueFirst >= fAdaptiveHighLimit) {
451  if(weight >= 0.) toysInTails += weight;
452  else toysInTails += 1.;
453  }
454  }
455 
456  // clean up
457  *allVars = *saveAll;
458  delete saveAll;
459  delete allVars;
460  delete paramPoint;
461 
463 }
464 
465 ////////////////////////////////////////////////////////////////////////////////
466 
468 
469 
471  ooccoutE((TObject*)NULL,InputArguments) << "Global Observables not set." << endl;
472  return;
473  }
474 
475 
477 
478  // generate one set of global observables and assign it
479  // has problem for sim pdfs
480  RooSimultaneous* simPdf = dynamic_cast<RooSimultaneous*>( &pdf );
481  if (!simPdf) {
482  RooDataSet *one = pdf.generate(*fGlobalObservables, 1);
483 
484  const RooArgSet *values = one->get(0);
485  if (!_allVars) {
486  _allVars = pdf.getVariables();
487  }
488  *_allVars = *values;
489  delete one;
490 
491  } else {
492 
493  if (_pdfList.size() == 0) {
494  RooCategory& channelCat = (RooCategory&)simPdf->indexCat();
495  int nCat = channelCat.numTypes();
496  for (int i=0; i < nCat; ++i){
497  channelCat.setIndex(i);
498  RooAbsPdf* pdftmp = simPdf->getPdf(channelCat.getLabel());
499  assert(pdftmp);
500  RooArgSet* globtmp = pdftmp->getObservables(*fGlobalObservables);
501  RooAbsPdf::GenSpec* gs = pdftmp->prepareMultiGen(*globtmp, NumEvents(1));
502  _pdfList.push_back(pdftmp);
503  _obsList.push_back(globtmp);
504  _gsList.push_back(gs);
505  }
506  }
507 
508  list<RooArgSet*>::iterator oiter = _obsList.begin();
509  list<RooAbsPdf::GenSpec*>::iterator giter = _gsList.begin();
510  for (list<RooAbsPdf*>::iterator iter = _pdfList.begin(); iter != _pdfList.end(); ++iter, ++giter, ++oiter) {
511  //RooDataSet* tmp = (*iter)->generate(**oiter,1) ;
512  RooDataSet* tmp = (*iter)->generate(**giter);
513  **oiter = *tmp->get(0);
514  delete tmp;
515  }
516  }
517 
518 
519  } else {
520 
521  // not using multigen for global observables
523  const RooArgSet *values = one->get(0);
524  RooArgSet* allVars = pdf.getVariables();
525  *allVars = *values;
526  delete allVars;
527  delete one;
528 
529  }
530 }
531 
532 ////////////////////////////////////////////////////////////////////////////////
533 /// This method generates a toy data set for the given parameter point taking
534 /// global observables into account.
535 /// The values of the generated global observables remain in the pdf's variables.
536 /// They have to have those values for the subsequent evaluation of the
537 /// test statistics.
538 
539 RooAbsData* ToyMCSampler::GenerateToyData(RooArgSet& paramPoint, double& weight, RooAbsPdf& pdf) const {
540 
541  if(!fObservables) {
542  ooccoutE((TObject*)NULL,InputArguments) << "Observables not set." << endl;
543  return NULL;
544  }
545 
546  // assign input paramPoint
547  RooArgSet* allVars = fPdf->getVariables();
548  *allVars = paramPoint;
549 
550 
551  // create nuisance parameter points
555  oocoutI((TObject*)NULL,InputArguments) << "Cannot use multigen when nuisance parameters vary for every toy" << endl;
556  }
557 
558  // generate global observables
559  RooArgSet observables(*fObservables);
561  observables.remove(*fGlobalObservables);
563  }
564 
565  // save values to restore later.
566  // but this must remain after(!) generating global observables
567  const RooArgSet* saveVars = (const RooArgSet*)allVars->snapshot();
568 
569  if(fNuisanceParametersSampler) { // use nuisance parameters?
570  // Construct a set of nuisance parameters that has the parameters
571  // in the input paramPoint removed. Therefore, no parameter in
572  // paramPoint is randomized.
573  // Therefore when a parameter is given (should be held fixed),
574  // but is also in the list of nuisance parameters, the parameter
575  // will be held fixed. This is useful for debugging to hold single
576  // parameters fixed although under "normal" circumstances it is
577  // randomized.
578  RooArgSet allVarsMinusParamPoint(*allVars);
579  allVarsMinusParamPoint.remove(paramPoint, kFALSE, kTRUE); // match by name
580 
581  // get nuisance parameter point and weight
582  fNuisanceParametersSampler->NextPoint(allVarsMinusParamPoint, weight);
583 
584 
585  }else{
586  weight = 1.0;
587  }
588 
589  RooAbsData *data = Generate(pdf, observables);
590 
591  // We generated the data with the randomized nuisance parameter (if hybrid)
592  // but now we are setting the nuisance parameters back to where they were.
593  *allVars = *saveVars;
594  delete allVars;
595  delete saveVars;
596 
597  return data;
598 }
599 
600 ////////////////////////////////////////////////////////////////////////////////
601 /// This is the generate function to use in the context of the ToyMCSampler
602 /// instead of the standard RooAbsPdf::generate(...).
603 /// It takes into account whether the number of events is given explicitly
604 /// or whether it should use the expected number of events. It also takes
605 /// into account the option to generate a binned data set (ie RooDataHist).
606 
607 RooAbsData* ToyMCSampler::Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooDataSet* protoData, int forceEvents) const {
608 
609  if(fProtoData) {
610  protoData = fProtoData;
611  forceEvents = protoData->numEntries();
612  }
613 
614  RooAbsData *data = NULL;
615  int events = forceEvents;
616  if(events == 0) events = fNEvents;
617 
618  // cannot use multigen when the nuisance parameters change for every toy
619  bool useMultiGen = (fUseMultiGen || fgAlwaysUseMultiGen) && !fNuisanceParametersSampler;
620 
621  if(events == 0) {
622  if( pdf.canBeExtended() && pdf.expectedEvents(observables) > 0) {
623  if(fGenerateBinned) {
624  if(protoData) data = pdf.generate(observables, AllBinned(), Extended(), ProtoData(*protoData, true, true));
625  else data = pdf.generate(observables, AllBinned(), Extended());
626  }else{
627  if(protoData) {
628  if (useMultiGen) {
629  if (!_gs2) { _gs2 = pdf.prepareMultiGen(observables, Extended(), AutoBinned(fGenerateAutoBinned), GenBinned(fGenerateBinnedTag), ProtoData(*protoData, true, true)) ; }
630  data = pdf.generate(*_gs2) ;
631  } else {
632  data = pdf.generate (observables, Extended(), AutoBinned(fGenerateAutoBinned), GenBinned(fGenerateBinnedTag), ProtoData(*protoData, true, true));
633  }
634  }
635  else {
636  if (useMultiGen) {
638  data = pdf.generate(*_gs1) ;
639  } else {
641  }
642 
643  }
644  }
645  }else{
647  << "ToyMCSampler: Error : pdf is not extended and number of events per toy is zero"
648  << endl;
649  }
650  }else{
651  if(fGenerateBinned) {
652  if(protoData) data = pdf.generate(observables, events, AllBinned(), ProtoData(*protoData, true, true));
653  else data = pdf.generate(observables, events, AllBinned());
654  }else{
655  if(protoData) {
656  if (useMultiGen) {
657  if (!_gs3) { _gs3 = pdf.prepareMultiGen(observables, NumEvents(events), AutoBinned(fGenerateAutoBinned), GenBinned(fGenerateBinnedTag), ProtoData(*protoData, true, true)); }
658  data = pdf.generate(*_gs3) ;
659  } else {
660  data = pdf.generate (observables, NumEvents(events), AutoBinned(fGenerateAutoBinned), GenBinned(fGenerateBinnedTag), ProtoData(*protoData, true, true));
661  }
662  } else {
663  if (useMultiGen) {
665  data = pdf.generate(*_gs4) ;
666  } else {
667  data = pdf.generate (observables, NumEvents(events), AutoBinned(fGenerateAutoBinned), GenBinned(fGenerateBinnedTag));
668  }
669  }
670  }
671  }
672 
673  // in case of number counting print observables
674  // if (data->numEntries() == 1) {
675  // std::cout << "generate observables : ";
676  // RooStats::PrintListContent(*data->get(0), std::cout);
677  // }
678 
679  return data;
680 }
681 
682 ////////////////////////////////////////////////////////////////////////////////
683 /// Extended interface to append to sampling distribution more samples
684 
686  RooArgSet& allParameters,
687  SamplingDistribution* last,
688  Int_t additionalMC)
689 {
690  Int_t tmp = fNToys;
691  fNToys = additionalMC;
692  SamplingDistribution* newSamples = GetSamplingDistribution(allParameters);
693  fNToys = tmp;
694 
695  if(last){
696  last->Add(newSamples);
697  delete newSamples;
698  return last;
699  }
700 
701  return newSamples;
702 }
703 
704 ////////////////////////////////////////////////////////////////////////////////
705 /// clear the cache obtained from the pdf used for speeding the toy and global observables generation
706 /// needs to be called every time the model pdf (fPdf) changes
707 
709 
710  if (_gs1) delete _gs1;
711  _gs1 = 0;
712  if (_gs2) delete _gs2;
713  _gs2 = 0;
714  if (_gs3) delete _gs3;
715  _gs3 = 0;
716  if (_gs4) delete _gs4;
717  _gs4 = 0;
718 
719  // no need to delete the _pdfList since it is managed by the RooSimultaneous object
720  if (_pdfList.size() > 0) {
721  std::list<RooArgSet*>::iterator oiter = _obsList.begin();
722  for (std::list<RooAbsPdf::GenSpec*>::iterator giter = _gsList.begin(); giter != _gsList.end(); ++giter, ++oiter) {
723  delete *oiter;
724  delete *giter;
725  }
726  _pdfList.clear();
727  _obsList.clear();
728  _gsList.clear();
729  }
730 
731  //LM: is this set really needed ??
732  if (_allVars) delete _allVars;
733  _allVars = 0;
734 
735 }
736 
737 } // end namespace RooStats
RooArgSet * getVariables(Bool_t stripDisconnected=kTRUE) const
Return RooArgSet with all variables (tree leaf nodes of expresssion tree)
Definition: RooAbsArg.cxx:2078
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:1878
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.
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:2365
#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:2953
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...
ROOT::R::TRInterface & r
Definition: Object.C:4
const RooAbsCategoryLValue & indexCat() const
RooAbsArg * first() const
const RooDataSet * fProtoData
Definition: ToyMCSampler.h:277
TObject * Next()
Definition: TCollection.h:247
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:88
Namespace for the RooStats classes.
Definition: Asimov.h:20
RooAbsPdf::GenSpec * _gs4
GenSpec #3.
Definition: ToyMCSampler.h:291
#define ClassImp(name)
Definition: Rtypes.h:359
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:1725
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:2406
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:87
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
char name[80]
Definition: TGX11.cxx:109
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:42
virtual Int_t numEntries() const
Definition: RooAbsData.cxx:285
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48