Skip to content

Commit e609aee

Browse files
cburgardguitargeek
authored andcommitted
[RF] ToyMCSampler improvements for binned toys
Adds the ability to control the ToyMCSampler to emit binned toys. Makes use of this new capability in the HypoTestCalculator in cases where the observed data is binned. (cherry picked from commit 95320d1)
1 parent 37602cc commit e609aee

File tree

8 files changed

+23
-11
lines changed

8 files changed

+23
-11
lines changed

roofit/roofitcore/inc/RooGlobalFunc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ RooCmdArg Conditional(const RooArgSet& pdfSet, const RooArgSet& depSet, bool dep
358358
* @{
359359
*/
360360
// RooAbsPdf::generate arguments
361-
RooCmdArg ProtoData(const RooDataSet& protoData, bool randomizeOrder=false, bool resample=false) ;
361+
RooCmdArg ProtoData(const RooAbsData& protoData, bool randomizeOrder=false, bool resample=false) ;
362362
RooCmdArg NumEvents(Int_t numEvents) ;
363363
RooCmdArg NumEvents(double numEvents) ;
364364
RooCmdArg AutoBinned(bool flag=true) ;

roofit/roofitcore/src/RooAbsPdf.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ RooAbsGenContext* RooAbsPdf::autoGenContext(const RooArgSet &vars, const RooData
12001200
/// as binned generation is always executed at the top-level node for a regular
12011201
/// PDF, so for those it only mattes that the top-level node is tagged.
12021202
///
1203-
/// <tr><td> ProtoData(const RooDataSet& data, bool randOrder)
1203+
/// <tr><td> ProtoData(const RooAbsData& data, bool randOrder)
12041204
/// <td> Use specified dataset as prototype dataset. If randOrder in ProtoData() is set to true,
12051205
/// the order of the events in the dataset will be read in a random order if the requested
12061206
/// number of events to be generated does not match the number of events in the prototype dataset.

roofit/roofitcore/src/RooBinnedGenContext.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ RooBinnedGenContext::RooBinnedGenContext(const RooAbsPdf &model, const RooArgSet
6060
if (prototype)
6161
{
6262
RooArgSet coefNSet(vars) ;
63-
coefNSet.add(*prototype->get()) ;
63+
coefNSet.add(*prototype->get(),true) ;
6464
_pdf->fixAddCoefNormalization(coefNSet) ;
6565
}
6666

roofit/roofitcore/src/RooGenContext.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ RooGenContext::RooGenContext(const RooAbsPdf &model, const RooArgSet &vars,
8787
// Optionally fix RooAddPdf normalizations
8888
if (prototype&&_pdfClone->dependsOn(*prototype->get())) {
8989
RooArgSet fullNormSet(vars) ;
90-
fullNormSet.add(*prototype->get()) ;
90+
fullNormSet.add(*prototype->get(),true) ;
9191
_pdfClone->fixAddCoefNormalization(fullNormSet) ;
9292
}
9393

roofit/roofitcore/src/RooGlobalFunc.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ RooCmdArg Conditional(const RooArgSet &pdfSet, const RooArgSet &depSet, bool dep
807807
};
808808

809809
// RooAbsPdf::generate arguments
810-
RooCmdArg ProtoData(const RooDataSet &protoData, bool randomizeOrder, bool resample)
810+
RooCmdArg ProtoData(const RooAbsData &protoData, bool randomizeOrder, bool resample)
811811
{
812812
return RooCmdArg("PrototypeData", randomizeOrder, resample, 0, 0, nullptr, nullptr, &protoData, nullptr);
813813
}

roofit/roostats/inc/RooStats/ToyMCSampler.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,14 @@ class ToyMCSampler: public TestStatSampler {
229229
fAdaptiveLowLimit = low_threshold;
230230
}
231231

232-
void SetProtoData(const RooDataSet* d) { fProtoData = d; }
232+
void SetProtoData(const RooAbsData* d) { fProtoData = d; }
233233

234234
protected:
235235

236236
const RooArgList* EvaluateAllTestStatistics(RooAbsData& data, const RooArgSet& poi, DetailedOutputAggregator& detOutAgg);
237237

238238
/// helper for GenerateToyData
239-
std::unique_ptr<RooAbsData> Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooDataSet *protoData=nullptr, int forceEvents=0) const;
239+
std::unique_ptr<RooAbsData> Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooAbsData *protoData=nullptr, int forceEvents=0) const;
240240

241241
/// helper method for clearing the cache
242242
virtual void ClearCache();
@@ -272,7 +272,7 @@ class ToyMCSampler: public TestStatSampler {
272272
double fAdaptiveLowLimit;
273273
double fAdaptiveHighLimit;
274274

275-
const RooDataSet *fProtoData = nullptr; ///< in dev
275+
const RooAbsData *fProtoData = nullptr; ///< in dev
276276

277277
mutable NuisanceParametersSampler *fNuisanceParametersSampler = nullptr; ///<!
278278

roofit/roostats/src/HypoTestCalculatorGeneric.cxx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,20 @@ HypoTestCalculatorGeneric::HypoTestCalculatorGeneric(
6363
*altModel.GetPdf(),
6464
altModel.GetSnapshot());
6565

66-
fDefaultSampler = new ToyMCSampler(*fDefaultTestStat, 1000);
67-
fTestStatSampler = fDefaultSampler;
66+
auto toymcs = new ToyMCSampler(*fDefaultTestStat, 1000);
67+
const bool dataIsBinned = dynamic_cast<const RooDataHist*>(fData);
68+
if (dataIsBinned) {
69+
// Ensure the ToyMCSampler generates toys with the same structure as the observed data.
70+
// TODO: understand why this is needed only in the RooDataHist case,
71+
// but results in backwards-incompatibility for RooDataSet, which
72+
// manifests for example in the stressRooStats tests.
73+
// See also: https://github.com/root-project/root/pull/20171
74+
toymcs->SetProtoData(&data);
75+
}
76+
toymcs->SetGenerateBinned(dataIsBinned); // if observed is RooDataHist -> generate RooDataHist toys
77+
78+
fDefaultSampler = toymcs;
79+
fTestStatSampler = toymcs;
6880
}
6981

7082

roofit/roostats/src/ToyMCSampler.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ RooAbsData* ToyMCSampler::GenerateToyData(RooArgSet& paramPoint, double& weight,
476476
/// or whether it should use the expected number of events. It also takes
477477
/// into account the option to generate a binned data set (*i.e.* RooDataHist).
478478

479-
std::unique_ptr<RooAbsData> ToyMCSampler::Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooDataSet* protoData, int forceEvents) const {
479+
std::unique_ptr<RooAbsData> ToyMCSampler::Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooAbsData* protoData, int forceEvents) const {
480480

481481
if(fProtoData) {
482482
protoData = fProtoData;

0 commit comments

Comments
 (0)