Example #1
0
	def GetBJetPlusXHistograms(self):
		this_analyses = ["trigbbh_CSVTM", "trigbbl_CSVTM", "trigbbll_CSVTM", "trigbbh_trigbbl_CSVTM"]
		this_samples = ["BJetPlusX_2012", "BJetPlusX_2012BCD"]
		for analysis in this_analyses:
			for sample in this_samples:
				name = analysis + "_" + sample
				f = ROOT.TFile(analysis_config.get_b_histogram_filename(analysis, sample), "READ")
				self._mjj_histograms_fine[name] = f.Get("BHistograms/h_pfjet_mjj")
				self._mjj_histograms_fine[name].SetName("h_" + name + "_mjj_fine")
				self._mjj_histograms_fine[name].SetDirectory(0)
				self._mjj_histograms[name] = histogram_tools.rebin_histogram(self._mjj_histograms_fine[name], self._mass_bins, normalization_bin_width=1)
				self._mjj_histograms[name].SetName("h_" + name + "_mjj")
				self._mjj_histograms[name].SetDirectory(0)
				self._mjj_histograms_csvorder[name] = f.Get("BHistograms/h_pfjet_mjj_csvorder")
				self._mjj_histograms_csvorder[name].SetName("h_" + name + "_mjj_csvorder")
				self._mjj_histograms_csvorder[name].SetDirectory(0)
				self._mjj_histograms_csvorder[name] = histogram_tools.rebin_histogram(self._mjj_histograms_csvorder[name], self._mass_bins, normalization_bin_width=1)
				self._mjj_histograms_vetothirdjet[name] = f.Get("BHistograms/h_pfjet_mjj_vetothirdjet")
				self._mjj_histograms_vetothirdjet[name].SetName("h_" + name + "_mjj_vetothirdjet")
				self._mjj_histograms_vetothirdjet[name].SetDirectory(0)
				self._mjj_histograms_vetothirdjet[name] = histogram_tools.rebin_histogram(self._mjj_histograms_vetothirdjet[name], self._mass_bins, normalization_bin_width=1)
				self._mjj_histograms_fine[name].Rebin(5)
				self._analyses.append(name)
				f.Close()

				if "bbll" in analysis:
					self._mjj_histograms[name].Scale(1.7)
					self._mjj_histograms_fine[name].Scale(1.7)
Example #2
0
	def GetSingleMuHistograms(self):
		#this_analyses = ["trigmu_highmass_CSVTM", "trigmu_lowmass_CSVTM", "trigmubbh_highmass_CSVTM", "trigmubbl_lowmass_CSVTM", "trigmubbll_lowmass_CSVTM", "trigmu24i_lowmass_CSVTM", "trigmu24ibbl_lowmass_CSVTM", "trigmu24ibbll_lowmass_CSVTM", "trigmu40_lowmass_CSVTM", "trigmu40bbl_lowmass_CSVTM", "trigmu40bbll_lowmass_CSVTM"]
		this_analyses = ["trigmu24i_lowmass_CSVTM", "trigmu24ibbl_lowmass_CSVTM"]
		this_analyses.extend(["trigmu24i_highmass_CSVTM", "trigmu24ibbh_highmass_CSVTM"])
		for analysis in this_analyses:
			print "Opening " + analysis_config.get_b_histogram_filename(analysis, "SingleMu_2012")
			f = ROOT.TFile(analysis_config.get_b_histogram_filename(analysis, "SingleMu_2012"), "READ")
			self._mjj_histograms_fine[analysis] = f.Get("BHistograms/h_pfjet_mjj")
			self._mjj_histograms_fine[analysis].SetName("h_" + analysis + "_mjj_fine")
			self._mjj_histograms_fine[analysis].SetDirectory(0)
			self._mjj_histograms[analysis] = histogram_tools.rebin_histogram(self._mjj_histograms_fine[analysis], self._mass_bins, normalization_bin_width=1)
			self._mjj_histograms[analysis].SetName("h_" + analysis + "_mjj")
			self._mjj_histograms[analysis].SetDirectory(0)

			if not "highmass" in analysis:
				self._mjj_histograms_csvorder[analysis] = f.Get("BHistograms/h_pfjet_mjj_csvorder")
				self._mjj_histograms_csvorder[analysis].SetName("h_" + analysis + "_mjj_csvorder")
				self._mjj_histograms_csvorder[analysis].SetDirectory(0)
				self._mjj_histograms_csvorder[analysis] = histogram_tools.rebin_histogram(self._mjj_histograms_csvorder[analysis], self._mass_bins, normalization_bin_width=1)

				self._mjj_histograms_vetothirdjet[analysis] = f.Get("BHistograms/h_pfjet_mjj_vetothirdjet")
				self._mjj_histograms_vetothirdjet[analysis].SetName("h_" + analysis + "_mjj_vetothirdjet")
				self._mjj_histograms_vetothirdjet[analysis].SetDirectory(0)
				self._mjj_histograms_vetothirdjet[analysis] = histogram_tools.rebin_histogram(self._mjj_histograms_vetothirdjet[analysis], self._mass_bins, normalization_bin_width=1)

			if "bbll" in analysis:
				self._mjj_histograms_fine[analysis].Scale(1.7) # Prescale for singlemu + 60/53. The prescale was not computer for these analyses.
				self._mjj_histograms[analysis].Scale(1.7) # Prescale for singlemu + 60/53. The prescale was not computer for these analyses.
			self._mjj_histograms_fine[analysis].Rebin(5)
			self._analyses.append(analysis)
			f.Close()
Example #3
0
def data_jes_plot(new_sample, old_sample, analysis):
	print "Making data JES plot for {}".format(analysis)
	f_new = TFile(analysis_config.get_b_histogram_filename(analysis, new_sample), "READ")
	h_new = f_new.Get("BHistograms/h_pfjet_mjj")
	h_new.SetName("h_pfjet_mjj_newJEC")
	h_new.SetDirectory(0)
	h_new = histogram_tools.rebin_histogram(h_new, dijet_binning, normalization_bin_width=1.)
	print "New integral = {}".format(h_new.Integral())

	f_old = TFile(analysis_config.get_b_histogram_filename(analysis, old_sample).replace("EightTeeEeVeeBee/BHistograms", "EightTeeEeVeeBee/BHistograms/old/JEC2012"), "READ")
	h_old = f_old.Get("BHistograms/h_pfjet_mjj")
	h_old.SetName("h_pfjet_mjj_oldJEC")
	h_old.SetDirectory(0)
	h_old = histogram_tools.rebin_histogram(h_old, dijet_binning, normalization_bin_width=1.)
	print "Old integral = {}".format(h_old.Integral())

	c = TCanvas("c_jeccomparison_{}".format(analysis), "c_jeccomparison_{}".format(analysis), 800, 1200)
	top = TPad("top", "top", 0., 0.5, 1., 1.)
	top.SetBottomMargin(0.02)
	top.SetLogy()
	top.Draw()
	top.cd()
	h_new.SetMarkerStyle(20)
	h_new.GetXaxis().SetTitleSize(0)
	h_new.GetXaxis().SetLabelSize(0)
	h_new.GetYaxis().SetTitle("Events / GeV")
	h_new.Draw()
	h_old.SetMarkerStyle(25)
	h_old.Draw("same")

	c.cd()
	bottom = TPad("bottom", "bottom", 0., 0., 1., 0.5)
	bottom.SetTopMargin(0.02)
	bottom.SetBottomMargin(0.2)
	bottom.Draw()
	bottom.cd()
	h_ratio = h_new.Clone()
	h_ratio.Divide(h_old)
	h_ratio.GetXaxis().SetTitle("m_{jj} [GeV]")
	h_ratio.GetYaxis().SetTitle("New JEC / Old JEC")
	h_ratio.SetMarkerStyle(21)
	h_ratio.Draw()

	c.cd()
	c.SaveAs(analysis_config.figure_directory + "/" + c.GetName() + ".pdf")


	f_new.Close()
	f_old.Close()
def plot_all_masses(analysis, model, masses):
	ratio_up = {}
	ratio_down = {}
	for mass in masses:
		f = TFile(analysis_config.get_b_histogram_filename(analysis, analysis_config.simulation.get_signal_tag(model, mass, "FULLSIM")), "READ")
		h_central = f.Get("BHistograms/h_pfjet_mjj")
		h_central.SetDirectory(0)
		h_central = histogram_tools.rebin_histogram(h_central, dijet_binning, normalization_bin_width=1.)
		h_up = f.Get("BHistograms/h_pfjet_mjj_BTagOfflineSFUp")
		h_up.SetDirectory(0)
		h_up = histogram_tools.rebin_histogram(h_up, dijet_binning, normalization_bin_width=1.)
		h_down = f.Get("BHistograms/h_pfjet_mjj_BTagOfflineSFDown")
		h_down.SetDirectory(0)
		h_down = histogram_tools.rebin_histogram(h_down, dijet_binning, normalization_bin_width=1.)
		ratio_up[mass] = h_up.Clone()
		ratio_up[mass].SetName("ratio_up_{}".format(mass))
		ratio_up[mass].SetDirectory(0)
		ratio_up[mass].Divide(h_central)
		ratio_up[mass].SetMarkerStyle(26)
		ratio_up[mass].Draw("p hist same")
		ratio_down[mass] = h_down.Clone()
		ratio_down[mass].SetName("ratio_down_{}".format(mass))
		ratio_down[mass].SetDirectory(0)
		ratio_down[mass].Divide(h_central)
		ratio_down[mass].SetMarkerStyle(32)
		ratio_down[mass].Draw("p hist same")
		norm = h_central.Integral()
		print "{}\t{}\t{}:{}".format(analysis, model, mass, h_up.Integral()/norm-1.)
		f.Close()

	c = TCanvas("c_offb_unc_{}_{}_all".format(analysis, model), "c_offb_unc_{}_{}_all".format(analysis, model), 800, 600)
	frame = TH1D("frame", "frame", 100, 0., 1300.)
	frame.SetMinimum(0.8)
	frame.SetMaximum(1.2)
	frame.GetXaxis().SetTitle("m_{jj}")
	frame.GetYaxis().SetTitle("SF Variation / Central")
	frame.Draw()
	for style_counter, mass in enumerate(masses):
		ratio_up[mass].SetMarkerStyle(20)
		ratio_up[mass].SetMarkerColor(seaborn.GetColorRoot("cubehelixlarge", style_counter, len(masses)+2))
		ratio_up[mass].SetLineColor(seaborn.GetColorRoot("cubehelixlarge", style_counter, len(masses)+2))
		ratio_up[mass].Draw("hist same")
		ratio_down[mass].SetMarkerStyle(20)
		ratio_down[mass].SetMarkerColor(seaborn.GetColorRoot("cubehelixlarge", style_counter, len(masses)+2))
		ratio_down[mass].SetLineColor(seaborn.GetColorRoot("cubehelixlarge", style_counter, len(masses)+2))
		ratio_down[mass].Draw("hist same")
	c.SaveAs(analysis_config.figure_directory + "/" + c.GetName() + ".pdf")
