Skip to content

Commit 4f2cf43

Browse files
committed
[RF] Modernize RooFit code with static_range_cast and dynamic_range_cast
The `static_range_cast` and `dynamic_range_cast` are RooFit helper functions to iterate over polymorphic collections like `RooArgSet` and `RooArgList`. Using them instead of casting in a separate line makes the code more concise with fewer lines and throw-away temoprary variables.
1 parent 2b876bb commit 4f2cf43

22 files changed

Lines changed: 76 additions & 122 deletions

roofit/hs3/src/JSONFactories_HistFactory.cxx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,8 +1412,7 @@ class HistFactoryStreamer_ProdPdf : public RooFit::JSONIO::Exporter {
14121412
{
14131413
std::vector<RooAbsPdf *> constraints;
14141414
RooRealSumPdf *sumpdf = nullptr;
1415-
for (RooAbsArg *v : prodpdf->pdfList()) {
1416-
RooAbsPdf *pdf = static_cast<RooAbsPdf *>(v);
1415+
for (auto *pdf : static_range_cast<RooAbsPdf *>(prodpdf->pdfList())) {
14171416
auto thispdf = dynamic_cast<RooRealSumPdf *>(pdf);
14181417
if (thispdf) {
14191418
if (!sumpdf)

roofit/hs3/src/JSONFactories_RooFitCore.cxx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,7 @@ class ParamHistFuncFactory : public RooFit::JSONIO::Importer {
627627

628628
// Now build the final list following the order in varList
629629
RooArgList vars;
630-
for (int i = 0; i < varList.getSize(); ++i) {
631-
const auto *refVar = dynamic_cast<RooRealVar *>(varList.at(i));
630+
for (auto *refVar : dynamic_range_cast<RooRealVar *>(varList)) {
632631
if (!refVar)
633632
continue;
634633

roofit/roofit/src/RooJeffreysPrior.cxx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,10 @@ double RooJeffreysPrior::evaluate() const
8282
auto& pdf = _nominal.arg();
8383
RooAbsPdf* clonePdf = static_cast<RooAbsPdf*>(pdf.cloneTree());
8484
std::unique_ptr<RooArgSet> vars{clonePdf->getParameters(_obsSet)};
85-
for (auto varTmp : *vars) {
86-
auto& var = static_cast<RooRealVar&>(*varTmp);
87-
auto range = var.getRange();
85+
for (auto* var : static_range_cast<RooRealVar*>(*vars)) {
86+
auto range = var->getRange();
8887
double span = range.second - range.first;
89-
var.setRange(range.first - 0.1*span, range.second + 0.1 * span);
88+
var->setRange(range.first - 0.1*span, range.second + 0.1 * span);
9089
}
9190

9291
cacheElm = new CacheElem;

roofit/roofit/src/RooLagrangianMorphFunc.cxx

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,7 @@ void setOwnerRecursive(TFolder *theFolder)
461461
theFolder->SetOwner();
462462
// And also need to set up ownership for nested folders
463463
auto subdirs = theFolder->GetListOfFolders();
464-
for (auto *subdir : *subdirs) {
465-
auto thisfolder = dynamic_cast<TFolder *>(subdir);
464+
for (auto *thisfolder : dynamic_range_cast<TFolder *>(*subdirs)) {
466465
if (thisfolder) {
467466
// no explicit deletion here, will be handled by parent
468467
setOwnerRecursive(thisfolder);
@@ -678,8 +677,7 @@ inline bool setParam(RooRealVar *p, double val, bool force)
678677
template <class T1, class T2>
679678
inline bool setParams(const T2 &args, T1 val)
680679
{
681-
for (auto itr : args) {
682-
RooRealVar *param = dynamic_cast<RooRealVar *>(itr);
680+
for (auto *param : dynamic_range_cast<RooRealVar *>(args)) {
683681
if (!param)
684682
continue;
685683
setParam(param, val, true);
@@ -696,8 +694,7 @@ inline bool
696694
setParams(const std::map<const std::string, T1> &point, const T2 &args, bool force = false, T1 defaultVal = 0)
697695
{
698696
bool ok = true;
699-
for (auto itr : args) {
700-
RooRealVar *param = dynamic_cast<RooRealVar *>(itr);
697+
for (auto *param : dynamic_range_cast<RooRealVar *>(args)) {
701698
if (!param || param->isConstant())
702699
continue;
703700
ok = setParam(param, defaultVal, force) && ok;
@@ -725,8 +722,7 @@ inline bool setParams(TH1 *hist, const T &args, bool force = false)
725722
{
726723
bool ok = true;
727724

728-
for (auto itr : args) {
729-
RooRealVar *param = dynamic_cast<RooRealVar *>(itr);
725+
for (auto *param : dynamic_range_cast<RooRealVar *>(args)) {
730726
if (!param)
731727
continue;
732728
ok = setParam(param, 0., force) && ok;
@@ -752,8 +748,7 @@ template <class T>
752748
inline RooLagrangianMorphFunc::ParamSet getParams(const T &parameters)
753749
{
754750
RooLagrangianMorphFunc::ParamSet retval;
755-
for (auto itr : parameters) {
756-
RooRealVar *param = dynamic_cast<RooRealVar *>(itr);
751+
for (auto *param : dynamic_range_cast<RooRealVar *>(parameters)) {
757752
if (!param)
758753
continue;
759754
retval[param->GetName()] = param->getVal();
@@ -977,9 +972,7 @@ inline void fillFeynmanDiagram(FeynmanDiagram &diagram, const std::vector<List *
977972
for (auto const &vertex : vertices) {
978973
std::vector<bool> vertexCouplings(ncouplings, false);
979974
int idx = -1;
980-
RooAbsReal *coupling;
981-
for (auto citr : couplings) {
982-
coupling = dynamic_cast<RooAbsReal *>(citr);
975+
for (auto *coupling : dynamic_range_cast<RooAbsReal *>(couplings)) {
983976
idx++;
984977
if (!coupling) {
985978
std::cerr << "encountered invalid list of couplings in vertex!" << std::endl;
@@ -1120,20 +1113,17 @@ FormulaList buildFormulas(const char *mfname, const RooLagrangianMorphFunc::Para
11201113
std::cerr << "internal error, number of operators inconsistent!" << std::endl;
11211114
}
11221115

1123-
RooAbsReal *obj0;
11241116
int idx = 0;
11251117

1126-
for (auto itr1 : couplings) {
1127-
obj0 = dynamic_cast<RooAbsReal *>(itr1);
1118+
for (auto *obj0 : dynamic_range_cast<RooAbsReal *>(couplings)) {
11281119
if (obj0->getVal() != 0) {
11291120
couplingsZero[idx] = false;
11301121
}
11311122
idx++;
11321123
}
11331124
}
11341125

1135-
for (auto itr2 : flags) {
1136-
auto obj1 = dynamic_cast<RooAbsReal *>(itr2);
1126+
for (auto *obj1 : dynamic_range_cast<RooAbsReal *>(flags)) {
11371127
int nZero = 0;
11381128
int nNonZero = 0;
11391129
for (auto sampleit : inputFlags) {
@@ -1203,8 +1193,7 @@ FormulaList buildFormulas(const char *mfname, const RooLagrangianMorphFunc::Para
12031193
// check and apply flags
12041194
bool removedByFlag = false;
12051195

1206-
for (auto itr : flags) {
1207-
auto obj = dynamic_cast<RooAbsReal *>(itr);
1196+
for (auto *obj : dynamic_range_cast<RooAbsReal *>(flags)) {
12081197
if (!obj)
12091198
continue;
12101199
TString sval(obj->getStringAttribute("NewPhysics"));
@@ -1411,10 +1400,8 @@ class RooLagrangianMorphFunc::CacheElem : public RooAbsCacheElement {
14111400
// set all vars to value stored in input file
14121401
setParams(sampleit.second, operators, true);
14131402
bool first = true;
1414-
RooAbsReal *obj;
14151403

1416-
for (auto itr : _couplings) {
1417-
obj = dynamic_cast<RooAbsReal *>(itr);
1404+
for (auto *obj : dynamic_range_cast<RooAbsReal *>(_couplings)) {
14181405
if (!first)
14191406
std::cerr << ", ";
14201407
oocxcoutW((TObject *)nullptr, Eval) << obj->GetName() << "=" << obj->getVal();
@@ -1953,8 +1940,8 @@ RooLagrangianMorphFunc::RooLagrangianMorphFunc(const RooLagrangianMorphFunc &oth
19531940
{
19541941
for (size_t j = 0; j < other._diagrams.size(); ++j) {
19551942
std::vector<RooListProxy *> diagram;
1956-
for (size_t i = 0; i < other._diagrams[j].size(); ++i) {
1957-
RooListProxy *list = new RooListProxy(other._diagrams[j][i]->GetName(), this, *(other._diagrams[j][i]));
1943+
for (auto *elem : other._diagrams[j]) {
1944+
RooListProxy *list = new RooListProxy(elem->GetName(), this, *elem);
19581945
diagram.push_back(list);
19591946
}
19601947
_diagrams.push_back(diagram);
@@ -2158,8 +2145,7 @@ RooProduct *RooLagrangianMorphFunc::getSumElement(const char *name) const
21582145
prodname.Append("_");
21592146
prodname.Append(this->GetName());
21602147

2161-
for (auto itr : *args) {
2162-
RooProduct *prod = dynamic_cast<RooProduct *>(itr);
2148+
for (auto *prod : dynamic_range_cast<RooProduct *>(*args)) {
21632149
if (!prod)
21642150
continue;
21652151
TString sname(prod->GetName());
@@ -2215,11 +2201,9 @@ void RooLagrangianMorphFunc::printSampleWeights() const
22152201

22162202
void RooLagrangianMorphFunc::randomizeParameters(double z)
22172203
{
2218-
RooRealVar *obj;
22192204
TRandom3 r;
22202205

2221-
for (auto itr : _operators) {
2222-
obj = dynamic_cast<RooRealVar *>(itr);
2206+
for (auto *obj : dynamic_range_cast<RooRealVar *>(_operators)) {
22232207
double val = obj->getVal();
22242208
if (obj->isConstant())
22252209
continue;
@@ -2504,8 +2488,7 @@ RooLagrangianMorphFunc::ParamSet RooLagrangianMorphFunc::getMorphParameters(cons
25042488

25052489
void RooLagrangianMorphFunc::setParameters(const RooArgList *list)
25062490
{
2507-
for (auto itr : *list) {
2508-
RooRealVar *param = dynamic_cast<RooRealVar *>(itr);
2491+
for (auto *param : dynamic_range_cast<RooRealVar *>(*list)) {
25092492
if (!param)
25102493
continue;
25112494
this->setParameter(param->GetName(), param->getVal());
@@ -2562,8 +2545,7 @@ TH1 *RooLagrangianMorphFunc::createTH1(const std::string &name, bool correlateEr
25622545
double val = 0;
25632546
double unc2 = 0;
25642547
double unc = 0;
2565-
for (auto itr : *args) {
2566-
RooProduct *prod = dynamic_cast<RooProduct *>(itr);
2548+
for (auto *prod : dynamic_range_cast<RooProduct *>(*args)) {
25672549
if (!prod)
25682550
continue;
25692551
RooAbsArg *phys = prod->components().find(Form("phys_%s", prod->GetName()));
@@ -2595,8 +2577,7 @@ int RooLagrangianMorphFunc::countContributingFormulas() const
25952577
if (!mf)
25962578
coutE(InputArguments) << "unable to retrieve morphing function" << std::endl;
25972579
std::unique_ptr<RooArgSet> args{mf->getComponents()};
2598-
for (auto itr : *args) {
2599-
RooProduct *prod = dynamic_cast<RooProduct *>(itr);
2580+
for (auto *prod : dynamic_range_cast<RooProduct *>(*args)) {
26002581
if (prod->getVal() != 0) {
26012582
nFormulas++;
26022583
}
@@ -2721,8 +2702,7 @@ const RooArgList *RooLagrangianMorphFunc::getCouplingSet() const
27212702
RooLagrangianMorphFunc::ParamSet RooLagrangianMorphFunc::getCouplings() const
27222703
{
27232704
RooLagrangianMorphFunc::ParamSet couplings;
2724-
for (auto obj : *(this->getCouplingSet())) {
2725-
RooAbsReal *var = dynamic_cast<RooAbsReal *>(obj);
2705+
for (auto *var : dynamic_range_cast<RooAbsReal *>(*(this->getCouplingSet()))) {
27262706
if (!var)
27272707
continue;
27282708
const std::string name(var->GetName());
@@ -2847,8 +2827,7 @@ double RooLagrangianMorphFunc::expectedUncertainty() const
28472827
void RooLagrangianMorphFunc::printParameters() const
28482828
{
28492829
// print the parameters and their current values
2850-
for (auto obj : _operators) {
2851-
RooRealVar *param = static_cast<RooRealVar *>(obj);
2830+
for (auto *param : static_range_cast<RooRealVar *>(_operators)) {
28522831
if (!param)
28532832
continue;
28542833
param->Print();
@@ -2860,8 +2839,7 @@ void RooLagrangianMorphFunc::printParameters() const
28602839

28612840
void RooLagrangianMorphFunc::printFlags() const
28622841
{
2863-
for (auto flag : _flags) {
2864-
RooRealVar *param = static_cast<RooRealVar *>(flag);
2842+
for (auto *param : static_range_cast<RooRealVar *>(_flags)) {
28652843
if (!param)
28662844
continue;
28672845
param->Print();

roofit/roofit/src/RooLegacyExpPoly.cxx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ double RooLegacyExpPoly::evaluateLog() const
109109
const double x = _x;
110110
double xpow = std::pow(x, lowestOrder);
111111
double retval = 0;
112-
for (size_t i = 0; i < sz; ++i) {
113-
retval += coefs[i] * xpow;
112+
for (double coef : coefs) {
113+
retval += coef * xpow;
114114
xpow *= x;
115115
}
116116

@@ -158,9 +158,8 @@ void RooLegacyExpPoly::adjustLimits()
158158
if (x) {
159159
const double xmax = x->getMax();
160160
double xmaxpow = std::pow(xmax, lowestOrder);
161-
for (size_t i = 0; i < sz; ++i) {
161+
for (auto *coef : dynamic_range_cast<RooRealVar *>(_coefList)) {
162162
double thismax = max / xmaxpow;
163-
RooRealVar *coef = dynamic_cast<RooRealVar *>(this->_coefList.at(i));
164163
if (coef) {
165164
coef->setVal(thismax);
166165
coef->setMax(thismax);

roofit/roofit/src/RooMultiBinomial.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ double RooMultiBinomial::evaluate() const
126126
// Calculate efficiency for combination of accept/reject categories
127127
// put equal to zero if combination of only zeros AND chosen to be invisible
128128

129-
for (int i=0; i<effFuncListSize; ++i) {
130-
_effVal=_effVal*effValue[i];
129+
for (double ev : effValue) {
130+
_effVal=_effVal*ev;
131131
if (notVisible && _ignoreNonVisible){
132132
_effVal=0;
133133
}

roofit/roofit/src/RooNDKeysPdf.cxx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,10 +1179,10 @@ double RooNDKeysPdf::analyticalIntegral(Int_t code, const char* rangeName) const
11791179
norm = bi->nEventsBMSW;
11801180
if (norm<0.) norm=0.;
11811181

1182-
for (Int_t i=0; i<Int_t(bi->sIdcs.size()); ++i) {
1182+
for (Int_t sIdx : bi->sIdcs) {
11831183
double prob=1.;
1184-
const vector<double>& x = _dataPts[bi->sIdcs[i]];
1185-
const vector<double>& weight = (*_weights)[_idx[bi->sIdcs[i]]];
1184+
const vector<double>& x = _dataPts[sIdx];
1185+
const vector<double>& weight = (*_weights)[_idx[sIdx]];
11861186

11871187
vector<double> chi(_nDim,100.);
11881188

@@ -1202,7 +1202,7 @@ double RooNDKeysPdf::analyticalIntegral(Int_t code, const char* rangeName) const
12021202
}
12031203
}
12041204

1205-
norm += prob * _wMap.at(_idx[bi->sIdcs[i]]);
1205+
norm += prob * _wMap.at(_idx[sIdx]);
12061206
}
12071207

12081208
cxcoutD(Eval) << "RooNDKeysPdf::analyticalIntegral() : Final normalization : " << norm << " " << bi->nEventsBW << std::endl;

roofit/roofitcore/src/FitHelpers.cxx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -783,8 +783,7 @@ std::unique_ptr<RooAbsReal> createNLL(RooAbsPdf &pdf, RooAbsData &data, const Ro
783783
// Create range with name 'fit' with above limits on all observables
784784
RooArgSet obs;
785785
pdf.getObservables(data.get(), obs);
786-
for (auto arg : obs) {
787-
RooRealVar *rrv = dynamic_cast<RooRealVar *>(arg);
786+
for (auto *rrv : dynamic_range_cast<RooRealVar *>(obs)) {
788787
if (rrv)
789788
rrv->setRange("fit", rangeLo, rangeHi);
790789
}
@@ -1040,8 +1039,8 @@ std::unique_ptr<RooAbsReal> createChi2(RooAbsReal &real, RooDataHist &data, cons
10401039
const double rangeHi = pc.getDouble("rangeHi");
10411040
RooArgSet obs;
10421041
real.getObservables(data.get(), obs);
1043-
for (auto arg : obs) {
1044-
if (auto *rrv = dynamic_cast<RooRealVar *>(arg)) {
1042+
for (auto *rrv : dynamic_range_cast<RooRealVar *>(obs)) {
1043+
if (rrv) {
10451044
rrv->setRange("fit", rangeLo, rangeHi);
10461045
}
10471046
}

roofit/roofitcore/src/RooAbsAnaConvPdf.cxx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,7 @@ bool RooAbsAnaConvPdf::changeModel(const RooResolutionModel& newModel)
209209
{
210210
RooArgList newConvSet ;
211211
bool allOK(true) ;
212-
for (auto convArg : _convSet) {
213-
auto conv = static_cast<RooResolutionModel*>(convArg);
212+
for (auto *conv : static_range_cast<RooResolutionModel*>(_convSet)) {
214213

215214
// Build new resolution model
216215
std::unique_ptr<RooResolutionModel> newConv{newModel.convolution(const_cast<RooFormulaVar*>(&conv->basis()),this)};
@@ -330,8 +329,7 @@ double RooAbsAnaConvPdf::evaluate() const
330329
double result(0) ;
331330

332331
Int_t index(0) ;
333-
for (auto convArg : _convSet) {
334-
auto conv = static_cast<RooAbsPdf*>(convArg);
332+
for (auto *conv : static_range_cast<RooAbsPdf*>(_convSet)) {
335333
double coef = coefficient(index++) ;
336334
if (coef!=0.) {
337335
const double c = conv->getVal(nullptr);

0 commit comments

Comments
 (0)