Skip to content

Commit f87a55e

Browse files
committed
[RF][HS3] Deduplicate gamma constraint relative-error dispatch
Extract the RooPoisson/RooGaussian constraint dispatch that was duplicated in the Barlow-Beeston (mc-stat) and shapesys branches of readChannel into a single constraintRelError() helper.
1 parent 6698ba1 commit f87a55e

1 file changed

Lines changed: 17 additions & 18 deletions

File tree

roofit/hs3/src/JSONFactories_HistFactory.cxx

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,20 @@ double poissonTau(RooPoisson const &constraint, RooAbsArg const &gamma)
331331
return std::numeric_limits<double>::quiet_NaN();
332332
}
333333

334+
// Returns the relative uncertainty encoded by a gamma constraint pdf. Only RooPoisson (via its tau) and RooGaussian
335+
// (via sigma/mean) are supported; anything else raises an error.
336+
double constraintRelError(RooAbsPdf const &constraint, RooAbsArg const &gamma)
337+
{
338+
if (auto constraintP = dynamic_cast<RooPoisson const *>(&constraint)) {
339+
return 1. / std::sqrt(poissonTau(*constraintP, gamma));
340+
}
341+
if (auto constraintG = dynamic_cast<RooGaussian const *>(&constraint)) {
342+
return constraintG->getSigma().getVal() / constraintG->getMean().getVal();
343+
}
344+
RooJSONFactoryWSTool::error("currently, only RooPoisson and RooGaussian are supported as constraint types");
345+
return std::numeric_limits<double>::quiet_NaN();
346+
}
347+
334348
bool importHistSample(RooJSONFactoryWSTool &tool, RooDataHist &dh, RooArgSet const &varlist,
335349
RooAbsArg const *mcStatObject, const std::string &fprefix, const JSONNode &p,
336350
RooArgSet &constraints)
@@ -1138,16 +1152,7 @@ Channel readChannel(RooJSONFactoryWSTool *tool, const std::string &pdfname, cons
11381152
channel.tot_yield[idx] += sample.hist[idx - 1];
11391153
channel.tot_yield2[idx] += (sample.hist[idx - 1] * sample.hist[idx - 1]);
11401154
if (constraint) {
1141-
if (RooPoisson *constraint_p = dynamic_cast<RooPoisson *>(constraint)) {
1142-
double erel = 1. / std::sqrt(poissonTau(*constraint_p, *g));
1143-
channel.rel_errors[idx] = erel;
1144-
} else if (RooGaussian *constraint_g = dynamic_cast<RooGaussian *>(constraint)) {
1145-
double erel = constraint_g->getSigma().getVal() / constraint_g->getMean().getVal();
1146-
channel.rel_errors[idx] = erel;
1147-
} else {
1148-
RooJSONFactoryWSTool::error(
1149-
"currently, only RooPoisson and RooGaussian are supported as constraint types");
1150-
}
1155+
channel.rel_errors[idx] = constraintRelError(*constraint, *g);
11511156
}
11521157
}
11531158
sample.useBarlowBeestonLight = true;
@@ -1173,15 +1178,9 @@ Channel readChannel(RooJSONFactoryWSTool *tool, const std::string &pdfname, cons
11731178
if (!constraint) {
11741179
sys.constraints.push_back(0.0);
11751180
sys.constraintPdfs.push_back(nullptr);
1176-
} else if (auto constraint_p = dynamic_cast<RooPoisson *>(constraint)) {
1177-
sys.constraints.push_back(1. / std::sqrt(poissonTau(*constraint_p, *g)));
1178-
sys.constraintPdfs.push_back(constraint_p);
1179-
} else if (auto constraint_g = dynamic_cast<RooGaussian *>(constraint)) {
1180-
sys.constraints.push_back(constraint_g->getSigma().getVal() / constraint_g->getMean().getVal());
1181-
sys.constraintPdfs.push_back(constraint_g);
11821181
} else {
1183-
RooJSONFactoryWSTool::error(
1184-
"currently, only RooPoisson and RooGaussian are supported as constraint types");
1182+
sys.constraints.push_back(constraintRelError(*constraint, *g));
1183+
sys.constraintPdfs.push_back(constraint);
11851184
}
11861185
}
11871186
sample.shapesys.emplace_back(std::move(sys));

0 commit comments

Comments
 (0)