Пример #1
0
def plot(var, fileName, cut, weight, allSignal, allBkg):
    """ Creates a plot """

    # Creates empty list 'allVariables' and appends first passed argument to that list
    # 'allVariables.append(var)' replaces the following append statements at position 1 (~ ll. 76)
    allVariables = []
    allVariables.append(var)

    # Creating dictionaries of different variables with defined keys (But why is it commented out?)

    #amt2 = {'name':'myAmt2', 'fileName':fileName+'_amt2'+normString+logString, 'varStr':'amt2', 'Xtitle':'am_{T2}', 'Ytitle':'Events', 'binning':[30,0,600], 'binningIsExplicit':False}
    #met = {'name':'myMET', 'fileName':fileName+'_met'+normString+logString, 'varStr':'(met*0.001)', 'Xtitle':'E_{T}^{miss} [GeV]', 'Ytitle':'Events', 'binning':[30,0,600], 'binningIsExplicit':False}
    #dphi = {'name':'mydPhi', 'fileName':fileName+'_dphi'+normString+logString, 'varStr':'dphi_met_lep', 'Xtitle':'#Delta#phi(l, E_{T}^{miss})', 'Ytitle':'Events', 'binning':[40,0,3.2], 'binningIsExplicit':False}
    #mt = {'name':'myMT','fileName':fileName+'_mt'+normString+logString, 'varStr':'mt*0.001', 'Xtitle':'m_{T}', 'Ytitle':'Events', 'binning':[30,0,500], 'binningIsExplicit':False}
    #Q = {'name':'myQ','fileName':fileName+'_Q'+normString+logString, 'varStr':'1-mt*mt/(2*met*lep_pt[0])', 'Xtitle':'Q', 'Ytitle':'Events', 'binning':[30,-1,1], 'binningIsExplicit':False}
    #njet = {'name':'mynjet','fileName':fileName+'_njet'+normString+logString, 'varStr':'n_jet', 'Xtitle':'N jets', 'Ytitle':'Events', 'binning':[10,0,10], 'binningIsExplicit':False}
    #nbjet = {'name':'mynbjet','fileName':fileName+'_nbjet'+normString+logString, 'varStr':'n_bjet', 'Xtitle':'N bjets', 'Ytitle':'Events', 'binning':[10,0,10], 'binningIsExplicit':False}
    #jetpt = {'name':'myjetpT','fileName':fileName+'_jetpT'+normString+logString, 'varStr':'jet_pt*0.001', 'Xtitle':'p_{T}^{jet}', 'Ytitle':'Events', 'binning':[30,0,500], 'binningIsExplicit':False}
    #ht = {'name':'myht','fileName':fileName+'_hT'+normString+logString, 'varStr':'ht*0.001', 'Xtitle':'h_{T}', 'Ytitle':'Events', 'binning':[30,0,500], 'binningIsExplicit':False}
    #dphi_jet0_ptmiss = {'name':'mydPhi_jet0ptmiss', 'fileName':fileName+'_dphi_jet0_ptmiss'+normString+logString, 'varStr':'dphi_jet0_ptmiss', 'Xtitle':'#Delta#phi(jet0, p_{T}^{miss})', 'Ytitle':'Events', 'binning':[40,0,3.2], 'binningIsExplicit':False}
    #dphi_jet1_ptmiss = {'name':'mydPhi_jet1ptmiss', 'fileName':fileName+'_dphi_jet1_ptmiss'+normString+logString, 'varStr':'dphi_jet1_ptmiss', 'Xtitle':'#Delta#phi(jet1, p_{T}^{miss})', 'Ytitle':'Events', 'binning':[40,0,3.2], 'binningIsExplicit':False}
    #amm = {'name':'myamm','fileName':fileName+'_amm'+normString+logString, 'varStr':'(met*lep_pt[0])*(0.001*0.001)', 'Xtitle':'amm', 'Ytitle':'Events', 'binning':[30,0,500], 'binningIsExplicit':False}

    # Here is position 1

    #allVariables.append(met)
    #allVariables.append(dphi)
    #allVariables.append(amt2)
    #allVariables.append(mt)
    #allVariables.append(Q)
    #allVariables.append(njet)
    #allVariables.append(nbjet)
    #allVariables.append(jetpt)
    #allVariables.append(ht)
    #allVariables.append(dphi_jet0_ptmiss)
    #allVariables.append(dphi_jet1_ptmiss)
    #allVariables.append(amm)

    # Create empty dictionary of histograms
    histos = {}
    # Final structure of histos will be as following:
    # histos = {sample1_name: {variable 1: actual ROOT histogram, variable 2: actual ROOT histogram, ...}, sample2_name:{variable 1: actual ROOT histogram, ...}, ... }

    # Going through all entries of combined list of background and signal data
    # allBkg & allSignal are lists whose entries are dictionaries, therefore allBkg+allSignal is joined list
    # sample is one item of that joined list and it is a dictionary (see ~ ll. 230)
    for sample in allBkg + allSignal:

        # For every item in list do following:
        # Create a new key-value-pair: The key of this new pair is the value of "sample['name']" and its value is an empty dictionary
        histos[sample['name']] = {}

        # Now we are going through each variable "var" in list "allVariables"
        # At this point we are going thorugh each variable of each decay
        for var in allVariables:

            # Adds Key-Pair value with key "var['name']" and as a value it adds the ROOT histogram
            # Upper Key-Pair value is added as value to key sample['name']

            # Check if variable has an explicit binning value set to true
            if var.has_key('binningIsExplicit') and var['binningIsExplicit']:
                # Set explicit binning accoirding to settings, ROOT syntax applies
                histos[sample['name']][var['name']] = ROOT.TH1F(
                    sample['name'] + '_' + var['name'],
                    sample['name'] + '_' + var['name'],
                    len(var['binning']) - 1, array('d', var['binning']))

            # No explicit binning set
            else:
                histos[sample['name']][var['name']] = ROOT.TH1F(
                    sample['name'] + '_' + var['name'],
                    sample['name'] + '_' + var['name'], *var['binning'])

            # Check if variable has explicit cutt
            if sample.has_key('addCut'):
                cutString = cut + ' && ' + sample['addCut']
            else:
                cutString = cut

            # Draws value of key "chain"
            sample['chain'].Draw(
                var['varStr'] + '>>' +
                histos[sample['name']][var['name']].GetName(),
                '(' + weight + ') * (' + cutString + ')', 'goff')

    # Set Style for each variable
    for var in allVariables:
        canv = ROOT.TCanvas(var['name'] + '_Window', var['name'] + '_Window',
                            600, 500)
        if setRatioPlot or BgFractions:
            pad1 = ROOT.TPad('pad1', 'pad1', 0., 0.3, 1., 1.)
            pad1.SetBottomMargin(0.018)
            pad1.SetRightMargin(0.06)
        else:
            pad1 = ROOT.TPad('pad1', 'pad1', 0., 0., 1., 1.)
        if setLogY:
            pad1.SetLogy()
    pad1.Draw()
    pad1.cd()
    legend = ROOT.TLegend(0.7, 0.55, 0.92, 0.92)
    legend.SetFillColor(0)
    legend.SetBorderSize(0)
    legend.SetShadowColor(ROOT.kWhite)

    # Stacks histograms! Important for adding up background signal
    stack = ROOT.THStack('stack', 'Stacked Histograms')

    first = True
    bkgHists = []

    # Draw each background sample in histogram and stack values!
    for sample in allBkg:
        histos[sample['name']][var['name']].SetLineColor(ROOT.kBlack)
        histos[sample['name']][var['name']].SetLineWidth(2)
        histos[sample['name']][var['name']].SetFillColor(sample['color'])
        histos[sample['name']][var['name']].SetMarkerStyle(0)
        histos[sample['name']][var['name']].GetXaxis().SetTitle(var['Xtitle'])
        histos[sample['name']][var['name']].GetYaxis().SetTitle(
            var['Ytitle']
        )  # / '+ str( (var['binning'][2] - var['binning'][1])/var['binning'][0])+'GeV')
        if setRatioPlot or BgFractions:
            histos[sample['name']][var['name']].GetXaxis().SetLabelSize(0.0)
        #histos[sample['name']][var['name']].GetYaxis().SetLabelSize(0.04)
        stack.Add(histos[sample['name']][var['name']])
        legend.AddEntry(histos[sample['name']][var['name']],
                        sample['legendName'], 'f')
        bkgHists.append(histos[sample['name']][var['name']])

    stack.Draw('hist')
    stack.GetXaxis().SetTitle(var['Xtitle'])
    if setRatioPlot or BgFractions:
        stack.GetXaxis().SetLabelSize(0.0)
    stack.GetYaxis().SetTitle(
        var['Ytitle']
    )  # / '+ str( (var['binning'][2] - var['binning'][1])/var['binning'][0])+'GeV')

    # Check for logarithmic scale
    if setLogY:
        stack.SetMinimum(10**(-1))
        stack.SetMaximum(1000 * stack.GetMaximum())
    else:
        stack.SetMinimum(0.)
        stack.SetMaximum(1.7 * stack.GetMaximum())

    # Plotting signal in same histogram with set style. Do NOT stack!
    for sig in allSignal:
        histos[sig['name']][var['name']].SetLineColor(sig['color'])
        histos[sig['name']][var['name']].SetLineWidth(2)
        histos[sig['name']][var['name']].SetLineStyle(ROOT.kDashed)
        histos[sig['name']][var['name']].SetFillColor(0)
        histos[sig['name']][var['name']].SetMarkerStyle(0)
        if setRatioPlot or BgFractions:
            histos[sample['name']][var['name']].GetXaxis().SetLabelSize(0.0)

        # Trick for scaling up signal data and make it more visible, fakes higher luminosity for better visibility
        # Comment out if not needed.
        #if histos['ttbar1L'][var['name']].Integral()>0.:
        #    histos[sig['name']][var['name']].Scale(histos['ttbar1L'][var['name']].Integral()/histos[sig['name']][var['name']].Integral())

        # Draw
        histos[sig['name']][var['name']].Draw('hist same')
        legend.AddEntry(histos[sig['name']][var['name']], sig['legendName'])

    legend.Draw()

    canv.cd()

    # Check if ratio plot is true. If ratio plot is set, two diagrams will appear
    if setRatioPlot:
        helpers.ATLASLabelRatioPad(0.18, 0.88, 'Work in progress')
        helpers.ATLASLumiLabelRatioPad(0.18, 0.6, str(lumi * 0.001))
        pad2 = ROOT.TPad('pad2', 'pad2', 0., 0., 1., 0.3)
        pad2.SetTopMargin(0.01)
        pad2.SetBottomMargin(0.32)
        pad2.SetRightMargin(0.06)
        pad2.SetGrid()
        pad2.Draw()
        pad2.cd()

        ratio1 = doRatio(histos['ttbar2L'][var['name']],
                         histos['ttbar1L'][var['name']], var['Xtitle'])
        ratio1.Draw('hist')

    elif BgFractions:
        ATLASLabel(0.18, 0.9, 'Work in progress')
        ATLASLumiLabel(0.18, 0.895, str(lumi * 0.001))
        pad2 = ROOT.TPad('pad2', 'pad2', 0., 0., 1., 0.3)
        pad2.SetTopMargin(0.01)
        pad2.SetBottomMargin(0.32)
        pad2.SetRightMargin(0.06)
        pad2.SetGrid()
        pad2.Draw()
        pad2.cd()

        bkgFrac, sigFrac = doFractions(bkgHists,
                                       histos['stop_bWN_450_300'][var['name']],
                                       var['Xtitle'])
        bkgFrac.Draw('hist')
        sigFrac.Draw('hist same')
    else:
        pad1.cd()
        ATLASLabel(0.18, 0.89, 'Work in progress')
        ATLASLumiLabel(0.18, 0.88, str(lumi * 0.001))

    # Creating files
    canv.cd()
    canv.Print(wwwDir + var['fileName'] + '.pdf')
    canv.Print(wwwDir + var['fileName'] + '.root')
    canv.Print(wwwDir + var['fileName'] + '.png')
    canv.Close()
