Hi,
as in the listing, the string cant be printed normally whenever I passed a string through argv[] to label1 or label2.
but the case3 succeed. How to pass stored string or TString into Form() ?
int main( int argc, char* argv[] ){
TString label1 = Form("_%s",argv[1]); // this failed
std::string label2 = argv[1]; // this failed
TString ResultsName("");
ResultsName.Form("results/TrainingResult%s.root",label1);
std::cout << ResultsName << std::endl;
// case 3: worked
TString s("a=");
s.Form("This is %s string w/ a %d and a %f", "a", 5, 3.14);
std::cout << s << std::endl;
Please read tips for efficient and successful posting and posting code
_ROOT Version: 6.24 (PyROOT via conda)
_Platform:Centos7
_Compiler: gcc9
Are you trying to use it as a ROOT macro?
Well, âint main( int argc, char* argv[])â suggests that you are building a compiled executable.
exactly. and i have included the (sorry for skipping that part)
i am building an executable program using the code
Can you post a complete âminimal reproducerâ (so that we can try it).
#include <iostream>
#include "TChain.h"
#include "TFile.h"
#include "TTree.h"
#include "TString.h"
#include "TObjString.h"
#include "TSystem.h"
#include "TROOT.h"
#include "TMVA/Factory.h"
#include "TMVA/DataLoader.h"
#include "TMVA/Tools.h"
#include "TMVA/TMVAGui.h"
int main( int argc, char* argv[] ){
TString label1 = Form("_%s",argv[1]); // this failed
std::string label2 = argv[1]; // this failed
TString ResultsName("");
ResultsName.Form("results/TrainingResult%s.root",label1);
std::cout << ResultsName << std::endl;
// case 3: worked
TString s("a=");
s.Form("This is %s string w/ a %d and a %f", "a", 5, 3.14);
std::cout << s << std::endl;
}
FYI, i am currently working on codes involving string and TSTrings. i removed the part from TMVA training (which I adapted from the example Classification.C), now i am working to take external args into parameters that go into the TMVA training
So you can either use
TString label1 = Form("_%s",argv[1]);
[...]
ResultsName.Form("results/TrainingResult%s.root",label1.Data());
or
std::string label2 = argv[1];
[...]
ResultsName.Form("results/TrainingResult%s.root",label2.c_str());
thanks. Both worked.
Could you simply explain whatâs going on my code and the solution if possible ?
You should have got an explicit note (from the compiler) in the form:
main.cxx:23:50: warning: format â%sâ expects argument of type âchar*â, but argument 3 has type âTStringâ [-Wformat=]
So, itâs clear you need âlabel1.Data()â there.
actually my code compiled successfully. here is the make command:
g++ training.cpp -Wl,--no-as-needed -pthread -std=c++17 -m64 -I/home/user/.conda/envs/testroot/include -ggdb -L/home/user/.conda/envs/testroot/lib -lCore -lImt -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lROOTVecOps -lTree -lTreePlayer -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -lMultiProc -lROOTDataFrame -Wl,-rpath,/home/user/.conda/envs/testroot/lib -pthread -lm -ldl -rdynamic -lMLP -lMinuit -lTreePlayer -lTMVA -lTMVAGui -lXMLIO -lMLP -lm -o training.out
the problem arises when i run the training.out
Form("%s") requires a const char * argument, not a TString, nor a std::string. TString::Data() and std::string::c_str() return both a const char *