PyRoot write and retrieve correctly custom fitted pdf to and from file

Dear RooFit users

I have some problems saving to a file a pdf generated with RooFit.
I generated a cxx file using the RooClassFactory::makePdfInstance function and then I modified the evaluate method

    import ROOT
    from ROOT import RooWorkspace, RooRealVar

    ROOT.gROOT.ProcessLineSync(".x super_novosibirsk.cxx+")
    w = RooWorkspace("w")

    Mass = RooRealVar("Mass", "Mass", 120, 600)
    p0 = RooRealVar("p0", "p0", 0, 0.005)
    p1 = RooRealVar("p1", "p1", 1600, 2000)
    p3 = RooRealVar("p3", "p3", 30, 50)
    p4 = RooRealVar("p4", "p4", 40, 80)
    p5 = RooRealVar("p5", "p5", 0, 5)
    p6 = RooRealVar("p6", "p6", -0.05, -0.001)
    super_novosibirsk = ROOT.super_novosibirsk(
        "super_novosibirsk", "super_novosibirsk", Mass, p0, p1, p3, p4, p5, p6)
    frame = Mass.frame()
    print("Retrieving data from tree")
    data = RooDataSet("Mass", "Mass", tree, RooArgSet(Mass)) # I have a tree defined before
    data.plotOn(frame, Binning(100))
    print("Plotting and fitting stuff")
    # super_novosibirsk.plotOn(frames[i])                                                                                                                                                                          
    # fitResult = super_novosibirsk.fitTo(                                                                                                                                                                         
    #     data, ROOT.RooFit.Save(), ROOT.RooFit.PrintLevel(-1))                                                                                                                                                    
    # fitResult.plotOn(frame)                                                                                                                                                                                      

    getattr(w, 'import')(super_novosibirsk)
    c = "output_file" 
    output_filename = c
    output_filename = os.path.join(output_dir_fitted, c + ".root")
    # out_file = TFile(os.path.join(output_dir_fitted, c + ".root"), "recreate")                                                                                                                                   
    w.writeToFile(output_filename, True)
    # out_file.Close()                                                                                                                                                                                             

No errors are printed with this piece of code but when I try to get the workspace back I have the following errors

root -l super_novosibirsk.cxx output_file.root
root [0] TBrowser br
(I click on the saved workspace)
Error in <TBufferFile::CheckByteCount>: object of class super_novosibirsk read too few bytes: 22 instead of 4074

I also used some scripts to try to get my pdf back but none of them worked. What am I doing wrong?
Thank you

1 Like

Hi Fabio,

Could you attach either the super_novosibrisk.cxx or the output workspace (or both) such that I can verify?



super_novosibirsk.cxx (2.1 KB)
super_novosibirsk.h (1.4 KB)

I am uploading them in 2 different replies because I cannot upload more as I am a new user.
Thank you

output_roofit.root (7.6 KB)
Here is the output



Sometimes the TBrowser doesn’t quite get how to handle custom functions. But I wrote you a simple script (based on your own code) to get your pdf back.

import ROOT
ROOT.gROOT.ProcessLineSync(".x super_novosibirsk.cxx+")
f = ROOT.TFile.Open("output_roofit.root")
w = f.Get("w")
pdf = w.pdf("super_novosibirsk")
x = w.var("Mass")
xframe = x.frame()

This produces the attached plot without issue.

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