14 #ifndef ROO_MSG_SERVICE
48 void NuisanceParametersSampler::NextPoint(
RooArgSet& nuisPoint,
Double_t& weight) {
54 if (fIndex >= fNToys) {
60 nuisPoint = *fPoints->get(fIndex++);
61 weight = fPoints->weight();
64 if(fPoints->weight() == 0.0) {
66 NextPoint(nuisPoint, weight);
69 void NuisanceParametersSampler::Refresh() {
75 if (!fPrior || !fParams)
return;
77 if (fPoints)
delete fPoints;
87 TIter it2 = fParams->createIterator();
89 while ((myarg2 = dynamic_cast<RooRealVar*>(it2.
Next()))) {
94 fPoints = fPrior->generate(
100 if(fPoints->numEntries() != fNToys) {
101 fNToys = fPoints->numEntries();
103 "Adjusted number of toys to number of bins of nuisance parameters: " << fNToys << endl;
121 fPoints = fPrior->generate(*fParams, fNToys);
130 void ToyMCSampler::SetAlwaysUseMultiGen(
Bool_t flag) { fgAlwaysUseMultiGen = flag ; }
134 ToyMCSampler::ToyMCSampler() : fSamplingDistName(
"SD"), fNToys(1)
139 fParametersForTestStat =
NULL;
140 fPriorNuisance =
NULL;
141 fNuisancePars =
NULL;
143 fGlobalObservables =
NULL;
148 fGenerateBinnedTag =
"";
149 fGenerateAutoBinned =
kTRUE;
150 fExpectedNuisancePar =
kFALSE;
160 fNuisanceParametersSampler =
NULL;
174 ToyMCSampler::ToyMCSampler(TestStatistic &ts,
Int_t ntoys) :
175 fSamplingDistName(ts.GetVarName()), fNToys(ntoys)
178 fParametersForTestStat =
NULL;
179 fPriorNuisance =
NULL;
180 fNuisancePars =
NULL;
182 fGlobalObservables =
NULL;
187 fGenerateBinnedTag =
"";
188 fGenerateAutoBinned =
kTRUE;
189 fExpectedNuisancePar =
kFALSE;
199 fNuisanceParametersSampler =
NULL;
212 AddTestStatistic(&ts);
216 ToyMCSampler::~ToyMCSampler() {
217 if(fNuisanceParametersSampler)
delete fNuisanceParametersSampler;
223 Bool_t ToyMCSampler::CheckConfig(
void) {
226 bool goodConfig =
true;
230 if(!fParametersForTestStat) {
ooccoutE((
TObject*)
NULL,
InputArguments) <<
"Parameter values used to evaluate the test statistic are not set." << endl; goodConfig =
false; }
249 DetailedOutputAggregator detOutAgg;
250 const RooArgList* allTS = EvaluateAllTestStatistics(data, poi, detOutAgg);
251 if (!allTS)
return 0;
258 RooArgSet *allVars = fPdf ? fPdf->getVariables() : 0;
260 for(
unsigned int i = 0; i < fTestStatistics.size(); i++ ) {
261 if( fTestStatistics[i] ==
NULL )
continue;
264 RooRealVar ts( name, fTestStatistics[i]->GetVarName(), fTestStatistics[i]->Evaluate( data, *parForTS ) );
266 detOutAgg.AppendArgSet(&tset);
268 if (
const RooArgSet* detOut = fTestStatistics[i]->GetDetailedOutput()) {
270 detOutAgg.AppendArgSet(detOut, name);
272 if (saveAll) *allVars = *saveAll;
276 return detOutAgg.GetAsArgList();
280 SamplingDistribution* ToyMCSampler::GetSamplingDistribution(
RooArgSet& paramPointIn) {
281 if(fTestStatistics.size() > 1) {
283 for(
unsigned int i=0; i < fTestStatistics.size(); i++ ) {
288 RooDataSet*
r = GetSamplingDistributions(paramPointIn);
294 SamplingDistribution* samp =
new SamplingDistribution( r->
GetName(), r->
GetTitle(), *
r );
306 return GetSamplingDistributionsSingleWorker(paramPointIn);
312 <<
"Bad COnfiguration in ToyMCSampler "
321 <<
"Adaptive sampling in ToyMCSampler is not supported for parallel runs."
326 Int_t totToys = fNToys;
327 fNToys = (int)
ceil((
double)fNToys / (
double)fProofConfig->GetNExperiments());
330 ToyMCStudy* toymcstudy =
new ToyMCStudy ;
331 toymcstudy->SetToyMCSampler(*
this);
332 toymcstudy->SetParamPoint(paramPointIn);
338 studymanager.runProof(fProofConfig->GetNExperiments(), fProofConfig->GetHost(), fProofConfig->GetShowGui());
365 <<
"Bad COnfiguration in ToyMCSampler "
374 RooArgSet *allVars = fPdf->getVariables();
378 DetailedOutputAggregator detOutAgg;
384 for (
Int_t i = 0; i < fMaxToys; ++i) {
386 if (toysInTails >= fToysInTails && i+1 > fNToys)
break;
389 if ( i% 500 == 0 && i>0 ) {
391 if (fToysInTails)
ooccoutP((
TObject*)0,
Generation) <<
" (tails: " << toysInTails <<
" / " << fToysInTails <<
")" << std::endl;
399 Double_t valueFirst = -999.0, weight = 1.0;
404 RooAbsData* toydata = GenerateToyData(*paramPoint, weight);
406 *allVars = *fParametersForTestStat;
408 const RooArgList* allTS = EvaluateAllTestStatistics(*toydata, *fParametersForTestStat, detOutAgg);
410 detOutAgg.AppendArgSet( fGlobalObservables,
"globObs_" );
412 valueFirst = firstTS->getVal();
417 if(valueFirst != valueFirst) {
422 detOutAgg.CommitSet(weight);
425 if (valueFirst <= fAdaptiveLowLimit || valueFirst >= fAdaptiveHighLimit) {
426 if(weight >= 0.) toysInTails += weight;
427 else toysInTails += 1.;
437 return detOutAgg.GetAsDataSet(fSamplingDistName, fSamplingDistName);
440 void ToyMCSampler::GenerateGlobalObservables(
RooAbsPdf& pdf)
const {
443 if(!fGlobalObservables || fGlobalObservables->getSize()==0) {
449 if (fUseMultiGen || fgAlwaysUseMultiGen) {
466 if (_pdfList.size() == 0) {
469 for (
int i=0; i < nCat; ++i){
475 _pdfList.push_back(pdftmp);
476 _obsList.push_back(globtmp);
477 _gsList.push_back(gs);
481 list<RooArgSet*>::iterator oiter = _obsList.begin();
482 list<RooAbsPdf::GenSpec*>::iterator giter = _gsList.begin();
483 for (list<RooAbsPdf*>::iterator
iter = _pdfList.begin();
iter != _pdfList.end(); ++
iter, ++giter, ++oiter) {
486 **oiter = *tmp->
get(0);
518 RooArgSet* allVars = fPdf->getVariables();
519 *allVars = paramPoint;
523 if(!fNuisanceParametersSampler && fPriorNuisance && fNuisancePars) {
524 fNuisanceParametersSampler =
new NuisanceParametersSampler(fPriorNuisance, fNuisancePars, fNToys, fExpectedNuisancePar);
525 if ((fUseMultiGen || fgAlwaysUseMultiGen) && fNuisanceParametersSampler )
531 if(fGlobalObservables && fGlobalObservables->getSize()) {
532 observables.
remove(*fGlobalObservables);
533 GenerateGlobalObservables(pdf);
540 if(fNuisanceParametersSampler) {
549 RooArgSet allVarsMinusParamPoint(*allVars);
553 fNuisanceParametersSampler->NextPoint(allVarsMinusParamPoint, weight);
560 RooAbsData *data = Generate(pdf, observables);
564 *allVars = *saveVars;
581 protoData = fProtoData;
586 int events = forceEvents;
587 if(events == 0) events = fNEvents;
590 bool useMultiGen = (fUseMultiGen || fgAlwaysUseMultiGen) && !fNuisanceParametersSampler;
594 if(fGenerateBinned) {
618 <<
"ToyMCSampler: Error : pdf is not extended and number of events per toy is zero"
622 if(fGenerateBinned) {
658 SamplingDistribution* ToyMCSampler::AppendSamplingDistribution(
660 SamplingDistribution* last,
664 fNToys = additionalMC;
665 SamplingDistribution* newSamples = GetSamplingDistribution(allParameters);
669 last->Add(newSamples);
680 void ToyMCSampler::ClearCache() {
685 if (_gs1)
delete _gs1;
687 if (_gs2)
delete _gs2;
689 if (_gs3)
delete _gs3;
691 if (_gs4)
delete _gs4;
695 if (_pdfList.size() > 0) {
696 std::list<RooArgSet*>::iterator oiter = _obsList.begin();
697 for (std::list<RooAbsPdf::GenSpec*>::iterator giter = _gsList.begin(); giter != _gsList.end(); ++giter, ++oiter) {
707 if (_allVars)
delete _allVars;
virtual const char * GetTitle() const
Returns title of object.
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents: An owning collection is returned containing clones o...
virtual Bool_t setIndex(Int_t index, Bool_t printError=kTRUE)
Set value by specifying the index code of the desired state.
RooArgSet * getObservables(const RooArgSet &set, Bool_t valueOnly=kTRUE) const
const RooAbsCategoryLValue & indexCat() const
RooCmdArg NumEvents(Int_t numEvents)
StreamConfig & getStream(Int_t id)
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...
void removeTopic(RooFit::MsgTopic oldTopic)
static RooMsgService & instance()
Return reference to singleton instance.
RooAbsPdf * getPdf(const char *catName) const
Return the p.d.f associated with the given index category name.
virtual Double_t expectedEvents(const RooArgSet *nset) const
Return expected number of events from this p.d.f for use in extended likelihood calculations.
RooCmdArg Extended(Bool_t flag=kTRUE)
RooAbsArg * first() const
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
RooArgSet * getVariables(Bool_t stripDisconnected=kTRUE) const
Return RooArgSet with all variables (tree leaf nodes of expresssion tree)
Int_t numTypes(const char *=0) const
std::map< std::string, std::string >::const_iterator iter
TString & Append(const char *cs)
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
void setBins(Int_t nBins, const char *name=0)
RooCmdArg ExpectedData(Bool_t flag=kTRUE)
virtual Int_t numEntries() const
static Double_t infinity()
Return internal infinity representation.
virtual const char * GetName() const
Returns name of object.
ToyMCSampler is an implementation of the TestStatSampler interface.
RooCmdArg GenBinned(const char *tag)
Namespace for the RooStats classes.
RooCmdArg AutoBinned(Bool_t flag=kTRUE)
RooCmdArg ProtoData(const RooDataSet &protoData, Bool_t randomizeOrder=kFALSE, Bool_t resample=kFALSE)
Bool_t canBeExtended() const
Mother of all ROOT objects.
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
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...
ClassImp(RooStats::ToyMCSampler) namespace RooStats
static void output(int code)
virtual RooDataSet * generateSimGlobal(const RooArgSet &whatVars, Int_t nEvents)
Special generator interface for generation of 'global observables' – for RooStats tools...
virtual const RooArgSet * get(Int_t index) const
Return RooArgSet with coordinates of event 'index'.
virtual const char * getLabel() const
Return label string of current state.