# Cannot instance cpp class from pyroot script

**URL:** <https://root-forum.cern.ch/t/cannot-instance-cpp-class-from-pyroot-script/45557>\
**Category:** Newbie\
**Tags:** python, root\
**Created:** [June 25, 2021, 12:52pm UTC](https://root-forum.cern.ch/t/cannot-instance-cpp-class-from-pyroot-script/45557 "2021-06-25T12:52:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AConaboy2923](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/c0e974/32.png) [@AConaboy2923](https://root-forum.cern.ch/u/AConaboy2923)\
**Post date:** [June 25, 2021, 12:52pm UTC](https://root-forum.cern.ch/t/cannot-instance-cpp-class-from-pyroot-script/45557/1 "2021-06-25T12:52:01Z")

</div>

Hi,  
I am trying to instance a ROOT object from a python script. The ROOT object accepts a pointer to another object as a parameter. I thought in pyroot I could just pass the object to the class without any manipulation. I also tried passing the cppyy functions, AddressOf(obj) and addressof(obj) to the constructor.

I have uploaded photos of the constructor, the python script and the error. To reproduce the error is not trivial however if someone would like to I can help with doing that.

Thank you in advance for any help, Andrew

 ![test_digi_error](https://root-forum.cern.ch/uploads/default/original/3X/3/6/36f2ac12f54b29cfa79dfa6834ce9fe0afc2df46.png)  
 ![MuFilterHitConstructor](https://root-forum.cern.ch/uploads/default/original/3X/b/7/b7b4abf7083ac42541b5dad6d56485bccf98d700.png)  
 ![digi_script](https://root-forum.cern.ch/uploads/default/original/3X/f/2/f2c6ac426fe45f529763120b820494eef30c6309.png)

_ROOT Version:_ 6.24/00  
_Platform:_ Unsure what this is  
_Compiler:_ c++ (GCC) 7.3.0

* * *

---

<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 25, 2021, 1:27pm UTC](https://root-forum.cern.ch/t/cannot-instance-cpp-class-from-pyroot-script/45557/2 "2021-06-25T13:27:29Z")

</div>

Hello,

Yes you should be able to pass a Python object to a C++ method that expects a pointer to a C++ object (provided that the types match). So no need to use `AddressOf` here.

What is the result of `type(mufilterpoint)` in your script?

---

<div class="post-metadata">

**Author:** ![AConaboy2923](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/c0e974/32.png) [@AConaboy2923](https://root-forum.cern.ch/u/AConaboy2923)\
**Post date:** [June 25, 2021, 1:31pm UTC](https://root-forum.cern.ch/t/cannot-instance-cpp-class-from-pyroot-script/45557/3 "2021-06-25T13:31:44Z")

</div>

Hi,

The result of that is: \<class cppyy.gbl.MuFilterPoint at 0x2d86298\> it is in the terminal output photo I uploaded but I didn’t specify that.  
Cheers

---

<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 25, 2021, 1:40pm UTC](https://root-forum.cern.ch/t/cannot-instance-cpp-class-from-pyroot-script/45557/4 "2021-06-25T13:40:24Z")

</div>

The type seems correct, then.

What is the segfault you get? Do you have any information?

One thing to try (if possible) is to comment out all the code in `MuFilterHit::MuFilterHit(MuFilterPoint *p)`, to see if the invocation itself works (there might be a crash inside the code of the function).

---

<div class="post-metadata">

**Author:** ![AConaboy2923](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/c0e974/32.png) [@AConaboy2923](https://root-forum.cern.ch/u/AConaboy2923)\
**Post date:** [June 25, 2021, 2:16pm UTC](https://root-forum.cern.ch/t/cannot-instance-cpp-class-from-pyroot-script/45557/5 "2021-06-25T14:16:09Z")

</div>

Hi, Thanks for that advice. I have located the error, it is coming from line 45 in the constructor. I assume “MuFilter” is not a module known to FairRunSim. Although I then tried to print FairRunSim::Instance()-\>GetListOfModules but this again caused a segmentation violation.

---

<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 25, 2021, 2:36pm UTC](https://root-forum.cern.ch/t/cannot-instance-cpp-class-from-pyroot-script/45557/6 "2021-06-25T14:36:08Z")

</div>

I see, it seems then an issue on the C++ side. Note that you can still use gdb even if you are launching that code from Python.