Пример #2
0
  for sig in allSignal:
    histos[sig['name']][var['name']].SetLineColor(sig['color'])
    histos[sig['name']][var['name']].SetLineWidth(2)
    histos[sig['name']][var['name']].SetLineStyle(ROOT.kDashed)
    histos[sig['name']][var['name']].SetFillColor(0)
    histos[sig['name']][var['name']].SetMarkerStyle(0)
    histos[sig['name']][var['name']].Draw('hist same')
    legend.AddEntry(histos[sig['name']][var['name']], sig['legendName'])
                        
  legend.Draw()

  canv.cd()

  if setRatioPlot:
    helpers.ATLASLabelRatioPad(0.18,0.88,"Work in progress")
    helpers.ATLASLumiLabelRatioPad(0.18,0.6, str(lumi*0.001))
    pad2 = ROOT.TPad("pad2","pad2",0.,0.,1.,0.3)
    pad2.SetTopMargin(0.01)
    pad2.SetBottomMargin(0.3)
    pad2.SetGrid()
    pad2.Draw()
    pad2.cd()
  
    ratio1 = doRatio(histos['ttbar2L'][var['name']],histos['ttbar1L'][var['name']],var['Xtitle'])
    ratio1.Draw("hist")
  else:
    pad1.cd()
    ATLASLabel(0.18,0.88,"Work in progress")
    ATLASLumiLabel(0.18,0.86, str(lumi*0.001))
