String branch cannot be read from TTree

Hi,

I have a root file filled with a TTree, with many branches. When I have an interactive root session and I do a print, I get the following list:

*Tree    :GmDataTTree: GmDataTTree
*Br    0 :Event_EventID : Event_EventID/I                                    *
*Br    1 :Event_InitialPosX : Event_InitialPosX/D                            *
... etc... 
*Br    5 :Event_InitialLogicalVolume : string 
... etc...

My problem is in viewing Event_InitialLogicalVolume. When I do it interactively, with “GmDataTTree->Draw("Event_InitialLogicalVolume")” there is no problem. But when I have a code like this:

	int Event_EventID;
	string Event_InitialLogicalVolume_HELLO;
	GmDataTTree->SetBranchAddress("Event_EventID", &Event_EventID);
	GmDataTTree->SetBranchAddress("Event_InitialLogicalVolume", &Event_InitialLogicalVolume_HELLO);
	GmDataTTree->GetEntry(0);

The call to GmDataTTree->SetBranchAddress("Event_InitialLogicalVolume", &Event_InitialLogicalVolume_HELLO); gives me an error:
Error in <TTree::SetBranchAddress>: The address for "Event_InitialLogicalVolume" should be the address of a pointer!
and the call to GmDataTTree->GetEntry(0); gives me a segmentation fault.

When I change the declaration to string* Event_InitialLogicalVolume_HELLO; I don’t get an error in SetBranchAddress but the call to GmDataTTree->GetEntry(0); still gives me a segmentation fault.

Maybe the way the TTree was filled was wrong (maybe it should read Event_InitialLogicalVolume : Event_InitialLogicalVolume/string instead of Event_InitialLogicalVolume : string?) but if an interactive root session can read it, I wonder if a normal C++ code (or root script) could do so too? (I checked and when I copy the C++ code in a Root script and execute the script, I get the same errors).
__

_ROOT Version: 6.18.00
_Platform: Ubuntu 18
_Compiler: g++


Hi,

This post could be of help: Reading out std::string from a TTree/TFile - #2 by Wile_E_Coyote

Best,
D

When I change the declaration to string* Event_InitialLogicalVolume_HELLO;

This must be:

string* Event_InitialLogicalVolume_HELLO = nullptr;

Thanks for your help. I missed the obvious. The solution was staring me in the face all along.
This helped:

string Event_InitialLogicalVolume;
string* Event_InitialLogicalVolume_ptr = &Event_InitialLogicalVolume;
GmDataTTree->SetBranchAddress("Event_InitialLogicalVolume", &Event_InitialLogicalVolume_ptr);

Thanks.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.