Skip to content

Commit fb1eaee

Browse files
committed
[math] Fix warnings related to snprintf
Fix warnigs related to `snprintf`, like for example in TSplot.cxx: ```txt /home/rembserj/code/root/math/splot/src/TSPlot.cxx: In member function ‘void TSPlot::FillXvarHists(Int_t)’: /home/rembserj/code/root/math/splot/src/TSPlot.cxx:535:39: error: ‘__builtin___snprintf_chk’ output may be truncated before the last format character [-Werror=format-truncation=] 535 | snprintf(name,sizeof(name), "x%d", i); | ```
1 parent 36d550c commit fb1eaee

File tree

3 files changed

+30
-35
lines changed

3 files changed

+30
-35
lines changed

math/minuit/src/TLinearFitter.cxx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
#include "TBuffer.h"
2525
#include "TROOT.h"
2626
#include "strlcpy.h"
27-
#include "snprintf.h"
27+
28+
#include <string>
2829

2930
ClassImp(TLinearFitter);
3031

@@ -1564,12 +1565,10 @@ void TLinearFitter::SetFormula(const char *formula)
15641565
fFunctions.Expand(fNfunctions);
15651566

15661567
//check if the old notation of xi is used somewhere instead of x[i]
1567-
char pattern[12];
1568-
char replacement[14];
15691568
for (i=0; i<fNdim; i++){
1570-
snprintf(pattern,sizeof(pattern), "x%d", i);
1571-
snprintf(replacement,sizeof(replacement), "x[%d]", i);
1572-
sstring = sstring.ReplaceAll(pattern, Int_t(i/10)+2, replacement, Int_t(i/10)+4);
1569+
std::string pattern = "x" + std::to_string(i);
1570+
std::string replacement = "x[" + std::to_string(i) + "]";
1571+
sstring = sstring.ReplaceAll(pattern.c_str(), replacement.c_str());
15731572
}
15741573

15751574
//fill the array of functions