Example #5
0
	def GetJetHTHistogram(self):
		for sr_name in ["highmass", "lowmass"]:
			analyses = {}
			HT_slices = []
			for mass in xrange(200, 600, 50):
				HT_slices.append("HT" + str(mass))
				analyses["HT" + str(mass)] = "trigjetht" + str(mass)
				if sr_name == "lowmass":
					analyses["HT" + str(mass)] += "_eta1p7"
				analyses["HT" + str(mass)] += "_CSVTM"
			sample = "JetHT_2012BCD"
			HT_slice_histograms = {}
			for HT_slice in HT_slices:
				f = TFile(analysis_config.get_b_histogram_filename(analyses[HT_slice], sample), "READ")
				HT_slice_histograms[HT_slice] = f.Get("BHistograms/h_pfjet_mjj")
				print "On file " + analysis_config.get_b_histogram_filename(analyses[HT_slice], sample)
				HT_slice_histograms[HT_slice].SetName(HT_slice_histograms[HT_slice].GetName() + "_" + analyses[HT_slice])
				HT_slice_histograms[HT_slice].SetDirectory(0)
				f.Close()
			HT_slices.append("HTUnprescaled")
			unprescaled_analysis_name = "trigjetht"
			if sr_name == "lowmass":
				unprescaled_analysis_name += "_eta1p7"
			unprescaled_analysis_name += "_CSVTM"
			analyses["HTUnprescaled"] = unprescaled_analysis_name
			f_unprescaled = TFile(analysis_config.get_b_histogram_filename(unprescaled_analysis_name, sample), "READ")
			HT_slice_histograms["HTUnprescaled"] = f_unprescaled.Get("BHistograms/h_pfjet_mjj")
			HT_slice_histograms["HTUnprescaled"].SetName(HT_slice_histograms["HTUnprescaled"].GetName() + "_" + analyses["HTUnprescaled"])
			HT_slice_histograms["HTUnprescaled"].SetDirectory(0)
			f_unprescaled.Close()
			ranges = {
				"HT200":[220, 386],
				"HT250":[386, 489],
				"HT300":[489, 526],
				"HT350":[526, 606],
				"HT400":[606, 649],
				"HT450":[649, 740],
				"HT500":[740, 788],
				"HT550":[788, 890],
				#"HT650":[800, 890],
				"HTUnprescaled":[890, 2000]
			}

			self._analyses.append("JetHT")
			self._mjj_histograms_fine["JetHT"] = self.FrankenHist(HT_slices, HT_slice_histograms, ranges)
			self._mjj_histograms["JetHT"] = histogram_tools.rebin_histogram(self._mjj_histograms_fine["JetHT"], self._mass_bins, normalization_bin_width=1)
			self._mjj_histograms_fine["JetHT"].Rebin(5)
     this_num_hist = f_num.Get("BHistograms/h_pfjet_mjj")
     #this_num_hist.Scale(19700.*qcd_cross_sections[qcd_sample] / input_nevents_num)
     input_nevents_den = f_den.Get("BHistograms/h_input_nevents").Integral()
     this_den_hist = f_den.Get("BHistograms/h_pfjet_mjj")
     #this_den_hist.Scale(19700.*qcd_cross_sections[qcd_sample] / input_nevents_den)
     if not sr in qcd_hists_num:
         qcd_hists_num[sr] = this_num_hist.Clone()
         qcd_hists_num[sr].SetDirectory(0)
         qcd_hists_den[sr] = this_den_hist.Clone()
         qcd_hists_den[sr].SetDirectory(0)
     else:
         qcd_hists_num[sr].Add(this_num_hist)
         qcd_hists_den[sr].Add(this_den_hist)
     f_num.Close()
     f_den.Close()
 qcd_hists_num[sr] = histogram_tools.rebin_histogram(
     qcd_hists_num[sr], dijet_binning)
 qcd_hists_den[sr] = histogram_tools.rebin_histogram(
     qcd_hists_den[sr], dijet_binning)
 for bin in xrange(1, qcd_hists_num[sr].GetNbinsX() + 1):
     bin_center = qcd_hists_num[sr].GetXaxis().GetBinCenter(bin)
     if sr == "lowmass":
         if bin_center < 296 or bin_center > 1058:
             qcd_hists_num[sr].SetBinContent(bin, 0)
             qcd_hists_num[sr].SetBinError(bin, 0)
             qcd_hists_den[sr].SetBinContent(bin, 0)
             qcd_hists_den[sr].SetBinError(bin, 0)
     elif sr == "highmass":
         if bin_center < 526 or bin_center > 1607:
             qcd_hists_num[sr].SetBinContent(bin, 0)
             qcd_hists_num[sr].SetBinError(bin, 0)
             qcd_hists_den[sr].SetBinContent(bin, 0)
	def FrankenEfficiencyPlot(self, logy=True, binning=None, simple_rebin=None, save_tag="", x_range=None, ratio_range=None, legend_position="topright", numerator_legend=None, denominator_legend=None, ratio_title=None):
		ht_slices = ["HT200","HT250","HT300","HT350","HT400","HT450","HT500","HT550","HTUnprescaled"] # HT650
		if self._numerator_analysis == "trigjetht_eta1p7_CSVTM":
			numerator_analyses = {
				"HT200":"trigjetht200_eta1p7_CSVTM",
				"HT250":"trigjetht250_eta1p7_CSVTM",
				"HT300":"trigjetht300_eta1p7_CSVTM",
				"HT350":"trigjetht350_eta1p7_CSVTM",
				"HT400":"trigjetht400_eta1p7_CSVTM",
				"HT450":"trigjetht450_eta1p7_CSVTM",
				"HT500":"trigjetht500_eta1p7_CSVTM",
				"HT550":"trigjetht550_eta1p7_CSVTM",
				#"HT650":"trigjetht650_eta1p7_CSVTM",
				"HTUnprescaled":"trigjetht_eta1p7_CSVTM"
			}
		elif self._numerator_analysis == "trigjetht_eta2p2_CSVTM":
			numerator_analyses = {
				"HT200":"trigjetht200_CSVTM",
				"HT250":"trigjetht250_CSVTM",
				"HT300":"trigjetht300_CSVTM",
				"HT350":"trigjetht350_CSVTM",
				"HT400":"trigjetht400_CSVTM",
				"HT450":"trigjetht450_CSVTM",
				"HT500":"trigjetht500_CSVTM",
				"HT550":"trigjetht550_CSVTM",
				#"HT650":"trigjetht650_CSVTM",
				"HTUnprescaled":"trigjetht_CSVTM"
			}
		else:
			print "[OfflineBTagPlots::FrankenEfficiencyPlot] ERROR : numerator analysis must be trigjetht_eta1p7_CSVTM or trigjetht_eta2p2_CSVTM"
			sys.exit(1)
		if self._denominator_analysis == "trigjetht_eta1p7":
			denominator_analyses = {
				"HT200":"trigjetht200_eta1p7",
				"HT250":"trigjetht250_eta1p7",
				"HT300":"trigjetht300_eta1p7",
				"HT350":"trigjetht350_eta1p7",
				"HT400":"trigjetht400_eta1p7",
				"HT450":"trigjetht450_eta1p7",
				"HT500":"trigjetht500_eta1p7",
				"HT550":"trigjetht550_eta1p7",
				#"HT650":"trigjetht650_eta1p7",
				"HTUnprescaled":"trigjetht_eta1p7"
			}
		elif self._denominator_analysis == "trigjetht_eta2p2":
			denominator_analyses = {
				"HT200":"trigjetht200",
				"HT250":"trigjetht250",
				"HT300":"trigjetht300",
				"HT350":"trigjetht350",
				"HT400":"trigjetht400",
				"HT450":"trigjetht450",
				"HT500":"trigjetht500",
				"HT550":"trigjetht550",
				#"HT650":"trigjetht650",
				"HTUnprescaled":"trigjetht"
			}
		else:
			print "[OfflineBTagPlots::FrankenEfficiencyPlot] ERROR : denominator analysis must be trigjetht_eta1p7 or trigjetht_eta2p2"
			sys.exit(1)
		ht_ranges = {
			"HT200":[220, 386],
			"HT250":[386, 489],
			"HT300":[489, 526],
			"HT350":[526, 606],
			"HT400":[606, 649],
			"HT450":[649, 740],
			"HT500":[740, 788],
			"HT550":[788, 890],
			#"HT650":[890, 2000],
			"HTUnprescaled":[890, 2000]
		}
		numerator_histogram = None
		denominator_histogram = None
		for sample in self._samples:
			print "[EfficiencyPlot] DEBUG : Sample " + sample
			numerator_slice_histograms = {}
			denominator_slice_histograms = {}
			for slice_name in ht_slices:
				print slice_name
				print analysis_config.get_b_histogram_filename(numerator_analyses[slice_name], sample)
				numerator_file = TFile(analysis_config.get_b_histogram_filename(numerator_analyses[slice_name], sample), "READ")
				print analysis_config.get_b_histogram_filename(denominator_analyses[slice_name], sample)
				denominator_file = TFile(analysis_config.get_b_histogram_filename(denominator_analyses[slice_name], sample), "READ")

				# Check input nevents
				num_nevents = numerator_file.Get("BHistograms/h_input_nevents").Integral()
				den_nevents = denominator_file.Get("BHistograms/h_input_nevents").Integral()
				if num_nevents != den_nevents:
					# Allow tiny differences...?
					if abs((num_nevents - den_nevents) / den_nevents) < 0.001:
						print "[EfficiencyPlot] ERROR : Small inconsistency between number of events between numerator and denominator. I'm going to rescale away the difference, but you may want to fix this."
						numerator_normalization = den_nevents / num_nevents
						denominator_normalization = 1.
					else:
						print "[EfficiencyPlot] ERROR : Inconsistent number of events between numerator and denominator. Results would be wrong, so I'm aborting."
						print "[EfficiencyPlot] ERROR : \tNumerator = " + str(numerator_file.Get("BHistograms/h_input_nevents").Integral())
						print "[EfficiencyPlot] ERROR : \tDenominator = " + str(denominator_file.Get("BHistograms/h_input_nevents").Integral())
						sys.exit(1)
				else:
					numerator_normalization = 1.
					denominator_normalization = 1.
				numerator_slice_histograms[slice_name] = numerator_file.Get("BHistograms/h_pfjet_mjj")
				numerator_slice_histograms[slice_name].SetName("h_pfjet_mjj_num_" + slice_name + "_" + sample)
				numerator_slice_histograms[slice_name].SetDirectory(0)
				numerator_slice_histograms[slice_name].Scale(numerator_normalization)
				denominator_slice_histograms[slice_name] = denominator_file.Get("BHistograms/h_pfjet_mjj")
				denominator_slice_histograms[slice_name].SetName("h_pfjet_mjj_den_" + slice_name + "_" + sample)
				denominator_slice_histograms[slice_name].SetDirectory(0)
				denominator_slice_histograms[slice_name].Scale(denominator_normalization)
				numerator_file.Close()
				denominator_file.Close()
			# Make frankenhist
			this_numerator_histogram = self.FrankenHist(ht_slices, numerator_slice_histograms, ht_ranges)
			this_denominator_histogram = self.FrankenHist(ht_slices, denominator_slice_histograms, ht_ranges)
			if not numerator_histogram:
				numerator_histogram = this_numerator_histogram.Clone()
				numerator_histogram.SetName(numerator_histogram.GetName() + save_tag + "_num_" + str(time.time()))
				denominator_histogram = this_denominator_histogram.Clone()
				denominator_histogram.SetName(denominator_histogram.GetName() + save_tag + "_num_" + str(time.time()))
			else:
				numerator_histogram.Add(this_numerator_histogram)
				denominator_histogram.Add(this_denominator_histogram)

		# Rebin
		if binning:
			numerator_histogram = histogram_tools.rebin_histogram(numerator_histogram, binning)
			denominator_histogram = histogram_tools.rebin_histogram(denominator_histogram, binning)
		elif simple_rebin:
			numerator_histogram.Rebin(simple_rebin)
			denominator_histogram.Rebin(simple_rebin)


		cname = "c_offline_btag_eff_mjj"
		if logy:
			cname += "_log"
		c = TCanvas(cname, "Offline b-tag #epsilon", 800, 1000)
		top = TPad("top", "top", 0., 0.5, 1., 1.)
		top.SetBottomMargin(0.02)
		if logy:
			top.SetLogy()
		top.Draw()
		top.cd()

		frame_top = numerator_histogram.Clone()
		frame_top.Reset()
		if x_range:
			frame_top.GetXaxis().SetRangeUser(x_range[0], x_range[1])
		if logy:
			y_min = 0.1
			y_max = max(numerator_histogram.GetMaximum(), denominator_histogram.GetMaximum()) * 10.
		else:
			y_min = 0.
			y_max = max(numerator_histogram.GetMaximum(), denominator_histogram.GetMaximum()) * 1.5
		frame_top.SetMinimum(y_min)
		frame_top.SetMaximum(y_max)
		frame_top.GetXaxis().SetLabelSize(0)
		frame_top.GetXaxis().SetTitleSize(0)
		if binning:
			#frame_top.GetYaxis().SetTitle("Events / 1 GeV")
			frame_top.GetYaxis().SetTitle("Events")
		else:
			frame_top.GetYaxis().SetTitle("Events")
		frame_top.Draw("axis")
		print "numerator integral = " + str(numerator_histogram.Integral())
		print "denominator integral = " + str(denominator_histogram.Integral())
		numerator_histogram.SetMarkerStyle(20)
		numerator_histogram.SetMarkerColor(seaborn.GetColorRoot("default", 0))
		numerator_histogram.SetLineColor(seaborn.GetColorRoot("default", 0))
		numerator_histogram.Draw("same")
		denominator_histogram.SetMarkerStyle(24)
		denominator_histogram.SetMarkerColor(seaborn.GetColorRoot("default", 2))
		denominator_histogram.SetLineColor(seaborn.GetColorRoot("default", 2))
		denominator_histogram.Draw("same")
		if legend_position == "topright":
			l = TLegend(0.6, 0.6, 0.85, 0.8)
		elif legend_position == "bottomright":
			l = TLegend(0.6, 0.2, 0.85, 0.4)

		l.SetFillColor(0)
		l.SetBorderSize(0)
		if numerator_legend:
			l.AddEntry(numerator_histogram, numerator_legend)
		else:
			l.AddEntry(numerator_histogram, "CSVT+CSVM")
		if denominator_legend:
			l.AddEntry(denominator_histogram, denominator_legend)

		else:
			l.AddEntry(denominator_histogram, "No CSV")
		l.Draw()

		c.cd()
		bottom = TPad("bottom", "bottom", 0., 0., 1., 0.5)
		bottom.SetTopMargin(0.01)
		bottom.SetBottomMargin(0.2)
		bottom.Draw()
		bottom.cd()
		ratio_histogram = numerator_histogram.Clone()
		ratio_histogram.Reset()
		if x_range:
			ratio_histogram.GetXaxis().SetRangeUser(x_range[0], x_range[1])
		ratio_histogram.SetName(numerator_histogram.GetName() + "_ratio_" + save_tag + str(time.time()))
		ratio_histogram.SetDirectory(0)
		for bin in xrange(1, numerator_histogram.GetNbinsX() + 1):
			# Undo bin normalization
			if numerator_histogram.GetBinError(bin) > 0 and denominator_histogram.GetBinError(bin) > 0:
				num_unnormalized = (numerator_histogram.GetBinContent(bin))**2 / (numerator_histogram.GetBinError(bin))**2
				den_unnormalized = (denominator_histogram.GetBinContent(bin))**2 / (denominator_histogram.GetBinError(bin))**2
				num = numerator_histogram.GetBinContent(bin)
				den = denominator_histogram.GetBinContent(bin)
				ratio = 1. * num_unnormalized / den_unnormalized 
				ratio_err = sqrt(ratio * (1. - ratio) / den_unnormalized)
				#ratio_err = max(sqrt(ratio * (1. - ratio) / den), 1./den)
			else:
				ratio = 0.
				ratio_err = 0.
			ratio_histogram.SetBinContent(bin, ratio)
			ratio_histogram.SetBinError(bin, ratio_err)
		ratio_histogram.SetMarkerSize(1)
		ratio_histogram.SetMarkerColor(kBlack)
		ratio_histogram.SetLineColor(kBlack)
		ratio_histogram.SetLineWidth(2)
		ratio_histogram.GetXaxis().SetTitle("m_{jj} [GeV]")
		if ratio_title:
			ratio_histogram.GetYaxis().SetTitle(ratio_title)
		else:
			ratio_histogram.GetYaxis().SetTitle("Offline 2#timesb-tag efficiency")
		if ratio_range:
			ratio_histogram.SetMinimum(ratio_range[0])
			ratio_histogram.SetMaximum(ratio_range[1])
		else:
			ratio_histogram.SetMinimum(0.)
			ratio_histogram.SetMaximum(1.)
		ratio_histogram.Draw()

		c.cd()
		c.SaveAs(analysis_config.figure_directory + "/OfflineBTag/" + c.GetName() + save_tag + ".pdf")
		ROOT.SetOwnership(c, False)
		ROOT.SetOwnership(top, False)
		ROOT.SetOwnership(bottom, False)
	def EfficiencyPlot(self, var="mjj", logy=True, binning=None, simple_rebin=None, save_tag="", sample_xsecs=None, x_range=None, ratio_range=None, legend_position="topright", numerator_legend=None, denominator_legend=None, ratio_title=None, prescaled=False, fit=False, fit_range=None):
		numerator_histogram = None
		denominator_histogram = None
		for sample in self._samples:
			if sample_xsecs:
				xsec = sample_xsecs[sample]
				numerator_normalization = 19710. * xsec / self._input_files["numerator"][sample].Get("BHistograms/h_sample_nevents").Integral()
				denominator_normalization = 19710. * xsec / self._input_files["denominator"][sample].Get("BHistograms/h_sample_nevents").Integral()
				if numerator_normalization != denominator_normalization:
					print "[EfficiencyPlot] WARNING : Numerator and denominator normalizations do not match! Continuing, but investigate this. Numerator nevents = " + str(self._input_files["numerator"][sample].Get("BHistograms/h_sample_nevents").Integral()) + ", denominator nevents = " + str(self._input_files["denominator"][sample].Get("BHistograms/h_sample_nevents").Integral())
			else:
				# Check input nevents
				if self._input_files["numerator"][sample].Get("BHistograms/h_sample_nevents"):
					num_nevents = self._input_files["numerator"][sample].Get("BHistograms/h_sample_nevents").Integral()
					den_nevents = self._input_files["denominator"][sample].Get("BHistograms/h_sample_nevents").Integral()
				else:
					num_nevents = self._input_files["numerator"][sample].Get("BHistograms/h_input_nevents").Integral()
					den_nevents = self._input_files["denominator"][sample].Get("BHistograms/h_input_nevents").Integral()
				if num_nevents != den_nevents:
					# Allow tiny differences...?
					if abs((num_nevents - den_nevents) / den_nevents) < 0.001:
						print "[EfficiencyPlot] ERROR : Small inconsistency between number of events between numerator and denominator. I'm going to rescale away the difference, but you may want to fix this."
						numerator_normalization = den_nevents / num_nevents
						denominator_normalization = 1.
					else:
						print "[EfficiencyPlot] ERROR : Inconsistent number of events between numerator and denominator. Results would be wrong, so I'm aborting."
						print "[EfficiencyPlot] ERROR : \tNumerator = " + str(self._input_files["numerator"][sample].Get("BHistograms/h_sample_nevents").Integral())
						print "[EfficiencyPlot] ERROR : \tDenominator = " + str(self._input_files["denominator"][sample].Get("BHistograms/h_sample_nevents").Integral())
						sys.exit(1)
				else:
					numerator_normalization = 1.
					denominator_normalization = 1.
			if numerator_histogram:
				numerator_histogram.Add(self._input_files["numerator"][sample].Get("BHistograms/h_pfjet_" + var), numerator_normalization)
			else:
				numerator_histogram = self._input_files["numerator"][sample].Get("BHistograms/h_pfjet_" + var).Clone()
				numerator_histogram.Scale(numerator_normalization)
				numerator_histogram.SetDirectory(0)
				numerator_histogram.SetName(numerator_histogram.GetName() + save_tag + "_num" + str(time.time()))
			if denominator_histogram:
				denominator_histogram.Add(self._input_files["denominator"][sample].Get("BHistograms/h_pfjet_" + var), denominator_normalization)
			else:
				denominator_histogram = self._input_files["denominator"][sample].Get("BHistograms/h_pfjet_" + var).Clone()
				denominator_histogram.Scale(denominator_normalization)
				denominator_histogram.SetDirectory(0)
				denominator_histogram.SetName(denominator_histogram.GetName() + save_tag + "_den" + str(time.time()))

		# Rebin
		numerator_histogram_fine = numerator_histogram.Clone()
		denominator_histogram_fine = denominator_histogram.Clone()
		if binning:
			numerator_histogram = histogram_tools.rebin_histogram(numerator_histogram, binning)
			denominator_histogram = histogram_tools.rebin_histogram(denominator_histogram, binning)
		elif simple_rebin:
			numerator_histogram.Rebin(simple_rebin)
			denominator_histogram.Rebin(simple_rebin)


		cname = "c_offline_btag_eff_" + var
		if logy:
			cname += "_log"
		c = TCanvas(cname, "Offline b-tag #epsilon", 800, 1000)
		top = TPad("top", "top", 0., 0.5, 1., 1.)
		top.SetBottomMargin(0.02)
		if logy:
			top.SetLogy()
		top.Draw()
		top.cd()

		frame_top = numerator_histogram.Clone()
		frame_top.Reset()
		if x_range:
			frame_top.GetXaxis().SetRangeUser(x_range[0], x_range[1])
		if logy:
			y_min = 0.1
			y_max = max(numerator_histogram.GetMaximum(), denominator_histogram.GetMaximum()) * 10.
		else:
			y_min = 0.
			y_max = max(numerator_histogram.GetMaximum(), denominator_histogram.GetMaximum()) * 1.5
		frame_top.SetMinimum(y_min)
		frame_top.SetMaximum(y_max)
		frame_top.GetXaxis().SetLabelSize(0)
		frame_top.GetXaxis().SetTitleSize(0)
		if binning:
			#frame_top.GetYaxis().SetTitle("Events / 1 GeV")
			frame_top.GetYaxis().SetTitle("Events")
		else:
			frame_top.GetYaxis().SetTitle("Events")
		frame_top.Draw("axis")
		print "numerator integral = " + str(numerator_histogram.Integral())
		print "denominator integral = " + str(denominator_histogram.Integral())
		numerator_histogram.SetMarkerStyle(20)
		numerator_histogram.SetMarkerColor(seaborn.GetColorRoot("default", 0))
		numerator_histogram.SetLineColor(seaborn.GetColorRoot("default", 0))
		numerator_histogram.Draw("same")
		denominator_histogram.SetMarkerStyle(24)
		denominator_histogram.SetMarkerColor(seaborn.GetColorRoot("default", 2))
		denominator_histogram.SetLineColor(seaborn.GetColorRoot("default", 2))
		denominator_histogram.Draw("same")
		if legend_position == "topright":
			l = TLegend(0.6, 0.6, 0.85, 0.8)
		elif legend_position == "bottomright":
			l = TLegend(0.6, 0.2, 0.85, 0.4)

		l.SetFillColor(0)
		l.SetBorderSize(0)
		if numerator_legend:
			l.AddEntry(numerator_histogram, numerator_legend)
		else:
			l.AddEntry(numerator_histogram, "CSVT+CSVM")
		if denominator_legend:
			l.AddEntry(denominator_histogram, denominator_legend)

		else:
			l.AddEntry(denominator_histogram, "No CSV")
		l.Draw()

		c.cd()
		bottom = TPad("bottom", "bottom", 0., 0., 1., 0.5)
		bottom.SetTopMargin(0.01)
		bottom.SetBottomMargin(0.2)
		bottom.Draw()
		bottom.cd()
		ratio_histogram = numerator_histogram.Clone()
		ratio_histogram.Reset()
		if x_range:
			ratio_histogram.GetXaxis().SetRangeUser(x_range[0], x_range[1])
		ratio_histogram.SetName(numerator_histogram.GetName() + "_ratio_" + save_tag + str(time.time()))
		ratio_histogram.SetDirectory(0)
		for bin in xrange(1, numerator_histogram.GetNbinsX() + 1):
			# Undo bin normalization
			if numerator_histogram.GetBinError(bin) > 0 and denominator_histogram.GetBinError(bin) > 0:
				if prescaled:
					num = (numerator_histogram.GetBinContent(bin))**2 / (numerator_histogram.GetBinError(bin))**2
					den = (denominator_histogram.GetBinContent(bin))**2 / (denominator_histogram.GetBinError(bin))**2
				else:
					num = numerator_histogram.GetBinContent(bin)
					den = denominator_histogram.GetBinContent(bin)
				ratio = 1. * num / den 
				ratio_err = sqrt(ratio * (1. - ratio) / den)
				#ratio_err = max(sqrt(ratio * (1. - ratio) / den), 1./den)
			else:
				ratio = 0.
				ratio_err = 0.
			ratio_histogram.SetBinContent(bin, ratio)
			ratio_histogram.SetBinError(bin, ratio_err)

		ratio_histogram.SetMarkerSize(1)
		ratio_histogram.SetMarkerColor(kBlack)
		ratio_histogram.SetLineColor(kBlack)
		ratio_histogram.SetLineWidth(2)
		if "mjj" in var:
			ratio_histogram.GetXaxis().SetTitle("m_{jj} [GeV]")
		elif "pt" in var:
			ratio_histogram.GetXaxis().SetTitle("p_{T} [GeV]")
		if ratio_title:
			ratio_histogram.GetYaxis().SetTitle(ratio_title)
		else:
			ratio_histogram.GetYaxis().SetTitle("Offline 2#timesb-tag efficiency")
		if ratio_range:
			ratio_histogram.SetMinimum(ratio_range[0])
			ratio_histogram.SetMaximum(ratio_range[1])
		else:
			ratio_histogram.SetMinimum(0.)
			ratio_histogram.SetMaximum(1.)
		ratio_histogram.Draw()

		c.cd()
		c.SaveAs(analysis_config.figure_directory + "/OfflineBTag/" + c.GetName() + save_tag + ".pdf")
		ROOT.SetOwnership(c, False)
		ROOT.SetOwnership(top, False)
		ROOT.SetOwnership(bottom, False)


		# Fit the ratio
		if fit:
			# Make finely binned ratio
			#ratio_histogram_fine = numerator_histogram_fine.Clone()
			#ratio_histogram_fine.Reset()
			#if x_range:
			#	ratio_histogram_fine.GetXaxis().SetRangeUser(x_range[0], x_range[1])
			#ratio_histogram_fine.SetName(numerator_histogram_fine.GetName() + "_ratio_fine_" + save_tag + str(time.time()))
			#ratio_histogram_fine.SetDirectory(0)
			#for bin in xrange(1, numerator_histogram_fine.GetNbinsX() + 1):
			#	# Undo bin normalization
			#	if numerator_histogram_fine.GetBinError(bin) > 0 and denominator_histogram_fine.GetBinError(bin) > 0:
			#		if prescaled:
			#			num = (numerator_histogram_fine.GetBinContent(bin))**2 / (numerator_histogram_fine.GetBinError(bin))**2
			#			den = (denominator_histogram_fine.GetBinContent(bin))**2 / (denominator_histogram_fine.GetBinError(bin))**2
			#		else:
			#			num = numerator_histogram_fine.GetBinContent(bin)
			#			den = denominator_histogram_fine.GetBinContent(bin)
			#		ratio = 1. * num / den 
			#		ratio_err = sqrt(ratio * (1. - ratio) / den)
			#		#ratio_err = max(sqrt(ratio * (1. - ratio) / den), 1./den)
			#	else:
			#		ratio = 0.
			#		ratio_err = 0.
			#	ratio_histogram_fine.SetBinContent(bin, ratio)
			#	ratio_histogram_fine.SetBinError(bin, ratio_err)

			print "Fitting " + save_tag + " with some functions"
			fitter = TBinomialEfficiencyFitter(numerator_histogram, denominator_histogram)
			fits = {}
			print "\nFitting quadratic"
			fits["quadratic"] = TF1("quadratic", "[0]+[1]*x+[2]*x*x", fit_range[0], fit_range[1])
			fits["quadratic"].SetParameter(0, 0.01)
			fits["quadratic"].SetParameter(1, -1.e-5)
			fits["quadratic"].SetParLimits(1, -1.e5, 1.e5)
			fits["quadratic"].SetParameter(2, 1.e-11)
			fits["quadratic"].SetParLimits(2, -1.e5, 1.e5)
			ratio_histogram.Fit(fits["quadratic"], "R0")

			print "\nFitting quadexp"
			fits["quadexp"] = TF1("quadexp", "[0]+[1]*x+[2]*x*x+ [3]*exp([4]*x)", fit_range[0], fit_range[1])
			fits["quadexp"].SetParameter(0, 3.97724e-03)
			fits["quadexp"].SetParameter(1, -3.69019e-06)
			fits["quadexp"].SetParameter(2, 1.37598e-09)
			fits["quadexp"].SetParameter(3, 2.95236e+00)
			fits["quadexp"].SetParameter(4, -1.54774e-02)
			fitter.Fit(fits["quadexp"], "R0")

			print "\nFitting linexp"
			fits["linexp"] = TF1("linexp", "[0]+[1]*x + [2]*exp([3]*x)", fit_range[0], fit_range[1])
			fits["linexp"].SetParameter(0, 3.97724e-03)
			fits["linexp"].SetParameter(1, -3.69019e-06)
			fits["linexp"].SetParameter(2, 2.95236e+00)
			fits["linexp"].SetParameter(3, -1.54774e-02)
			fitter.Fit(fits["linexp"], "R0")

			# Print chi2/ndfs
			for fit_name, fit_function in fits.iteritems():
				print "chi^2/ndf (p) for " + fit_name + " = " + str(fit_function.GetChisquare()) + "/" + str(fit_function.GetNDF()) + " (" + str(TMath.Prob(fit_function.GetChisquare(), fit_function.GetNDF()))

			pull_histograms = {}
			for fit_name, fit_function in fits.iteritems():
				print "[debug] Making pull histogram for " + fit_name
				pull_histograms[fit_name] = ratio_histogram.Clone()
				pull_histograms[fit_name].SetDirectory(0)
				pull_histograms[fit_name].SetName("ratio_fit_pulls_" + fit_name)
				pull_histograms[fit_name].Reset()
				for bin in xrange(1, pull_histograms[fit_name].GetNbinsX() + 1):
					bin_center = pull_histograms[fit_name].GetXaxis().GetBinCenter(bin)
					if (bin_center < fit_range[0]) or (bin_center > fit_range[1]):
						#print "Pull hist : skipping bin with center " + str(bin_center)
						pull_histograms[fit_name].SetBinContent(bin, 0.)
					elif ratio_histogram.GetBinError(bin):
						#print "[debug] Bin " + str(bin) + " / pull = " + str(round(ratio_histogram.GetBinContent(bin), 2)) + "-" + str(round(fit_function.Eval(bin_center), 2)) +  "/" +  str(round(ratio_histogram.GetBinError(bin), 2)) + " = " + str(round((ratio_histogram.GetBinContent(bin) - fit_function.Eval(bin_center)) / ratio_histogram.GetBinError(bin), 2))
						pull_histograms[fit_name].SetBinContent(bin, (ratio_histogram.GetBinContent(bin) - fit_function.Eval(bin_center)) / ratio_histogram.GetBinError(bin))
					else:
						pull_histograms[fit_name].SetBinContent(bin, 0.)
			c_eff_fit = TCanvas("c_offline_btag_fit", "c_offline_btag_fit", 800, 1000)
			top_eff_fit = TPad("top_fit", "top_fit", 0., 0.5, 1., 1.)
			top_eff_fit.SetBottomMargin(0.02)
			top_eff_fit.Draw()
			top_eff_fit.cd()
			ratio_histogram.GetXaxis().SetTitleSize(0)
			ratio_histogram.GetXaxis().SetLabelSize(0)
			ratio_histogram.GetYaxis().SetTitle("Offline b-tag efficiency")
			ratio_histogram.Draw()
			fits["quadratic"].SetLineColor(seaborn.GetColorRoot("dark", 2))
			fits["quadratic"].SetLineWidth(2)
			pull_histograms["quadratic"].SetLineColor(seaborn.GetColorRoot("dark", 2))
			fits["quadratic"].Draw("same")
			fits["linexp"].SetLineColor(seaborn.GetColorRoot("dark", 4))
			pull_histograms["linexp"].SetLineColor(seaborn.GetColorRoot("dark", 4))
			fits["linexp"].SetLineWidth(2)
			fits["linexp"].Draw("same")
			l_eff_fit = TLegend(0.6, 0.6, 0.88, 0.8)
			l_eff_fit.SetBorderSize(0)
			l_eff_fit.SetFillColor(0)
			l_eff_fit.AddEntry(ratio_histogram, "Data", "pl")
			l_eff_fit.AddEntry(fits["quadratic"], "Fit (quadratic)", "l")
			l_eff_fit.AddEntry(fits["linexp"], "Fit (linear+exp)", "l")
			l_eff_fit.Draw()

			c_eff_fit.cd()
			bottom_eff_fit = TPad("bottom_fit", "bottom_fit", 0., 0., 1., 0.5)
			bottom_eff_fit.SetBottomMargin(0.2)
			bottom_eff_fit.SetTopMargin(0.02)
			bottom_eff_fit.Draw()
			bottom_eff_fit.cd()
			if fit_range[0] <= 296:
				default_fit = "quadratic"
			else:
				default_fit = "linexp"
			pull_histograms[default_fit].SetLineWidth(2)
			pull_histograms[default_fit].GetXaxis().SetTitle("m_{jj} [GeV]")
			pull_histograms[default_fit].GetYaxis().SetTitle
			if "mjj" in var:
				pull_histograms[default_fit].GetXaxis().SetTitle("m_{jj} [GeV]")
			elif "pt" in var:
				pull_histograms[default_fit].GetXaxis().SetTitle("p_{T} [GeV]")
			pull_histograms[default_fit].GetYaxis().SetTitle("(fit - data) / (#sigma(data))")
			pull_histograms[default_fit].SetMinimum(-10.)
			pull_histograms[default_fit].SetMaximum(10.)
			pull_histograms[default_fit].Draw("hist")

			#pull_histograms["quadratic"].SetLineWidth(2)
			#pull_histograms["quadratic"].SetLineColor(seaborn.GetColorRoot("dark", 2))
			#pull_histograms["quadratic"].Draw("hist same")

			c_eff_fit.cd()
			c_eff_fit.SaveAs(analysis_config.figure_directory + "/OfflineBTag/" + c_eff_fit.GetName() + save_tag + ".pdf")
			
			ROOT.SetOwnership(c_eff_fit, False)
			ROOT.SetOwnership(top_eff_fit, False)
			ROOT.SetOwnership(bottom_eff_fit, False)
