How to write in a text file


I have been using something like

[code] tr_x_sec = new TTree(“CrossX”,“CrossX info”);

Double_t x_sec;

in order to read from a text file

How can I write some string / values to a text file and save it as well?


try, for example, “ofstream”:
Examples: create/open/close a file: and/or and/or
Example: write data:

Unless what you wanted was just something like:

root […] tr_x_sec->SetScanField(0);
root […] tr_x_sec->Scan("*"); > tr_x_sec.out.txt

See: and for details.

However, I don’t know if ROOT is able to parse such a text output afterwards, for example in order to ‘tr_x_sec->ReadFile(“tr_x_sec.out.txt”)’. You might need to edit this file manually. If you have just one “text row” per “tree entry”, remove the first column (called “Row”) and get rid of all “*” characters that are present inside (and possibly modify/add the first line which can keep all branch descriptions - see for details).
An expert’s advice is needed here.

I am stupid. No?
Pepe Le Pew.


You can also redirect the output of Scan as follow:

// Read and dump the tree entries 
t->GetPlayer()->SetScanRedirect(kTRUE); // You may have to cast t.GetPlayer() to a TTreePlayer*


1 Like

The method:


produces the same output text file format as the one in my previous post here.

The question is … can ROOT read it back?
If not, is there any other simple method to “dump” a TTree into an ascii file and read it back later (let’s assume that the TTree is “simple enough”, whatever this “requirement” may mean in the context here)?

No. short of writing your own MakeClass/MakeSelector/MakeProxy or TTree::Draw script listing explicitly the variable and the format.


where can I find this .txt file then…??? in the root session, I do not have this file…

where an I find this .txt file. In the root session, this file is absent…??

root [0] ((TTreePlayer*)(ntuple->GetPlayer()))->SetScanRedirect(kTRUE);
root [1] ((TTreePlayer*)(ntuple->GetPlayer()))->SetScanFileName("output.txt");
root [2] ntuple->Scan();
File <output.txt> created
root [3] .! ls -l output.txt
-rw-r--r--  1 couet  staff  1825292 Sep  3 13:22 output.txt
root [4] 
1 Like

Thankyou Dear couet…God Bless you…!!