How to convert a string to a file name?

Hi I am asking this because I want to change the directory flexibly. (Looping through the .root files in different mass100, mass200 directory. ) I wonder if there is a way to convert a string to a file name?


string file ="tag_1_pythia_lhe_events.root";
TFile *f = new TFile(file);

returns the error of

Error: Can’t call TFile::TFile(file) in current scope Partoncut.c:32:
Possible candidates are…
(in TFile)
/usr/local/Cellar/root/5.34.26/lib/root/ -1:-1 0 private: TFile TFile::TFile(const TFile&); //Files cannot be copied
/usr/local/Cellar/root/5.34.26/lib/root/ -1:-1 0 public: TFile TFile::TFile(void);
/usr/local/Cellar/root/5.34.26/lib/root/ -1:-1 0 public: TFile TFile::TFile(const char* fname,Option_t* option="",const char* ftitle="",Int_t compress=1);
(in TDirectoryFile)
*** Interpreter error recovered ***

Thanks in advance.


Hi thanks for the input.
I think the function attached seems to change the filename into a string though? I think I would need the opposite.
because when you look at my example…when I call a string in TFile, it creates error.


Pass file.c_str() to the TFile constructor. It returns the const char* of a std::string, exactly what you need.

Cheers, Axel.