Example #9
0
		f = TFile(analysis_config.get_b_histogram_filename(analysis, qcd_sample), "READ")
		input_nevents = f.Get("BHistograms/h_input_nevents").Integral()
		this_h_mjj = f.Get("BHistograms/h_pfjet_mjj").Clone()
		this_h_mjj.Scale(19700.*qcd_cross_sections[qcd_sample] / input_nevents)
		this_h_mjj_truthbb = f.Get("BHistograms/h_pfjet_mjj_truthbb").Clone()
		this_h_mjj_truthbb.Scale(19700.*qcd_cross_sections[qcd_sample] / input_nevents)
		if not h_mjj:
			h_mjj = this_h_mjj.Clone()
			h_mjj.SetDirectory(0)
			h_mjj_truthbb = this_h_mjj_truthbb.Clone()
			h_mjj_truthbb.SetDirectory(0)
		else:
			h_mjj.Add(this_h_mjj)
			h_mjj_truthbb.Add(this_h_mjj_truthbb)
		f.Close()
	h_mjj = histogram_tools.rebin_histogram(h_mjj, dijet_binning, normalization_bin_width=1.)
	h_mjj_truthbb = histogram_tools.rebin_histogram(h_mjj_truthbb, dijet_binning, normalization_bin_width=1.)
	c = TCanvas("c_purity_{}".format(analysis), "c_purity_{}".format(analysis), 800, 1000)
	top = TPad("top", "top", 0., 0.5, 1., 1.)
	top.SetBottomMargin(0.02)
	top.SetLogy()
	top.Draw()
	top.cd()
	h_mjj.SetLineColor(1)
	h_mjj.SetLineWidth(2)
	h_mjj.GetXaxis().SetTitleSize(0)
	h_mjj.GetXaxis().SetLabelSize(0)
	h_mjj.GetYaxis().SetTitle("Events / GeV")
	h_mjj.GetXaxis().SetRangeUser(0., 2000.)
	h_mjj.Draw("hist")
	h_mjj_truthbb.SetLineColor(seaborn.GetColorRoot("cubehelixlarge", 2, 20))
		this_num_hist = f_num.Get("BHistograms/h_pfjet_mjj")
		#this_num_hist.Scale(19700.*qcd_cross_sections[qcd_sample] / input_nevents_num)
		input_nevents_den = f_den.Get("BHistograms/h_input_nevents").Integral()
		this_den_hist = f_den.Get("BHistograms/h_pfjet_mjj")
		#this_den_hist.Scale(19700.*qcd_cross_sections[qcd_sample] / input_nevents_den)
		if not sr in qcd_hists_num:
			qcd_hists_num[sr] = this_num_hist.Clone()
			qcd_hists_num[sr].SetDirectory(0)
			qcd_hists_den[sr] = this_den_hist.Clone()
			qcd_hists_den[sr].SetDirectory(0)
		else:
			qcd_hists_num[sr].Add(this_num_hist)
			qcd_hists_den[sr].Add(this_den_hist)
		f_num.Close()
		f_den.Close()
	qcd_hists_num[sr] = histogram_tools.rebin_histogram(qcd_hists_num[sr], dijet_binning)
	qcd_hists_den[sr] = histogram_tools.rebin_histogram(qcd_hists_den[sr], dijet_binning)
	for bin in xrange(1, qcd_hists_num[sr].GetNbinsX()+1):
		bin_center = qcd_hists_num[sr].GetXaxis().GetBinCenter(bin)
		if sr == "lowmass":
			if bin_center < 296 or bin_center > 1058:
				qcd_hists_num[sr].SetBinContent(bin, 0)
				qcd_hists_num[sr].SetBinError(bin, 0)
				qcd_hists_den[sr].SetBinContent(bin, 0)
				qcd_hists_den[sr].SetBinError(bin, 0)
		elif sr == "highmass":
			if bin_center < 526 or bin_center > 1607:
				qcd_hists_num[sr].SetBinContent(bin, 0)
				qcd_hists_num[sr].SetBinError(bin, 0)
				qcd_hists_den[sr].SetBinContent(bin, 0)
				qcd_hists_den[sr].SetBinError(bin, 0)
def plot_all_masses(analysis, model, masses):
    ratio_up = {}
    ratio_down = {}
    for mass in masses:
        f = TFile(
            analysis_config.get_b_histogram_filename(
                analysis,
                analysis_config.simulation.get_signal_tag(
                    model, mass, "FULLSIM")), "READ")
        h_central = f.Get("BHistograms/h_pfjet_mjj")
        h_central.SetDirectory(0)
        h_central = histogram_tools.rebin_histogram(h_central,
                                                    dijet_binning,
                                                    normalization_bin_width=1.)
        h_up = f.Get("BHistograms/h_pfjet_mjj_BTagOfflineSFUp")
        h_up.SetDirectory(0)
        h_up = histogram_tools.rebin_histogram(h_up,
                                               dijet_binning,
                                               normalization_bin_width=1.)
        h_down = f.Get("BHistograms/h_pfjet_mjj_BTagOfflineSFDown")
        h_down.SetDirectory(0)
        h_down = histogram_tools.rebin_histogram(h_down,
                                                 dijet_binning,
                                                 normalization_bin_width=1.)
        ratio_up[mass] = h_up.Clone()
        ratio_up[mass].SetName("ratio_up_{}".format(mass))
        ratio_up[mass].SetDirectory(0)
        ratio_up[mass].Divide(h_central)
        ratio_up[mass].SetMarkerStyle(26)
        ratio_up[mass].Draw("p hist same")
        ratio_down[mass] = h_down.Clone()
        ratio_down[mass].SetName("ratio_down_{}".format(mass))
        ratio_down[mass].SetDirectory(0)
        ratio_down[mass].Divide(h_central)
        ratio_down[mass].SetMarkerStyle(32)
        ratio_down[mass].Draw("p hist same")
        norm = h_central.Integral()
        print "{}\t{}\t{}:{}".format(analysis, model, mass,
                                     h_up.Integral() / norm - 1.)
        f.Close()

    c = TCanvas("c_offb_unc_{}_{}_all".format(analysis, model),
                "c_offb_unc_{}_{}_all".format(analysis, model), 800, 600)
    frame = TH1D("frame", "frame", 100, 0., 1300.)
    frame.SetMinimum(0.8)
    frame.SetMaximum(1.2)
    frame.GetXaxis().SetTitle("m_{jj}")
    frame.GetYaxis().SetTitle("SF Variation / Central")
    frame.Draw()
    for style_counter, mass in enumerate(masses):
        ratio_up[mass].SetMarkerStyle(20)
        ratio_up[mass].SetMarkerColor(
            seaborn.GetColorRoot("cubehelixlarge", style_counter,
                                 len(masses) + 2))
        ratio_up[mass].SetLineColor(
            seaborn.GetColorRoot("cubehelixlarge", style_counter,
                                 len(masses) + 2))
        ratio_up[mass].Draw("hist same")
        ratio_down[mass].SetMarkerStyle(20)
        ratio_down[mass].SetMarkerColor(
            seaborn.GetColorRoot("cubehelixlarge", style_counter,
                                 len(masses) + 2))
        ratio_down[mass].SetLineColor(
            seaborn.GetColorRoot("cubehelixlarge", style_counter,
                                 len(masses) + 2))
        ratio_down[mass].Draw("hist same")
    c.SaveAs(analysis_config.figure_directory + "/" + c.GetName() + ".pdf")
variables = ["mjj", "pt1", "pt2", "pt_btag1", "pt_btag2"]
histograms = {}
for analysis in analyses:
    histograms[analysis] = {}
    for variable in variables:
        histograms[analysis][variable] = files[analysis].Get(
            "BHistograms/h_pfjet_" + variable)
        histograms[analysis][variable].SetName("h_" + analysis + "_" +
                                               variable)
        histograms[analysis][variable].SetDirectory(0)
        if analysis == "trigmubbll_lowmass":
            histograms[analysis][variable].Scale(1.7)  # Prescale
        if variable == "mjj":
            print "(rebinning)...",
            histograms[analysis][variable] = histogram_tools.rebin_histogram(
                histograms[analysis][variable],
                mass_bins,
                normalization_bin_width=1)
            print "(done)"
        elif "pt" in variable:
            histograms[analysis][variable].Rebin(25)

