# How to properly save custom types to TTree branches in Python

**URL:** <https://root-forum.cern.ch/t/how-to-properly-save-custom-types-to-ttree-branches-in-python/55929>\
**Category:** ROOT\
**Tags:** python\
**Created:** [August 5, 2023, 4:01am UTC](https://root-forum.cern.ch/t/how-to-properly-save-custom-types-to-ttree-branches-in-python/55929 "2023-08-05T04:01:06Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jackhammer0102](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Jackhammer0102](https://root-forum.cern.ch/u/Jackhammer0102)\
**Post date:** [August 5, 2023, 4:01am UTC](https://root-forum.cern.ch/t/how-to-properly-save-custom-types-to-ttree-branches-in-python/55929/1 "2023-08-05T04:01:07Z")

</div>

Hi, ROOT experts,

I have some custom types that I want to save to TTree branches, and I have done something similar before in C++ with different custom types but I don’t know how to make it work in Python, could you please enlighten me?

```auto
    parser = argparse.ArgumentParser()
    parser.add_argument("inFileName", type=str)
    args = parser.parse_args()

    inFile = TFile(args.inFileName)

    daqstatus = ROOT.mattak.DAQStatus()
    header = ROOT.mattak.Header()
    waveforms = ROOT.mattak.Waveforms()

    inTree = inFile.Get("combined")
    inTree.SetBranchAddress("daqstatus", daqstatus)
    inTree.SetBranchAddress("header", header)
    inTree.SetBranchAddress("waveforms", waveforms)

    outFileName = 'filtered_' + args.inFileName
    outFile = TFile(outFileName, "recreate" )
    outTree = TTree("combined", "combined")
    outTree.Branch("daqstatus", daqstatus)
    outTree.Branch("header", header)
    outTree.Branch("waveforms", waveforms)

    nTotalEvents = inTree.GetEntries()
    nPassedEvents = 0
    hitAll = 4
    HF = HitFilter()

    nChannels = 24
    for i_event in range(nTotalEvents):
        inTree.GetEntry(i_event)
        wfs = []
        for i_channel in range(nChannels):
            if i_channel < 4:
                wfs.append(waveforms.makeGraph(i_channel).GetY())
            else:
                break
        nHits = HF.scanForHits(np.array(wfs))
        if nHits == hitAll:
            nPassedEvents += 1
            outTree.Fill()

    inFile.Close()
    outFile.cd()
    outTree.Write()
    outFile.Close()

```

Although this script works and the output file can be generated, but when I opened the output file I got errors:

```auto
 ***Break*** segmentation violation
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libCore.so] TUnixSystem::DispatchSignals(ESignals) (no debug info)
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)
[/Users/martin/Desktop/RNO-G/software/mattak/build/libmattak.dylib] TGraph* graphImpl<mattak::Waveforms>(mattak::Waveforms const&, int, bool) /Users/martin/Desktop/RNO-G/software/mattak/src/Waveforms.cc:80
[<unknown binary>] (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libcppyy_backend3_10.so] WrapperCall(long, unsigned long, void*, void*, void*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libcppyy_backend3_10.so] Cppyy::CallR(long, void*, unsigned long, void*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libcppyy3_10.so] CPyCppyy::(anonymous namespace)::InstancePtrExecutor::Execute(long, void*, CPyCppyy::CallContext*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libcppyy3_10.so] CPyCppyy::CPPMethod::ExecuteFast(void*, long, CPyCppyy::CallContext*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libcppyy3_10.so] CPyCppyy::CPPMethod::ExecuteProtected(void*, long, CPyCppyy::CallContext*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libcppyy3_10.so] CPyCppyy::CPPMethod::Call(CPyCppyy::CPPInstance*&, _object*, _object*, CPyCppyy::CallContext*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libcppyy3_10.so] CPyCppyy::(anonymous namespace)::mp_call(CPyCppyy::CPPOverload*, _object*, _object*) (no debug info)
[/opt/homebrew/Cellar/python@3.10/3.10.10_1/Frameworks/Python.framework/Versions/3.10/Python] _PyObject_MakeTpCall (no debug info)
[/opt/homebrew/Cellar/python@3.10/3.10.10_1/Frameworks/Python.framework/Versions/3.10/Python] call_function (no debug info)
[/opt/homebrew/Cellar/python@3.10/3.10.10_1/Frameworks/Python.framework/Versions/3.10/Python] _PyEval_EvalFrameDefault (no debug info)
[/opt/homebrew/Cellar/python@3.10/3.10.10_1/Frameworks/Python.framework/Versions/3.10/Python] _PyEval_Vector (no debug info)
[/opt/homebrew/Cellar/python@3.10/3.10.10_1/Frameworks/Python.framework/Versions/3.10/Python] PyEval_EvalCode (no debug info)
[/opt/homebrew/Cellar/python@3.10/3.10.10_1/Frameworks/Python.framework/Versions/3.10/Python] run_eval_code_obj (no debug info)
[/opt/homebrew/Cellar/python@3.10/3.10.10_1/Frameworks/Python.framework/Versions/3.10/Python] run_mod (no debug info)
[/opt/homebrew/Cellar/python@3.10/3.10.10_1/Frameworks/Python.framework/Versions/3.10/Python] pyrun_file (no debug info)
[/opt/homebrew/Cellar/python@3.10/3.10.10_1/Frameworks/Python.framework/Versions/3.10/Python] _PyRun_SimpleFileObject (no debug info)
[/opt/homebrew/Cellar/python@3.10/3.10.10_1/Frameworks/Python.framework/Versions/3.10/Python] _PyRun_AnyFileObject (no debug info)
[/opt/homebrew/Cellar/python@3.10/3.10.10_1/Frameworks/Python.framework/Versions/3.10/Python] pymain_run_file_obj (no debug info)
[/opt/homebrew/Cellar/python@3.10/3.10.10_1/Frameworks/Python.framework/Versions/3.10/Python] pymain_run_file (no debug info)
[/opt/homebrew/Cellar/python@3.10/3.10.10_1/Frameworks/Python.framework/Versions/3.10/Python] Py_RunMain (no debug info)
[/opt/homebrew/Cellar/python@3.10/3.10.10_1/Frameworks/Python.framework/Versions/3.10/Python] Py_BytesMain (no debug info)
[/usr/lib/dyld] start (no debug info)
Traceback (most recent call last):
  File "/Users/martin/Desktop/NuRadioMC/noise/applyHitFilter.py", line 49, in <module>
    wfs.append(waveforms.makeGraph(i_channel).GetY())
cppyy.ll.SegmentationViolation: TGraph* mattak::Waveforms::makeGraph(int chan, bool ns = true) =>
    SegmentationViolation: segfault in C++; program state was reset

```

* * *

_ROOT Version:_ 6.26/06  
_Python Version:_ 3.10.10

* * *

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [August 7, 2023, 1:54pm UTC](https://root-forum.cern.ch/t/how-to-properly-save-custom-types-to-ttree-branches-in-python/55929/2 "2023-08-07T13:54:58Z")

</div>

Hi,  
To store custom types in a ROOT tree you need fist to generate the dictionary and then you can also use them from Python. See for example [Fill Branch from Vector of Vectors - #2 by moneta](https://root-forum.cern.ch/t/fill-branch-from-vector-of-vectors/55906/2)

Lorenzo

---

<div class="post-metadata">

**Author:** ![Jackhammer0102](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Jackhammer0102](https://root-forum.cern.ch/u/Jackhammer0102)\
**Post date:** [August 8, 2023, 3:24am UTC](https://root-forum.cern.ch/t/how-to-properly-save-custom-types-to-ttree-branches-in-python/55929/3 "2023-08-08T03:24:53Z")

</div>

Hi, Lorenzo,

I am still running into some issues:

```auto
root [0] .L saveCustomTypesToTree.C+
Info in <TMacOSXSystem::ACLiC>: creating shared library /Users/martin/Desktop/NuRadioMC/noise/./saveCustomTypesToTree_C.so
Warning in <TClassTable::Add>: class mattak::Header already in TClassTable
Warning in <TClassTable::Add>: class mattak::VoltageCalibration already in TClassTable
Warning in <TClassTable::Add>: class mattak::Waveforms already in TClassTable
root [1] saveCustomTypesToTree()
Error in <TBranchElement::InitializeOffsets>: Could not find the real data member '__elems_[4096]' when constructing the branch 'scan_result' [Likely missing ShowMember].

 ***Break*** segmentation violation
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libTree.so] TTree::BranchImp(char const*, char const*, TClass*, void*, int, int) (no debug info)
[/Users/martin/Desktop/NuRadioMC/noise/saveCustomTypesToTree_C.so] saveCustomTypesToTree() (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libCling.so] cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libCling.so] cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libCling.so] cling::Interpreter::EvaluateInternal(std:: __1::basic_string<char, std::__ 1::char_traits<char>, std::__1::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libCling.so] cling::Interpreter::process(std:: __1::basic_string<char, std::__ 1::char_traits<char>, std::__1::allocator<char> > const&, cling::Value*, cling::Transaction**, bool) (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libCling.so] cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libCling.so] HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libCling.so] TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libRint.so] TRint::ProcessLineNr(char const*, char const*, int*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libRint.so] TRint::HandleTermInput() (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libCore.so] TUnixSystem::CheckDescriptors() (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libCore.so] TMacOSXSystem::DispatchOneEvent(bool) (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libCore.so] TSystem::InnerLoop() (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libCore.so] TSystem::Run() (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libCore.so] TApplication::Run(bool) (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/lib/root/libRint.so] TRint::Run(bool) (no debug info)
[/opt/homebrew/Cellar/root/6.26.06_2/bin/root.exe] main (no debug info)
[/usr/lib/dyld] start (no debug info)

```

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [August 8, 2023, 7:26am UTC](https://root-forum.cern.ch/t/how-to-properly-save-custom-types-to-ttree-branches-in-python/55929/4 "2023-08-08T07:26:34Z")

</div>

Hi,  
It looks like there are still some problem with the generated dictionaries. Some class definitions seem meeting. Maybe @pcanal can help you more on this

---

<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:** [August 8, 2023, 5:44pm UTC](https://root-forum.cern.ch/t/how-to-properly-save-custom-types-to-ttree-branches-in-python/55929/5 "2023-08-08T17:44:12Z")

</div>

> [@Jackhammer0102](#):
>
> ```auto
> root [0] .L saveCustomTypesToTree.C+
> Info in <TMacOSXSystem::ACLiC>: creating shared library /Users/martin/Desktop/NuRadioMC/noise/./saveCustomTypesToTree_C.so
> Warning in <TClassTable::Add>: class mattak::Header already in TClassTable
> Warning in <TClassTable::Add>: class mattak::VoltageCalibration already in TClassTable
> Warning in <TClassTable::Add>: class mattak::Waveforms already in TClassTable
> 
> ```

This indicates that the dictionary is loaded twice (and/or there is 2 dictionary for the same classes).

For example this could happens if the type are defined in `saveCustomTypesToTree.C` or `saveCustomTypesToTree.h` and also in a shared library that is automatic loaded.

---

<div class="post-metadata">

**Author:** ![Jackhammer0102](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Jackhammer0102](https://root-forum.cern.ch/u/Jackhammer0102)\
**Post date:** [August 8, 2023, 6:44pm UTC](https://root-forum.cern.ch/t/how-to-properly-save-custom-types-to-ttree-branches-in-python/55929/6 "2023-08-08T18:44:24Z")

</div>

@moneta and @pcanal,

Thank you for your replies! I am still confused about what I should do to indicate the custom types in the tree branches in Python:

```auto
    daqstatus = ROOT.mattak.DAQStatus()
    header = ROOT.mattak.Header()
    waveforms = ROOT.mattak.Waveforms()

    inTree = inFile.Get("combined")
    inTree.SetBranchAddress("daqstatus", daqstatus)
    inTree.SetBranchAddress("header", header)
    inTree.SetBranchAddress("waveforms", waveforms)

    outFileName = 'filtered_' + args.inFileName
    outFile = TFile(outFileName, "recreate" )
    outTree = TTree("combined", "combined")
    outTree.Branch("daqstatus", daqstatus)
    outTree.Branch("header", header)
    outTree.Branch("waveforms", waveforms)

```

Could you please tell me what I need to change here?

Thanks!

---

<div class="post-metadata">

**Author:** ![Jackhammer0102](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Jackhammer0102](https://root-forum.cern.ch/u/Jackhammer0102)\
**Post date:** [August 14, 2023, 12:27pm UTC](https://root-forum.cern.ch/t/how-to-properly-save-custom-types-to-ttree-branches-in-python/55929/7 "2023-08-14T12:27:25Z")

</div>

@moneta and @pcanal,

I’ve figured out how to do that, thanks for your previous responses, which have inspired me.

---

<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:** [August 14, 2023, 3:34pm UTC](https://root-forum.cern.ch/t/how-to-properly-save-custom-types-to-ttree-branches-in-python/55929/8 "2023-08-14T15:34:56Z")

</div>

Don’t hesitate to post the answer/solution here for other to see when finding your post when they have a similar question.

---

<div class="post-metadata">

**Author:** ![Jackhammer0102](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Jackhammer0102](https://root-forum.cern.ch/u/Jackhammer0102)\
**Post date:** [August 14, 2023, 5:58pm UTC](https://root-forum.cern.ch/t/how-to-properly-save-custom-types-to-ttree-branches-in-python/55929/9 "2023-08-14T17:58:50Z")

</div>

What I’ve done is to indicate the custom types in the same way as what we do in C++, though it might look funny in a Python script but it works:

```auto
    parser = argparse.ArgumentParser()
    parser.add_argument("inFileName", type=str)
    args = parser.parse_args()

    inFile = TFile(args.inFileName)

    daqstatus = ROOT.mattak.DAQStatus()
    header = ROOT.mattak.Header()
    waveforms = ROOT.mattak.Waveforms()

    inTree = inFile.Get("combined")
    inTree.SetBranchAddress("daqstatus", ROOT.AddressOf(daqstatus))
    inTree.SetBranchAddress("header", ROOT.AddressOf(header))
    inTree.SetBranchAddress("waveforms", ROOT.AddressOf(waveforms))

    outFileName = 'filtered_' + args.inFileName
    outFile = TFile(outFileName, "recreate" )
    outTree = TTree("combined", "combined")
    outTree.Branch("daqstatus", "mattak::DAQStatus", ROOT.AddressOf(daqstatus))
    outTree.Branch("header", "mattak::Header", ROOT.AddressOf(header))
    outTree.Branch("waveforms", "mattak::Waveforms", ROOT.AddressOf(waveforms))

    nTotalEvents = inTree.GetEntries()
    nPassedEvents = 0
    hitAll = 4
    HF = HitFilter()

    nChannels = 24
    for i_event in range(nTotalEvents):
        inTree.GetEntry(i_event)
        wfs = []
        for i_channel in range(nChannels):
            if i_channel < 4:
                wfs.append(waveforms.makeGraph(i_channel).GetY())
            else:
                break
        nHits = HF.scanForHits(np.array(wfs))
        if nHits == hitAll:
            nPassedEvents += 1
            outTree.Fill()

    inFile.Close()
    outFile.cd()
    outTree.Write()
    outFile.Close()

```

---

<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:** [August 28, 2023, 5:59pm UTC](https://root-forum.cern.ch/t/how-to-properly-save-custom-types-to-ttree-branches-in-python/55929/10 "2023-08-28T17:59:31Z")

</div>

Closed on August 28, 2023, 5:59pm UTC

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