# Initializing a TBranch in PyROOT with a custom class in Python

**URL:** <https://root-forum.cern.ch/t/initializing-a-tbranch-in-pyroot-with-a-custom-class-in-python/63522>\
**Category:** ROOT\
**Tags:** python\
**Created:** [May 8, 2025, 1:48pm UTC](https://root-forum.cern.ch/t/initializing-a-tbranch-in-pyroot-with-a-custom-class-in-python/63522 "2025-05-08T13:48:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RobinS](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/d26b3c/32.png) [@RobinS](https://root-forum.cern.ch/u/RobinS)\
**Post date:** [May 8, 2025, 1:48pm UTC](https://root-forum.cern.ch/t/initializing-a-tbranch-in-pyroot-with-a-custom-class-in-python/63522/1 "2025-05-08T13:48:03Z")

</div>

Hello everyone,

I have a question about how I can make a custom class written in Python known to ROOT so that I can store an object of that class in a ROOT TBranch.

My custom Python class is a simple class that looks like this:

```auto
class Event:
    def __init___ (self):
        self.event_data = {} # Python dictionary with 36-64 keys
        self.event_num = 0 # Int
    def load_data (self, event_num, data):
        self.event_data = data
        self.event_num = event_num

```

I then try to load this event into a TBranch as below

```auto
with ROOT.TFile("test_tree.root", "RECREATE") as infile:
    tree = ROOT.TTree("Event_Tree", "Event_Tree")
    for times in range(0, 5):
        dummy_event = Event()
        dummy_event.load_data(1, {1: [1, 2, 3], 2:[5, 6, 7]})
        branch_name = "Event_obj"
        tree.Branch(branch_name, dummy_event)
        tree.Fill()
        tree.Write()
    print("Done!")

```

However, running this code gives me the error “TypeError: Template method resolution failed:  
none of the 8 overloaded methods succeeded.” I realize that my custom class “isn’t known to ROOT” as it says on the ROOT CERN TTree documentation page.

How do I make my class known to ROOT so that I can put objects of the Event class in branches of my TTree? For context, I am relatively new to C++ so I would appreciate some pointers here if there is some C++ required!

Thanks!

\_ROOT Version: 6.32.02  
\_Platform: Ubuntu 22.04.5 LTS  
\_Compiler: linux8664gcc

---

<div class="post-metadata">

**Author:** ![mdessole](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mdessole/32/34021_2.png) [@mdessole](https://root-forum.cern.ch/u/mdessole)\
**Post date:** [May 8, 2025, 3:11pm UTC](https://root-forum.cern.ch/t/initializing-a-tbranch-in-pyroot-with-a-custom-class-in-python/63522/2 "2025-05-08T15:11:12Z")

</div>

Hello @RobinS,

welcome to the ROOT Forum! You have first to generate the dictionary for your class and then you can use it from Python too. See [this example](https://root-forum.cern.ch/t/fill-branch-from-vector-of-vectors/55906/2) for a vector of vectors structure.

Cheers,  
Monica

---

<div class="post-metadata">

**Author:** ![RobinS](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/d26b3c/32.png) [@RobinS](https://root-forum.cern.ch/u/RobinS)\
**Post date:** [May 8, 2025, 4:08pm UTC](https://root-forum.cern.ch/t/initializing-a-tbranch-in-pyroot-with-a-custom-class-in-python/63522/3 "2025-05-08T16:08:46Z")

</div>

What does generating a dictionary for my class mean in this context? Do you mean that I have to write a C++ file redefining my Python class and use an std::map\<int, std::vector\>?

If so, when I’m reading my stored events back from my tree, how would I do this? Do I simply create an object of my class first and then read into it like so?

```auto
temp_event = Event()
tree.SetBranchAddress("event1", Event)

```

Will ROOT understand how to link the stored data back into my object structure?

---

<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:** [May 8, 2025, 7:03pm UTC](https://root-forum.cern.ch/t/initializing-a-tbranch-in-pyroot-with-a-custom-class-in-python/63522/4 "2025-05-08T19:03:20Z")

</div>

> Do you mean that I have to write a C++ file redefining my Python class

If you _need_ store a class in a `TTree`, you will indeed need a C++ (interpreted via Cling and gInterpreter or compiled via the generation of a dictionary).

However you might need to store class and you might be able to just store the individual component of your `Event` each in their own branch.

---

<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:** [May 22, 2025, 7:03pm UTC](https://root-forum.cern.ch/t/initializing-a-tbranch-in-pyroot-with-a-custom-class-in-python/63522/5 "2025-05-22T19:03:54Z")

</div>

Closed on May 22, 2025, 7:03pm UTC

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