Skip to content

Commit f841b96

Browse files
committed
[RF] Improve the constructor matching in RooFactoryWSTool
So far, the a constructorr match was reported for `RooWorkspace::factory` when the number of constructor args was larger than the number of args passed to `factory`. Obviously, this lead to false positives, and it should actually check if the number of passed args (plus 2 for name and title) is somewhere between the number of minimum and maximum arguments that the constructor can take. In other works, instead of assuming there is an arbitrary number of default arguments one should do the check with their exact number. A unit test to check that this works now is also implemented. Closes root-project#7965.
1 parent 172c924 commit f841b96

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

roofit/roofitcore/src/RooFactoryWSTool.cxx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ namespace {
241241
return false;
242242
}
243243

244-
static pair<list<string>,unsigned int> ctorArgs(const char* classname, UInt_t nMinArg) {
244+
static pair<list<string>,unsigned int> ctorArgs(const char* classname, std::size_t nPassedArgs) {
245245
// Utility function for RooFactoryWSTool. Return arguments of 'first' non-default, non-copy constructor of any RooAbsArg
246246
// derived class. Only constructors that start with two `const char*` arguments (for name and title) are considered
247247
// The returned object contains
@@ -269,7 +269,8 @@ namespace {
269269

270270
// Skip default constructor
271271
int nargs = gInterpreter->MethodInfo_NArg(func);
272-
if (nargs==0 || nargs==gInterpreter->MethodInfo_NDefaultArg(func)) {
272+
int nDefaultArgs = gInterpreter->MethodInfo_NDefaultArg(func);
273+
if (nargs == nDefaultArgs) {
273274
continue;
274275
}
275276

@@ -287,12 +288,12 @@ namespace {
287288
}
288289
gInterpreter->MethodArgInfo_Delete(arg);
289290

290-
// Check that the number of required arguments is at least nMinArg
291-
if (ret.size()<nMinArg) {
292-
continue;
291+
// If the number of passed args (plus 2 for name and title) is somewhere
292+
// between the number of minimum and maximum arguments that the
293+
// constructor can take, it's a match!
294+
if(static_cast<int>(nPassedArgs) >= nargs - nDefaultArgs && static_cast<int>(nPassedArgs) <= nargs) {
295+
break;
293296
}
294-
295-
break;
296297
}
297298
gInterpreter->MethodInfo_Delete(func);
298299
gInterpreter->ClassInfo_Delete(cls);

roofit/roofitcore/test/testWorkspace.cxx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ using namespace RooStats;
2222
///
2323
TEST(RooWorkspace, CloneModelConfig_ROOT_9777)
2424
{
25+
constexpr bool verbose = false;
26+
2527
const char* filename = "ROOT-9777.root";
2628

2729
RooRealVar x("x", "x", 1, 0, 10);
@@ -56,17 +58,17 @@ TEST(RooWorkspace, CloneModelConfig_ROOT_9777)
5658
delete w;
5759
}
5860

59-
w2->Print();
61+
if(verbose) w2->Print();
6062

6163
ModelConfig *mc = dynamic_cast<ModelConfig*>(w2->genobj("ModelConfig"));
6264
ASSERT_TRUE(mc) << "ModelConfig not retrieved.";
6365
mc->Print();
6466

6567
ASSERT_TRUE(mc->GetGlobalObservables()) << "GlobalObsevables in mc broken.";
66-
mc->GetGlobalObservables()->Print();
68+
if(verbose) mc->GetGlobalObservables()->Print();
6769

6870
ASSERT_TRUE(mc->GetParametersOfInterest()) << "ParametersOfInterest in mc broken.";
69-
mc->GetParametersOfInterest()->Print();
71+
if(verbose) mc->GetParametersOfInterest()->Print();
7072

7173
gSystem->Unlink(filename);
7274
}
@@ -234,3 +236,13 @@ TEST_F(TestRooWorkspaceWithGaussian, HashLookupInWorkspace) {
234236
EXPECT_FALSE(model_constrained_orig->dependsOn(*ws->var("mu2")));
235237
EXPECT_NE(ws->pdf("Gauss_editPdf_orig"), nullptr);
236238
}
239+
240+
/// Covers an issue about a RooAddPdf constructor not properly picked up by
241+
/// RooFactoryWSTool.
242+
TEST(RooWorkspace, Issue_7965)
243+
{
244+
RooWorkspace ws{"ws"};
245+
ws.factory("RooAddPdf::addPdf({})");
246+
247+
ASSERT_NE(ws.pdf("addPdf"), nullptr);
248+
}

0 commit comments

Comments
 (0)