Exemplo n.º 1
0
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)
Exemplo n.º 2
0
def drawMCStack( treeName="photonTree" ):
	mh = Multihisto()
	mh.orderByIntegral = False

	datasetsToStack = [gz,tt,wjets,qcd,gjets]

	mh = fillMh( mh, datasetsToStack, treeName )

	can = ROOT.TCanvas()
	can.cd()
	mh.Draw()
	info = PlotCaption(treeName=treeName)
	info.Draw()
	allDatasetAbbr = ''.join([x.datasetAbbr for x in datasetsToStack ])
	plot = "met"
	SaveAs( can, "stackedHisto_%s_%s_%s"%(treeName, plot,allDatasetAbbr) )
Exemplo n.º 3
0
def drawMCStack(treeName="photonTree"):
    mh = Multihisto()
    mh.orderByIntegral = False

    datasetsToStack = [gz, tt, wjets, qcd, gjets]

    mh = fillMh(mh, datasetsToStack, treeName)

    can = ROOT.TCanvas()
    can.cd()
    mh.Draw()
    info = PlotCaption(treeName=treeName)
    info.Draw()
    allDatasetAbbr = ''.join([x.datasetAbbr for x in datasetsToStack])
    plot = "met"
    SaveAs(can, "stackedHisto_%s_%s_%s" % (treeName, plot, allDatasetAbbr))
Exemplo n.º 4
0
def finalDistributionMC( plot ):

	# Sample names
	treeVersion = 12
	g1 = "slimGJets_200_400_V03.%s_tree.root"%treeVersion
	g2 = "slimGJets_400_inf_V03.%s_tree.root"%treeVersion
	q1 = "slimQCD_250_500_V03.%s_tree.root"%treeVersion
	q2 = "slimQCD_500_1000_V03.%s_tree.root"%treeVersion
	q3 = "slimQCD_1000_inf_V03.%s_tree.root"%treeVersion

	tt = "slimTTJets_V03.%s_tree.root"%treeVersion
	w1 = "slimWJets_250_300_V03.%s_tree.root"%treeVersion
	w2 = "slimWJets_300_400_V03.%s_tree.root"%treeVersion
	w3 = "slimWJets_400_inf_V03.%s_tree.root"%treeVersion


	wg1 = "slimWGamma_50_130_V03.%s_tree.root"%treeVersion
	wg2 = "slimWGamma_130_inf_V03.%s_tree.root"%treeVersion
	tg = "slimTTGamma_V03.%s_tree.root"%treeVersion
	zgn = "slimZGammaNuNu_V03.%s_tree.root"%treeVersion
	data = [ g1, g2, q1, q2, q3, tt, w1, w2, w3, zgn, wg1,wg2,tg ]

	# 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]()"

	# Compute the weights:
	#weight2D = getMixedWeigthHisto( data, data, commonCut )
	#attachWeightsToFiles( data, weight2D, "foWeights" )
	#from qcdClosure import drawWeightHisto
	#drawWeightHisto( weight2D, "MC" )

	# Get Histograms
	dataHist = getHists( data, plot, commonCut )
	fgammaHist, fgammaWeightError = predictionHistos( data, plot, commonCut, modifyEmptyBins=False )

	egammaHist = multiDimFakeRate( data, plot, commonCut, isData=False )
	egammaHistsys = setRelativeUncertainty( egammaHist.Clone(randomName()), ewkUncertainty )

	fsrZ = getHists( [zgn], plot, commonCut )
	fsrZsys = setRelativeUncertainty( fsrZ, isrUncertaintyZ  )
	fsrW = getHists( [wg1,wg2], plot, commonCut )
	fsrWsys = setRelativeUncertainty( fsrW, isrUncertaintyW  )
	fsrT = getHists( [tg], plot, commonCut )
	fsrTsys = setRelativeUncertainty( fsrT, isrUncertaintyT  )
	fsr = addHistos( [fsrT, fsrW, fsrZ ] )
	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.orderByIntegral = False
	mh.addHisto( fsrT, "#gamma t#bar{t}", True )
	mh.addHisto( fsrW, "#gamma W", True )
	mh.addHisto( fsrZ, "#gamma Z", True )
	mh.addHisto( egammaHist, "e#rightarrow#gamma", True )
	mh.addHisto( fgammaHist, "Multijet", True )
	dataLegName = "Data"
	mh.addHisto( dataHist, dataLegName, draw="pe" )
	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( fsrTsys )
	#systematicUncertHistStack.Add( fsrWsys )
	#systematicUncertHistStack.Add( fsrZsys )
	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()
	errorBand = mh.stack.GetStack().Last().Clone( randomName() )
	sysUncert = systematicUncertHistStack.GetStack().Last()
	for bin in range(errorBand.GetNbinsX()+2):
		errorBand.SetBinError( bin, errorBand.GetBinError(bin) |qPlus| sysUncert.GetBinError(bin) )
	errorBand.SetFillStyle(3002)
	errorBand.SetMarkerSize(0)
	errorBand.SetFillColor(1)
	errorBand.Draw("e2 same")

	from myRatio import Ratio
	r = Ratio( "Data / Bkg", dataHist, mh.stack.GetStack().Last(), systematicUncertHistStack.GetStack().Last() )
	r.draw(0.5,1.5)

	infoText.Draw()

	SaveAs( can, "finalDistributionData_%s"%plot )
