Example #1
0
def make1dPlot_ratio(dataType, pad_ratio, hists, title_hist):
	#
	# producing the ratio plot only
	#
	# this function takes the following parameters:
	# dataType....type of the lepton ('mu', 'el')
	# pad_ratio...pad carrying the ratio plot
	# hists.......list of histograms to be plotted
	# title_hist..histogram where we take the axis labels from

	pad_ratio.cd()

	# create ratio and draw it

	data_bg_ratio = copy.deepcopy(hists[0][0])
	data_bg_ratio.Divide(copy.deepcopy(hists[1][0].GetStack().Last()))
	data_bg_ratio.Draw("p e")

	# set standard ratio plot stype 

	data_bg_ratio = helper.setRatioStyle(dataType, data_bg_ratio, title_hist)

	# draw line at 1.0 ratio

	line = helper.makeLine(data_bg_ratio.GetXaxis().GetXmin(), 1.00, data_bg_ratio.GetXaxis().GetXmax(), 1.00)
	line.Draw()
Example #2
0
def make1dPlot(dataType, canv, pad_plot, pad_ratio, outputDir, hists, title_hist, file_name, leg):
	#
	# a given canvas, which carries a 1d histogram, is plotted (plot + ratio)
	#
	# this function takes the following parameters:
	# dataType....type of the lepton ('mu', 'el')
	# canv........canvas to be plotted
	# pad_plot....pad to be plotted
	# pad_ratio...pad carrying the ratio plot
	# outputDir...basic output directory
	# hists.......list of histograms to be plotted
	# title_hist..histogram where we take the axis labels from
	# file_name...filename of the plot to be created
	# leg.........legend to be drawn


	# create PLOT

	make1dPlot_plot(dataType, pad_plot, hists, title_hist, leg) 

	# create RATIO 

	#make1dPlot_ratio(dataType, pad_ratio, hists, title_hist)
	# calling the function to produce the ratio plot results in a malfunction i do not understand properly
	# so we copy the lines from make1dPlot_ratio() while we still keep that function for other purposes

	pad_ratio.cd()
	data_bg_ratio = copy.deepcopy(hists[0][0])
	data_bg_ratio.Divide(copy.deepcopy(hists[1][0].GetStack().Last()))
	data_bg_ratio.Draw("p e")
	data_bg_ratio = helper.setRatioStyle(dataType, data_bg_ratio, title_hist)
	line = helper.makeLine(data_bg_ratio.GetXaxis().GetXmin(), 1.00, data_bg_ratio.GetXaxis().GetXmax(), 1.00)
	line.Draw()

	# save plot

	ROOT.gPad.RedrawAxis()
	helper.saveCanvas(canv, pad_plot, outputDir + "plots_1d/", file_name)
Example #3
0
def canvasWithRatio(stack, histo, legend):
	c1 = ROOT.TCanvas('canvas', 'canvas', 675, 675)
	pad_plot  = lib.makePad('plot')
	pad_ratio = lib.makePad('ratio')
	pad_plot.SetTicks(1,1)
	pad_ratio.SetTicks(1,1)

	stackcp = copy.deepcopy(stack)
	stackhisto = copy.deepcopy(stackcp.GetStack().Last())
	hlist = stackcp.GetHists()
	newstack = ROOT.THStack('st', 'st')
	for i in hlist:
		i.Scale(1./stackhisto.Integral())
		newstack.Add(copy.deepcopy(i))
	
	pad_plot.cd()
	stackcp.Draw('hist')
	stackcp.GetYaxis().SetLabelSize(0.05)

	histoerr = copy.deepcopy(stackhisto)
	histoerr.SetFillColor(ROOT.kGray+3)
	histoerr.SetLineColor(ROOT.kGray+3)
	histoerr.SetFillStyle(3004)
	histoerr.Draw('same e2')

	#newstack.Draw('hist')
	histo.SetMarkerStyle(20)
	histo.SetMarkerSize(0.9)
	histo.SetMarkerColor(ROOT.kBlack)
	histo.SetLineColor(ROOT.kBlack)
	histonorm = copy.deepcopy(histo)
	#histonorm.Scale(1./histonorm.Integral())
	histonorm.Draw('same pe')

	stackmax = stackcp.GetMaximum()
	histomax = histo.GetMaximum()
	stackcp.SetMaximum(1.15* max(stackmax, histomax) )

	legend.Draw('same')
	
	pad_ratio.cd()
	#stackhisto.Scale(1./stackhisto.Integral())
	histocp = copy.deepcopy(histo)
	histocp.SetName('ratio')
	histocp.SetStats(0)
	#histocp.Scale(1./histocp.Integral())
	histocp.Divide(stackhisto)
	histocp.GetYaxis().SetRangeUser(0.00, 2.0)
	histocp.Draw('pe')
	histocp.GetYaxis().SetNdivisions(505)
	histocp.GetYaxis().SetLabelSize(0.08)
	histocp.GetXaxis().SetLabelSize(0.12)
	histocp.GetXaxis().SetTitle(histocp.GetName().split('_')[0])
	histocp.GetXaxis().SetTitleOffset(1.15)
	histocp.GetXaxis().SetTitleSize(0.15)
	histocp.SetTitle('')

	fl = ROOT.TF1("fl","[0]*x+[1]",0.,1.)
	histocp.Fit(fl)
	fl.Draw('same')

	line = lib.makeLine(histocp.GetXaxis().GetXmin(), 1., histocp.GetXaxis().GetXmax(), 1.)
	line.Draw('same')
	pad_ratio.Draw()
	pad_ratio.Update()
	c1.Update()
	return c1, stackhisto, histocp, stackcp, newstack, histonorm, line, histoerr
