Problem with double in TBranch


I have the following problem when filling a branch with a class. To make it clear, I am using root 5.34.11 and the actual code is a Geant4 application.

In the header of my code, I create the class:

class SecondariesClass

Int_t GammaNum_S, particle_PDG_S, parent_ID_S, track_ID_S, stepNo_S;
Double_t xPre_S, yPre_S, zPre_S;

} Secondaries;[/code]

Then in the implementation, I create the tree and the branch:

sec_Tree = new TTree("SecondariesTree", "Secondaries ROOT tree"); sec_Tree->Branch("SecondariesBranch",&Secondaries,"GammaNum_S/I:particle_PDG_S/I:parent_ID_S/I:track_ID_S/I:stepNo_S/I:xPre_S/D:yPre_S/D:zPre_S/D");

Finally I fill the branch:

Secondaries.GammaNum_S = GammaNum; Secondaries.particle_PDG_S = pdgID; Secondaries.parent_ID_S = ParentID; Secondaries.track_ID_S = TrackID; Secondaries.stepNo_S = StepNo; Secondaries.xPre_S = xPre/cm; Secondaries.yPre_S = yPre/cm; Secondaries.zPre_S = zPre/cm

Then when I Scan the tree I get completely meaningless values for the doubles:

3.35e+216, 2.904e-21, -2.3e-185

instead of -2.42113, 0.901501, 5.4

The xPre, yPre, zPre variables are G4double. Is there any incompatibility? I have also tried to bypass that by filling the variables with hardcoded number, like 5.4 . Again no result. Using Float_t in the SecondariesClass and /F in the branch is less problematic, but not quite yet ok. For a hardcoded 5.4 the Scan will tell me 5.40001.

Any idea?


PS: When I remove the integer variables completely, the doubles are correct…

It’s probably a class/struct alignment/padding problem (search for “padding” in the TTree class description and in the “Limitation of CINT” web page, if you like).
Try to keep your variables sorted in the decreasing order of size:

class SecondariesClass {public: Double_t xPre_S, yPre_S, zPre_S; Int_t GammaNum_S, particle_PDG_S, parent_ID_S, track_ID_S, stepNo_S; } Secondaries;
P.S. “float” numbers keep 6 to 9 significant decimal digits precision, “double” values keep 15 to 17 significant decimal digits precision.

Works like charm!

Thanks for the fast and efficient support.