I want to fill a TTree with mixed typed values using Python. I followed this tutorial: https://root.cern.ch/how/how-write-ttree-python and below is my attempt for different data types (Int_t and Double_t):
#!/usr/bin/env python3
from ROOT import TFile, TTree
from ROOT import gROOT
gROOT.ProcessLine(r'''
struct MyStruct {
Int_t a;
Double_t b;
};
''');
from ROOT import MyStruct
f = TFile('test.root', 'recreate')
t = TTree('t', 't')
mystruct = MyStruct()
t.Branch('mystruct', mystruct, 'a/I:b/D')
mystruct.a = 1
mystruct.b = 2.
t.Fill()
f.Write()
f.Close()
The resulting TTree has valid entries for a, but not for b, i.e. in this case b is zero (instead of 2), but I also recognized other (invalid) values when filling entries in a loop. When I use the same data type for both, a and b (similar to the case in the tutorial) everything works fine.
Can you help me to fix this issue?
Best regards.
ROOT Version: 6.16/00 Platform: Not Provided Compiler: Not Provided
I think it has to do with ROOT expecting contiguous memory for the consecutive members of a struct or object. An odd number of integers leaves a āgapā before the double, which breaks ROOT. So either ensure you have an even number of ints before a double, or put the integers after the doubles, e.g.: