Fill branch with class struct including string


I want to write (and eventually read) a tree. It has always worked smoothly for me, until I tried filling some string variables…

So the class is:

[code]class EdepClass

Int_t parent_ID, track_ID, step_No;
Float_t x_edep, y_edep, z_edep, prestep_Ekin, poststep_Ekin, edep;
std::string particle_Name;

} EnergyDep;[/code]

Then I create the ROOT file, tree etc:

rootfile = new TFile("Output.root","RECREATE"); atree = new TTree("EdepTree", "A Root tree"); atree->Branch("EdepBranch",&EnergyDep,"parent_ID/I:track_ID/I:step_No/I:x_edep/F:y_edep/F:z_edep/F:prestep_Ekin/F:poststep_Ekin/F:edep/F:particle_Name/C");

The leaf with the string although it get the correct number of entries, it doesn’t get the correct values.

There is an example code in this post:

[ … ing#p33267](TTree with string

but this deals with the case of having only one leaf in the branch.

Any help on thet is highly welcomed,

I think using a single branch of a struct or class with data members in the way you describe will only work with “plain old data” types like ints, doubles, and chars. The “/C” code is for C-style strings, not for std::strings. So if you change your std::string into a C-style char[], I think that would work.

People often have issues with storing std:: objects in TTrees. In the case of a string, there is a convenient TObjString object in ROOT that is more easily used. I don’t know offhand how to put it in a struct or class with other members for a single branch.