How to write in a text file

Hi

I have been using something like

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

Double_t x_sec;
tr_x_sec->Branch(“x_sec”,&x_sec,“x_sec/D”);
tr_x_sec->ReadFile(“file.txt”,“x_sec”);
[/code]

in order to read from a text file

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

Thanks

VIVE L’AMOUR!
try, for example, “ofstream”: http://www.cplusplus.com/reference/iostream/ofstream
Examples: create/open/close a file: http://www.cplusplus.com/reference/iostream/ofstream/ofstream/ and/or http://www.cplusplus.com/reference/iostream/ofstream/open/ and/or http://www.cplusplus.com/reference/iostream/ofstream/close/
Example: write data: http://www.cplusplus.com/reference/iostream/ostream/operator%3C%3C/

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: http://root.cern.ch/root/html/TTree.html#TTree:Scan and http://root.cern.ch/root/html/TTreePlayer.html#TTreePlayer:Scan 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 http://root.cern.ch/root/html/TTree.html#TTree:ReadFile for details).
An expert’s advice is needed here.

I am stupid. No?
Pepe Le Pew.

Hi,

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*
t->GetPlayer()->SetScanFileName("output.txt");
t->Scan("*");

Cheers,
Philippe.

1 Like

The method:

((TTreePlayer*)(t->GetPlayer()))->SetScanRedirect(kTRUE);
((TTreePlayer*)(t->GetPlayer()))->SetScanFileName(“output.txt”);
t->Scan("*");

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.

Cheers,
Philippe.

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…!!