Skip to content

Commit c4f3f2b

Browse files
committed
[RF] Don't use std::to_string for floating point numbers
The `std::to_string` function is not locale-independent, so we should better not use is for floating point numbers.
1 parent d31ba4e commit c4f3f2b

File tree

6 files changed

+43
-25
lines changed

6 files changed

+43
-25
lines changed

roofit/codegen/src/CodegenImpl.cxx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -501,15 +501,16 @@ void codegenImpl(RooParamHistFunc &arg, CodegenContext &ctx)
501501
{
502502
std::string const &idx = arg.dataHist().calculateTreeIndexForCodeSquash(ctx, arg.xList());
503503
std::string arrName = ctx.buildArg(arg.paramList());
504-
std::string result = arrName + "[" + idx + "]";
504+
std::stringstream result;
505+
result << arrName << "[" << idx << "]";
505506
if (arg.relParam()) {
506507
// get weight[idx] * binv[idx]. Here we get the bin volume for the first element as we assume the distribution to
507508
// be binned uniformly.
508509
double binV = arg.dataHist().binVolume(0);
509510
std::string weightArr = arg.dataHist().declWeightArrayForCodeSquash(ctx, false);
510-
result += " * *(" + weightArr + " + " + idx + ") * " + std::to_string(binV);
511+
result << " * *(" << weightArr << " + " << idx + ") * " << binV;
511512
}
512-
ctx.addResult(&arg, result);
513+
ctx.addResult(&arg, result.str());
513514
}
514515

515516
void codegenImpl(RooPoisson &arg, CodegenContext &ctx)
@@ -812,7 +813,9 @@ std::string rooHistIntegralTranslateImpl(int code, RooAbsArg const &arg, RooData
812813
<< std::endl;
813814
return "";
814815
}
815-
return std::to_string(dataHist.sum(histFuncMode));
816+
std::stringstream ss;
817+
ss << dataHist.sum(histFuncMode);
818+
return ss.str();
816819
}
817820

818821
} // namespace
@@ -853,7 +856,9 @@ std::string codegenIntegralImpl(RooMultiVarGaussian &arg, int code, const char *
853856
throw std::runtime_error(errorMsg.str().c_str());
854857
}
855858

856-
return std::to_string(arg.analyticalIntegral(code, rangeName));
859+
std::stringstream ss;
860+
ss << arg.analyticalIntegral(code, rangeName);
861+
return ss.str();
857862
}
858863

859864
std::string codegenIntegralImpl(RooPoisson &arg, int code, const char *rangeName, CodegenContext &ctx)

roofit/jsoninterface/src/JSONParser.cxx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ inline nlohmann::json parseWrapper(std::istream &is)
2929

3030
// TJSONTree methods
3131

32-
TJSONTree::TJSONTree() : root(this){};
32+
TJSONTree::TJSONTree() : root(this) {};
3333

34-
TJSONTree::TJSONTree(std::istream &is) : root(this, is){};
34+
TJSONTree::TJSONTree(std::istream &is) : root(this, is) {};
3535

