Hi,
So the problem I am having seems like it should be straight forward but I cannot sort it out. I am creating a number of histograms and saving them to a canvas, but upon looking at the canvas after creation, it will either be empty or only have the last histogram drawn on it. The weird thing is that I am doing the same thing with a TGraph and it works totally fine. I strongly suspect that the issue is the histograms are being deleted by python, and for some reason the same isn’t happening to the TGraphs.
My relevant code is as follows (I’ve omitted some of it as there is a large chunk that just sets up values for filling the graph/histogram):
def make_graphs():
# The canvases to be used
canvas = TCanvas("Sigma vs. " + x_axis +
" with fixed " + canvas_var + "=" + str(canvas_var_val),
canvas_var + "=" + str(canvas_var_val) + fixed_vars_str,
800, 1000)
canvas_2D = TCanvas("2D: Sigma vs. " + x_axis + " and " + lines +
" with fixed " + canvas_var + "=" + str(canvas_var_val),
"2D_" + canvas_var + "=" + str(canvas_var_val) +
fixed_vars_str, 800, 1000)
panels = len(params[graph_var])
canvas.Divide(min(2, panels), panels/2)
canvas_2D.Divide(min(2, panels), panels/2)
# Lists for storing the graphs
graph_list = []
hist_list_2D = []
leg_list = []
for panel_var in panel_var_list:
"""
Misc. omitted code for setting up the values to fill the hist and graph with
"""
# Initialize the graph and legend
title = "#sigma_{Eff} / E vs. " + x_axis + " where " + \
graph_var + "=" + str(panel_var) + " and " + \
canvas_var + "=" + str(canvas_var_val) + fixed_vars_str
title_2D = "#sigma_{Eff} / E vs. " + x_axis + " and " + lines + \
graph_var + "=" + str(panel_var) + " and " + \
canvas_var + "=" + str(canvas_var_val) + fixed_vars_str
graph = TMultiGraph()
hist_2D = TH2F(title_2D, title_2D, nx_bins, x_bins, ny_bins, y_bins)
legend = TLegend(0.8,0.2,1.0,0.95)
graph.SetName(title)
graph.SetTitle(title)
hist_2D.SetName(title_2D)
hist_2D.SetTitle(title_2D)
legend.SetName(title + "_leg")
legend.SetBorderSize(1)
# Add the points to the graph/legend and filling the histogram
for g in g_lines:
graph.Add(g[0])
legend.AddEntry(g[0], g[1], "p")
for i, p in enumerate(hist_vals_2D):
hist_2D.Fill(i+1, i+1, p[2])
# Formatting and drawing the graphs on the canvas
graph_list.append(deepcopy(graph))
hist_list_2D.append(deepcopy(hist_2D))
leg_list.append(deepcopy(legend))
index = len(graph_list) - 1
canvas_2D.cd(canvas_panel_num)
hist_2D.Draw("COLZ")
hist_list_2D[index].GetXaxis().SetTitle(x_axis)
hist_list_2D[index].GetXaxis().SetRangeUser(0, int(ceil(max_2D[0]*1.1)))
hist_list_2D[index].GetYaxis().SetTitle(lines)
hist_list_2D[index].GetYaxis().SetRangeUser(0, int(ceil(max_2D[1]*1.1)))
hist_list_2D[index].GetZaxis().SetTitle("#sigma_{Eff} / E")
canvas_2D.Update()
canvas.cd(canvas_panel_num)
graph_list[index].Draw("APL")
graph_list[index].GetXaxis().SetTitle(x_axis)
graph_list[index].GetYaxis().SetTitle("#sigma_{Eff} / E")
leg_list[index].Draw()
canvas_panel_num += 1
canvas_2D.Write()
canvas.Write()
As you can see, for both the graphs and the histograms, they are copied and accumulated in a list stored outside of loop in order to keep them in memory (or that was the idea at least). Any help on this would be much appreciated!