Example #4
0
def make1dFRPlot(dataType, canv, pad_plot, pad_ratio, outputDir, hists, title_hist, file_name, usemarkers = False, ratio_title = '', ratio_max = 1.99, ratio_min = 0.0):
	#
	# produces all 1d fake rate plots (this function, among others, wants to be improved)
	# NOTE: when calling this function make sure that the first histogram in hists is the total data, and the second is the total bg (important for ratio)
	#
	# this function takes the following paramters:
	# dataType..........type of the lepton ('mu', 'el')
	# canv..............canvas to be plotted
	# pad_plot..........pad of the plot to be plotted
	# pad_ratio.........pad of the ratio to be plotted
	# outputDir.........basic output directory
	# hists.............list of histograms to be plotted to be plotted on top of each other
	# title_hist........histogram to get the X axis title from
	# file_name.........name of the output file
	# usemarkers........True if we want to plot both data and mc with markers (e.g. comparing different ewk subtraction methods), False then mc is plotted as a colored rectangle
	# ratio_title.......Y axis title of the ratio plot
	# ratio_max.........maximum Y range of the ratio plot
	# ratio_min.........minimum Y range of the ratio plot


	# define pads, draw options (we use markers for mc if 

	pad_plot.cd()
	
	if usemarkers:
		markersize_mc = 1.4
		drawoption_data = "p e1"
		drawoption_mc = "p e1 same"
		legendoption_mc = "pe"
	else:
		markersize_mc = 0
		drawoption_data = "p e1 x0"
		drawoption_mc = "e2 same"
		legendoption_mc = "f"

	# draw histograms

	hists[0][0].Draw(drawoption_data)
	hists[0][0].SetMarkerSize(1.4)
	for i in range(1,len(hists)):
		hists[i][0].SetMarkerSize(markersize_mc)
		hists[i][0].Draw(drawoption_mc)
	hists[0][0].Draw(drawoption_data + " same")

	# set plot style
	
	for i in range(len(hists)):
		hists[i][0] = helper.setFRPlotStyle(dataType, hists[i][0], helper.getColor(hists[i][1]))
		if i+1 == len(hists): 
			hists[i][0] = helper.setFRPlotStyle(dataType, hists[i][0], helper.getColor(hists[i][1]), 'FR as function of ' + helper.getXTitle(dataType, title_hist), title_hist)

	# define legend, fill and draw it

	leg1 = helper.makeLegend(0.22, 0.4, 0.47, 0.65)
	#if dataType == 'el': leg1 = helper.makeLegend(0.62, 0.08, 0.87, 0.33)
	#else               : leg1 = helper.makeLegend(0.22, 0.6, 0.47, 0.85)

	leg1.AddEntry(hists[0][0], helper.getLegendName(hists[0][1]), 'pe')
	for i in range(1,len(hists)):
		leg1.AddEntry(hists[i][0], helper.getLegendName(hists[i][1]), legendoption_mc)

	leg1.Draw()


	# create RATIO PLOT

	pad_ratio.cd()
	data_bg_ratio = copy.deepcopy(hists[0][0])
	data_bg_ratio.Divide(hists[1][0])
	data_bg_ratio.Draw("p e")
	data_bg_ratio = helper.setRatioStyle(dataType, data_bg_ratio, title_hist, ratio_title, ratio_max, ratio_min)
	line = helper.makeLine(data_bg_ratio.GetXaxis().GetXmin(), 1.00, data_bg_ratio.GetXaxis().GetXmax(), 1.00)
	line.Draw()

	# produce plot
	
	helper.saveCanvas(canv, pad_plot, outputDir + "fakerates_1d/", file_name)
