def checkOnMC(unfolding, method): global bins, nbins RooUnfold.SVD_n_toy = 1000 pulls = [] for sub in range(1,9): inputFile2 = File('../data/unfolding_merged_sub%d.root' % sub, 'read') h_data = asrootpy(inputFile2.unfoldingAnalyserElectronChannel.measured.Rebin(nbins, 'measured', bins)) nEvents = inputFile2.EventFilter.EventCounter.GetBinContent(1) lumiweight = 164.5 * 5050 / nEvents # print sub, nEvents h_data.Scale(lumiweight) doUnfoldingSequence(unfolding, h_data, method, '_sub%d' %sub) pull = unfolding.pull_inputErrorOnly() # unfolding.printTable() pulls.append(pull) unfolding.Reset() allpulls = [] for pull in pulls: allpulls.extend(pull) h_allpulls = Hist(100,-30,30) filling = h_allpulls.Fill for entry in allpulls: filling(entry) fit = h_allpulls.Fit('gaus', 'WWS') h_fit = asrootpy(h_allpulls.GetFunction("gaus").GetHistogram()) canvas = Canvas(width=1600, height=1000) canvas.SetLeftMargin(0.15) canvas.SetBottomMargin(0.15) canvas.SetTopMargin(0.10) canvas.SetRightMargin(0.05) h_allpulls.Draw() fit.Draw('same') canvas.SaveAs('plots/Pull_allBins_withFit.png') plt.figure(figsize=(16, 10), dpi=100) rplt.errorbar(h_allpulls, label=r'Pull distribution for all bins', emptybins=False) rplt.hist(h_fit, label=r'fit') plt.xlabel('(unfolded-true)/error', CMS.x_axis_title) plt.ylabel('entries', CMS.y_axis_title) plt.title('Pull distribution for all bins', CMS.title) plt.tick_params(**CMS.axis_label_major) plt.tick_params(**CMS.axis_label_minor) plt.legend(numpoints=1) plt.savefig('plots/Pull_allBins.png') #individual bins for bin_i in range(nbins): h_pull = Hist(100,-30,30) for pull in pulls: h_pull.Fill(pull[bin_i]) plt.figure(figsize=(16, 10), dpi=100) rplt.errorbar(h_pull, label=r'Pull distribution for bin %d' % (bin_i + 1), emptybins=False) plt.xlabel('(unfolded-true)/error', CMS.x_axis_title) plt.ylabel('entries', CMS.y_axis_title) plt.title('Pull distribution for bin %d' % (bin_i + 1), CMS.title) plt.savefig('Pull_bin_%d.png' % (bin_i + 1))
def CreatCanvas(self): 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 # Set the tdr style canvas = Canvas(width=W, height=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
# easily set visual attributes h_simple.linecolor = 'blue' h_simple.fillcolor = 'green' h_simple.fillstyle = '/' # attributes may be accessed in the same way print(h_simple.name) print(h_simple.title) print(h_simple.markersize) # plot canvas = Canvas(width=700, height=500) canvas.SetLeftMargin(0.15) canvas.SetBottomMargin(0.15) canvas.SetTopMargin(0.10) canvas.SetRightMargin(0.05) h_simple.Draw() # create the legend legend = Legend([h_simple], pad=canvas, header='Header', leftmargin=0.05, rightmargin=0.5) legend.Draw() # 2D and 3D histograms are handled in the same way # the constructor arguments are repetitions of #bins, left bound, right bound. h2d = Hist2D(10, 0, 1, 50, -40, 10, name='2d hist') h3d = Hist3D(3, -1, 4, 10, -1000, -200, 2, 0, 1, name='3d hist')
print variable + " " + min + " " + max + " " + nbins + " " + title + " " + ytitle for (xvariable, xmin, xmax, xnbins, xtitle, xytitle, xplot) in zip(xvariable_list, xmin_list, xmax_list, xnbins_list, xtitle_list, xytitle_list, xplot_list): if xplot == "Plot": if xvariable == variable: continue # Canvas Variables and declaration print xvariable + " " + xmin + " " + xmax + " " + xnbins + " " + xtitle + " " + xytitle canvas = Canvas(width=700, height=500) #canvas.SetLeftMargin(0.1) canvas.SetRightMargin(0.15) canvas.SetTopMargin(0.05) canvas.Draw() #canvas.SetLogy() canvas.SetGrid() histname = "hist" + xvariable + "_" + variable + background_name print histname histogram = histname + "(" + xnbins + "," + xmin + "," + xmax + "," + nbins + "," + min + "," + max + ")" print histogram histname = compressed.Draw(xvariable + ":" + variable + ">>" + histogram, selection=weight * total, drawstyle='COLZ') histname.set_title(signalprocess + " " + background_name)
def plot_central_and_systematics(channel): global variable, translate_options, k_value, b_tag_bin, maximum, categories ROOT.TH1.SetDefaultSumw2(False) ROOT.gROOT.SetBatch(True) ROOT.gROOT.ProcessLine('gErrorIgnoreLevel = 1001;') plotting.setStyle() gStyle.SetTitleYOffset(1.4) ROOT.gROOT.ForceStyle() canvas = Canvas(width=700, height=500) canvas.SetLeftMargin(0.15) canvas.SetBottomMargin(0.15) canvas.SetTopMargin(0.05) canvas.SetRightMargin(0.05) legend = plotting.create_legend(x0=0.6, y1=0.5) hist_data_central = read_xsection_measurement_results( 'central', channel)[0]['unfolded'] hist_data_central.GetXaxis().SetTitle(translate_options[variable] + ' [GeV]') hist_data_central.GetYaxis().SetTitle('#frac{1}{#sigma} #frac{d#sigma}{d' + translate_options[variable] + '} [GeV^{-1}]') hist_data_central.GetXaxis().SetTitleSize(0.05) hist_data_central.GetYaxis().SetTitleSize(0.05) hist_data_central.SetMinimum(0) hist_data_central.SetMaximum(maximum[variable]) hist_data_central.SetMarkerSize(1) hist_data_central.SetMarkerStyle(20) # plotAsym = TGraphAsymmErrors(hist_data) # plotStatErr = TGraphAsymmErrors(hist_data) gStyle.SetEndErrorSize(20) hist_data_central.Draw('P') # plotStatErr.Draw('same P') # plotAsym.Draw('same P Z') legend.AddEntry(hist_data_central, 'measured (unfolded)', 'P') for systematic in categories: if systematic != 'central': hist_data_systematic = read_xsection_measurement_results( systematic, channel)[0]['unfolded'] hist_data_systematic.SetMarkerSize(0.5) hist_data_systematic.SetMarkerStyle(20) colour_number = categories.index(systematic) + 1 if colour_number == 10: colour_number = 42 hist_data_systematic.SetMarkerColor(colour_number) hist_data_systematic.Draw('same P') legend.AddEntry(hist_data_systematic, systematic, 'P') # for central_generator in ['MADGRAPH', 'POWHEG', 'MCATNLO']: # hist_MC = read_xsection_measurement_results('central', channel)[0][central_generator] # hist_MC.SetLineStyle(7) # hist_MC.SetLineWidth(2) # #setting colours # if central_generator == 'POWHEG': # hist_MC.SetLineColor(kBlue) # elif central_generator == 'MADGRAPH': # hist_MC.SetLineColor(kRed + 1) # elif central_generator == 'MCATNLO': # hist_MC.SetLineColor(kMagenta + 3) # hist_MC.Draw('hist same') #legend.AddEntry(hist_MC, translate_options[central_generator], 'l') legend.Draw() mytext = TPaveText(0.5, 0.97, 1, 1.01, "NDC") channelLabel = TPaveText(0.18, 0.97, 0.5, 1.01, "NDC") if channel == 'electron': channelLabel.AddText( "e, %s, %s, k_v = %s" % ("#geq 4 jets", b_tag_bins_latex[b_tag_bin], k_value)) elif channel == 'muon': channelLabel.AddText( "#mu, %s, %s, k_v = %s" % ("#geq 4 jets", b_tag_bins_latex[b_tag_bin], k_value)) else: channelLabel.AddText( "combined, %s, %s, k_v = %s" % ("#geq 4 jets", b_tag_bins_latex[b_tag_bin], k_value)) mytext.AddText("CMS Preliminary, L = %.1f fb^{-1} at #sqrt{s} = 8 TeV" % (5.8)) mytext.SetFillStyle(0) mytext.SetBorderSize(0) mytext.SetTextFont(42) mytext.SetTextAlign(13) channelLabel.SetFillStyle(0) channelLabel.SetBorderSize(0) channelLabel.SetTextFont(42) channelLabel.SetTextAlign(13) mytext.Draw() if not channel == 'combination': channelLabel.Draw() canvas.Modified() canvas.Update() path = save_path + '/' + variable make_folder_if_not_exists(path) canvas.SaveAs(path + '/normalised_xsection_' + channel + '_altogether_kv' + str(k_value) + '.png') canvas.SaveAs(path + '/normalised_xsection_' + channel + '_altogether_kv' + str(k_value) + '.pdf')
def make_plots_ROOT(histograms, category, save_path, histname, channel): global variable, translateOptions, k_value, b_tag_bin, maximum ROOT.TH1.SetDefaultSumw2(False) ROOT.gROOT.SetBatch(True) ROOT.gROOT.ProcessLine('gErrorIgnoreLevel = 1001;') plotting.setStyle() gStyle.SetTitleYOffset(2.) ROOT.gROOT.ForceStyle() canvas = Canvas(width=700, height=500) canvas.SetLeftMargin(0.18) canvas.SetBottomMargin(0.15) canvas.SetTopMargin(0.05) canvas.SetRightMargin(0.05) legend = plotting.create_legend(x0=0.6, y1=0.5) hist_data = histograms['unfolded'] hist_data.GetXaxis().SetTitle(translate_options[variable] + ' [GeV]') hist_data.GetYaxis().SetTitle('#frac{1}{#sigma} #frac{d#sigma}{d' + translate_options[variable] + '} [GeV^{-1}]') hist_data.GetXaxis().SetTitleSize(0.05) hist_data.GetYaxis().SetTitleSize(0.05) hist_data.SetMinimum(0) hist_data.SetMaximum(maximum[variable]) hist_data.SetMarkerSize(1) hist_data.SetMarkerStyle(8) plotAsym = TGraphAsymmErrors(hist_data) plotStatErr = TGraphAsymmErrors(hist_data) xsections = read_unfolded_xsections(channel) bins = variable_bins_ROOT[variable] assert (len(bins) == len(xsections['central'])) for bin_i in range(len(bins)): scale = 1 # / width centralresult = xsections['central'][bin_i] fit_error = centralresult[1] uncertainty = calculateTotalUncertainty(xsections, bin_i) uncertainty_total_plus = uncertainty['Total+'][0] uncertainty_total_minus = uncertainty['Total-'][0] uncertainty_total_plus, uncertainty_total_minus = symmetriseErrors( uncertainty_total_plus, uncertainty_total_minus) error_up = sqrt(fit_error**2 + uncertainty_total_plus**2) * scale error_down = sqrt(fit_error**2 + uncertainty_total_minus**2) * scale plotStatErr.SetPointEYhigh(bin_i, fit_error * scale) plotStatErr.SetPointEYlow(bin_i, fit_error * scale) plotAsym.SetPointEYhigh(bin_i, error_up) plotAsym.SetPointEYlow(bin_i, error_down) gStyle.SetEndErrorSize(20) plotAsym.SetLineWidth(2) plotStatErr.SetLineWidth(2) hist_data.Draw('P') plotStatErr.Draw('same P') plotAsym.Draw('same P Z') legend.AddEntry(hist_data, 'unfolded', 'P') hist_measured = histograms['measured'] hist_measured.SetMarkerSize(1) hist_measured.SetMarkerStyle(20) hist_measured.SetMarkerColor(2) #hist_measured.Draw('same P') #legend.AddEntry(hist_measured, 'measured', 'P') for key, hist in sorted(histograms.iteritems()): if not 'unfolded' in key and not 'measured' in key: hist.SetLineStyle(7) hist.SetLineWidth(2) # setting colours if 'POWHEG' in key or 'matchingdown' in key: hist.SetLineColor(kBlue) elif 'MADGRAPH' in key or 'matchingup' in key: hist.SetLineColor(kRed + 1) elif 'MCATNLO' in key or 'scaleup' in key: hist.SetLineColor(kGreen - 3) elif 'scaledown' in key: hist.SetLineColor(kMagenta + 3) hist.Draw('hist same') legend.AddEntry(hist, translate_options[key], 'l') legend.Draw() mytext = TPaveText(0.5, 0.97, 1, 1.01, "NDC") channelLabel = TPaveText(0.18, 0.97, 0.5, 1.01, "NDC") if 'electron' in histname: channelLabel.AddText( "e, %s, %s, k = %s" % ("#geq 4 jets", b_tag_bins_latex[b_tag_bin], k_value)) elif 'muon' in histname: channelLabel.AddText( "#mu, %s, %s, k = %s" % ("#geq 4 jets", b_tag_bins_latex[b_tag_bin], k_value)) else: channelLabel.AddText( "combined, %s, %s, k = %s" % ("#geq 4 jets", b_tag_bins_latex[b_tag_bin], k_value)) mytext.AddText("CMS Preliminary, L = %.1f fb^{-1} at #sqrt{s} = 8 TeV" % (5.8)) mytext.SetFillStyle(0) mytext.SetBorderSize(0) mytext.SetTextFont(42) mytext.SetTextAlign(13) channelLabel.SetFillStyle(0) channelLabel.SetBorderSize(0) channelLabel.SetTextFont(42) channelLabel.SetTextAlign(13) mytext.Draw() channelLabel.Draw() canvas.Modified() canvas.Update() path = save_path + '/' + variable + '/' + category make_folder_if_not_exists(path) canvas.SaveAs(path + '/' + histname + '_kv' + str(k_value) + '.png') canvas.SaveAs(path + '/' + histname + '_kv' + str(k_value) + '.pdf')
def plot_fit_results(histograms, category, channel): global variable, translate_options, b_tag_bin, save_path #ROOT.TH1.SetDefaultSumw2(False) ROOT.gROOT.SetBatch(True) ROOT.gROOT.ProcessLine('gErrorIgnoreLevel = 1001;') plotting.setStyle() gStyle.SetTitleYOffset(1.4) ROOT.gROOT.ForceStyle() for variable_bin in variable_bins_ROOT[variable]: path = save_path + '/' + variable + '/' + category + '/fit_results/' make_folder_if_not_exists(path) plotname = path + channel + '_bin_' + variable_bin + '.png' # check if template plots exist already if os.path.isfile(plotname): continue canvas = Canvas(width=700, height=500) canvas.SetLeftMargin(0.15) canvas.SetBottomMargin(0.15) canvas.SetTopMargin(0.05) canvas.SetRightMargin(0.05) legend = plotting.create_legend(x0=0.7, y1=0.8) h_data = histograms[variable_bin]['data'] h_signal = histograms[variable_bin]['signal'] h_background = histograms[variable_bin]['background'] h_data.GetXaxis().SetTitle('Lepton #eta') h_data.GetYaxis().SetTitle('Number of Events') h_data.GetXaxis().SetTitleSize(0.05) h_data.GetYaxis().SetTitleSize(0.05) h_data.SetMinimum(0) h_data.SetMarkerSize(1) h_data.SetMarkerStyle(20) gStyle.SetEndErrorSize(20) h_data.Draw('P') h_signal.SetFillColor(kRed + 1) h_background.SetFillColor(kGreen - 3) h_signal.SetLineWidth(2) h_background.SetLineWidth(2) h_signal.SetFillStyle(1001) h_background.SetFillStyle(1001) mcStack = THStack("MC", "MC") mcStack.Add(h_background) mcStack.Add(h_signal) mcStack.Draw('hist same') h_data.Draw('error P same') legend.AddEntry(h_data, 'data', 'P') legend.AddEntry(h_signal, 'signal', 'F') legend.AddEntry(h_background, 'background', 'F') legend.Draw() mytext = TPaveText(0.5, 0.97, 1, 1.01, "NDC") channelLabel = TPaveText(0.18, 0.97, 0.5, 1.01, "NDC") if channel == 'electron': channelLabel.AddText("e, %s, %s" % ("#geq 4 jets", b_tag_bins_latex[b_tag_bin])) elif channel == 'muon': channelLabel.AddText("#mu, %s, %s" % ("#geq 4 jets", b_tag_bins_latex[b_tag_bin])) else: channelLabel.AddText("combined, %s, %s" % ("#geq 4 jets", b_tag_bins_latex[b_tag_bin])) mytext.AddText( "CMS Preliminary, L = %.1f fb^{-1} at #sqrt{s} = 8 TeV" % (5.8)) mytext.SetFillStyle(0) mytext.SetBorderSize(0) mytext.SetTextFont(42) mytext.SetTextAlign(13) channelLabel.SetFillStyle(0) channelLabel.SetBorderSize(0) channelLabel.SetTextFont(42) channelLabel.SetTextAlign(13) mytext.Draw() channelLabel.Draw() canvas.Modified() canvas.Update() canvas.SaveAs(plotname) canvas.SaveAs(plotname.replace('png', 'pdf'))
def make_template_plots(histograms, category, channel): global variable, translate_options, b_tag_bin, save_path ROOT.TH1.SetDefaultSumw2(False) ROOT.gROOT.SetBatch(True) ROOT.gROOT.ProcessLine('gErrorIgnoreLevel = 1001;') plotting.setStyle() gStyle.SetTitleYOffset(1.4) ROOT.gROOT.ForceStyle() for variable_bin in variable_bins_ROOT[variable]: path = save_path + '/' + variable + '/' + category + '/fit_templates/' make_folder_if_not_exists(path) plotname = path + channel + '_templates_bin_' + variable_bin + '.png' # check if template plots exist already if os.path.isfile(plotname): continue canvas = Canvas(width=700, height=500) canvas.SetLeftMargin(0.15) canvas.SetBottomMargin(0.15) canvas.SetTopMargin(0.05) canvas.SetRightMargin(0.05) legend = plotting.create_legend(x0=0.7, y1=0.8) h_signal = histograms[variable_bin]['signal'] h_VJets = histograms[variable_bin]['V+Jets'] h_QCD = histograms[variable_bin]['QCD'] h_signal.GetXaxis().SetTitle('Lepton #eta') h_signal.GetYaxis().SetTitle('Normalised Events') h_signal.GetXaxis().SetTitleSize(0.05) h_signal.GetYaxis().SetTitleSize(0.05) h_signal.SetMinimum(0) h_signal.SetMaximum(0.2) h_signal.SetLineWidth(2) h_VJets.SetLineWidth(2) h_QCD.SetLineWidth(2) h_signal.SetLineColor(kRed + 1) h_VJets.SetLineColor(kBlue) h_QCD.SetLineColor(kYellow) h_signal.Draw('hist') h_VJets.Draw('hist same') h_QCD.Draw('hist same') legend.AddEntry(h_signal, 'signal', 'l') legend.AddEntry(h_VJets, 'V+Jets', 'l') legend.AddEntry(h_QCD, 'QCD', 'l') legend.Draw() mytext = TPaveText(0.5, 0.97, 1, 1.01, "NDC") channelLabel = TPaveText(0.18, 0.97, 0.5, 1.01, "NDC") if channel == 'electron': channelLabel.AddText("e, %s, %s" % ("#geq 4 jets", b_tag_bins_latex[b_tag_bin])) elif channel == 'muon': channelLabel.AddText("#mu, %s, %s" % ("#geq 4 jets", b_tag_bins_latex[b_tag_bin])) else: channelLabel.AddText("combined, %s, %s" % ("#geq 4 jets", b_tag_bins_latex[b_tag_bin])) mytext.AddText( "CMS Preliminary, L = %.1f fb^{-1} at #sqrt{s} = 8 TeV" % (5.8)) mytext.SetFillStyle(0) mytext.SetBorderSize(0) mytext.SetTextFont(42) mytext.SetTextAlign(13) channelLabel.SetFillStyle(0) channelLabel.SetBorderSize(0) channelLabel.SetTextFont(42) channelLabel.SetTextAlign(13) mytext.Draw() channelLabel.Draw() canvas.Modified() canvas.Update() canvas.SaveAs(plotname) canvas.SaveAs(plotname.replace('png', 'pdf'))
else: expr = expr.format( var_info['name'], h.name, (var_info['bins'], var_info['range'][0], var_info['range'][1])) print expr tree.Draw(expr, cut, var_info['style']) h = asrootpy(ROOT.gPad.GetPrimitive(h.name)) h.xaxis.title = get_label(var_info) h.yaxis.title = 'Number of Events' if h.integral() != 0: h /= h.integral() h.SetLineWidth(2) h.yaxis.SetRangeUser(0, 0.5) if 'binlabels' in var_info.keys(): for ib, lab in enumerate(var_info['binlabels']): h.xaxis.SetBinLabel(ib + 1, lab) c = Canvas() # c.SetLogy() c.SetTopMargin(0.08) h.Draw('HIST') label = ROOT.TLatex( c.GetLeftMargin() + 0.05, 1 - c.GetTopMargin() + 0.02, '{0}H #rightarrow #tau#tau (m_{{H}} = {1} GeV) - Hist Integral = {2}'.format( get_dir_mode(d), get_dir_mass(d), h.Integral())) label.SetNDC() label.SetTextSize(20) label.Draw('same') c.SaveAs('./plots/{0}_mass{1}_mode{2}.png'.format( key, get_dir_mass(d), get_dir_mode(d)))