math/splot/inc/TSPlot.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ class TSPlot: public TObject {
3232
TObjArray fYpdfHists; //histograms of pdfs
3333
TObjArray fSWeightsHists; //histograms of weighted variables
3434

35-
TTree *fTree; //!
36-
TString* fTreename; //The name of the data tree
37-
TString* fVarexp; //Variables used for splot
38-
TString* fSelection; //Selection on the tree
35+
TTree *fTree = nullptr; //!
36+
TString* fTreename = nullptr; //The name of the data tree
37+
TString* fVarexp = nullptr; //Variables used for splot
38+
TString* fSelection = nullptr; //Selection on the tree
3939

4040

4141
Int_t fNx; //Number of control variables

math/splot/src/TSPlot.cxx

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#include "TMath.h"
2020
#include "snprintf.h"
2121

22+
#include <sstream>
23+
#include <string>
24+
2225
extern void Yields(Int_t &, Double_t *, Double_t &f, Double_t *x, Int_t iflag);
2326

2427
ClassImp(TSPlot);
@@ -286,11 +289,7 @@ The results above can be obtained by running the tutorial TestSPlot.C
286289
////////////////////////////////////////////////////////////////////////////////
287290
/// default constructor (used by I/O only)
288291

289-
TSPlot::TSPlot() :
290-
fTree(nullptr),
291-
fTreename(nullptr),
292-
fVarexp(nullptr),
293-
fSelection(nullptr)
292+
TSPlot::TSPlot()
294293
{
295294
fNx = 0;
296295
fNy=0;
@@ -307,11 +306,7 @@ TSPlot::TSPlot() :
307306
/// - ns : number of species
308307
/// - tree: input data
309308

310-
TSPlot::TSPlot(Int_t nx, Int_t ny, Int_t ne, Int_t ns, TTree *tree) :
311-
fTreename(nullptr),
312-
fVarexp(nullptr),
313-
fSelection(nullptr)
314-
309+
TSPlot::TSPlot(Int_t nx, Int_t ny, Int_t ne, Int_t ns, TTree *tree)
315310
{
316311
fNx = nx;
317312
fNy=ny;
@@ -530,10 +525,9 @@ void TSPlot::FillXvarHists(Int_t nbins)
530525
}
531526

532527
//make the histograms
533-
char name[12];
534528
for (i=0; i<fNx; i++){
535-
snprintf(name,sizeof(name), "x%d", i);
536-
TH1D *h = new TH1D(name, name, nbins, fMinmax(0, i), fMinmax(1, i));
529+
std::string name = "x" + std::to_string(i);
530+
TH1D *h = new TH1D(name.c_str(), name.c_str(), nbins, fMinmax(0, i), fMinmax(1, i));
537531
for (j=0; j<fNevents; j++)
538532
h->Fill(fXvar(j, i));
539533
fXvarHists.Add(h);
@@ -587,10 +581,9 @@ void TSPlot::FillYvarHists(Int_t nbins)
587581
}
588582

589583
//make the histograms
590-
char name[12];
591584
for (i=0; i<fNy; i++){
592-
snprintf(name,sizeof(name), "y%d", i);
593-
TH1D *h=new TH1D(name, name, nbins, fMinmax(0, fNx+i), fMinmax(1, fNx+i));
585+
std::string name = "y" + std::to_string(i);
586+
TH1D *h=new TH1D(name.c_str(), name.c_str(), nbins, fMinmax(0, fNx+i), fMinmax(1, fNx+i));
594587
for (j=0; j<fNevents; j++)
595588
h->Fill(fYvar(j, i));
596589
fYvarHists.Add(h);
@@ -639,12 +632,13 @@ void TSPlot::FillYpdfHists(Int_t nbins)
639632
return;
640633
}
641634

642-
char name[34];
643635
for (ispecies=0; ispecies<fNSpecies; ispecies++){
644636
for (i=0; i<fNy; i++){
645-
snprintf(name,sizeof(name), "pdf_species%d_y%d", ispecies, i);
637+
std::stringstream nameStream;
638+
nameStream << "pdf_species" << ispecies << "_y" << i;
639+
std::string name = nameStream.str();
646640
//TH1D *h = new TH1D(name, name, nbins, ypdfmin[ispecies*fNy+i], ypdfmax[ispecies*fNy+i]);
647-
TH1D *h = new TH1D(name, name, nbins, fMinmax(0, fNx+fNy+ispecies*fNy+i), fMinmax(1, fNx+fNy+ispecies*fNy+i));
641+
TH1D *h = new TH1D(name.c_str(), name.c_str(), nbins, fMinmax(0, fNx+fNy+ispecies*fNy+i), fMinmax(1, fNx+fNy+ispecies*fNy+i));
648642
for (j=0; j<fNevents; j++)
649643
h->Fill(fYpdf(j, ispecies*fNy+i));
650644
fYpdfHists.Add(h);
@@ -702,13 +696,13 @@ void TSPlot::FillSWeightsHists(Int_t nbins)
702696
return;
703697
}
704698

705-
char name[30];
706-
707699
//Fill histograms of x-variables weighted with sWeights
708700
for (Int_t ivar=0; ivar<fNx; ivar++){
709701
for (Int_t ispecies=0; ispecies<fNSpecies; ispecies++){
710-
snprintf(name,30, "x%d_species%d", ivar, ispecies);
711-
TH1D *h = new TH1D(name, name, nbins, fMinmax(0, ivar), fMinmax(1, ivar));
702+
std::stringstream nameStream;
703+
nameStream << "x" << ivar << "_species" << ispecies;
704+
std::string name = nameStream.str();
705+
TH1D *h = new TH1D(name.c_str(), name.c_str(), nbins, fMinmax(0, ivar), fMinmax(1, ivar));
712706
h->Sumw2();
713707
for (Int_t ievent=0; ievent<fNevents; ievent++)
714708
h->Fill(fXvar(ievent, ivar), fSWeights(ievent, ispecies));
@@ -719,8 +713,10 @@ void TSPlot::FillSWeightsHists(Int_t nbins)
719713
//Fill histograms of y-variables (excluded from the fit), weighted with sWeights
720714
for (Int_t iexcl=0; iexcl<fNy; iexcl++){
721715
for(Int_t ispecies=0; ispecies<fNSpecies; ispecies++){
722-
snprintf(name,30, "y%d_species%d", iexcl, ispecies);
723-
TH1D *h = new TH1D(name, name, nbins, fMinmax(0, fNx+iexcl), fMinmax(1, fNx+iexcl));
716+
std::stringstream nameStream;
717+
nameStream << "y" << iexcl << "_species" << ispecies;
718+
std::string name = nameStream.str();
719+
TH1D *h = new TH1D(name.c_str(), name.c_str(), nbins, fMinmax(0, fNx+iexcl), fMinmax(1, fNx+iexcl));
724720
h->Sumw2();
725721
for (Int_t ievent=0; ievent<fNevents; ievent++)
726722
h->Fill(fYvar(ievent, iexcl), fSWeights(ievent, fNSpecies*(iexcl+1)+ispecies));

0 commit comments

Comments
 (0)