Dear Expert,
I extracted data from a root file and covert it to txt file in root v5 by use of the following commands:
#include <string>
#include <fstream>
using namespace std;
ofstream myfile;
myfile.open ("cst52.txt");
TFile * inputfile = TFile::Open( "cst5.root");
TTree * yourtree = (TTree*) inputfile->Get("Singles;4");
float Energy; // EDIT:you may actually want to change double to float because that is usually the branch type
yourtree->SetBranchAddress("energy", &Energy);
for (int i=0;i<yourtree->GetEntries();++i){
yourtree->GetEntry(i);
myfile << Energy << "\n";
}
Unfortunately, this code don’t work in root v6 and I have some errors as following:
fatal error: file '/usr/include/c++/5/fstream' modified since it was first
processed
root [5]
root [5] myfile.open ("cst52.txt");
input_line_55:2:3: error: use of undeclared identifier 'myfile'
(myfile.open("cst52.txt"))
.
.
input_line_871:2:3: error: use of undeclared identifier 'myfile'
(myfile << *(float*)0x7fb82c6435c0 << "\n")
^
input_line_872:2:3: error: use of undeclared identifier 'myfile'
(myfile << *(float*)0x7fb82c6435c0 << "\n")
^
input_line_873:2:3: error: use of undeclared identifier 'myfile'
(myfile << *(float*)0x7fb82c6435c0 << "\n")
Any help would be greatly appreciated.
ROOT Version: root_v6.02 Platform: Ubuntu Compiler: Not Provided
p@ubuntu:~/Desktop/root$ root tree2txt.C
------------------------------------------------------------
| Welcome to ROOT 6.02/08 http://root.cern.ch |
| (c) 1995-2014, The ROOT Team |
| Built for linuxx8664gcc |
| From tag v6-02-08, 13 April 2015 |
| Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
------------------------------------------------------------
root [0]
Processing tree2txt.C...
In file included from input_line_24:1:
/home/p/Desktop/root/tree2txt.C:8:11: error: no viable overloaded '='
myfile = fopen ("cst52.txt","w");
~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/libio.h: note: candidate function (the implicit copy assignment
operator) not viable: no known conversion from 'FILE *' (aka '_IO_FILE *')
to 'const _IO_FILE' for 1st argument; dereference the argument with *
fatal error: file '/usr/include/libio.h' modified since it was first processed
root [1]
for (int i=0; i<10; ++i) {
fprintf(myfile,"%d\n",i);
}
TFile * inputfile = TFile::Open( “cst5.root”);
TTree * yourtree = (TTree*) inputfile->Get(“Singles;4”);
float Energy; // EDIT:you may actually want to change double to float because that is usually the branch type
yourtree->SetBranchAddress(“energy”, &Energy);
for (int i=0; iGetEntries();++i) {
yourtree->GetEntry(i);
myfile << Energy << “\n”;
}
}
When i typed commands in the terminal separately, error only appear in the “for loop”:
for (int i=0; iGetEntries();++i) {
yourtree->GetEntry(i);
myfile << Energy << “\n”;
root [12] for (int i=0; iGetEntries();++i) {
root (cont’ed, cancel with .@) [13] yourtree->GetEntry(i);
root (cont’ed, cancel with .@) [14] myfile << Energy << “\n”;
root (cont’ed, cancel with .@) [15] }
ROOT_prompt_14:1:8: error: invalid operands to binary expression (‘FILE *’
(aka ‘_IO_FILE *’) and ‘float’)
myfile << Energy << “\n”;