Exemplo n.º 5
0
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) )
Exemplo n.º 6
0
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))
Exemplo n.º 7
0
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)
Exemplo n.º 8
0
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)
Exemplo n.º 9
0
def finalDistributionMC(plot):

    # Sample names
    treeVersion = 12
    g1 = "slimGJets_200_400_V03.%s_tree.root" % treeVersion
    g2 = "slimGJets_400_inf_V03.%s_tree.root" % treeVersion
    q1 = "slimQCD_250_500_V03.%s_tree.root" % treeVersion
    q2 = "slimQCD_500_1000_V03.%s_tree.root" % treeVersion
    q3 = "slimQCD_1000_inf_V03.%s_tree.root" % treeVersion

    tt = "slimTTJets_V03.%s_tree.root" % treeVersion
    w1 = "slimWJets_250_300_V03.%s_tree.root" % treeVersion
    w2 = "slimWJets_300_400_V03.%s_tree.root" % treeVersion
    w3 = "slimWJets_400_inf_V03.%s_tree.root" % treeVersion

    wg1 = "slimWGamma_50_130_V03.%s_tree.root" % treeVersion
    wg2 = "slimWGamma_130_inf_V03.%s_tree.root" % treeVersion
    tg = "slimTTGamma_V03.%s_tree.root" % treeVersion
    zgn = "slimZGammaNuNu_V03.%s_tree.root" % treeVersion
    data = [g1, g2, q1, q2, q3, tt, w1, w2, w3, zgn, wg1, wg2, tg]

    # 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]()"

    # Compute the weights:
    #weight2D = getMixedWeigthHisto( data, data, commonCut )
    #attachWeightsToFiles( data, weight2D, "foWeights" )
    #from qcdClosure import drawWeightHisto
    #drawWeightHisto( weight2D, "MC" )

    # Get Histograms
    dataHist = getHists(data, plot, commonCut)
    fgammaHist, fgammaWeightError = predictionHistos(data,
                                                     plot,
                                                     commonCut,
                                                     modifyEmptyBins=False)

    egammaHist = multiDimFakeRate(data, plot, commonCut, isData=False)
    egammaHistsys = setRelativeUncertainty(egammaHist.Clone(randomName()),
                                           ewkUncertainty)

    fsrZ = getHists([zgn], plot, commonCut)
    fsrZsys = setRelativeUncertainty(fsrZ, isrUncertaintyZ)
    fsrW = getHists([wg1, wg2], plot, commonCut)
    fsrWsys = setRelativeUncertainty(fsrW, isrUncertaintyW)
    fsrT = getHists([tg], plot, commonCut)
    fsrTsys = setRelativeUncertainty(fsrT, isrUncertaintyT)
    fsr = addHistos([fsrT, fsrW, fsrZ])
    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.orderByIntegral = False
    mh.addHisto(fsrT, "#gamma t#bar{t}", True)
    mh.addHisto(fsrW, "#gamma W", True)
    mh.addHisto(fsrZ, "#gamma Z", True)
    mh.addHisto(egammaHist, "e#rightarrow#gamma", True)
    mh.addHisto(fgammaHist, "Multijet", True)
    dataLegName = "Data"
    mh.addHisto(dataHist, dataLegName, draw="pe")
    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( fsrTsys )
    #systematicUncertHistStack.Add( fsrWsys )
    #systematicUncertHistStack.Add( fsrZsys )
    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()
    errorBand = mh.stack.GetStack().Last().Clone(randomName())
    sysUncert = systematicUncertHistStack.GetStack().Last()
    for bin in range(errorBand.GetNbinsX() + 2):
        errorBand.SetBinError(
            bin,
            errorBand.GetBinError(bin) | qPlus | sysUncert.GetBinError(bin))
    errorBand.SetFillStyle(3002)
    errorBand.SetMarkerSize(0)
    errorBand.SetFillColor(1)
    errorBand.Draw("e2 same")

    from myRatio import Ratio
    r = Ratio("Data / Bkg", dataHist,
              mh.stack.GetStack().Last(),
              systematicUncertHistStack.GetStack().Last())
    r.draw(0.5, 1.5)

    infoText.Draw()

    SaveAs(can, "finalDistributionData_%s" % plot)
Exemplo n.º 10
0
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) )
Exemplo n.º 11
0
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 )