Skip to content

Commit 3408092

Browse files
committed
[RF] Replace some usage of Form with standard C++
1 parent fcab2d5 commit 3408092

File tree

8 files changed

+76
-64
lines changed

8 files changed

+76
-64
lines changed

roofit/roofit/test/testRooJohnson.cxx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,13 @@ TEST(RooJohnson, Integral)
170170
<< theMu << " " << theSig << " " << theGamma << " " << theDelta;
171171
}
172172

173-
// auto frame = mass.frame();
174-
// johnson.plotOn(frame, RooFit::LineColor(kBlue));
175-
// TCanvas canv;
176-
// frame->Draw();
177-
// canv.SaveAs(Form("/tmp/Johnson_%f_%f_%f_%f.png", theMu, theSig, theGamma, theDelta));
173+
// auto frame = mass.frame();
174+
// johnson.plotOn(frame, RooFit::LineColor(kBlue));
175+
// TCanvas canv;
176+
// frame->Draw();
177+
// std::stringstream ss;
178+
// ss << "/tmp/Johnson_" << theMu << "_" << theSig << "_" << theGamma << "_" << theDelta << ".png";
179+
// canv.SaveAs(ss.str().s_str());
178180
}
179181
}
180182
}

roofit/roofitcore/src/RooAbsData.cxx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,7 @@ RooPlot* RooAbsData::statOn(RooPlot* frame, const char* what, const char *label,
12251225
// create the box and set its options
12261226
TPaveText *box= new TPaveText(xmin,ymax,xmax,ymin,"BRNDC");
12271227
if(!box) return nullptr;
1228-
box->SetName(Form("%s_statBox",GetName())) ;
1228+
box->SetName((std::string{GetName()} + "_statBox").c_str());
12291229
box->SetFillColor(0);
12301230
box->SetBorderSize(1);
12311231
box->SetTextAlign(12);
@@ -2010,7 +2010,7 @@ RooPlot* RooAbsData::plotAsymOn(RooPlot* frame, const RooAbsCategoryLValue& asym
20102010

20112011
// convert this histogram to a RooHist object on the heap
20122012
RooHist *graph= new RooHist(*hist1,*hist2,0,1,o.etype,o.xErrorSize,false,o.scaleFactor);
2013-
graph->setYAxisLabel(Form("Asymmetry in %s",asymCat.GetName())) ;
2013+
graph->setYAxisLabel((std::string{"Asymmetry in "} + asymCat.GetName()).c_str());
20142014

20152015
// initialize the frame's normalization setup, if necessary
20162016
frame->updateNormVars(_vars);
@@ -2019,14 +2019,15 @@ RooPlot* RooAbsData::plotAsymOn(RooPlot* frame, const RooAbsCategoryLValue& asym
20192019
if (o.histName) {
20202020
graph->SetName(o.histName) ;
20212021
} else {
2022-
std::string hname{Form("h_%s_Asym[%s]",GetName(),asymCat.GetName())};
2023-
if (o.cutRange && strlen(o.cutRange)>0) {
2024-
hname += Form("_CutRange[%s]",o.cutRange);
2022+
std::stringstream hname;
2023+
hname << "h_" << GetName() << "_Asym[" << asymCat.GetName() << "]";
2024+
if (o.cutRange && strlen(o.cutRange) > 0) {
2025+
hname << "_CutRange[" << o.cutRange << "]";
20252026
}
20262027
if (o.cuts && strlen(o.cuts)>0) {
2027-
hname += Form("_Cut[%s]",o.cuts);
2028+
hname << "_Cut[" << o.cuts << "]";
20282029
}
2029-
graph->SetName(hname.c_str()) ;
2030+
graph->SetName(hname.str().c_str());
20302031
}
20312032

20322033
// add the RooHist to the specified plot

roofit/roofitcore/src/RooAbsPdf.cxx

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,11 @@ double RooAbsPdf::normalizeWithNaNPacking(double rawVal, double normVal) const {
288288
}
289289

290290
if (rawVal < 0.) {
291-
logEvalError(Form("p.d.f value is less than zero (%f), trying to recover", rawVal));
292-
clearValueAndShapeDirty();
293-
return RooNaNPacker::packFloatIntoNaN(-rawVal);
291+
std::stringstream ss;
292+
ss << "p.d.f value is less than zero (" << rawVal << "), trying to recover";
293+
logEvalError(ss.str().c_str());
294+
clearValueAndShapeDirty();
295+
return RooNaNPacker::packFloatIntoNaN(-rawVal);
294296
}
295297

296298
if (TMath::IsNaN(rawVal)) {
@@ -551,7 +553,7 @@ bool RooAbsPdf::syncNormalization(const RooArgSet* nset, bool adjustProxies) con
551553
if (!cacheParams.empty()) {
552554
cxcoutD(Caching) << "RooAbsReal::createIntObj(" << GetName() << ") INFO: constructing " << cacheParams.size()
553555
<< "-dim value cache for integral over " << depList << " as a function of " << cacheParams << " in range " << (nr?nr:"<default>") << std::endl ;
554-
string name = Form("%s_CACHE_[%s]",normInt->GetName(),cacheParams.contentsString().c_str()) ;
556+
std::string name = normInt->GetName() + ("_CACHE_[" + cacheParams.contentsString()) + "]";
555557
RooCachedReal* cachedIntegral = new RooCachedReal(name.c_str(),name.c_str(),*normInt,cacheParams) ;
556558
cachedIntegral->setInterpolationOrder(2) ;
557559
cachedIntegral->addOwnedComponents(*normInt) ;
@@ -2003,23 +2005,21 @@ RooPlot* RooAbsPdf::plotOn(RooPlot* frame, RooLinkedList& cmdList) const
20032005
bool haveCompSel = ( (compSpec && strlen(compSpec)>0) || compSet) ;
20042006

20052007
// Suffix for curve name
2006-
std::string nameSuffix ;
2008+
std::stringstream nameSuffix;
20072009
if (compSpec && strlen(compSpec)>0) {
2008-
nameSuffix.append("_Comp[") ;
2009-
nameSuffix.append(compSpec) ;
2010-
nameSuffix.append("]") ;
2010+
nameSuffix << "_Comp[" << compSpec << "]";
20112011
} else if (compSet) {
2012-
nameSuffix += "_Comp[" + compSet->contentsString() + "]";
2012+
nameSuffix << "_Comp[" << compSet->contentsString() << "]";
20132013
}
20142014

20152015
// Remove PDF-only commands from command list
20162016
RooCmdConfig::stripCmdList(cmdList,"SelectCompSet,SelectCompSpec") ;
20172017

20182018
// Adjust normalization, if so requested
20192019
if (asymCat) {
2020-
RooCmdArg cnsuffix("CurveNameSuffix",0,0,0,0,nameSuffix.c_str(),nullptr,nullptr,nullptr) ;
2021-
cmdList.Add(&cnsuffix);
2022-
return RooAbsReal::plotOn(frame,cmdList) ;
2020+
RooCmdArg cnsuffix("CurveNameSuffix", 0, 0, 0, 0, nameSuffix.str().c_str(), nullptr, nullptr, nullptr);
2021+
cmdList.Add(&cnsuffix);
2022+
return RooAbsReal::plotOn(frame, cmdList);
20232023
}
20242024

20252025
// More sanity checks
@@ -2060,7 +2060,7 @@ RooPlot* RooAbsPdf::plotOn(RooPlot* frame, RooLinkedList& cmdList) const
20602060
ccoutI(Plotting) << std::endl ;
20612061
}
20622062

2063-
nameSuffix.append(Form("_Range[%f_%f]",rangeLo,rangeHi)) ;
2063+
nameSuffix << "_Range[" << rangeLo << "_" << rangeHi << "]";
20642064

20652065
} else if (pc.hasProcessed("RangeWithName")) {
20662066

@@ -2083,7 +2083,7 @@ RooPlot* RooAbsPdf::plotOn(RooPlot* frame, RooLinkedList& cmdList) const
20832083
ccoutI(Plotting) << std::endl ;
20842084
}
20852085

2086-
nameSuffix.append("_Range[" + std::string(pc.getString("rangeName")) + "]");
2086+
nameSuffix << "_Range[" << pc.getString("rangeName") << "]";
20872087
}
20882088
// Specification of a normalization range override those in a regular range
20892089
if (pc.hasProcessed("NormRange")) {
@@ -2101,8 +2101,7 @@ RooPlot* RooAbsPdf::plotOn(RooPlot* frame, RooLinkedList& cmdList) const
21012101
hasCustomRange = true ;
21022102
coutI(Plotting) << "RooAbsPdf::plotOn(" << GetName() << ") p.d.f. curve is normalized using explicit choice of ranges '" << pc.getString("normRangeName", "", false) << "'" << std::endl ;
21032103

2104-
nameSuffix.append("_NormRange[" + std::string(pc.getString("rangeName")) + "]");
2105-
2104+
nameSuffix << "_NormRange[" << pc.getString("rangeName") << "]";
21062105
}
21072106

21082107
if (hasCustomRange && adjustNorm) {
@@ -2198,8 +2197,7 @@ RooPlot* RooAbsPdf::plotOn(RooPlot* frame, RooLinkedList& cmdList) const
21982197
}
21992198
}
22002199

2201-
2202-
RooCmdArg cnsuffix("CurveNameSuffix",0,0,0,0,nameSuffix.c_str(),nullptr,nullptr,nullptr) ;
2200+
RooCmdArg cnsuffix("CurveNameSuffix", 0, 0, 0, 0, nameSuffix.str().c_str(), nullptr, nullptr, nullptr);
22032201
cmdList.Add(&cnsuffix);
22042202

22052203
RooPlot* ret = RooAbsReal::plotOn(frame,cmdList) ;

roofit/roofitcore/src/RooDouble.cxx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Minimal implementation of a TObject holding a double value.
2323
**/
2424

2525
#include "RooDouble.h"
26+
27+
#include <sstream>
2628
#include <string>
2729

2830

@@ -31,7 +33,9 @@ Minimal implementation of a TObject holding a double value.
3133

3234
RooDouble::RooDouble(double value) : _value(value)
3335
{
34-
SetName(Form("%f",value)) ;
36+
std::stringstream ss;
37+
ss << value;
38+
SetName(ss.str().c_str());
3539
}
3640

3741

roofit/roofitcore/src/RooMinimizer.cxx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,9 @@ RooFit::OwningPtr<RooFitResult> RooMinimizer::save(const char *userName, const c
536536
return nullptr;
537537
}
538538

539-
TString name = userName ? userName : Form("%s", _fcn->getFunctionName().c_str());
540-
TString title = userTitle ? userTitle : Form("%s", _fcn->getFunctionTitle().c_str());
541-
auto fitRes = std::make_unique<RooFitResult>(name, title);
539+
std::string name = userName ? std::string{userName} : _fcn->getFunctionName();
540+
std::string title = userTitle ? std::string{userTitle} : _fcn->getFunctionTitle();
541+
auto fitRes = std::make_unique<RooFitResult>(name.c_str(), title.c_str());
542542

543543
fitRes->setConstParList(_fcn->constParams());
544544

@@ -682,7 +682,9 @@ RooPlot *RooMinimizer::contour(RooRealVar &var1, RooRealVar &var2, double n1, do
682682
ycoor[npoints] = ycoor[0];
683683
TGraph *graph = new TGraph(npoints + 1, xcoor.data(), ycoor.data());
684684

685-
graph->SetName(Form("contour_%s_n%f", _fcn->getFunctionName().c_str(), n[ic]));
685+
std::stringstream name;
686+
name << "contour_" << _fcn->getFunctionName() << "_n" << n[ic];
687+
graph->SetName(name.str().c_str());
686688
graph->SetLineStyle(ic + 1);
687689
graph->SetLineWidth(2);
688690
graph->SetLineColor(kBlue);

roofit/roofitcore/src/RooSecondMoment.cxx

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,19 @@ RooSecondMoment::RooSecondMoment(const char* name, const char* title, RooAbsReal
5757
std::unique_ptr<RooAbsReal> XF;
5858
if (centr) {
5959

60-
string m1name=Form("%s_moment1",GetName()) ;
61-
_mean.putOwnedArg(std::unique_ptr<RooAbsMoment>{func.mean(x)}) ;
60+
std::string m1name = std::string{GetName()} + "_moment1";
61+
_mean.putOwnedArg(std::unique_ptr<RooAbsMoment>{func.mean(x)});
6262

63-
string pname=Form("%s_product",name) ;
64-
_xfOffset = _mean->getVal() ;
65-
XF = std::make_unique<RooFormulaVar>(pname.c_str(),Form("pow((@0-%f),2)*@1",_xfOffset),RooArgList(x,func)) ;
63+
std::string pname = std::string{name} + "_product";
64+
_xfOffset = _mean->getVal();
65+
std::stringstream formula;
66+
formula << "std::pow((@0-" << _xfOffset << "),2) * @1";
67+
XF = std::make_unique<RooFormulaVar>(pname.c_str(), formula.str().c_str(), RooArgList(x, func));
6668

6769
} else {
6870

69-
string pname=Form("%s_product",name) ;
70-
XF = std::make_unique<RooProduct>(pname.c_str(),pname.c_str(),RooArgList(x,x,func)) ;
71+
std::string pname = std::string{name} + "_product";
72+
XF = std::make_unique<RooProduct>(pname.c_str(), pname.c_str(), RooArgList(x, x, func));
7173
}
7274

7375
XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
@@ -106,19 +108,19 @@ RooSecondMoment::RooSecondMoment(const char* name, const char* title, RooAbsReal
106108
std::unique_ptr<RooAbsReal> XF;
107109
if (centr) {
108110

109-
string m1name=Form("%s_moment1",GetName()) ;
110-
_mean.putOwnedArg(std::unique_ptr<RooAbsMoment>{func.mean(x,nset)}) ;
111-
112-
string pname=Form("%s_product",name) ;
113-
_xfOffset = _mean->getVal() ;
114-
XF = std::make_unique<RooFormulaVar>(pname.c_str(),Form("pow((@0-%f),2)*@1",_xfOffset),RooArgList(x,func)) ;
111+
std::string m1name = std::string{GetName()} + "_moment1";
112+
_mean.putOwnedArg(std::unique_ptr<RooAbsMoment>{func.mean(x, nset)});
115113

114+
std::string pname = std::string{name} + "_product";
115+
_xfOffset = _mean->getVal();
116+
std::stringstream formula;
117+
formula << "std::pow((@0-" << _xfOffset << "),2) * @1";
118+
XF = std::make_unique<RooFormulaVar>(pname.c_str(), formula.str().c_str(), RooArgList(x, func));
116119

117120
} else {
118121

119-
string pname=Form("%s_product",name) ;
120-
XF = std::make_unique<RooProduct>(pname.c_str(),pname.c_str(),RooArgList(x,x,func)) ;
121-
122+
std::string pname = std::string{name} + "_product";
123+
XF = std::make_unique<RooProduct>(pname.c_str(), pname.c_str(), RooArgList(x, x, func));
122124
}
123125

124126
XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;

roofit/roofitcore/test/testRooDataSet.cxx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,15 @@ TEST(RooDataSet, ReducingData)
169169

170170
for (int i = 0; i < 3; ++i) {
171171
// Check with root:
172-
TH1F test_hist(Form("h%i", i), "histo", 10, massmin, massmax);
172+
TH1F test_hist(("h" + std::to_string(i)).c_str(), "histo", 10, massmin, massmax);
173173
chi2cutval += 0.5;
174174

175-
TCut chi2_test_cut = Form("max(track0_chi2,track1_chi2)<%f", chi2cutval);
175+
std::stringstream cutString;
176+
cutString << "max(track0_chi2,track1_chi2)<" << chi2cutval;
177+
TCut chi2_test_cut = cutString.str().c_str();
176178

177-
Long64_t drawnEvents = mytree.Draw(Form("mass>>h%i", i), chi2_test_cut /*&& mass_cut*/);
179+
std::string drawString = std::string{"mass>>"} + test_hist.GetName();
180+
Long64_t drawnEvents = mytree.Draw(drawString.c_str(), chi2_test_cut /*&& mass_cut*/);
178181
ASSERT_NE(drawnEvents, 0l);
179182
ASSERT_EQ(test_hist.Integral(), drawnEvents);
180183

roofit/roostats/src/BayesianCalculator.cxx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -861,10 +861,10 @@ RooAbsReal* BayesianCalculator::GetPosteriorFunction() const
861861

862862
#ifdef DOLATER // (not clear why this does not work)
863863
// need to make in this case a likelihood from the nll and make the product with the prior
864-
TString likeName = TString("likelihood_times_prior_") + TString(fPriorPdf->GetName());
865-
TString formula;
866-
formula.Form("exp(-@0+%f+log(@1))",fNLLMin);
867-
fLikelihood = new RooFormulaVar(likeName,formula,RooArgList(*fLogLike,*fPriorPdf));
864+
std::string likeName = std::string{"likelihood_times_prior_"} + fPriorPdf->GetName();
865+
std::stringstream formula;
866+
formula << "std::exp(-@0+" << fNllMin << "+log(@1))";
867+
fLikelihood = new RooFormulaVar(likeName.c_str(), formula, RooArgList(*fLogLike, *fPriorPdf));
868868
#else
869869
// here use RooProdPdf (not very nice) but working
870870

@@ -877,9 +877,9 @@ RooAbsReal* BayesianCalculator::GetPosteriorFunction() const
877877
// // create a unique name for the product pdf
878878
RooAbsPdf * pdfAndPrior = fPdf;
879879
if (fPriorPdf) {
880-
TString prodName = TString("product_") + TString(fPdf->GetName()) + TString("_") + TString(fPriorPdf->GetName() );
880+
std::string prodName = std::string{"product_"} + fPdf->GetName() + "_" + fPriorPdf->GetName();
881881
// save this as data member since it needs to be deleted afterwards
882-
fProductPdf = new RooProdPdf(prodName,"",RooArgList(*fPdf,*fPriorPdf));
882+
fProductPdf = new RooProdPdf(prodName.c_str(), "", RooArgList(*fPdf, *fPriorPdf));
883883
pdfAndPrior = fProductPdf;
884884
}
885885

@@ -888,10 +888,10 @@ RooAbsReal* BayesianCalculator::GetPosteriorFunction() const
888888
RemoveConstantParameters(&*constrParams);
889889
fLogLike = std::unique_ptr<RooAbsReal>{pdfAndPrior->createNLL(*fData, RooFit::Constrain(*constrParams),RooFit::ConditionalObservables(fConditionalObs),RooFit::GlobalObservables(fGlobalObs) )};
890890

891-
TString likeName = TString("likelihood_times_prior_") + TString(pdfAndPrior->GetName());
892-
TString formula;
893-
formula.Form("exp(-@0+%f)",fNLLMin);
894-
fLikelihood = new RooFormulaVar(likeName,formula,RooArgList(*fLogLike));
891+
std::string likeName = std::string{"likelihood_times_prior_"} + pdfAndPrior->GetName();
892+
std::stringstream formula;
893+
formula << "exp(-@0+" << fNLLMin << ")";
894+
fLikelihood = new RooFormulaVar(likeName.c_str(), formula.str().c_str(), RooArgList(*fLogLike));
895895
#endif
896896

897897

0 commit comments

Comments
 (0)