Skip to content

Commit

Permalink
Replace deprecated legacy iterators
Browse files Browse the repository at this point in the history
The legacy iterators are deprecated since ROOT 6.18 and they will be
removed at some point.

This commit also fixes a few memory leaks, and it might also benefit the
performance a bit because the legacy iterators had quite some overhead.
  • Loading branch information
guitargeek committed Dec 2, 2023
1 parent be488af commit 3664ef7
Show file tree
Hide file tree
Showing 37 changed files with 84 additions and 271 deletions.
2 changes: 0 additions & 2 deletions interface/AsymQuad.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ class AsymQuad : public RooAbsReal {
RooListProxy _coefList; // List of coefficients
Double_t smoothRegion_;
Int_t smoothAlgo_;
TIterator* _funcIter; //! Iterator over FUNC list
TIterator* _coefIter; //! Iterator over coefficient list

private:
Double_t interpolate(Double_t theta_, Double_t valueCenter_, Double_t valueHigh_, Double_t valueLow_) const;
Expand Down
3 changes: 0 additions & 3 deletions interface/AtlasPdfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,6 @@ class RooStarMomentMorph : public RooAbsPdf {
mutable std::vector<int> _nnuis;
mutable std::vector<double> _nref;

TIterator* _parItr ; //! do not persist
TIterator* _obsItr ; //! do not persist
TIterator* _pdfItr ; //!
mutable TMatrixD* _M; //!

Setting _setting;
Expand Down
1 change: 0 additions & 1 deletion interface/HZZ4L_RooSpinZeroPdf_1D.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class HZZ4L_RooSpinZeroPdf_1D : public RooAbsPdf {
// RooRealProxy ksmd ;
RooRealProxy fai ;
RooListProxy _coefList ; // List of funcficients
TIterator* _coefIter ; //! Iterator over funcficient lis
Double_t evaluate() const ;
public:
HZZ4L_RooSpinZeroPdf_1D() {} ;
Expand Down
3 changes: 1 addition & 2 deletions src/AsimovUtils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ RooAbsData *asimovutils::asimovDatasetNominal(RooStats::ModelConfig *mc, double

if (verbose>2) {
Logger::instance().log(std::string(Form("AsimovUtils.cc: %d -- Parameters after fit for asimov dataset",__LINE__)),Logger::kLogLevelInfo,__func__);
std::unique_ptr<TIterator> iter(mc->GetPdf()->getParameters((const RooArgSet*) 0)->createIterator());
for (RooAbsArg *a = (RooAbsArg *) iter->Next(); a != 0; a = (RooAbsArg *) iter->Next()) {
for (RooAbsArg *a : *std::unique_ptr<RooArgSet>{mc->GetPdf()->getParameters((const RooArgSet*) 0)}) {
TString varstring = utils::printRooArgAsString(a);
Logger::instance().log(std::string(Form("AsimovUtils.cc: %d -- %s",__LINE__,varstring.Data())),Logger::kLogLevelInfo,__func__);
}
Expand Down
38 changes: 9 additions & 29 deletions src/AsymQuad.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ _coefList("coefList", "List of coefficients", this),
smoothRegion_(0),
smoothAlgo_(0)
{
_funcIter = _funcList.createIterator();
_coefIter = _coefList.createIterator();
}

AsymQuad::AsymQuad(const char *name, const char *title, const RooArgList& inFuncList, const RooArgList& inCoefList, Double_t smoothRegion, Int_t smoothAlgo) :
Expand All @@ -28,30 +26,21 @@ smoothAlgo_(smoothAlgo)
assert(0);
}

TIterator* funcIter = inFuncList.createIterator();
RooAbsArg* func;
while ((func = (RooAbsArg*)funcIter->Next())) {
for (RooAbsArg* func : inFuncList) {
if (!dynamic_cast<RooAbsReal*>(func)) {
coutE(InputArguments) << "ERROR: AsymQuad::AsymQuad(" << GetName() << ") function " << func->GetName() << " is not of type RooAbsReal" << std::endl;
assert(0);
}
_funcList.add(*func);
}
delete funcIter;

TIterator* coefIter = inCoefList.createIterator();
RooAbsArg* coef;
while ((coef = (RooAbsArg*)coefIter->Next())) {
for (RooAbsArg* coef : inCoefList) {
if (!dynamic_cast<RooAbsReal*>(coef)) {
coutE(InputArguments) << "ERROR: AsymQuad::AsymQuad(" << GetName() << ") coefficient " << coef->GetName() << " is not of type RooAbsReal" << std::endl;
assert(0);
}
_coefList.add(*coef);
}
delete coefIter;

_funcIter = _funcList.createIterator();
_coefIter = _coefList.createIterator();
}

AsymQuad::AsymQuad(const AsymQuad& other, const char* name):
Expand All @@ -61,30 +50,21 @@ _coefList("!coefList", this, other._coefList),
smoothRegion_(other.smoothRegion_),
smoothAlgo_(other.smoothAlgo_)
{
_funcIter = _funcList.createIterator();
_coefIter = _coefList.createIterator();
}

AsymQuad::~AsymQuad() {
delete _funcIter;
delete _coefIter;
}
AsymQuad::~AsymQuad() = default;

Double_t AsymQuad::evaluate() const {
Double_t result(0);

_funcIter->Reset();
_coefIter->Reset();
RooAbsReal* coef;
RooAbsReal* func = (RooAbsReal*)_funcIter->Next();
RooAbsReal* func = &(RooAbsReal&)_funcList[0];

Double_t central = func->getVal();
result = central;
Double_t result = central;

while ((coef=(RooAbsReal*)_coefIter->Next())) {
Double_t coefVal = coef->getVal();
RooAbsReal* funcUp = (RooAbsReal*)_funcIter->Next();
RooAbsReal* funcDn = (RooAbsReal*)_funcIter->Next();
for (int iCoef = 0; iCoef < _coefList.getSize(); ++iCoef) {
Double_t coefVal = static_cast<RooAbsReal&>(_coefList[iCoef]).getVal();
RooAbsReal* funcUp = &(RooAbsReal&)_funcList[2 * iCoef + 1];
RooAbsReal* funcDn = &(RooAbsReal&)_funcList[2 * iCoef + 2];
result += interpolate(coefVal, central, funcUp->getVal(), funcDn->getVal());
}

Expand Down
3 changes: 1 addition & 2 deletions src/AsymptoticLimits.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ bool AsymptoticLimits::run(RooWorkspace *w, RooStats::ModelConfig *mc_s, RooStat
*/
hasDiscreteParams_ = false;
if (params_.get() == 0) params_.reset(mc_s->GetPdf()->getParameters(data));
std::unique_ptr<TIterator> itparam(params_->createIterator());
for (RooAbsArg *a = (RooAbsArg *) itparam->Next(); a != 0; a = (RooAbsArg *) itparam->Next()) {
for (RooAbsArg *a : *params_) {
if (a->IsA()->InheritsFrom(RooCategory::Class())) { hasDiscreteParams_ = true; break; }
}

Expand Down
66 changes: 10 additions & 56 deletions src/AtlasPdfs.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,8 @@ RooBSpline::RooBSpline(const char* name, const char* title,

//bool even = fabs(_n/2-_n/2.) < 0.0000000001;
bool first=1;
TIterator* pointIter = controlPoints.createIterator() ;
RooAbsArg* point ;
RooAbsArg* lastPoint=NULL ;
while((point = (RooAbsArg*)pointIter->Next())) {
for (RooAbsArg * point : controlPoints) {
if (!dynamic_cast<RooAbsReal*>(point)) {
coutE(InputArguments) << "RooBSpline::ctor(" << GetName() << ") ERROR: control point " << point->GetName()
<< " is not of type RooAbsReal" << std::endl ;
Expand All @@ -456,19 +454,9 @@ RooBSpline::RooBSpline(const char* name, const char* title,
lastPoint=point;
}
for (int i=0;i<(_n)/2;i++) _controlPoints.add(*lastPoint);
delete pointIter ;


TIterator* varItr = vars.createIterator();
RooAbsArg* arg;
while ((arg=(RooAbsArg*)varItr->Next())) {
//std::cout << "======== Adding "<<arg->GetName()<<" to list of _vars of "<<name<<"." << std::endl;
_vars.add(*arg);
}
// std::cout << "all _vars: " << std::endl;
// _vars.Print("V");
delete varItr;
//std::cout << "out Ctor" << std::endl;
_vars.add(vars);
}

//_____________________________________________________________________________
Expand Down Expand Up @@ -556,10 +544,8 @@ void RooBSpline::setWeights(const RooArgList& weights)
{
_weights.removeAll();
bool first=1;
TIterator* pointIter = weights.createIterator() ;
RooAbsArg* point ;
RooAbsArg* lastPoint=NULL ;
while((point = (RooAbsArg*)pointIter->Next())) {
for (RooAbsArg *point : weights) {
if (!dynamic_cast<RooAbsReal*>(point)) {
coutE(InputArguments) << "RooBSpline::ctor(" << GetName() << ") ERROR: control point " << point->GetName()
<< " is not of type RooAbsReal" << std::endl ;
Expand All @@ -577,7 +563,6 @@ void RooBSpline::setWeights(const RooArgList& weights)
lastPoint=point;
}
for (int i=0;i<(_n+1)/2;i++) _weights.add(*lastPoint);
delete pointIter;
}


Expand Down Expand Up @@ -1235,9 +1220,6 @@ RooStarMomentMorph::RooStarMomentMorph() :
{

// coverity[UNINIT_CTOR]
_parItr = _parList.createIterator() ;
_obsItr = _obsList.createIterator() ;
_pdfItr = _pdfList.createIterator() ;
}


Expand Down Expand Up @@ -1269,45 +1251,31 @@ RooStarMomentMorph::RooStarMomentMorph(const char *name, const char *title,
// CTOR

// fit parameters
TIterator* parItr = parList.createIterator() ;
RooAbsArg* par ;
for (Int_t i=0; (par = (RooAbsArg*)parItr->Next()); ++i) {
for (RooAbsArg *par : parList) {
if (!dynamic_cast<RooAbsReal*>(par)) {
coutE(InputArguments) << "RooStarMomentMorph::ctor(" << GetName() << ") ERROR: parameter " << par->GetName() << " is not of type RooAbsReal" << endl ;
throw std::string("RooStarMomentMorh::ctor() ERROR parameter is not of type RooAbsReal") ;
}
_parList.add(*par) ;
}
delete parItr ;

// observables
TIterator* obsItr = obsList.createIterator() ;
RooAbsArg* var ;
for (Int_t i=0; (var = (RooAbsArg*)obsItr->Next()); ++i) {
for (RooAbsArg *var : obsList) {
if (!dynamic_cast<RooAbsReal*>(var)) {
coutE(InputArguments) << "RooStarMomentMorph::ctor(" << GetName() << ") ERROR: variable " << var->GetName() << " is not of type RooAbsReal" << endl ;
throw std::string("RooStarMomentMorh::ctor() ERROR variable is not of type RooAbsReal") ;
}
_obsList.add(*var) ;
}
delete obsItr ;

// reference p.d.f.s
TIterator* pdfItr = pdfList.createIterator() ;
RooAbsPdf* pdf ;
for (Int_t i=0; (pdf = dynamic_cast<RooAbsPdf*>(pdfItr->Next())); ++i) {
if (!pdf) {
for (RooAbsArg *pdf : pdfList) {
if (!dynamic_cast<RooAbsPdf*>(pdf)) {
coutE(InputArguments) << "RooStarMomentMorph::ctor(" << GetName() << ") ERROR: pdf " << pdf->GetName() << " is not of type RooAbsPdf" << endl ;
throw std::string("RooStarMomentMorph::ctor() ERROR pdf is not of type RooAbsPdf") ;
}
_pdfList.add(*pdf) ;
}
delete pdfItr ;

_parItr = _parList.createIterator() ;
_obsItr = _obsList.createIterator() ;
_pdfItr = _pdfList.createIterator() ;


// initialization
initialize();
Expand All @@ -1329,12 +1297,6 @@ RooStarMomentMorph::RooStarMomentMorph(const RooStarMomentMorph& other, const ch
_nnuisvar(other._nnuisvar),
_useHorizMorph(other._useHorizMorph)
{


_parItr = _parList.createIterator() ;
_obsItr = _obsList.createIterator() ;
_pdfItr = _pdfList.createIterator() ;

// nref is resized in initialize, so reduce the size here
if (_nref.size()>0) {
_nref.resize( _nref.size()-1 );
Expand All @@ -1347,9 +1309,6 @@ RooStarMomentMorph::RooStarMomentMorph(const RooStarMomentMorph& other, const ch
//_____________________________________________________________________________
RooStarMomentMorph::~RooStarMomentMorph()
{
if (_parItr) delete _parItr;
if (_obsItr) delete _obsItr;
if (_pdfItr) delete _pdfItr;
if (_M) delete _M;
}

Expand Down Expand Up @@ -1512,15 +1471,11 @@ RooStarMomentMorph::CacheElem* RooStarMomentMorph::getCache(const RooArgSet* /*n
//}

// construction of unit pdfs
_pdfItr->Reset();
RooAbsPdf* pdf;
RooArgList transPdfList;

for (Int_t i=0; i<nPdf; ++i) {
_obsItr->Reset() ;
RooRealVar* var ;

pdf = (RooAbsPdf*)_pdfItr->Next();
RooAbsPdf* pdf = &(RooAbsPdf&)_pdfList[i];

std::string pdfName = Form("pdf_%d",i);
RooCustomizer cust(*pdf,pdfName.c_str());
Expand All @@ -1545,7 +1500,7 @@ RooStarMomentMorph::CacheElem* RooStarMomentMorph::getCache(const RooArgSet* /*n
ownedComps.add(RooArgSet(*slope[sij(i,j)],*offsetVar[sij(i,j)])) ;

// linear transformations, so pdf can be renormalized easily
var = (RooRealVar*)(_obsItr->Next());
RooRealVar *var = (RooRealVar*)(_obsList[j]);
std::string transVarName = Form("%s_transVar_%d_%d",GetName(),i,j);
transVar[sij(i,j)] = new RooLinearVar(transVarName.c_str(),transVarName.c_str(),*var,*slope[sij(i,j)],*offsetVar[sij(i,j)]);

Expand Down Expand Up @@ -1680,7 +1635,6 @@ void RooStarMomentMorph::CacheElem::calculateFractions(const RooStarMomentMorph&
//int nObs=self._obsList.getSize();

// loop over parList
self._parItr->Reset();
int nnuis=0;

// zero all fractions
Expand All @@ -1691,7 +1645,7 @@ void RooStarMomentMorph::CacheElem::calculateFractions(const RooStarMomentMorph&
}
for (Int_t j=0; j<self._parList.getSize(); j++) {

RooRealVar* m = (RooRealVar*)(self._parItr->Next());
RooRealVar* m = &(RooRealVar&)self._parList[j];
double m0=m->getVal();

if (m0==0.) continue;
Expand Down
15 changes: 5 additions & 10 deletions src/CMSHggFormula.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ CMSHggFormulaA1::CMSHggFormulaA1(const char* name, const char* title, RooAbsReal
p3_("p3", "p3", this, p3),
terms_("terms", "terms", this),
coeffs_(coeffs) {
RooFIter iter = terms.fwdIterator();
for (RooAbsArg* a = iter.next(); a != 0; a = iter.next()) {
for (RooAbsArg* a : terms) {
RooAbsReal* rar = dynamic_cast<RooAbsReal*>(a);
if (!rar) {
throw std::invalid_argument(std::string("Component ") + a->GetName() +
Expand Down Expand Up @@ -61,8 +60,7 @@ CMSHggFormulaA2::CMSHggFormulaA2(const char* name, const char* title, RooAbsReal
p3_("p3", "p3", this, p3),
terms_("terms", "terms", this),
coeffs_(coeffs) {
RooFIter iter = terms.fwdIterator();
for (RooAbsArg* a = iter.next(); a != 0; a = iter.next()) {
for (RooAbsArg *a : terms) {
RooAbsReal* rar = dynamic_cast<RooAbsReal*>(a);
if (!rar) {
throw std::invalid_argument(std::string("Component ") + a->GetName() +
Expand Down Expand Up @@ -107,8 +105,7 @@ CMSHggFormulaB1::CMSHggFormulaB1(const char* name, const char* title, RooAbsReal
p0_("p0", "p0", this, p0),
terms_("terms", "terms", this),
coeffs_(coeffs) {
RooFIter iter = terms.fwdIterator();
for (RooAbsArg* a = iter.next(); a != 0; a = iter.next()) {
for (RooAbsArg *a : terms) {
RooAbsReal* rar = dynamic_cast<RooAbsReal*>(a);
if (!rar) {
throw std::invalid_argument(std::string("Component ") + a->GetName() +
Expand Down Expand Up @@ -150,8 +147,7 @@ CMSHggFormulaB2::CMSHggFormulaB2(const char* name, const char* title, double con
p0_(p0),
terms_("terms", "terms", this),
coeffs_(coeffs) {
RooFIter iter = terms.fwdIterator();
for (RooAbsArg* a = iter.next(); a != 0; a = iter.next()) {
for (RooAbsArg* a : terms) {
RooAbsReal* rar = dynamic_cast<RooAbsReal*>(a);
if (!rar) {
throw std::invalid_argument(std::string("Component ") + a->GetName() +
Expand Down Expand Up @@ -192,8 +188,7 @@ CMSHggFormulaC1::CMSHggFormulaC1(const char* name, const char* title, RooArgList
: RooAbsReal(name, title),
terms_("terms", "terms", this),
coeffs_(coeffs) {
RooFIter iter = terms.fwdIterator();
for (RooAbsArg* a = iter.next(); a != 0; a = iter.next()) {
for (RooAbsArg* a : terms) {
RooAbsReal* rar = dynamic_cast<RooAbsReal*>(a);
if (!rar) {
throw std::invalid_argument(std::string("Component ") + a->GetName() +
Expand Down
4 changes: 1 addition & 3 deletions src/CMSHistErrorPropagator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,7 @@ void CMSHistErrorPropagator::setAnalyticBarlowBeeston(bool flag) const {
if (bintypes_[j][0] == 1 && !vbinpars_[j][0]->isConstant()) {
bb_.use.push_back(j);
double gobs_val = 0.;
RooFIter iter = vbinpars_[j][0]->valueClientMIterator();
RooAbsArg *arg = nullptr;
while((arg = iter.next())) {
for (RooAbsArg * arg : vbinpars_[j][0]->valueClients()) {
if (arg == this || arg == &binsentry_) {
// std::cout << "Skipping " << this << " " << this->GetName() << "\n";
} else {
Expand Down
4 changes: 1 addition & 3 deletions src/CMSHistFunc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1202,9 +1202,7 @@ Double_t CMSHistFunc::analyticalIntegral(Int_t code,
}

CMSHistFuncWrapper const* CMSHistFunc::wrapper() const {
RooFIter iter = this->valueClientMIterator();
RooAbsArg *arg = nullptr;
while((arg = iter.next())) {
for (RooAbsArg *arg : valueClients()) {
CMSHistFuncWrapper const* wrapper = dynamic_cast<CMSHistFuncWrapper const*>(arg);
if (wrapper) return wrapper;
}
Expand Down
4 changes: 1 addition & 3 deletions src/CMSHistFuncWrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ void CMSHistFuncWrapper::initialize() const {
pfunc_ = dynamic_cast<CMSHistFunc const*>(&(func_.arg()));
perr_ = dynamic_cast<CMSHistErrorPropagator *>(err_.absArg());
auto sentry_args = perr_->getSentryArgs();
RooFIter iter = sentry_args->fwdIterator() ;
RooAbsArg* arg;
while((arg = iter.next())) {
for (RooAbsArg *arg : *sentry_args) {
sentry_.addArg(*arg);
}
sentry_.setValueDirty();
Expand Down
4 changes: 1 addition & 3 deletions src/CMSHistSum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,7 @@ void CMSHistSum::setAnalyticBarlowBeeston(bool flag) const {
if (bintypes_[j][0] == 1 && !vbinpars_[j][0]->isConstant()) {
bb_.use.push_back(j);
double gobs_val = 0.;
RooFIter iter = vbinpars_[j][0]->valueClientMIterator();
RooAbsArg *arg = nullptr;
while((arg = iter.next())) {
for (RooAbsArg *arg : vbinpars_[j][0]->valueClients()) {
if (arg == this || arg == &binsentry_) {
// std::cout << "Skipping " << this << " " << this->GetName() << "\n";
} else {
Expand Down
Loading

0 comments on commit 3664ef7

Please sign in to comment.