print "Making efficiency histograms"
efficiency_pairs = {
    "highmass": ["trigmubbh_highmass_CSVTM", "trigmu_highmass_CSVTM"],
    "lowmass": ["trigmubbl_lowmass_CSVTM", "trigmu_lowmass_CSVTM"],
    "llowmass": ["trigmubbll_lowmass_CSVTM", "trigmu_lowmass_CSVTM"]
}
efficiency_titles = {
    "highmass": "160/120",
    "lowmass": "80/70",
    "llowmass": "60/53"
    def FrankenEfficiencyPlot(self,
                              logy=True,
                              binning=None,
                              simple_rebin=None,
                              save_tag="",
                              x_range=None,
                              ratio_range=None,
                              legend_position="topright",
                              numerator_legend=None,
                              denominator_legend=None,
                              ratio_title=None):
        ht_slices = [
            "HT200", "HT250", "HT300", "HT350", "HT400", "HT450", "HT500",
            "HT550", "HTUnprescaled"
        ]  # HT650
        if self._numerator_analysis == "trigjetht_eta1p7_CSVTM":
            numerator_analyses = {
                "HT200": "trigjetht200_eta1p7_CSVTM",
                "HT250": "trigjetht250_eta1p7_CSVTM",
                "HT300": "trigjetht300_eta1p7_CSVTM",
                "HT350": "trigjetht350_eta1p7_CSVTM",
                "HT400": "trigjetht400_eta1p7_CSVTM",
                "HT450": "trigjetht450_eta1p7_CSVTM",
                "HT500": "trigjetht500_eta1p7_CSVTM",
                "HT550": "trigjetht550_eta1p7_CSVTM",
                #"HT650":"trigjetht650_eta1p7_CSVTM",
                "HTUnprescaled": "trigjetht_eta1p7_CSVTM"
            }
        elif self._numerator_analysis == "trigjetht_eta2p2_CSVTM":
            numerator_analyses = {
                "HT200": "trigjetht200_CSVTM",
                "HT250": "trigjetht250_CSVTM",
                "HT300": "trigjetht300_CSVTM",
                "HT350": "trigjetht350_CSVTM",
                "HT400": "trigjetht400_CSVTM",
                "HT450": "trigjetht450_CSVTM",
                "HT500": "trigjetht500_CSVTM",
                "HT550": "trigjetht550_CSVTM",
                #"HT650":"trigjetht650_CSVTM",
                "HTUnprescaled": "trigjetht_CSVTM"
            }
        else:
            print "[OfflineBTagPlots::FrankenEfficiencyPlot] ERROR : numerator analysis must be trigjetht_eta1p7_CSVTM or trigjetht_eta2p2_CSVTM"
            sys.exit(1)
        if self._denominator_analysis == "trigjetht_eta1p7":
            denominator_analyses = {
                "HT200": "trigjetht200_eta1p7",
                "HT250": "trigjetht250_eta1p7",
                "HT300": "trigjetht300_eta1p7",
                "HT350": "trigjetht350_eta1p7",
                "HT400": "trigjetht400_eta1p7",
                "HT450": "trigjetht450_eta1p7",
                "HT500": "trigjetht500_eta1p7",
                "HT550": "trigjetht550_eta1p7",
                #"HT650":"trigjetht650_eta1p7",
                "HTUnprescaled": "trigjetht_eta1p7"
            }
        elif self._denominator_analysis == "trigjetht_eta2p2":
            denominator_analyses = {
                "HT200": "trigjetht200",
                "HT250": "trigjetht250",
                "HT300": "trigjetht300",
                "HT350": "trigjetht350",
                "HT400": "trigjetht400",
                "HT450": "trigjetht450",
                "HT500": "trigjetht500",
                "HT550": "trigjetht550",
                #"HT650":"trigjetht650",
                "HTUnprescaled": "trigjetht"
            }
        else:
            print "[OfflineBTagPlots::FrankenEfficiencyPlot] ERROR : denominator analysis must be trigjetht_eta1p7 or trigjetht_eta2p2"
            sys.exit(1)
        ht_ranges = {
            "HT200": [220, 386],
            "HT250": [386, 489],
            "HT300": [489, 526],
            "HT350": [526, 606],
            "HT400": [606, 649],
            "HT450": [649, 740],
            "HT500": [740, 788],
            "HT550": [788, 890],
            #"HT650":[890, 2000],
            "HTUnprescaled": [890, 2000]
        }
        numerator_histogram = None
        denominator_histogram = None
        for sample in self._samples:
            print "[EfficiencyPlot] DEBUG : Sample " + sample
            numerator_slice_histograms = {}
            denominator_slice_histograms = {}
            for slice_name in ht_slices:
                print slice_name
                print analysis_config.get_b_histogram_filename(
                    numerator_analyses[slice_name], sample)
                numerator_file = TFile(
                    analysis_config.get_b_histogram_filename(
                        numerator_analyses[slice_name], sample), "READ")
                print analysis_config.get_b_histogram_filename(
                    denominator_analyses[slice_name], sample)
                denominator_file = TFile(
                    analysis_config.get_b_histogram_filename(
                        denominator_analyses[slice_name], sample), "READ")

                # Check input nevents
                num_nevents = numerator_file.Get(
                    "BHistograms/h_input_nevents").Integral()
                den_nevents = denominator_file.Get(
                    "BHistograms/h_input_nevents").Integral()
                if num_nevents != den_nevents:
                    # Allow tiny differences...?
                    if abs((num_nevents - den_nevents) / den_nevents) < 0.001:
                        print "[EfficiencyPlot] ERROR : Small inconsistency between number of events between numerator and denominator. I'm going to rescale away the difference, but you may want to fix this."
                        numerator_normalization = den_nevents / num_nevents
                        denominator_normalization = 1.
                    else:
                        print "[EfficiencyPlot] ERROR : Inconsistent number of events between numerator and denominator. Results would be wrong, so I'm aborting."
                        print "[EfficiencyPlot] ERROR : \tNumerator = " + str(
                            numerator_file.Get(
                                "BHistograms/h_input_nevents").Integral())
                        print "[EfficiencyPlot] ERROR : \tDenominator = " + str(
                            denominator_file.Get(
                                "BHistograms/h_input_nevents").Integral())
                        sys.exit(1)
                else:
                    numerator_normalization = 1.
                    denominator_normalization = 1.
                numerator_slice_histograms[slice_name] = numerator_file.Get(
                    "BHistograms/h_pfjet_mjj")
                numerator_slice_histograms[slice_name].SetName(
                    "h_pfjet_mjj_num_" + slice_name + "_" + sample)
                numerator_slice_histograms[slice_name].SetDirectory(0)
                numerator_slice_histograms[slice_name].Scale(
                    numerator_normalization)
                denominator_slice_histograms[
                    slice_name] = denominator_file.Get(
                        "BHistograms/h_pfjet_mjj")
                denominator_slice_histograms[slice_name].SetName(
                    "h_pfjet_mjj_den_" + slice_name + "_" + sample)
                denominator_slice_histograms[slice_name].SetDirectory(0)
                denominator_slice_histograms[slice_name].Scale(
                    denominator_normalization)
                numerator_file.Close()
                denominator_file.Close()
            # Make frankenhist
            this_numerator_histogram = self.FrankenHist(
                ht_slices, numerator_slice_histograms, ht_ranges)
            this_denominator_histogram = self.FrankenHist(
                ht_slices, denominator_slice_histograms, ht_ranges)
            if not numerator_histogram:
                numerator_histogram = this_numerator_histogram.Clone()
                numerator_histogram.SetName(numerator_histogram.GetName() +
                                            save_tag + "_num_" +
                                            str(time.time()))
                denominator_histogram = this_denominator_histogram.Clone()
                denominator_histogram.SetName(denominator_histogram.GetName() +
                                              save_tag + "_num_" +
                                              str(time.time()))
            else:
                numerator_histogram.Add(this_numerator_histogram)
                denominator_histogram.Add(this_denominator_histogram)

        # Rebin
        if binning:
            numerator_histogram = histogram_tools.rebin_histogram(
                numerator_histogram, binning)
            denominator_histogram = histogram_tools.rebin_histogram(
                denominator_histogram, binning)
        elif simple_rebin:
            numerator_histogram.Rebin(simple_rebin)
            denominator_histogram.Rebin(simple_rebin)

        cname = "c_offline_btag_eff_mjj"
        if logy:
            cname += "_log"
        c = TCanvas(cname, "Offline b-tag #epsilon", 800, 1000)
        top = TPad("top", "top", 0., 0.5, 1., 1.)
        top.SetBottomMargin(0.02)
        if logy:
            top.SetLogy()
        top.Draw()
        top.cd()

        frame_top = numerator_histogram.Clone()
        frame_top.Reset()
        if x_range:
            frame_top.GetXaxis().SetRangeUser(x_range[0], x_range[1])
        if logy:
            y_min = 0.1
            y_max = max(numerator_histogram.GetMaximum(),
                        denominator_histogram.GetMaximum()) * 10.
        else:
            y_min = 0.
            y_max = max(numerator_histogram.GetMaximum(),
                        denominator_histogram.GetMaximum()) * 1.5
        frame_top.SetMinimum(y_min)
        frame_top.SetMaximum(y_max)
        frame_top.GetXaxis().SetLabelSize(0)
        frame_top.GetXaxis().SetTitleSize(0)
        if binning:
            #frame_top.GetYaxis().SetTitle("Events / 1 GeV")
            frame_top.GetYaxis().SetTitle("Events")
        else:
            frame_top.GetYaxis().SetTitle("Events")
        frame_top.Draw("axis")
        print "numerator integral = " + str(numerator_histogram.Integral())
        print "denominator integral = " + str(denominator_histogram.Integral())
        numerator_histogram.SetMarkerStyle(20)
        numerator_histogram.SetMarkerColor(seaborn.GetColorRoot("default", 0))
        numerator_histogram.SetLineColor(seaborn.GetColorRoot("default", 0))
        numerator_histogram.Draw("same")
        denominator_histogram.SetMarkerStyle(24)
        denominator_histogram.SetMarkerColor(seaborn.GetColorRoot(
            "default", 2))
        denominator_histogram.SetLineColor(seaborn.GetColorRoot("default", 2))
        denominator_histogram.Draw("same")
        if legend_position == "topright":
            l = TLegend(0.6, 0.6, 0.85, 0.8)
        elif legend_position == "bottomright":
            l = TLegend(0.6, 0.2, 0.85, 0.4)

        l.SetFillColor(0)
        l.SetBorderSize(0)
        if numerator_legend:
            l.AddEntry(numerator_histogram, numerator_legend)
        else:
            l.AddEntry(numerator_histogram, "CSVT+CSVM")
        if denominator_legend:
            l.AddEntry(denominator_histogram, denominator_legend)

        else:
            l.AddEntry(denominator_histogram, "No CSV")
        l.Draw()

        c.cd()
        bottom = TPad("bottom", "bottom", 0., 0., 1., 0.5)
        bottom.SetTopMargin(0.01)
        bottom.SetBottomMargin(0.2)
        bottom.Draw()
        bottom.cd()
        ratio_histogram = numerator_histogram.Clone()
        ratio_histogram.Reset()
        if x_range:
            ratio_histogram.GetXaxis().SetRangeUser(x_range[0], x_range[1])
        ratio_histogram.SetName(numerator_histogram.GetName() + "_ratio_" +
                                save_tag + str(time.time()))
        ratio_histogram.SetDirectory(0)
        for bin in xrange(1, numerator_histogram.GetNbinsX() + 1):
            # Undo bin normalization
            if numerator_histogram.GetBinError(
                    bin) > 0 and denominator_histogram.GetBinError(bin) > 0:
                num_unnormalized = (numerator_histogram.GetBinContent(
                    bin))**2 / (numerator_histogram.GetBinError(bin))**2
                den_unnormalized = (denominator_histogram.GetBinContent(
                    bin))**2 / (denominator_histogram.GetBinError(bin))**2
                num = numerator_histogram.GetBinContent(bin)
                den = denominator_histogram.GetBinContent(bin)
                ratio = 1. * num_unnormalized / den_unnormalized
                ratio_err = sqrt(ratio * (1. - ratio) / den_unnormalized)
                #ratio_err = max(sqrt(ratio * (1. - ratio) / den), 1./den)
            else:
                ratio = 0.
                ratio_err = 0.
            ratio_histogram.SetBinContent(bin, ratio)
            ratio_histogram.SetBinError(bin, ratio_err)
        ratio_histogram.SetMarkerSize(1)
        ratio_histogram.SetMarkerColor(kBlack)
        ratio_histogram.SetLineColor(kBlack)
        ratio_histogram.SetLineWidth(2)
        ratio_histogram.GetXaxis().SetTitle("m_{jj} [GeV]")
        if ratio_title:
            ratio_histogram.GetYaxis().SetTitle(ratio_title)
        else:
            ratio_histogram.GetYaxis().SetTitle(
                "Offline 2#timesb-tag efficiency")
        if ratio_range:
            ratio_histogram.SetMinimum(ratio_range[0])
            ratio_histogram.SetMaximum(ratio_range[1])
        else:
            ratio_histogram.SetMinimum(0.)
            ratio_histogram.SetMaximum(1.)
        ratio_histogram.Draw()

        c.cd()
        c.SaveAs(analysis_config.figure_directory + "/OfflineBTag/" +
                 c.GetName() + save_tag + ".pdf")
        ROOT.SetOwnership(c, False)
        ROOT.SetOwnership(top, False)
        ROOT.SetOwnership(bottom, False)
    def EfficiencyPlot(self,
                       var="mjj",
                       logy=True,
                       binning=None,
                       simple_rebin=None,
                       save_tag="",
                       sample_xsecs=None,
                       x_range=None,
                       ratio_range=None,
                       legend_position="topright",
                       numerator_legend=None,
                       denominator_legend=None,
                       ratio_title=None,
                       prescaled=False,
                       fit=False,
                       fit_range=None):
        numerator_histogram = None
        denominator_histogram = None
        for sample in self._samples:
            if sample_xsecs:
                xsec = sample_xsecs[sample]
                numerator_normalization = 19710. * xsec / self._input_files[
                    "numerator"][sample].Get(
                        "BHistograms/h_sample_nevents").Integral()
                denominator_normalization = 19710. * xsec / self._input_files[
                    "denominator"][sample].Get(
                        "BHistograms/h_sample_nevents").Integral()
                if numerator_normalization != denominator_normalization:
                    print "[EfficiencyPlot] WARNING : Numerator and denominator normalizations do not match! Continuing, but investigate this. Numerator nevents = " + str(
                        self._input_files["numerator"]
                        [sample].Get("BHistograms/h_sample_nevents").Integral(
                        )) + ", denominator nevents = " + str(
                            self._input_files["denominator"][sample].Get(
                                "BHistograms/h_sample_nevents").Integral())
            else:
                # Check input nevents
                if self._input_files["numerator"][sample].Get(
                        "BHistograms/h_sample_nevents"):
                    num_nevents = self._input_files["numerator"][sample].Get(
                        "BHistograms/h_sample_nevents").Integral()
                    den_nevents = self._input_files["denominator"][sample].Get(
                        "BHistograms/h_sample_nevents").Integral()
                else:
                    num_nevents = self._input_files["numerator"][sample].Get(
                        "BHistograms/h_input_nevents").Integral()
                    den_nevents = self._input_files["denominator"][sample].Get(
                        "BHistograms/h_input_nevents").Integral()
                if num_nevents != den_nevents:
                    # Allow tiny differences...?
                    if abs((num_nevents - den_nevents) / den_nevents) < 0.001:
                        print "[EfficiencyPlot] ERROR : Small inconsistency between number of events between numerator and denominator. I'm going to rescale away the difference, but you may want to fix this."
                        numerator_normalization = den_nevents / num_nevents
                        denominator_normalization = 1.
                    else:
                        print "[EfficiencyPlot] ERROR : Inconsistent number of events between numerator and denominator. Results would be wrong, so I'm aborting."
                        print "[EfficiencyPlot] ERROR : \tNumerator = " + str(
                            self._input_files["numerator"][sample].Get(
                                "BHistograms/h_sample_nevents").Integral())
                        print "[EfficiencyPlot] ERROR : \tDenominator = " + str(
                            self._input_files["denominator"][sample].Get(
                                "BHistograms/h_sample_nevents").Integral())
                        sys.exit(1)
                else:
                    numerator_normalization = 1.
                    denominator_normalization = 1.
            if numerator_histogram:
                numerator_histogram.Add(
                    self._input_files["numerator"][sample].Get(
                        "BHistograms/h_pfjet_" + var), numerator_normalization)
            else:
                numerator_histogram = self._input_files["numerator"][
                    sample].Get("BHistograms/h_pfjet_" + var).Clone()
                numerator_histogram.Scale(numerator_normalization)
                numerator_histogram.SetDirectory(0)
                numerator_histogram.SetName(numerator_histogram.GetName() +
                                            save_tag + "_num" +
                                            str(time.time()))
            if denominator_histogram:
                denominator_histogram.Add(
                    self._input_files["denominator"][sample].Get(
                        "BHistograms/h_pfjet_" + var),
                    denominator_normalization)
            else:
                denominator_histogram = self._input_files["denominator"][
                    sample].Get("BHistograms/h_pfjet_" + var).Clone()
                denominator_histogram.Scale(denominator_normalization)
                denominator_histogram.SetDirectory(0)
                denominator_histogram.SetName(denominator_histogram.GetName() +
                                              save_tag + "_den" +
                                              str(time.time()))

        # Rebin
        numerator_histogram_fine = numerator_histogram.Clone()
        denominator_histogram_fine = denominator_histogram.Clone()
        if binning:
            numerator_histogram = histogram_tools.rebin_histogram(
                numerator_histogram, binning)
            denominator_histogram = histogram_tools.rebin_histogram(
                denominator_histogram, binning)
        elif simple_rebin:
            numerator_histogram.Rebin(simple_rebin)
            denominator_histogram.Rebin(simple_rebin)

        cname = "c_offline_btag_eff_" + var
        if logy:
            cname += "_log"
        c = TCanvas(cname, "Offline b-tag #epsilon", 800, 1000)
        top = TPad("top", "top", 0., 0.5, 1., 1.)
        top.SetBottomMargin(0.02)
        if logy:
            top.SetLogy()
        top.Draw()
        top.cd()

        frame_top = numerator_histogram.Clone()
        frame_top.Reset()
        if x_range:
            frame_top.GetXaxis().SetRangeUser(x_range[0], x_range[1])
        if logy:
            y_min = 0.1
            y_max = max(numerator_histogram.GetMaximum(),
                        denominator_histogram.GetMaximum()) * 10.
        else:
            y_min = 0.
            y_max = max(numerator_histogram.GetMaximum(),
                        denominator_histogram.GetMaximum()) * 1.5
        frame_top.SetMinimum(y_min)
        frame_top.SetMaximum(y_max)
        frame_top.GetXaxis().SetLabelSize(0)
        frame_top.GetXaxis().SetTitleSize(0)
        if binning:
            #frame_top.GetYaxis().SetTitle("Events / 1 GeV")
            frame_top.GetYaxis().SetTitle("Events")
        else:
            frame_top.GetYaxis().SetTitle("Events")
        frame_top.Draw("axis")
        print "numerator integral = " + str(numerator_histogram.Integral())
        print "denominator integral = " + str(denominator_histogram.Integral())
        numerator_histogram.SetMarkerStyle(20)
        numerator_histogram.SetMarkerColor(seaborn.GetColorRoot("default", 0))
        numerator_histogram.SetLineColor(seaborn.GetColorRoot("default", 0))
        numerator_histogram.Draw("same")
        denominator_histogram.SetMarkerStyle(24)
        denominator_histogram.SetMarkerColor(seaborn.GetColorRoot(
            "default", 2))
        denominator_histogram.SetLineColor(seaborn.GetColorRoot("default", 2))
        denominator_histogram.Draw("same")
        if legend_position == "topright":
            l = TLegend(0.6, 0.6, 0.85, 0.8)
        elif legend_position == "bottomright":
            l = TLegend(0.6, 0.2, 0.85, 0.4)

        l.SetFillColor(0)
        l.SetBorderSize(0)
        if numerator_legend:
            l.AddEntry(numerator_histogram, numerator_legend)
        else:
            l.AddEntry(numerator_histogram, "CSVT+CSVM")
        if denominator_legend:
            l.AddEntry(denominator_histogram, denominator_legend)

        else:
            l.AddEntry(denominator_histogram, "No CSV")
        l.Draw()

        c.cd()
        bottom = TPad("bottom", "bottom", 0., 0., 1., 0.5)
        bottom.SetTopMargin(0.01)
        bottom.SetBottomMargin(0.2)
        bottom.Draw()
        bottom.cd()
        ratio_histogram = numerator_histogram.Clone()
        ratio_histogram.Reset()
        if x_range:
            ratio_histogram.GetXaxis().SetRangeUser(x_range[0], x_range[1])
        ratio_histogram.SetName(numerator_histogram.GetName() + "_ratio_" +
                                save_tag + str(time.time()))
        ratio_histogram.SetDirectory(0)
        for bin in xrange(1, numerator_histogram.GetNbinsX() + 1):
            # Undo bin normalization
            if numerator_histogram.GetBinError(
                    bin) > 0 and denominator_histogram.GetBinError(bin) > 0:
                if prescaled:
                    num = (numerator_histogram.GetBinContent(bin))**2 / (
                        numerator_histogram.GetBinError(bin))**2
                    den = (denominator_histogram.GetBinContent(bin))**2 / (
                        denominator_histogram.GetBinError(bin))**2
                else:
                    num = numerator_histogram.GetBinContent(bin)
                    den = denominator_histogram.GetBinContent(bin)
                ratio = 1. * num / den
                ratio_err = sqrt(ratio * (1. - ratio) / den)
                #ratio_err = max(sqrt(ratio * (1. - ratio) / den), 1./den)
            else:
                ratio = 0.
                ratio_err = 0.
            ratio_histogram.SetBinContent(bin, ratio)
            ratio_histogram.SetBinError(bin, ratio_err)

        ratio_histogram.SetMarkerSize(1)
        ratio_histogram.SetMarkerColor(kBlack)
        ratio_histogram.SetLineColor(kBlack)
        ratio_histogram.SetLineWidth(2)
        if "mjj" in var:
            ratio_histogram.GetXaxis().SetTitle("m_{jj} [GeV]")
        elif "pt" in var:
            ratio_histogram.GetXaxis().SetTitle("p_{T} [GeV]")
        if ratio_title:
            ratio_histogram.GetYaxis().SetTitle(ratio_title)
        else:
            ratio_histogram.GetYaxis().SetTitle(
                "Offline 2#timesb-tag efficiency")
        if ratio_range:
            ratio_histogram.SetMinimum(ratio_range[0])
            ratio_histogram.SetMaximum(ratio_range[1])
        else:
            ratio_histogram.SetMinimum(0.)
            ratio_histogram.SetMaximum(1.)
        ratio_histogram.Draw()

        c.cd()
        c.SaveAs(analysis_config.figure_directory + "/OfflineBTag/" +
                 c.GetName() + save_tag + ".pdf")
        ROOT.SetOwnership(c, False)
        ROOT.SetOwnership(top, False)
        ROOT.SetOwnership(bottom, False)

        # Fit the ratio
        if fit:
            # Make finely binned ratio
            #ratio_histogram_fine = numerator_histogram_fine.Clone()
            #ratio_histogram_fine.Reset()
            #if x_range:
            #	ratio_histogram_fine.GetXaxis().SetRangeUser(x_range[0], x_range[1])
            #ratio_histogram_fine.SetName(numerator_histogram_fine.GetName() + "_ratio_fine_" + save_tag + str(time.time()))
            #ratio_histogram_fine.SetDirectory(0)
            #for bin in xrange(1, numerator_histogram_fine.GetNbinsX() + 1):
            #	# Undo bin normalization
            #	if numerator_histogram_fine.GetBinError(bin) > 0 and denominator_histogram_fine.GetBinError(bin) > 0:
            #		if prescaled:
            #			num = (numerator_histogram_fine.GetBinContent(bin))**2 / (numerator_histogram_fine.GetBinError(bin))**2
            #			den = (denominator_histogram_fine.GetBinContent(bin))**2 / (denominator_histogram_fine.GetBinError(bin))**2
            #		else:
            #			num = numerator_histogram_fine.GetBinContent(bin)
            #			den = denominator_histogram_fine.GetBinContent(bin)
            #		ratio = 1. * num / den
            #		ratio_err = sqrt(ratio * (1. - ratio) / den)
            #		#ratio_err = max(sqrt(ratio * (1. - ratio) / den), 1./den)
            #	else:
            #		ratio = 0.
            #		ratio_err = 0.
            #	ratio_histogram_fine.SetBinContent(bin, ratio)
            #	ratio_histogram_fine.SetBinError(bin, ratio_err)

            print "Fitting " + save_tag + " with some functions"
            fitter = TBinomialEfficiencyFitter(numerator_histogram,
                                               denominator_histogram)
            fits = {}
            print "\nFitting quadratic"
            fits["quadratic"] = TF1("quadratic", "[0]+[1]*x+[2]*x*x",
                                    fit_range[0], fit_range[1])
            fits["quadratic"].SetParameter(0, 0.01)
            fits["quadratic"].SetParameter(1, -1.e-5)
            fits["quadratic"].SetParLimits(1, -1.e5, 1.e5)
            fits["quadratic"].SetParameter(2, 1.e-11)
            fits["quadratic"].SetParLimits(2, -1.e5, 1.e5)
            ratio_histogram.Fit(fits["quadratic"], "R0")

            print "\nFitting quadexp"
            fits["quadexp"] = TF1("quadexp",
                                  "[0]+[1]*x+[2]*x*x+ [3]*exp([4]*x)",
                                  fit_range[0], fit_range[1])
            fits["quadexp"].SetParameter(0, 3.97724e-03)
            fits["quadexp"].SetParameter(1, -3.69019e-06)
            fits["quadexp"].SetParameter(2, 1.37598e-09)
            fits["quadexp"].SetParameter(3, 2.95236e+00)
            fits["quadexp"].SetParameter(4, -1.54774e-02)
            fitter.Fit(fits["quadexp"], "R0")

            print "\nFitting linexp"
            fits["linexp"] = TF1("linexp", "[0]+[1]*x + [2]*exp([3]*x)",
                                 fit_range[0], fit_range[1])
            fits["linexp"].SetParameter(0, 3.97724e-03)
            fits["linexp"].SetParameter(1, -3.69019e-06)
            fits["linexp"].SetParameter(2, 2.95236e+00)
            fits["linexp"].SetParameter(3, -1.54774e-02)
            fitter.Fit(fits["linexp"], "R0")

            # Print chi2/ndfs
            for fit_name, fit_function in fits.iteritems():
                print "chi^2/ndf (p) for " + fit_name + " = " + str(
                    fit_function.GetChisquare()) + "/" + str(
                        fit_function.GetNDF()) + " (" + str(
                            TMath.Prob(fit_function.GetChisquare(),
                                       fit_function.GetNDF()))

            pull_histograms = {}
            for fit_name, fit_function in fits.iteritems():
                print "[debug] Making pull histogram for " + fit_name
                pull_histograms[fit_name] = ratio_histogram.Clone()
                pull_histograms[fit_name].SetDirectory(0)
                pull_histograms[fit_name].SetName("ratio_fit_pulls_" +
                                                  fit_name)
                pull_histograms[fit_name].Reset()
                for bin in xrange(1,
                                  pull_histograms[fit_name].GetNbinsX() + 1):
                    bin_center = pull_histograms[fit_name].GetXaxis(
                    ).GetBinCenter(bin)
                    if (bin_center < fit_range[0]) or (bin_center >
                                                       fit_range[1]):
                        #print "Pull hist : skipping bin with center " + str(bin_center)
                        pull_histograms[fit_name].SetBinContent(bin, 0.)
                    elif ratio_histogram.GetBinError(bin):
                        #print "[debug] Bin " + str(bin) + " / pull = " + str(round(ratio_histogram.GetBinContent(bin), 2)) + "-" + str(round(fit_function.Eval(bin_center), 2)) +  "/" +  str(round(ratio_histogram.GetBinError(bin), 2)) + " = " + str(round((ratio_histogram.GetBinContent(bin) - fit_function.Eval(bin_center)) / ratio_histogram.GetBinError(bin), 2))
                        pull_histograms[fit_name].SetBinContent(
                            bin, (ratio_histogram.GetBinContent(bin) -
                                  fit_function.Eval(bin_center)) /
                            ratio_histogram.GetBinError(bin))
                    else:
                        pull_histograms[fit_name].SetBinContent(bin, 0.)
            c_eff_fit = TCanvas("c_offline_btag_fit", "c_offline_btag_fit",
                                800, 1000)
            top_eff_fit = TPad("top_fit", "top_fit", 0., 0.5, 1., 1.)
            top_eff_fit.SetBottomMargin(0.02)
            top_eff_fit.Draw()
            top_eff_fit.cd()
            ratio_histogram.GetXaxis().SetTitleSize(0)
            ratio_histogram.GetXaxis().SetLabelSize(0)
            ratio_histogram.GetYaxis().SetTitle("Offline b-tag efficiency")
            ratio_histogram.Draw()
            fits["quadratic"].SetLineColor(seaborn.GetColorRoot("dark", 2))
            fits["quadratic"].SetLineWidth(2)
            pull_histograms["quadratic"].SetLineColor(
                seaborn.GetColorRoot("dark", 2))
            fits["quadratic"].Draw("same")
            fits["linexp"].SetLineColor(seaborn.GetColorRoot("dark", 4))
            pull_histograms["linexp"].SetLineColor(
                seaborn.GetColorRoot("dark", 4))
            fits["linexp"].SetLineWidth(2)
            fits["linexp"].Draw("same")
            l_eff_fit = TLegend(0.6, 0.6, 0.88, 0.8)
            l_eff_fit.SetBorderSize(0)
            l_eff_fit.SetFillColor(0)
            l_eff_fit.AddEntry(ratio_histogram, "Data", "pl")
            l_eff_fit.AddEntry(fits["quadratic"], "Fit (quadratic)", "l")
            l_eff_fit.AddEntry(fits["linexp"], "Fit (linear+exp)", "l")
            l_eff_fit.Draw()

            c_eff_fit.cd()
            bottom_eff_fit = TPad("bottom_fit", "bottom_fit", 0., 0., 1., 0.5)
            bottom_eff_fit.SetBottomMargin(0.2)
            bottom_eff_fit.SetTopMargin(0.02)
            bottom_eff_fit.Draw()
            bottom_eff_fit.cd()
            if fit_range[0] <= 296:
                default_fit = "quadratic"
            else:
                default_fit = "linexp"
            pull_histograms[default_fit].SetLineWidth(2)
            pull_histograms[default_fit].GetXaxis().SetTitle("m_{jj} [GeV]")
            pull_histograms[default_fit].GetYaxis().SetTitle
            if "mjj" in var:
                pull_histograms[default_fit].GetXaxis().SetTitle(
                    "m_{jj} [GeV]")
            elif "pt" in var:
                pull_histograms[default_fit].GetXaxis().SetTitle("p_{T} [GeV]")
            pull_histograms[default_fit].GetYaxis().SetTitle(
                "(fit - data) / (#sigma(data))")
            pull_histograms[default_fit].SetMinimum(-10.)
            pull_histograms[default_fit].SetMaximum(10.)
            pull_histograms[default_fit].Draw("hist")

            #pull_histograms["quadratic"].SetLineWidth(2)
            #pull_histograms["quadratic"].SetLineColor(seaborn.GetColorRoot("dark", 2))
            #pull_histograms["quadratic"].Draw("hist same")

            c_eff_fit.cd()
            c_eff_fit.SaveAs(analysis_config.figure_directory +
                             "/OfflineBTag/" + c_eff_fit.GetName() + save_tag +
                             ".pdf")

            ROOT.SetOwnership(c_eff_fit, False)
            ROOT.SetOwnership(top_eff_fit, False)
            ROOT.SetOwnership(bottom_eff_fit, False)
                denominator_legend="Without offline b-tag",
                ratio_title="Offline b-tag efficiency)")

        #for ht_slice in [200, 250, 300, 350, 400, 450, 500, 550, 650]:
        #	if sr == "lowmass":
        #		numerator_analysis = "trigjetht" + str(ht_slice) + "_eta1p7_CSVTM"
        #		denominator_analysis = "trigjetht" + str(ht_slice) + "_eta1p7"
        #	else:
        #		numerator_analysis = "trigjetht" + str(ht_slice) + "_CSVTM"
        #		denominator_analysis = "trigjetht" + str(ht_slice)

        #	plotter = OfflineBTagPlots(numerator_analysis, denominator_analysis, ["JetHT_2012BCD"])
        #	plotter.EfficiencyPlot(var="mjj", logy=True, binning=dijet_binning, save_tag="_CSVTM_" + sr + "_jetht" + str(ht_slice), ratio_range=[0.,0.01], x_range=[0., 2000.], legend_position="topright", numerator_legend="With offline b-tag", denominator_legend="Without offline b-tag", ratio_title="Offline b-tag efficiency", prescaled=True)

    if args.do_singlejet:
        for analysis in ["trigbbl_CSVTM", "trigbbh_CSVTM"]:
            f = TFile(
                analysis_config.get_b_histogram_filename(
                    analysis, "BJetPlusX_2012"), "READ")
            h_mjj = histogram_tools.rebin_histogram(
                f.Get("BHistograms/h_pfjet_mjj"), dijet_binning)
            h_mjj_btagcorr = histogram_tools.rebin_histogram(
                f.Get("BHistograms/h_pfjet_mjj_btagcorr"), dijet_binning)
            EfficiencyPlot(h_mjj,
                           h_mjj_btagcorr,
                           name_num="No correction",
                           name_den="b tag correction",
                           logy=True,
                           save_directory=analysis_config.figure_directory +
                           "/OfflineBTag",
                           save_tag="from_singlejet_" + analysis)