3636
TJSONTree::~TJSONTree()
3737
{
@@ -248,7 +248,11 @@ std::string TJSONTree::Node::val() const
248248
case nlohmann::json::value_t::boolean: return node->get().get<bool>() ? "true" : "false";
249249
case nlohmann::json::value_t::number_integer: return std::to_string(node->get().get<int>());
250250
case nlohmann::json::value_t::number_unsigned: return std::to_string(node->get().get<unsigned int>());
251-
case nlohmann::json::value_t::number_float: return std::to_string(node->get().get<double>());
251+
case nlohmann::json::value_t::number_float: {
252+
std::stringstream ss;
253+
ss << node->get().get<double>();
254+
return ss.str();
255+
}
252256
default:
253257
throw std::runtime_error("node \"" + node->key() + "\": implicit string conversion for type " +
254258
node->get().type_name() + " not supported!");
@@ -321,9 +325,12 @@ using const_json_iterator = nlohmann::basic_json<>::const_iterator;
321325
template <class Nd, class NdType, class json_it>
322326
class TJSONTree::Node::ChildItImpl final : public RooFit::Detail::JSONNode::child_iterator_t<Nd>::Impl {
323327
public:
324-
enum class POS { BEGIN, END };
328+
enum class POS {
329+
BEGIN,
330+
END
331+
};
325332
ChildItImpl(NdType &n, POS p)
326-
: node(n), iter(p == POS::BEGIN ? n.get_node().get().begin() : n.get_node().get().end()){};
333+
: node(n), iter(p == POS::BEGIN ? n.get_node().get().begin() : n.get_node().get().end()) {};
327334
ChildItImpl(NdType &n, json_it it) : node(n), iter(it) {}
328335
ChildItImpl(const ChildItImpl &other) : node(other.node), iter(other.iter) {}
329336
using child_iterator = RooFit::Detail::JSONNode::child_iterator_t<Nd>;

roofit/roofitcore/inc/RooFit/CodegenContext.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,12 @@ std::string CodegenContext::buildArgSpanImpl(std::span<const T> arr)
223223
{
224224
unsigned int n = arr.size();
225225
std::string arrName = getTmpVarName();
226-
std::string arrDecl = typeName<T>() + " " + arrName + "[" + std::to_string(n) + "] = {";
226+
std::stringstream ss;
227+
ss << typeName<T>() << " " << arrName << "[" << n << "] = {";
227228
for (unsigned int i = 0; i < n; i++) {
228-
arrDecl += " " + std::to_string(arr[i]) + ",";
229+
ss << " " << arr[i] << ",";
229230
}
231+
std::string arrDecl = ss.str();
230232
arrDecl.back() = '}';
231233
arrDecl += ";\n";
232234
addToCodeBody(arrDecl, true);

roofit/roofitcore/src/RooAbsArg.cxx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1970,12 +1970,14 @@ void RooAbsArg::graphVizTree(std::ostream &os, const char *delimiter, bool useTi
19701970
std::string typeFormat = "\\texttt{";
19711971
std::string nodeType = (useLatex) ? typeFormat + node->ClassName() + "}" : node->ClassName();
19721972

1973+
os << "\"" << nodeName << "\" [ color=" << (node->isFundamental() ? "blue" : "red") << ", label=\"" << nodeType
1974+
<< delimiter << nodeLabel;
1975+
19731976
if (auto realNode = dynamic_cast<RooAbsReal *>(node)) {
1974-
nodeLabel += delimiter + std::to_string(realNode->getVal());
1977+
os << delimiter << realNode->getVal();
19751978
}
19761979

1977-
os << "\"" << nodeName << "\" [ color=" << (node->isFundamental() ? "blue" : "red") << ", label=\"" << nodeType
1978-
<< delimiter << nodeLabel << "\"];" << std::endl;
1980+
os << "\"];" << std::endl;
19791981
}
19801982

19811983
// Get set of all server links

roofit/roofitcore/test/testRooProdPdf.cxx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ TEST(RooProdPdf, TestGetPartIntList)
8383

8484
RooWorkspace ws;
8585

86-
double a = 10.;
87-
double b = 4.;
88-
double c = 2.5;
86+
auto &x = static_cast<RooRealVar &>(*ws.factory("x[0, 0, 10.]"));
87+
auto &y = static_cast<RooRealVar &>(*ws.factory("y[0, 0, 4.]"));
88+
auto &z = static_cast<RooRealVar &>(*ws.factory("z[0, 0, 2.5]"));
8989

90-
auto &x = static_cast<RooRealVar &>(*ws.factory("x[0, 0, " + std::to_string(a) + "]"));
91-
auto &y = static_cast<RooRealVar &>(*ws.factory("y[0, 0, " + std::to_string(b) + "]"));
92-
auto &z = static_cast<RooRealVar &>(*ws.factory("z[0, 0, " + std::to_string(c) + "]"));
90+
double a = x.getMax();
91+
double b = y.getMax();
92+
double c = z.getMax();
9393

9494
// Factorize the product in one 1D and one 2D pdf to get a more complicated
9595
// and complete test case.

roofit/roofitcore/test/testRooRombergIntegrator.cxx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ void testConvergenceSettings(const RooFormulaVar &formula, const RooArgSet &obse
112112
SCOPED_TRACE(std::string("Function to integrate: ") + name + "\t" + formula.GetTitle());
113113

114114
for (double relEps : {1.E-3, 1.E-6, 1.E-8}) {
115-
SCOPED_TRACE(std::string("relEps=" + std::to_string(relEps)));
115+
std::stringstream traceName;
116+
traceName << "relEps=" << relEps;
117+
SCOPED_TRACE(traceName.str());
116118
TStopwatch stopNew;
117119
TStopwatch stopRoot;
118120

@@ -300,9 +302,9 @@ TEST(RooIntegrator1D, RunErf_NStep)
300302
const double min = limits.first;
301303
const double max = limits.second;
302304
RooRealVar theX("theX", "x", min, max);
303-
std::string funcStr =
304-
std::string("ROOT::Math::gaussian_pdf(theX, ") + std::to_string(sigma) + ", " + std::to_string(mean) + ")";
305-
RooFormulaVar gaus("gaus", funcStr.c_str(), theX);
305+
std::stringstream funcStr;
306+
funcStr << "ROOT::Math::gaussian_pdf(theX, " << sigma << ", " << mean << ")";
307+
RooFormulaVar gaus("gaus", funcStr.str().c_str(), theX);
306308
RooRealBinding binding(gaus, theX);
307309

308310
double targetError = 0.;

0 commit comments

Comments
 (0)