Пример #3
0
def plot(var, fileName, cut, weight, allSignal, allBkg):
    """ Creates a plot """

    histos = {}

    sig = []
    sig_var = []
    first = True
    for sample in allBkg + allSignal:

        print sample['name']

        histos[sample['name']] = {}

        histos[sample['name']][var['name']] = ROOT.TH1F(
            sample['name'] + '_' + var['name'],
            sample['name'] + '_' + var['name'], *var['binning'])

        # Check if variable has explicit cutt
        if sample.has_key('addCut'):
            cutString = cut + ' && ' + sample['addCut']
        else:
            cutString = cut

        # Draws value of key "chain"
        sample['chain'].Draw(
            var['varStr'] + '>>' +
            histos[sample['name']][var['name']].GetName(),
            '(' + weight + ') * (' + cutString + ')', 'goff')

        nbins = histos[sample['name']][var['name']].GetNbinsX()

        if not sample.has_key("isSignal") and first:
            totBkg = nbins * [0.]
            totBkg_var = nbins * [0.]
            first = False

        for i in range(nbins):
            y = histos[sample['name']][var['name']].GetBinContent(i + 1)
            y_err = histos[sample['name']][var['name']].GetBinError(i + 1)
            y_var = y_err * y_err

            if sample.has_key("isSignal") and sample['isSignal']:
                sig.append(y)
                sig_var.append(y_var)

            else:
                totBkg[i] = totBkg[i] + y
                totBkg_var[i] = totBkg_var[i] + y_var

    sig = np.array(sig)
    sig_var = np.array(sig_var)
    totBkg = np.array(totBkg)
    totBkg_var = np.array(totBkg_var)
    rebinned = outputTransform(sig, sig_var, totBkg, totBkg_var,
                               var['binning'])

    # Redraw the hists with variable bin width
    for sample in allBkg + allSignal:

        histos[sample['name']][var['name']] = ROOT.TH1F(
            sample['name'] + '_' + var['name'],
            sample['name'] + '_' + var['name'],
            len(rebinned) - 1, array('d', rebinned))

        # Check if variable has explicit cutt
        if sample.has_key('addCut'):
            cutString = cut + ' && ' + sample['addCut']
        else:
            cutString = cut

        # Draws value of key "chain"
        sample['chain'].Draw(
            var['varStr'] + '>>' +
            histos[sample['name']][var['name']].GetName(),
            '(' + weight + ') * (' + cutString + ')', 'goff')

    canv = ROOT.TCanvas(var['name'] + '_Window', var['name'] + '_Window', 600,
                        500)
    if setRatioPlot or BgFractions:
        pad1 = ROOT.TPad('pad1', 'pad1', 0., 0.3, 1., 1.)
        pad1.SetBottomMargin(0.018)
        pad1.SetRightMargin(0.06)
        pad1.SetTopMargin(0.06)
    else:
        pad1 = ROOT.TPad('pad1', 'pad1', 0., 0., 1., 1.)
    if setLogY:
        pad1.SetLogy()
    pad1.Draw()
    pad1.cd()
    legend = ROOT.TLegend(0.7, 0.5, 0.9, 0.9)
    legend.SetFillColor(0)
    legend.SetBorderSize(0)
    legend.SetShadowColor(ROOT.kWhite)

    # Stacks histograms! Important for adding up background signal
    stack = ROOT.THStack('stack', 'Stacked Histograms')

    first = True
    bkgHists = []

    # Draw each background sample in histogram and stack values!
    for sample in allBkg:
        histos[sample['name']][var['name']].SetLineColor(ROOT.kBlack)
        histos[sample['name']][var['name']].SetLineWidth(2)
        histos[sample['name']][var['name']].SetFillColor(sample['color'])
        histos[sample['name']][var['name']].SetMarkerStyle(0)
        histos[sample['name']][var['name']].GetXaxis().SetTitle(var['Xtitle'])
        histos[sample['name']][var['name']].GetYaxis().SetTitle(
            var['Ytitle']
        )  # / '+ str( (var['binning'][2] - var['binning'][1])/var['binning'][0])+'GeV')
        if setRatioPlot or BgFractions:
            histos[sample['name']][var['name']].GetXaxis().SetLabelSize(0.0)
        #histos[sample['name']][var['name']].GetYaxis().SetLabelSize(0.04)
        stack.Add(histos[sample['name']][var['name']])
        legend.AddEntry(histos[sample['name']][var['name']],
                        sample['legendName'], 'f')
        bkgHists.append(histos[sample['name']][var['name']])

    stack.Draw('hist')
    stack.GetXaxis().SetTitle(var['Xtitle'])
    if setRatioPlot or BgFractions:
        stack.GetXaxis().SetLabelSize(0.0)
    stack.GetYaxis().SetTitle(
        var['Ytitle']
    )  # / '+ str( (var['binning'][2] - var['binning'][1])/var['binning'][0])+'GeV')

    # Check for logarithmic scale
    if setLogY:
        stack.SetMinimum(10**(-2))
        stack.SetMaximum(100 * stack.GetMaximum())
    else:
        stack.SetMinimum(0.)
        stack.SetMaximum(1.7 * stack.GetMaximum())

    # Plotting signal in same histogram with set style. Do NOT stack!
    for sig in allSignal:
        histos[sig['name']][var['name']].SetLineColor(sig['color'])
        histos[sig['name']][var['name']].SetLineWidth(2)
        histos[sig['name']][var['name']].SetLineStyle(ROOT.kDashed)
        histos[sig['name']][var['name']].SetFillColor(0)
        histos[sig['name']][var['name']].SetMarkerStyle(0)
        if setRatioPlot or BgFractions:
            histos[sample['name']][var['name']].GetXaxis().SetLabelSize(0.0)

        # Trick for scaling up signal data and make it more visible, fakes higher luminosity for better visibility
        # Comment out if not needed.
        #if histos['ttbar1L'][var['name']].Integral()>0.:
        #    histos[sig['name']][var['name']].Scale(histos['ttbar1L'][var['name']].Integral()/histos[sig['name']][var['name']].Integral())

        # Draw
        histos[sig['name']][var['name']].Draw('hist same')
        legend.AddEntry(histos[sig['name']][var['name']], sig['legendName'])

    legend.Draw()

    canv.cd()

    # Check if ratio plot is true. If ratio plot is set, two diagrams will appear
    if setRatioPlot:
        helpers.ATLASLabelRatioPad(0.18, 0.89, 'Work in progress')
        helpers.ATLASLumiLabelRatioPad(0.18, 0.88, str(lumi * 0.001))
        pad2 = ROOT.TPad('pad2', 'pad2', 0., 0., 1., 0.3)
        pad2.SetTopMargin(0.01)
        pad2.SetBottomMargin(0.32)
        pad2.SetRightMargin(0.06)
        pad2.SetGrid()
        pad2.Draw()
        pad2.cd()

        ratio1 = doRatio(histos['ttbar2L'][var['name']],
                         histos['ttbar1L'][var['name']], var['Xtitle'])
        ratio1.Draw('hist')

    elif BgFractions:
        ATLASLabel(0.18, 0.88, 'Work in progress')
        ATLASLumiLabel(0.18, 0.86, str(lumi * 0.001))
        pad2 = ROOT.TPad('pad2', 'pad2', 0., 0., 1., 0.3)
        pad2.SetTopMargin(0.01)
        pad2.SetBottomMargin(0.32)
        pad2.SetRightMargin(0.06)
        pad2.SetGrid()
        pad2.Draw()
        pad2.cd()

        bkgFrac, sigFrac = doFractions(bkgHists,
                                       histos['stop_bWN_450_300'][var['name']],
                                       var['Xtitle'])
        bkgFrac.Draw('hist')
        sigFrac.Draw('hist same')
    else:
        pad1.cd()
        ATLASLabel(0.18, 0.88, 'Work in progress')
        ATLASLumiLabel(0.18, 0.86, str(lumi * 0.001))

    # Creating files
    canv.cd()
    canv.Print(wwwDir + var['fileName'] + '.pdf')
    canv.Print(wwwDir + var['fileName'] + '.root')
    canv.Print(wwwDir + var['fileName'] + '.png')
    canv.Close()