Example #16
0
	def rooplot(self, save_tag, fit_functions, background_workspaces, fitted_signal_workspaces=None, expected_signal_workspaces=None, log=False, x_range=None, data_binning=None, normalization_bin_width=1, draw_chi2ndf=False):
		print "RooPlotting " + save_tag
		c = TCanvas("c_" + save_tag, "c_" + save_tag, 800, 1200)
		l = TLegend(0.55, 0.6, 0.88, 0.88)
		l.SetFillColor(0)
		l.SetBorderSize(0)
		top = TPad("top", "top", 0., 0.5, 1., 1.)
		top.SetBottomMargin(0.03)
		top.Draw()
		if log:
			top.SetLogy()
		c.cd()
		bottom = TPad("bottom", "bottom", 0., 0., 1., 0.5)
		bottom.SetTopMargin(0.02)
		bottom.SetBottomMargin(0.2)
		bottom.Draw()
		ROOT.SetOwnership(c, False)
		ROOT.SetOwnership(top, False)
		ROOT.SetOwnership(bottom, False)
		top.cd()

		# Make data histogram
		data_hist = self.data_histogram_.Clone()

		if data_binning:
			data_hist = histogram_tools.rebin_histogram(data_hist, data_binning, normalization_bin_width=normalization_bin_width)
			roobinning = RooBinning(len(data_binning) - 1, data_binning, "data_binning")
		else:
			data_hist = data_hist_raw
			bins_array = array('d', [])
			for bin in xrange(1, data_hist.GetNbinsX() + 1):
				bins_array.append(data_hist.GetXaxis().GetBinLowEdge(bin))
			bins_array.append(data_hist.GetXaxis().GetBinUpEdge(data_hist.GetNbinsX()))
			roobinning = RooBinning(len(bins_array) - 1, bins_array, "data_binning")

		if x_range:
			x_min = x_range[0]
			x_max = x_range[1]
		else:
			x_min = self.mjj_.GetMin()
			x_max = self.mjj_.GetMax()
		frame_top = TH1D("frame_top", "frame_top", 100, x_min, x_max)
		if log:
			frame_top.SetMaximum(data_hist.GetMaximum() * 50.)
			frame_top.SetMinimum(0.1)
		else:
			frame_top.SetMaximum(data_hist.GetMaximum() * 1.3)
			frame_top.SetMinimum(0.)
		frame_top.GetYaxis().SetTitle("Events / " + str(normalization_bin_width) + " GeV")
		frame_top.GetXaxis().SetTitleSize(0)
		frame_top.GetXaxis().SetLabelSize(0)
		#if log:
		#	frame_top.SetMaximum(y_max * 10.)
		#	frame_top.SetMinimum(y_min / 100.)
		#else:
		#	frame_top.SetMaximum(y_max * 1.3)
		#	frame_top.SetMinimum(0.)
		frame_top.Draw()
		#data_rdh.plotOn(frame_top, RooFit.Name("Data"))
		data_hist.SetMarkerStyle(20)
		data_hist.SetMarkerColor(1)
		data_hist.SetMarkerSize(1)
		data_hist.Draw("p same")
		l.AddEntry(data_hist, "Data", "pl")

		background_histograms = {}
		background_histograms_fitted_range = {}
		chi2s = {}
		chi2ndfs = {}
		mc_stack = THStack("mc_stack", "")
		style_counter = 0
		for fit_function in fit_functions:
			f_workspace = TFile(background_workspaces[fit_function], "READ")
			workspace = f_workspace.Get("w")

			# MC histograms
			background_histograms[fit_function + "_MC"] = data_hist.Clone()
			background_histograms[fit_function + "_MC"].Reset()
			background_histograms[fit_function + "_MC"].SetName(fit_function + "_MC")
			if len(self.backgrounds_):
				for mc_background in self.backgrounds_:
					print "Trying to get " + "h_pfjet_mjj_" + mc_background
					f_workspace.ls()
					this_mc_histogram = f_workspace.Get("h_pfjet_mjj_" + mc_background)
					this_mc_histogram.SetDirectory(0)
					print "Fitted normalization is scaled from initial by " + str(workspace.var(background + "_norm_backgroundonly").getVal() / workspace.var(background + "_initial_norm_backgroundonly").getVal())
					this_mc_histogram.Scale(workspace.var(background + "_norm_backgroundonly").getVal() / workspace.var(background + "_initial_norm_backgroundonly").getVal())
					this_mc_histogram = histogram_tools.rebin_histogram(this_mc_histogram, data_binning, normalization_bin_width=normalization_bin_width)
					background_histograms[fit_function + "_MC"].Add(this_mc_histogram)
				background_histograms[fit_function + "_MC"].SetLineColor(seaborn.GetColorRoot("default", style_counter))
				background_histograms[fit_function + "_MC"].SetLineStyle(2)
				background_histograms[fit_function + "_MC"].SetLineWidth(2)
				background_histograms[fit_function + "_MC"].SetMarkerStyle(20)
				background_histograms[fit_function + "_MC"].SetMarkerSize(0)
				background_histograms[fit_function + "_MC"].SetDirectory(0)
				background_histograms[fit_function + "_MC"].Draw("hist same")
				l.AddEntry(background_histograms[fit_function + "_MC"], "MC " + fit_function, "l")

			# Make histogram from fitted background
			fitresult_name = "fitresult_background_data_roohistogram"
			fitresult = workspace.genobj(fitresult_name)
			fitresult.Print()
			fit = self.make_background_tf1_from_roofitresult(fit_function, fitresult, mjj_range=x_range)

			# Normalize fit
			scale_factor = workspace.var("fit_norm_backgroundonly").getVal() / fit.Integral(self.mjj_.getMin(), self.mjj_.getMax())
			fit.SetParameter(0, fit.GetParameter(0) * scale_factor)
			fit.SetParError(0, fit.GetParError(0) * scale_factor)

			background_histograms[fit_function] = data_hist.Clone()
			background_histograms[fit_function].Reset()
			background_histograms[fit_function].SetName(fit_function)
			for bin in xrange(1, background_histograms[fit_function].GetNbinsX() + 1):
				low_edge = background_histograms[fit_function].GetXaxis().GetBinLowEdge(bin)
				up_edge = background_histograms[fit_function].GetXaxis().GetBinUpEdge(bin)
				background_histograms[fit_function].SetBinContent(bin, fit.Integral(low_edge, up_edge) / (up_edge - low_edge) * normalization_bin_width)

			chi2s[fit_function] = 0.
			chi2ndfs[fit_function] = 0.
			ndf = -1 * fit.GetNpar()
			for bin in xrange(1, self.data_histogram_.GetNbinsX() + 1):
				low_edge = self.data_histogram_.GetXaxis().GetBinLowEdge(bin)
				up_edge = self.data_histogram_.GetXaxis().GetBinUpEdge(bin)
				if up_edge < self.mjj_.getMin() or low_edge > self.mjj_.getMax():
					continue
				if self.data_histogram_.GetBinError(bin):
					chi2s[fit_function] += ((self.data_histogram_.GetBinContent(bin) - fit.Integral(low_edge, up_edge)) / self.data_histogram_.GetBinError(bin))**2
					ndf += 1
			chi2ndfs[fit_function] = chi2s[fit_function] / ndf
			print "#chi^2/NDF(" + fit_function + ") = " + str(round(chi2s[fit_function], 3)) + "/" + str(ndf) + " = " + str(round(chi2ndfs[fit_function], 3)) + " / p = " + str(TMath.Prob(chi2s[fit_function], ndf))

			# Add in all the MC background
			background_histograms[fit_function].Add(background_histograms[fit_function + "_MC"])

			# Draw total background histogram as a colored line
			background_histograms[fit_function].SetLineColor(seaborn.GetColorRoot("default", style_counter))
			background_histograms[fit_function].SetLineWidth(1)
			background_histograms[fit_function].SetLineStyle(1)
			background_histograms[fit_function].Draw("hist same")
			if draw_chi2ndf:
				legend_entry = "Total background " + fit_function + "(#chi^{2}/NDF=" + str(round(chi2ndfs[fit_function], 2)) + ")"
			else:
				legend_entry = "Total background " + fit_function
			l.AddEntry(background_histograms[fit_function], legend_entry, "l")
			background_histograms[fit_function].SetDirectory(0)

			f_workspace.Close()
			style_counter += 1

		l.Draw()
		
		# Pull histogram
		c.cd()
		bottom.cd()
		pull_histograms = {}
		pull_histograms_fitted_range = {}
		for fit_function in fit_functions:
			pull_histograms[fit_function] = data_hist.Clone()
			pull_histograms[fit_function].Reset()
			for bin in xrange(1, data_hist.GetNbinsX() + 1):
				if data_hist.GetBinError(bin) > 0:
					pull = (data_hist.GetBinContent(bin) - background_histograms[fit_function].GetBinContent(bin)) / (data_hist.GetBinError(bin))
				else:
					pull = 0.
				#print "[debug] Pull = " + str(pull)
				pull_histograms[fit_function].SetBinContent(bin, pull)
				pull_histograms[fit_function].SetBinError(bin, 0.)

			pull_histograms_fitted_range[fit_function] = pull_histograms[fit_function].Clone()
			for bin in xrange(1, pull_histograms_fitted_range[fit_function].GetNbinsX() + 1):
				bin_center = pull_histograms_fitted_range[fit_function].GetXaxis().GetBinCenter(bin)
				if bin_center < self.mjj_.getMin() or bin_center > self.mjj_.getMax():
					pull_histograms_fitted_range[fit_function].SetBinContent(bin, 0)
					pull_histograms_fitted_range[fit_function].SetBinError(bin, 0)

			#pull_histogram = frame_top.pullHist("Data", "B Fit")
		frame_bottom = TH1D("frame_bottom", "frame_bottom", 100, x_min, x_max)
		frame_bottom.SetMinimum(-5.)
		frame_bottom.SetMaximum(5.)
		#pull_histogram.plotOn(frame_bottom, RooFit.Name(fit_pdf_name))
		frame_bottom.GetXaxis().SetTitle("m_{jj} [GeV]")
		frame_bottom.GetYaxis().SetTitle("#frac{Data - Fit}{#sigma(Data)}")
		frame_bottom.Draw()

		style_counter = 0
		for fit_function in fit_functions:
			pull_histograms[fit_function].SetLineColor(seaborn.GetColorRoot("default", style_counter))
			pull_histograms[fit_function].SetLineWidth(2)
			pull_histograms[fit_function].SetLineStyle(2)
			pull_histograms[fit_function].Draw("hist same")

			pull_histograms_fitted_range[fit_function].SetLineColor(seaborn.GetColorRoot("dark", style_counter))
			pull_histograms_fitted_range[fit_function].SetLineWidth(2)
			pull_histograms_fitted_range[fit_function].SetLineStyle(1)
			pull_histograms_fitted_range[fit_function].Draw("hist same")
			style_counter += 1
		#pull_histogram.GetXaxis().SetTitle("m_{jj} [GeV]")
		#pull_histogram.GetYaxis().SetTitle("#frac{Data - Fit}{#sigma(Fit)}")
		#pull_histogram.Draw("same")
		c.cd()

		c.SaveAs("/uscms/home/dryu/Dijets/data/EightTeeEeVeeBee/Results/figures/c_" + save_tag + ".pdf")
