# Python \*\*\* Break \*\*\* abort

**URL:** <https://root-forum.cern.ch/t/python-break-abort/42088>\
**Category:** ROOT\
**Tags:** bug, python\
**Created:** [November 1, 2020, 8:55pm UTC](https://root-forum.cern.ch/t/python-break-abort/42088 "2020-11-01T20:55:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kot](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/e274bd/32.png) [@kot](https://root-forum.cern.ch/u/kot)\
**Post date:** [November 1, 2020, 8:55pm UTC](https://root-forum.cern.ch/t/python-break-abort/42088/1 "2020-11-01T20:55:24Z")

</div>

Hello.  
I run fitting with minuit in python, at some stage program crashes with error:

```auto
#7 0x00007f081354d387 in raise () from /lib64/libc.so.6
#8 0x00007f081354ea78 in abort () from /lib64/libc.so.6
#9 0x00007f08048157d5 in __gnu_cxx::__ verbose_terminate_handler() () from /lib64/libstdc++.so.6
#10 0x00007f0804813746 in ?? () from /lib64/libstdc++.so.6
#11 0x00007f0804813773 in std::terminate() () from /lib64/libstdc++.so.6
#12 0x00007f0804813993 in __cxa_throw () from /lib64/libstdc++.so.6
#13 0x00007f07edcdcc6f in ?? ()
#14 0x00007ffe7abea680 in ?? ()
#15 0x000000000dab4080 in ?? ()
#16 0x0000000000000000 in ?? ()

```

Please, advise what’s wrong.  
I did not experience any problems with my code in Python 2.6 with ROOT 5.34.

_ROOT Version_: 6.22/02  
_Platform:_ CentOS Linux 7 (Core)  
_Compiler:_ Not Provided  
It’s CERN Lxplus machine.

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [November 2, 2020, 2:18pm UTC](https://root-forum.cern.ch/t/python-break-abort/42088/2 "2020-11-02T14:18:40Z")

</div>

Hello @kot,

Can you please send us a copy of your code in order to try to reproduce your error? Also, please, can you tell us the Python version that you are using.

Cheers,  
J.

---

<div class="post-metadata">

**Author:** ![kot](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/e274bd/32.png) [@kot](https://root-forum.cern.ch/u/kot)\
**Post date:** [November 2, 2020, 9:42pm UTC](https://root-forum.cern.ch/t/python-break-abort/42088/3 "2020-11-02T21:42:44Z")

</div>

Hello, @jalopezg  
I found the reason of the crash is an attempt to save a dictionary with TF1 functions which are required for further usage. I use python 2.7.5, which is called by default with `python2` at Lxplus.

The code sample is given below

```auto
import ROOT
import numpy as np
import shelve

class eff:
    def __call__ (self, x, par):
        return par[1] * exp0(x[0], par[0])

def exp0(x, par):
    return np.exp(-x * par)

def fit(x_min, x_max, n_pars):
    amp, alpha = 10, 1
    func_dct = {}
    # workaround with creation of python callable: https://root-forum.cern.ch/t/th1-fit-method-error/40895
    instance = eff()
    for j in range(2):
        key = 'key'+str(j)
        func_dct[key] = ROOT.TF1('func name ' + key, instance, 
        x_min, x_max, n_pars) 
        func_dct[key].SetParameter(0, amp)
        func_dct[key].SetParameter(1, alpha)
    return func_dct

if __name__ == ' __main__':
    x_min, x_max = 0, 5
    n_pars = 2
    func_dct = fit(x_min, x_max, n_pars)
    ses = shelve.open('filename','n')
    # crashes here
    ses['res'] = func_dct
    ses.close()

```

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [November 3, 2020, 9:20am UTC](https://root-forum.cern.ch/t/python-break-abort/42088/4 "2020-11-03T09:20:57Z")

</div>

Hello @kot,

I think @etejedor may have an answer for this issue.

Cheers,  
J.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [November 4, 2020, 12:06pm UTC](https://root-forum.cern.ch/t/python-break-abort/42088/5 "2020-11-04T12:06:28Z")

</div>

Hi @kot,  
it seems that pickling a PyROOT proxy to a TF1 (which is a C++ object) that uses a python functor as its expression is just _too much_ magic, although I can’t tell what goes wrong exactly.

Best guess: `instance` has already gone out of scope when the `TF1` is pickled/shelved.  
Keeping it alive resolves the crash (not 100% sure everything is written to file as you want, but that’s easily verified):

```python
def fit(x_min, x_max, n_pars):                                                                                          
    amp, alpha = 10, 1                                                                                                  
    func_dct = {}                                                                                                       
    # workaround with creation of python callable: https://root-forum.cern.ch/t/th1-fit-method-error/40895              
    instance = eff()                                                                                                    
    for j in range(2):                                                                                                  
        key = 'key'+str(j)                                                                                              
        func_dct[key] = ROOT.TF1('func name ' + key, "sin(x)",·                                                         
        x_min, x_max, n_pars)·                                                                                          
        func_dct[key].SetParameter(0, amp)                                                                              
        func_dct[key].SetParameter(1, alpha)                                                                            
    return func_dct, instance # RETURN instance TOO!!                 
                                                                                                                        
                                                                                                                        
if __name__ == ' __main__':                                                                                              
    x_min, x_max = 0, 5                                                                                                 
    n_pars = 2                                                                                                          
    func_dct, inst = fit(x_min, x_max, n_pars)                                                                          
    ses = shelve.open('filename','n')                                                                                   
    print("ok")                                                                                                         
    # crashes here                                                                                                      
    ses['res'] = func_dct                                                                                               
    print("ok")                                                                                                         
    ses.close() 

```

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![kot](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/e274bd/32.png) [@kot](https://root-forum.cern.ch/u/kot)\
**Post date:** [November 4, 2020, 6:08pm UTC](https://root-forum.cern.ch/t/python-break-abort/42088/6 "2020-11-04T18:08:38Z")

</div>

Dear Enrico,

Thank you for the workaround!  
TF1 objects have been saved and then read correctly.

Best regards,  
Alex

---

<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:** [November 18, 2020, 6:08pm UTC](https://root-forum.cern.ch/t/python-break-abort/42088/7 "2020-11-18T18:08:40Z")

</div>

Closed on November 18, 2020, 6:08pm UTC

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