# TTreeReaderArray with enum type

**URL:** <https://root-forum.cern.ch/t/ttreereaderarray-with-enum-type/54283>\
**Category:** Newbie\
**Tags:** tree, root\
**Created:** [March 29, 2023, 1:48pm UTC](https://root-forum.cern.ch/t/ttreereaderarray-with-enum-type/54283 "2023-03-29T13:48:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbalogh](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mbalogh/32/12666_2.png) [@mbalogh](https://root-forum.cern.ch/u/mbalogh)\
**Post date:** [March 29, 2023, 1:48pm UTC](https://root-forum.cern.ch/t/ttreereaderarray-with-enum-type/54283/1 "2023-03-29T13:48:05Z")

</div>

Dear experts,  
I have a TTree, that has a branch containing vector of structures, with structure containing enum type called `someEnum`. I am trying to read it with selector in maybe a bit unconventional way using the `TTreeReaderArray<someEnum>` (which works for basic types), but i keep getting following error:

```auto
Error in <TTreeReaderArrayBase::CreateProxy()>: The template argument type T of T accessing branch test_struct.e1 (which contains data of type Int_t) is not known to ROOT. You will need to create a dictionary for it.

```

I created a minimal example that mimics the way I am trying to use it, [you can find it on github](https://github.com/matLogh/root6_enum). Please correct me if I am wrong, but I believe that I am producing the dictionary for the enum types, which is why I am able to store these objects in a tree in a first place.

I reproduced this issue also when branch contains `vector<someEnum>` and I am trying to read it with the `TTreeReaderArray<someEnum>`.

(I also don’t quite understand why enum objects are stored as `Int_t` in a tree even when I ask them to be `unsigned int`, but I assume that decision is up to the compiler.)

Thank you for your time 🙂

* * *

_ROOT Version:_ 6.26/10  
_Platform:_ Ubuntu under WSL2  
_Compiler:_ g++

* * *

---

<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:** [March 29, 2023, 7:48pm UTC](https://root-forum.cern.ch/t/ttreereaderarray-with-enum-type/54283/2 "2023-03-29T19:48:16Z")

</div>

> [@mbalogh](#):
>
> I also don’t quite understand why enum objects are stored as `Int_t` in a tree even when I ask them to be `unsigned int`

The implementation for storing enums in ROOT files was done many years before the C++ standard introduce the ability to select the underlying storage. We have not yet ( ☹ ) update the code to properly handle this features (So all enums are stored as `int` no matter what).

---

<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:** [March 29, 2023, 7:50pm UTC](https://root-forum.cern.ch/t/ttreereaderarray-with-enum-type/54283/3 "2023-03-29T19:50:04Z")

</div>

Try using:

```auto
#pragma link C++ enum someEnum;
#pragma link C++ enum anotherEnum;

```

in your linkdef file.

---

<div class="post-metadata">

**Author:** ![mbalogh](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mbalogh/32/12666_2.png) [@mbalogh](https://root-forum.cern.ch/u/mbalogh)\
**Post date:** [March 29, 2023, 10:32pm UTC](https://root-forum.cern.ch/t/ttreereaderarray-with-enum-type/54283/4 "2023-03-29T22:32:46Z")

</div>

Actually I have those lines in my LinkDef.h…  
To explain the weird logic in provided example, I have two executables:

- one that generates the root file (in real-life application its GEANT)
- other is the selector that does the cherry picking of the data

The structures, enums etc., that are stored in the trees/files are treated as separate module with their own LinkDef file, so that then they can be later just linked to the executables, or just loaded as libraries if one needs to check the root files manually.  
In the provided example, the separate “module” is just the `enum.hh` file which has its own `LinkDef.h`. (I just push new version that mirror this intention, `enum.hh` and `LinkDef.h` are in separate directory with their own `CMakeLists.txt`.)

The selector executable, the one that fails, is linked to the shared library generated by the `enum.hh`, so it has access to the dictionary of the enums. Since it inherits from `TSelector`, it has its own `SelectorLinkDef.h` and indeed, if I add the pragmas you suggested to linkdef, it will complain that they are already known.

I apologize for convoluted example, but I wanted to reproduce the problem as I am experiencing it in my real application, because I am convinced that it is my bug, hidden in the linkdefs. Other possibility might be, that there is an issue with the “data\_structures” library is somehow being loaded too late to provide function template for the `TTreeReaderArray`, but I admit I know nothing about compilers and linkers…

---

<div class="post-metadata">

**Author:** ![mbalogh](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mbalogh/32/12666_2.png) [@mbalogh](https://root-forum.cern.ch/u/mbalogh)\
**Post date:** [March 29, 2023, 10:37pm UTC](https://root-forum.cern.ch/t/ttreereaderarray-with-enum-type/54283/5 "2023-03-29T22:37:26Z")

</div>

> The implementation for storing enums in ROOT files was done many years before the C++ standard introduce the ability to select the underlying storage. We have not yet ( ☹ ) update the code to properly handle this features (So all enums are stored as `int` no matter what).

I intent to use those enums in bit-wise manner anyway, so storage type does not really matter to me. It was just a curiosity.

---

<div class="post-metadata">

**Author:** ![mbalogh](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mbalogh/32/12666_2.png) [@mbalogh](https://root-forum.cern.ch/u/mbalogh)\
**Post date:** [April 3, 2023, 11:21am UTC](https://root-forum.cern.ch/t/ttreereaderarray-with-enum-type/54283/6 "2023-04-03T11:21:04Z")

</div>

@pcanal do you have any other suggestion I could try?

---

<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:** [April 13, 2023, 8:15pm UTC](https://root-forum.cern.ch/t/ttreereaderarray-with-enum-type/54283/7 "2023-04-13T20:15:58Z")

</div>

So, the issue is a missing enum support in `TTreeReader` (will be solved in next releases). To work around the issue, read `int` (or `Int_t`) instead:

```auto
@@ -43,8 +43,9 @@ public :
    TTreeReaderValue<testStruct_t> test_struct = {fReader, "test_struct"};
    TTreeReaderArray<int> test_foo = {fReader, "test_struct.foo"};
    // TTreeReaderArray<someEnum> vector_enum_someEnum = {fReader, "vector_enum"};
- TTreeReaderArray<someEnum> test_someEnum = {fReader, "test_struct.e1"};
- TTreeReaderArray<anotherEnum> test_anotherEnum = {fReader, "test_struct.e2"};
+ TTreeReaderValue<int> test_someEnum = {fReader, "test_struct.e1"};
+ TTreeReaderArray<int> test_anotherEnum = {fReader, "test_struct.e2"};

    // enum_tree() {};
    enum_tree(TTree * /*tree*/ =0) :TSelector() { };

```

Cheers,  
Philippe.

PS. valgrind pointed to another unrelated issue due to order of deletion. The fix for it is:

```auto
diff --git a/main_selector.cpp b/main_selector.cpp
index 195d634..c457003 100644
--- a/main_selector.cpp
+++ b/main_selector.cpp
@@ -7,10 +7,10 @@ int main()
 {
     testStruct_t test_struct;

- enum_tree selector(0);

     TFile file("enum_test.root","read");
+ enum_tree selector(0);
     TTree* tree = (TTree*)file.Get("enum_tree");
     tree->Process(&selector);
     return 0;

```

I.e. Since the selector does hold on to a reference to the tree and/or file, it need to be deleted before them.

---

<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:** [April 17, 2023, 2:04pm UTC](https://root-forum.cern.ch/t/ttreereaderarray-with-enum-type/54283/8 "2023-04-17T14:04:55Z")

</div>

FYI: Support for `TTreeReader(s)` of enums has been added in the development branch (and will be part of v6.30/00).

---

<div class="post-metadata">

**Author:** ![mbalogh](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mbalogh/32/12666_2.png) [@mbalogh](https://root-forum.cern.ch/u/mbalogh)\
**Post date:** [April 17, 2023, 6:19pm UTC](https://root-forum.cern.ch/t/ttreereaderarray-with-enum-type/54283/9 "2023-04-17T18:19:42Z")

</div>

Thank you @pcanal !
