Saving Strings to ROOT Files

I am having some issues saving and reading strings from ROOT files.

I am trying to save using the following code which compiles and runs without complaint.

string data;
PostSel->Branch("data",&data,"data/C");

//some code here

data="zgamma";

However, when I try to read using the following, I get a seg fault

[code]

string data;
chain->SetBranchAddress(“data”,&data);

//some code here to get entry, etc

cout<<data<<endl;

[/code][/code]

Hi,

a std::string is not a char*, which is what you “promise” to pass by defining the branch as “/C”. Simply do char* data = new char[1024] when filling, and when reading define it as char* data = 0; Or, better yet, create the branch with a TString, using the object pointer overload of TTree::Branch(), i.e. TString* pdata = &data; PostSel->Branch(“data”, &p_data).

Cheers, Axel.