Example #17
0
def mc_jes_plot(analysis, model, mass):
	f_new = TFile(analysis_config.get_b_histogram_filename(analysis, analysis_config.simulation.get_signal_tag(model, mass, "FULLSIM")), "READ")
	h_new = f_new.Get("BHistograms/h_pfjet_mjj")
	h_new.SetName("h_pfjet_mjj_newJEC")
	h_new.SetDirectory(0)
	h_new = histogram_tools.rebin_histogram(h_new, dijet_binning, normalization_bin_width=1.)

	f_old = TFile(analysis_config.get_b_histogram_filename(analysis, analysis_config.simulation.get_signal_tag(model, mass, "FULLSIM")).replace("EightTeeEeVeeBee/BHistograms", "EightTeeEeVeeBee/BHistograms/old/JEC2012"), "READ")
	h_old = f_old.Get("BHistograms/h_pfjet_mjj")
	h_old.SetName("h_pfjet_mjj_oldJEC")
	h_old.SetDirectory(0)
	h_old = histogram_tools.rebin_histogram(h_old, dijet_binning, normalization_bin_width=1.)

	c = TCanvas("c_jeccomparison_{}_{}_{}".format(analysis, model, mass), "c_jeccomparison_{}_{}_{}".format(analysis, model, mass), 800, 1200)
	top = TPad("top", "top", 0., 0.5, 1., 1.)
	top.SetBottomMargin(0.02)
	#top.SetLogy()
	top.Draw()
	top.cd()
	frame_top = TH1D("frame_top", "frame_top", 100, 0., mass*2)
	frame_top.GetXaxis().SetTitleSize(0)
	frame_top.GetXaxis().SetLabelSize(0)
	frame_top.GetYaxis().SetTitle("Events / GeV")
	frame_top.SetMinimum(0.01)
	frame_top.SetMaximum(h_new.GetMaximum() * 1.3)
	frame_top.Draw()
	h_new.SetMarkerStyle(20)
	h_new.GetXaxis().SetTitleSize(0)
	h_new.GetXaxis().SetLabelSize(0)
	h_new.GetYaxis().SetTitle("Events / GeV")
	h_new.Draw("same")
	h_old.SetMarkerStyle(25)
	h_old.Draw("same")
	l = TLegend(0.6, 0.6, 0.88, 0.8)
	l.SetFillColor(0)
	l.SetBorderSize(0)
	l.AddEntry(h_new, "New JEC")
	l.AddEntry(h_old, "Old JEC")
	l.Draw()

	c.cd()
	bottom = TPad("bottom", "bottom", 0., 0., 1., 0.5)
	bottom.SetTopMargin(0.02)
	bottom.SetBottomMargin(0.2)
	bottom.Draw()
	bottom.cd()
	frame_bottom = TH1D("frame_bottom", "frame_bottom", 100, 0., mass*2)
	frame_bottom.GetXaxis().SetTitle("m_{jj} [GeV]")
	frame_bottom.GetYaxis().SetTitle("New JEC / Old JEC")
	frame_bottom.SetMinimum(0.5)
	frame_bottom.SetMaximum(1.5)
	frame_bottom.Draw()
	h_ratio = h_new.Clone()
	h_ratio.Divide(h_old)
	h_ratio.SetMarkerStyle(21)
	h_ratio.Draw("same")

	c.cd()
	c.SaveAs(analysis_config.figure_directory + "/" + c.GetName() + ".pdf")


	f_new.Close()
	f_old.Close()
from array import array
mass_bins = array("d", [1, 3, 6, 10, 16, 23, 31, 40, 50, 61, 74, 88, 103, 119, 137, 156, 176, 197, 220, 244, 270, 296, 325, 354, 386, 419, 453, 489, 526, 565, 606, 649, 693, 740, 788, 838, 890, 944, 1000, 1058, 1118, 1181, 1246, 1313, 1383, 1455, 1530, 1607, 1687, 1770, 1856, 1945, 2037, 2132, 2231, 2332, 2438, 2546, 2659, 2775, 2895, 3019, 3147, 3279, 3416, 3558, 3704, 3854, 4010, 4171, 4337, 4509, 4686, 4869, 5058, 5253, 5455, 5663, 5877, 6099, 6328, 6564, 6808, 7060, 7320, 7589, 7866, 8000])

variables = ["mjj", "pt1", "pt2", "pt_btag1", "pt_btag2"]
histograms = {}
for analysis in analyses:
    histograms[analysis] = {}
    for variable in variables:
        histograms[analysis][variable] = files[analysis].Get("BHistograms/h_pfjet_" + variable)
        histograms[analysis][variable].SetName("h_" + analysis + "_" + variable)
        histograms[analysis][variable].SetDirectory(0)
        if analysis == "trigmubbll_lowmass":
            histograms[analysis][variable].Scale(1.7) # Prescale
        if variable == "mjj":
            print "(rebinning)...",
            histograms[analysis][variable] = histogram_tools.rebin_histogram(histograms[analysis][variable], mass_bins, normalization_bin_width=1)
            print "(done)"
        elif "pt" in variable:
            histograms[analysis][variable].Rebin(25)

print "Making efficiency histograms"
efficiency_pairs = {
    "highmass":["trigmubbh_highmass_CSVTM", "trigmu_highmass_CSVTM"],
    "lowmass":["trigmubbl_lowmass_CSVTM", "trigmu_lowmass_CSVTM"],
    "llowmass":["trigmubbll_lowmass_CSVTM", "trigmu_lowmass_CSVTM"]
}
efficiency_titles = {"highmass":"160/120", "lowmass":"80/70", "llowmass":"60/53"}
efficiency_colors = {"highmass":"red", "lowmass":"green", "llowmass":"blue"}

efficiency_histograms = {}
for efficiency_name, efficiency_analyses in efficiency_pairs.iteritems():
def plot(analysis, model, mass):
    f = TFile(
        analysis_config.get_b_histogram_filename(
            analysis,
            analysis_config.simulation.get_signal_tag(model, mass, "FULLSIM")),
        "READ")
    h_central = f.Get("BHistograms/h_pfjet_mjj")
    h_central.SetDirectory(0)
    h_central = histogram_tools.rebin_histogram(h_central,
                                                dijet_binning,
                                                normalization_bin_width=1.)
    h_up = f.Get("BHistograms/h_pfjet_mjj_BTagOfflineSFUp")
    h_up.SetDirectory(0)
    h_up = histogram_tools.rebin_histogram(h_up,
                                           dijet_binning,
                                           normalization_bin_width=1.)
    h_down = f.Get("BHistograms/h_pfjet_mjj_BTagOfflineSFDown")
    h_down.SetDirectory(0)
    h_down = histogram_tools.rebin_histogram(h_down,
                                             dijet_binning,
                                             normalization_bin_width=1.)

    c = TCanvas("c_offb_unc_{}_{}{}".format(analysis, model, mass),
                "c_offb_unc_{}_{}{}".format(analysis, model, mass), 800, 1000)
    top = TPad("top", "top", 0., 0.5, 1., 1.)
    top.SetBottomMargin(0.02)
    #top.SetLogy()
    top.Draw()
    top.cd()
    frame_top = TH1D("frame_top", "frame_top", 100, 0., mass * 2)
    frame_top.GetXaxis().SetTitleSize(0)
    frame_top.GetXaxis().SetLabelSize(0)
    frame_top.GetYaxis().SetTitle("Events / GeV")
    frame_top.SetMinimum(0.01)
    frame_top.SetMaximum(h_central.GetMaximum() * 1.3)
    frame_top.Draw()
    h_central.SetMarkerStyle(20)
    h_central.Draw("same")
    h_up.SetMarkerStyle(26)
    h_up.Draw("same")
    h_down.SetMarkerStyle(32)
    h_down.Draw("same")
    l = TLegend(0.6, 0.6, 0.88, 0.8)
    l.SetFillColor(0)
    l.SetBorderSize(0)
    l.AddEntry(h_central, "Central", "p")
    l.AddEntry(h_up, "SF unc up", "p")
    l.AddEntry(h_down, "SF unc down", "p")
    l.Draw()

    c.cd()
    bottom = TPad("bottom", "bottom", 0., 0., 1., 0.5)
    bottom.SetTopMargin(0.02)
    bottom.SetBottomMargin(0.2)
    bottom.Draw()
    bottom.cd()
    frame_bottom = TH1D("frame_bottom", "frame_bottom", 100, 0., mass * 2)
    frame_bottom.GetXaxis().SetTitle("m_{jj} [GeV]")
    frame_bottom.GetYaxis().SetTitle("Unc / Central")
    frame_bottom.SetMinimum(0.5)
    frame_bottom.SetMaximum(1.5)
    frame_bottom.Draw()
    ratio_up = h_up.Clone()
    ratio_up.Divide(h_central)
    ratio_up.SetMarkerStyle(26)
    ratio_up.Draw("p hist same")
    ratio_down = h_down.Clone()
    ratio_down.Divide(h_central)
    ratio_down.SetMarkerStyle(32)
    ratio_down.Draw("p hist same")

    avg_up = TLine(0.,
                   h_up.Integral() / h_central.Integral(), mass * 2,
                   h_up.Integral() / h_central.Integral())
    avg_up.SetLineStyle(2)
    avg_up.SetLineColor(seaborn.GetColorRoot("pastel", 2))
    avg_up.Draw("same")
    avg_down = TLine(0.,
                     h_down.Integral() / h_central.Integral(), mass * 2,
                     h_down.Integral() / h_central.Integral())
    avg_down.SetLineStyle(2)
    avg_down.SetLineColor(seaborn.GetColorRoot("pastel", 2))
    avg_down.Draw("same")

    c.cd()
    c.SaveAs(analysis_config.figure_directory + "/" + c.GetName() + ".pdf")

    f.Close()
