Re: Problem reading branches of structs

From: Rene Brun <>
Date: Wed, 27 Apr 2005 08:18:05 +0200 (MEST)


I do not understand your argument that:
"gcc keeps the struct fields aligned to 4 bytes boundary" This is impossible with your struct.
Could you replace

    tree->SetBranchAddress ("bTagInfo", &info); by

    tree->SetBranchAddress ("bTagInfo", &info.jet_E);

if you do not solve your problem, please send the shortest possible system reproduding it.

Note that we keep discouraging users to use structs instead of classes. With a class and its dictionary, you will simplify your interface when defining the branch: no need to specify the names of the data members since they are already known by the dictionary, and in addition you will get support for correct alignment of members in the class.

Rene Brun

On Tue, 26
2005, Andrea Bocci

> Hi,
> after quite some debugging of oure code, we've discovered a problem in reading a struct back from a branch.
> The structure definition is:
> struct bTagInfo {
> bTagInfo (void) {
> std::memset (this, 0, sizeof(bTagInfo));
> }
> // MonteCarlo truth (or a good approximation...)
> unsigned int jet_flavour;
> // Reconstructed informations
> double jet_E;
> double jet_ET;
> double mu_p;
> double mu_pT;
> double mu_pTrel;
> double mu_pTrelexcl;
> double mu_sip3d;
> double mu_deltaR;
> };
> A branch of these struct is crated like this:
> tree->Branch ("bTagInfo", &data, "jet_flavour/i:jet_E/D:jet_ET:mu_p:mu_pT:mu_pTrel:mu_pTrelexcl:mu_sip3d:mu_deltaR");
> and it is read back like this:
> bTagInfo info;
> if (tree->GetBranch ("bTagInfo")) {
> tree->SetBranchAddress ("bTagInfo", &info);
> }
> Now, when I read back the root file in a compiled executable, everythin works as expected, ie. the struct gets filled back correctly. Howeer, if I try to read back the struct in an interactive ROOT session, this doesn't work, and only the first field is correct.
> After some debugging, I've found out that this is due to root/cint alligning the fields in the sruct to 8-bytes boundary, while the version in the root file is obviously packed.
> The only workaround I've found is to read back the branch to a buffer, and the copying the memory from it into the struct as needed to proprly re-allign it.
> The most confusing thing has been the different behaviour between the compiled version (which runs fine, as gcc keeps the struct fields alligned to 4 bytes boundaries) and the interpreted version!
> So, what is wrong here?
> Is this the expected behaviour, and I'm reading and writing the rootfile in a wrong way, or is this a bug?
> .Andrea.
Received on Wed Apr 27 2005 - 08:18:11 MEST

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:07 MET