Skip to content

Commit 1f0d5ad

Browse files
committed
[RF] Avoid duplication of RooRealSumPdf code in RooAddition
Much of the functionality of RooAddition 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 3645164 commit 1f0d5ad

File tree

3 files changed

+23
-158
lines changed

3 files changed

+23
-158
lines changed

roofit/roofitcore/inc/RooAddition.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ class RooArgList ;
2727
class RooAddition : public RooAbsReal {
2828
public:
2929

30-
RooAddition() ;
30+
RooAddition() : _cacheMgr(this,10) {}
3131
RooAddition(const char *name, const char *title, const RooArgList& sumSet, bool takeOwnerShip=false) ;
3232
RooAddition(const char *name, const char *title, const RooArgList& sumSet1, const RooArgList& sumSet2, bool takeOwnerShip=false) ;
33-
~RooAddition() override ;
3433

3534
RooAddition(const RooAddition& other, const char* name = 0);
3635
TObject* clone(const char* newname) const override { return new RooAddition(*this, newname); }
@@ -66,10 +65,9 @@ class RooAddition : public RooAbsReal {
6665

6766
class CacheElem : public RooAbsCacheElement {
6867
public:
69-
~CacheElem() override;
7068
// Payload
7169
RooArgList _I ;
72-
RooArgList containedArgs(Action) override ;
70+
RooArgList containedArgs(Action) override { return _I; }
7371
};
7472
mutable RooObjCacheManager _cacheMgr ; ///<! The cache manager
7573

roofit/roofitcore/inc/RooRealSumPdf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ class RooRealSumPdf : public RooAbsPdf {
9595

9696
private:
9797

98+
friend class RooAddPdf;
99+
friend class RooAddition;
98100
friend class RooRealSumFunc;
99101

100102
static void initializeFuncsAndCoefs(RooAbsReal const& caller,

roofit/roofitcore/src/RooAddition.cxx

Lines changed: 19 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ in the two sets.
2727

2828
#include "Riostream.h"
2929
#include "RooAddition.h"
30+
#include "RooRealSumFunc.h"
31+
#include "RooRealSumPdf.h"
3032
#include "RooProduct.h"
3133
#include "RooAbsReal.h"
3234
#include "RooErrorHandler.h"
@@ -41,18 +43,7 @@ in the two sets.
4143
#include <algorithm>
4244
#include <cmath>
4345

44-
using namespace std;
45-
4646
ClassImp(RooAddition);
47-
;
48-
49-
50-
////////////////////////////////////////////////////////////////////////////////
51-
/// Empty constructor
52-
RooAddition::RooAddition() : _cacheMgr(this,10)
53-
{
54-
}
55-
5647

5748

5849
////////////////////////////////////////////////////////////////////////////////
@@ -70,7 +61,7 @@ RooAddition::RooAddition(const char* name, const char* title, const RooArgList&
7061
for (const auto comp : sumSet) {
7162
if (!dynamic_cast<RooAbsReal*>(comp)) {
7263
coutE(InputArguments) << "RooAddition::ctor(" << GetName() << ") ERROR: component " << comp->GetName()
73-
<< " is not of type RooAbsReal" << endl ;
64+
<< " is not of type RooAbsReal" << std::endl;
7465
RooErrorHandler::softAbort() ;
7566
}
7667
_set.add(*comp) ;
@@ -101,7 +92,7 @@ RooAddition::RooAddition(const char* name, const char* title, const RooArgList&
10192
, _cacheMgr(this,10)
10293
{
10394
if (sumSet1.getSize() != sumSet2.getSize()) {
104-
coutE(InputArguments) << "RooAddition::ctor(" << GetName() << ") ERROR: input lists should be of equal length" << endl ;
95+
coutE(InputArguments) << "RooAddition::ctor(" << GetName() << ") ERROR: input lists should be of equal length" << std::endl;
10596
RooErrorHandler::softAbort() ;
10697
}
10798

@@ -111,13 +102,13 @@ RooAddition::RooAddition(const char* name, const char* title, const RooArgList&
111102

112103
if (!dynamic_cast<RooAbsReal*>(comp1)) {
113104
coutE(InputArguments) << "RooAddition::ctor(" << GetName() << ") ERROR: component " << comp1->GetName()
114-
<< " in first list is not of type RooAbsReal" << endl ;
105+
<< " in first list is not of type RooAbsReal" << std::endl;
115106
RooErrorHandler::softAbort() ;
116107
}
117108

118109
if (!dynamic_cast<RooAbsReal*>(comp2)) {
119110
coutE(InputArguments) << "RooAddition::ctor(" << GetName() << ") ERROR: component " << comp2->GetName()
120-
<< " in first list is not of type RooAbsReal" << endl ;
111+
<< " in first list is not of type RooAbsReal" << std::endl;
121112
RooErrorHandler::softAbort() ;
122113
}
123114
// TODO: add flag to RooProduct c'tor to make it assume ownership...
@@ -150,14 +141,6 @@ RooAddition::RooAddition(const RooAddition& other, const char* name)
150141
// Member _ownedList is intentionally not copy-constructed -- ownership is not transferred
151142
}
152143

153-
154-
////////////////////////////////////////////////////////////////////////////////
155-
156-
RooAddition::~RooAddition()
157-
{ // Destructor
158-
159-
}
160-
161144
////////////////////////////////////////////////////////////////////////////////
162145
/// Calculate and return current value of self
163146

@@ -166,15 +149,10 @@ double RooAddition::evaluate() const
166149
double sum(0);
167150
const RooArgSet* nset = _set.nset() ;
168151

169-
// cout << "RooAddition::eval sum = " ;
170-
171-
for (const auto arg : _set) {
172-
const auto comp = static_cast<RooAbsReal*>(arg);
152+
for (auto* comp : static_range_cast<RooAbsReal*>(_set)) {
173153
const double tmp = comp->getVal(nset);
174-
// cout << tmp << " " ;
175154
sum += tmp ;
176155
}
177-
// cout << " = " << sum << endl ;
178156
return sum ;
179157
}
180158

@@ -211,7 +189,7 @@ double RooAddition::defaultErrorLevel() const
211189
RooAbsReal* nllArg(0) ;
212190
RooAbsReal* chi2Arg(0) ;
213191

214-
RooArgSet* comps = getComponents() ;
192+
std::unique_ptr<RooArgSet> comps{getComponents()};
215193
for(RooAbsArg * arg : *comps) {
216194
if (dynamic_cast<RooNLLVar*>(arg) || dynamic_cast<ROOT::Experimental::RooNLLVarNew*>(arg)) {
217195
nllArg = (RooAbsReal*)arg ;
@@ -220,22 +198,21 @@ double RooAddition::defaultErrorLevel() const
220198
chi2Arg = (RooAbsReal*)arg ;
221199
}
222200
}
223-
delete comps ;
224201

225202
if (nllArg && !chi2Arg) {
226203
coutI(Fitting) << "RooAddition::defaultErrorLevel(" << GetName()
227-
<< ") Summation contains a RooNLLVar, using its error level" << endl ;
204+
<< ") Summation contains a RooNLLVar, using its error level" << std::endl;
228205
return nllArg->defaultErrorLevel() ;
229206
} else if (chi2Arg && !nllArg) {
230207
coutI(Fitting) << "RooAddition::defaultErrorLevel(" << GetName()
231-
<< ") Summation contains a RooChi2Var, using its error level" << endl ;
208+
<< ") Summation contains a RooChi2Var, using its error level" << std::endl;
232209
return chi2Arg->defaultErrorLevel() ;
233210
} else if (!nllArg && !chi2Arg) {
234211
coutI(Fitting) << "RooAddition::defaultErrorLevel(" << GetName() << ") WARNING: "
235-
<< "Summation contains neither RooNLLVar nor RooChi2Var server, using default level of 1.0" << endl ;
212+
<< "Summation contains neither RooNLLVar nor RooChi2Var server, using default level of 1.0" << std::endl;
236213
} else {
237214
coutI(Fitting) << "RooAddition::defaultErrorLevel(" << GetName() << ") WARNING: "
238-
<< "Summation contains BOTH RooNLLVar and RooChi2Var server, using default level of 1.0" << endl ;
215+
<< "Summation contains BOTH RooNLLVar and RooChi2Var server, using default level of 1.0" << std::endl;
239216
}
240217

241218
return 1.0 ;
@@ -267,18 +244,11 @@ bool RooAddition::setData(RooAbsData& data, bool cloneData)
267244

268245
////////////////////////////////////////////////////////////////////////////////
269246

270-
void RooAddition::printMetaArgs(ostream& os) const
247+
void RooAddition::printMetaArgs(std::ostream& os) const
271248
{
272-
bool first(true) ;
273-
for (const auto arg : _set) {
274-
if (!first) {
275-
os << " + " ;
276-
} else {
277-
first = false ;
278-
}
279-
os << arg->GetName() ;
280-
}
281-
os << " " ;
249+
// We can use the implementation of RooRealSumPdf with an empy coefficient list.
250+
static const RooArgList coefs{};
251+
RooRealSumPdf::printMetaArgs(_set, coefs, os);
282252
}
283253

284254
////////////////////////////////////////////////////////////////////////////////
@@ -335,128 +305,23 @@ double RooAddition::analyticalIntegral(Int_t code, const char* rangeName) const
335305
}
336306

337307

338-
339308
////////////////////////////////////////////////////////////////////////////////
340309

341310
std::list<double>* RooAddition::binBoundaries(RooAbsRealLValue& obs, double xlo, double xhi) const
342311
{
343-
std::list<double>* sumBinB = 0 ;
344-
bool needClean(false) ;
345-
346-
// Loop over components pdf
347-
for(auto * func : static_range_cast<RooAbsReal*>(_set)) {
348-
349-
std::list<double>* funcBinB = func->binBoundaries(obs,xlo,xhi) ;
350-
351-
// Process hint
352-
if (funcBinB) {
353-
if (!sumBinB) {
354-
// If this is the first hint, then just save it
355-
sumBinB = funcBinB ;
356-
} else {
357-
358-
std::list<double>* newSumBinB = new std::list<double>(sumBinB->size()+funcBinB->size()) ;
359-
360-
// Merge hints into temporary array
361-
merge(funcBinB->begin(),funcBinB->end(),sumBinB->begin(),sumBinB->end(),newSumBinB->begin()) ;
362-
363-
// Copy merged array without duplicates to new sumBinBArrau
364-
delete sumBinB ;
365-
delete funcBinB ;
366-
sumBinB = newSumBinB ;
367-
needClean = true ;
368-
}
369-
}
370-
}
371-
372-
// Remove consecutive duplicates
373-
if (needClean) {
374-
std::list<double>::iterator new_end = unique(sumBinB->begin(),sumBinB->end()) ;
375-
sumBinB->erase(new_end,sumBinB->end()) ;
376-
}
377-
378-
return sumBinB ;
312+
return RooRealSumPdf::binBoundaries(_set, obs, xlo, xhi);
379313
}
380314

381315

382-
//_____________________________________________________________________________B
383316
bool RooAddition::isBinnedDistribution(const RooArgSet& obs) const
384317
{
385-
// If all components that depend on obs are binned that so is the product
386-
387-
RooFIter iter = _set.fwdIterator() ;
388-
RooAbsReal* func ;
389-
while((func=(RooAbsReal*)iter.next())) {
390-
if (func->dependsOn(obs) && !func->isBinnedDistribution(obs)) {
391-
return false ;
392-
}
393-
}
394-
395-
return true ;
318+
return RooRealSumPdf::isBinnedDistribution(_set, obs);
396319
}
397320

398321

399-
400-
401322
////////////////////////////////////////////////////////////////////////////////
402323

403324
std::list<double>* RooAddition::plotSamplingHint(RooAbsRealLValue& obs, double xlo, double xhi) const
404325
{
405-
std::list<double>* sumHint = 0 ;
406-
bool needClean(false) ;
407-
408-
RooFIter iter = _set.fwdIterator() ;
409-
RooAbsReal* func ;
410-
// Loop over components pdf
411-
while((func=(RooAbsReal*)iter.next())) {
412-
413-
std::list<double>* funcHint = func->plotSamplingHint(obs,xlo,xhi) ;
414-
415-
// Process hint
416-
if (funcHint) {
417-
if (!sumHint) {
418-
419-
// If this is the first hint, then just save it
420-
sumHint = funcHint ;
421-
422-
} else {
423-
424-
std::list<double>* newSumHint = new std::list<double>(sumHint->size()+funcHint->size()) ;
425-
426-
// Merge hints into temporary array
427-
merge(funcHint->begin(),funcHint->end(),sumHint->begin(),sumHint->end(),newSumHint->begin()) ;
428-
429-
// Copy merged array without duplicates to new sumHintArrau
430-
delete sumHint ;
431-
sumHint = newSumHint ;
432-
needClean = true ;
433-
}
434-
}
435-
}
436-
437-
// Remove consecutive duplicates
438-
if (needClean) {
439-
std::list<double>::iterator new_end = unique(sumHint->begin(),sumHint->end()) ;
440-
sumHint->erase(new_end,sumHint->end()) ;
441-
}
442-
443-
return sumHint ;
444-
}
445-
446-
447-
448-
////////////////////////////////////////////////////////////////////////////////
449-
/// Return list of all RooAbsArgs in cache element
450-
451-
RooArgList RooAddition::CacheElem::containedArgs(Action)
452-
{
453-
RooArgList ret(_I) ;
454-
return ret ;
326+
return RooRealSumPdf::plotSamplingHint(_set, obs, xlo, xhi);
455327
}
456-
457-
RooAddition::CacheElem::~CacheElem()
458-
{
459-
// Destructor
460-
}
461-
462-

0 commit comments

Comments
 (0)