# Filling mixed typed TTree with Python

**URL:** <https://root-forum.cern.ch/t/filling-mixed-typed-ttree-with-python/34415>\
**Category:** ROOT\
**Created:** [June 4, 2019, 2:46pm UTC](https://root-forum.cern.ch/t/filling-mixed-typed-ttree-with-python/34415 "2019-06-04T14:46:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![denvercoder9](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/b782af/32.png) [@denvercoder9](https://root-forum.cern.ch/u/denvercoder9)\
**Post date:** [June 4, 2019, 2:47pm UTC](https://root-forum.cern.ch/t/filling-mixed-typed-ttree-with-python/34415/1 "2019-06-04T14:47:00Z")

</div>

Hi,

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](https://root.cern.ch/how/how-write-ttree-python) and below is my attempt for different data types (`Int_t` and `Double_t`):

```auto
#!/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

* * *

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [June 5, 2019, 9:03am UTC](https://root-forum.cern.ch/t/filling-mixed-typed-ttree-with-python/34415/2 "2019-06-05T09:03:28Z")

</div>

Hi,

I can reproduce the same behaviour also in C++:

```cpp
{

struct MyStruct { Int_t a; Double_t b; };

TFile f("testcpp.root", "recreate");
TTree t("t", "t");

MyStruct mystruct;
t.Branch("mystruct", &mystruct, "a/I:b/D");

mystruct.a = 1;
mystruct.b = 2.;
t.Fill();

f.Write();
f.Close();

}

```

If instead of a double I use a float for `b`, the value is stored correctly.

@pcanal do you know what we are missing?

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [June 5, 2019, 10:47am UTC](https://root-forum.cern.ch/t/filling-mixed-typed-ttree-with-python/34415/3 "2019-06-05T10:47:13Z")

</div>

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.:

```auto
struct MyStruct {
   Double_t b;
   Int_t a;
};

```

---

<div class="post-metadata">

**Author:** ![denvercoder9](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/b782af/32.png) [@denvercoder9](https://root-forum.cern.ch/u/denvercoder9)\
**Post date:** [June 5, 2019, 10:51am UTC](https://root-forum.cern.ch/t/filling-mixed-typed-ttree-with-python/34415/4 "2019-06-05T10:51:33Z")

</div>

Ok. Thanks for your help. So I guess, it is not a good idea to mix more than two different data types in one struct.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [June 5, 2019, 10:55am UTC](https://root-forum.cern.ch/t/filling-mixed-typed-ttree-with-python/34415/5 "2019-06-05T10:55:56Z")

</div>

I stopped using objects/structures inside TTrees because of this 🙂 but they can be used, just being mindful of the order and sizes.

---

<div class="post-metadata">

**Author:** ![binet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/binet/32/11089_2.png) [@binet](https://root-forum.cern.ch/u/binet)\
**Post date:** [June 5, 2019, 11:07am UTC](https://root-forum.cern.ch/t/filling-mixed-typed-ttree-with-python/34415/6 "2019-06-05T11:07:39Z")

</div>

You can take padding into account and add dummy `char[xx]` fields so everything is properly aligned. (But beware of 32b/64b)

---

<div class="post-metadata">

**Author:** ![binet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/binet/32/11089_2.png) [@binet](https://root-forum.cern.ch/u/binet)\
**Post date:** [June 5, 2019, 11:38am UTC](https://root-forum.cern.ch/t/filling-mixed-typed-ttree-with-python/34415/7 "2019-06-05T11:38:36Z")

</div>

Or use ` __attribute__ ((packed))` (but that isn’t standardized, afaik)

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [June 5, 2019, 1:58pm UTC](https://root-forum.cern.ch/t/filling-mixed-typed-ttree-with-python/34415/8 "2019-06-05T13:58:17Z")

</div>

Or simply sort from the ‘largest’ to the ‘smallest’ data type (i.e. double first, then 64 bits longs, and float and 32 bits longs, etc …)

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [June 19, 2019, 2:02pm UTC](https://root-forum.cern.ch/t/filling-mixed-typed-ttree-with-python/34415/9 "2019-06-19T14:02:12Z")

</div>

Closed on June 19, 2019, 2:02pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