Example #5
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)
Example #6
0
def PlotMETZooms(dataType, outputDir, datasets, mcsets, leg, grouping = False):
	#
	# plotting MET distribution in bins of lepton eta and lepton pt (binning according to fake rate maps)
	# NOTE: ALL SAMPLES MUST CONTAIN THE EXACT SAME LIST OF HISTOGRAMS, OTHERWISE THIS FUNCTION WILL NOT WORK
	#
	# dataType....type of the lepton ('mu', 'el')
	# outputDir...basic output directory
	# datasets....list of data samples [datasample1, datasample2, ..]
	# mcsets......list of mc samples [mcsample1, mcsample2, ..]
	# leg.........legend to be plotted
	# grouping....True if mc samples should be grouped before stacking (useful in case of e.g. several QCD files)

	# define canvas and pads

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

	# text for eta binning

	t_eta = ROOT.TLatex()
	t_eta.SetNDC()
	t_eta.SetTextSize(0.05)
	t_eta.SetTextAlign(11)
	t_eta.SetTextColor(ROOT.kBlack)

	# text for pt binning

	t_pt = ROOT.TLatex()
	t_pt.SetNDC()
	t_pt.SetTextSize(0.05)
	t_pt.SetTextAlign(11)
	t_pt.SetTextColor(ROOT.kBlack)

	# bins in eta and pt, with total number of bins

	bins_eta = [0.0, 0.5, 1.0, 1.5, 2.0, 2.5]
	bins_pt  = [20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0]
	bins_tot = (len(bins_eta)-1)*(len(bins_pt)-1)

	# 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 datasets[0].hists:

		# get index of the histogram

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

		# only histograms in the parameter histlist are plotted

		if not "METZoom" in hist.GetName(): continue

		# pre- and postpends

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

		# get the id of the plot, i.e. the last number in the name (e.g. '0' in h_Loose_FRMETZoom_0)

		id = hist.GetName().split('_')[-1]

		# we stack both data and mc samples (i.e. one may use several data and several mc samples)

		data = ROOT.THStack()
		mc = ROOT.THStack()

		for dataset in datasets:
			data.Add(dataset.hists[i])

		# if grouping is enabled, we first sum all 'similar' mc samples before adding them to the stack
		# similar means, that we sum all samples which have the same name up to a few digits at the end
		# in this way, e.g., the samples dyjets50 and dyjets10 are stacked together
		# if grouping is not enabled, we just stack all mc samples in one stack

		if grouping:

			mcgroups = []
			mcnames  = []

			for mcset in mcsets:
				label = ''.join([j for j in mcset.GetName() if not j.isdigit()])
				
				foundat = -1
				for j, mcname in enumerate(mcnames): 
					if label == mcname: foundat = j

				if foundat == -1:
					mcgroups.append(ROOT.THStack())
					mcgroups[len(mcgroups)-1].Add(mcset.hists[i])
					mcnames.append(label)
				else:
					mcgroups[foundat].Add(mcset.hists[i])
		

			for j, group in enumerate(mcgroups):
				group.Draw('hist')
				histogram = group.GetStack().Last()
				mc.Add(histogram)
		
		else:
			for mcset in mcsets:
				mc.Add(mcset.hists[i])


		# draw histogram

		mc.Draw('hist')

		# define the list of histograms which shall be plotted
		# i.e. one has to take the histogram of the stack, not the stack itself

		hists = []
		hists.append([data.GetStack().Last(), 'data' ])
		hists.append([mc                    , 'totbg'])

		# plot pad_plot first, then we add the texts and plot ratio afterwards

		make1dPlot_plot(dataType, pad_plot, hists, hists[0][0], leg)

		# write bin texts

		m = int(id)//(len(bins_pt)-1)
		n = int(id)%(len(bins_pt)-1)

		if dataType == 'el': lepton = 'e'
		else               : lepton = '#mu'

		text_eta = str(bins_eta[m]) + " #leq " + lepton + "-|#eta| < " + str(bins_eta[m+1])
		text_pt  = str(bins_pt[n])  + " #leq " + lepton + "-p_{T} < " + str(bins_pt[n+1])

		t_eta.DrawLatex(0.22, 0.8, text_eta)
		t_pt.DrawLatex(0.22, 0.73, text_pt)

		# plot ratio

		#make1dPlot_ratio(dataType, pad_ratio, hists, hists[0][0])
		# calling the function to produce the ratio plot results in a malfunction i do not understand properly
		# so we copy the lines from make1dPlot_ratio() while we still keep that function for other purposes

		pad_ratio.cd()
		data_bg_ratio = copy.deepcopy(hists[0][0])
		data_bg_ratio.Divide(copy.deepcopy(hists[1][0].GetStack().Last()))
		data_bg_ratio.Draw("p e")
		data_bg_ratio = helper.setRatioStyle(dataType, data_bg_ratio, hists[0][0])
		line = helper.makeLine(data_bg_ratio.GetXaxis().GetXmin(), 1.00, data_bg_ratio.GetXaxis().GetXmax(), 1.00)
		line.Draw()

		# save plot

		ROOT.gPad.RedrawAxis()
		helper.saveCanvas(canv, pad_plot, outputDir + "zoom_met/", prepend + helper.getSaveName(hist, '-2:') + postpend)