Returning Issue with TH1F from function

Dear experts,

I am trying to return TH1F from a function but it was returning me type “PyROOT_NoneType”. For this I searched and fixed this issue using link [1] & [2].

But, later when I am going to save the histogram using

c1.SaveAs("test.pdf")

Then it is giving me error:

     c1.SaveAs("test.pdf");
SystemError: void TPad::SaveAs(const char* filename = "", const char* option = "") =>
     problem in C++; program state has been reset

Please let me know how I can fix this. you can see part of my code [3]

with regards,
Ram

===============

[1] Returning TH1F from function
[2] NoneType Feturned from Function that returns TH1F

[3] code:

def aQGC_GetHisto(plot_info, key1, ColNum):

    file1 = ROOT.TFile(plot_info["file_name"][0])
    tree1 = file1.Get(plot_info["tree_folder"] + plot_info["tree_name"])

    hist = ROOT.TH1F()
    hist1 = ROOT.TH1F("hist1", "Test", plot_info["nbin"], plot_info["xmin"], plot_info["xmax"])    
    tree1.Draw(plot_info["tree_var"][0] + ">>hist1","LHEWeights["+str(key1)+"]","",1000)
    return hist1
        
def aQGC_plotting (plot_info, aQGC_key, aQGC_val, outputNameString):
    c1 = getCanvas()   
    legend = ROOT.TLegend(.8 ,.60 ,1.0 ,.900)
    legend.SetFillColor(ROOT.kWhite)

    hist = ROOT.TH1F()
    print type(hist)
    for i in range(0,1):
        hist = aQGC_GetHisto(plot_info,aQGC_key[i],i)
        print "hist type : ",type(hist)
        legend.AddEntry(hist, aQGC_val[i].replace("_m"," = ").replace("p",".").replace("_0"," = 0"))
        if i==0:
                hist.Draw()
        else:
                hist.Draw("sames")
    legend.Draw("same")
    #setTDRStyle(c1, 1, 13, plot_info["printCMS"]) 
    hist1.GetXaxis().SetTitle(plot_info["xlabel"])
    if plot_info["ylabel"] == "":
        plot_info["ylabel"] = "Events / %s GeV" % int(hist1.GetBinWidth(1))
    hist1.GetYaxis().SetTitle(plot_info["ylabel"])
    c1.SaveAs("test.pdf")

Hi,

what script are you actually running?

Cheers,
D

Hi,

I just added part of my code in question.

Hi,

is the c1 you get from getCanvas() still well defined when you try to print, e.g. is it non-null?

Cheers,
D

Hi,

Thanks for help.

when I print c1 then I got

<ROOT.TCanvas object ("c2") at 0x487a950>

Here [1] is my function getCanvas() is defined.

[1]

def getCanvas():
    H_ref = 600; 
    W_ref = 800; 
    W = W_ref
    H  = H_ref

    T = 0.08*H_ref
    B = 0.12*H_ref 
    L = 0.12*W_ref
    R = 0.04*W_ref

    canvas = ROOT.TCanvas("c2","c2",50,50,W,H)
    canvas.SetFillColor(0)
    canvas.SetBorderMode(0)
    canvas.SetFrameFillStyle(0)
    canvas.SetFrameBorderMode(0)
    canvas.SetLeftMargin( L/W )
    canvas.SetRightMargin( R/W )
    canvas.SetTopMargin( T/H )
    canvas.SetBottomMargin( B/H )
    canvas.SetTickx(0)
    canvas.SetTicky(0) 
    return canvas

Hi,

For you information, my older version of this code is working, where I did not defined a function aQGC_GetHisto. I am just defining all my histograms in the same function aQGC_plotting and saving there only.