Example #20
0
    def draw(self,
             binning=None,
             logy=False,
             draw_ratio=False,
             draw_pull=False,
             x_range=None,
             y_range=None,
             ratio_range=None,
             pull_range=None,
             pull_dataerrors=False,
             save_tag="",
             cms_label=None,
             x_title=None,
             y_title=None,
             ratio_title=None,
             color_scheme="cubehelixhuge",
             complex_rebinning=None,
             simple_rebinning=None,
             legend_position="topright",
             lumi_string=None,
             save_directory=None):
        print "[debug] x_range = ",
        print x_range
        if complex_rebinning:
            self._data_histogram = histogram_tools.rebin_histogram(
                self._data_histogram,
                complex_rebinning,
                normalization_bin_width=1.)
            self._mc_sum = histogram_tools.rebin_histogram(
                self._mc_sum, complex_rebinning, normalization_bin_width=1.)
            for mc_name in self._mc_names:
                self._mc_histograms[mc_name] = histogram_tools.rebin_histogram(
                    self._mc_histograms[mc_name],
                    complex_rebinning,
                    normalization_bin_width=1.)
                self._mc_histograms[mc_name].SetDirectory(0)
            for signal_name in self._signal_names:
                self._signal_histograms[
                    signal_name] = histogram_tools.rebin_histogram(
                        self._signal_histograms[signal_name],
                        complex_rebinning,
                        normalization_bin_width=1.)
                self._signal_histograms[signal_name].SetDirectory(0)
        elif simple_rebinning:
            self._data_histogram.Rebin(simple_rebinning)
            for mc_name in self._mc_names:
                self._mc_histograms[mc_name].Rebin(simple_rebinning)
            for signal_name in self._signal_names:
                self._signal_histograms[signal_name].Rebin(simple_rebinning)

        if draw_ratio or draw_pull:
            self._canvas = TCanvas("c_" + save_tag, "c_" + save_tag, 800, 800)
            self._top = TPad("top_" + save_tag, "top_" + save_tag, 0.,
                             self._bottom_fraction, 1., 1.)
            self._top.SetTopMargin(0.07)
            self._top.SetBottomMargin(0.03)
            self._top.Draw()
            self._canvas.cd()
            self._bottom = TPad("bottom_" + save_tag, "bottom_" + save_tag, 0.,
                                0., 1., self._bottom_fraction)
            self._bottom.SetTopMargin(0.04)
            self._bottom.SetBottomMargin(0.28)
            self._bottom.Draw()
            self._canvas.cd()
        else:
            self._canvas = TCanvas("c_" + save_tag, "c_" + save_tag, 800, 600)

        if legend_position == "topright":
            self._legend = TLegend(0.5, 0.53, 0.92, 0.87)
        elif legend_position == "topleft":
            self._legend = TLegend(0.2, 0.6, 0.45, 0.8)
        elif legend_position == "bottomright":
            self._legend = TLegend(0.6, 0.2, 0.85, 0.4)
        elif legend_position == "bottomleft":
            self._legend = TLegend(0.2, 0.2, 0.45, 0.4)
        self._legend.SetFillColor(0)
        self._legend.SetBorderSize(0)

        if draw_ratio or draw_pull:
            self._top.cd()
        self._frame_top = self._data_histogram.Clone()
        self._frame_top.Reset()
        if logy:
            if draw_ratio or draw_pull:
                self._top.SetLogy()
            else:
                self._canvas.SetLogy()
        if x_range:
            self._frame_top.GetXaxis().SetRangeUser(x_range[0], x_range[1])
        if y_range:
            y_min = y_range[0]
            y_max = y_range[1]
        else:
            if logy:
                y_min = 0.1
                y_max = max(self._data_histogram.GetMaximum(),
                            self._mc_sum.GetMaximum()) * 50.
            else:
                y_min = 0.
                y_max = max(self._data_histogram.GetMaximum(),
                            self._mc_sum.GetMaximum()) * 1.3
        self._frame_top.SetMinimum(y_min)
        self._frame_top.SetMaximum(y_max)
        if draw_ratio or draw_pull:
            self._frame_top.GetXaxis().SetLabelSize(0)
            self._frame_top.GetXaxis().SetTitleSize(0)
        else:
            if x_title:
                self._frame_top.GetXaxis().SetTitle(x_title)
        if y_title:
            self._frame_top.GetYaxis().SetTitle(y_title)
        self._frame_top.GetYaxis().SetTitleSize(0.06)
        self._frame_top.GetYaxis().SetTitleOffset(0.95)
        self._frame_top.GetYaxis().SetLabelSize(0.035 /
                                                (1. - self._bottom_fraction))
        self._frame_top.Draw("axis")

        # MC: sort histograms by integral and stack
        for tf1_name in self._tf1_names:
            if tf1_name in self._tf1_colors:
                self._tf1s[tf1_name].SetLineColor(self._tf1_colors[tf1_name])
                self._tf1s[tf1_name].SetLineWidth(2)
                self._tf1s[tf1_name].SetLineStyle(1)
                self._tf1s[tf1_name].Draw("same")

        if not self._draw_tf1only:
            mc_names_sorted = sorted(
                self._mc_names,
                key=lambda x: self._mc_histograms[x].Integral())
            self._mc_stack = THStack("mc_stack", "mc_stack")
            style_counter = 0
            for mc_name in mc_names_sorted:
                if mc_name in self._mc_colors:
                    self._mc_histograms[mc_name].SetFillColor(
                        self._mc_colors[mc_name])
                else:
                    self._mc_histograms[mc_name].SetFillColor(
                        seaborn.GetColorRoot(color_scheme, style_counter,
                                             len(mc_names_sorted)))
                self._mc_histograms[mc_name].SetLineColor(kBlack)
                self._mc_histograms[mc_name].SetLineWidth(1)
                self._mc_histograms[mc_name].SetFillStyle(1001)
                self._mc_histograms[mc_name].SetMarkerStyle(20)
                self._mc_histograms[mc_name].SetMarkerSize(0)
                self._mc_stack.Add(self._mc_histograms[mc_name])
            self._mc_stack.Draw("hist same")
            ROOT.SetOwnership(self._mc_stack, False)
            for mc_name in mc_names_sorted:
                ROOT.SetOwnership(self._mc_histograms[mc_name], False)

        for signal_name in self._signal_names:
            if self._stack_signals:
                self._signal_histograms[signal_name].Add(self._mc_sum)
            if signal_name in self._signal_styles:
                self._signal_histograms[signal_name].SetLineStyle(
                    self._signal_styles[signal_name])
            if signal_name in self._signal_colors:
                self._signal_histograms[signal_name].SetLineColor(
                    self._signal_colors[signal_name])
            self._signal_histograms[signal_name].SetLineWidth(3)
            self._signal_histograms[signal_name].Draw("hist same ][")

        self._data_histogram.SetMarkerStyle(20)
        self._data_histogram.SetMarkerSize(1)
        self._data_histogram.SetMarkerColor(kBlack)
        self._data_histogram.Draw("same")
        print "[debug] Data integral = " + str(
            self._data_histogram.Integral()) + " / MC integral = " + str(
                self._mc_sum.Integral())
        self._frame_top.Draw("axis same")

        self._legend.AddEntry(self._data_histogram, self._data_name, "ep")
        for tf1_name in self._tf1_names:
            self._legend.AddEntry(self._tf1s[tf1_name], tf1_name, "l")
        if not self._draw_tf1only:
            for mc_name in reversed(mc_names_sorted):
                self._legend.AddEntry(self._mc_histograms[mc_name], mc_name,
                                      "lf")
        for signal_name in self._signal_names:
            self._legend.AddEntry(self._signal_histograms[signal_name],
                                  signal_name, "l")
        self._legend.Draw()
        if cms_label != None:
            if draw_ratio or draw_pull:
                cms_label_offset = 1. - self._bottom_fraction
            else:
                cms_label_offset = 1.
            Root.CMSLabel(0.22, 0.8, cms_label, 1, 0.65, cms_label_offset)

        if lumi_string:
            Root.myText(0.74, 0.95, kBlack, lumi_string, 0.57)

        if draw_ratio:
            self._canvas.cd()
            self._bottom.cd()
            self._frame_bottom = self._data_histogram.Clone()
            self._frame_bottom.Reset()
            if ratio_range:
                self._frame_bottom.SetMinimum(ratio_range[0])
                self._frame_bottom.SetMaximum(ratio_range[1])
            else:
                self._frame_bottom.SetMinimum(0.)
                self._frame_bottom.SetMaximum(2.)
            if x_range:
                self._frame_bottom.GetXaxis().SetRangeUser(
                    x_range[0], x_range[1])
            if x_title:
                self._frame_bottom.GetXaxis().SetTitle(x_title)
            self._frame_bottom.GetXaxis().SetTitleSize(0.04 /
                                                       self._bottom_fraction)
            self._frame_bottom.GetXaxis().SetTitleOffset(1.15)
            self._frame_bottom.GetXaxis().SetLabelOffset(0.005 * 4.4)

            self._frame_bottom.GetYaxis().SetTitleSize(0.04 /
                                                       self._bottom_fraction)
            self._frame_bottom.GetYaxis().SetLabelSize(0.04 /
                                                       self._bottom_fraction)
            self._frame_bottom.GetYaxis().SetTitleOffset(0.6)
            self._frame_bottom.GetYaxis().SetLabelOffset(0.005 * 2.2)

            self._frame_bottom.GetYaxis().SetTitle("Data / Background")
            self._frame_bottom.Draw("axis")

            self._ratio_histogram = self._data_histogram.Clone()
            self._ratio_histogram.Reset()
            for bin in xrange(1, self._data_histogram.GetNbinsX() + 1):
                num = self._data_histogram.GetBinContent(bin)
                num_err = self._data_histogram.GetBinError(bin)
                den = self._mc_sum.GetBinContent(bin)
                den_err = self._mc_sum.GetBinError(bin)
                if num > 0 and den > 0:
                    ratio = num / den
                    ratio_err = ratio * sqrt((num_err / num)**2 +
                                             (den_err / den)**2)
                else:
                    ratio = 0.
                    ratio_err = 0.
                self._ratio_histogram.SetBinContent(bin, ratio)
                self._ratio_histogram.SetBinError(bin, ratio_err)
                print "[debug] Set ratio(mjj=" + str(
                    self._data_histogram.GetXaxis().GetBinCenter(
                        bin)) + ") = " + str(ratio)
            self._ratio_histogram.SetMarkerStyle(24)
            self._ratio_histogram.SetMarkerSize(1)
            self._ratio_histogram.SetMarkerColor(kBlack)
            self._ratio_histogram.SetLineWidth(1)
            self._ratio_histogram.SetLineColor(kBlack)
            self._ratio_histogram.Draw("same")
            self._frame_bottom.Draw("axis same")
        elif draw_pull:
            self._canvas.cd()
            self._bottom.cd()
            self._frame_bottom = self._data_histogram.Clone()
            self._frame_bottom.Reset()
            if pull_range:
                self._frame_bottom.SetMinimum(pull_range[0])
                self._frame_bottom.SetMaximum(pull_range[1])
            else:
                self._frame_bottom.SetMinimum(-3.)
                self._frame_bottom.SetMaximum(3.)
            if x_range:
                self._frame_bottom.GetXaxis().SetRangeUser(
                    x_range[0], x_range[1])
            if pull_dataerrors:
                self._frame_bottom.GetYaxis().SetTitle(
                    "#frac{(Data - Bkgd)}{#sqrt{Data}}")
            else:
                self._frame_bottom.GetYaxis().SetTitle(
                    "#frac{(Data - Bkgd)}{#sqrt{Bkgd}}")
            if x_title:
                self._frame_bottom.GetXaxis().SetTitle(x_title)
            self._frame_bottom.GetXaxis().SetTitleSize(0.035 /
                                                       self._bottom_fraction)
            self._frame_bottom.GetXaxis().SetLabelSize(0.035 /
                                                       self._bottom_fraction)
            self._frame_bottom.GetXaxis().SetTitleOffset(1.07)
            self._frame_bottom.GetXaxis().SetLabelOffset(0.005 * 4.2)
            self._frame_bottom.GetYaxis().SetTitleSize(0.035 /
                                                       self._bottom_fraction)
            self._frame_bottom.GetYaxis().SetLabelSize(0.035 /
                                                       self._bottom_fraction)
            self._frame_bottom.GetYaxis().SetTitleOffset(0.55)
            self._frame_bottom.GetYaxis().SetLabelOffset(0.005 * 2.2)
            self._frame_bottom.Draw("axis")

            self._pull_histogram = self._data_histogram.Clone()
            self._pull_histogram.Reset()
            self._systematic_histogram = self._data_histogram.Clone()
            self._systematic_histogram.Reset()
            for bin in xrange(1, self._data_histogram.GetNbinsX() + 1):
                data = self._data_histogram.GetBinContent(bin)
                data_err = self._data_histogram.GetBinError(bin)
                background = self._mc_sum.GetBinContent(bin)
                background_err = self._mc_sum.GetBinError(bin)

                if complex_rebinning:
                    # Bins are scaled by bin width. Need to undo before calculating background uncertainty.
                    bin_width = self._data_histogram.GetXaxis().GetBinWidth(
                        bin)
                    data = data * bin_width
                    data_err = data_err * bin_width
                    background = background * bin_width
                    background_err = background_err * bin_width
                if background > 0:
                    if pull_dataerrors:
                        pull = (data - background) / data_err
                    else:
                        pull = (data - background) / sqrt(background)
                    syst = background_err / sqrt(background)
                else:
                    pull = 0.
                    syst = 0.

                self._pull_histogram.SetBinContent(bin, pull)
                self._pull_histogram.SetBinError(bin, 0.)
                self._systematic_histogram.SetBinContent(bin, 0.)
                self._systematic_histogram.SetBinError(bin, syst)
            self._systematic_histogram.SetFillColor(17)
            self._systematic_histogram.SetFillStyle(1001)
            self._systematic_histogram.SetMarkerStyle(20)
            self._systematic_histogram.SetMarkerSize(0)
            #self._systematic_histogram.Draw("e2 hist same")
            self._pull_histogram.SetMarkerStyle(24)
            self._pull_histogram.SetMarkerSize(1)
            self._pull_histogram.SetMarkerColor(kBlack)
            self._pull_histogram.SetLineStyle(1)
            self._pull_histogram.SetLineWidth(2)
            self._pull_histogram.SetLineColor(1)
            self._pull_histogram.SetFillStyle(1001)
            self._pull_histogram.SetFillColor(
                seaborn.GetColorRoot("default", 2))
            #self._pull_histogram.SetLineWidth(1)
            #self._pull_histogram.SetLineColor(kBlack)
            self._pull_histogram.Draw("hist same")
            self._frame_bottom.Draw("axis same")

            self._signal_pulls = {}
            for signal_name in self._signal_names:
                if self._signal_plot_pull[signal_name]:
                    self._signal_pulls[signal_name] = self._signal_histograms[
                        signal_name].Clone()
                    for bin in xrange(
                            1,
                            self._signal_histograms[signal_name].GetNbinsX() +
                            1):
                        signal_counts = self._signal_histograms[
                            signal_name].GetBinContent(bin)
                        signal_err = self._signal_histograms[
                            signal_name].GetBinError(bin)
                        background = self._mc_sum.GetBinContent(bin)
                        background_err = self._mc_sum.GetBinError(bin)

                        if complex_rebinning:
                            # Bins are scaled by bin width. Need to undo before calculating background uncertainty.
                            bin_width = self._data_histogram.GetXaxis(
                            ).GetBinWidth(bin)
                            signal_counts = signal_counts * bin_width
                            signal_err = signal_err * bin_width
                            background = background * bin_width
                            background_err = background_err * bin_width
                        if background > 0:
                            if pull_dataerrors:
                                pull = signal_counts / signal_err
                            else:
                                pull = signal_counts / sqrt(background)
                        else:
                            pull = 0.
                        self._signal_pulls[signal_name].SetBinContent(
                            bin, pull)
                        self._signal_pulls[signal_name].SetBinError(bin, 0.)
                    self._signal_pulls[signal_name].Draw("hist same")
        self._canvas.cd()

        if draw_pull or draw_ratio:
            ROOT.SetOwnership(self._canvas, False)
            ROOT.SetOwnership(self._top, False)
            ROOT.SetOwnership(self._bottom, False)
        if save_directory:
            self._canvas.cd()
            self._canvas.SaveAs(save_directory + "/" + self._canvas.GetName() +
                                ".png")
            self._canvas.SaveAs(save_directory + "/" + self._canvas.GetName() +
                                ".pdf")
            self._canvas.SaveAs(save_directory + "/" + self._canvas.GetName() +
                                ".eps")
        return self._canvas
def plot(analysis, model, mass):
	f = TFile(analysis_config.get_b_histogram_filename(analysis, analysis_config.simulation.get_signal_tag(model, mass, "FULLSIM")), "READ")
	h_central = f.Get("BHistograms/h_pfjet_mjj")
	h_central.SetDirectory(0)
	h_central = histogram_tools.rebin_histogram(h_central, dijet_binning, normalization_bin_width=1.)
	h_up = f.Get("BHistograms/h_pfjet_mjj_BTagOfflineSFUp")
	h_up.SetDirectory(0)
	h_up = histogram_tools.rebin_histogram(h_up, dijet_binning, normalization_bin_width=1.)
	h_down = f.Get("BHistograms/h_pfjet_mjj_BTagOfflineSFDown")
	h_down.SetDirectory(0)
	h_down = histogram_tools.rebin_histogram(h_down, dijet_binning, normalization_bin_width=1.)

	c = TCanvas("c_offb_unc_{}_{}{}".format(analysis, model, mass), "c_offb_unc_{}_{}{}".format(analysis, model, mass), 800, 1000)
	top = TPad("top", "top", 0., 0.5, 1., 1.)
	top.SetBottomMargin(0.02)
	#top.SetLogy()
	top.Draw()
	top.cd()
	frame_top = TH1D("frame_top", "frame_top", 100, 0., mass*2)
	frame_top.GetXaxis().SetTitleSize(0)
	frame_top.GetXaxis().SetLabelSize(0)
	frame_top.GetYaxis().SetTitle("Events / GeV")
	frame_top.SetMinimum(0.01)
	frame_top.SetMaximum(h_central.GetMaximum() * 1.3)
	frame_top.Draw()
	h_central.SetMarkerStyle(20)
	h_central.Draw("same")
	h_up.SetMarkerStyle(26)
	h_up.Draw("same")
	h_down.SetMarkerStyle(32)
	h_down.Draw("same")
	l = TLegend(0.6, 0.6, 0.88, 0.8)
	l.SetFillColor(0)
	l.SetBorderSize(0)
	l.AddEntry(h_central, "Central", "p")
	l.AddEntry(h_up, "SF unc up", "p")
	l.AddEntry(h_down, "SF unc down", "p")
	l.Draw()

	c.cd()
	bottom = TPad("bottom", "bottom", 0., 0., 1., 0.5)
	bottom.SetTopMargin(0.02)
	bottom.SetBottomMargin(0.2)
	bottom.Draw()
	bottom.cd()
	frame_bottom = TH1D("frame_bottom", "frame_bottom", 100, 0., mass*2)
	frame_bottom.GetXaxis().SetTitle("m_{jj} [GeV]")
	frame_bottom.GetYaxis().SetTitle("Unc / Central")
	frame_bottom.SetMinimum(0.5)
	frame_bottom.SetMaximum(1.5)
	frame_bottom.Draw()
	ratio_up = h_up.Clone()
	ratio_up.Divide(h_central)
	ratio_up.SetMarkerStyle(26)
	ratio_up.Draw("p hist same")
	ratio_down = h_down.Clone()
	ratio_down.Divide(h_central)
	ratio_down.SetMarkerStyle(32)
	ratio_down.Draw("p hist same")

	avg_up = TLine(0., h_up.Integral() / h_central.Integral(), mass*2, h_up.Integral() / h_central.Integral())
	avg_up.SetLineStyle(2)
	avg_up.SetLineColor(seaborn.GetColorRoot("pastel", 2))
	avg_up.Draw("same")
	avg_down = TLine(0., h_down.Integral() / h_central.Integral(), mass*2, h_down.Integral() / h_central.Integral())
	avg_down.SetLineStyle(2)
	avg_down.SetLineColor(seaborn.GetColorRoot("pastel", 2))
	avg_down.Draw("same")


	c.cd()
	c.SaveAs(analysis_config.figure_directory + "/" + c.GetName() + ".pdf")

	f.Close()
			plotter = OfflineBTagPlots(numerator_analysis, denominator_analysis, ["SingleMu_2012"])
			plotter.EfficiencyPlot(var="mjj", logy=True, binning=dijet_binning, save_tag="_" + wp + "_" + sr + "_onbtag_singlemu", ratio_range=[0.,0.1], x_range=[0., 2000.], legend_position="topright", numerator_legend="With online b-tag", denominator_legend="Without online b-tag", ratio_title="Online b-tag efficiency (no offline CSV)")

	if args.do_jetht:
		for sr in ["lowmass", "highmass"]:
			if sr == "lowmass":
				numerator_analysis = "trigjetht_eta1p7_CSVTM"
				denominator_analysis = "trigjetht_eta1p7"
			else:
				numerator_analysis = "trigjetht_eta2p2_CSVTM"
				denominator_analysis = "trigjetht_eta2p2"
			plotter = OfflineBTagPlots(numerator_analysis, denominator_analysis, ["JetHT_2012BCD"])
			plotter.FrankenEfficiencyPlot(logy=True, binning=dijet_binning, save_tag="_JetHT_CSVTM_" + sr, ratio_range=[0.,0.01], x_range=[0., 2000.], legend_position="topright", numerator_legend="With offline b-tag", denominator_legend="Without offline b-tag", ratio_title="Offline b-tag efficiency)")

		#for ht_slice in [200, 250, 300, 350, 400, 450, 500, 550, 650]:
		#	if sr == "lowmass":
		#		numerator_analysis = "trigjetht" + str(ht_slice) + "_eta1p7_CSVTM"
		#		denominator_analysis = "trigjetht" + str(ht_slice) + "_eta1p7"
		#	else:
		#		numerator_analysis = "trigjetht" + str(ht_slice) + "_CSVTM"
		#		denominator_analysis = "trigjetht" + str(ht_slice)

		#	plotter = OfflineBTagPlots(numerator_analysis, denominator_analysis, ["JetHT_2012BCD"])
		#	plotter.EfficiencyPlot(var="mjj", logy=True, binning=dijet_binning, save_tag="_CSVTM_" + sr + "_jetht" + str(ht_slice), ratio_range=[0.,0.01], x_range=[0., 2000.], legend_position="topright", numerator_legend="With offline b-tag", denominator_legend="Without offline b-tag", ratio_title="Offline b-tag efficiency", prescaled=True)

	if args.do_singlejet:
		for analysis in ["trigbbl_CSVTM", "trigbbh_CSVTM"]:
			f = TFile(analysis_config.get_b_histogram_filename(analysis, "BJetPlusX_2012"), "READ")
			h_mjj = histogram_tools.rebin_histogram(f.Get("BHistograms/h_pfjet_mjj"), dijet_binning)
			h_mjj_btagcorr = histogram_tools.rebin_histogram(f.Get("BHistograms/h_pfjet_mjj_btagcorr"), dijet_binning)
			EfficiencyPlot(h_mjj, h_mjj_btagcorr, name_num="No correction", name_den="b tag correction", logy=True, save_directory=analysis_config.figure_directory + "/OfflineBTag", save_tag="from_singlejet_"+analysis)
