Skip to content

Commit 7195dcc

Browse files
committed
[RF] Replace some usage of Form with standard C++
1 parent 9c5289f commit 7195dcc

File tree

5 files changed

+39
-26
lines changed

5 files changed

+39
-26
lines changed

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: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,18 @@ 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()) ;
60+
std::string m1name = std::string{GetName()} + "_moment1";
6161
_mean.putOwnedArg(std::unique_ptr<RooAbsMoment>{func.mean(x)}) ;
6262

63-
string pname=Form("%s_product",name) ;
63+
std::string pname = std::string{name} + "_product";
6464
_xfOffset = _mean->getVal() ;
65-
XF = std::make_unique<RooFormulaVar>(pname.c_str(),Form("pow((@0-%f),2)*@1",_xfOffset),RooArgList(x,func)) ;
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) ;
71+
std::string pname = std::string{name} + "_product";
7072
XF = std::make_unique<RooProduct>(pname.c_str(),pname.c_str(),RooArgList(x,x,func)) ;
7173
}
7274

@@ -106,17 +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()) ;
111+
std::string m1name = std::string{GetName()} + "_moment1";
110112
_mean.putOwnedArg(std::unique_ptr<RooAbsMoment>{func.mean(x,nset)}) ;
111113

112-
string pname=Form("%s_product",name) ;
114+
std::string pname = std::string{name} + "_product";
113115
_xfOffset = _mean->getVal() ;
114-
XF = std::make_unique<RooFormulaVar>(pname.c_str(),Form("pow((@0-%f),2)*@1",_xfOffset),RooArgList(x,func)) ;
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)) ;
115119

116120

117121
} else {
118122

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

122126
}

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)