def make_plots(data, img_name): """ make_plot(rates) """ x_order = (0, 0.5, 1, 5) x = array('f', x_order) x_er = array('f', [k*0.01 for k in x_order]) # assumed errors cu = array('f', [data[k].get_rate("cu").value for k in x_order]) cu_er = array('f', [data[k].get_rate("cu").error for k in x_order]) f = array('f', [data[k].get_rate("f").value for k in x_order]) f_er = array('f', [data[k].get_rate("f").error for k in x_order]) cu_graph = TGraphErrors(len(x), x, cu, x_er, cu_er) set_graph_values(cu_graph, "Simulated copper muon rates", "degrader thickness (mm)", "Muons per proton") f_graph = TGraphErrors(len(x), x, f, x_er, f_er) set_graph_values(f_graph, "Simulated free muon rates", "degrader thickness (mm)", "Muons per proton") canvas = make_canvas("Simulated muon rates (from direct counts)", n_x=2, n_y=1, resize=True) canvas.cd(1) cu_graph.Draw("ALP") canvas.cd(2) f_graph.Draw("ALP") canvas.Update() canvas.SaveAs(img_name+".svg") canvas.SaveAs(img_name+".png") sleep (5)
def main(): gStyle.SetOptStat(10) gStyle.SetOptFit(111) save_header() root_file = TFile(img_dir + "out.root", "RECREATE"); for run in run_info: tree = init_tree_and_hists(run) fill_hists(tree) for ch in channels: hist = tree.hists[ch] root_file.Add(hist); print hist.GetTitle(), hist.GetEntries() can = make_canvas(hist.GetTitle(),resize=True) hist.Draw("E") fit_hist(hist) # print "\nChi^2/NDF {:.1f} / {}\n\n".format(hist.fit_func.GetChisquare(), hist.fit_func.GetNDF()) can.Update() img_name = img_fmt.format(id=hist.file_id, ch=hist.ch) can.SaveAs(img_name+".eps") can.SaveAs(img_name+".svg") if debug: sleep(2) save_fit_info(hist) root_file.Write(); root_file.Close();
def make_and_save_count_hist(target, data, data_type, img_dir, out_root_file): name = "Count " if data_type == "run" else "Simulated count " name += "of muons decaying in "+target hist = make_count_hist(name, target, data) canvas = make_canvas(name, resize=True) hist.Draw() img_name = img_dir+"counts/"+data_type+"_muon_count_in_"+target canvas.SaveAs(img_name+".svg") canvas.SaveAs(img_name+".eps") return hist
def save_hist_around_zero_region(data_file, img_dir, l_bound=-200, u_bound=200): for hist_key, hist in data_file.hists.items(): img_name = img_dir+"zoom/{}_{}".format(data_file.short_name, hist.GetName()) can = make_canvas(img_name) hist.GetXaxis().SetRangeUser(l_bound, u_bound) hist.Draw() can.Update() can.SaveAs(img_name+".eps") can.SaveAs(img_name+".svg") hist.GetXaxis().UnZoom() # reset the ranges to full
def draw_pretty_plots(rates): canvas = make_canvas("Trigger Rate", 3,2, True) # attach all the plots to the canvas to keep them in scope canvas.plots = [] for pad_id, (file_id, data) in enumerate(rates.items(), 1): canvas.cd(pad_id) canvas.plots.append(get_plot(file_id, data)) canvas.plots[-1].Draw("ALP") canvas.Update() # Don't let the canvas go out of scope return canvas
def main(): gStyle.SetOptStat(10) gStyle.SetOptFit(111) materials = {"Cu":{"Z":29,"A":63}, "Al":{"Z":13,"A":26}} for mat, element in materials.items(): hist = get_hist(**element) can = make_canvas(mat) hist.Draw() initial_settings = (("N", lambda x: 100, lambda x: 0.0 , lambda x: 1e6), ("#tau", lambda x: 100, lambda x: 0.0 , lambda x: 5000)) fit_histogram(hist, "[0]*exp(-x/[1])", initial_settings, mat,) can.Update()
def main(): gStyle.SetOptFit() res = {'data':{}, 'sec':{}} for f_id in file_ids: print "Processing", f_id filename = file_prefix + f_id + file_suffix treename = "ts" if f_id == "448" else "Scaler" tree = get_tree_from_file(treename, filename) print tree.filename, tree load_branch(tree, f_id) # tfile, tree, branch = get_tfile_tree_and_branch(name, **file_data) key = int(f_id) res['data'][key] = get_gain_stability_dict(tree) res['sec'][key] = get_sec_dict(tree) # tfile.Close() canvas = make_canvas("Gain Stability", 3,2, True) # add the histograms to the dictionary res.update({'gain_hist':{}, 'count_hist':{}}) for pad_id, (file_id, data) in enumerate(res['data'].items(), 1): gain_hist = get_gain_stability_hist_for_file(data, file_id) # count_hist = get_trigger_count_hist_for_file(data, file_id) # potential_hist = get_potential_trigger_count_hist_for_file(data, file_id) sec_hist = get_sec_count_hist_for_file(res['sec'][int(file_id)], file_id) # c1 = make_canvas('foobar', 1, 1, True) # c1.cd() # sec_hist.Draw() # c1.SaveAs("foobar%i.png"%file_id) # continue full_range = max(data.keys())[0] - min(data.keys())[0] # Fit the central 80% fit_min = full_range * 0.05 fit_max = full_range * 0.95 print fit_min, fit_max # gain_hist.Fit("pol0") # attempt to fit the gain with a flat function gain_hist.Fit("pol0", "", "", fit_min, fit_max) # attempt to fit the gain with a flat function # draw_gain_and_count_hist(gain_hist, count_hist, canvas, pad_id) # draw_gain_and_count_hist(gain_hist, potential_hist, canvas, pad_id) draw_gain_and_count_hist(gain_hist, sec_hist, canvas, pad_id) res['gain_hist'][file_id] = gain_hist res['count_hist'][file_id] = sec_hist canvas.SaveAs("images/gain_stability.svg") canvas.SaveAs("images/gain_stability.eps")
def fit_histogram(hist, func_fmt, initial_settings, func_name, fit_options="MER",img_name=""): func = get_func_with_named_initialised_param(func_name, hist, func_fmt, initial_settings) if img_name: canvas = make_canvas(img_name, resize=True) hist.Draw() hist.Fit(func, fit_options) canvas.Update() canvas.SaveAs(img_name+".eps") canvas.SaveAs(img_name+".svg") else: hist.Fit(func, fit_options+"N") hist.func = func hist.fit_param = get_fit_parameters(func)
def main(): gStyle.SetOptStat(10) gStyle.SetOptFit(111) for run in run_info: tree = init_tree(run) fill_hists(tree) for hist in tree.hists.values(): print hist.GetTitle(), hist.GetEntries() can = make_canvas(hist.GetTitle(),resize=True) hist.Draw() fit_hist(hist) print "\nChi^2/NDF {:.1f} / {}".format(hist.fit_func.GetChisquare(), hist.fit_func.GetNDF()) can.Update() can.SaveAs("images/noise_fit_zoom.png") if debug: sleep(5)
def fit_data(data_file, fit_type, data_type, inc_phase, inc_sin, l_bound, u_bound, img_dir="", fit_options="ILMER"): data_file.sum_integrals = {'f':0.0, 'cu':0.0} short_name = data_file.short_name bin_width = data_file.bin_width for hist_key, hist in data_file.hists.items(): func_fmt, initial_settings = get_fit_func_and_settings(data_type, hist, fit_type, inc_phase, inc_sin) func_name = "{}_{}".format(short_name, hist.GetName()) if img_dir: can = make_canvas(func_name, resize=True) hist.Draw() fit_histogram(hist, func_fmt, initial_settings, func_name, fit_options, l_bound, u_bound) can.Update() img_name = img_dir+"fits/"+func_name can.SaveAs(img_name+".eps") can.SaveAs(img_name+".svg") else: fit_histogram(hist, func_fmt, initial_settings, func_name, fit_options+"N") calculate_exp_integrals(hist, l_bound, u_bound) for k in data_file.sum_integrals: data_file.sum_integrals[k] += hist.integrals[k]