Example #23
0
     this_h_mjj.Scale(19700. * qcd_cross_sections[qcd_sample] /
                      input_nevents)
     this_h_mjj_truthbb = f.Get("BHistograms/h_pfjet_mjj_truthbb").Clone()
     this_h_mjj_truthbb.Scale(19700. * qcd_cross_sections[qcd_sample] /
                              input_nevents)
     if not h_mjj:
         h_mjj = this_h_mjj.Clone()
         h_mjj.SetDirectory(0)
         h_mjj_truthbb = this_h_mjj_truthbb.Clone()
         h_mjj_truthbb.SetDirectory(0)
     else:
         h_mjj.Add(this_h_mjj)
         h_mjj_truthbb.Add(this_h_mjj_truthbb)
     f.Close()
 h_mjj = histogram_tools.rebin_histogram(h_mjj,
                                         dijet_binning,
                                         normalization_bin_width=1.)
 h_mjj_truthbb = histogram_tools.rebin_histogram(h_mjj_truthbb,
                                                 dijet_binning,
                                                 normalization_bin_width=1.)
 c = TCanvas("c_purity_{}".format(analysis), "c_purity_{}".format(analysis),
             800, 1000)
 top = TPad("top", "top", 0., 0.5, 1., 1.)
 top.SetBottomMargin(0.02)
 top.SetLogy()
 top.Draw()
 top.cd()
 h_mjj.SetLineColor(1)
 h_mjj.SetLineWidth(2)
 h_mjj.GetXaxis().SetTitleSize(0)
 h_mjj.GetXaxis().SetLabelSize(0)
Example #24
0
def rooplot(save_tag,
            fit_functions,
            background_workspace,
            fitted_signal_workspaces=None,
            expected_signal_workspaces=None,
            log=False,
            x_range=None,
            data_binning=None,
            normalization_bin_width=1,
            draw_chi2ndf=False,
            draw_chi2prob=False,
            data_histogram=None,
            trigger_correction=None,
            draw_trigeff=False):
    print "Making plot " + save_tag
    c = TCanvas("c_" + save_tag, "c_" + save_tag, 800, 1200)
    l = TLegend(0.65, 0.55, 0.88, 0.8)
    l.SetFillColor(0)
    l.SetBorderSize(0)
    top = TPad("top", "top", 0., 0.5, 1., 1.)
    top.SetBottomMargin(0.03)
    top.SetLeftMargin(0.15)
    top.Draw()
    if log:
        top.SetLogy()
    c.cd()
    bottom = TPad("bottom", "bottom", 0., 0., 1., 0.5)
    bottom.SetLeftMargin(0.15)
    bottom.SetTopMargin(0.02)
    bottom.SetBottomMargin(0.2)
    bottom.Draw()
    ROOT.SetOwnership(c, False)
    ROOT.SetOwnership(top, False)
    ROOT.SetOwnership(bottom, False)
    top.cd()

    background_histograms = {}
    background_histograms_fitted_range = {}
    pull_histograms = {}
    pull_histograms_fitted_range = {}
    chi2s = {}
    ndfs = {}
    chi2ndfs = {}
    style_counter = 0
    first = True
    print "[rooplot] INFO : Opening workspace file {}".format(
        background_workspace)
    f_workspace = TFile(background_workspace, "READ")
    workspace = f_workspace.Get("w")
    workspace.Print()
    for fit_function in fit_functions:
        mjj = workspace.var("mjj")

        # First time: draw frame and data histogram
        if first:
            first = False
            if not data_histogram:
                fine_bins_array = array('d', range(int(x_min), int(x_max + 1)))
                if data_binning:
                    roobinning = RooBinning(
                        len(data_binning) - 1, data_binning, "data_binning")
                else:
                    roobinning = RooBinning(
                        len(fine_bins_array) - 1, fine_bins_array,
                        "data_binning")
                fine_roobinning = RooBinning(
                    len(fine_bins_array) - 1, fine_bins_array,
                    "data_fine_binning")
                data_histogram = workspace.data("data_obs").createHistogram(
                    "data", mjj, RooFit.Binning(roobinning))
                data_histogram.SetDirectory(0)
                data_histogram_unrebinned = workspace.data(
                    "data_obs").createHistogram(
                        "data_fine", mjj, RooFit.Binning(fine_roobinning))
                data_histogram_unrebinned.SetDirectory(0)
            else:
                data_histogram_unrebinned = data_histogram.Clone()
                data_histogram_unrebinned.SetDirectory(0)
                data_histogram = histogram_tools.rebin_histogram(
                    data_histogram, mass_bins, normalization_bin_width=1)

            if x_range:
                x_min = x_range[0]
                x_max = x_range[1]
            else:
                x_min = mjj.GetMin()
                x_max = mjj.GetMax()
            frame_top = TH1D("frame_top", "frame_top", 100, x_min, x_max)
            frame_top.SetDirectory(0)
            if log:
                frame_top.SetMaximum(data_histogram.GetMaximum() * 50.)
                frame_top.SetMinimum(0.1)
            else:
                frame_top.SetMaximum(data_histogram.GetMaximum() * 1.3)
                frame_top.SetMinimum(0.)
            if normalization_bin_width == 1:
                frame_top.GetYaxis().SetTitle("Events / GeV")
            else:
                frame_top.GetYaxis().SetTitle(
                    "Events / " + str(int(normalization_bin_width)) + " GeV")
            frame_top.GetYaxis().SetTitleSize(0.06)
            frame_top.GetYaxis().SetTitleOffset(1)
            frame_top.GetYaxis().SetLabelSize(0.06)
            frame_top.GetXaxis().SetTitleSize(0)
            frame_top.GetXaxis().SetLabelSize(0)
            frame_top.Draw()

            data_histogram.SetMarkerStyle(20)
            data_histogram.SetMarkerColor(1)
            data_histogram.SetMarkerSize(1)
            data_histogram.Draw("p same")
            l.AddEntry(data_histogram, "Data", "pl")

        # Make histogram from fitted background
        fitresult_name = "fitresult_model_" + fit_function + "_rooDatahist"
        fitresult = workspace.genobj(fitresult_name)
        fitresult.Print()
        fit = make_background_tf1_from_roofitresult(fit_function,
                                                    fitresult,
                                                    mjj_range=x_range,
                                                    trigger_correction=None)
        print "[debug] Printing fit function"
        fit.Print()

        # Normalize fit
        scale_factor = workspace.var("background_" + fit_function +
                                     "_norm").getVal() / fit.Integral(
                                         mjj.getMin(), mjj.getMax())
        fit.SetParameter(0, fit.GetParameter(0) * scale_factor)
        fit.SetParError(0, fit.GetParError(0) * scale_factor)

        background_histograms[fit_function] = data_histogram.Clone()
        background_histograms[fit_function].Reset()
        background_histograms[fit_function].SetDirectory(0)
        background_histograms[fit_function].SetName(fit_function)
        for bin in xrange(1,
                          background_histograms[fit_function].GetNbinsX() + 1):
            low_edge = background_histograms[fit_function].GetXaxis(
            ).GetBinLowEdge(bin)
            up_edge = background_histograms[fit_function].GetXaxis(
            ).GetBinUpEdge(bin)
            background_histograms[fit_function].SetBinContent(
                bin,
                fit.Integral(low_edge, up_edge) / (up_edge - low_edge) *
                normalization_bin_width)

        #if draw_trigeff:
        #	fit_notrigcorr = make_background_tf1_from_roofitresult(fit_function, fitresult, mjj_range=x_range, trigger_correction=None)
        #	scale_factor = workspace.var("background_" + fit_function + "_norm").getVal() / fit_notrigcorr.Integral(mjj.getMin(), mjj.getMax())
        #	fit_notrigcorr.SetParameter(0, fit_notrigcorr.GetParameter(0) * scale_factor)
        #	fit_notrigcorr.SetParError(0, fit_notrigcorr.GetParError(0) * scale_factor)
        #	background_histograms[fit_function + "_notrigcorr"] = data_histogram.Clone()
        #	background_histograms[fit_function + "_notrigcorr"].Reset()
        #	background_histograms[fit_function + "_notrigcorr"].SetDirectory(0)
        #	background_histograms[fit_function + "_notrigcorr"].SetName(fit_function)
        #	for bin in xrange(1, background_histograms[fit_function + "_notrigcorr"].GetNbinsX() + 1):
        #		low_edge = background_histograms[fit_function + "_notrigcorr"].GetXaxis().GetBinLowEdge(bin)
        #		up_edge = background_histograms[fit_function + "_notrigcorr"].GetXaxis().GetBinUpEdge(bin)
        #		background_histograms[fit_function + "_notrigcorr"].SetBinContent(bin, fit_notrigcorr.Integral(low_edge, up_edge) / (up_edge - low_edge) * normalization_bin_width)

        chi2s[fit_function] = 0.
        chi2ndfs[fit_function] = 0.
        ndfs[fit_function] = -1 * fit.GetNpar()
        for bin in xrange(1, data_histogram_unrebinned.GetNbinsX() + 1):
            low_edge = data_histogram_unrebinned.GetXaxis().GetBinLowEdge(bin)
            up_edge = data_histogram_unrebinned.GetXaxis().GetBinUpEdge(bin)
            if up_edge <= mjj.getMin() or low_edge >= mjj.getMax():
                continue
            # Data errors
            #this_chi2 = ((data_histogram_unrebinned.GetBinContent(bin) - fit.Integral(low_edge, up_edge)) / data_histogram_unrebinned.GetBinError(bin))**2
            # Fit errors
            this_fit = fit.Integral(low_edge, up_edge)
            this_chi2 = (
                (data_histogram_unrebinned.GetBinContent(bin) - this_fit) /
                (this_fit**0.5))**2
            chi2s[fit_function] += this_chi2
            ndfs[fit_function] += 1
            #print "[debug] Bin {}, chi2 = (({} - {})/{})**2=\t{}".format(bin, data_histogram_unrebinned.GetBinContent(bin), fit.Integral(low_edge, up_edge), this_fit**0.5, this_chi2)

        chi2ndfs[fit_function] = chi2s[fit_function] / ndfs[fit_function]
        print "#chi^2/NDF(" + fit_function + ") = " + str(
            round(chi2s[fit_function], 3)) + "/" + str(
                ndfs[fit_function]) + " = " + str(
                    round(chi2ndfs[fit_function], 3)) + " / p = " + str(
                        TMath.Prob(chi2s[fit_function], ndfs[fit_function]))

        # Draw total background histogram as a colored line
        background_histograms[fit_function].SetLineColor(
            seaborn.GetColorRoot("default", style_counter))
        background_histograms[fit_function].SetLineWidth(1)
        background_histograms[fit_function].SetLineStyle(1)
        background_histograms[fit_function].Draw("hist same")
        if draw_chi2ndf:
            legend_entry = fit_function + " (#chi^{2}/NDF=" + str(
                round(chi2ndfs[fit_function], 2)) + ")"
        elif draw_chi2prob:
            legend_entry = fit_function + " (p=" + str(
                round(ROOT.TMath.Prob(chi2s[fit_function], ndfs[fit_function]),
                      2)) + ")"
        else:
            legend_entry = fit_function
        l.AddEntry(background_histograms[fit_function], legend_entry, "l")
        background_histograms[fit_function].SetDirectory(0)

        if draw_trigeff:
            background_histograms[fit_function + "_notrigcorr"].SetLineColor(
                seaborn.GetColorRoot("pastel", style_counter))
            background_histograms[fit_function + "_notrigcorr"].SetLineWidth(1)
            background_histograms[fit_function + "_notrigcorr"].SetLineStyle(3)
            background_histograms[fit_function +
                                  "_notrigcorr"].Draw("hist same")
            legend_entry = fit_function + ", no trig corr"
            l.AddEntry(background_histograms[fit_function + "_notrigcorr"],
                       legend_entry, "l")
            background_histograms[fit_function + "_notrigcorr"].SetDirectory(0)

        pull_histograms[fit_function] = data_histogram.Clone()
        pull_histograms[fit_function].Reset()
        pull_histograms[fit_function].SetDirectory(0)
        for bin in xrange(1, data_histogram.GetNbinsX() + 1):
            if data_histogram.GetBinError(bin) > 0:
                pull = (data_histogram.GetBinContent(bin) -
                        background_histograms[fit_function].GetBinContent(bin)
                        ) / (data_histogram.GetBinError(bin))
            else:
                pull = 0.
            #print "[debug] Pull = " + str(pull)
            pull_histograms[fit_function].SetBinContent(bin, pull)
            pull_histograms[fit_function].SetBinError(bin, 0.)

        if draw_trigeff:
            pull_histograms[fit_function +
                            "_notrigcorr"] = data_histogram.Clone()
            pull_histograms[fit_function + "_notrigcorr"].Reset()
            pull_histograms[fit_function + "_notrigcorr"].SetDirectory(0)
            for bin in xrange(1, data_histogram.GetNbinsX() + 1):
                if data_histogram.GetBinError(bin) > 0:
                    pull = (
                        data_histogram.GetBinContent(bin) -
                        background_histograms[fit_function + "_notrigcorr"].
                        GetBinContent(bin)) / (data_histogram.GetBinError(bin))
                else:
                    pull = 0.
                #print "[debug] Pull = " + str(pull)
                pull_histograms[fit_function + "_notrigcorr"].SetBinContent(
                    bin, pull)
                pull_histograms[fit_function + "_notrigcorr"].SetBinError(
                    bin, 0.)

        pull_histograms_fitted_range[fit_function] = pull_histograms[
            fit_function].Clone()
        pull_histograms_fitted_range[fit_function].SetDirectory(0)
        for bin in xrange(
                1, pull_histograms_fitted_range[fit_function].GetNbinsX() + 1):
            bin_center = pull_histograms_fitted_range[fit_function].GetXaxis(
            ).GetBinCenter(bin)
            if bin_center < mjj.getMin() or bin_center > mjj.getMax():
                pull_histograms_fitted_range[fit_function].SetBinContent(
                    bin, 0)
                pull_histograms_fitted_range[fit_function].SetBinError(bin, 0)

        style_counter += 1
    f_workspace.Close()

    l.Draw()
    Root.CMSLabelTwoPane(0.65, 0.84, "Internal", 1, 0.6)
    # Pull histogram
    c.cd()
    bottom.cd()

    #pull_histogram = frame_top.pullHist("Data", "B Fit")
    frame_bottom = TH1D("frame_bottom", "frame_bottom", 100, x_min, x_max)
    frame_bottom.SetMinimum(-5.)
    frame_bottom.SetMaximum(5.)
    #pull_histogram.plotOn(frame_bottom, RooFit.Name(fit_pdf_name))
    frame_bottom.GetXaxis().SetNdivisions(505)
    frame_bottom.GetXaxis().SetTitle("m_{jj} [GeV]")
    frame_bottom.GetXaxis().SetLabelSize(0.06)
    frame_bottom.GetXaxis().SetTitleSize(0.06)
    frame_bottom.GetXaxis().SetTitleOffset(1.0)
    frame_bottom.GetYaxis().SetTitle("#frac{Data - Fit}{#sigma(Data)}")
    frame_bottom.GetYaxis().SetLabelSize(0.06)
    frame_bottom.GetYaxis().SetTitleSize(0.06)
    frame_bottom.GetYaxis().SetTitleOffset(1.0)
    frame_bottom.Draw()

    style_counter = 0
    for fit_function in fit_functions:
        pull_histograms[fit_function].SetLineColor(
            seaborn.GetColorRoot("default", style_counter))
        pull_histograms[fit_function].SetLineWidth(2)
        pull_histograms[fit_function].SetLineStyle(2)
        pull_histograms[fit_function].Draw("hist same")

        if draw_trigeff:
            pull_histograms[fit_function].SetLineColor(
                seaborn.GetColorRoot("pastel", style_counter))
            pull_histograms[fit_function].SetLineWidth(1)
            pull_histograms[fit_function].SetLineStyle(3)
            pull_histograms[fit_function].Draw("hist same")

        pull_histograms_fitted_range[fit_function].SetLineColor(
            seaborn.GetColorRoot("dark", style_counter))
        pull_histograms_fitted_range[fit_function].SetLineWidth(2)
        pull_histograms_fitted_range[fit_function].SetLineStyle(1)
        pull_histograms_fitted_range[fit_function].Draw("hist same")
        style_counter += 1
    #pull_histogram.GetXaxis().SetTitle("m_{jj} [GeV]")
    #pull_histogram.GetYaxis().SetTitle("#frac{Data - Fit}{#sigma(Fit)}")
    #pull_histogram.Draw("same")
    c.cd()

    c.SaveAs(
        "/uscms/home/dryu/Dijets/data/EightTeeEeVeeBee/Results/figures/c_" +
        save_tag + ".pdf")