I use a RooChebychev pdf. I transfer it to another function and can also make a copy of that.
I wrote a demo based on that suggestion by @jonas to take care of ownership (so that variables of RooAbsPdf do not get destroyed):
import copy
import ROOT
def demo_init():
x = ROOT.RooRealVar("x", "x", -1, 1)
x.setBins(10)
a1 = ROOT.RooRealVar("a1", "a1", 0.5, 0., 1.)
a2 = ROOT.RooRealVar("a2", "a2", 0.0, 0., 1.)
cheb = ROOT.RooChebychev("cheb", "Background", x, ROOT.RooArgSet(a1, a2))
ROOT.SetOwnership(x, False)
ROOT.SetOwnership(a1, False)
ROOT.SetOwnership(a2, False)
cheb.addOwnedComponents([x, a1, a2])
## Choose a copy method here ##
# cheb2 = cheb
cheb2 = copy.deepcopy(cheb)
return (cheb, cheb2)
def demo():
cheb, cheb2 = demo_init()
cheb.Print()
del cheb
cheb2.Print()
demo()
The results depend on whether I use a simple copy or a widely used copy.deepcopy:
A simple copy (everything works fine):
RooChebychev::cheb[ x=x coefficients=(a1,a2) ] = 1
RooChebychev::cheb[ x=x coefficients=(a1,a2) ] = 1
A deep copy (the second function fails with after the first one is destroyed) (the same holds for copy.copy):
RooChebychev::cheb[ x=x coefficients=(a1,a2) ] = 1
RooChebychev::cheb = 1
I believe that in the worst case copy.deepcopy for a PyROOT object should resort to a simple copy. I tried to store parameters of the pdf in an unrelated RooArgList, but surprisingly it had no effect on the cloned function (its parameters got destroyed with the destruction of cheb).
I have to use ROOT 6.30/06 with Python 2 because of some unknown bugs. Feel free to check that with a recent version.