# Acessing custom class branches in python

**URL:** <https://root-forum.cern.ch/t/acessing-custom-class-branches-in-python/46116>\
**Category:** ROOT\
**Tags:** python\
**Created:** [August 1, 2021, 4:11pm UTC](https://root-forum.cern.ch/t/acessing-custom-class-branches-in-python/46116 "2021-08-01T16:11:26Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [August 1, 2021, 4:11pm UTC](https://root-forum.cern.ch/t/acessing-custom-class-branches-in-python/46116/1 "2021-08-01T16:11:26Z")

</div>

Hi all,

My post is somewhat related to the difference between `dd4hep::rec::Vector3D` and `XYZVector` discussed [here](https://root-forum.cern.ch/t/how-to-create-vector-vector3d-branch-w-o-dictionary/44176/4)…

I used to creating `XYZVector` branches without a dictionary, which allows me to immediately start to analyze output root file in python scripts, like e.g.:

```auto
df = ROOT.RDataFrame("tree_name", "output.root")
h = df.Define("pos_z", "my_vec.z()").Histo1D("pos_z")
h.Draw()

```

However, I couldn’t achieve the same with `dd4hep::rec::Vector3D`

I created a dictionary and compiled and linked it, so it successfully created output ROOT file.

But now, there are probably additional steps I need to make in my python script so everything works?

Cause it outputs an error:

```auto
input_line_68:2:38: error: use of undeclared identifier 'dd4hep'
auto lambda0 = [](ROOT::VecOps::RVec<dd4hep::rec::Vector3D>& var0){return var0.z

```

cheers,  
Bohdan

---

<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 1, 2021, 10:13pm UTC](https://root-forum.cern.ch/t/acessing-custom-class-branches-in-python/46116/2 "2021-08-01T22:13:18Z")

</div>

Did you request a dictionary also for the namespaces?

---

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [August 2, 2021, 8:08am UTC](https://root-forum.cern.ch/t/acessing-custom-class-branches-in-python/46116/3 "2021-08-02T08:08:41Z")

</div>

Do I understand correctly that I need modify my `ContLinkDef.h` from

`#pragma link C++ class std::vector<dd4hep::rec::Vector3D>+;`

to

```auto
#pragma link C++ class dd4hep::rec::Vector3D+;
#pragma link C++ class std::vector<dd4hep::rec::Vector3D>+;

```

Will it also work for the namespaces? E.g. I should do:

```auto
#pragma link C++ class dd4hep::rec+;
#pragma link C++ class dd4hep::rec::Vector3D+;
#pragma link C++ class std::vector<dd4hep::rec::Vector3D>+;

```

?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 2, 2021, 10:09am UTC](https://root-forum.cern.ch/t/acessing-custom-class-branches-in-python/46116/4 "2021-08-02T10:09:33Z")

</div>

Try:

```cpp
#pragma link C++ nestedclass;
#pragma link C++ nestedtypedef;
#pragma link C++ namespace dd4hep;
#pragma link C++ defined_in namespace dd4hep;
#pragma link C++ namespace dd4hep::rec;
#pragma link C++ defined_in namespace dd4hep::rec;
#pragma link C++ class dd4hep::rec::Vector3D+;
#pragma link C++ class std::vector<dd4hep::rec::Vector3D>+;

```

---

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [August 2, 2021, 11:40am UTC](https://root-forum.cern.ch/t/acessing-custom-class-branches-in-python/46116/5 "2021-08-02T11:40:06Z")

</div>

It works, thanks a lot.

---

<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 16, 2021, 11:41am UTC](https://root-forum.cern.ch/t/acessing-custom-class-branches-in-python/46116/6 "2021-08-16T11:41:01Z")

</div>

Closed on August 16, 2021, 11:41am UTC

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

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [September 5, 2021, 1:49pm UTC](https://root-forum.cern.ch/t/acessing-custom-class-branches-in-python/46116/7 "2021-09-05T13:49:18Z")

</div>

Opened on September 5, 2021, 1:49pm UTC



---

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [September 5, 2021, 12:52pm UTC](https://root-forum.cern.ch/t/acessing-custom-class-branches-in-python/46116/8 "2021-09-05T12:52:30Z")

</div>

Hi all,

During the discussion [here](https://root-forum.cern.ch/t/acessing-custom-class-branches-in-python/46116/4) I didn’t check thorough enough and now I can’t make it work again…

Is it possible to write **methods** of `dd4hep::rec::Vector3D` or is it impossible by design?

_ROOT Version:_ 6.18  
_Platform:_ Centos 7  
_Compiler:_ g++ 8.2

* * *

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [September 5, 2021, 1:40pm UTC](https://root-forum.cern.ch/t/acessing-custom-class-branches-in-python/46116/9 "2021-09-05T13:40:39Z")

</div>

Well, the dictionary created by “`#pragma link C++ class dd4hep::rec::Vector3D+;`” should (automatically) include all data members and all methods of this class.

---

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [September 5, 2021, 8:43pm UTC](https://root-forum.cern.ch/t/acessing-custom-class-branches-in-python/46116/10 "2021-09-05T20:43:28Z")

</div>

Ok… Then I try to ask again attaching full examples as I should have done in the first place to not cause confusion and in hopes to finally understand this topic…

### Setup

- OS: Centos 7
- ROOT: 6.18/04 Built for linuxx8664gcc on Sep 11 2019, 15:38:23
- g++: 8.2.0
- DD4hep: v01-11-02

### Code example

I have the following script and the file for dictionaries in the same folder:  
[test.cpp](https://root-forum.cern.ch/uploads/short-url/r5DAi0Med0J27fQIEZlJ7S966so.cpp) (422 Bytes)  
[ContLinkDef.h](https://root-forum.cern.ch/uploads/short-url/pu3cA79EQWdSZQdUoUUhs4RYhtu.h) (341 Bytes)

### Steps

1. I compile dictionaries with  
`rootcling -f ContDict.cxx -I${PWD} vector DDRec/Vector3D.h ContLinkDef.h`  
which creates me dictionary files:  
[ContDict.cxx](https://root-forum.cern.ch/uploads/short-url/1qTUegOcCGHFJmn5itjRGWSkXiW.cxx) (11.3 KB)  
and `ContDict_rdict.pcm` (can’t attach)

2. I compile both files with  
`g++ test.cpp ContDict.cxx -I$DD4hep_DIR/include `root-config --cflags --glibs` `  
and I obtain my compiled executable `a.out`.

3. Run `./a.out` which outputs me my root file:  
[test.root](https://root-forum.cern.ch/uploads/short-url/fF03IvAo0rdsNyizwksaOcmgK7M.root) (6.3 KB)

4. Trying to look at the file via TBrowser:

```auto
$ root
root [0] new TBrowser
(TBrowser *) 0x21fb010
root [1]

```

After clicking and opening the file it produces a warning:

```auto
root [1] Warning in <TClass::Init>: no dictionary for class dd4hep::rec::Vector3D is available
(TFile *) 0x37c7460

```

1. Looking inside the tree shows correct data stored inside `_x`, `_y`, `_z` variables of the `Vector3D`, but no other methods are written.  
For the `std::vector` however, branch `@size` for the method `size()` is present.

### Expected behavior

I would expect `Vector3D` to be written with all the physical methods, e.g.: `theta()`, `rho()`, etc.

### Trying to understand on my own

I was trying to read some documentation about dictionaries and stumbled upon this [link](https://root.cern.ch/root/htmldoc/guides/users-guide/AddingaClass.html).  
which I don’t fully understand, but I see sentences like this:

> Looking in the file `eventdict.cxx` we can see, the`Streamer()` and`ShowMembers()`methods for the two classes. `Streamer()` is used to stream an object to/from a **`TBuffer`** and `ShowMembers()` is used by the `Dump()` and `Inspect()` methods of **`TObject`**. Here is the `TEvent::Streamer` method:

And I don’t see neither `Streamer()` nor `ShowMembers()` in my generated `ContDict.cxx`

Also this section:

> The `ShowMembers` and `Streamer` method, as well as the `>>` operator overloads, are implemented **!!!only!!!** if you use `ClassDef` and `ClassImp`. See `$ROOTSYS/include/Rtypes.h` for the definition of `ClassDef` and `ClassImp`

indicated to me that it will not produce these methods if I don’t use `ClassDef` and `ClassImp` whatever that means.

Also this section:

> **Step 4:** Compile the class using the `Makefile.`In the `Makefile` call `rootcling` to make the dictionary for the class. Call it `SClassDict.cxx`. The `rootcling` utility generates the methods `Streamer`, **`TBuffer`** &operator\>\>() and `ShowMembers` for ROOT classes.

Indicated to me that it creates these methods for **ROOT** classes making me understand that I cannot do that for outside ROOT classes like `dd4hep::rec::Vector3D`

And at this point I was completely frustrated and lost hope in using dictionaries.

#### minor notes

initially my question would be how to do that with `cmake`. But I switched to `g++` to simplify the example, assuming that I will not have problem transferring working example to the `cmake`.

P.S. I have tried it with recent ROOT version as well and I believe it does not make any different and behavior stays the same. Only some `verbose` messages change if I call `rootcint` with `-v` flag.

cheers,  
Bohdan

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [September 5, 2021, 9:19pm UTC](https://root-forum.cern.ch/t/acessing-custom-class-branches-in-python/46116/11 "2021-09-05T21:19:04Z")

</div>

In principle, you would need to create a shared library:

```bash
`root-config --cxx --cflags` -fPIC -shared -I${DD4hep_DIR}/include ContDict.cxx -o libContDict.so `root-config --libs`

```

and then load it into ROOT before opening your file:

```bash
root [0] .L libContDict.so
root [1] new TBrowser()

```

---

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [September 6, 2021, 2:08pm UTC](https://root-forum.cern.ch/t/acessing-custom-class-branches-in-python/46116/12 "2021-09-06T14:08:18Z")

</div>

Thanks, it works.

And to access the file from python, I can use:  
`ROOT.gInterpreter.LoadFile("libContDict.so")`

So, is it always the case that the end user which uses my root file also should have `libContDict.so` file? Is there a way of dealing with this w/o additional library file?

I assume it works for `ROOT::Math::XYZVector` w/o library file because the library is always read by default somewhere under the hood?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [September 6, 2021, 2:20pm UTC](https://root-forum.cern.ch/t/acessing-custom-class-branches-in-python/46116/13 "2021-09-06T14:20:04Z")

</div>

Maybe better: `ROOT.gSystem.Load("libContDict")`

@couet I don’t know any “guide” / “manual” which explicitly demonstrates how to achieve autoloading of ROOT classes (i.e., all steps needed without using CMake).

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 6, 2021, 3:24pm UTC](https://root-forum.cern.ch/t/acessing-custom-class-branches-in-python/46116/14 "2021-09-06T15:24:15Z")

</div>

We will have soon a doc review . I Added your point as an issue for the ROOT manual. Thanks. [https://github.com/root-project/web/issues/641](https://github.com/root-project/web/issues/641)

---

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [September 6, 2021, 4:04pm UTC](https://root-forum.cern.ch/t/acessing-custom-class-branches-in-python/46116/15 "2021-09-06T16:04:42Z")

</div>

So did I understand correctly that it possible to create a ROOT file with `dd4hep::rec::Vector3D` branch using `cmake` such that the output file can be read without preloading library?
