def plotHisto(self, histo, canvasName): plot = plots.PlotBase() plot.histoMgr.appendHisto(histograms.Histo(histo, histo.GetName())) plot.createFrame(canvasName + self.label, opts={ "ymin": 0.1, "ymaxfactor": 2. }) histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(x=None, y=None, lumi=self.lumi) plot.getPad().SetLogy(True) integralValue = int(0.5 + histo.Integral(0, histo.GetNbinsX(), "width")) print histo.GetName(), "Integral", histo.Integral( 0, histo.GetNbinsX(), "width") histograms.addText(0.4, 0.7, "Integral = %s ev" % integralValue) match = re.search("aseline", histo.GetName()) if match: self.nBaseQCD = integralValue match = re.search("nverted", histo.GetName()) if match: self.nInvQCD = integralValue self.plotIntegral(plot, histo.GetName())
def distComparison(datasets): # Create a comparison plot of two distributions (must have the same binning) # Set the names of DatasetRootHisto objects in order to be able easily reference them later drh1 = datasets.getDataset("Data").getDatasetRootHisto(analysis+"/MET_BaseLineTauId") drh1.setName("Base") drh1.normalizeToOne() drh2 = datasets.getDataset("Data").getDatasetRootHisto(analysis+"/MET_InvertedTauId") drh2.setName("Inv") drh2.normalizeToOne() plot = plots.ComparisonPlot(drh1, drh2) # Set the styles st1 = styles.getDataStyle().clone() st2 = st1.clone() st2.append(styles.StyleLine(lineColor=ROOT.kRed)) plot.histoMgr.forHisto("Base", st1) plot.histoMgr.forHisto("Inv", st2) # Set the legend labels plot.histoMgr.setHistoLegendLabelMany({"Base": "Baseline Tau ID", "Inv": "Inverted Tau ID"}) # Set the legend styles plot.histoMgr.setHistoLegendStyleAll("L") plot.histoMgr.setHistoLegendStyle("Base", "P") # exception to the general rule # Set the drawing styles plot.histoMgr.setHistoDrawStyleAll("HIST") plot.histoMgr.setHistoDrawStyle("Base", "EP") # exception to the general rule # Rebin, if necessary plot.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(10)) # Create frame with a ratio pad plot.createFrame("METbaseVSinvertedTauID", opts={"ymin":1e-5, "ymaxfactor": 1.5}, createRatio=True, opts2={"ymin": -10, "ymax": 50}, # bounds of the ratio plot ) # Set Y axis of the upper pad to logarithmic plot.getPad1().SetLogy(True) # Create legend to the default position plot.setLegend(histograms.createLegend()) # Set the X/Y axis labels plot.frame.GetXaxis().SetTitle("MET (GeV)") plot.frame.GetYaxis().SetTitle("Arbitrary units") # Draw the plot plot.draw() # Add the various texts to histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(x=None, y=None, lumi=datasets.getDataset("Data").getLuminosity()) # Save the plot to files plot.save()
def plotHisto(self,histo,canvasName): plot = plots.PlotBase() plot.histoMgr.appendHisto(histograms.Histo(histo,histo.GetName())) plot.createFrame(canvasName+self.label, opts={"ymin": 0.1, "ymaxfactor": 2.}) histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(x=None, y=None, lumi=self.lumi) plot.getPad().SetLogy(True) integralValue = int(0.5 + histo.Integral(0,histo.GetNbinsX(),"width")) print histo.GetName(),"Integral",histo.Integral(0,histo.GetNbinsX(),"width") histograms.addText(0.4,0.7,"Integral = %s ev"% integralValue) match = re.search("aseline",histo.GetName()) if match: self.nBaseQCD = integralValue match = re.search("nverted",histo.GetName()) if match: self.nInvQCD = integralValue self.plotIntegral(plot, histo.GetName())
def fitQCD(self, origHisto): histo = origHisto.Clone("histo") rangeMin = histo.GetXaxis().GetXmin() rangeMax = histo.GetXaxis().GetXmax() numberOfParameters = 8 print "Fit range ", rangeMin, " - ", rangeMax class FitFunction: def __call__(self, x, par): return QCDFunction(x, par, 1) theFit = TF1('theFit', FitFunction(), rangeMin, rangeMax, numberOfParameters) theFit.SetParLimits(0, 1, 20) theFit.SetParLimits(1, 20, 40) theFit.SetParLimits(2, 10, 25) theFit.SetParLimits(3, 1, 10) theFit.SetParLimits(4, 0, 150) theFit.SetParLimits(5, 10, 100) theFit.SetParLimits(6, 0.0001, 1) theFit.SetParLimits(7, 0.001, 0.05) if self.label == "Baseline": rangeMax = 240 if self.label == "7080": theFit.SetParLimits(5, 10, 100) # if self.label == "100120": # theFit.SetParLimits(0,1,20) # theFit.SetParLimits(2,1,25) # theFit.SetParLimits(3,0.1,20) if self.label == "120150": theFit.SetParLimits(0, 1, 20) theFit.SetParLimits(3, 0.1, 5) gStyle.SetOptFit(0) plot = plots.PlotBase() plot.histoMgr.appendHisto(histograms.Histo(histo, histo.GetName())) plot.createFrame("qcdfit" + self.label, opts={ "ymin": 1e-5, "ymaxfactor": 2. }) histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(x=None, y=None, lumi=self.lumi) self.normInvQCD = histo.Integral(0, histo.GetNbinsX()) histo.Scale(1 / self.normInvQCD) histo.Fit(theFit, "LR") theFit.SetRange(histo.GetXaxis().GetXmin(), histo.GetXaxis().GetXmax()) theFit.SetLineStyle(2) theFit.Draw("same") histograms.addText(0.4, 0.8, "Inverted TauID") plot.histoMgr.appendHisto(histograms.Histo(theFit, "Fit")) plot.getPad().SetLogy(True) plot.draw() plot.save() self.parInvQCD = theFit.GetParameters() fitPars = "fit parameters " i = 0 while i < numberOfParameters: fitPars = fitPars + " " + str(self.parInvQCD[i]) i = i + 1 print fitPars self.nFitInvQCD = theFit.Integral(0, 1000, self.parInvQCD) print "Integral ", self.normInvQCD * self.nFitInvQCD
def cutefficiency(self, histo1, histo2): h1 = histo1.Clone("h1") h2 = histo2.Clone("h2") h1.Scale(1 / h1.GetMaximum()) h2.Scale(1 / h2.GetMaximum()) # check that no bin has negative value, negative values possible after subtracting EWK from data iBin = 1 nBins = h1.GetNbinsX() while iBin < nBins: value1 = h1.GetBinContent(iBin) value2 = h2.GetBinContent(iBin) if value1 < 0: h1.SetBinContent(iBin, 0) if value2 < 0: h2.SetBinContent(iBin, 0) iBin = iBin + 1 h1cut = h1.Clone("h1cut") h1cut.Reset() h1cut.GetYaxis().SetTitle("Efficiency") h1cut.GetXaxis().SetTitle("PF MET cut (GeV)") h2cut = h2.Clone("h2cut") h2cut.Reset() h2cut.SetLineColor(2) integralError = ROOT.Double(0.0) integralValue = h1.IntegralAndError(1, h1cut.GetNbinsX(), integralError) h1_integral = h1.Integral(0, h1.GetNbinsX()) h2_integral = h2.Integral(0, h2.GetNbinsX()) iBin = 1 nBins = h1cut.GetNbinsX() while iBin < nBins: error = ROOT.Double(0.0) selected1 = h1.IntegralAndError(iBin, nBins, error) if selected1 > 0: error = error / selected1 else: error = integralError / integralValue efficiency1 = selected1 / h1_integral h1cut.SetBinContent(iBin, efficiency1) if self.errorBars: h1cut.SetBinError(iBin, error) error = ROOT.Double(0.0) selected2 = h2.IntegralAndError(iBin, nBins, error) if selected2 > 0: error = error / selected2 else: error = integralError / integralValue efficiency2 = selected2 / h2_integral h2cut.SetBinContent(iBin, efficiency2) if self.errorBars: h2cut.SetBinError(iBin, error) iBin = iBin + 1 plot = plots.ComparisonPlot( histograms.Histo(h1cut, "Inv"), histograms.Histo(h2cut, "Base"), ) # Set the styles st1 = styles.getDataStyle().clone() st2 = st1.clone() st2.append(styles.StyleLine(lineColor=ROOT.kRed)) st2.append(styles.StyleMarker(markerColor=ROOT.kRed)) plot.histoMgr.forHisto("Base", st1) plot.histoMgr.forHisto("Inv", st2) # Set the legend labels plot.histoMgr.setHistoLegendLabelMany({ "Inv": h1.GetTitle(), "Base": h2.GetTitle() }) # Set the legend styles #plot.histoMgr.setHistoLegendStyleAll("L") plot.histoMgr.setHistoLegendStyleAll("P") # Set the drawing styles #plot.histoMgr.setHistoDrawStyleAll("HIST") plot.histoMgr.setHistoDrawStyleAll("EP") # Create frame with a ratio pad plot.createFrame( "cuteff" + self.label, opts={ "ymin": 1e-5, "ymaxfactor": 2 }, createRatio=True, opts2={ "ymin": 0, "ymax": 2 }, # bounds of the ratio plot ) # Set Y axis of the upper pad to logarithmic plot.getPad().SetLogy(True) plot.setLegend(histograms.createLegend(0.4, 0.82, 0.9, 0.93)) histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(x=None, y=None, lumi=self.lumi) plot.draw() plot.save() ###### hError = h1cut.Clone("hError") hError.Divide(h2cut) iBin = 1 nBins = hError.GetNbinsX() while iBin < nBins: hError.SetBinContent(iBin, abs(hError.GetBinContent(iBin) - 1)) iBin = iBin + 1 hError.GetYaxis().SetTitle( "abs( (#varepsilon^{Inverted} - #varepsilon^{Baseline})/#varepsilon^{Baseline} )" ) hError.GetXaxis().SetTitle("PF MET cut (GeV)") plot2 = plots.PlotBase() plot2.histoMgr.appendHisto(histograms.Histo(hError, "ShapeUncertainty")) plot2.histoMgr.forHisto("ShapeUncertainty", st1) plot2.histoMgr.setHistoDrawStyleAll("EP") # plot2.createFrame("shapeUncertainty"+self.label, opts={"ymin":-1, "ymax": 1}) plot2.createFrame("shapeUncertainty" + self.label, opts={ "ymin": -0.1, "ymax": 1.1 }) histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(x=None, y=None, lumi=self.lumi) rangeMin = hError.GetXaxis().GetXmin() rangeMax = hError.GetXaxis().GetXmax() # rangeMax = 80 rangeMax = 120 # rangeMax = 380 numberOfParameters = 2 class FitFunction: def __call__(self, x, par): # return Linear(x,par) return ErrorFunction(x, par) theFit = TF1('theFit', FitFunction(), rangeMin, rangeMax, numberOfParameters) theFit.SetParLimits(0, 0.01, 0.03) theFit.SetParLimits(1, 50, 150) # theFit.FixParameter(0,0.02) # theFit.FixParameter(1,100) hError.Fit(theFit, "LRN") print "Error MET > 40", theFit.Eval(40) print "Error MET > 50", theFit.Eval(50) print "Error MET > 70", theFit.Eval(70) plot2.histoMgr.appendHisto(histograms.Histo(theFit, "Fit")) plot2.draw() plot2.save()
def comparison(self, histo1, histo2, norm=1): h1 = histo1.Clone("h1") h2 = histo2.Clone("h2") if norm == 1: h1.Scale(1 / h1.GetMaximum()) h2.Scale(1 / h2.GetMaximum()) # check that no bin has negative value, negative values possible after subtracting EWK from data iBin = 1 nBins = h1.GetNbinsX() while iBin < nBins: value1 = h1.GetBinContent(iBin) value2 = h2.GetBinContent(iBin) if value1 < 0: h1.SetBinContent(iBin, 0) if value2 < 0: h2.SetBinContent(iBin, 0) iBin = iBin + 1 if norm > 0: h1.GetYaxis().SetTitle("Arbitrary units") plot = plots.ComparisonPlot( histograms.Histo(h1, "Inv"), histograms.Histo(h2, "Base"), ) # Set the styles st1 = styles.getDataStyle().clone() st2 = st1.clone() st2.append(styles.StyleMarker(markerColor=ROOT.kRed)) plot.histoMgr.forHisto("Base", st1) plot.histoMgr.forHisto("Inv", st2) # Set the legend labels plot.histoMgr.setHistoLegendLabelMany({ "Inv": h1.GetTitle(), "Base": h2.GetTitle() }) # Set the legend styles plot.histoMgr.setHistoLegendStyleAll("P") # Set the drawing styles plot.histoMgr.setHistoDrawStyleAll("EP") # Create frame with a ratio pad plot.createFrame( "comparison" + self.label, opts={ "ymin": 1e-5, "ymaxfactor": 2 }, createRatio=True, opts2={ "ymin": 0, "ymax": 2 }, # bounds of the ratio plot ) # Set Y axis of the upper pad to logarithmic plot.getPad1().SetLogy(True) plot.setLegend(histograms.createLegend(0.4, 0.82, 0.9, 0.93)) histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(x=None, y=None, lumi=self.lumi) plot.draw() plot.save()
def getEfficiency(postfix, lumi): f = ROOT.TFile.Open("tagprobe_output_%s.root" % postfix) trig = "" if "Mu20" in postfix: trig = "HLT_Mu20" elif "Mu24" in postfix: trig = "HLT_Mu24" elif "Mu30" in postfix: trig = "HLT_Mu30" elif "Mu40" in postfix: trig = "HLT_Mu40" l = ROOT.TLatex() l.SetNDC() l.SetTextFont(l.GetTextFont()-20) # bold -> normal # cntfit = "cnt" cntfit = "fit" path = "tnpTree/All/%s_eff_plots" % cntfit plot = "pt_PLOT" graph = "hxy_%s_eff" % cntfit pathPt = "tnpTree/All_pt/%s_eff_plots" % cntfit grahpPt = "hxy_%s_eff" % cntfit canv = f.Get(path+"/"+plot) canv.SetName("TagProbe_%s_%s_%s" % (postfix, plot, graph)) # canv.SaveAs(".eps") canv.SaveAs(".png") eff = canv.FindObject(graph) #print eff eff_value = eff.GetY()[0] eff_plus = eff.GetErrorYhigh(0) eff_minus = eff.GetErrorYlow(0) canv = f.Get(pathPt+"/"+plot) gr = canv.FindObject(graph).Clone() name = "TagProbe_Pt100_%s_%s_%s" % (postfix, plot, graph) c = ROOT.TCanvas(name, name) frame = c.DrawFrame(40, 0, 100, 1.1) gr.Draw("EP") frame.GetXaxis().SetTitle("Probe muon p_{T} (GeV/c)") frame.GetYaxis().SetTitle("Trigger and ID efficiency") histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(None, None, lumi) l.DrawLatex(0.2, 0.4, trig) c.SaveAs(".png") gr = canv.FindObject(graph).Clone() name = "TagProbe_Pt400_%s_%s_%s" % (postfix, plot, graph) c = ROOT.TCanvas(name, name) frame = c.DrawFrame(40, 0, 400, 1.1) gr.Draw("EP") frame.GetXaxis().SetTitle("Probe muon p_{T} (GeV/c)") frame.GetYaxis().SetTitle("Trigger and ID efficiency") histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(None, None, lumi) l.DrawLatex(0.2, 0.4, trig) c.SaveAs(".png") # if postfix == "Run2011A_Mu40": # eff_plus = 0.006 # eff_minus = 0.006 print "%s overall efficiency %f + %f - %f" % (postfix, eff_value, eff_plus, eff_minus) return (eff_value, eff_plus, eff_minus)
def doPlots(table, onlyWjets, mcEvents, normalize, lumi): nrows = table.getNrows() function = ROOT.TF1("fitFunction", "[0]") function.SetParameter(0, 0) f2 = ROOT.TF1("fitG", "gaus") f2.SetLineColor(ROOT.kRed) f2.SetLineWidth(2) binning = { "Data": (8, 60, 100), "Diboson": (8, 0, 2), "DYJetsToLL": (8, 1, 5), "EWKMCsum": (8, 40, 120), "SingleTop": (8, 3, 6), "TTJets": (10, 25, 35), "W3Jets": (10, 6, 11), "WJets": (14, 10, 80), } if onlyWjets: binning["WJets"] = (24, 10, 90) if not normalize: binning["Data"] = (10, 70, 120) binning["EWKMCsum"] = (6, 60, 120) binning["SingleTop"] = (8, 4, 6) binning["TTJets"] = (10, 32, 42) binning["W3Jets"] = (12, 6, 12) if onlyWjets: binning["WJets"] = (10, 20, 60) if mcEvents: binning["TTJets"] = (12, 320, 440) binning["WJets"] = (24, 30, 90) for icol in xrange(table.getNcolumns()): name = table.getColumnNames()[icol] label = plots._legendLabels.get(name, name) if name != "Data": label += " simulation" h = ROOT.TH1F(name, name, nrows, 0, nrows) h2 = ROOT.TH1F(name+"_dist", name, *(binning.get(name, (10, 0, 100)))) mean = dataset.Count(0, 0) for irow in xrange(nrows): count = table.getCount(irow, icol) h.SetBinContent(irow+1, count.value()) h.SetBinError(irow+1, count.uncertainty()) h2.Fill(count.value()) mean.add(count) mean = dataset.Count(mean.value()/nrows, mean.uncertainty()/nrows) h.Fit("fitFunction") value = function.GetParameter(0) error = function.GetParError(0) # function.SetParameters(1., 40., 1.); # function.SetParLimits(0, 0.0, 1.0); # fitResult = graph.Fit(function, "NRSE+EX0"); # print "Fit status", fitResult.Status() # #fitResult.Print("V"); # #fitResult.GetCovarianceMatrix().Print(); # function.SetLineColor(graph.GetMarkerColor()); # function.SetLineWidth(2); function.Draw("same") ROOT.gPad.Update() stat = h.FindObject("stats") if stat: stat.SetX1NDC(0.2) stat.SetX2NDC(0.44) stat.SetY1NDC(0.2) stat.SetY2NDC(0.3) stat.SetTextColor(ROOT.kRed) stat.SetLineColor(ROOT.kRed) # return (function, fitResult) styles.dataStyle.apply(h) p = plots.PlotBase([h]) p.histoMgr.setHistoDrawStyle(name, "EP") p.createFrame("fluctuation_"+name, opts={"ymin": 0, "ymaxfactor": 1.2, "nbins": nrows}) p.frame.GetXaxis().SetTitle("Embedding trial number") ylabel = "Simulation" if name == "Data": ylabel = "Data" ylabel += " events" p.frame.GetYaxis().SetTitle(ylabel) step = 1 start = 0 if onlyWjets: start = 4 step = 5 for irow in xrange(start, nrows, step): p.frame.GetXaxis().SetBinLabel(irow+1, "%d"%(irow+1)) xmin = p.frame.GetXaxis().GetXmin() xmax = p.frame.GetXaxis().GetXmax() leg = histograms.moveLegend(histograms.createLegend(), dx=-0.07, dy=-0.6, dh=-0.15) leg.AddEntry(h, "Trial values", "P") def createLine(val, st=1, col=ROOT.kRed): l = ROOT.TLine(xmin, val, xmax, val) l.SetLineWidth(2) l.SetLineStyle(st) l.SetLineColor(col) return l fv = createLine(value) leg.AddEntry(fv, "Fitted value", "L") p.appendPlotObject(fv) # fe = createLine(value+error, ROOT.kDashed) # leg.AddEntry(fe, "Fit uncertainty", "L") # p.appendPlotObject(fe) # p.appendPlotObject(createLine(value-error, ROOT.kDashed)) v = createLine(mean.value(), col=ROOT.kBlue) leg.AddEntry(v, "Mean", "L") p.appendPlotObject(v) ve = createLine(mean.value()+mean.uncertainty(), st=ROOT.kDashed, col=ROOT.kBlue) leg.AddEntry(ve, "Mean uncertainty", "L") p.appendPlotObject(ve) p.appendPlotObject(createLine(mean.value()-mean.uncertainty(), st=ROOT.kDashed, col=ROOT.kBlue)) p.legend = leg p.appendPlotObject(histograms.PlotText(0.65, 0.33, label, size=20)) p.draw() if name != "Data": histograms.addCmsPreliminaryText(text="Simulation") histograms.addEnergyText() histograms.addLuminosityText(None, None, lumi) p.save() ############### f2.SetParameter(1, value) h2.Fit("fitG") # f2.Draw("same") ROOT.gPad.Update() stat = h2.FindObject("stats") if stat: stat.SetX1NDC(0.62) stat.SetX2NDC(0.9) stat.SetY1NDC(0.7) stat.SetY2NDC(0.85) stat.SetTextColor(ROOT.kRed) stat.SetLineColor(ROOT.kRed) styles.dataStyle.apply(h2) p = plots.PlotBase([h2]) p.histoMgr.setHistoDrawStyle(name+"_dist", "HIST") p.createFrame("fluctuation_"+name+"_dist", opts={"ymin": 0, "ymaxfactor": 1.4, "nbins": nrows}) p.frame.GetXaxis().SetTitle(ylabel) p.frame.GetYaxis().SetTitle("Occurrances") ymin = p.frame.GetYaxis().GetXmin() ymax = p.frame.GetYaxis().GetXmax() leg = histograms.moveLegend(histograms.createLegend(), dx=-0.07, dy=-0.25, dh=-0.15) leg.AddEntry(h2, "Trials", "F") leg.AddEntry(f2, "Gaussian fit", "L") def createLine2(val, st=1): l = ROOT.TLine(val, ymin, val, ymax) l.SetLineWidth(1) l.SetLineColor(ROOT.kBlue) l.SetLineStyle(st) return l p.appendPlotObject(h2, "FUNC") p.appendPlotObject(stat) p.appendPlotObject(histograms.PlotText(0.65, 0.88, label, size=20)) # fv = createLine2(value) # leg.AddEntry(fv, "Fit of values", "L") # p.appendPlotObject(fv) # fe = createLine2(value+error, ROOT.kDashed) # leg.AddEntry(fe, "Fit of values unc.", "L") # p.appendPlotObject(fe) # p.appendPlotObject(createLine2(value-error, ROOT.kDashed)) p.legend = leg p.draw() if name != "Data": histograms.addCmsPreliminaryText(text="Simulation") histograms.addEnergyText() histograms.addLuminosityText(None, None, lumi) p.save()
def getEfficiency(postfix, lumi): f = ROOT.TFile.Open("tagprobe_output_%s.root" % postfix) trig = "" if "Mu20" in postfix: trig = "HLT_Mu20" elif "Mu24" in postfix: trig = "HLT_Mu24" elif "Mu30" in postfix: trig = "HLT_Mu30" elif "Mu40" in postfix: trig = "HLT_Mu40" l = ROOT.TLatex() l.SetNDC() l.SetTextFont(l.GetTextFont() - 20) # bold -> normal # cntfit = "cnt" cntfit = "fit" path = "tnpTree/All/%s_eff_plots" % cntfit plot = "pt_PLOT" graph = "hxy_%s_eff" % cntfit pathPt = "tnpTree/All_pt/%s_eff_plots" % cntfit grahpPt = "hxy_%s_eff" % cntfit canv = f.Get(path + "/" + plot) canv.SetName("TagProbe_%s_%s_%s" % (postfix, plot, graph)) # canv.SaveAs(".eps") canv.SaveAs(".png") eff = canv.FindObject(graph) #print eff eff_value = eff.GetY()[0] eff_plus = eff.GetErrorYhigh(0) eff_minus = eff.GetErrorYlow(0) canv = f.Get(pathPt + "/" + plot) gr = canv.FindObject(graph).Clone() name = "TagProbe_Pt100_%s_%s_%s" % (postfix, plot, graph) c = ROOT.TCanvas(name, name) frame = c.DrawFrame(40, 0, 100, 1.1) gr.Draw("EP") frame.GetXaxis().SetTitle("Probe muon p_{T} (GeV/c)") frame.GetYaxis().SetTitle("Trigger and ID efficiency") histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(None, None, lumi) l.DrawLatex(0.2, 0.4, trig) c.SaveAs(".png") gr = canv.FindObject(graph).Clone() name = "TagProbe_Pt400_%s_%s_%s" % (postfix, plot, graph) c = ROOT.TCanvas(name, name) frame = c.DrawFrame(40, 0, 400, 1.1) gr.Draw("EP") frame.GetXaxis().SetTitle("Probe muon p_{T} (GeV/c)") frame.GetYaxis().SetTitle("Trigger and ID efficiency") histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(None, None, lumi) l.DrawLatex(0.2, 0.4, trig) c.SaveAs(".png") # if postfix == "Run2011A_Mu40": # eff_plus = 0.006 # eff_minus = 0.006 print "%s overall efficiency %f + %f - %f" % (postfix, eff_value, eff_plus, eff_minus) return (eff_value, eff_plus, eff_minus)
def fitData(self,histo): parInvQCD = self.parInvQCD parMCEWK = self.parMCEWK nInvQCD = self.nInvQCD nFitInvQCD = self.nFitInvQCD nMCEWK = self.nMCEWK class FitFunction: def __call__( self, x, par ): return par[0]*(par[1] * QCDFunction(x,parInvQCD,1/nFitInvQCD) + ( 1 - par[1] ) * EWKFunction(x,parMCEWK,1/nMCEWK)) class QCDOnly: def __call__( self, x, par ): return par[0]*par[1] * QCDFunction(x,parInvQCD,1/nFitInvQCD) rangeMin = histo.GetXaxis().GetXmin() rangeMax = histo.GetXaxis().GetXmax() numberOfParameters = 2 print "Fit range ",rangeMin, " - ",rangeMax theFit = TF1("theFit",FitFunction(),rangeMin,rangeMax,numberOfParameters) plot = plots.PlotBase() plot.histoMgr.appendHisto(histograms.Histo(histo,histo.GetName())) plot.createFrame("combinedfit"+self.label, opts={"ymin": 1e-5, "ymaxfactor": 2.}) histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(x=None, y=None, lumi=self.lumi) print "data events ",histo.Integral(0,histo.GetNbinsX()) histo.Fit(theFit,"R") theFit.SetRange(histo.GetXaxis().GetXmin(),histo.GetXaxis().GetXmax()) theFit.SetLineStyle(2) theFit.Draw("same") par = theFit.GetParameters() qcdOnly = TF1("qcdOnly",QCDOnly(),rangeMin,rangeMax,numberOfParameters) qcdOnly.FixParameter(0,par[0]) qcdOnly.FixParameter(1,par[1]) qcdOnly.SetLineStyle(2) qcdOnly.Draw("same") histograms.addText(0.35,0.8,"Data, Baseline TauID") histograms.addText(0.4,0.3,"QCD",15) plot.histoMgr.appendHisto(histograms.Histo(qcdOnly,"qcdOnly")) plot.getPad().SetLogy(True) plot.draw() plot.save() fitPars = "fit parameters " i = 0 while i < numberOfParameters: fitPars = fitPars + " " + str(par[i]) i = i + 1 print fitPars nBaseQCD = par[0] self.QCDfraction = par[1] if len(self.label) > 0: print "Bin ",self.label print "Integral ", nBaseQCD print "QCD fraction ",self.QCDfraction return theFit
def fitEWK(self,histo,options="R"): rangeMin = histo.GetXaxis().GetXmin() rangeMax = histo.GetXaxis().GetXmax() # rangeMin = 120 # rangeMax = 120 numberOfParameters = 4 print "Fit range ",rangeMin, " - ",rangeMax class FitFunction: def __call__( self, x, par ): return EWKFunction(x,par,1,1) # return SumFunction(x,par) # return TestFunction(x,par,1) class PlotFunction: def __call__( self, x, par ): return EWKFunction(x,par,0) theFit = TF1('theFit',FitFunction(),rangeMin,rangeMax,numberOfParameters) thePlot = TF1('thePlot',PlotFunction(),rangeMin,rangeMax,numberOfParameters) theFit.SetParLimits(0,5,30) theFit.SetParLimits(1,90,120) theFit.SetParLimits(2,30,50) theFit.SetParLimits(3,0.001,1) if self.label == "4050": theFit.SetParLimits(0,5,20) theFit.SetParLimits(1,90,120) theFit.SetParLimits(2,30,50) theFit.SetParLimits(3,0.001,1) if self.label == "5060": theFit.SetParLimits(0,5,20) theFit.SetParLimits(1,90,120) theFit.SetParLimits(2,20,50) theFit.SetParLimits(3,0.001,1) if self.label == "6070": theFit.SetParLimits(0,5,50) theFit.SetParLimits(1,90,150) theFit.SetParLimits(2,20,50) theFit.SetParLimits(3,0.001,1) if self.label == "7080": theFit.SetParLimits(0,5,60) theFit.SetParLimits(1,90,200) theFit.SetParLimits(2,20,100) theFit.SetParLimits(3,0.001,1) if self.label == "80100": theFit.SetParLimits(0,5,50) theFit.SetParLimits(1,50,170) theFit.SetParLimits(2,20,60) theFit.SetParLimits(3,0.001,1) if self.label == "100120": theFit.SetParLimits(0,5,50) theFit.SetParLimits(1,90,170) theFit.SetParLimits(2,20,60) theFit.SetParLimits(3,0.001,1) if self.label == "120150": theFit.SetParLimits(0,5,50) theFit.SetParLimits(1,60,170) theFit.SetParLimits(2,10,100) theFit.SetParLimits(3,0.001,1) if self.label == "150": theFit.SetParLimits(0,5,50) theFit.SetParLimits(1,70,170) theFit.SetParLimits(2,20,100) theFit.SetParLimits(3,0.001,1) plot = plots.PlotBase() plot.histoMgr.appendHisto(histograms.Histo(histo,histo.GetName())) plot.createFrame("ewkfit"+self.label, opts={"ymin": 1e-5, "ymaxfactor": 2.}) histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(x=None, y=None, lumi=self.lumi) self.normEWK = histo.Integral(0,histo.GetNbinsX()) histo.Scale(1/self.normEWK) histo.Fit(theFit,options) theFit.SetRange(histo.GetXaxis().GetXmin(),histo.GetXaxis().GetXmax()) theFit.SetLineStyle(2) theFit.Draw("same") self.parMCEWK = theFit.GetParameters() fitPars = "fit parameters " i = 0 while i < numberOfParameters: fitPars = fitPars + " " + str(self.parMCEWK[i]) thePlot.SetParameter(i,theFit.GetParameter(i)) i = i + 1 thePlot.Draw("same") histograms.addText(0.2,0.2,"EWK MC, baseline TauID") plot.histoMgr.appendHisto(histograms.Histo(theFit,"Fit")) plot.getPad().SetLogy(True) plot.draw() plot.save() self.parMCEWK = theFit.GetParameters() print fitPars self.nMCEWK = theFit.Integral(0,1000,self.parMCEWK) print "Integral ",self.normEWK*self.nMCEWK
def fitQCD(self,origHisto): histo = origHisto.Clone("histo") rangeMin = histo.GetXaxis().GetXmin() rangeMax = histo.GetXaxis().GetXmax() numberOfParameters = 8 print "Fit range ",rangeMin, " - ",rangeMax class FitFunction: def __call__( self, x, par ): return QCDFunction(x,par,1) theFit = TF1('theFit',FitFunction(),rangeMin,rangeMax,numberOfParameters) theFit.SetParLimits(0,1,20) theFit.SetParLimits(1,20,40) theFit.SetParLimits(2,10,25) theFit.SetParLimits(3,1,10) theFit.SetParLimits(4,0,150) theFit.SetParLimits(5,10,100) theFit.SetParLimits(6,0.0001,1) theFit.SetParLimits(7,0.001,0.05) if self.label == "Baseline": rangeMax = 240 if self.label == "7080": theFit.SetParLimits(5,10,100) # if self.label == "100120": # theFit.SetParLimits(0,1,20) # theFit.SetParLimits(2,1,25) # theFit.SetParLimits(3,0.1,20) if self.label == "120150": theFit.SetParLimits(0,1,20) theFit.SetParLimits(3,0.1,5) gStyle.SetOptFit(0) plot = plots.PlotBase() plot.histoMgr.appendHisto(histograms.Histo(histo,histo.GetName())) plot.createFrame("qcdfit"+self.label, opts={"ymin": 1e-5, "ymaxfactor": 2.}) histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(x=None, y=None, lumi=self.lumi) self.normInvQCD = histo.Integral(0,histo.GetNbinsX()) histo.Scale(1/self.normInvQCD) histo.Fit(theFit,"LR") theFit.SetRange(histo.GetXaxis().GetXmin(),histo.GetXaxis().GetXmax()) theFit.SetLineStyle(2) theFit.Draw("same") histograms.addText(0.4,0.8,"Inverted TauID") plot.histoMgr.appendHisto(histograms.Histo(theFit,"Fit")) plot.getPad().SetLogy(True) plot.draw() plot.save() self.parInvQCD = theFit.GetParameters() fitPars = "fit parameters " i = 0 while i < numberOfParameters: fitPars = fitPars + " " + str(self.parInvQCD[i]) i = i + 1 print fitPars self.nFitInvQCD = theFit.Integral(0,1000,self.parInvQCD) print "Integral ",self.normInvQCD*self.nFitInvQCD
def cutefficiency(self,histo1,histo2): h1 = histo1.Clone("h1") h2 = histo2.Clone("h2") h1.Scale(1/h1.GetMaximum()) h2.Scale(1/h2.GetMaximum()) # check that no bin has negative value, negative values possible after subtracting EWK from data iBin = 1 nBins = h1.GetNbinsX() while iBin < nBins: value1 = h1.GetBinContent(iBin) value2 = h2.GetBinContent(iBin) if value1 < 0: h1.SetBinContent(iBin,0) if value2 < 0: h2.SetBinContent(iBin,0) iBin = iBin + 1 h1cut = h1.Clone("h1cut") h1cut.Reset() h1cut.GetYaxis().SetTitle("Efficiency") h1cut.GetXaxis().SetTitle("PF MET cut (GeV)") h2cut = h2.Clone("h2cut") h2cut.Reset() h2cut.SetLineColor(2) integralError = ROOT.Double(0.0) integralValue = h1.IntegralAndError(1,h1cut.GetNbinsX(),integralError) h1_integral = h1.Integral(0,h1.GetNbinsX()) h2_integral = h2.Integral(0,h2.GetNbinsX()) iBin = 1 nBins = h1cut.GetNbinsX() while iBin < nBins: error = ROOT.Double(0.0) selected1 = h1.IntegralAndError(iBin,nBins,error) if selected1 > 0: error = error/selected1 else: error = integralError/integralValue efficiency1 = selected1/h1_integral h1cut.SetBinContent(iBin,efficiency1) if self.errorBars: h1cut.SetBinError(iBin,error) error = ROOT.Double(0.0) selected2 = h2.IntegralAndError(iBin,nBins,error) if selected2 > 0: error = error/selected2 else: error = integralError/integralValue efficiency2 = selected2/h2_integral h2cut.SetBinContent(iBin,efficiency2) if self.errorBars: h2cut.SetBinError(iBin,error) iBin = iBin + 1 plot = plots.ComparisonPlot( histograms.Histo(h1cut, "Inv"), histograms.Histo(h2cut, "Base"), ) # Set the styles st1 = styles.getDataStyle().clone() st2 = st1.clone() st2.append(styles.StyleLine(lineColor=ROOT.kRed)) st2.append(styles.StyleMarker(markerColor=ROOT.kRed)) plot.histoMgr.forHisto("Base", st1) plot.histoMgr.forHisto("Inv", st2) # Set the legend labels plot.histoMgr.setHistoLegendLabelMany({"Inv": h1.GetTitle(), "Base": h2.GetTitle()}) # Set the legend styles #plot.histoMgr.setHistoLegendStyleAll("L") plot.histoMgr.setHistoLegendStyleAll("P") # Set the drawing styles #plot.histoMgr.setHistoDrawStyleAll("HIST") plot.histoMgr.setHistoDrawStyleAll("EP") # Create frame with a ratio pad plot.createFrame("cuteff"+self.label, opts={"ymin":1e-5, "ymaxfactor": 2}, createRatio=True, opts2={"ymin": 0, "ymax": 2}, # bounds of the ratio plot ) # Set Y axis of the upper pad to logarithmic plot.getPad().SetLogy(True) plot.setLegend(histograms.createLegend(0.4,0.82,0.9,0.93)) histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(x=None, y=None, lumi=self.lumi) plot.draw() plot.save() ###### hError = h1cut.Clone("hError") hError.Divide(h2cut) iBin = 1 nBins = hError.GetNbinsX() while iBin < nBins: hError.SetBinContent(iBin,abs(hError.GetBinContent(iBin) - 1)) iBin = iBin + 1 hError.GetYaxis().SetTitle("abs( (#varepsilon^{Inverted} - #varepsilon^{Baseline})/#varepsilon^{Baseline} )") hError.GetXaxis().SetTitle("PF MET cut (GeV)") plot2 = plots.PlotBase() plot2.histoMgr.appendHisto(histograms.Histo(hError,"ShapeUncertainty")) plot2.histoMgr.forHisto("ShapeUncertainty", st1) plot2.histoMgr.setHistoDrawStyleAll("EP") # plot2.createFrame("shapeUncertainty"+self.label, opts={"ymin":-1, "ymax": 1}) plot2.createFrame("shapeUncertainty"+self.label, opts={"ymin":-0.1, "ymax": 1.1}) histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(x=None, y=None, lumi=self.lumi) rangeMin = hError.GetXaxis().GetXmin() rangeMax = hError.GetXaxis().GetXmax() # rangeMax = 80 rangeMax = 120 # rangeMax = 380 numberOfParameters = 2 class FitFunction: def __call__( self, x, par ): # return Linear(x,par) return ErrorFunction(x,par) theFit = TF1('theFit',FitFunction(),rangeMin,rangeMax,numberOfParameters) theFit.SetParLimits(0,0.01,0.03) theFit.SetParLimits(1,50,150) # theFit.FixParameter(0,0.02) # theFit.FixParameter(1,100) hError.Fit(theFit,"LRN") print "Error MET > 40",theFit.Eval(40) print "Error MET > 50",theFit.Eval(50) print "Error MET > 70",theFit.Eval(70) plot2.histoMgr.appendHisto(histograms.Histo(theFit,"Fit")) plot2.draw() plot2.save()
def comparison(self,histo1,histo2,norm=1): h1 = histo1.Clone("h1") h2 = histo2.Clone("h2") if norm == 1: h1.Scale(1/h1.GetMaximum()) h2.Scale(1/h2.GetMaximum()) # check that no bin has negative value, negative values possible after subtracting EWK from data iBin = 1 nBins = h1.GetNbinsX() while iBin < nBins: value1 = h1.GetBinContent(iBin) value2 = h2.GetBinContent(iBin) if value1 < 0: h1.SetBinContent(iBin,0) if value2 < 0: h2.SetBinContent(iBin,0) iBin = iBin + 1 if norm > 0: h1.GetYaxis().SetTitle("Arbitrary units") plot = plots.ComparisonPlot( histograms.Histo(h1, "Inv"), histograms.Histo(h2, "Base"), ) # Set the styles st1 = styles.getDataStyle().clone() st2 = st1.clone() st2.append(styles.StyleMarker(markerColor=ROOT.kRed)) plot.histoMgr.forHisto("Base", st1) plot.histoMgr.forHisto("Inv", st2) # Set the legend labels plot.histoMgr.setHistoLegendLabelMany({"Inv": h1.GetTitle(), "Base": h2.GetTitle()}) # Set the legend styles plot.histoMgr.setHistoLegendStyleAll("P") # Set the drawing styles plot.histoMgr.setHistoDrawStyleAll("EP") # Create frame with a ratio pad plot.createFrame("comparison"+self.label, opts={"ymin":1e-5, "ymaxfactor": 2}, createRatio=True, opts2={"ymin": 0, "ymax": 2}, # bounds of the ratio plot ) # Set Y axis of the upper pad to logarithmic plot.getPad1().SetLogy(True) plot.setLegend(histograms.createLegend(0.4,0.82,0.9,0.93)) histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(x=None, y=None, lumi=self.lumi) plot.draw() plot.save()
def fitEWK(self, histo, options="R"): rangeMin = histo.GetXaxis().GetXmin() rangeMax = histo.GetXaxis().GetXmax() # rangeMin = 120 # rangeMax = 120 numberOfParameters = 4 print "Fit range ", rangeMin, " - ", rangeMax class FitFunction: def __call__(self, x, par): return EWKFunction(x, par, 1, 1) # return SumFunction(x,par) # return TestFunction(x,par,1) class PlotFunction: def __call__(self, x, par): return EWKFunction(x, par, 0) theFit = TF1('theFit', FitFunction(), rangeMin, rangeMax, numberOfParameters) thePlot = TF1('thePlot', PlotFunction(), rangeMin, rangeMax, numberOfParameters) theFit.SetParLimits(0, 5, 30) theFit.SetParLimits(1, 90, 120) theFit.SetParLimits(2, 30, 50) theFit.SetParLimits(3, 0.001, 1) if self.label == "4050": theFit.SetParLimits(0, 5, 20) theFit.SetParLimits(1, 90, 120) theFit.SetParLimits(2, 30, 50) theFit.SetParLimits(3, 0.001, 1) if self.label == "5060": theFit.SetParLimits(0, 5, 20) theFit.SetParLimits(1, 90, 120) theFit.SetParLimits(2, 20, 50) theFit.SetParLimits(3, 0.001, 1) if self.label == "6070": theFit.SetParLimits(0, 5, 50) theFit.SetParLimits(1, 90, 150) theFit.SetParLimits(2, 20, 50) theFit.SetParLimits(3, 0.001, 1) if self.label == "7080": theFit.SetParLimits(0, 5, 60) theFit.SetParLimits(1, 90, 200) theFit.SetParLimits(2, 20, 100) theFit.SetParLimits(3, 0.001, 1) if self.label == "80100": theFit.SetParLimits(0, 5, 50) theFit.SetParLimits(1, 50, 170) theFit.SetParLimits(2, 20, 60) theFit.SetParLimits(3, 0.001, 1) if self.label == "100120": theFit.SetParLimits(0, 5, 50) theFit.SetParLimits(1, 90, 170) theFit.SetParLimits(2, 20, 60) theFit.SetParLimits(3, 0.001, 1) if self.label == "120150": theFit.SetParLimits(0, 5, 50) theFit.SetParLimits(1, 60, 170) theFit.SetParLimits(2, 10, 100) theFit.SetParLimits(3, 0.001, 1) if self.label == "150": theFit.SetParLimits(0, 5, 50) theFit.SetParLimits(1, 70, 170) theFit.SetParLimits(2, 20, 100) theFit.SetParLimits(3, 0.001, 1) plot = plots.PlotBase() plot.histoMgr.appendHisto(histograms.Histo(histo, histo.GetName())) plot.createFrame("ewkfit" + self.label, opts={ "ymin": 1e-5, "ymaxfactor": 2. }) histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(x=None, y=None, lumi=self.lumi) self.normEWK = histo.Integral(0, histo.GetNbinsX()) histo.Scale(1 / self.normEWK) histo.Fit(theFit, options) theFit.SetRange(histo.GetXaxis().GetXmin(), histo.GetXaxis().GetXmax()) theFit.SetLineStyle(2) theFit.Draw("same") self.parMCEWK = theFit.GetParameters() fitPars = "fit parameters " i = 0 while i < numberOfParameters: fitPars = fitPars + " " + str(self.parMCEWK[i]) thePlot.SetParameter(i, theFit.GetParameter(i)) i = i + 1 thePlot.Draw("same") histograms.addText(0.2, 0.2, "EWK MC, baseline TauID") plot.histoMgr.appendHisto(histograms.Histo(theFit, "Fit")) plot.getPad().SetLogy(True) plot.draw() plot.save() self.parMCEWK = theFit.GetParameters() print fitPars self.nMCEWK = theFit.Integral(0, 1000, self.parMCEWK) print "Integral ", self.normEWK * self.nMCEWK
def fitData(self, histo): parInvQCD = self.parInvQCD parMCEWK = self.parMCEWK nInvQCD = self.nInvQCD nFitInvQCD = self.nFitInvQCD nMCEWK = self.nMCEWK class FitFunction: def __call__(self, x, par): return par[0] * ( par[1] * QCDFunction(x, parInvQCD, 1 / nFitInvQCD) + (1 - par[1]) * EWKFunction(x, parMCEWK, 1 / nMCEWK)) class QCDOnly: def __call__(self, x, par): return par[0] * par[1] * QCDFunction(x, parInvQCD, 1 / nFitInvQCD) rangeMin = histo.GetXaxis().GetXmin() rangeMax = histo.GetXaxis().GetXmax() numberOfParameters = 2 print "Fit range ", rangeMin, " - ", rangeMax theFit = TF1("theFit", FitFunction(), rangeMin, rangeMax, numberOfParameters) plot = plots.PlotBase() plot.histoMgr.appendHisto(histograms.Histo(histo, histo.GetName())) plot.createFrame("combinedfit" + self.label, opts={ "ymin": 1e-5, "ymaxfactor": 2. }) histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(x=None, y=None, lumi=self.lumi) print "data events ", histo.Integral(0, histo.GetNbinsX()) histo.Fit(theFit, "R") theFit.SetRange(histo.GetXaxis().GetXmin(), histo.GetXaxis().GetXmax()) theFit.SetLineStyle(2) theFit.Draw("same") par = theFit.GetParameters() qcdOnly = TF1("qcdOnly", QCDOnly(), rangeMin, rangeMax, numberOfParameters) qcdOnly.FixParameter(0, par[0]) qcdOnly.FixParameter(1, par[1]) qcdOnly.SetLineStyle(2) qcdOnly.Draw("same") histograms.addText(0.35, 0.8, "Data, Baseline TauID") histograms.addText(0.4, 0.3, "QCD", 15) plot.histoMgr.appendHisto(histograms.Histo(qcdOnly, "qcdOnly")) plot.getPad().SetLogy(True) plot.draw() plot.save() fitPars = "fit parameters " i = 0 while i < numberOfParameters: fitPars = fitPars + " " + str(par[i]) i = i + 1 print fitPars nBaseQCD = par[0] self.QCDfraction = par[1] if len(self.label) > 0: print "Bin ", self.label print "Integral ", nBaseQCD print "QCD fraction ", self.QCDfraction return theFit
def Save(self, name): if len(self.histogramsNotFound) > 0: for name in self.histogramsNotFound: print "Histo",name,"not found" self.histogramsNotFound = [] return hObserved = self.histograms[self.FindHistoIndex(self.dataHistos[0].name)].Clone("Data") hObserved.Reset() for histo in self.dataHistos: hObserved.Add(self.histograms[self.FindHistoIndex(histo.label)]) print " Data: ", hObserved.Integral(0,hObserved.GetNbinsX()),"events" hEstimatedEWKfake = self.histograms[self.FindHistoIndex("EWKfake")].Clone("hEstimatedEWKfake") print " EWKfake: ",hEstimatedEWKfake.Integral(0,hEstimatedEWKfake.GetNbinsX()),"events" hEstimatedEWK = self.histograms[self.FindHistoIndex("EWK")].Clone("hEstimatedEWK") print " EWK: ",hEstimatedEWK.Integral(0,hEstimatedEWK.GetNbinsX()),"events" hEstimatedEWK.Add(hEstimatedEWKfake) hEstimatedQCD = self.histograms[self.FindHistoIndex("QCD")].Clone("hEstimatedQCD") print " QCD: ",hEstimatedQCD.Integral(0,hEstimatedQCD.GetNbinsX()),"events" hEstimatedQCD.Add(hEstimatedEWK) hUncertainty = hEstimatedQCD.Clone("BackgrUncertainty") hUncertainty.SetFillColor(1) hUncertainty.SetFillStyle(3354) hUncertainty.SetLineColor(0) hUncertainty.SetLineStyle(0) hUncertainty.SetLineWidth(0) hUncertainty.SetMarkerColor(0) hUncertainty.SetMarkerSize(0) hSignal = self.histograms[self.FindHistoIndex("Signal")].Clone("hSignal") print " Signal: ",hSignal.Integral(0,hSignal.GetNbinsX()),"events" hSignal.Add(hEstimatedQCD) style = tdrstyle.TDRStyle() plot = plots.ComparisonManyPlot( histograms.Histo(hObserved, "Data"), [ histograms.Histo(hUncertainty, "Backgr.Uncertainty"), histograms.Histo(hEstimatedEWKfake, "EWKfake"), histograms.Histo(hEstimatedEWK, "EWK"), histograms.Histo(hEstimatedQCD, "QCD"), histograms.Histo(hSignal, "Signal") ] ) plot.histoMgr.forHisto("Data", styles.getDataStyle()) plot.histoMgr.forHisto("EWK",styles.getEWKStyle()) plot.histoMgr.forHisto("EWKfake",styles.getEWKFakeStyle()) plot.histoMgr.forHisto("QCD",styles.getQCDStyle()) plot.histoMgr.forHisto("Signal",styles.getSignalStyle()) plot.histoMgr.forHisto("Backgr.Uncertainty",styles.getErrorStyle()) plot.histoMgr.setHistoDrawStyleAll("HIST") plot.histoMgr.setHistoDrawStyle("Data", "EP") plot.histoMgr.setHistoDrawStyle("Backgr.Uncertainty", "E2") plot.histoMgr.setHistoLegendStyleAll("F") plot.histoMgr.setHistoLegendStyle("Data", "P") plot.histoMgr.setHistoLegendStyle("Signal", "L") plot.createFrame(name, opts={"ymin":0, "ymaxfactor": 1.2}) plot.frame.GetXaxis().SetTitle(self.xtitle) plot.frame.GetYaxis().SetTitle(self.ytitle) plot.histoMgr.reorderLegend(["Data", "Signal", "QCD", "EWK", "EWKfake", "Backgr.Uncertainty"]) self.setLegendLabels(plot) plot.setLegend(histograms.createLegend(0.55,0.68,0.9,0.93)) histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(x=None, y=None, lumi=self.lumi) plot.draw() plot.save()
def Save(self, name): hObserved = self.CreateGraph("Observed") hExpected = self.CreateGraph("Expected") hExpected1s = self.CreateGraph("Expected1") hExpected1s.SetFillColor(ROOT.kYellow) hExpected2s = self.CreateGraph("Expected2") hExpected2s.SetFillColor(ROOT.kOrange) style = tdrstyle.TDRStyle() plot = plots.ComparisonManyPlot( histograms.HistoGraph(hObserved, "Observed"), [ histograms.HistoGraph(hExpected, "Expected"), histograms.HistoGraph(hExpected1s, "Expected1"), histograms.HistoGraph(hExpected2s, "Expected2") ]) obsStyle = styles.getDataStyle().clone() plot.histoMgr.forHisto("Observed", styles.getDataStyle().clone()) plot.histoMgr.setHistoDrawStyle("Observed", "PL") expStyle = styles.getDataStyle().clone() expStyle.append(styles.StyleLine(lineStyle=2)) expStyle.append(styles.StyleLine(lineColor=ROOT.kRed)) expStyle.append(styles.StyleMarker(markerStyle=ROOT.kFullSquare)) expStyle.append(styles.StyleMarker(markerColor=ROOT.kRed)) plot.histoMgr.forHisto("Expected", expStyle) plot.histoMgr.setHistoDrawStyle("Expected", "PL") plot.histoMgr.setHistoDrawStyle("Expected1", "PL3") plot.histoMgr.setHistoDrawStyle("Expected2", "PL3") plot.createFrame(name, opts={ "xmin": 70.1, "xmax": 169.9, "ymin": 0, "ymax": 0.2 }) plot.frame.GetXaxis().SetTitle(self.xtitle) plot.frame.GetYaxis().SetTitle(self.ytitle) plot.histoMgr.setHistoLegendStyle("Observed", "PL") plot.histoMgr.setHistoLegendStyle("Expected", "PL") plot.histoMgr.setHistoLegendStyle("Expected1", "F") plot.histoMgr.setHistoLegendStyle("Expected2", "F") plot.histoMgr.setHistoLegendLabelMany({ "Expected": "Expected median", "Expected1": "Expected median #pm1#sigma", "Expected2": "Expected median #pm2#sigma" }) plot.setLegend(histograms.createLegend(0.55, 0.68, 0.9, 0.93)) histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(x=None, y=None, lumi=self.lumi) textSize = 16 textX = 0.19 textY = 0.8 textDY = 0.038 histograms.addText(textX, textY + 2 * textDY, "t#rightarrowbH^{#pm}, H^{#pm}#rightarrow#tau#nu", textSize) histograms.addText(textX, textY + textDY, "Fully hadronic final state", textSize) histograms.addText(textX, textY, "BR(H^{#pm}#rightarrow#tau^{#pm} #nu) = 1", textSize) plot.draw() plot.save()
def Save(self, name): if len(self.histogramsNotFound) > 0: for name in self.histogramsNotFound: print "Histo", name, "not found" self.histogramsNotFound = [] return hObserved = self.histograms[self.FindHistoIndex( self.dataHistos[0].name)].Clone("Data") hObserved.Reset() for histo in self.dataHistos: hObserved.Add(self.histograms[self.FindHistoIndex(histo.label)]) print " Data: ", hObserved.Integral( 0, hObserved.GetNbinsX()), "events" hEstimatedEWKfake = self.histograms[self.FindHistoIndex( "EWKfake")].Clone("hEstimatedEWKfake") print " EWKfake: ", hEstimatedEWKfake.Integral( 0, hEstimatedEWKfake.GetNbinsX()), "events" hEstimatedEWK = self.histograms[self.FindHistoIndex("EWK")].Clone( "hEstimatedEWK") print " EWK: ", hEstimatedEWK.Integral( 0, hEstimatedEWK.GetNbinsX()), "events" hEstimatedEWK.Add(hEstimatedEWKfake) hEstimatedQCD = self.histograms[self.FindHistoIndex("QCD")].Clone( "hEstimatedQCD") print " QCD: ", hEstimatedQCD.Integral( 0, hEstimatedQCD.GetNbinsX()), "events" hEstimatedQCD.Add(hEstimatedEWK) hUncertainty = hEstimatedQCD.Clone("BackgrUncertainty") hUncertainty.SetFillColor(1) hUncertainty.SetFillStyle(3354) hUncertainty.SetLineColor(0) hUncertainty.SetLineStyle(0) hUncertainty.SetLineWidth(0) hUncertainty.SetMarkerColor(0) hUncertainty.SetMarkerSize(0) hSignal = self.histograms[self.FindHistoIndex("Signal")].Clone( "hSignal") print " Signal: ", hSignal.Integral(0, hSignal.GetNbinsX()), "events" hSignal.Add(hEstimatedQCD) style = tdrstyle.TDRStyle() plot = plots.ComparisonManyPlot(histograms.Histo(hObserved, "Data"), [ histograms.Histo(hUncertainty, "Backgr.Uncertainty"), histograms.Histo(hEstimatedEWKfake, "EWKfake"), histograms.Histo(hEstimatedEWK, "EWK"), histograms.Histo(hEstimatedQCD, "QCD"), histograms.Histo(hSignal, "Signal") ]) plot.histoMgr.forHisto("Data", styles.getDataStyle()) plot.histoMgr.forHisto("EWK", styles.getEWKStyle()) plot.histoMgr.forHisto("EWKfake", styles.getEWKFakeStyle()) plot.histoMgr.forHisto("QCD", styles.getQCDStyle()) plot.histoMgr.forHisto("Signal", styles.getSignalStyle()) plot.histoMgr.forHisto("Backgr.Uncertainty", styles.getErrorStyle()) plot.histoMgr.setHistoDrawStyleAll("HIST") plot.histoMgr.setHistoDrawStyle("Data", "EP") plot.histoMgr.setHistoDrawStyle("Backgr.Uncertainty", "E2") plot.histoMgr.setHistoLegendStyleAll("F") plot.histoMgr.setHistoLegendStyle("Data", "P") plot.histoMgr.setHistoLegendStyle("Signal", "L") plot.createFrame(name, opts={"ymin": 0, "ymaxfactor": 1.2}) plot.frame.GetXaxis().SetTitle(self.xtitle) plot.frame.GetYaxis().SetTitle(self.ytitle) plot.histoMgr.reorderLegend( ["Data", "Signal", "QCD", "EWK", "EWKfake", "Backgr.Uncertainty"]) self.setLegendLabels(plot) plot.setLegend(histograms.createLegend(0.55, 0.68, 0.9, 0.93)) histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(x=None, y=None, lumi=self.lumi) plot.draw() plot.save()
def Save(self,name): hObserved = self.CreateGraph("Observed") hExpected = self.CreateGraph("Expected") hExpected1s = self.CreateGraph("Expected1") hExpected1s.SetFillColor(ROOT.kYellow) hExpected2s = self.CreateGraph("Expected2") hExpected2s.SetFillColor(ROOT.kOrange) style = tdrstyle.TDRStyle() plot = plots.ComparisonManyPlot( histograms.HistoGraph(hObserved, "Observed"), [ histograms.HistoGraph(hExpected, "Expected"), histograms.HistoGraph(hExpected1s, "Expected1"), histograms.HistoGraph(hExpected2s, "Expected2") ] ) obsStyle = styles.getDataStyle().clone() plot.histoMgr.forHisto("Observed", styles.getDataStyle().clone()) plot.histoMgr.setHistoDrawStyle("Observed", "PL") expStyle = styles.getDataStyle().clone() expStyle.append(styles.StyleLine(lineStyle=2)) expStyle.append(styles.StyleLine(lineColor=ROOT.kRed)) expStyle.append(styles.StyleMarker(markerStyle=ROOT.kFullSquare)) expStyle.append(styles.StyleMarker(markerColor=ROOT.kRed)) plot.histoMgr.forHisto("Expected", expStyle) plot.histoMgr.setHistoDrawStyle("Expected", "PL") plot.histoMgr.setHistoDrawStyle("Expected1", "PL3") plot.histoMgr.setHistoDrawStyle("Expected2", "PL3") plot.createFrame(name, opts={"xmin": 70.1, "xmax": 169.9, "ymin":0, "ymax": 0.2}) plot.frame.GetXaxis().SetTitle(self.xtitle) plot.frame.GetYaxis().SetTitle(self.ytitle) plot.histoMgr.setHistoLegendStyle("Observed", "PL") plot.histoMgr.setHistoLegendStyle("Expected", "PL") plot.histoMgr.setHistoLegendStyle("Expected1", "F") plot.histoMgr.setHistoLegendStyle("Expected2", "F") plot.histoMgr.setHistoLegendLabelMany({ "Expected": "Expected median", "Expected1": "Expected median #pm1#sigma", "Expected2": "Expected median #pm2#sigma" }) plot.setLegend(histograms.createLegend(0.55,0.68,0.9,0.93)) histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText(x=None, y=None, lumi=self.lumi) textSize = 16 textX = 0.19 textY = 0.8 textDY = 0.038 histograms.addText(textX,textY+2*textDY,"t#rightarrowbH^{#pm}, H^{#pm}#rightarrow#tau#nu",textSize) histograms.addText(textX,textY+textDY,"Fully hadronic final state",textSize) histograms.addText(textX,textY,"BR(H^{#pm}#rightarrow#tau^{#pm} #nu) = 1",textSize) plot.draw() plot.save()
def distComparison(datasets): # Create a comparison plot of two distributions (must have the same binning) # Set the names of DatasetRootHisto objects in order to be able easily reference them later drh1 = datasets.getDataset("Data").getDatasetRootHisto( analysis + "/MET_BaseLineTauId") drh1.setName("Base") drh1.normalizeToOne() drh2 = datasets.getDataset("Data").getDatasetRootHisto( analysis + "/MET_InvertedTauId") drh2.setName("Inv") drh2.normalizeToOne() plot = plots.ComparisonPlot(drh1, drh2) # Set the styles st1 = styles.getDataStyle().clone() st2 = st1.clone() st2.append(styles.StyleLine(lineColor=ROOT.kRed)) plot.histoMgr.forHisto("Base", st1) plot.histoMgr.forHisto("Inv", st2) # Set the legend labels plot.histoMgr.setHistoLegendLabelMany({ "Base": "Baseline Tau ID", "Inv": "Inverted Tau ID" }) # Set the legend styles plot.histoMgr.setHistoLegendStyleAll("L") plot.histoMgr.setHistoLegendStyle("Base", "P") # exception to the general rule # Set the drawing styles plot.histoMgr.setHistoDrawStyleAll("HIST") plot.histoMgr.setHistoDrawStyle("Base", "EP") # exception to the general rule # Rebin, if necessary plot.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(10)) # Create frame with a ratio pad plot.createFrame( "METbaseVSinvertedTauID", opts={ "ymin": 1e-5, "ymaxfactor": 1.5 }, createRatio=True, opts2={ "ymin": -10, "ymax": 50 }, # bounds of the ratio plot ) # Set Y axis of the upper pad to logarithmic plot.getPad1().SetLogy(True) # Create legend to the default position plot.setLegend(histograms.createLegend()) # Set the X/Y axis labels plot.frame.GetXaxis().SetTitle("MET (GeV)") plot.frame.GetYaxis().SetTitle("Arbitrary units") # Draw the plot plot.draw() # Add the various texts to histograms.addCmsPreliminaryText() histograms.addEnergyText() histograms.addLuminosityText( x=None, y=None, lumi=datasets.getDataset("Data").getLuminosity()) # Save the plot to files plot.save()