Hello,
I try to get a string written directly in my tree using TTree::ReadFile.
Here is my code:
Int_t runnb;
string *str = new string();
if( ! tree.ReadFile("input.txt", "runnb/I:criteria/C", '\t') ) { // Find the good flag for String ........
cerr << "[Error] Failed to read the txtfile. Is it correctly formatted ?" << endl;
return 1;
}
tree.SetBranchAddress("runnb", &runnb);
tree.SetBranchAddress("criteria", str);
return 1;
My input is the following :
263600 var1=2 && var2>1 && var2<4
263600 var1=1 && var2>1 && var2<5
263600 var1=4 && var2>1 && var2<3
Using the following code I get this error message:
I looked at this thread : TTree::ReadFile & strings
I think I am maybe doing something wrong ?
Thank you !
[code]{
TTree *tree = new TTree(“tree”, “tree”);
if( ! tree->ReadFile(“input.txt”, “runnb/I:criteria/C”, ‘\t’) ) {
std::cerr << “[Error] Failed to read the txtfile.” << std::endl;
return 1;
}
Int_t runnb = -1;
char str[256] = “”;
tree->SetBranchAddress(“runnb”, &runnb);
tree->SetBranchAddress(“criteria”, str);
for (Long64_t i = 0; i < tree->GetEntries(); i++) {
tree->GetEntry(i);
std::cout << i << " : " << runnb << " : " << str << " :" << std::endl;
}
tree->ResetBranchAddresses(); // detach from local variables
return 1;
}[/code]
0 : 263600 : var1=2 && var2>1 && var2<4 :
1 : 263600 : var1=1 && var2>1 && var2<5 :
2 : 263600 : var1=4 && var2>1 && var2<3 :
input.txt (102 Bytes)
meyerma
October 10, 2016, 10:00am
3
I tried aswell, but copy/paste your code is not working… I don’t know why.
Looking at each element of the array, the str variable seems empty.
I even tried to remove \t and use the default delimiter, but same result…
See my previous post again.
meyerma
October 10, 2016, 10:19am
5
Sorry I didn’t see the second post…
So finaly I tried to loop after the ResetBranchAddress… of course… !
I understood it was a command to detach the tree from the file and keep the content in memory…
Anyway, that’s working perfectly now
Thank you Pepe