def drawSignalContamination(filename, xy, split): import array label, unit, binning = readAxisConf("met") metBinning = array.array("d", binning) gHisto = readHisto(filename, "gMet%s_%s" % xy).Rebin( len(metBinning) - 1, randomName(), metBinning) eHisto = readHisto(filename, "eMet%s_%s" % xy).Rebin( len(metBinning) - 1, randomName(), metBinning) fHisto = readHisto(filename, "fMet%s_%s" % xy).Rebin( len(metBinning) - 1, randomName(), metBinning) eHisto = applyFakeRateEWK(eHisto) eContamination = divideHistos(eHisto, gHisto) eContamination.SetLineColor(2) fContamination = divideHistos(fHisto, gHisto) fContamination.SetLineColor(4) totalContamination = divideHistos(addHistos([eHisto, fHisto]), gHisto) totalContamination.SetLineColor(1) totalContamination.SetMarkerColor(1) for h in [totalContamination, fContamination, eContamination]: h.Scale(100.) # in % h.GetYaxis().SetTitleOffset(0.9) h.SetTitle(";%s [#text{%s}];Signal Contamination [%s]" % ("#met", unit, "%")) h.SetLineWidth(2) h.SetMarkerSize(0) h.SetLabelSize(1. / 15.8447, "xy") h.SetTitleSize(1. / 15.8447, "xy") mh = Multihisto() mh.setMinimum(0) if split: mh.addHisto(totalContamination, "Total", draw="pe") mh.addHisto(fContamination, "b_{#text{signal}}^{#text{QCD}}/s", draw="hist") mh.addHisto(eContamination, "b_{#text{signal}}^{e#rightarrow#gamma}/s", draw="hist") else: mh.addHisto(totalContamination, "", draw="e0") can = ROOT.TCanvas() can.cd() can.SetLogy(False) mh.Draw() totalContamination.Draw("same") info.Draw() saveName = "signalContamination_%s_%s_%s_%s" % (xy + (split, filename[0:-4])) SaveAs(can, saveName) ROOT.gPad.SaveAs("/home/knut/master/documents/thesis/plots/%stex" % saveName) correctTiksPlot("/home/knut/master/documents/thesis/plots/%stex" % saveName)
def drawPileUpHistos( saveTex=False ): inputHistPath = "../TreeWriter/pileUpReweighting/" data = readHisto( inputHistPath + "nTrueVertexData.root", "pileup" ) data.SetLineColor(1) data.SetMarkerColor(1) data.SetMarkerStyle(20) data.Sumw2() if saveTex: data.SetMarkerSize(.8) else: data.SetMarkerSize(1.2) mc = readHisto( inputHistPath + "nTrueVertexMC.root", "pileupS10" ) mc.SetLineColor(2) mc.SetMarkerColor(2) for h in [mc, data ]: h.Scale( 1./h.Integral() ) h.SetTitle( ";Number of Pile-up Events;Normalized Entries" ) if saveTex: h.SetLineWidth(2) h.SetTitleSize( 0.06311227345609463, "yx" ) h.SetLabelSize( 0.06311227345609463, "yx" ) muhist = Multihisto() muhist.addHisto( data, "Data", draw="ep" ) muhist.addHisto( mc, "Simulation", draw="hist" ) muhist.setMinimum(0) if saveTex: muhist.leg.SetTextSize(0.063112267888) texStyle() pc1 = ROOT.TLatex(0,.96, "CMS Private Work") pc2 = ROOT.TLatex( .76,.96, "\SI{19.8}{fb^{-1}} #sqrt{s}=\SI{8}{TeV}") masterPath = "~/master/documents/thesis/plots/" can = ROOT.TCanvas("","",2000,1000) can.cd() can.SetLogy(0) muhist.Draw() for pc in [pc1, pc2]: pc.SetNDC() if saveTex: pc.SetTextSize(0.06311227345609463) pc.Draw() ending = 'tex' if saveTex else 'pdf' ROOT.gPad.SaveAs(masterPath+"puDistribution.%s"%ending)
def split2Din1DMultihist(h2D, useYaxis, uFlow, oFlow): """Creates many TH1 out of one TH2 and put them in a Multihisto. h2D: TH2 (rebin appropriate if necessary ) useYaxis: The TH2 will be splitted in different values of Y. If false, the X axis will be used (bool). uFlow: Take the underFlow into account (bool). oFlow: Take the overFlow into account (bool). """ mh = Multihisto() mh.setMinimum(1e-8) style = ROOT.gROOT.GetStyle("tdrStyle") # for nice color gradient if useYaxis: axis = h2D.GetYaxis() else: axis = h2D.GetXaxis() variable = axis.GetTitle() nBins = axis.GetNbins() binRange = range(nBins + 2) if not uFlow: binRange = binRange[1:] if not oFlow: binRange = binRange[:-1] for iColor, iBin in enumerate(binRange): if useYaxis: h = h2D.ProjectionX("_px%s" % iBin, iBin, iBin) else: h = h2D.ProjectionY("_px%s" % iBin, iBin, iBin) lowEdge = axis.GetBinLowEdge(iBin) upEdge = axis.GetBinUpEdge(iBin) if iBin == 0: title = " %s < %s" % (variable, upEdge) elif iBin != nBins + 1: title = "%s #leq %s < %s" % (lowEdge, variable, upEdge) else: title = "%s #leq %s" % (lowEdge, variable) color = style.GetColorPalette( int(1. * iColor / (len(binRange) - 1) * (style.GetNumberOfColors() - 1))) h.SetLineColor(color) h.SetMarkerColor(h.GetLineColor()) h.SetMarkerSize(0) h.GetXaxis().SetTitle(h2D.GetXaxis().GetTitle()) mh.addHisto(h, title, draw="e0") return mh
def drawPileUpHistos(saveTex=False): inputHistPath = "../TreeWriter/pileUpReweighting/" data = readHisto(inputHistPath + "nTrueVertexData.root", "pileup") data.SetLineColor(1) data.SetMarkerColor(1) data.SetMarkerStyle(20) data.Sumw2() if saveTex: data.SetMarkerSize(.8) else: data.SetMarkerSize(1.2) mc = readHisto(inputHistPath + "nTrueVertexMC.root", "pileupS10") mc.SetLineColor(2) mc.SetMarkerColor(2) for h in [mc, data]: h.Scale(1. / h.Integral()) h.SetTitle(";Number of Pile-up Events;Normalized Entries") if saveTex: h.SetLineWidth(2) h.SetTitleSize(0.06311227345609463, "yx") h.SetLabelSize(0.06311227345609463, "yx") muhist = Multihisto() muhist.addHisto(data, "Data", draw="ep") muhist.addHisto(mc, "Simulation", draw="hist") muhist.setMinimum(0) if saveTex: muhist.leg.SetTextSize(0.063112267888) texStyle() pc1 = ROOT.TLatex(0, .96, "CMS Private Work") pc2 = ROOT.TLatex(.76, .96, "\SI{19.8}{fb^{-1}} #sqrt{s}=\SI{8}{TeV}") masterPath = "~/master/documents/thesis/plots/" can = ROOT.TCanvas("", "", 2000, 1000) can.cd() can.SetLogy(0) muhist.Draw() for pc in [pc1, pc2]: pc.SetNDC() if saveTex: pc.SetTextSize(0.06311227345609463) pc.Draw() ending = 'tex' if saveTex else 'pdf' ROOT.gPad.SaveAs(masterPath + "puDistribution.%s" % ending)
def split2Din1DMultihist( h2D, useYaxis, uFlow, oFlow ): """Creates many TH1 out of one TH2 and put them in a Multihisto. h2D: TH2 (rebin appropriate if necessary ) useYaxis: The TH2 will be splitted in different values of Y. If false, the X axis will be used (bool). uFlow: Take the underFlow into account (bool). oFlow: Take the overFlow into account (bool). """ mh = Multihisto() mh.setMinimum(1e-8) style = ROOT.gROOT.GetStyle("tdrStyle") # for nice color gradient if useYaxis: axis = h2D.GetYaxis() else: axis = h2D.GetXaxis() variable = axis.GetTitle() nBins = axis.GetNbins() binRange = range( nBins + 2 ) if not uFlow: binRange = binRange[1:] if not oFlow: binRange = binRange[:-1] for iColor, iBin in enumerate(binRange): if useYaxis: h = h2D.ProjectionX( "_px%s"%iBin, iBin, iBin ) else: h = h2D.ProjectionY( "_px%s"%iBin, iBin, iBin ) lowEdge = axis.GetBinLowEdge( iBin ) upEdge = axis.GetBinUpEdge( iBin ) if iBin == 0: title = " %s < %s"%( variable, upEdge ) elif iBin != nBins+1: title = "%s #leq %s < %s"%( lowEdge, variable, upEdge ) else: title = "%s #leq %s"%( lowEdge, variable ) color = style.GetColorPalette( int( 1.*iColor/(len(binRange)-1)*(style.GetNumberOfColors()-1) ) ) h.SetLineColor( color ) h.SetMarkerColor( h.GetLineColor() ) h.SetMarkerSize(0) h.GetXaxis().SetTitle( h2D.GetXaxis().GetTitle() ) mh.addHisto( h, title,draw="e0" ) return mh
def zGammaCut(plot="photons[0].pt", merge=False): cut = "[email protected]() && [email protected]()" nunuTree = readTree("slimZGammaNuNu_V03.19b_tree.root", "photonTree") llTree = readTree("slimZGammaLL_V02.19b_tree.root", "photonTree") zIncTree = readTree("slimZGamma_V03.18_tree.root", "photonTree") nBins = range(60, 500, 10) if plot == "photons[0].pt" or plot == "genPhotons[0].pt" else readAxisConf(plot)[2] h_nunu = getHisto(nunuTree, plot, cut + "&&genPhotons[0].pt>130", nBins=nBins, color=2) h_ll = getHisto(llTree, plot, cut, nBins=nBins, color=5) h_zInc = getHisto(zIncTree, plot, cut, nBins=nBins) llTree.AddFriend("photonTreeAddVariables", llTree.GetFile().GetName()) llPlot = "metLL" if plot == "met" else plot zMod = getHisto(llTree, llPlot, "genPhotons[0].pt<130", nBins=nBins, color=4) zMod.Scale(20.0 / (2.0 * 3.363)) mh = Multihisto() mh.orderByIntegral = False mh.setMinimum(0.01) mh.addHisto(h_nunu, "#gammaZ#rightarrow#gamma#nu#nu(p_{T#gamma}^{gen}#geq130GeV)", draw="e", toStack=True) mh.addHisto(zMod, "ll to #slash{E}_{T}", toStack=True) mh.addHisto(h_ll, "#gammaZ#rightarrow#gammall", draw="e", toStack=True) mh.addHisto(h_zInc, "#gammaZ", draw="e") if merge: h_nunuPart = getHisto(nunuTree, plot, cut + "&&genPhotons[0].pt>=130", nBins=nBins, color=6) h_zIncPart = getHisto(zIncTree, plot, cut + "&&genPhotons[0].pt<130", nBins=nBins, color=6) merged = addHistos([h_nunuPart, h_zIncPart]) mh.addHisto(merged, "merged", draw="e") mh.Draw() total = mh.stack.GetStack().Last().Clone(randomName()) total.SetFillStyle(3002) total.SetFillColor(1) total.Draw("same e2") if plot == "photons[0].pt": l = ROOT.TLine(130, 0, 130, 1) l.Draw() if merge: plot += "_merged" SavePad("zGammaCut_%s" % plot)
def drawSignalContamination( filename, xy, split ): import array label, unit, binning = readAxisConf("met") metBinning = array.array( "d", binning ) gHisto = readHisto( filename, "gMet%s_%s"%xy ).Rebin(len(metBinning)-1, randomName(), metBinning ) eHisto = readHisto( filename, "eMet%s_%s"%xy ).Rebin(len(metBinning)-1, randomName(), metBinning ) fHisto = readHisto( filename, "fMet%s_%s"%xy ).Rebin(len(metBinning)-1, randomName(), metBinning ) eHisto = applyFakeRateEWK( eHisto ) eContamination = divideHistos( eHisto, gHisto ) eContamination.SetLineColor(2) fContamination = divideHistos( fHisto, gHisto ) fContamination.SetLineColor(4) totalContamination = divideHistos( addHistos( [eHisto, fHisto] ), gHisto ) totalContamination.SetLineColor(1) totalContamination.SetMarkerColor(1) for h in [totalContamination, fContamination, eContamination]: h.Scale(100.) # in % h.GetYaxis().SetTitleOffset( 0.9 ) h.SetTitle(";%s [#text{%s}];Signal Contamination [%s]"%("#met",unit, "%")) h.SetLineWidth(2) h.SetMarkerSize(0) h.SetLabelSize(1./15.8447, "xy") h.SetTitleSize(1./15.8447, "xy") mh = Multihisto() mh.setMinimum(0) if split: mh.addHisto( totalContamination, "Total", draw="pe" ) mh.addHisto( fContamination, "b_{#text{signal}}^{#text{QCD}}/s", draw="hist" ) mh.addHisto( eContamination, "b_{#text{signal}}^{e#rightarrow#gamma}/s", draw="hist" ) else: mh.addHisto( totalContamination, "", draw="e0" ) can = ROOT.TCanvas() can.cd() can.SetLogy(False) mh.Draw() totalContamination.Draw("same") info.Draw() saveName = "signalContamination_%s_%s_%s_%s"%(xy+(split,filename[0:-4] )) SaveAs(can, saveName ) ROOT.gPad.SaveAs("/home/knut/master/documents/thesis/plots/%stex"%saveName ) correctTiksPlot( "/home/knut/master/documents/thesis/plots/%stex"%saveName )
def closure( filenames, plot ): leptonPtCut = 15 # only larger than 15 make sense here, since this is the reprocessing cut commonCut = "([email protected]() || Max$(electrons.pt)<{0}) && ([email protected]() || Max$(muons.pt)<{0})".format(leptonPtCut) #commonCut = "1" totalHist = getHists( filenames, plot, cut=commonCut ) gGenHist = getHists( filenames, plot, cut="photons[0].isGen(0) && "+commonCut ) eHist = multiDimFakeRate( filenames, plot, commonCut, False ) fakeHist = predictionHistos( filenames, plot, cut=commonCut )[0] fakeHist.SetLineColor(4) eHist.SetLineColor(3) gGenHist.SetLineColor(2) eHistSys = eHist.Clone( randomName() ) eHistSys = setRelativeUncertainty( eHistSys, 0.11 ) eHistSys.SetFillColor( eHistSys.GetLineColor() ) eHistSys.SetFillStyle(3354) eHistSys.SetMarkerSize(0) gDatasetAbbrs = [getDatasetAbbr(f) for f in filenames ] gDatasetAbbrs = mergeDatasetAbbr( gDatasetAbbrs ) multihisto = Multihisto() multihisto.setMinimum(0.01) multihisto.setMaximum(20) multihisto.leg.SetHeader( "/".join([ datasetToLatex(x) for x in gDatasetAbbrs]) ) multihisto.addHisto( totalHist, "Simulation", draw="e0 hist" ) multihisto.addHisto( fakeHist, "QCD", toStack=True, draw="hist" ) multihisto.addHisto( gGenHist, "gen#gamma", toStack=True, draw="e0 hist" ) multihisto.addHisto( eHist, "e#rightarrow#gamma", toStack=True, draw="hist" ) infoText = ROOT.TLatex(0.03,.96, "CMS Private Work - 8TeV #geq1#gamma,#geq2jets" ) infoText.SetNDC() can = ROOT.TCanvas() can.cd() multihisto.Draw() infoText.Draw() r = Ratio( "Sim./Pred.", totalHist, multihisto.stack.GetStack().Last() ) r.draw(0,2) SaveAs( can, "ewkPrediction_%s_%s"%(getSaveNameFromDatasets(filenames), plot)) ROOT.SetOwnership( can, False ) del can
def drawStackedBackground( plot, treeName, listOfFiles, sumBinned, order=False ): cut = "[email protected]() && [email protected]()" # if a data histogram is present, the mc integral will scaled to data mcHists = [] dataHists = [] for fileName in listOfFiles: datasetAbbr = getDatasetAbbr( fileName ) try: color = colors[datasetAbbr] except: color = 1 tree = readTree( fileName, treeName ) histo = getHisto( tree, plot, color=color, cut=cut ) if "PhotonHad" in fileName: dataHists.append( (datasetAbbr, histo ) ) else: mcHists.append( ( datasetAbbr, histo ) ) # merge datahists if there dataHist = addHistos( [histo for datasetAbbr, histo in dataHists] ) if dataHists else None scale = 1.*dataHist.Integral()/addHistos( [histo for datasetAbbr, histo in mcHists] ).Integral() if dataHists else 1 scale = 1. # combine MC datasets if sumBinned: for combiAbbr, abbrList in combinedDatasets.iteritems(): if set(abbrList).issubset( set( [ a for a, h in mcHists ] ) ): #print "a valid combination was found" histosToAdd = [ h for a,h in mcHists if a in abbrList ] thisSum = addHistos( histosToAdd ) mcHists = [ (a,h) for a,h in mcHists if a not in abbrList]+[(combiAbbr,thisSum)] mh = Multihisto() mh.setMinimum(0.01) mh.leg.SetX1NDC(0.5) mh.leg.SetY1NDC(0.5) mh.orderByIntegral = order if dataHist: mh.addHisto( dataHist, "Data", draw="e0" ) for abbr, hist in mcHists: hist.Scale( scale ) mh.addHisto( hist, datasetToLatex(abbr), toStack=True, draw="hist" ) # add signal histos #signalFiles = ["slimW_1000_1020_375_V02.44_tree.root", "slimW_1200_1120_375_V02.44_tree.root"] signalFiles = ["slimW_1200_1120_375_V02.44_tree.root"] signalFiles = [] for iColor, sf in enumerate(signalFiles): tree = readTree( sf, treeName ) histo = getHisto( tree, plot, color=ROOT.kMagenta+iColor, cut=cut ) mh.addHisto( histo, datasetToLatex(getDatasetAbbr(sf)), draw="hist" ) can = ROOT.TCanvas() can.cd() mh.Draw() info = PlotCaption(treeName=treeName) info.Draw() if dataHist: from myRatio import Ratio den = mh.stack.GetStack().Last().Clone( randomName() ) den.SetLineColor(2) r = Ratio( "Data/Sim", dataHist, den ) r.draw(0,2) allDatasetAbbr = getSaveNameFromDatasets( listOfFiles ) SaveAs( can, "stackedHisto_%s_%s_%s"%(treeName, plot,allDatasetAbbr) )
def inclusiveAndIsrSamples(fList1, fList2, saveAffix=""): cut = "[email protected]() && [email protected]()" treeName = "photonTree" plot = "met" if saveAffix == "pt130": cut += " && photons[0].pt>130" saveAffix = "_" + saveAffix mh = Multihisto() h1 = getHists(fList1, plot, cut) h1gen = getHists(fList1, plot, cut + "&&photons[0].isGen(0)") h2 = getHists(fList2, plot, cut) h2gen = getHists(fList2, plot, cut + "&&photons[0].isGen(0)") for h in h2, h2gen: h.SetLineColor(2) for h in h1gen, h2gen: h.SetLineStyle(2) for h in [h2, h2gen, h1, h1gen]: h.SetLabelSize(1.0 / 31.5562 / 0.502113, "xyz") h.SetTitleSize(1.0 / 31.5562 / 0.502113, "xyz") h.GetXaxis().SetTitleOffset(1.1) h.GetYaxis().SetTitleOffset(0.85) if plot == "met": h.GetXaxis().SetTitle("#met#text{ [GeV]}") abbr1 = shortName(fList1) abbr2 = shortName(fList2) mh = Multihisto() if "ZGamma" in fList1[0]: mh.setMaximum(10) mh.setMinimum(2e-4) mh.leg.SetFillStyle(0) mh.leg.SetX1(0.6) mh.leg.SetY1(0.6) mh.addHisto(h1, datasetToLatex(abbr1)) mh.addHisto(h1gen, "#text{match to gen }#gamma") mh.addHisto(h2, datasetToLatex(abbr2)) mh.addHisto(h2gen, "#text{match to gen }#gamma") mh.Draw() info.SetTextSize(1.0 / 31.5562 / 0.502113) info.Draw() if "pt130" in saveAffix: cutInfo = ROOT.TLatex(0.2, 0.8, "p_{T,#gamma}#geq#SI{130}{GeV}") cutInfo.SetNDC() info.SetTextSize(1.0 / 31.5562 / 0.502113) cutInfo.Draw() SavePad("inclusiveAndIsrSample_%s%s" % (abbr1, saveAffix)) ROOT.gPad.SaveAs("/home/knut/master/documents/thesis/plots/inclusiveAndIsrSample_%s%s.tex" % (abbr1, saveAffix)) correctTiksPlot("/home/knut/master/documents/thesis/plots/inclusiveAndIsrSample_%s%s.tex" % (abbr1, saveAffix))
"color": ROOT.kGreen - 4 } #bkg["qcd"] = { "files": ["slimQCD_1000_inf_V03.24_tree.root", "slimQCD_250_500_V03.24_tree.root", "slimQCD_500_1000_V03.24_tree.root"], "title":"Multijet", "color": ROOT.kCyan+3 } nestedBkgFiles = [bkg[a]["files"] for a in bkg.keys()] bkgFiles = [item for sublist in nestedBkgFiles for item in sublist] kFactor = getkFactor(dataFiles, bkgFiles, opts.plot, chi2Cut) #signal = getHists( ["slimW_1700_720_375_V03.24_tree.root" ], opts.plot, cut ) #signal.SetLineColor( ROOT.kGreen ) #signal.SetLineWidth(2) mh = Multihisto() mh.setMinimum(0) mh.addHisto(data, "Data", draw="pe") for name, d in bkg.iteritems(): histo = getHists(d["files"], opts.plot, cut) histo.SetLineColor(d["color"]) histo.Scale(kFactor) mh.addHisto(histo, d["title"], True) #mh.addHisto( signal, "Wino", False ) #combiBkg = getCombinatoricalBkg( dataFiles, opts.plot ) #combiBkg.SetLineWidth(2) #combiBkg.SetLineColor( ROOT.kBlue ) #combiBkg.Scale( data.Integral(0, data.FindBin(70), "width") / combiBkg.Integral(0,data.FindBin(70),"width")) #mh.addHisto( combiBkg, "bkg", draw="hist e" )
def inclusiveAndIsrSamples(fList1, fList2, saveAffix=""): cut = "[email protected]() && [email protected]()" treeName = "photonTree" plot = "met" if saveAffix == "pt130": cut += " && photons[0].pt>130" saveAffix = "_" + saveAffix mh = Multihisto() h1 = getHists(fList1, plot, cut) h1gen = getHists(fList1, plot, cut + "&&photons[0].isGen(0)") h2 = getHists(fList2, plot, cut) h2gen = getHists(fList2, plot, cut + "&&photons[0].isGen(0)") for h in h2, h2gen: h.SetLineColor(2) for h in h1gen, h2gen: h.SetLineStyle(2) for h in [h2, h2gen, h1, h1gen]: h.SetLabelSize(1. / 31.5562 / 0.502113, "xyz") h.SetTitleSize(1. / 31.5562 / 0.502113, "xyz") h.GetXaxis().SetTitleOffset(1.1) h.GetYaxis().SetTitleOffset(0.85) if plot == "met": h.GetXaxis().SetTitle("#met#text{ [GeV]}") abbr1 = shortName(fList1) abbr2 = shortName(fList2) mh = Multihisto() if "ZGamma" in fList1[0]: mh.setMaximum(10) mh.setMinimum(2e-4) mh.leg.SetFillStyle(0) mh.leg.SetX1(0.6) mh.leg.SetY1(0.6) mh.addHisto(h1, datasetToLatex(abbr1)) mh.addHisto(h1gen, "#text{match to gen }#gamma") mh.addHisto(h2, datasetToLatex(abbr2)) mh.addHisto(h2gen, "#text{match to gen }#gamma") mh.Draw() info.SetTextSize(1. / 31.5562 / 0.502113) info.Draw() if "pt130" in saveAffix: cutInfo = ROOT.TLatex(.2, .8, "p_{T,#gamma}#geq#SI{130}{GeV}") cutInfo.SetNDC() info.SetTextSize(1. / 31.5562 / 0.502113) cutInfo.Draw() SavePad("inclusiveAndIsrSample_%s%s" % (abbr1, saveAffix)) ROOT.gPad.SaveAs( "/home/knut/master/documents/thesis/plots/inclusiveAndIsrSample_%s%s.tex" % (abbr1, saveAffix)) correctTiksPlot( "/home/knut/master/documents/thesis/plots/inclusiveAndIsrSample_%s%s.tex" % (abbr1, saveAffix))
def drawStackedBackground( plot, treeName, listOfFiles, sumBinned, order=False ): tightCut = " photons[0].sigmaIetaIeta<0.011 && photons[0].chargedIso<0.7 && photons[0].neutralIso < 0.4+0.04*photons[0].pt && photons[0].photonIso < 0.5+0.005*photons[0].pt && photons[0].r9<0.9" cut = "[email protected]() && [email protected]()" #cut = cut +"&&" + tightCut cut = "std::abs(photons[0].eta)>1.4442" # if a data histogram is present, the mc integral will scaled to data mcHists = [] dataHists = [] for fileName in listOfFiles: datasetAbbr = getDatasetAbbr( fileName ) try: color = colors[datasetAbbr] except: color = 1 tree = readTree( fileName, treeName ) histo = getHisto( tree, plot, color=color, cut=cut ) if "PhotonHad" in fileName: dataHists.append( (datasetAbbr, histo ) ) else: mcHists.append( ( datasetAbbr, histo ) ) # merge datahists if there dataHist = addHistos( [histo for datasetAbbr, histo in dataHists] ) if dataHists else None scale = 1.*dataHist.Integral()/addHistos( [histo for datasetAbbr, histo in mcHists] ).Integral() if dataHists else 1 scale = 1. # combine MC datasets if sumBinned: for combiAbbr, abbrList in combinedDatasets.iteritems(): if set(abbrList).issubset( set( [ a for a, h in mcHists ] ) ): indices = [] for a,b in mcHists: if a in abbrList: indices.append(mcHists.index((a,b))) #print "a valid combination was found" histosToAdd = [ h for a,h in mcHists if a in abbrList ] thisSum = addHistos( histosToAdd ) mcHists = [ (a,h) for a,h in mcHists if a not in abbrList] mcHists.insert(min(indices),(combiAbbr,thisSum)) mh = Multihisto() mh.setMinimum(0.01) mh.leg.SetFillStyle(0) mh.leg.SetNColumns(2) mh.orderByIntegral = order if dataHist: mh.addHisto( dataHist, "Data", draw="ep" ) for abbr, hist in mcHists: hist.Scale( scale ) mh.addHisto( hist, datasetToLatex(abbr), toStack=True, draw="hist" ) egammaHist = True if egammaHist: egammaHist = multiDimFakeRate( [ filename for filename in listOfFiles if "PhotonHad" in filename ], plot, cut ) egammaHist.SetLineColor(ROOT.kGreen ) mh.addHisto( egammaHist, "e#rightarrow#gamma", toStack=True ) #totalBG = addHistos( [ h for abbr, h in mcHists ] ) #totalBG.SetLineColor(2) #mh.addHisto( totalBG, "SM Simulation", toStack=False, draw="hist" ) # add signal histos #signalFiles = ["slimW_1000_1020_375_V02.44_tree.root", "slimW_1200_1120_375_V02.44_tree.root"] signalFiles = ["slimW_1200_1120_375_V02.44_tree.root"] signalFiles = ["slimW_1700_720_375_V03.22_tree.root"] for iColor, sf in enumerate(signalFiles): tree = readTree( sf, treeName ) histo = getHisto( tree, plot, color=ROOT.kBlue+iColor, cut=cut ) #mh.addHisto( histo, datasetToLatex(getDatasetAbbr(sf)), draw="hist" ) mh.addHisto( histo, "Signal", draw="hist" ) infoText = ROOT.TLatex(0,.97, "#text{CMS Private Work }#geq1#gamma_{#text{pixel}},#geq2#text{jets}" ) infoText.SetNDC() infoText.SetTextSize(0.05) can = ROOT.TCanvas() can.cd() mh.Draw() infoText.Draw() #if dataHist: # from myRatio import Ratio # den = totalBG #den = mh.stack.GetStack().Last().Clone( randomName() ) # den.SetLineColor(2) # r = Ratio( "Data/Sim.", dataHist, den ) # r.draw(0,2) allDatasetAbbr = getSaveNameFromDatasets( listOfFiles ) SaveAs( can, "stackedHisto_%s_%s_%s"%(treeName, plot,allDatasetAbbr) )
def drawStackedBackground(plot, treeName, listOfFiles, sumBinned, order=False): tightCut = " photons[0].sigmaIetaIeta<0.011 && photons[0].chargedIso<0.7 && photons[0].neutralIso < 0.4+0.04*photons[0].pt && photons[0].photonIso < 0.5+0.005*photons[0].pt && photons[0].r9<0.9" cut = "[email protected]() && [email protected]()" #cut = cut +"&&" + tightCut cut = "std::abs(photons[0].eta)>1.4442" # if a data histogram is present, the mc integral will scaled to data mcHists = [] dataHists = [] for fileName in listOfFiles: datasetAbbr = getDatasetAbbr(fileName) try: color = colors[datasetAbbr] except: color = 1 tree = readTree(fileName, treeName) histo = getHisto(tree, plot, color=color, cut=cut) if "PhotonHad" in fileName: dataHists.append((datasetAbbr, histo)) else: mcHists.append((datasetAbbr, histo)) # merge datahists if there dataHist = addHistos([histo for datasetAbbr, histo in dataHists ]) if dataHists else None scale = 1. * dataHist.Integral() / addHistos( [histo for datasetAbbr, histo in mcHists]).Integral() if dataHists else 1 scale = 1. # combine MC datasets if sumBinned: for combiAbbr, abbrList in combinedDatasets.iteritems(): if set(abbrList).issubset(set([a for a, h in mcHists])): indices = [] for a, b in mcHists: if a in abbrList: indices.append(mcHists.index((a, b))) #print "a valid combination was found" histosToAdd = [h for a, h in mcHists if a in abbrList] thisSum = addHistos(histosToAdd) mcHists = [(a, h) for a, h in mcHists if a not in abbrList] mcHists.insert(min(indices), (combiAbbr, thisSum)) mh = Multihisto() mh.setMinimum(0.01) mh.leg.SetFillStyle(0) mh.leg.SetNColumns(2) mh.orderByIntegral = order if dataHist: mh.addHisto(dataHist, "Data", draw="ep") for abbr, hist in mcHists: hist.Scale(scale) mh.addHisto(hist, datasetToLatex(abbr), toStack=True, draw="hist") egammaHist = True if egammaHist: egammaHist = multiDimFakeRate( [filename for filename in listOfFiles if "PhotonHad" in filename], plot, cut) egammaHist.SetLineColor(ROOT.kGreen) mh.addHisto(egammaHist, "e#rightarrow#gamma", toStack=True) #totalBG = addHistos( [ h for abbr, h in mcHists ] ) #totalBG.SetLineColor(2) #mh.addHisto( totalBG, "SM Simulation", toStack=False, draw="hist" ) # add signal histos #signalFiles = ["slimW_1000_1020_375_V02.44_tree.root", "slimW_1200_1120_375_V02.44_tree.root"] signalFiles = ["slimW_1200_1120_375_V02.44_tree.root"] signalFiles = ["slimW_1700_720_375_V03.22_tree.root"] for iColor, sf in enumerate(signalFiles): tree = readTree(sf, treeName) histo = getHisto(tree, plot, color=ROOT.kBlue + iColor, cut=cut) #mh.addHisto( histo, datasetToLatex(getDatasetAbbr(sf)), draw="hist" ) mh.addHisto(histo, "Signal", draw="hist") infoText = ROOT.TLatex( 0, .97, "#text{CMS Private Work }#geq1#gamma_{#text{pixel}},#geq2#text{jets}") infoText.SetNDC() infoText.SetTextSize(0.05) can = ROOT.TCanvas() can.cd() mh.Draw() infoText.Draw() #if dataHist: # from myRatio import Ratio # den = totalBG #den = mh.stack.GetStack().Last().Clone( randomName() ) # den.SetLineColor(2) # r = Ratio( "Data/Sim.", dataHist, den ) # r.draw(0,2) allDatasetAbbr = getSaveNameFromDatasets(listOfFiles) SaveAs(can, "stackedHisto_%s_%s_%s" % (treeName, plot, allDatasetAbbr))
def finalDistributionData(plot): # Sample names treeVersion = "31" wg = [ "slimWGamma_50_130_V03.%s_tree.root"%treeVersion, \ "slimWGamma_130_inf_V03.%s_tree.root"%treeVersion ] tg = ["slimTTGamma_V03.%s_tree.root" % treeVersion] zgn = ["slimZGammaNuNu_V03.%s_tree.root" % treeVersion] zgll = ["slimZGammaLL_V02.19b_tree.root"] data = [ "PhotonHad%s_V03.%s_tree.root" % (x, treeVersion) for x in ["A", "B", "C", "D"] ] # additional ISR uncertainty ewkUncertainty = 0.11 isrUncertaintyZ = 0.5 isrUncertaintyW = 0.5 isrUncertaintyT = 0.5 isrUncertainty = 0.5 leptonPtCut = 25 # only larger than 15 make sense here, since this is the reprocessing cut #commonCut = "([email protected]() || Max$(electrons.pt)<{0}) && ([email protected]() || Max$(muons.pt)<{0})".format(leptonPtCut) commonCut = "[email protected]() && [email protected]() && thisPt>0 && recoilChr > 0" # Compute the weights: weight2D = getMixedWeigthHisto(data, data, commonCut) attachWeightsToFiles(data, weight2D, "foWeights") drawWeightHisto(weight2D, "Data", writeWeightFile=True) # Get Histograms dataHist = getHists(data, plot, commonCut) fgammaHist, fgammaWeightError = predictionHistos(data, plot, commonCut, modifyEmptyBins=False) egammaHist = multiDimFakeRate(data, plot, commonCut) egammaHistsys = setRelativeUncertainty(egammaHist.Clone(randomName()), ewkUncertainty) fsrZ = getHists(zgn, plot, commonCut + "&&genPhotons[0].pt>130") fsrZll = getHists(zgll, plot, commonCut) if plot == "met": fsrZ2 = getHists(zgll, plot + "LL", commonCut + "&&genPhotons[0].pt<130") else: fsrZ2 = getHists(zgll, plot, commonCut + "&&0") fsrZ2.Scale(20. / (2. * 3.363)) fsrW = getHists(wg, plot, commonCut) fsrT = getHists(tg, plot, commonCut) # apply common scale factor for h in fsrW, fsrT, fsrZ, fsrZ2, fsrZll: h.Scale(1.5) fsr = addHistos([fsrT, fsrW, fsrZ, fsrZ2, fsrZll]) fsr.SetLineColor(ROOT.kRed) fsrSys = setRelativeUncertainty(fsr, isrUncertainty) #signal1 = getMetHisto( "W", 900, 1720 ) #signal2 = getMetHisto( "B", 1700, 1120 ) signal1 = getHists(["slimW_1700_720_375_V03.06_tree.root"], plot, commonCut) signal2 = getHists(["slimW_900_1720_375_V03.06_tree.root"], plot, commonCut) signal3 = getHists(["slimB_1300_1720_375_V03.06_tree.root"], plot, commonCut) signal4 = getHists(["slimB_1700_1120_375_V03.06_tree.root"], plot, commonCut) for i, signal in enumerate([signal1, signal2, signal3, signal4]): signal.SetLineColor(ROOT.kGreen + i) signal.SetLineColor(ROOT.kBlue + i) #signal.SetLineStyle(5+i) # prettify histograms fgammaHist.SetLineColor(7) egammaHist.SetLineColor(3) fsrZ.SetLineColor(ROOT.kRed - 7) fsrW.SetLineColor(ROOT.kRed - 9) fsrT.SetLineColor(ROOT.kRed) mh = Multihisto() mh.setMinimum(0.2) mh.orderByIntegral = False mh.addHisto(fsr, "ISR", True) mh.addHisto(egammaHist, "e#rightarrow#gamma", True) mh.addHisto(fgammaHist, "Multijet", True) dataLegName = "Data" mh.addHisto(dataHist, dataLegName, draw="pe x0") mh.addHisto(signal2, "Bino-like #chi_{1}^{0}", draw="hist") mh.addHisto(signal1, "Wino-like #chi_{1}^{0}", draw="hist") # get all SYSTEMATICAL uncertainties: systematicUncertHistStack = ROOT.THStack() systematicUncertHistStack.Add(fgammaWeightError) systematicUncertHistStack.Add(egammaHistsys) systematicUncertHistStack.Add(fsrSys) if plot == "met": writeDataCard(treeVersion, dataHist, fgammaHist, fgammaWeightError, egammaHist, egammaHistsys, fsr, fsrSys) # draw stuff luminosity = 19.7 infoText = ROOT.TLatex( 0, .96, "CMS Private Work - %sfb^{-1} #sqrt{s}=8TeV #geq1#gamma_{tight},#geq2jets" % luminosity) infoText.SetNDC() infoText.SetTextSize(.04) can = ROOT.TCanvas() mh.Draw() statUncert = mh.stack.GetStack().Last().Clone(randomName()) systUncert = systematicUncertHistStack.GetStack().Last().Clone( randomName()) totalUncert = statUncert.Clone(randomName()) for bin in range(totalUncert.GetNbinsX() + 2): totalUncert.SetBinError( bin, statUncert.GetBinError(bin) | qPlus | systUncert.GetBinError(bin)) for h in statUncert, systUncert, totalUncert: h.SetMarkerSize(0) totalUncert.SetFillStyle(3002) totalUncert.SetFillColor(1) totalUncert.Draw("same e2") systUncert.SetFillStyle(3254) systUncert.SetFillColor(2) systUncert.Draw("same e2") statUncert.SetLineWidth(3) statUncert.SetLineColor(2) statUncert.Draw("same e x0") dataHist.Draw("same pe x0") from myRatio import Ratio r = Ratio("Data / Bkg", dataHist, mh.stack.GetStack().Last(), systematicUncertHistStack.GetStack().Last()) r.draw(.5, 1.5) infoText.Draw() SaveAs(can, "finalDistributionData_%s" % plot)
bkg["tt"] = { "files": ["slimTTGamma_V03.%s_tree.root"%treeVersion], "title": "#gammat#bar{t}", "color": ROOT.kBlue } bkg["wjets"] = { "files": ["slimWJets_250_300_V03.24_tree.root", "slimWJets_300_400_V03.24_tree.root", "slimWJets_400_inf_V03.24_tree.root" ], "title": "W", "color": ROOT.kGreen+4 } bkg["wgamma"] = { "files": ["slimWGamma_130_inf_V03.24_tree.root", "slimWGamma_50_130_V03.24_tree.root" ], "title": "#gammaW", "color": ROOT.kGreen-4 } #bkg["qcd"] = { "files": ["slimQCD_1000_inf_V03.24_tree.root", "slimQCD_250_500_V03.24_tree.root", "slimQCD_500_1000_V03.24_tree.root"], "title":"Multijet", "color": ROOT.kCyan+3 } nestedBkgFiles = [ bkg[a]["files"] for a in bkg.keys()] bkgFiles = [item for sublist in nestedBkgFiles for item in sublist] kFactor = getkFactor( dataFiles, bkgFiles, opts.plot, chi2Cut ) #signal = getHists( ["slimW_1700_720_375_V03.24_tree.root" ], opts.plot, cut ) #signal.SetLineColor( ROOT.kGreen ) #signal.SetLineWidth(2) mh = Multihisto() mh.setMinimum(0) mh.addHisto( data, "Data", draw="pe" ) for name, d in bkg.iteritems(): histo = getHists( d["files"], opts.plot, cut ) histo.SetLineColor( d["color"] ) histo.Scale( kFactor ) mh.addHisto( histo, d["title"], True ) #mh.addHisto( signal, "Wino", False ) #combiBkg = getCombinatoricalBkg( dataFiles, opts.plot ) #combiBkg.SetLineWidth(2) #combiBkg.SetLineColor( ROOT.kBlue ) #combiBkg.Scale( data.Integral(0, data.FindBin(70), "width") / combiBkg.Integral(0,data.FindBin(70),"width")) #mh.addHisto( combiBkg, "bkg", draw="hist e" )
def finalDistributionData( plot ): # Sample names treeVersion = "31" wg = [ "slimWGamma_50_130_V03.%s_tree.root"%treeVersion, \ "slimWGamma_130_inf_V03.%s_tree.root"%treeVersion ] tg = [ "slimTTGamma_V03.%s_tree.root"%treeVersion ] zgn = [ "slimZGammaNuNu_V03.%s_tree.root"%treeVersion ] zgll = [ "slimZGammaLL_V02.19b_tree.root" ] data = [ "PhotonHad%s_V03.%s_tree.root"%(x,treeVersion) for x in ["A","B","C","D" ] ] # additional ISR uncertainty ewkUncertainty = 0.11 isrUncertaintyZ = 0.5 isrUncertaintyW = 0.5 isrUncertaintyT = 0.5 isrUncertainty = 0.5 leptonPtCut = 25 # only larger than 15 make sense here, since this is the reprocessing cut #commonCut = "([email protected]() || Max$(electrons.pt)<{0}) && ([email protected]() || Max$(muons.pt)<{0})".format(leptonPtCut) commonCut = "[email protected]() && [email protected]() && thisPt>0 && recoilChr > 0" # Compute the weights: weight2D = getMixedWeigthHisto( data, data, commonCut ) attachWeightsToFiles( data, weight2D, "foWeights" ) drawWeightHisto( weight2D, "Data", writeWeightFile=True ) # Get Histograms dataHist = getHists( data, plot, commonCut ) fgammaHist, fgammaWeightError = predictionHistos( data, plot, commonCut, modifyEmptyBins=False ) egammaHist = multiDimFakeRate( data, plot, commonCut ) egammaHistsys = setRelativeUncertainty( egammaHist.Clone(randomName()), ewkUncertainty ) fsrZ = getHists( zgn, plot, commonCut+"&&genPhotons[0].pt>130" ) fsrZll = getHists( zgll, plot, commonCut ) if plot=="met": fsrZ2 = getHists( zgll, plot+"LL", commonCut+"&&genPhotons[0].pt<130" ) else: fsrZ2 = getHists( zgll, plot, commonCut+"&&0" ) fsrZ2.Scale( 20./(2.*3.363) ) fsrW = getHists( wg, plot, commonCut ) fsrT = getHists( tg, plot, commonCut ) # apply common scale factor for h in fsrW, fsrT, fsrZ, fsrZ2, fsrZll: h.Scale(1.5) fsr = addHistos( [fsrT, fsrW, fsrZ, fsrZ2,fsrZll ] ) fsr.SetLineColor(ROOT.kRed) fsrSys = setRelativeUncertainty( fsr, isrUncertainty ) #signal1 = getMetHisto( "W", 900, 1720 ) #signal2 = getMetHisto( "B", 1700, 1120 ) signal1 = getHists( ["slimW_1700_720_375_V03.06_tree.root"], plot, commonCut ) signal2 = getHists( ["slimW_900_1720_375_V03.06_tree.root"], plot, commonCut ) signal3 = getHists( ["slimB_1300_1720_375_V03.06_tree.root"], plot, commonCut ) signal4 = getHists( ["slimB_1700_1120_375_V03.06_tree.root"], plot, commonCut ) for i, signal in enumerate([signal1, signal2, signal3, signal4]): signal.SetLineColor( ROOT.kGreen + i ) signal.SetLineColor( ROOT.kBlue + i ) #signal.SetLineStyle(5+i) # prettify histograms fgammaHist.SetLineColor(7) egammaHist.SetLineColor( 3 ) fsrZ.SetLineColor( ROOT.kRed-7 ) fsrW.SetLineColor( ROOT.kRed-9 ) fsrT.SetLineColor( ROOT.kRed ) mh = Multihisto() mh.setMinimum(0.2) mh.orderByIntegral = False mh.addHisto( fsr, "ISR", True ) mh.addHisto( egammaHist, "e#rightarrow#gamma", True ) mh.addHisto( fgammaHist, "Multijet", True ) dataLegName = "Data" mh.addHisto( dataHist, dataLegName, draw="pe x0" ) mh.addHisto( signal2, "Bino-like #chi_{1}^{0}", draw="hist" ) mh.addHisto( signal1, "Wino-like #chi_{1}^{0}", draw="hist" ) # get all SYSTEMATICAL uncertainties: systematicUncertHistStack = ROOT.THStack() systematicUncertHistStack.Add( fgammaWeightError ) systematicUncertHistStack.Add( egammaHistsys ) systematicUncertHistStack.Add( fsrSys ) if plot == "met": writeDataCard( treeVersion, dataHist, fgammaHist, fgammaWeightError, egammaHist, egammaHistsys, fsr, fsrSys ) # draw stuff luminosity = 19.7 infoText = ROOT.TLatex(0,.96, "CMS Private Work - %sfb^{-1} #sqrt{s}=8TeV #geq1#gamma_{tight},#geq2jets"%luminosity ) infoText.SetNDC() infoText.SetTextSize(.04) can = ROOT.TCanvas() mh.Draw() statUncert = mh.stack.GetStack().Last().Clone( randomName() ) systUncert = systematicUncertHistStack.GetStack().Last().Clone( randomName() ) totalUncert = statUncert.Clone( randomName() ) for bin in range( totalUncert.GetNbinsX()+2 ): totalUncert.SetBinError( bin, statUncert.GetBinError(bin) |qPlus| systUncert.GetBinError(bin) ) for h in statUncert, systUncert, totalUncert: h.SetMarkerSize(0) totalUncert.SetFillStyle(3002) totalUncert.SetFillColor(1) totalUncert.Draw("same e2") systUncert.SetFillStyle(3254) systUncert.SetFillColor(2) systUncert.Draw("same e2") statUncert.SetLineWidth(3) statUncert.SetLineColor(2) statUncert.Draw("same e x0") dataHist.Draw("same pe x0") from myRatio import Ratio r = Ratio( "Data / Bkg", dataHist, mh.stack.GetStack().Last(), systematicUncertHistStack.GetStack().Last() ) r.draw(.5,1.5) infoText.Draw() SaveAs( can, "finalDistributionData_%s"%plot )
def zGammaCut(plot="photons[0].pt", merge=False): cut = "[email protected]() && [email protected]()" nunuTree = readTree("slimZGammaNuNu_V03.19b_tree.root", "photonTree") llTree = readTree("slimZGammaLL_V02.19b_tree.root", "photonTree") zIncTree = readTree("slimZGamma_V03.18_tree.root", "photonTree") nBins = range( 60, 500, 10 ) if plot == "photons[0].pt" or plot == "genPhotons[0].pt" else readAxisConf( plot)[2] h_nunu = getHisto(nunuTree, plot, cut + "&&genPhotons[0].pt>130", nBins=nBins, color=2) h_ll = getHisto(llTree, plot, cut, nBins=nBins, color=5) h_zInc = getHisto(zIncTree, plot, cut, nBins=nBins) llTree.AddFriend("photonTreeAddVariables", llTree.GetFile().GetName()) llPlot = "metLL" if plot == "met" else plot zMod = getHisto(llTree, llPlot, "genPhotons[0].pt<130", nBins=nBins, color=4) zMod.Scale(20. / (2. * 3.363)) mh = Multihisto() mh.orderByIntegral = False mh.setMinimum(0.01) mh.addHisto(h_nunu, "#gammaZ#rightarrow#gamma#nu#nu(p_{T#gamma}^{gen}#geq130GeV)", draw="e", toStack=True) mh.addHisto(zMod, "ll to #slash{E}_{T}", toStack=True) mh.addHisto(h_ll, "#gammaZ#rightarrow#gammall", draw="e", toStack=True) mh.addHisto(h_zInc, "#gammaZ", draw="e") if merge: h_nunuPart = getHisto(nunuTree, plot, cut + "&&genPhotons[0].pt>=130", nBins=nBins, color=6) h_zIncPart = getHisto(zIncTree, plot, cut + "&&genPhotons[0].pt<130", nBins=nBins, color=6) merged = addHistos([h_nunuPart, h_zIncPart]) mh.addHisto(merged, "merged", draw="e") mh.Draw() total = mh.stack.GetStack().Last().Clone(randomName()) total.SetFillStyle(3002) total.SetFillColor(1) total.Draw("same e2") if plot == "photons[0].pt": l = ROOT.TLine(130, 0, 130, 1) l.Draw() if merge: plot += "_merged" SavePad("zGammaCut_%s" % plot)