Output .ls contents to a text file

I want to output a list of contents of a root file into txt. file.
I tried

  TFile* f_in = new TFile(sample.root);
  TFile fout("datanames.txt", "recreate");
  fout.cd();
  f_in->ls();
  fout.Close();

but this code outputs the contents only into the root command line.
How should I do?

Hi,
in ROOT6 you can do the following:

root [0]      TFile* f_in = new TFile("sample.root");
root [1]      .> mylog.txt
f_in->ls()
.>
root [4]      .q

Now the output of f_in->ls() is in the mylog.txt.

2 Likes

It works! Thank you very much for your reply!:grin:

In that particular case you can also use the ROOT command line rootls at the shell prompt:

$ rootls sample.root > mylog.txt
1 Like

Thank you very much for your reply!
It’s so helpful:grin:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.