Skip to content

Commit 6fa5c44

Browse files
committed
[RF] Avoid duplication of RooRealSumPdf code in RooAddPdf
Much of the functionality of RooAddPdf is implemented in exactly the same way as in RooRealSumPdf. Hence, to avoid code duplication, we can reuse the static functions in RooRealSumPdf that provide this implementation.
1 parent 1f0d5ad commit 6fa5c44

File tree

2 files changed

+11
-137
lines changed

2 files changed

+11
-137
lines changed

roofit/roofitcore/src/RooAddPdf.cxx

Lines changed: 8 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ An (enforced) condition for this assumption is that each \f$ \mathrm{PDF}_i \f$
7474
#include "RooRealVar.h"
7575
#include "RooRealConstant.h"
7676
#include "RooRealIntegral.h"
77+
#include "RooRealSumPdf.h"
7778
#include "RooRecursiveFraction.h"
7879

7980
#include <algorithm>
@@ -823,21 +824,7 @@ void RooAddPdf::resetErrorCounters(Int_t resetValue)
823824

824825
bool RooAddPdf::checkObservables(const RooArgSet* nset) const
825826
{
826-
bool ret(false) ;
827-
828-
// There may be fewer coefficients than PDFs.
829-
std::size_t end = std::min(_pdfList.size(), _coefList.size());
830-
for (std::size_t i = 0; i < end; ++i) {
831-
auto pdf = static_cast<const RooAbsPdf *>(_pdfList.at(i));
832-
auto coef = static_cast<const RooAbsReal*>(_coefList.at(i));
833-
if (pdf->observableOverlaps(nset,*coef)) {
834-
coutE(InputArguments) << "RooAddPdf::checkObservables(" << GetName() << "): ERROR: coefficient " << coef->GetName()
835-
<< " and PDF " << pdf->GetName() << " have one or more dependents in common" << endl ;
836-
ret = true ;
837-
}
838-
}
839-
840-
return ret ;
827+
return RooRealSumPdf::checkObservables(*this, nset, _pdfList, _coefList);
841828
}
842829

843830

@@ -1079,41 +1066,7 @@ RooArgList RooAddPdf::CacheElem::containedArgs(Action)
10791066

10801067
std::list<double>* RooAddPdf::plotSamplingHint(RooAbsRealLValue& obs, double xlo, double xhi) const
10811068
{
1082-
std::unique_ptr<std::list<double>> sumHint = nullptr ;
1083-
bool needClean = false;
1084-
1085-
// Loop over components pdf
1086-
for (const auto arg : _pdfList) {
1087-
auto pdf = static_cast<const RooAbsPdf*>(arg);
1088-
1089-
std::unique_ptr<std::list<double>> pdfHint{pdf->plotSamplingHint(obs,xlo,xhi)} ;
1090-
1091-
// Process hint
1092-
if (pdfHint) {
1093-
if (!sumHint) {
1094-
1095-
// If this is the first hint, then just save it
1096-
sumHint = std::move(pdfHint) ;
1097-
1098-
} else {
1099-
1100-
auto newSumHint = std::make_unique<std::list<double>>(sumHint->size()+pdfHint->size());
1101-
1102-
// Merge hints into temporary array
1103-
merge(pdfHint->begin(),pdfHint->end(),sumHint->begin(),sumHint->end(),newSumHint->begin()) ;
1104-
1105-
// Copy merged array without duplicates to new sumHintArrau
1106-
sumHint = std::move(newSumHint) ;
1107-
needClean = true ;
1108-
1109-
}
1110-
}
1111-
}
1112-
if (needClean) {
1113-
sumHint->erase(std::unique(sumHint->begin(),sumHint->end()), sumHint->end()) ;
1114-
}
1115-
1116-
return sumHint.release() ;
1069+
return RooRealSumPdf::plotSamplingHint(_pdfList, obs, xlo, xhi);
11171070
}
11181071

11191072

