def rdhToGKS(ws, nbins, jetbin, uname, postfix=""): """ extract RooDataHist from ws and create the Gaussian Kernel Smoother """ dhs = ROOT.TList() gkss = ROOT.TList() rdhs_gks = ROOT.TList() pdfs_gks = ROOT.TList() cdfs_gks = ROOT.TList() assert uname in ["u1", "u2"], "uname must be either u1 or u2" for ibin in xrange(nbins): varname = "u_{}_{}_pt{}".format(uname, jetbin, ibin) var = ws.var(varname) rdhname = "datahist_{}_{}_pt{}".format(uname, jetbin, ibin) rdh = ws.data(rdhname) dh = rdh.createHistogram( "dh_{}_{}_pt{}_{}".format(uname, jetbin, ibin, postfix), var) # apply Gaussian Kernel Smoother smo = Smoother() smo.histo = dh smo.computeSmoothHisto() gks = smo.smoothHisto #cdf = gks.GetCumulative() rdh_gks, pdf_gks, cdf = HistToCdfTF(gks, var, postfix) print dh.GetName(), gks.GetName(), cdf.GetName() dhs.Add(dh) gkss.Add(gks) rdhs_gks.Add(rdh_gks) pdfs_gks.Add(pdf_gks) cdfs_gks.Add(cdf) doPlot = True if doPlot: # plot the comparison between orignal and smoothed xtitle = "u_{#parallel} [GeV]" if uname == "u1" else "u_{#perp} [GeV]" DrawHistos([dh, gks], ["Original", "Smoothed"], dh.GetXaxis().GetXmin(), dh.GetXaxis().GetXmax(), xtitle, 0., 1.2 * dh.GetMaximum(), "Events / GeV", "hcomp_{}_{}_pt{}_{}".format(uname, jetbin, ibin, postfix), dology=False, showchi=True, mycolors=[1, 2], drawoptions=["", "HIST"], legendoptions=["PE", "L"], ratiobase=1, doNewman=True, legendPos=[0.88, 0.84, 0.72, 0.74]) return dhs, gkss, rdhs_gks, pdfs_gks, cdfs_gks
'u_GEN_pt') hresponses.update(hresponses_WoPUPPI) hresols_paral_diff.update(hresols_paral_diff_WoPUPPI) hresols_perp.update(hresols_perp_WoPUPPI) hresponsesSc.update(hresponsesSc_WoPUPPI) hresolsSc_paral_diff.update(hresolsSc_paral_diff_WoPUPPI) hresolsSc_perp.update(hresolsSc_perp_WoPUPPI) DrawHistos(hresponses.values(), [labels[itype] for itype in hresponses.keys()], 0, 150., "q_{T} [GeV]", 0., 1.15, "Reponse -<u_{#parallel}>/<q_{T}>", "reco_recoil_response", drawashist=True, dology=False, legendPos=[0.55, 0.17, 0.73, 0.44], mycolors=[colors[itype] for itype in hresponses.keys()], linestyles=[1, 1, 1, 1, 2]) DrawHistos(hresols_paral_diff.values(), [labels[itype] for itype in hresols_paral_diff.keys()], 0, 150., "q_{T} [GeV]", 0, 39.0, "#sigma u_{#parallel} [GeV]",
rdf = rdf.Define("{RECOIL}_scale".format(RECOIL=itype), '1.0/get_response(u_GEN_pt, hprof_{RECOIL})'.format(RECOIL=itype)) \ .Define("u_{RECOIL}Sc_x".format(RECOIL=itype), "{RECOIL}_scale * u_{RECOIL}_x".format(RECOIL=itype)) \ .Define("u_{RECOIL}Sc_y".format(RECOIL=itype), "{RECOIL}_scale * u_{RECOIL}_y".format(RECOIL=itype)) recoilsSc = [itype + "Sc" for itype in recoils] recoilanalyzerSc = RecoilAnalyzer(rdf, recoilsSc) recoilanalyzerSc.prepareVars() recoilanalyzerSc.prepareResponses( 'u_GEN_pt', xbins_qT) recoilanalyzerSc.prepareResolutions( 'u_GEN_pt', xbins_qT, 400, -200, 200) recoilanalyzerSc.prepareResolutions1D(400, -200, 200) hresponsesSc = recoilanalyzerSc.getResponses('u_GEN_pt') hresolsSc_paral_diff, hresolsSc_perp = recoilanalyzerSc.getResolutions('u_GEN_pt') vresolsSc_paral_diff, vresolsSc_perp = recoilanalyzerSc.getResolutions1D() DrawHistos(hresponses.values(), [labels[itype] for itype in hresponses.keys()], 0, 60., "q_{T} [GeV]", 0., 1.15, "Reponse -<u_{#parallel}>/<q_{T}>", "recoil_response", drawashist=True, dology=False, legendPos=[0.22, 0.17, 0.90, 0.36], legendNCols=2, mycolors=[colors[itype] for itype in hresponses.keys()], linestyles=[linestyles[itype] for itype in hresponses.keys()]) DrawHistos(hresols_paral_diff.values(), [labels[itype] for itype in hresols_paral_diff.keys()], 0, 60., "q_{T} [GeV]", 0, 30.0, "#sigma u_{#parallel} [GeV]", "recoil_resol_paral", drawashist=True, dology=False, legendPos=[0.20, 0.73, 0.88, 0.92], legendNCols=2, mycolors=[colors[itype] for itype in hresols_paral_diff.keys()], linestyles=[linestyles[itype] for itype in hresols_paral_diff.keys()]) DrawHistos(hresols_perp.values(), [labels[itype] for itype in hresols_perp.keys()], 0, 60., "q_{T} [GeV]", 0, 20.0, "#sigma u_{#perp} [GeV]", "recoil_resol_perp", drawashist=True, dology=False, legendPos=[0.20, 0.73, 0.88, 0.92], legendNCols=2, mycolors=[colors[itype] for itype in hresols_perp.keys()], linestyles=[linestyles[itype] for itype in hresols_perp.keys()]) ## ## Scaled ## DrawHistos(hresponsesSc.values(), [labels[itype] for itype in hresponses.keys()], 0, 60., "q_{T} [GeV]", 0., 1.15, "Scaled Reponse -<u_{#parallel}>/<q_{T}>", "recoil_response_scaled", drawashist=True, dology=False, legendPos=[0.22, 0.17, 0.90, 0.36], legendNCols=2, mycolors=[colors[itype] for itype in hresponses.keys()], linestyles=[linestyles[itype] for itype in hresponses.keys()]) DrawHistos(hresolsSc_paral_diff.values(), [labels[itype] for itype in hresols_paral_diff.keys()], 0, 60., "q_{T} [GeV]", 0, 50.0, "Scaled #sigma u_{#parallel} [GeV]", "recoil_resol_paral_scaled", drawashist=True, dology=False, legendPos=[0.20, 0.73, 0.88, 0.92], legendNCols=2, mycolors=[colors[itype] for itype in hresols_paral_diff.keys()], linestyles=[linestyles[itype] for itype in hresols_paral_diff.keys()]) DrawHistos(hresolsSc_perp.values(), [labels[itype] for itype in hresols_perp.keys()], 0, 60., "q_{T} [GeV]", 0, 30.0, "Scaled #sigma u_{#perp} [GeV]", "recoil_resol_perp_scaled", drawashist=True, dology=False, legendPos=[0.20, 0.73, 0.88, 0.92], legendNCols=2, mycolors=[colors[itype] for itype in hresols_perp.keys()], linestyles=[linestyles[itype] for itype in hresols_perp.keys()]) for itype in vresols_paral_diff:
def _DrawPlot(self, h_data, h_mcs, drawconfigs, hname): legends = [] h_data.Scale(1.0) h_data.SetLineColor(1) h_data.SetMarkerStyle(20) h_data.SetMarkerSize(1) legends.append("Data") # count the number of events only need to be done once docounting = (len(self.counts)==0) if docounting: self.counts.append(h_data.Integral(0, h_data.GetNbinsX()+1)) hgroupedmcs = OrderedDict() group_to_renormalize = "" for imc in xrange(len(h_mcs)): # scale the MC to the xsec h_mcs[imc].Scale( self.normfactors[imc] ) if self.mcs[imc].renormalizefactor!=1.0: print "renormalize MC {} with a factor or {}".format(self.mcs[imc].name, self.mcs[imc].renormalizefactor) h_mcs[imc].Scale( self.mcs[imc].renormalizefactor ) if docounting: self.counts.append( h_mcs[imc].Integral(0, h_mcs[imc].GetNbinsX()+1) ) groupname = self.mcs[imc].groupname if groupname not in hgroupedmcs: hgroupedmcs[groupname] = h_mcs[imc].Clone(h_mcs[imc].GetName().replace("_mc_","_grouped_"+groupname)) hgroupedmcs[groupname].SetLineColor( self.mcs[imc].groupcolor ) hgroupedmcs[groupname].SetFillColor( self.mcs[imc].groupcolor ) legends.append( self.mcs[imc].grouplegend ) if self.mcs[imc].renormalize: group_to_renormalize = groupname else: # group mc already exist. Add to the histogram hgroupedmcs[groupname].Add( h_mcs[imc].GetValue() ) if group_to_renormalize: ndata = h_data.Integral(0, h_data.GetNbinsX()+1) nmc=0 for gname, ghisto in hgroupedmcs.iteritems(): if gname!=group_to_renormalize: nmc += ghisto.Integral(0, ghisto.GetNbinsX()+1) weight = float(ndata-nmc) / hgroupedmcs[group_to_renormalize].Integral(0, hgroupedmcs[group_to_renormalize].GetNbinsX()+1) print "Renormalize group for {}, with {} data, {} MC and weight {}".format(group_to_renormalize, ndata, nmc, weight) hgroupedmcs[group_to_renormalize].Scale(weight) hsname = "hs_" + drawconfigs.outputname hs_gmc = ROOT.THStack( hsname, hsname) for h_gmc in reversed(hgroupedmcs.values()): if drawconfigs.donormalizebin: # scale to bin width h_gmc.Scale(1.0, "width") hs_gmc.Add( h_gmc ) if not drawconfigs.legends: drawconfigs.legends = legends if drawconfigs.outputname == "test": # change default outputname to the histo name drawconfigs.outputname = hname if drawconfigs.donormalizebin: h_data.Scale(1.0, "width") self.hdatas[ drawconfigs.outputname] = h_data self.hsmcs[ drawconfigs.outputname] = hs_gmc self.hratios[drawconfigs.outputname] = DrawHistos( [h_data, hs_gmc], drawconfigs.legends, drawconfigs.xmin, drawconfigs.xmax, drawconfigs.xlabel, drawconfigs.ymin, drawconfigs.ymax, drawconfigs.ylabel, drawconfigs.outputname, dology=drawconfigs.dology, dologx=drawconfigs.dologx, showratio=drawconfigs.showratio, yrmax = drawconfigs.yrmax, yrmin = drawconfigs.yrmin, yrlabel = drawconfigs.yrlabel, donormalize=drawconfigs.donormalize, ratiobase=drawconfigs.ratiobase, legendPos = drawconfigs.legendPos, redrawihist = drawconfigs.redrawihist, extraText = drawconfigs.extraText, noCMS = drawconfigs.noCMS, addOverflow = drawconfigs.addOverflow, addUnderflow = drawconfigs.addUnderflow, nMaxDigits = drawconfigs.nMaxDigits)
recoilsSc = [itype + "Sc" for itype in recoils] recoilanalyzerSc = RecoilAnalyzer(rdf, recoilsSc) recoilanalyzerSc.prepareVars() recoilanalyzerSc.prepareResponses( 'u_GEN_pt', xbins_qT) recoilanalyzerSc.prepareResolutions( 'u_GEN_pt', xbins_qT, 400, -200, 200) recoilanalyzerSc.prepareResponses( 'PV_npvsGood', xbins_nVtx) recoilanalyzerSc.prepareResolutions( 'PV_npvsGood', xbins_nVtx, 400, -200, 200) hresponsesSc = recoilanalyzerSc.getResponses('u_GEN_pt') hresolsSc_paral_diff, hresolsSc_perp = recoilanalyzerSc.getResolutions('u_GEN_pt') hresponsesSc_nVtx = recoilanalyzerSc.getResponses('PV_npvsGood') hresolsSc_paral_diff_VS_nVtx, hresolsSc_perp_VS_nVtx = recoilanalyzerSc.getResolutions('PV_npvsGood') DrawHistos(hresponses.values(), [labels[itype] for itype in hresponses.keys()], 0, 60., "q_{T} [GeV]", 0., 1.15, "Reponse -<u_{#parallel}>/<q_{T}>", "reco_recoil_response", drawashist=True, dology=False, legendPos=[0.70, 0.17, 0.88, 0.36], mycolors=[colors[itype] for itype in hresponses.keys()]) DrawHistos(hresols_paral_diff.values(), [labels[itype] for itype in hresols_paral_diff.keys()], 0, 60., "q_{T} [GeV]", 0, 39.0, "#sigma u_{#parallel} [GeV]", "reco_recoil_resol_paral", drawashist=True, dology=False, legendPos=[0.20, 0.73, 0.38, 0.92], mycolors=[colors[itype] for itype in hresols_paral_diff.keys()]) DrawHistos(hresols_perp.values(), [labels[itype] for itype in hresols_perp.keys()], 0, 60., "q_{T} [GeV]", 0, 32.0, "#sigma u_{#perp} [GeV]", "reco_recoil_resol_perp", drawashist=True, dology=False, legendPos=[0.20, 0.73, 0.40, 0.92], mycolors=[colors[itype] for itype in hresols_perp.keys()]) DrawHistos(hresponses_nVtx.values(), [labels[itype] for itype in hresponses_nVtx.keys()], 0, 50., "# Vertices", 0., 1.15, "Reponse -<u_{#parallel}>/<q_{T}>", "reco_recoil_response_VS_nVtx", drawashist=True, dology=False, legendPos=[0.70, 0.17, 0.88, 0.36], mycolors=[colors[itype] for itype in hresponses.keys()]) DrawHistos(hresols_paral_diff_VS_nVtx.values(), [labels[itype] for itype in hresols_paral_diff_VS_nVtx.keys()], 0, 50., "# Vertices", 0, 50.0, "#sigma u_{#parallel} [GeV]", "reco_recoil_resol_paral_VS_nVtx", drawashist=True, dology=False, legendPos=[0.20, 0.73, 0.38, 0.92], mycolors=[colors[itype] for itype in hresols_paral_diff_VS_nVtx.keys()]) DrawHistos(hresols_perp_VS_nVtx.values(), [labels[itype] for itype in hresols_perp_VS_nVtx.keys()], 0, 50., "# Vertices", 0, 50.0, "#sigma u_{#perp} [GeV]", "reco_recoil_resol_perp_VS_nVtx", drawashist=True, dology=False, legendPos=[0.20, 0.73, 0.40, 0.92], mycolors=[colors[itype] for itype in hresols_perp_VS_nVtx.keys()]) ## ## Scaled ## DrawHistos(hresponsesSc.values(), [labels[itype] for itype in hresponses.keys()], 0, 60., "q_{T} [GeV]", 0., 1.15, "Scaled Reponse -<u_{#parallel}>/<q_{T}>", "reco_recoil_response_Scaled", drawashist=True, dology=False, legendPos=[0.70, 0.17, 0.88, 0.36], mycolors=[colors[itype] for itype in hresponses.keys()])
def compMCRatios(hnames, colors, linestyles, legends, outputtag, ymin=0.8, ymax=1.2): # compare MC with MC histos_met = [ THStack2TH1(sampMan.hsmcs["h_wjets_deepmet_pt_{}".format(hname)], "MCRatio") for hname in hnames ] histos_u_pt = [ THStack2TH1( sampMan.hsmcs["h_wjets_u_reco_pt_Deep_{}".format(hname)], "MCRatio") for hname in hnames ] histos_mt = [ THStack2TH1(sampMan.hsmcs["h_wjets_mT_Deep_{}".format(hname)], "MCRatio") for hname in hnames ] histos_met_base = histos_met[0].Clone(histos_met[0].GetName() + "_Cloned") histos_u_pt_base = histos_u_pt[0].Clone(histos_u_pt[0].GetName() + "_Cloned") histos_mt_base = histos_mt[0].Clone(histos_mt[0].GetName() + "_Cloned") for hmet, hupt, hmt in zip(histos_met, histos_u_pt, histos_mt): hmet.Divide(histos_met_base) hmet.SetFillColor(0) hupt.Divide(histos_u_pt_base) hupt.SetFillColor(0) hmt.Divide(histos_mt_base) hmt.SetFillColor(0) if len(hnames) > 5: legendNCols = 2 legendPos = [0.20, 0.88, 0.70, 0.74] elif len(hnames) > 8: legendNCols = 3 legendPos = [0.15, 0.99, 0.92, 0.72] else: legendNCols = 1 legendPos = [0.20, 0.88, 0.35, 0.74] if "corr" in hnames[0]: metlabel = "Deep MET Corrected [GeV]" qtlabel = "Deep q_{T} Corrected [GeV]" mtlabel = "Deep m_{T} Corrected [GeV]" else: metlabel = "Deep MET [GeV]" qtlabel = "Deep q_{T} [GeV]" mtlabel = "Deep m_{T} [GeV]" DrawHistos(histos_met, legends, 0., 150., metlabel, ymin, ymax, "Ratio", "h_wjets_MCratio_deepmet_pt_{}".format(outputtag), dology=False, drawashist=True, mycolors=colors, linestyles=linestyles, legendPos=legendPos, legendNCols=legendNCols) DrawHistos(histos_u_pt, legends, 0., 140., qtlabel, ymin, ymax, "Ratio", "h_wjets_MCratio_u_reco_pt_Deep_{}".format(outputtag), dology=False, drawashist=True, mycolors=colors, linestyles=linestyles, legendPos=legendPos, legendNCols=legendNCols) DrawHistos(histos_mt, legends, 0., 140., mtlabel, ymin, ymax, "Ratio", "h_wjets_MCratio_mT_Deep_{}".format(outputtag), dology=False, drawashist=True, mycolors=colors, linestyles=linestyles, legendPos=legendPos, legendNCols=legendNCols)
def compMCRatios(hnames, colors, linestyles, legends, outputtag): # compare MC with MC histos_met = [ THStack2TH1( sampMan.hsmcs["histo_zjets_deepmet_pt_{}".format(hname)], "MCRatio") for hname in hnames ] histos_u_pt = [ THStack2TH1(sampMan.hsmcs["histo_zjets_u_pt_{}".format(hname)], "MCRatio") for hname in hnames ] histos_u1 = [ THStack2TH1(sampMan.hsmcs["histo_zjets_u1_{}".format(hname)], "MCRatio") for hname in hnames ] histos_u2 = [ THStack2TH1(sampMan.hsmcs["histo_zjets_u2_{}".format(hname)], "MCRatio") for hname in hnames ] histos_met_base = histos_met[0].Clone(histos_met[0].GetName() + "_Cloned") histos_u_pt_base = histos_u_pt[0].Clone(histos_u_pt[0].GetName() + "_Cloned") histos_u1_base = histos_u1[0].Clone(histos_u1[0].GetName() + "_Cloned") histos_u2_base = histos_u2[0].Clone(histos_u2[0].GetName() + "_Cloned") for hmet, hupt, hu1, hu2 in zip(histos_met, histos_u_pt, histos_u1, histos_u2): hmet.Divide(histos_met_base) hmet.SetFillColor(0) hupt.Divide(histos_u_pt_base) hupt.SetFillColor(0) hu1.Divide(histos_u1_base) hu1.SetFillColor(0) hu2.Divide(histos_u2_base) hu2.SetFillColor(0) if len(hnames) > 5: legendNCols = 2 legendPos = [0.20, 0.88, 0.70, 0.74] else: legendNCols = 1 legendPos = [0.20, 0.88, 0.35, 0.74] DrawHistos(histos_met, legends, 0., 150., "Deep MET Corrected [GeV]", rmin, rmax, "Ratio", "histo_zjets_MCratio_deepmet_pt_{}".format(outputtag), dology=False, drawashist=True, mycolors=colors, linestyles=linestyles, legendPos=legendPos, legendNCols=legendNCols) DrawHistos(histos_u_pt, legends, 0., 150., "u Corrected [GeV]", 0.7, 1.3, "Ratio", "histo_zjets_MCratio_u_pt_{}".format(outputtag), dology=False, drawashist=True, mycolors=colors, linestyles=linestyles, legendPos=legendPos, legendNCols=legendNCols) DrawHistos(histos_u1, legends, -40.0, 100., "u_{#parallel} Corrected [GeV]", 0.7, 1.3, "Ratio", "histo_zjets_MCratio_u1_{}".format(outputtag), dology=False, drawashist=True, mycolors=colors, linestyles=linestyles, legendPos=legendPos, legendNCols=legendNCols) DrawHistos(histos_u2, legends, -80.0, 80., "u_{#perp} Corrected [GeV]", 0.7, 1.3, "Ratio", "histo_zjets_MCratio_u2_{}".format(outputtag), dology=False, drawashist=True, mycolors=colors, linestyles=linestyles, legendPos=legendPos, legendNCols=legendNCols)
def compRatios(hnames, colors, linestyles, legends, outputtag): histos_met = [ sampMan.hratios["histo_zjets_deepmet_pt_{}".format( hname)].values()[0] for hname in hnames ] histos_u_pt = [ sampMan.hratios["histo_zjets_u_pt_{}".format(hname)].values()[0] for hname in hnames ] histos_u1 = [ sampMan.hratios["histo_zjets_u1_{}".format(hname)].values()[0] for hname in hnames ] histos_u2 = [ sampMan.hratios["histo_zjets_u2_{}".format(hname)].values()[0] for hname in hnames ] if len(hnames) > 5: legendNCols = 2 legendPos = [0.20, 0.88, 0.70, 0.74] else: legendNCols = 1 legendPos = [0.20, 0.88, 0.35, 0.74] DrawHistos(histos_met, legends, 0., 150., "Deep MET Corrected [GeV]", rmin, rmax, "Data / MC", "histo_zjets_ratio_deepmet_pt_{}".format(outputtag), dology=False, drawashist=True, mycolors=colors, linestyles=linestyles, legendPos=legendPos, legendNCols=legendNCols) DrawHistos(histos_u_pt, legends, 0., 150., "u Corrected [GeV]", 0.7, 1.3, "Data / MC", "histo_zjets_ratio_u_pt_{}".format(outputtag), dology=False, drawashist=True, mycolors=colors, linestyles=linestyles, legendPos=legendPos, legendNCols=legendNCols) DrawHistos(histos_u1, legends, -40.0, 100., "u_{#parallel} Corrected [GeV]", 0.7, 1.3, "Data / MC", "histo_zjets_ratio_u1_{}".format(outputtag), dology=False, drawashist=True, mycolors=colors, linestyles=linestyles, legendPos=legendPos, legendNCols=legendNCols) DrawHistos(histos_u2, legends, -80.0, 80., "u_{#perp} Corrected [GeV]", 0.7, 1.3, "Data / MC", "histo_zjets_ratio_u2_{}".format(outputtag), dology=False, drawashist=True, mycolors=colors, linestyles=linestyles, legendPos=legendPos, legendNCols=legendNCols)