Skip to content

Commit 4751306

Browse files
committed
[RF] Add CompileContext::markSubtreeAsCompiled()
This is a reoccuring pattern that merits its own helper function. (cherry picked from commit 3d2fc3f)
1 parent 37adbbe commit 4751306

6 files changed

Lines changed: 29 additions & 30 deletions

File tree

roofit/roofit/src/RooMomentMorphFuncND.cxx

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,6 @@ RooMomentMorphFuncND::compileForNormSet(RooArgSet const &normSet, RooFit::Detail
368368
{
369369
RooArgSet branches;
370370
cache->_sum->branchNodeServerList(&branches);
371-
branches.add(*cache->_sum);
372371
for (auto *b : branches) {
373372
std::vector<std::string> toRemove;
374373
for (auto const &attr : b->attributes()) {
@@ -408,17 +407,10 @@ RooMomentMorphFuncND::compileForNormSet(RooArgSet const &normSet, RooFit::Detail
408407
std::unique_ptr<RooAbsReal> newSum{static_cast<RooAbsReal *>(cust.build())};
409408
newSum->addOwnedComponents(std::move(newFractions));
410409

411-
// Mark every node in the freshly-cloned subtree as already compiled, so
412-
// the recursive compileServers call below doesn't try to re-clone any of
413-
// them. The leaves we still want compiled (the morph parameters and the
414-
// observables) are reachable from these nodes' own server lists and will
415-
// be visited.
416-
ctx.markAsCompiled(*newSum);
417-
RooArgSet allBranches;
418-
newSum->branchNodeServerList(&allBranches);
419-
for (auto *b : allBranches) {
420-
ctx.markAsCompiled(*b);
421-
}
410+
// Tell the compile context the freshly-cloned subtree is already in its
411+
// final form, so the recursive descent below only visits the leaves that
412+
// still need compiling (the morph parameters and the observables).
413+
ctx.markSubtreeAsCompiled(*newSum);
422414
ctx.compileServers(*newSum, normSet);
423415

424416
return newSum;

roofit/roofitcore/inc/RooFit/Detail/NormalizationHelpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class CompileContext {
4242
void compileServer(RooAbsArg &server, RooAbsArg &arg, RooArgSet const &normSet);
4343

4444
void markAsCompiled(RooAbsArg &arg) const;
45+
void markSubtreeAsCompiled(RooAbsArg &arg) const;
4546

4647
// This information is used for the binned likelihood optimization.
4748
void setLikelihoodMode(bool flag) { _likelihoodMode = flag; }

roofit/roofitcore/src/NormalizationHelpers.cxx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ void RooFit::Detail::CompileContext::markAsCompiled(RooAbsArg &arg) const
7676
arg.setAttribute("_COMPILED");
7777
}
7878

79+
/// Mark `arg` and every branch node reachable through its server tree as
80+
/// already compiled. Use this after assembling or cloning a sub-graph
81+
/// yourself inside `compileForNormSet`: it prevents a follow-up
82+
/// `compileServers` call from re-cloning any of those internal nodes, while
83+
/// still letting the recursive descent reach the genuine leaves
84+
/// (fundamental observables and parameters) at the bottom of the tree.
85+
void RooFit::Detail::CompileContext::markSubtreeAsCompiled(RooAbsArg &arg) const
86+
{
87+
RooArgSet branches;
88+
arg.branchNodeServerList(&branches);
89+
for (RooAbsArg *b : branches) {
90+
markAsCompiled(*b);
91+
}
92+
}
93+
7994
bool RooFit::Detail::CompileContext::isMarkedAsCompiled(RooAbsArg const &arg) const
8095
{
8196
return arg.getAttribute("_COMPILED");

roofit/roofitcore/src/RooAbsCachedPdf.cxx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -406,12 +406,9 @@ RooAbsCachedPdf::compileForNormSet(RooArgSet const &normSet, RooFit::Detail::Com
406406

407407
auto newArg = std::make_unique<RooFit::Detail::RooNormalizedPdf>(*pdfClone, normSet);
408408

409-
// The direct servers are this pdf and the normalization integral, which
410-
// don't need to be compiled further.
411-
for (RooAbsArg *server : newArg->servers()) {
412-
ctx.markAsCompiled(*server);
413-
}
414-
ctx.markAsCompiled(*newArg);
409+
// The direct servers are the cloned pdf (already compiled above) and the
410+
// freshly-built normalization integral. Neither needs further compilation.
411+
ctx.markSubtreeAsCompiled(*newArg);
415412
newArg->addOwnedComponents(std::move(pdfClone));
416413
return newArg;
417414
}

roofit/roofitcore/src/RooAbsPdf.cxx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2790,12 +2790,9 @@ RooAbsPdf::compileForNormSet(RooArgSet const &normSet, RooFit::Detail::CompileCo
27902790

27912791
auto newArg = std::make_unique<RooFit::Detail::RooNormalizedPdf>(*pdfClone, normSet);
27922792

2793-
// The direct servers are this pdf and the normalization integral, which
2794-
// don't need to be compiled further.
2795-
for (RooAbsArg *server : newArg->servers()) {
2796-
ctx.markAsCompiled(*server);
2797-
}
2798-
ctx.markAsCompiled(*newArg);
2793+
// The direct servers are the cloned pdf (already compiled above) and the
2794+
// freshly-built normalization integral. Neither needs further compilation.
2795+
ctx.markSubtreeAsCompiled(*newArg);
27992796
newArg->addOwnedComponents(std::move(pdfClone));
28002797
return newArg;
28012798
}

roofit/roofitcore/src/RooRealSumPdf.cxx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -767,12 +767,9 @@ RooRealSumPdf::compileForNormSet(RooArgSet const &normSet, RooFit::Detail::Compi
767767

768768
auto newArg = std::make_unique<RooFit::Detail::RooNormalizedPdf>(*pdfClone, depList);
769769

770-
// The direct servers are this pdf and the normalization integral, which
771-
// don't need to be compiled further.
772-
for (RooAbsArg *server : newArg->servers()) {
773-
ctx.markAsCompiled(*server);
774-
}
775-
ctx.markAsCompiled(*newArg);
770+
// The direct servers are the cloned pdf (already compiled above) and the
771+
// freshly-built normalization integral. Neither needs further compilation.
772+
ctx.markSubtreeAsCompiled(*newArg);
776773
newArg->addOwnedComponents(std::move(pdfClone));
777774
return newArg;
778775
}

0 commit comments

Comments
 (0)