Esempio n. 1
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)
Esempio n. 2
0
def PlotJPtZooms(dataType, outputDir, dataset, mcsets, leg):
	#
	# plotting JetPt and JetRawPt distributions 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
	#
	# this function takes the following parameters:
	# 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

	# define canvas and pads

	canv = helper.makeCanvas(900, 675, 'c1dZ')
	pad_plot = helper.makePad('tot')
	pad_plot.SetTicks(1,1)
	pad_plot.cd()

	# 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, 1.0, 2.4]
	#bins_pt = [10.0, 20.0, 30.0, 35.0, 37.5, 40.0, 42.5, 45.0, 47.5, 50.0, 55.0, 60.0, 70.0] # old
	bins_pt  = [10.0, 20.0, 22.5, 25.0, 27.5, 30.0, 32.5, 35.0, 40.0, 50.0, 60.0, 70.0] # new
	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 dataset.hists:

		# get index of the histogram

		i = dataset.hists.index(hist)

		# only histograms in the parameter histlist are plotted

		if not "JPtZoom" 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_DJPtZoomC_0)
		
		id = hist.GetName().split('_')[-1]

		# draw data

		hist.Scale(1.0/hist.Integral())
		hist.SetFillStyle(0)
		hist.SetLineStyle(2)
		hist.Draw("HIST")
		max = hist.GetMaximum()

		# draw mc samples

		for mc in mcsets:
			mc.hists[i].Scale(1.0/mc.hists[i].Integral())
			mc.hists[i].SetFillStyle(0)
			mc.hists[i].Draw("HIST SAME")
			if mc.hists[i].GetMaximum()>max: max = mc.hists[i].GetMaximum()

		# do some cosmetics

		hist.SetMaximum(1.5*max)
		hist.GetXaxis().SetTitle(helper.getXTitle(dataType, hist))
		hist.GetYaxis().SetTitle("1/Integral")
		hist.SetTitle("")
		hist.GetXaxis().SetTitleSize(0.07)
		hist.GetXaxis().SetLabelSize(0.07)
		hist.GetYaxis().SetTitleSize(0.07)
		hist.GetYaxis().SetLabelSize(0.07)
		hist.GetXaxis().SetNdivisions(505)
		hist.GetYaxis().SetNdivisions(505)

		# draw legend

		leg.Draw()

		# write bin texts 

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

		if "ZoomC" in hist.GetName(): write = "corr."
		else:                         write = "raw"
	
		text_eta = str(bins_eta[m]) + " #leq jet-|#eta| < " + str(bins_eta[m+1])
		text_pt  = str(bins_pt[n])  + " #leq jet-p_{T} (" + write + ") < " + str(bins_pt[n+1])

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

		# draw plots

		helper.saveCanvas(canv, pad_plot, outputDir + "zoom_jpt/", prepend + helper.getSaveName(hist, '-2:') + postpend, False)
Esempio n. 3
0
def Plot2d(dataType, outputDir, datasets, mcsets, histlist):
	#
	# this function is basically the 2d analogon to Plot1d: create 2d plots according to the histograms in the list
	# 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
	# datasets....list of data samples [datasample1, datasample2, ..]
	# mcsets......list of mc samples [mcsample1, mcsample2, ..]
	# histlist....list of 1d histogram names ['h_Loose_LepIso', 'h_Loose_LepPhi', ..]

	# define canvas and pads

	canv = helper.makeCanvas(900, 675, 'c2d')
	pad_plot = helper.makePad('tot')
	pad_plot.cd()
	pad_plot.SetTicks(1,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)
		
		# only histograms in the parameter histlist are plotted
	
		if not hist.GetName() in histlist: continue

		# pre- and postpends	
	
		prepend = ''
		postpend = ''
		if '_Loose_' in hist.GetName(): prepend = 'Loose_'
		if '_Tight_' in hist.GetName(): prepend = 'Tight_'

		# 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])
		for mcset   in mcsets:   mc  .Add(mcset  .hists[i])

		# call make2dPlot to make plots for data, total MC, each mc sample

		make2dPlot(dataType, canv, pad_plot, outputDir, data.GetStack().Last(), 'data', prepend + helper.getSaveName(hist) + postpend)
		make2dPlot(dataType, canv, pad_plot, outputDir, mc  .GetStack().Last(), 'MC'  , prepend + helper.getSaveName(hist) + postpend)
		for mcset in mcsets: make2dPlot(dataType, canv, pad_plot, outputDir, mcset.hists[i], mcset.GetName(), prepend + helper.getSaveName(hist) + postpend)
Esempio n. 4
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)
Esempio n. 5
0
def Plot1d(dataType, outputDir, datasets, mcsets, histlist, leg, grouping = False):
	#
	# given data and mc samples, all histograms in histlist are produced with mc samples stacked
	# 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
	# datasets....list of data samples [datasample1, datasample2, ..]
	# mcsets......list of mc samples [mcsample1, mcsample2, ..]
	# histlist....list of 1d histogram names ['h_Loose_LepIso', 'h_Loose_LepPhi', ..]
	# leg.........legend to be drawn
	# 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, 'c1d')
	pad_plot = helper.makePad('plot')
	pad_ratio = helper.makePad('ratio')
	pad_plot.SetTicks(1,1)
	pad_ratio.SetTicks(1,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 hist.GetName() in histlist: continue

		# there are two NVertices plots in the list with different binning
		# we only plot the NVertices, but not the NVertices1 (this we use for the fake rate plots, see lib_FakeRate.py)

		if hist.GetName()[-10:] == "NVertices1": continue

		# pre- and postpends

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


		# 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 mc in mcsets:	
				mc.Add(mc.hists[i])


		# define the list of histograms which shall be plotted
		# i.e. one has to take the histogram of the stack, not the stack itself
	
		histstoplot = []
		histstoplot.append([data.GetStack().Last(), 'data' ])
		histstoplot.append([mc                    , 'totbg'])

		# call make1dPlot to create the plot

		make1dPlot(dataType, canv, pad_plot, pad_ratio, outputDir, histstoplot, hist, prepend + helper.getSaveName(hist) + postpend, leg)