Home | News | Documentation | Download

Cout string value from a TTree branch

Hi ,

I am looping trough a TTree and trying to cout the values of a particular branch text, which contains string values. I am running the code as macro in ROOT. However I get nothing when the code runs, it just output blank lines (Everything worked fine including printing float values).

this is the code that does the thing:

string        text;

Tree->SetBranchAddress("text", &text);

for(int i=0; i <1000; i++){
	InTree->GetEntry(i);
	cout << text << endl;
}

Can somebody helps to point out the problem in my code?
Thanks in advance

_ROOT Version:6.24.00 (conda-forge channel, conda = 4.8.3)
_Platform:_Linux 3.10.0-1127.el7.x86_64
_Compiler:g++ (crosstool-NG 1.24.0.133_b0863d8_dirty) 9.3.0 (comes with conda)

Attach the output from: Tree->Print();

Try:

std::string *muDecayVolume = 0;
Tree->SetBranchAddress("muDecayVolume", &muDecayVolume);

for(Long_64 i = 0; i < 1000; i++) {
  Tree->GetEntry(i);
  cout << *muDecayVolume << endl;
}

Tree->ResetBranchAddresses(); // disconnect from local variables
delete muDecayVolume; // cleanup

Thank you it worked !
Can you explain what is the reson behind this to me?

Probably an “object of some class” (e.g., a “std::string”) versus an “ordinary C++ data type” (e.g., a “double”).

thanks !