def aQGC_plotting (plot_info, aQGC_key, aQGC_val, outputNameString):
    c1 = getCanvas()
    legend = ROOT.TLegend(.8 ,.60 ,1.0 ,.900)
    legend.SetFillColor(ROOT.kWhite)
    file1 = ROOT.TFile(plot_info["file_name"][0])
    tree1 = file1.Get(plot_info["tree_folder"] + plot_info["tree_name"])

    hist1 = ROOT.TH1F("hist1", "Test", plot_info["nbin"], plot_info["xmin"], plot_info["xmax"])    
    hist2 = ROOT.TH1F("hist2", "Test", plot_info["nbin"], plot_info["xmin"], plot_info["xmax"])    
    hist3 = ROOT.TH1F("hist3", "Test", plot_info["nbin"], plot_info["xmin"], plot_info["xmax"])    
    hist4 = ROOT.TH1F("hist4", "Test", plot_info["nbin"], plot_info["xmin"], plot_info["xmax"])    
    hist5 = ROOT.TH1F("hist5", "Test", plot_info["nbin"], plot_info["xmin"], plot_info["xmax"])    

    if len(aQGC_key) > 0:
        tree1.Draw(plot_info["tree_var"][0] + ">>hist1","LHEWeights["+str(aQGC_key[0])+"]")
        legend.AddEntry(hist1, aQGC_val[0].replace("_m"," = ").replace("p",".").replace("_0"," = 0"))
        hist1.Scale(1/hist1.Integral())
        setHistAttributes(hist1, plot_info, ROOT.kBlack,0)
    if len(aQGC_key) >= 2:
        tree1.Draw(plot_info["tree_var"][0] + ">>hist2","LHEWeights["+str(aQGC_key[1])+"]")
        legend.AddEntry(hist2, aQGC_val[1].replace("_m"," = ").replace("p",".").replace("_0"," = 0"))
        hist2.Scale(1/hist2.Integral())
        setHistAttributes(hist2, plot_info, ROOT.kRed,0)
    if len(aQGC_key) >= 3:
        tree1.Draw(plot_info["tree_var"][0] + ">>hist3","LHEWeights["+str(aQGC_key[2])+"]")
        legend.AddEntry(hist3, aQGC_val[2].replace("_m"," = ").replace("p",".").replace("_0"," = 0"))
        hist3.Scale(1/hist3.Integral())
        setHistAttributes(hist3, plot_info, ROOT.kGreen,0)
    if len(aQGC_key) >= 4:
        tree1.Draw(plot_info["tree_var"][0] + ">>hist4","LHEWeights["+str(aQGC_key[3])+"]")
        legend.AddEntry(hist4, aQGC_val[3].replace("_m"," = ").replace("p",".").replace("_0"," = 0"))
        hist4.Scale(1/hist4.Integral())
        setHistAttributes(hist4, plot_info, ROOT.kYellow,0)
    if len(aQGC_key) >= 5:
        tree1.Draw(plot_info["tree_var"][0] + ">>hist5","LHEWeights["+str(aQGC_key[4])+"]")
        legend.AddEntry(hist5, aQGC_val[4].replace("_m"," = ").replace("p",".").replace("_0"," = 0"))
        hist5.Scale(1/hist5.Integral())
        setHistAttributes(hist5, plot_info, ROOT.kViolet,0)

    hist1.Draw()
    if len(aQGC_key) >= 2:
        hist2.Draw("sames")
    if len(aQGC_key) >= 3:
        hist3.Draw("sames")
    if len(aQGC_key) >= 4:
        hist4.Draw("sames")
    if len(aQGC_key) >= 5:
        hist5.Draw("sames")

    legend.Draw("same")
    setTDRStyle(c1, 1, 13, "No") 
    hist1.GetXaxis().SetTitle(plot_info["xlabel"])
    if plot_info["ylabel"] == "":
        plot_info["ylabel"] = "Events / %s GeV" % int(hist1.GetBinWidth(1))
    hist1.GetYaxis().SetTitle(plot_info["ylabel"])
    c1.SaveAs("test.pdf")

Hi,

the histos are being garbage collected. Perhaps you can use DrawClone or keep a list of them that you update in your for loop.

Cheers,
D

Hi,

I tried DrawClone but it was not working then I saved list of histo and it worked.

Thank you very much.

with regards,
Ram

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