def makePlot(trees, cut, variable, combination, name, title=None, datasetName="Data"): from ROOT import TCanvas, TPad, TLegend, kBlue, kRed, TLatex from src.ratios import RatioGraph from src.histos import getHisto from math import sqrt from random import random if title is None: title = name cut = cut.replace("inv", "p4.M()") if "iso" in variable: cut = cut.replace("id1 < 0.15", "id1 < 10") cut = cut.replace("id2 < 0.15", "id2 < 10") print cut baseColor = kBlue if "jzb" in name: baseColor = kRed rmue = 1.21 trigger = {"EE": 0.970, "EMu": 0.942, "MuMu": 0.964} histos = { "MuMu": getHisto(trees["MuMu"], cut, variable), "EE": getHisto(trees["EE"], cut, variable), "EMu": getHisto(trees["EMu"], cut, variable), "EMu_MuLead": getHisto(trees["EMu"], "(%s) * (pt2 > pt1)" % cut, variable), } rmueSys = 0 predictionSrc = "EMu" if combination == "Both": sameFlavour = histos["MuMu"].Clone("sameFlavourSum") sameFlavour.Add(histos["EE"]) #~ nllPredictionScale = 0.5* sqrt(trigger["EE"]*trigger["MuMu"])*1./trigger["EMu"] *(rmue+1./(rmue)) #~ rmueSys = sqrt(sum(i**2 for i in [0.5*(1-(1./rmue**2))*0.1 + (1./trigger["EMu"] + 0.5*(1./trigger["EE"]+1./trigger["MuMu"]))*0.05])) nllPredictionScale = 1.02 rmueSys = 0.07 #0.5*(1-1./rmue**2)*0.1 elif combination in ["EE", "MuMu"]: rmueFactor = rmue if combination == "EE": rmueFactor = 1. / rmue nllPredictionScale = 0.5 * sqrt( trigger["EE"] * trigger["MuMu"]) * 1. / trigger["EMu"] * rmueFactor rmueSys = sqrt(0.1**2 + (0.67 / 1.025)**2) * nllPredictionScale sameFlavour = histos[combination].Clone("sameFlavourSum") elif combination == "MuMuSF": predictionSrc = "MuMu" rmueFactor = 1. / rmue nllPredictionScale = trigger["EE"] * 1. / trigger["MuMu"] * (rmueFactor **2) rmueSys = (2 * rmue) * 0.1 sameFlavour = histos["EE"].Clone("sameFlavourSum") else: raise StandardError, "unknown combination' %s'" % combination prediction = histos[predictionSrc].Clone("ofPrediction") predictionMuLead = histos["EMu_MuLead"].Clone("ofPrediction_MuLeading") prediction.Scale(nllPredictionScale) predictionMuLead.Scale(nllPredictionScale) rooTex = { "Both": "ee + #mu#mu", "EE": "ee", "MuMu": "#mu#mu", "MuMuSF": "ee", "EMu_Prediction": "OF-Prediciton", "MuMu_Prediction": "SF(#mu#mu)-Prediciton" } canv = TCanvas("canv", "", 800, 800) canv.Draw() pad = TPad("main_%x" % (1e16 * random()), "main", 0.01, 0.25, 0.99, 0.99) pad.SetNumber(1) pad.Draw() canv.cd() resPad = TPad("residual_%x" % (1e16 * random()), "residual", 0.01, 0.01, 0.99, 0.25) resPad.SetNumber(2) resPad.Draw() pad.cd() #~ if "iso" in variable: #~ canv.SetLogy(1) #~ pad.SetLogy(1) sameFlavour.SetMarkerStyle(20) sameFlavour.SetLineColor(1) sameFlavour.Draw("E P") sameFlavour.SetTitle(title) import ROOT histos["MuMu"].SetLineColor(ROOT.kBlack) histos["MuMu"].SetFillColor(ROOT.kWhite) prediction.SetLineColor(1) prediction.SetFillColor(baseColor - 7) prediction.Draw("SAME Hist") if predictionSrc == "EMu": predictionMuLead.SetLineColor(1) predictionMuLead.SetFillColor(baseColor - 9) predictionMuLead.Draw("SAME Hist") sameFlavour.Draw("E P SAME") histos["MuMu"].Draw("SAME Hist") pad.RedrawAxis() canv.RedrawAxis() leg = TLegend(0.75, 0.65, 1, 0.9) leg.SetFillColor(0) leg.AddEntry(sameFlavour, rooTex[combination], "P") leg.AddEntry(histos["MuMu"], "#mu#mu events", "l") leg.AddEntry(prediction, rooTex[predictionSrc + "_Prediction"], "F") if predictionSrc == "EMu": leg.AddEntry(predictionMuLead, "#mu leading", "F") leg.Draw() # print "_".join([name,variable]), # print "EE",histos["EE"].GetEntries(), "MuMu:", histos["MuMu"].GetEntries(), "EMu:", histos["EMu"].GetEntries(), # print " SF:", sameFlavour.GetEntries(), "OF:", prediction.GetEntries() # tex = TLatex() # tex.SetNDC() # tex.SetTextSize() # tex.DrawLatex(0.6, 0.7, title) residuals = RatioGraph(sameFlavour, prediction) residuals.addErrorBySize(rmueSys, rmueSys, add=False, color=kBlue - 9) residuals.draw(resPad, yMin=0.5, yMax=1.5) canv.Update() variable = variable.replace("(", "").replace(")", "") plotPath = "fig/%s.pdf" % ("_".join( [datasetName, name, combination, variable])) canv.Print(plotPath) pad.Delete() resPad.Delete() canv.Delete() # raw_input() return plotPath