Exemple #1
0
def make1dPlot_plot(dataType, pad_plot, hists, title_hist, leg):
	#
	# producing the plot without ratio plot
	#
	# this function takes the following parameters:
	# dataType....type of the lepton ('mu', 'el')
	# pad_plot....pad to be plotted
	# hists.......list of histograms to be plotted
	# title_hist..histogram where we take the axis labels from
	# leg.........legend to be drawn


	pad_plot.cd()

	# get maximum

	max = hists[0][0].GetMaximum()
	for i in range(1,len(hists)):
		if max < hists[i][0].GetStack().Last().GetMaximum():
			max = hists[i][0].GetStack().Last().GetMaximum()

	# set standard plot stype

	hists[0][0] = helper.set1dPlotStyle(dataType, hists[0][0], helper.getColor(hists[0][1]), '', title_hist)

	# draw and set maximum

	hists[0][0].Draw("p x0 e")
	hists[0][0].SetMinimum(0.001)
	hists[0][0].SetMaximum(1.5*max)
	for i in range(1,len(hists)):
		hists[i][0].Draw("hist same")
		hists[i][0].SetMinimum(0.001)
		hists[i][0].SetMaximum(1.5*max)
	
	hists[0][0].Draw("p x0 e same")

	# draw legend

	leg.Draw()
Exemple #2
0
def PlotCompare(dataType, outputDir, mcsets, histname, leg, cutoff = -1, precise = False):
	#
	# plot a set of histograms defined by histname and comparing mc samples
	# NOTE: ALL SAMPLES MUST CONTAIN THE EXACT SAME LIST OF HISTOGRAMS, OTHERWISE THIS FUNCTION WILL NOT WORK
	#
	# this function takes the following parameters:
	# dataType....type of the lepton ('mu', 'el')
	# outputDir...basic output directory
	# mcsets......list of mc samples [mcsample1, mcsample2, ..]
	# leg.........legend to be plotted
	# cutoff......cutoff for the histogram name (see function getSaveName() in lib.py)
	# precise.....True if only the histogram with the name matching histname exactly shall be plotted

	# define canvas and pads

	canv = helper.makeCanvas(900, 675)
	pad_plot = helper.makePad('plot')
	pad_ratio = helper.makePad('ratio')
	pad_ratio.cd()

	# iterate over all histograms in root files
	# it does not matter which sample we iterate on, as all samples contain the same list of histograms

	for hist in mcsets[0].hists:

		# get index of histogram

		i = mcsets[0].hists.index(hist)
		pad_plot.cd()

		# if precise is True, we only plot the histogram with the name matching histname exactly
		# else, we plot all histograms which have histname in their name (useful for several versions or bins of a histogram)

		if precise     and not histname == hist.GetName(): continue
		if not precise and not histname in hist.GetName(): continue

		# pre- and postpends

		prepend = ''
		postpend = '_compare'
		if '_Loose_' in hist.GetName(): prepend = 'Loose_'
		if '_Tight_' in hist.GetName(): prepend = 'Tight_'

		# draw first histogram

		hist.Draw('hist')
		hist.SetFillStyle(0)
		hist.Scale(1.0/hist.Integral())
		max = hist.GetMaximum()

		for j in range(1,len(mcsets)):	
			mcsets[j].hists[i].Draw('hist same')
			mcsets[j].hists[i].SetFillStyle(0)
			mcsets[j].hists[i].SetLineStyle(2)
			mcsets[j].hists[i].Scale(1.0/mcsets[j].hists[i].Integral())
			if max < mcsets[j].hists[i].GetMaximum(): max = mcsets[j].hists[i].GetMaximum()

		# do some cosmetics

		hist.SetMinimum(0.0001)
		hist.SetMaximum(1.5 * max)
		hist = helper.set1dPlotStyle(dataType, hist, helper.getColor(mcsets[0].GetName()), '', hist, '1/Integral')

		# draw legend

		leg.Draw()

		# draw ratio plot

		pad_ratio.cd()
		hist_ratio = copy.deepcopy(hist)
		hist_ratio.Divide(copy.deepcopy(mcsets[1].hists[i]))
		hist_ratio.Draw("p e1")
		hist_ratio = helper.setRatioStyle(dataType, hist_ratio, hist)
		line = helper.makeLine(hist_ratio.GetXaxis().GetXmin(), 1.00, hist_ratio.GetXaxis().GetXmax(), 1.00)
		line.Draw()

		# make plot

		helper.saveCanvas(canv, pad_plot, outputDir + "compare/", prepend + helper.getSaveName(hist, cutoff) + postpend)