@@ -1122,56 +1075,15 @@ std::list<double>* RooAddPdf::plotSamplingHint(RooAbsRealLValue& obs, double xlo
11221075

11231076
std::list<double>* RooAddPdf::binBoundaries(RooAbsRealLValue& obs, double xlo, double xhi) const
11241077
{
1125-
std::unique_ptr<list<double>> sumBinB = nullptr ;
1126-
bool needClean = false;
1127-
1128-
// Loop over components pdf
1129-
for (auto arg : _pdfList) {
1130-
auto pdf = static_cast<const RooAbsPdf *>(arg);
1131-
std::unique_ptr<list<double>> pdfBinB{pdf->binBoundaries(obs,xlo,xhi)};
1132-
1133-
// Process hint
1134-
if (pdfBinB) {
1135-
if (!sumBinB) {
1136-
1137-
// If this is the first hint, then just save it
1138-
sumBinB = std::move(pdfBinB) ;
1139-
1140-
} else {
1141-
1142-
auto newSumBinB = std::make_unique<list<double>>(sumBinB->size()+pdfBinB->size()) ;
1143-
1144-
// Merge hints into temporary array
1145-
merge(pdfBinB->begin(),pdfBinB->end(),sumBinB->begin(),sumBinB->end(),newSumBinB->begin()) ;
1146-
1147-
// Copy merged array without duplicates to new sumBinBArrau
1148-
sumBinB = std::move(newSumBinB) ;
1149-
needClean = true ;
1150-
}
1151-
}
1152-
}
1153-
1154-
// Remove consecutive duplicates
1155-
if (needClean) {
1156-
sumBinB->erase(std::unique(sumBinB->begin(),sumBinB->end()), sumBinB->end()) ;
1157-
}
1158-
1159-
return sumBinB.release() ;
1078+
return RooRealSumPdf::binBoundaries(_pdfList, obs, xlo, xhi);
11601079
}
11611080

11621081

11631082
////////////////////////////////////////////////////////////////////////////////
11641083
/// If all components that depend on obs are binned, so is their sum.
11651084
bool RooAddPdf::isBinnedDistribution(const RooArgSet& obs) const
11661085
{
1167-
for (const auto arg : _pdfList) {
1168-
auto pdf = static_cast<const RooAbsPdf*>(arg);
1169-
if (pdf->dependsOn(obs) && !pdf->isBinnedDistribution(obs)) {
1170-
return false ;
1171-
}
1172-
}
1173-
1174-
return true ;
1086+
return RooRealSumPdf::isBinnedDistribution(_pdfList, obs);
11751087
}
11761088

11771089

@@ -1180,14 +1092,7 @@ bool RooAddPdf::isBinnedDistribution(const RooArgSet& obs) const
11801092

11811093
void RooAddPdf::setCacheAndTrackHints(RooArgSet& trackNodes)
11821094
{
1183-
RooFIter aiter = pdfList().fwdIterator() ;
1184-
RooAbsArg* aarg ;
1185-
while ((aarg=aiter.next())) {
1186-
if (aarg->canNodeBeCached()==Always) {
1187-
trackNodes.add(*aarg) ;
1188-
//cout << "tracking node RooAddPdf component " << aarg->ClassName() << "::" << aarg->GetName() << endl ;
1189-
}
1190-
}
1095+
RooRealSumPdf::setCacheAndTrackHints(_pdfList, trackNodes);
11911096
}
11921097

11931098

@@ -1196,37 +1101,7 @@ void RooAddPdf::setCacheAndTrackHints(RooArgSet& trackNodes)
11961101
/// Customized printing of arguments of a RooAddPdf to more intuitively reflect the contents of the
11971102
/// product operator construction
11981103

1199-
void RooAddPdf::printMetaArgs(ostream& os) const
1104+
void RooAddPdf::printMetaArgs(std::ostream& os) const
12001105
{
1201-
bool first(true) ;
1202-
1203-
if (!_coefList.empty()) {
1204-
for (std::size_t i = 0; i < _pdfList.size(); ++i ) {
1205-
const RooAbsArg * coef = _coefList.at(i);
1206-
const RooAbsArg * pdf = _pdfList.at(i);
1207-
if (!first) {
1208-
os << " + " ;
1209-
} else {
1210-
first = false ;
1211-
}
1212-
1213-
if (i < _coefList.size()) {
1214-
os << coef->GetName() << " * " << pdf->GetName();
1215-
} else {
1216-
os << "[%] * " << pdf->GetName();
1217-
}
1218-
}
1219-
} else {
1220-
1221-
for (const auto pdf : _pdfList) {
1222-
if (!first) {
1223-
os << " + " ;
1224-
} else {
1225-
first = false ;
1226-
}
1227-
os << pdf->GetName() ;
1228-
}
1229-
}
1230-
1231-
os << " " ;
1106+
RooRealSumPdf::printMetaArgs(_pdfList, _coefList, os);
12321107
}

roofit/roofitcore/src/RooRealSumPdf.cxx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -602,8 +602,7 @@ bool RooRealSumPdf::isBinnedDistribution(const RooArgSet& obs) const
602602

603603
bool RooRealSumPdf::isBinnedDistribution(RooArgList const& funcList, const RooArgSet& obs)
604604
{
605-
for (const auto elm : funcList) {
606-
auto func = static_cast<RooAbsReal*>(elm);
605+
for (auto* func : static_range_cast<RooAbsReal*>(funcList)) {
607606

608607
if (func->dependsOn(obs) && !func->isBinnedDistribution(obs)) {
609608
return false ;
@@ -624,7 +623,7 @@ std::list<double>* RooRealSumPdf::plotSamplingHint(RooAbsRealLValue& obs, double
624623

625624
std::list<double>* RooRealSumPdf::plotSamplingHint(RooArgList const& funcList, RooAbsRealLValue& obs, double xlo, double xhi)
626625
{
627-
std::list<double>* sumHint = 0 ;
626+
std::list<double>* sumHint = nullptr;
628627
bool needClean(false) ;
629628

630629
// Loop over components pdf
@@ -642,7 +641,7 @@ std::list<double>* RooRealSumPdf::plotSamplingHint(RooArgList const& funcList, R
642641

643642
} else {
644643

645-
list<double>* newSumHint = new list<double>(sumHint->size()+funcHint->size()) ;
644+
auto* newSumHint = new std::list<double>(sumHint->size()+funcHint->size()) ;
646645

647646
// Merge hints into temporary array
648647
merge(funcHint->begin(),funcHint->end(),sumHint->begin(),sumHint->end(),newSumHint->begin()) ;

0 commit comments

Comments
 (0)