Skip to content

Commit 2061235

Browse files
guitargeekdpiparo
authored andcommitted
[PyROOT] Pythonize constructors to give ownership to current TFile
Python gives up ownership if the TTree or TH1 is attached to a TFile, which is the owner in that case. (cherry picked from commit 23c893d)
1 parent fb2a4b1 commit 2061235

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_th1.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,45 @@ def _imul(self, c):
2323
return self
2424

2525

26+
def _TH1_Constructor(self, *args, **kwargs):
27+
"""
28+
Forward the arguments to the C++ constructor and give up ownership if the
29+
TH1 is attached to a TFile, which is the owner in that case.
30+
"""
31+
import ROOT
32+
33+
self._cpp_constructor(*args, **kwargs)
34+
tdir = self.GetDirectory()
35+
if tdir and type(tdir).__cpp_name__ == "TFile":
36+
ROOT.SetOwnership(self, False)
37+
38+
# The constructors need to be pythonized for each derived class separately:
39+
40+
@pythonization('TH1D')
41+
def pythonize_th1(klass):
42+
klass._cpp_constructor = klass.__init__
43+
klass.__init__ = _TH1_Constructor
44+
45+
@pythonization('TH1F')
46+
def pythonize_th1(klass):
47+
klass._cpp_constructor = klass.__init__
48+
klass.__init__ = _TH1_Constructor
49+
50+
@pythonization('THDF')
51+
def pythonize_th1(klass):
52+
klass._cpp_constructor = klass.__init__
53+
klass.__init__ = _TH1_Constructor
54+
55+
@pythonization('TH2F')
56+
def pythonize_th1(klass):
57+
klass._cpp_constructor = klass.__init__
58+
klass.__init__ = _TH1_Constructor
59+
60+
@pythonization('TProfile')
61+
def pythonize_th1(klass):
62+
klass._cpp_constructor = klass.__init__
63+
klass.__init__ = _TH1_Constructor
64+
2665
@pythonization('TH1')
2766
def pythonize_th1(klass):
2867
# Parameters:

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_ttree.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,27 @@ def _TTree__getattr__(self, key):
279279
out = cppyy.ll.cast[cast_type](out)
280280
return out
281281

282+
def _TTree_Constructor(self, *args, **kwargs):
283+
"""
284+
Forward the arguments to the C++ constructor and give up ownership if the
285+
TTree is attached to a TFile, which is the owner in that case.
286+
"""
287+
import ROOT
288+
289+
self._cpp_constructor(*args, **kwargs)
290+
tdir = self.GetDirectory()
291+
if tdir and type(tdir).__cpp_name__ == "TFile":
292+
ROOT.SetOwnership(self, False)
282293

283294
@pythonization("TTree")
284295
def pythonize_ttree(klass, name):
285296
# Parameters:
286297
# klass: class to be pythonized
287298
# name: string containing the name of the class
288299

300+
klass._cpp_constructor = klass.__init__
301+
klass.__init__ = _TTree_Constructor
302+
289303
# Pythonizations that are common to TTree and its subclasses.
290304
# To avoid duplicating the same logic in the pythonizors of
291305
# the subclasses, inject the pythonizations for all the target
@@ -321,3 +335,10 @@ def pythonize_tchain(klass):
321335
# SetBranchAddress
322336
klass._OriginalSetBranchAddress = klass.SetBranchAddress
323337
klass.SetBranchAddress = _SetBranchAddress
338+
339+
@pythonization("TNtuple")
340+
def pythonize_tchain(klass):
341+
342+
# The constructor needs to be explicitly pythonized for derived classes.
343+
klass._cpp_constructor = klass.__init__
344+
klass.__init__ = _TTree_Constructor

0 commit comments

Comments
 (0)