コード例 #1
0
    def legend4Plot(plot, left = False):
        if left:
            theLeg = TLegend(0.2, 0.62, 0.55, 0.92, "", "NDC")
        else:
            theLeg = TLegend(0.60, 0.62, 0.92, 0.92, "", "NDC")
        theLeg.SetName('theLegend')

        theLeg.SetBorderSize(0)
        theLeg.SetLineColor(0)
        theLeg.SetFillColor(0)
        theLeg.SetFillStyle(0)
        theLeg.SetLineWidth(0)
        theLeg.SetLineStyle(0)
        theLeg.SetTextFont(42)
        theLeg.SetTextSize(.045)

        entryCnt = 0
        for obj in range(0, int(plot.numItems())):
            objName = plot.nameOf(obj)
            if (not plot.getInvisible(objName)):
                theObj = plot.getObject(obj)
                objTitle = theObj.GetTitle()
                if len(objTitle) < 1:
                    objTitle = objName
                dopts = plot.getDrawOptions(objName).Data()
                # print 'obj:',theObj,'title:',objTitle,'opts:',dopts,'type:',type(dopts)
                if theObj.IsA().InheritsFrom('TNamed'):
                    theLeg.AddEntry(theObj, objTitle, dopts)
                    entryCnt += 1
        theLeg.SetY1NDC(0.9 - 0.05*entryCnt - 0.005)
        theLeg.SetY1(theLeg.GetY1NDC())
        return theLeg
コード例 #2
0
ファイル: plot.py プロジェクト: zucchett/SFrame
def addSys(var, cut, sys):
    binLow = ""
    binHigh = ""
    binName = ""
    if "binned" in cut:
        binLow = cut[cut.find("LowVal") + 6:cut.find("HighVal") - 1]
        binHigh = cut[cut.find("HighVal") + 7:]
        binName = "bin_" + binLow + "_" + binHigh
        cut = cut[:cut.find("binned")]

    channel = cut
    weight = "eventWeightLumi"  #+ ("*stitchWeight" if any([x for x in back if x.endswith('b')]) else "")
    cut = selection[cut]
    if not binLow == "":
        cut = cut + " && " + var + " > " + binLow + " && " + var + " < " + binHigh

    weightUp = weightDown = weight
    varUp = varDown = var
    cutUp = cutDown = cut

    # Systematics
    if sys == 'CMS_scale_j':
        if var != "MET_sign": varUp = var.replace('pt', 'ptScaleUp')
        else: varUp = var.replace('sign', 'signScaleUp')
        if var != "MET_sign": varDown = var.replace('pt', 'ptScaleDown')
        else: varDown = var.replace('sign', 'signScaleDown')

        cutUp = cut.replace('MET_pt', 'MET_ptScaleUp')
        cutUp = cutUp.replace('Jets', 'JetsScaleUp')
        cutUp = cutUp.replace('12', '12ScaleUp')
        cutUp = cutUp.replace('mT>', 'mTScaleUp>')
        cutUp = cutUp.replace('mT2', 'mT2ScaleUp')

        cutDown = cut.replace('MET_pt', 'MET_ptScaleDown')
        cutDown = cutDown.replace('Jets', 'JetsScaleDown')
        cutDown = cutDown.replace('12', '12ScaleDown')
        cutDown = cutDown.replace('mT>', 'mTScaleDown>')
        cutDown = cutDown.replace('mT2', 'mT2ScaleDown')
    elif sys == 'CMS_res_j':
        if var != "MET_sign": varUp = var.replace('pt', 'ptResUp')
        else: varUp = var.replace('sign', 'signResUp')
        if var != "MET_sign": varDown = var.replace('pt', 'ptResDown')
        else: varDown = var.replace('sign', 'signResDown')

        cutUp = cut.replace('MET_pt', 'MET_ptResUp')
        cutUp = cutUp.replace('Jets', 'JetsResUp')
        cutUp = cutUp.replace('12', '12ResUp')
        cutUp = cutUp.replace('mT>', 'mTResUp>')
        cutUp = cutUp.replace('mT2', 'mT2ResUp')
        cutDown = cut.replace('MET_pt', 'MET_ptResDown')
        cutDown = cutDown.replace('Jets', 'JetsResDown')
        cutDown = cutDown.replace('12', '12ResDown')
        cutDown = cutDown.replace('mT>', 'mTResDown>')
        cutDown = cutDown.replace('mT2', 'mT2ResDown')

    elif sys == 'CMS_WqcdWeightRen':
        weightUp += "*WqcdWeightRenUp/WqcdWeight"
        weightDown += "*WqcdWeightRenDown/WqcdWeight"
    elif sys == 'CMS_WqcdWeightFac':
        weightUp += "*WqcdWeightFacUp/WqcdWeight"
        weightDown += "*WqcdWeightFacDown/WqcdWeight"
    elif sys == 'CMS_ZqcdWeightRen':
        weightUp += "*ZqcdWeightRenUp/ZqcdWeight"
        weightDown += "*ZqcdWeightRenDown/ZqcdWeight"
    elif sys == 'CMS_ZqcdWeightFac':
        weightUp += "*ZqcdWeightFacUp/ZqcdWeight"
        weightDown += "*ZqcdWeightFacDown/ZqcdWeight"
    elif sys == 'CMS_WewkWeight':
        weightUp += "/WewkWeight"
        weightDown += ""
    elif sys == 'CMS_ZewkWeight':
        weightUp += "/ZewkWeight"
        weightDown += ""

    elif sys == 'CMS_pdf':
        weightUp += "*PDFWeightUp/eventWeight"
        weightDown += "*PDFWeightDown/eventWeight"
    elif sys == 'CMS_HF':
        weightUp += "*1.20"
        weightDown += "*0.8"

    elif sys == 'CMS_eff_b':
        weightUp += "*bTagWeightUp/bTagWeight"
        weightDown += "*bTagWeightDown/bTagWeight"
    elif sys == 'CMS_scale_pu':
        weightUp += "*puWeightUp/puWeight"
        weightDown += "*puWeightDown/puWeight"
    elif sys == 'CMS_scale_top':
        weightUp += "/TopWeight"
        weightDown += ""
    elif sys == 'CMS_eff_trigger':
        weightUp += "*triggerWeightUp/triggerWeight"
        weightDown += "*triggerWeightDown/triggerWeight"
    elif sys == 'CMS_eff_e' and '2e' in cut or '1e' in channel:
        weightUp += "*leptonWeightUp/leptonWeight"
        weightDown += "*leptonWeightDown/leptonW\
eight"

    elif sys == 'CMS_eff_m' and '2m' in cut or '1m' in channel:
        weightUp += "*leptonWeightUp/leptonWeight"
        weightDown += "*leptonWeightDown/leptonW\
eight"

    elif sys == 'QCDscale_ren':
        weightUp += "*QCDRenWeightUp"
        weightDown += "*QCDRenWeightDown"
    elif sys == 'QCDscale_fac':
        weightUp += "*QCDFacWeightUp"
        weightDown += "*QCDFacWeightDown"
        # elif sys=='EWKscale_Z': weightDown += "/ZewkWeight"
        # elif sys=='EWKscale_W': weightDown += "/WewkWeight"
    else:
        print "Systematic", sys, "not applicable or not recognized."
    ### Create and fill MC histograms ###
    file = {}
    tree = {}
    hist = {}
    histUp = {}
    histDown = {}
    isBlind = BLIND and 'SR' in channel
    for i, s in enumerate(back + sign):
        tree[s] = TChain("tree")
        for j, ss in enumerate(sample[s]['files']):
            tree[s].Add(NTUPLEDIR + ss + ".root")
        if not binLow == "":
            hist[s] = TH1F(
                s, ";" + variable[var]['title'] + ";Events;" +
                ('log' if variable[var]['log'] else ''), 1, float(binLow),
                float(binHigh))
        elif binLow == "" and variable[var]['nbins'] > 0:
            hist[s] = TH1F(
                s, ";" + variable[var]['title'] + ";Events;" +
                ('log' if variable[var]['log'] else ''),
                variable[var]['nbins'], variable[var]['min'],
                variable[var]['max'])
        else:
            hist[s] = TH1F(s, ";" + variable[var]['title'],
                           len(variable[var]['bins']) - 1,
                           array('f', variable[var]['bins']))
        hist[s].Sumw2()
        histUp[s] = hist[s].Clone(s + 'Up')
        histDown[s] = hist[s].Clone(s + 'Down')
        redFactorString = ""
        redFactorValue = ""
        #if isBlind and 'data' not in s and options.limit:
        if isBlind and 'data' not in s:
            redFactorValue = " / 15"
        cutstring = ("*(" + cut + ")" if len(cut) > 0 else "")
        cutstringUp = ("*(" + cutUp + ")" if len(cut) > 0 else "")
        cutstringDown = ("*(" + cutDown + ")" if len(cut) > 0 else "")
        if '-' in s:
            cutstring = cutstring.replace(
                cut, cut + "&& nBQuarks==" + s.split('-')[1][0])
        tree[s].Project(s, var,
                        "(" + weight + redFactorValue + ")" + cutstring)

        if 'HF' not in sys or 'QCDscale' not in sys:
            tree[s].Project(s + 'Up', varUp,
                            "(" + weightUp + redFactorValue + ")" + cutstring)
            tree[s].Project(
                s + 'Down', varDown,
                "(" + weightDown + redFactorValue + ")" + cutstring)

        if 'HF' in sys:
            if s.startswith('WJ') or s.startswith('ZJ') or s.startswith(
                    'DYJets'):
                tree[s].Project(
                    s + 'Up', varUp,
                    "(" + weightUp + redFactorValue + ")" + cutstringUp)
                tree[s].Project(
                    s + 'Down', varDown,
                    "(" + weightDown + redFactorValue + ")" + cutstringDown)
            else:
                tree[s].Project(
                    s + 'Up', varUp,
                    "(" + weight + redFactorValue + ")" + cutstringUp)
                tree[s].Project(
                    s + 'Down', varDown,
                    "(" + weight + redFactorValue + ")" + cutstringDown)

        if 'QCDscale' in sys:
            if s.startswith('WJ') or s.startswith('ZJ') or s.startswith(
                    'DYJets'):
                tree[s].Project(
                    s + 'Up', varUp,
                    "(" + weight + redFactorValue + ")" + cutstringUp)
                tree[s].Project(
                    s + 'Down', varDown,
                    "(" + weight + redFactorValue + ")" + cutstringDown)
            else:
                tree[s].Project(
                    s + 'Up', varUp,
                    "(" + weightUp + redFactorValue + ")" + cutstringUp)
                tree[s].Project(
                    s + 'Down', varDown,
                    "(" + weightDown + redFactorValue + ")" + cutstringDown)

        hist[s].Scale(sample[s]['weight'] if hist[s].Integral() >= 0 else 0)
        hist[s].SetLineWidth(2)
        histUp[s].SetLineWidth(2)
        histDown[s].SetLineWidth(2)
        hist[s].SetLineColor(1)
        histUp[s].SetLineColor(629)
        histDown[s].SetLineColor(602)

    # Rescale normalization for QCD scales FIXME
    if 'QCDscale' in sys:
        for s in back + sign:  #['TTbar', 'TTbarSL', 'ST']:
            if s in hist and histUp[s].Integral(
            ) > 0. and histDown[s].Integral() > 0.:
                histUp[s].Scale(hist[s].Integral() / histUp[s].Integral())
                histDown[s].Scale(hist[s].Integral() / histDown[s].Integral())

    hist['BkgSum'] = hist[back[0]].Clone("BkgSum")
    hist['BkgSum'].Reset()
    histUp['BkgSum'] = hist['BkgSum'].Clone("BkgSumUp")
    histUp['BkgSum'].SetLineColor(629)
    histUp['BkgSum'].Reset()
    histDown['BkgSum'] = hist['BkgSum'].Clone("BkgSumDown")
    histDown['BkgSum'].SetLineColor(602)
    histDown['BkgSum'].Reset()

    for i, s in enumerate(back):
        hist['BkgSum'].Add(hist[s], 1)
        histUp['BkgSum'].Add(histUp[s], 1)
        histDown['BkgSum'].Add(histDown[s], 1)

    for i, s in enumerate(back + sign + ['BkgSum']):
        addOverflow(hist[s], False)
        addOverflow(histUp[s], False)
        addOverflow(histDown[s], False)

    c1 = TCanvas("c1", "Signals", 800, 600)
    c1.cd()
    gStyle.SetOptStat(0)
    gStyle.SetOptTitle(0)

    if RATIO:
        c1.Divide(1, 2)
        setTopPad(c1.GetPad(1), RATIO)
        setBotPad(c1.GetPad(2), RATIO)

    c1.cd(1)
    c1.GetPad(bool(RATIO)).SetTopMargin(0.06)
    c1.GetPad(bool(RATIO)).SetRightMargin(0.06)
    c1.GetPad(bool(RATIO)).SetTicks(1, 1)
    c1.GetPad(bool(RATIO)).SetLogy()

    histUp['BkgSum'].SetMaximum(histUp['BkgSum'].GetMaximum() * 5)
    histUp['BkgSum'].Draw("HIST")
    histDown['BkgSum'].Draw("SAME, HIST")
    hist['BkgSum'].Draw("SAME, HIST")
    drawCMS(-1, "Simulation", False)

    setHistStyle(histUp['BkgSum'], 1.2 if RATIO else 1.1)

    if RATIO:
        c1.cd(2)
        errUp = histUp['BkgSum'].Clone("BkgUp;")
        errUp.Add(hist['BkgSum'], -1)
        errUp.Divide(hist['BkgSum'])
        errUp.SetTitle("")
        errUp.GetYaxis().SetTitle("#frac{shifted-central}{central}")
        errUp.GetYaxis().SetNdivisions(503)
        setBotStyle(errUp)
        errUp.GetYaxis().SetRangeUser(-0.3, 0.3)
        errUp.Draw("HIST")

        errDown = histDown['BkgSum'].Clone("BkgDown;")
        errDown.Add(hist['BkgSum'], -1)
        errDown.Divide(hist['BkgSum'])
        errDown.Draw("SAME, HIST")

        f1 = TF1("myfunc", "[0]", -100000, 10000)
        f1.SetLineColor(1)
        f1.SetLineStyle(7)
        f1.SetLineWidth(1)
        f1.SetParameter(0, 0)
        f1.Draw("same")

    leg = TLegend(0.65, 0.80, 0.95, 0.80)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetHeader(sys.replace('CMS', '').replace('_', ' '))
    leg.AddEntry(histUp['BkgSum'], "Up", "l")
    leg.AddEntry(hist['BkgSum'], "Central", "l")
    leg.AddEntry(histDown['BkgSum'], "Down", "l")
    leg.SetY1(0.75 - leg.GetNRows() * 0.045)
    c1.cd(1)
    leg.Draw()

    if options.saveplots:
        if not os.path.exists("plotsSys_" + options.name + "/" + channel +
                              binName):
            os.makedirs("plotsSys_" + options.name + "/" + channel + binName)
        c1.Print("plotsSys_" + options.name + "/" + channel + binName + "/" +
                 sys + ".png")
        c1.Print("plotsSys_" + options.name + "/" + channel + binName + "/" +
                 sys + ".pdf")

    for i, s in enumerate(back + sign):
        c2 = TCanvas(s + "canvas", "Signals", 800, 600)
        c2.cd()
        gStyle.SetOptStat(0)
        gStyle.SetOptTitle(0)
        c2.GetPad(0).SetTopMargin(0.06)
        c2.GetPad(0).SetRightMargin(0.06)
        c2.GetPad(0).SetTicky(2)
        c2.GetPad(0).SetLogy()
        histUp[s].SetMaximum(histUp[s].GetMaximum() * 5)
        histUp[s].Draw("HIST")
        histDown[s].Draw("SAME, HIST")
        hist[s].Draw("SAME, HIST")
        drawCMS(-1, "Simulation", False)
        if options.saveplots:
            c2.Print("plotsSys_" + options.name + "/" + channel + binName +
                     "/" + sys + "_" + s + ".png")
            c2.Print("plotsSys_" + options.name + "/" + channel + binName +
                     "/" + sys + "_" + s + ".pdf")

    saveHist(histUp, channel + binName, sys + 'Up')
    saveHist(histDown, channel + binName, sys + 'Down')

    print "Added systematic", sys, "to channel", channel
コード例 #3
0
ファイル: plot.py プロジェクト: zucchett/SFrame
def plot(var, cut, norm=False, nm1=False):
    ### Preliminary Operations ###
    doBinned = False
    if options.mode == "binned": doBinned = True

    fileRead = os.path.exists("combinedCards_" + options.name +
                              "/fitDiagnostics_" + options.file + ".root")
    treeRead = not any(
        x == cut
        for x in ['0l', '1e', '1m', '2e', '2m', '1e1m', 'Gen', 'Trigger'
                  ])  #(var in variable.keys()) # Read from tree

    #signal definition
    if fileRead:
        sign = ['ttDM_MChi1_MPhi200_scalar',
                'tDM_MChi1_MPhi200_scalar']  #for postfit plot
    if not fileRead and not options.limit:
        sign = ['ttDM_MChi1_MPhi100_scalar',
                'tDM_MChi1_MPhi100_scalar']  #for normal plotting
    #bkg definition
    if fileRead or options.limit:
        back = [
            "QCD", "DYJetsToNuNu_HT", "DYJetsToLL_HT", "VV", "ST",
            "WJetsToLNu_HT", "TTbarSL"
        ]  #for postfit or limit
    if (cut).find('>250') or (cut).startswith('AH'):  #for hadronic selections
        back = [
            "QCD", "DYJetsToLL_HT", "VV", "ST", "WJetsToLNu_HT", "TTbarV",
            "TTbar2L", "TTbar1L", "DYJetsToNuNu_HT"
        ]
        if fileRead or options.limit:
            back = [
                "QCD", "DYJetsToLL_HT", "VV", "ST", "WJetsToLNu_HT", "TTbarSL",
                "DYJetsToNuNu_HT"
            ]  #for postfit or limit

    binLow = ""
    binHigh = ""
    binName = ""
    if "binned" in cut:
        binLow = cut[cut.find("LowVal") + 6:cut.find("HighVal") - 1]
        binHigh = cut[cut.find("HighVal") + 7:]
        binName = "bin_" + binLow + "_" + binHigh
        cut = cut[:cut.find("binned")]
    useformula = False
    if 'formula' in variable[var]:
        useformula = True
    channel = cut
    plotdir = cut
    plotname = var
    weight = "eventWeightLumi"  #*(2.2/35.9)
    isBlind = BLIND and ('SR' in channel or 'ps' in channel)
    if fileRead:
        isBlind = False
        options.saveplots = True
        RESIDUAL = True
    elif isBlind:
        RATIO = 0
        SIGNAL = 20
    else:
        RATIO = 4
        SIGNAL = 1
        RESIDUAL = False
    showSignal = True  #('SR' in channel)
    cutSplit = cut.split()
    for s in cutSplit:
        if s in selection.keys():
            plotdir = s
            cut = cut.replace(s, selection[s])
            if not binLow == "":
                cut = cut + " && " + var + " > " + binLow + " && " + var + " < " + binHigh
#if treeRead and cut in selection: cut  = cut.replace(cut, selection[cut])

# Determine Primary Dataset
    pd = []
    if any(w in cut
           for w in ['1l', '1m', '2m', 'isWtoMN', 'isZtoMM', 'isTtoEM']):
        pd += [x for x in sample['data_obs']['files'] if 'SingleMuon' in x]
    if any(w in cut for w in ['1l', '1e', '2e', 'isWtoEN', 'isZtoEE']):
        pd += [x for x in sample['data_obs']['files'] if 'SingleElectron' in x]
    if any(w in cut for w in ['0l', 'isZtoNN']):
        pd += [x for x in sample['data_obs']['files'] if 'MET' in x]
    if len(pd) == 0:
        raw_input("Warning: Primary Dataset not recognized, continue?")

    print "Plotting from", ("tree" if treeRead else
                            "file"), var, "in", channel, "channel with:"
    print "  dataset:", pd
    print "  cut    :", cut
    print "  cut    :", weight

    ### Create and fill MC histograms ###
    # Create dict
    file = {}
    tree = {}
    hist = {}

    ### Create and fill MC histograms ###
    for i, s in enumerate(data + back + sign):
        if fileRead:
            var = 'MET_pt'
            if channel.startswith('SL'): var = 'MET_sign'
            if channel.endswith('ZR'): var = 'FakeMET_pt'
            plotname = var

            hist[s] = TH1F(
                s, ";" + variable[var]['title'] + ";Events;" +
                ('log' if variable[var]['log'] else ''),
                variable[var]['nbins'], variable[var]['min'],
                variable[var]['max'])

            if doBinned:
                bins = np.array([])
                if 'bins' in variable[var].keys():
                    bins = np.array(variable[var]['bins'])
                else:
                    binsize = (variable[var]['max'] -
                               variable[var]['min']) / variable[var]['nbins']
                    bins = np.arange(variable[var]['min'],
                                     variable[var]['max'] + binsize, binsize)
                bins = np.append(bins, 10000)

                for i in range(0, len(bins) - 1):
                    rbin = str(bins[i]) + "_" + str(bins[i + 1])
                    fileName = "combinedCards_" + options.name + "/fitDiagnostics_" + options.file + ".root" if not any(
                        t in s for t in ['data', 'tDM']
                    ) else "rootfiles_" + options.name + "/" + channel + "bin_" + rbin + ".root"
                    histName = "shapes_fit_b/" + channel + "bin_" + rbin + "/" + s if not any(
                        t in s for t in ['data', 'tDM']) else s
                    file[s] = TFile(fileName, "READ")
                    tmphist = file[s].Get(histName)

                    if 'data' not in s: hist[s].SetMarkerSize(0)
                    if tmphist:
                        hist[s].SetBinContent(i + 1, tmphist.GetBinContent(1))
                        hist[s].SetBinError(i + 1, tmphist.GetBinError(1))
                    else:
                        hist[s].SetBinContent(i + 1, 0.)
                        hist[s].SetBinError(i + 1, 0.)
            else:
                fileName = "combinedCards_" + options.name + "/fitDiagnostics_" + options.file + ".root" if not s == 'data_obs' else "rootfiles_" + options.name + "/" + channel + binName + ".root"
                histName = "shapes_fit_b/" + channel + "/" + s if not s == 'data_obs' else s
                file[s] = TFile(fileName, "READ")
                tmphist = file[s].Get(histName)

                if tmphist == None:
                    tmphist = hist[back[0]].Clone(s)
                    tmphist.Reset("MICES")
                    print "Histogram", histName, "not found in file", fileName

                if s == 'data_obs': hist[s] = tmphist
                else:
                    hist[s] = hist['data_obs'].Clone(s)
                    hist[s].SetMarkerSize(0)
                    for i in range(tmphist.GetNbinsX() + 1):
                        hist[s].SetBinContent(i + 1,
                                              tmphist.GetBinContent(i + 1))

        elif treeRead:  # Project from tree
            tree[s] = TChain("tree")
            for j, ss in enumerate(sample[s]['files']):
                if not 'data' in s or ('data' in s and ss in pd):
                    tree[s].Add(NTUPLEDIR + ss + ".root")
            if not binLow == "":
                hist[s] = TH1F(
                    s, ";" + variable[var]['title'] + ";Events;" +
                    ('log' if variable[var]['log'] else ''), 1, float(binLow),
                    float(binHigh))
            elif binLow == "" and variable[var]['nbins'] > 0:
                hist[s] = TH1F(
                    s, ";" + variable[var]['title'] + ";Events;" +
                    ('log' if variable[var]['log'] else ''),
                    variable[var]['nbins'], variable[var]['min'],
                    variable[var]['max'])
            else:
                hist[s] = TH1F(
                    s, ";" + variable[var]['title'] + ";Events;" +
                    ('log' if variable[var]['log'] else ''),
                    len(variable[var]['bins']) - 1,
                    array('f', variable[var]['bins']))
            hist[s].Sumw2()
            redFactorString = ""
            redFactorValue = ""
            #if isBlind and 'data' in s:
            if isBlind and 'data' in s and options.limit:
                redFactorString = " && Entry$ % 15 == 1"
            #if isBlind and 'data' not in s:
            if isBlind and 'data' not in s and options.limit:
                redFactorValue = " / 15"
            cutstring = "(" + weight + redFactorValue + ")" + (
                "*(" + cut + redFactorString + ")" if len(cut) > 0 else "")
            if '-' in s:
                cutstring = cutstring.replace(
                    cut, cut + "&& nBQuarks==" + s.split('-')[1][0])
            if useformula == True:
                tree[s].Project(s, variable[var]['formula'], cutstring)
            else:
                tree[s].Project(s, var, cutstring)
            if not tree[s].GetTree() == None:
                hist[s].SetOption("%s" % tree[s].GetTree().GetEntriesFast())
        else:  # Histogram written to file
            for j, ss in enumerate(sample[s]['files']):
                if not 'data' in s or ('data' in s and ss in pd):
                    file[ss] = TFile(NTUPLEDIR + ss + ".root", "R")
                    if file[ss].IsZombie():
                        print "WARNING: file", NTUPLEDIR + ss + ".root", "does not exist"
                        continue
                    tmphist = file[ss].Get(cut + "/" + var)
                    if tmphist == None: continue
                    if not s in hist.keys(): hist[s] = tmphist
                    else: hist[s].Add(tmphist)
        if hist[s].Integral() < 0: hist[s].Scale(0)
        hist[s].SetFillColor(sample[s]['fillcolor'])
        hist[s].SetFillStyle(sample[s]['fillstyle'])
        hist[s].SetLineColor(sample[s]['linecolor'])
        hist[s].SetLineStyle(sample[s]['linestyle'])
        #if 'WJetsToLNu' in s and 'SL' in channel and 'WR' in channel: hist[s].Scale(1.30)
        #if 'TTbar' in s and 'SL' in channel and 'TR' in channel: hist[s].Scale(0.91)

    hist['BkgSum'] = hist[back[0]].Clone("BkgSum")
    hist['BkgSum'].Reset("MICES")
    for i, s in enumerate(back):
        hist['BkgSum'].Add(hist[s], 1)

    if fileRead:
        hist['PreFit'] = hist['BkgSum'].Clone("PreFit")
        if doBinned:
            for i in range(0, len(bins) - 1):
                rbin = str(bins[i]) + "_" + str(bins[i + 1])
                tmphist = file[back[0]].Get("shapes_prefit/" + channel +
                                            "bin_" + rbin + "/" +
                                            "total_background")

                if tmphist:
                    hist['PreFit'].SetBinContent(i + 1,
                                                 tmphist.GetBinContent(1))
                else:
                    hist['PreFit'].SetBinContent(i + 1, 0.)
        else:
            tmphist = file[back[0]].Get("shapes_prefit/" + channel + "/" +
                                        "total_background")
            for i in range(tmphist.GetNbinsX() + 1):
                hist['PreFit'].SetBinContent(i + 1,
                                             tmphist.GetBinContent(i + 1))
        addOverflow(hist['PreFit'], False)
        hist['PreFit'].SetLineStyle(2)
        hist['PreFit'].SetLineColor(617)  #923
        hist['PreFit'].SetLineWidth(3)
        hist['PreFit'].SetFillStyle(0)
    hist['BkgSum'].SetFillStyle(3002)
    hist['BkgSum'].SetFillColor(1)

    # Create data and Bkg sum histograms
    #    if options.blind or 'SR' in channel:
    #        hist['data_obs'] = hist['BkgSum'].Clone("data_obs")
    #        hist['data_obs'].Reset("MICES")
    # Set histogram style
    hist[data[0]].SetMarkerStyle(20)
    hist[data[0]].SetMarkerSize(1.25)

    for i, s in enumerate(data + back + sign + ['BkgSum']):
        addOverflow(hist[s], False)  # Add overflow
    for i, s in enumerate(sign):
        hist[s].SetLineWidth(3)
    #for i, s in enumerate(sign): sample[s]['plot'] = True#sample[s]['plot'] and s.startswith(channel[:2])

    if norm:
        for i, s in enumerate(sign):
            hist[s].Scale(hist['BkgSum'].Integral() / hist[s].Integral())
#        for i, s in enumerate(back):
#            hist[s].SetFillStyle(3005)
#            hist[s].SetLineWidth(2)
#        #for i, s in enumerate(sign):
#        #    hist[s].SetFillStyle(0)
#        if not var=="Events":
#            sfnorm = hist[data[0]].Integral()/hist['BkgSum'].Integral()
#            print "Applying SF:", sfnorm
#            for i, s in enumerate(back+['BkgSum']): hist[s].Scale(sfnorm)

    if SIGNAL > 1:
        if not var == "Events":
            for i, s in enumerate(sign):
                hist[s].Scale(SIGNAL)

    # Create stack
    bkg = THStack("Bkg",
                  ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events")
    for i, s in enumerate(back):
        bkg.Add(hist[s])

    # Legend
    #leg = TLegend(0.65, 0.6, 0.95, 0.9)
    leg = TLegend(0.45, 0.63, 0.93, 0.92)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    leg.SetNColumns(3)
    leg.SetTextFont(42)
    if len(data) > 0:
        leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pe")
    for i, s in reversed(list(enumerate(back))):
        leg.AddEntry(hist[s], sample[s]['label'], "f")
    if 'PreFit' not in hist:
        leg.AddEntry(hist['BkgSum'], sample['BkgSum']['label'], "f")
    else:
        leg.AddEntry(hist['BkgSum'], 'MC unc.', "l")
        leg.AddEntry(hist['PreFit'], sample['PreFit']['label'], "l")
    if showSignal:
        for i, s in enumerate(sign):
            if SIGNAL > 1:
                if sample[s]['plot']:
                    leg.AddEntry(hist[s],
                                 '%s (x%d)' % (sample[s]['label'], SIGNAL),
                                 "l")
            else:
                if sample[s]['plot']:
                    leg.AddEntry(hist[s], sample[s]['label'], "l")

    leg.SetY1(0.9 - leg.GetNRows() * 0.05)

    # --- Display ---
    c1 = TCanvas("c1",
                 hist.values()[0].GetXaxis().GetTitle(), 800,
                 800 if RATIO else 600)

    if RATIO:
        if RESIDUAL:
            c1.Divide(1, 3)
            setFitTopPad(c1.GetPad(1), RATIO)
            setFitBotPad(c1.GetPad(2), RATIO)
            setFitResPad(c1.GetPad(3), RATIO)
        else:
            c1.Divide(1, 2)
            setTopPad(c1.GetPad(1), RATIO)
            setBotPad(c1.GetPad(2), RATIO)
    c1.cd(1)
    c1.GetPad(bool(RATIO)).SetTopMargin(0.06)
    c1.GetPad(bool(RATIO)).SetRightMargin(0.05)
    c1.GetPad(bool(RATIO)).SetTicks(1, 1)

    log = ("log" in hist['BkgSum'].GetZaxis().GetTitle())
    if log: c1.GetPad(bool(RATIO)).SetLogy()

    # Draw
    bkg.Draw("HIST")  # stack
    hist['BkgSum'].Draw("SAME, E2")  # sum of bkg
    if not isBlind and len(data) > 0:
        graph = fixData(hist[data[0]], USEGARWOOD)
        graph.Draw("SAME, PE")
    #data_graph.Draw("SAME, PE")
    if 'PreFit' in hist: hist['PreFit'].Draw("SAME, HIST")
    if showSignal:
        for i, s in enumerate(sign):
            if sample[s]['plot']:
                hist[s].Draw(
                    "SAME, HIST"
                )  # signals Normalized, hist[s].Integral()*sample[s]['weight']
    bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset() * 1.075)
    bkg.SetMaximum((5. if log else 1.25) * max(
        bkg.GetMaximum(),
        hist[data[0]].GetBinContent(hist[data[0]].GetMaximumBin()) +
        hist[data[0]].GetBinError(hist[data[0]].GetMaximumBin())))
    if len(sign) > 0 and bkg.GetMaximum() < max(hist[sign[0]].GetMaximum(),
                                                hist[sign[-1]].GetMaximum()):
        bkg.SetMaximum(
            max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum()) *
            1.25)
    bkg.SetMinimum(
        max(
            min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin(
            )), hist[data[0]].GetMinimum()), 5.e-1) if log else 0.)
    if log:
        #bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4)
        bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e3)
        bkg.GetYaxis().SetMoreLogLabels(True)
    else:
        bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e3)

    leg.Draw()
    if fileRead and 'SR' in channel:
        drawCMS(LUMI / 15., "Preliminary")
    else:
        drawCMS(LUMI, "Preliminary")
    drawRegion(channel, True)
    drawAnalysis("DM" + channel[:2])
    drawOverflow()

    setHistStyle(bkg, 1.2 if RATIO else 1.1)
    setHistStyle(hist['BkgSum'], 1.2 if RATIO else 1.1)

    if RATIO:
        c1.cd(2)
        err = hist['BkgSum'].Clone("BkgErr;")
        err.SetTitle("")
        err.GetYaxis().SetTitle("Data / Bkg")
        for i in range(1, err.GetNbinsX() + 1):
            err.SetBinContent(i, 1)
            if hist['BkgSum'].GetBinContent(i) > 0:
                err.SetBinError(
                    i, hist['BkgSum'].GetBinError(i) /
                    hist['BkgSum'].GetBinContent(i))
        if RESIDUAL: setFitBotStyle(err)
        else: setBotStyle(err)
        errLine = err.Clone("errLine")
        errLine.SetLineWidth(1)
        errLine.SetFillStyle(0)
        res = hist[data[0]].Clone("Residues")
        for i in range(0, res.GetNbinsX() + 1):
            if hist['BkgSum'].GetBinContent(i) > 0:
                res.SetBinContent(
                    i,
                    res.GetBinContent(i) / hist['BkgSum'].GetBinContent(i))
                res.SetBinError(
                    i,
                    res.GetBinError(i) / hist['BkgSum'].GetBinContent(i))
        if RESIDUAL: setFitBotStyle(res)
        else: setBotStyle(res)
        #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5)
        #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2)
        err.Draw("E2")
        if 'PreFit' in hist:
            respre = hist[data[0]].Clone("ResiduesPreFit")
            respre.Divide(hist['PreFit'])
            respre.SetLineStyle(2)
            respre.SetLineColor(617)  #923
            respre.SetLineWidth(3)
            respre.SetFillStyle(0)
            respre.Draw("SAME, HIST")
        errLine.Draw("SAME, HIST")
        if not isBlind and len(data) > 0:
            res.Draw("SAME, PE0")
            #res_graph.Draw("SAME, PE0")
            if len(err.GetXaxis().GetBinLabel(
                    1)) == 0:  # Bin labels: not a ordinary plot
                drawRatio(hist['data_obs'], hist['BkgSum'])
                drawStat(hist['data_obs'], hist['BkgSum'])

    c1.Update()

    if RATIO and RESIDUAL:
        c1.cd(3)
        c1.SetGrid(1, 0)
        resFit = hist[data[0]].Clone("Residues")
        resFit.Reset("MICES")
        resFit.SetTitle("")
        #resFit.GetYaxis().SetTitle("Residuals")
        resFit.GetYaxis().SetTitle(
            "#frac{Data - Bkg}{#sqrt{#sigma_{Data}^{2}+#sigma_{Bkg}^{2}}}")
        for i in range(0, res.GetNbinsX() + 1):
            if hist['BkgSum'].GetBinContent(i) > 0:
                resFit.SetBinContent(
                    i, (hist[data[0]].GetBinContent(i) -
                        hist['BkgSum'].GetBinContent(i)) / (math.sqrt(
                            math.pow(hist['BkgSum'].GetBinError(i), 2) +
                            math.pow(hist[data[0]].GetBinError(i), 2))))
        setFitResStyle(resFit)
        resFit.SetLineColor(15)
        resFit.SetFillColor(15)
        resFit.SetFillStyle(1001)
        resFit.Draw("HIST")

        resFitLine = resFit.Clone("resFitLine")
        resFitLine.SetLineWidth(1)
        resFitLine.SetFillStyle(0)
        resFitLine.Draw("SAME, HIST")

    c1.Update()

    if gROOT.IsBatch(
    ) and options.saveplots:  # and (treeRead and channel in selection.keys()):
        AddString = ""
        if not os.path.exists("plots_" + options.name + "/" + plotdir):
            os.makedirs("plots_" + options.name + "/" + plotdir)
        if fileRead:
            if RESIDUAL: AddString = "_PostFit_Residual"
            else: AddString = "_PostFit"
        #c1.Print("plots_"+options.name+"/"+plotdir+"/"+plotname+binName+AddString+".png")
        c1.Print("plots_" + options.name + "/" + plotdir + "/" + plotname +
                 binName + AddString + ".pdf")

    # Print table
    printTable(hist, sign)

    if not gROOT.IsBatch(): raw_input("Press Enter to continue...")

    if gROOT.IsBatch() and not fileRead and (
            var == 'MET_pt' or
        (channel.startswith('SL') and var == 'MET_sign') or
        (channel.endswith('ZR') and var == 'FakeMET_pt')):
        saveHist(hist, channel + binName)
コード例 #4
0
def plot(var, cut, nm1=False):
    ### Preliminary Operations ###
    treeRead = True if not FILE else False  # Read from tree
    channel = cut
    isBlind = BLIND
    showSignal = False if 'SB' in cut or 'TR' in cut else True

    # Determine explicit cut
    if treeRead:
        for k in sorted(alias.keys(), key=len, reverse=True):
            if k in cut: cut = cut.replace(k, alias[k])

    # Determine Primary Dataset
    pd = []
    if "isSingleMuonPhotonTrigger" in cut:
        pd = [x for x in sample['data_obs']['files'] if "MuonEG" in x]
    elif "isJPsiTrigger" in cut:
        pd = [x for x in sample['data_obs']['files'] if "Charmonium" in x]
    else:
        print "Cannot determine Primary Dataset."
        exit()

    print "Plotting from", ("tree" if treeRead else
                            "file"), var, "in", channel, "channel with:"
    print "  dataset:", pd
    print "  cut    :", cut

    if isBlind and "SR" in channel and var in ["H_mass"]:
        cut += " && ( isMC ? 1 : !(H_mass > 86 && H_mass < 96) && !(H_mass > 120 && H_mass < 130) )"

    ### Create and fill MC histograms ###
    # Create dict
    file = {}
    tree = {}
    hist = {}
    cutstring = "(eventWeightLumi)" + ("*(" + cut +
                                       ")" if len(cut) > 0 else "")

    ### Create and fill MC histograms ###
    for i, s in enumerate(data + back + sign):
        hist[s] = TH1F(
            s, ";" + variable[var]['title'] + ";Events;" +
            ('logx' if variable[var]['logx'] else '') +
            ('logy' if variable[var]['logy'] else ''), variable[var]['nbins'],
            variable[var]['min'], variable[var]['max'])
        hist[s].Sumw2()
        tree[s] = TChain("Events")
        for j, ss in enumerate(sample[s]['files']):
            if s in data and not ss in pd: continue
            if YEAR == 2016 and not ('Run2016' in ss or 'Summer16' in ss):
                continue
            if YEAR == 2017 and not ('Run2017' in ss or 'Fall17' in ss):
                continue
            if YEAR == 2018 and not ('Run2018' in ss or 'Autumn18' in ss):
                continue
            for f in os.listdir(NTUPLEDIR + '/' + ss):
                tree[s].Add(NTUPLEDIR + '/' + ss + '/' + f)
        tree[s].Project(s, var, cutstring)
        if not tree[s].GetTree() == None:
            hist[s].SetOption("%s" % tree[s].GetTree().GetEntriesFast())

#    jobs = []
#    queue = multiprocessing.Queue()
#    for i, s in enumerate(data+back+sign):
#        for j, ss in enumerate(sample[s]['files']):
#            if s in data and not ss in pd: continue
#            if YEAR == 2016 and not ('Run2016' in ss or 'Summer16' in ss): continue
#            if YEAR == 2017 and not ('Run2017' in ss or 'Fall17' in ss): continue
#            if YEAR == 2018 and not ('Run2018' in ss or 'Autumn18' in ss): continue
#            if treeRead: # Project from tree
##                hist[s] = loopProject(s, ss, variable[var], cutstring, True)
#                p = multiprocessing.Process(target=parallelProject, args=(queue, s, ss, variable[var], cutstring, ))
#                jobs.append(p)
#                p.start()
#            else: # Histogram written to file
#                hist[s] = readhist(FILE, s, var, cut)
#
#    # Wait for all jobs to finish
#    for job in jobs:
#        h = queue.get()
#        if not h.GetOption() in hist: hist[h.GetOption()] = h
#        else: hist[h.GetOption()].Add(h)
#    for job in jobs:
#        job.join()

# Histogram style
    for i, s in enumerate(data + back + sign):
        hist[s].Scale(sample[s]['weight'] if hist[s].Integral() >= 0 else 0)
        hist[s].SetFillColor(sample[s]['fillcolor'])
        hist[s].SetFillStyle(sample[s]['fillstyle'] if not options.norm else 0)
        hist[s].SetLineColor(sample[s]['linecolor'])
        hist[s].SetLineStyle(sample[s]['linestyle'])
        hist[s].SetLineWidth(sample[s]['linewidth'])

    ### Create Bkg Sum histogram ###
    hist['BkgSum'] = hist['data_obs'].Clone(
        "BkgSum") if 'data_obs' in hist else hist[back[0]].Clone("BkgSum")
    hist['BkgSum'].Reset("MICES")
    hist['BkgSum'].SetFillStyle(3003)
    hist['BkgSum'].SetFillColor(1)
    for i, s in enumerate(back):
        hist['BkgSum'].Add(hist[s])

    if options.norm:
        for i, s in enumerate(back + ['BkgSum']):
            hist[s].Scale(hist[data[0]].Integral() / hist['BkgSum'].Integral())
        for i, s in enumerate(sign):
            hist[s].Scale(hist[data[0]].Integral() / hist[s].Integral())

    # Create data and Bkg sum histograms
#    if BLIND: # or 'SR' in channel:
#        hist['data_obs'] = hist['BkgSum'].Clone("data_obs")
#        hist['data_obs'].Reset("MICES")
# Set histogram style
    hist['data_obs'].SetMarkerStyle(20)
    hist['data_obs'].SetMarkerSize(1.25)

    #    for i, s in enumerate(data+back+sign+['BkgSum']): addOverflow(hist[s], False) # Add overflow
    for i, s in enumerate(sign):
        hist[s].SetLineWidth(3)
    for i, s in enumerate(sign):
        sample[s]['plot'] = True

    # Create stack
    bkg = THStack("Bkg",
                  ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events")
    for i, s in enumerate(back):
        bkg.Add(hist[s])

    # Legend
    leg = TLegend(0.65, 0.6, 0.95, 0.9)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    if len(data) > 0:
        leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pe")
    for i, s in reversed(list(enumerate(['BkgSum'] + back))):
        leg.AddEntry(hist[s], sample[s]['label'], "f")
    if showSignal:
        for i, s in enumerate(sign):
            if sample[s]['plot']:
                leg.AddEntry(hist[s], sample[s]['label'], "fl")

    leg.SetY1(0.9 - leg.GetNRows() * 0.04)

    # --- Display ---
    c1 = TCanvas("c1",
                 hist.values()[0].GetXaxis().GetTitle(), 800,
                 800 if RATIO else 600)

    if RATIO:
        c1.Divide(1, 2)
        setTopPad(c1.GetPad(1), RATIO)
        setBotPad(c1.GetPad(2), RATIO)
    c1.cd(1)
    c1.GetPad(bool(RATIO)).SetTopMargin(0.06)
    c1.GetPad(bool(RATIO)).SetRightMargin(0.05)
    c1.GetPad(bool(RATIO)).SetTicks(1, 1)

    logX, logY = "logx" in hist['BkgSum'].GetZaxis().GetTitle(
    ), "logy" in hist['BkgSum'].GetZaxis().GetTitle()
    if logY: c1.GetPad(bool(RATIO)).SetLogy()
    if logX: c1.GetPad(bool(RATIO)).SetLogx()

    # Draw
    bkg.Draw("HIST")  # stack
    hist['BkgSum'].Draw("SAME, E2")  # sum of bkg
    if len(data) > 0: hist['data_obs'].Draw("SAME, PE")  # data
    #data_graph.Draw("SAME, PE")
    #    if showSignal:
    #        smagn = 1. #if treeRead else 1.e2 #if logY else 1.e2
    for i, s in enumerate(sign):
        if sample[s]['plot']: hist[s].Draw("SAME, HIST")


#                hist[s].Scale(smagn)
#                hist[s].Draw("SAME, HIST") # signals Normalized, hist[s].Integral()*sample[s]['weight']
#        #textS = drawText(0.80, 0.9-leg.GetNRows()*0.05 - 0.02, stype+" (x%d)" % smagn, True)
    bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset() * 1.075)
    bkg.SetMaximum((5. if logY else 1.25) * max(
        bkg.GetMaximum(),
        hist['data_obs'].GetBinContent(hist['data_obs'].GetMaximumBin()) +
        hist['data_obs'].GetBinError(hist['data_obs'].GetMaximumBin())))
    #if bkg.GetMaximum() < max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum()): bkg.SetMaximum(max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum())*1.25)
    bkg.SetMinimum(
        max(
            min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin(
            )), hist['data_obs'].GetMinimum()), 5.e-1) if logY else 0.)
    if logY:
        bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4)
        bkg.GetYaxis().SetMoreLogLabels(True)

    #if logY: bkg.SetMinimum(1)
    leg.Draw()
    drawCMS(LUMI[YEAR], "Preliminary")
    if channel in aliasNames: drawRegion(aliasNames[channel], True)
    #drawAnalysis(channel)

    #if nm1 and not cutValue is None: drawCut(cutValue, bkg.GetMinimum(), bkg.GetMaximum()) #FIXME
    #if len(sign) > 0:
    #    if channel.startswith('X') and len(sign)>0: drawNorm(0.9-0.05*(leg.GetNRows()+1), "#sigma(X) = %.1f pb" % 1.)

    setHistStyle(bkg, 1.2 if RATIO else 1.1)
    setHistStyle(hist['BkgSum'], 1.2 if RATIO else 1.1)

    if RATIO:
        c1.cd(2)
        if logX: c1.GetPad(2).SetLogx()
        err = hist['BkgSum'].Clone("BkgErr;")
        err.SetTitle("")
        err.GetYaxis().SetTitle("Data / Bkg")
        for i in range(1, err.GetNbinsX() + 1):
            err.SetBinContent(i, 1)
            if hist['BkgSum'].GetBinContent(i) > 0:
                err.SetBinError(
                    i, hist['BkgSum'].GetBinError(i) /
                    hist['BkgSum'].GetBinContent(i))
        setBotStyle(err)
        errLine = err.Clone("errLine")
        errLine.SetLineWidth(1)
        errLine.SetFillStyle(0)
        res = hist['data_obs'].Clone("Residues")
        for i in range(0, res.GetNbinsX() + 1):
            if hist['BkgSum'].GetBinContent(i) > 0:
                res.SetBinContent(
                    i,
                    res.GetBinContent(i) / hist['BkgSum'].GetBinContent(i))
                res.SetBinError(
                    i,
                    res.GetBinError(i) / hist['BkgSum'].GetBinContent(i))
        setBotStyle(res)
        #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5)
        #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2)
        err.Draw("E2")
        errLine.Draw("SAME, HIST")
        if len(data) > 0:
            res.Draw("SAME, PE0")
            #res_graph.Draw("SAME, PE0")
            if len(err.GetXaxis().GetBinLabel(
                    1)) == 0:  # Bin labels: not a ordinary plot
                drawRatio(hist['data_obs'], hist['BkgSum'])
                drawStat(hist['data_obs'], hist['BkgSum'])

    if var in ["H_mass"]:
        c1.cd(bool(RATIO))
        boxZ = drawBox(XZMIN, hist['data_obs'].GetMinimum(), XZMAX,
                       hist['data_obs'].GetMaximum() / 1.30, "Z")
        boxH = drawBox(XHMIN, hist['data_obs'].GetMinimum(), XHMAX,
                       hist['data_obs'].GetMaximum() / 1.30, "H")

    c1.Update()

    if True:  #gROOT.IsBatch():
        varname = var.replace('.', '_').replace('()', '')
        if not os.path.exists("plots/" + channel):
            os.makedirs("plots/" + channel)
        c1.Print("plots/" + channel + "/" + varname + ".png")
        c1.Print("plots/" + channel + "/" + varname + ".pdf")

    # Print table
    printTable(hist, sign)

    if not gROOT.IsBatch(): raw_input("Press Enter to continue...")
コード例 #5
0
 normCMS.SetLineColor(kBlue + 2)
 normCMS.Draw('apz')
 normCMS.SetMaximum(0.3)
 leg = TLegend(0.4, 1. - gPad.GetTopMargin() - 0.03 - 0.18,
               1. - gPad.GetRightMargin() - 0.02,
               1. - gPad.GetTopMargin() - 0.03, '', 'NDC')
 leg.SetMargin(0.15)
 #leg.Dump()
 leg.SetFillStyle(0)
 leg.SetBorderSize(0)
 leg.AddEntry(normCMS, 'CMS, |y|<2, #sqrt{s}=7 TeV', 'pe')
 if (D0):
     D0.Draw('pz')
     leg.AddEntry(D0, 'D#oslash, |y|<1.8, #sqrt{s}=1.96 TeV', 'pe')
 else:
     leg.SetY1(1. - gPad.GetTopMargin() - 0.03 - 0.12)
 if (CDF):
     CDF.Draw('pz')
     leg.AddEntry(CDF, 'CDF, |y|<0.4, #sqrt{s}=1.8 TeV', 'pe')
 #leg.Dump()
 leg.Draw('same')
 l.DrawLatex(1.0 - gPad.GetRightMargin() - 0.04,
             leg.GetY1() - 0.06, '#varUpsilon(%iS)' % resonance)
 gPad.SetLogy()
 gPad.Update()
 normCMS.GetXaxis().SetTitle('p_{T} (GeV/c)')
 normCMS.GetYaxis().SetTitle('(d#sigma/dp_{T})/#sigma_{TOT} (GeV/c)^{-1}')
 normCMS.GetXaxis().SetLimits(0., 30.)
 gPad.Modified()
 gPad.Print('TevatronCompare%iS.eps' % resonance)
 gPad.Print('TevatronCompare%iS.png' % resonance)
コード例 #6
0
class Plot(object):
    """Structural class for representing, accessing, and maintaining references
    to ROOT graphical elements forming a plot, potentially with a ratio
    subplot.
    """

    # Plotting 'constants' for the plot class.  Ideally, one would allow these
    # to be flexible, but unfortunately ROOT's coordinate system is extremely
    # inconsistent and fragile, so it is best to fix these values here.  You
    # can change them dynamically with Plot.Whatever = value, but it is
    # probably best to leave them alone.
    # TODO: 600x600 and 800x600 are the ATLAS default for square and
    # rectangular plots respectively. Fix this when everything is calm.
    PLOT_WIDTH = 1280  # px
    PLOT_HEIGHT = 1024  # px
    #PLOT_MARGINS = (0.125, 0.05, 0.1, 0.1) # Left, Right, Bottom, Top
    PLOT_MARGINS = (0.125, 0.05, 0.1, 0.07)  # Left, Right, Bottom, Top
    PLOT_MARGINS_WITH_RATIO = (0.125, 0.05, 0.025, 0.1)
    PLOT_RATIO_MARGINS = (0.125, 0.05, 0.325, 0.05)
    PLOT_TITLE_X = 0.5
    PLOT_TITLE_Y = 0.95
    PLOT_TITLE_TEXT_SIZE = 0.04
    PLOT_TITLE_TEXT_COLOR = 1
    PLOT_TITLE_TEXT_FONT = 42
    PLOT_HEADER_HEIGHT = 400  # px
    PLOT_LEGEND_LEFT = 0.45
    PLOT_LEGEND_RIGHT = 0.95
    PLOT_LEGEND_BOTTOM = 0.7
    PLOT_LEGEND_BOTTOM_WITH_RATIO = 0.63
    PLOT_LEGEND_TOP = 0.90
    PLOT_LEGEND_TOP_WITH_RATIO = 0.86
    PLOT_LEGEND_TEXT_SIZE = 0.025
    PLOT_LEGEND_TEXT_SIZE_WITH_RATIO = 0.03
    PLOT_LEGEND_ROW_SIZE = 0.04
    PLOT_LEGEND_ROW_SIZE_WITH_RATIO = 0.045
    PLOT_LEGEND_N_COLUMNS = 1
    PLOT_LEGEND_PIVOT_COLUMNS = True
    PLOT_STAT_LEFT = 0.55
    PLOT_STAT_LEFT_WITH_RATIO = 0.60
    PLOT_STAT_RIGHT = 0.85
    PLOT_STAT_RIGHT_WITH_RATIO = 0.93
    PLOT_STAT_BOTTOM = 0.15
    PLOT_STAT_BOTTOM_WITH_RATIO = 0.07
    PLOT_STAT_TOP = 0.4
    PLOT_STAT_TOP_WITH_RATIO = 0.45
    PLOT_STAT_TEXT_FONT = 42
    PLOT_STAT_TEXT_SIZE = 0.03
    PLOT_STAT_TEXT_SIZE_WITH_RATIO = 0.04
    PLOT_RATIO_FRACTION = 0.3  # fraction of canvas height
    PLOT_X_AXIS_TITLE_SIZE = 0.042
    PLOT_X_AXIS_TITLE_SIZE_WITH_RATIO = 0.14
    PLOT_X_AXIS_TITLE_OFFSET = 0.95
    PLOT_X_AXIS_TITLE_OFFSET_WITH_RATIO = 0.96
    PLOT_X_AXIS_LABEL_SIZE_WITH_RATIO = 0.12
    PLOT_Y_AXIS_LABEL_OFFSET = 0.01
    PLOT_Y_AXIS_TITLE_SIZE = 0.042
    PLOT_Y_AXIS_TITLE_SIZE_WITH_RATIO = 0.06
    PLOT_Y_AXIS_TITLE_OFFSET = 1.0
    PLOT_Y_AXIS_TITLE_OFFSET_WITH_RATIO = 0.95
    PLOT_Y_AXIS_LABEL_SIZE_WITH_RATIO = 0.05
    PLOT_RATIO_Y_AXIS_TITLE_SIZE = 0.12
    PLOT_RATIO_Y_AXIS_TITLE_OFFSET = 0.40
    PLOT_RATIO_Y_AXIS_LABEL_SIZE = 0.12
    PLOT_RATIO_Y_AXIS_LABEL_OFFSET = PLOT_Y_AXIS_LABEL_OFFSET
    PLOT_RATIO_Y_AXIS_NDIVISIONS = 504
    PLOT_RATIO_Y_AXIS_MINIMUM = 0.6
    PLOT_RATIO_Y_AXIS_MAXIMUM = 1.4
    PLOT_ERROR_BAND_FILL_STYLE = 3254  # Diagonal lines
    PLOT_ERROR_BAND_FILL_COLOR = 13  # Gray
    PLOT_ERROR_BAND_LINE_WIDTH = 0
    PLOT_ERROR_BAND_LINE_COLOR = 0
    PLOT_RATIO_ERROR_BAND_FILL_STYLE = 3254  # Diagonal lines
    PLOT_RATIO_ERROR_BAND_FILL_COLOR = 807  # Orange
    PLOT_RATIO_ERROR_BAND_LINE_WIDTH = 0
    PLOT_RATIO_ERROR_BAND_LINE_COLOR = 0
    # Stamp settings
    PLOT_ATLAS_STAMP_TEXT_SIZE = 0.035
    PLOT_ATLAS_STAMP_TEXT_SIZE_WITH_RATIO = 0.05
    PLOT_ATLAS_STAMP_TEXT_COLOR = 1
    PLOT_ATLAS_STAMP_TEXT_FONT = 42
    PLOT_ATLAS_STAMP_LEFT = 0.18
    PLOT_ATLAS_STAMP_TOP = 0.875
    PLOT_ATLAS_STAMP_TOP_WITH_RATIO = 0.82
    # Stamp specializations
    PLOT_ATLAS_STAMP_ATLAS_TEXT_FONT = 72
    PLOT_ATLAS_STAMP_ATLAS_LABEL_LEFT = 0.28
    PLOT_ATLAS_STAMP_LUMINOSITY_OFFSET = 0.036
    PLOT_ATLAS_STAMP_LUMINOSITY_OFFSET_WITH_RATIO = 0.05
    PLOT_ATLAS_STAMP_LUMINOSITY_SIZE = 0.062
    PLOT_ATLAS_STAMP_LUMINOSITY_SIZE_WITH_RATIO = 0.085

    def __init__(self,
                 title='',
                 x_title=None,
                 y_title=None,
                 plot_header=True,
                 ratio=False,
                 x_range=None,
                 y_max=None,
                 y_log_scale=False):
        """Initializes a new instance of the Plot class.

        Args:
            title: The title to set for the histogram
            plot_header: Whether or not to include whitespace at the top of the
                plot for the ATLAS label and legend
            ratio: Whether or not to include a ratio plot
            x_range: A tuple of (x_min, x_max)
            y_max: The maximum Y axis value
            y_log_scale: Use log scale for Y axis
        """
        # Store the title
        self._title = title
        self._x_title, self._y_title = x_title, y_title

        # Store whether or not the user wants to create a plot header
        self._plot_header = plot_header

        # Calculate a unique name for the plot components
        name = _rand_uuid()

        # Create a canvas
        self._canvas = TCanvas(name + '_canvas', name, int(self.PLOT_WIDTH),
                               int(self.PLOT_HEIGHT))
        SetOwnership(self._canvas, False)

        # Create the main plot and draw it
        self._plot = TPad(name + '_plot', name, 0.0,
                          (self.PLOT_RATIO_FRACTION if ratio else 0.0), 1.0,
                          1.0)
        SetOwnership(self._plot, False)
        self._plot.SetMargin(
            *(self.PLOT_MARGINS_WITH_RATIO if ratio else self.PLOT_MARGINS))
        self._plot.Draw()

        # HACK: Draw the plot title.
        # https://root.cern.ch/phpBB3/viewtopic.php?t=18282. Wonderful.
        self._draw_title()

        # Store ranges
        self._x_range = x_range
        if y_max is not None:
            self._set_maximum_value(y_max)

        # Store log scale
        self._y_log_scale = y_log_scale

        # Switch back to the context of the canvas
        self._canvas.cd()

        # Create a ratio plot and draw it if requested
        if ratio:
            self._ratio_plot = TPad(name + '_ratio', name, 0.0, 0.0, 1.0,
                                    self.PLOT_RATIO_FRACTION)
            SetOwnership(self._ratio_plot, False)
            self._ratio_plot.SetMargin(*self.PLOT_RATIO_MARGINS)
            self._ratio_plot.SetGridy(True)
            self._ratio_plot.Draw()
        else:
            self._ratio_plot = None

        # Track whether or not we've already drawn to the main pad
        self._drawn = False

        # Track whether or not we've already drawn to the ratio pad
        self._ratio_drawn = False

        # Track that object which sets up the axes in the main plot
        self._axes_object = None

        # Create a structure to track any histograms we generate internally
        # which need to be added to any legends created
        self._legend_extras = []

        # Create lists of the cloned drawables, just to be certain
        self._drawables = []
        self._ratio_drawables = []

    def save(self, path, extensions=['pdf']):
        """Saves this plot to file.

        Args:
            path: The path where the plot should be saved.
        """
        # Force an update of the canvas
        self._canvas.Update()

        # Save to file
        for e in extensions:
            self._canvas.SaveAs(path + '.' + e)

    def _get_maximum_value(self):
        """Returns the currently set maximum value (possibly None).
        """
        if hasattr(self, '_maximum_value'):
            return self._maximum_value
        return None

    def _set_maximum_value(self, value):
        """Sets the current maximum value, possibly including room for a plot
        header.

        Args:
            value: The value to set
        """
        # Check if the current value is not None, and if so, throw an error
        # because this property should not be set twice
        if self._get_maximum_value() is not None:
            raise RuntimeError('maximum value should not be set twice')

        # If the value is None, ignore it
        if value is None:
            return

        # If the user wants a plot header, then add space for one
        if self._plot_header:
            # Grab the plot pad height (in pixels)
            plot_height = (self.PLOT_HEIGHT *
                           (self._plot.GetY2() - self._plot.GetY1()))

            # Adjust the height
            value *= (plot_height + self.PLOT_HEADER_HEIGHT) / plot_height

        # Set the value
        self._maximum_value = value

    def draw(self, *drawables_styles_options):
        """Plots a collection of plottables to the main plot pad.  All TH1
        objects are drawn with error bars.  THStack elements are only drawn
        with an error band if one is provided.

        This method may only be called once

        Args:
            drawables_styles_options: Each argument of this function must be
                of the form (object, style, options), where object is one of
                the following:

                - A TH1 object
                - A TH2 object
                - A THStack object
                - A tuple of the form (THStack, TGraph) where the latter
                  represents error bars
                - A TGraph object
                - A TLine object

                style is a tuple of the form (line_color, fill_color,
                marker_style), and options is a string which will be used for
                the options argument of the object's Draw method.  Plottables
                will be rendered in the order provided.  Axes drawing options
                (e.g.  'a' or 'same' should not be provided and will be set
                automatically).  A TLine may not be the first drawable
                element.
        """
        # Make sure there are drawables
        if len(drawables_styles_options) == 0:
            raise ValueError('must provide at least one plottable')

        # Check if we've already drawn
        if self._drawn:
            raise RuntimeError('cannot draw twice to a plot')
        self._drawn = True

        # Remove None-valued drawables
        drawables_styles_options = tuple(
            ((d, s, o) for d, s, o in drawables_styles_options
             if valid_drawable(d)))

        # Extract drawables
        drawables, _, _ = zip(*drawables_styles_options)

        # Check if there is a maximum value set, and if not, set it
        if self._get_maximum_value() is None:
            self._set_maximum_value(maximum_value(drawables))

        # Move to the context of the plot pad
        self._plot.cd()

        # Iterate through and draw drawables based on type
        first = True
        for drawable, style, option in drawables_styles_options:
            # Check if this a tuple of histogram, error_band
            if isinstance(drawable, tuple):
                drawable, error_band = drawable
            else:
                error_band = None

            # Make a clone of the drawable so we don't modify it
            o = clone(drawable)
            SetOwnership(o, False)

            # Add it to the list of drawables
            self._drawables.append(o)

            # Set the title appropriately
            if not is_line(o):
                o.SetTitle(drawable.GetTitle())

            # Style the drawable before it is drawn
            if style is not None:
                if is_line(drawable) or is_function(drawable):
                    if isinstance(style, dict):
                        style_line(o, **style)
                    else:
                        style_line(o, *style)
                else:
                    if isinstance(style, dict):
                        style_histogram(o, **style)
                    else:
                        style_histogram(o, *style)

            # Set the maximum value of the drawable if supported
            # HACK: I wish this could go into _handle_axes, but apparently it
            # can't because ROOT sucks and this has to be set on EVERY
            # drawable, not just the one with the axes.
            if is_scatter(o):
                o.SetMinimum(1 if self._y_log_scale else 0)
            if is_histo(o) or is_graph(o) or is_stack(o) or is_function(o):
                o.SetMaximum(self._get_maximum_value())
                # With TGraph, this is sometimes necessary. Perhaps with TH1
                # too. I'm not sure what happens if we set log scale, but
                # we'll cross that bridge then.
                o.SetMinimum(1 if self._y_log_scale else 0)

            # Include axes if we need to. Store the x-axis range.
            if first:
                if is_line(o):
                    raise ValueError('TLine may not be first drawable')
                if is_graph(o):
                    option += 'a'
            else:
                option += 'same'
            first = False

            # Draw the drawable
            o.Draw(option)

            # TODO: This method of plotting the stats box is a huge hack. We
            # should plot fit functions separately, and move the stats box
            # plotting to its own function. Home grow everything, the only
            # way you can make ROOT work.
            if is_graph(o) or is_histo(o):
                if len(o.GetListOfFunctions()) > 0:
                    # HACK: Need to call Update() to paint the fit stats
                    self._plot.Update()
                    stats = o.FindObject("stats")
                    if stats:
                        stats.SetTextFont(Plot.PLOT_STAT_TEXT_FONT)
                        stats.SetTextSize(
                            (Plot.PLOT_STAT_TEXT_SIZE_WITH_RATIO if
                             self._ratio_plot else Plot.PLOT_STAT_TEXT_SIZE))
                        stats.SetX1NDC(
                            (Plot.PLOT_STAT_LEFT_WITH_RATIO
                             if self._ratio_plot else Plot.PLOT_STAT_LEFT))
                        stats.SetY1NDC(
                            (Plot.PLOT_STAT_BOTTOM_WITH_RATIO
                             if self._ratio_plot else Plot.PLOT_STAT_BOTTOM))
                        stats.SetX2NDC(
                            (Plot.PLOT_STAT_RIGHT_WITH_RATIO
                             if self._ratio_plot else Plot.PLOT_STAT_RIGHT))
                        stats.SetY2NDC(
                            (Plot.PLOT_STAT_TOP_WITH_RATIO
                             if self._ratio_plot else Plot.PLOT_STAT_TOP))

            # Handle axes
            if not is_line(o):
                self._handle_axes(o, option)

            # If there is an error band, draw it
            if error_band is not None:
                self._draw_error_band(error_band)

        if self._y_log_scale:
            self._plot.SetLogy(1)

        # TODO: Verify this. It breaks 2D plotting.
        # HACK: Need to force a redraw of plot axes due to issue with ROOT:
        # http://root.cern.ch/phpBB3/viewtopic.php?f=3&t=14034
        #self._plot.RedrawAxis()

    def _handle_axes(self, drawable, option):
        """If there is no object currently registered as the owner of the axes
        drawn on the main plot, then this will set it.

        Args:
            drawable: The graph, histogram or stack whose axes were ALREADY
                drawn
            option: The option with which to draw the axes
        """
        # If we already have an axes object, ignore this one
        if self._axes_object is not None:
            return

        # Grab the histogram used for axes style/range manipulation
        if is_stack(drawable) or is_graph(drawable):
            axes_histogram = drawable.GetHistogram()
        else:
            axes_histogram = drawable
        self._axes_object = axes_histogram

        # Grab the histogram used for title manipulation
        if is_stack(drawable):
            title_histogram = drawable.GetHists()[0]
        else:
            title_histogram = drawable

        # Grab axes
        x_axis, y_axis = axes_histogram.GetXaxis(), axes_histogram.GetYaxis()

        # Grab titles from first histogram if not set explicitly
        if self._x_title is None:
            self._x_title = title_histogram.GetXaxis().GetTitle()
        if self._y_title is None:
            self._y_title = title_histogram.GetYaxis().GetTitle()

        if self._x_range is not None:
            #x_axis.SetRangeUser(*self._x_range)
            x_axis.SetLimits(*self._x_range)

        # Style x-axis, or hide it if this plot has a ratio plot
        if self._ratio_plot:
            x_axis.SetLabelOffset(999)
            x_axis.SetTitleOffset(999)
        else:
            x_axis.SetTitle(self._x_title)
            x_axis.SetTitleSize(self.PLOT_X_AXIS_TITLE_SIZE)
            x_axis.SetTitleOffset(self.PLOT_X_AXIS_TITLE_OFFSET)

        # Style y-axis
        if self._ratio_plot:
            y_axis.SetLabelSize(self.PLOT_Y_AXIS_LABEL_SIZE_WITH_RATIO)
        y_axis.SetLabelOffset(self.PLOT_Y_AXIS_LABEL_OFFSET)
        y_axis.SetTitle(self._y_title)
        y_axis.SetTitleSize(
            (self.PLOT_Y_AXIS_TITLE_SIZE_WITH_RATIO
             if self._ratio_plot else self.PLOT_Y_AXIS_TITLE_SIZE))
        y_axis.SetTitleOffset(
            (self.PLOT_Y_AXIS_TITLE_OFFSET_WITH_RATIO
             if self._ratio_plot else self.PLOT_Y_AXIS_TITLE_OFFSET))

        # Redraw the drawable with the new style
        drawable.Draw(option)

    def _draw_error_band(self, error_band):
        """Draws an error band on top of histogram objects.

        Args:
            error_band: The error band to draw (a TGraphAsymmErrors)
        """
        # Style it
        # HACK: Setting the marker style to 0 specifies this should be filled
        # in the legend
        error_band.SetMarkerStyle(0)
        error_band.SetMarkerSize(0)
        error_band.SetFillStyle(self.PLOT_ERROR_BAND_FILL_STYLE)
        error_band.SetFillColor(self.PLOT_ERROR_BAND_FILL_COLOR)
        error_band.SetLineWidth(self.PLOT_ERROR_BAND_LINE_WIDTH)
        error_band.SetLineColor(self.PLOT_ERROR_BAND_LINE_COLOR)

        # Draw it
        error_band.Draw('e2same')

        # Add it to the list of things we need to add to the legend
        self._legend_extras.append(error_band)

    def draw_ratio_histogram(self,
                             histogram,
                             draw_unity=True,
                             error_band=None):
        """Draws a ratio histogram to the ratio pad.

        Args:
            histogram: The ratio histogram to draw (use ratio_histogram)
            draw_unity: Whether or not to draw a line at 1
            error_band: An error band to draw under the ratio histogram
                (see owls_hep.uncertainty.ratio_uncertainty_band)

        The histogram X axis title is set by draw_histogram if not set
        explicitly. draw_ratio_histogram should therefore be called after
        draw_histogram.
        """
        # Check if we've already drawn
        if self._ratio_drawn:
            raise RuntimeError('cannot draw twice to a plot')
        self._ratio_drawn = True

        # Switch to the context of the ratio pad
        self._ratio_plot.cd()

        # Clone the histogram
        histogram = histogram.Clone(_rand_uuid())
        SetOwnership(histogram, False)

        # Style it
        x_axis, y_axis = histogram.GetXaxis(), histogram.GetYaxis()
        x_axis.SetTitleSize(self.PLOT_X_AXIS_TITLE_SIZE_WITH_RATIO)
        x_axis.SetTitleOffset(self.PLOT_X_AXIS_TITLE_OFFSET_WITH_RATIO)
        x_axis.SetLabelSize(self.PLOT_X_AXIS_LABEL_SIZE_WITH_RATIO)
        x_axis.SetTitle(self._x_title)
        if self._x_range:
            x_axis.SetLimits(*self._x_range)
            #x_axis.SetRangeUser(*self._x_range)
        else:
            x_axis.SetLimits(self._axes_object.GetXaxis().GetXmin(),
                             self._axes_object.GetXaxis().GetXmax())
            #x_axis.SetRangeUser(self._axes_object.GetXaxis().GetXmin(),
            #self._axes_object.GetXaxis().GetXmax())
        y_axis.SetTitleSize(self.PLOT_RATIO_Y_AXIS_TITLE_SIZE)
        y_axis.SetTitleOffset(self.PLOT_RATIO_Y_AXIS_TITLE_OFFSET)
        y_axis.SetLabelSize(self.PLOT_RATIO_Y_AXIS_LABEL_SIZE)
        y_axis.SetLabelOffset(self.PLOT_RATIO_Y_AXIS_LABEL_OFFSET)
        y_axis.SetRangeUser(self.PLOT_RATIO_Y_AXIS_MINIMUM,
                            self.PLOT_RATIO_Y_AXIS_MAXIMUM)
        y_axis.SetNdivisions(self.PLOT_RATIO_Y_AXIS_NDIVISIONS, False)

        # Draw it
        # NOTE: Have to specify E0 or points out of the vertical range won't
        # have their error bars drawn:
        #   https://root.cern.ch/phpBB3/viewtopic.php?f=3&t=13329
        # histogram.Draw('e0p')
        # NOTE: Or live with it and get rid of points from zero value bins
        histogram.Draw('ep')

        # Draw a line at unity if requested
        if draw_unity:
            # Calculate the line coordinates
            line_min = histogram.GetBinLowEdge(1)
            max_bin = histogram.GetNbinsX()
            line_max = (histogram.GetBinLowEdge(max_bin) +
                        histogram.GetBinWidth(max_bin))

            # Create and draw the line
            unit_line = TLine(line_min, 1.0, line_max, 1.0)
            SetOwnership(unit_line, False)
            unit_line.SetLineColor(2)  # Red
            unit_line.SetLineWidth(2)
            unit_line.Draw('same')

        # If an error band was provided, draw it and add it to our legend
        # elements
        if error_band:
            # Keep ownership of the error band
            SetOwnership(error_band, False)

            # Style it
            error_band.SetMarkerSize(0)
            error_band.SetFillStyle(self.PLOT_RATIO_ERROR_BAND_FILL_STYLE)
            error_band.SetFillColor(self.PLOT_RATIO_ERROR_BAND_FILL_COLOR)
            error_band.SetLineWidth(self.PLOT_RATIO_ERROR_BAND_LINE_WIDTH)
            error_band.SetLineColor(self.PLOT_RATIO_ERROR_BAND_LINE_COLOR)

            # Draw it
            error_band.Draw('e2same')

        # Now, if we've drawn unity or an error band, redraw our ratio
        # histogram so that its point lie on top of the unity line or error
        # band, but use 'same' so that the axes/ticks don't cover the red line
        if draw_unity or error_band:
            # histogram.Draw('e0psame')
            histogram.Draw('epsame')

    def draw_ratios(self,
                    drawables_styles_options,
                    draw_unity=True,
                    y_range=None,
                    y_title=None):
        """Draws a drawable to the ratio pad.

        Args:
            drawable: The drawable to draw
            draw_unity: Whether or not to draw a line at 1
        """
        # Check if we've already drawn
        if self._ratio_drawn:
            raise RuntimeError('cannot draw twice to a plot')
        self._ratio_drawn = True

        # Switch to the context of the ratio pad
        self._ratio_plot.cd()

        # Iterate through and draw drawables based on type
        first = True
        for drawable, style, option in drawables_styles_options:
            # Make a clone of the drawable so we don't modify it
            o = clone(drawable)
            SetOwnership(o, False)

            # Add it to the list of drawables
            self._ratio_drawables.append(o)

            # Set the title appropriately
            if not is_line(o):
                o.SetTitle(drawable.GetTitle())

            # Style the drawable before it is drawn
            if style is not None:
                if is_line(drawable) or is_function(drawable):
                    style_line(o, *style)
                else:
                    style_histogram(o, *style)

            if not is_line(o):
                if y_range is not None:
                    o.SetMinimum(y_range[0])
                    o.SetMaximum(y_range[1])

            # Include axes if we need
            if first:
                if is_line(o):
                    raise ValueError('TLine may not be first drawable')
                x_axis, y_axis = o.GetXaxis(), o.GetYaxis()
                if is_graph(o):
                    option += 'a'
            else:
                option += 'same'
            first = False

            # Draw the drawable
            o.Draw(option)

        x_axis.SetTitleSize(self.PLOT_X_AXIS_TITLE_SIZE_WITH_RATIO)
        x_axis.SetTitleOffset(self.PLOT_X_AXIS_TITLE_OFFSET_WITH_RATIO)
        x_axis.SetLabelSize(self.PLOT_X_AXIS_LABEL_SIZE_WITH_RATIO)
        x_axis.SetTitle(self._x_title)
        if self._x_range:
            x_axis.SetLimits(*self._x_range)
            #x_axis.SetRangeUser(*self._x_range)
        else:
            x_axis.SetLimits(self._axes_object.GetXaxis().GetXmin(),
                             self._axes_object.GetXaxis().GetXmax())
            #x_axis.SetRangeUser(self._axes_object.GetXaxis().GetXmin(),
            #self._axes_object.GetXaxis().GetXmax())
        y_axis.SetTitleSize(self.PLOT_RATIO_Y_AXIS_TITLE_SIZE)
        y_axis.SetTitleOffset(self.PLOT_RATIO_Y_AXIS_TITLE_OFFSET)
        y_axis.SetLabelSize(self.PLOT_RATIO_Y_AXIS_LABEL_SIZE)
        y_axis.SetLabelOffset(self.PLOT_RATIO_Y_AXIS_LABEL_OFFSET)
        y_axis.SetNdivisions(self.PLOT_RATIO_Y_AXIS_NDIVISIONS, False)
        if y_title is not None:
            y_axis.SetTitle(y_title)

        self._ratio_plot.Update()

    def _draw_title(self):
        """Draws a title on the plot.
        """
        title = TLatex()
        title.SetTextColor(self.PLOT_TITLE_TEXT_COLOR)
        title.SetTextFont(self.PLOT_TITLE_TEXT_FONT)
        title.SetNDC()
        title.SetTextSize(self.PLOT_TITLE_TEXT_SIZE)
        title.SetTextAlign(22)
        title.DrawLatex(self.PLOT_TITLE_X, self.PLOT_TITLE_Y, self._title)

    def draw_atlas_label(self,
                         luminosity=None,
                         sqrt_s=None,
                         custom_label=None,
                         atlas_label=None):
        """Draws an ATLAS stamp on the plot, with an optional categorization
        label.

        It is recommended that you construct the Plot with plot_header = True
        in order to make space for the label.

        Args:
            luminosity: The integrated luminosity, in pb^-1
            sqrt_s: The center of mass energy, in MeV
            label: The label to put after 'ATLAS', None to exclude the 'ATLAS'
                categorization entirely
        """
        # Change context to the plot pad
        self._plot.cd()

        # Create the latex object
        # TODO: Consider using TPaveText to overwrite drawn graphs and
        # histograms. At least for scatter plots.
        # TODO: Increase readability: Create two sets of constants -
        # one for plots with ratio and one for plots without. Select the
        # correct one in draw_ratio_histogram.
        stamp = TLatex()

        # Style it
        stamp.SetTextColor(self.PLOT_ATLAS_STAMP_TEXT_COLOR)
        stamp.SetTextSize(
            (self.PLOT_ATLAS_STAMP_TEXT_SIZE_WITH_RATIO
             if self._ratio_plot else self.PLOT_ATLAS_STAMP_TEXT_SIZE))
        stamp.SetTextFont(self.PLOT_ATLAS_STAMP_TEXT_FONT)
        stamp.SetNDC()

        top = (self.PLOT_ATLAS_STAMP_TOP_WITH_RATIO
               if self._ratio_plot else self.PLOT_ATLAS_STAMP_TOP)

        # Print an ATLAS label on top
        if atlas_label is not None:
            # Draw the label
            stamp.SetTextFont(self.PLOT_ATLAS_STAMP_ATLAS_TEXT_FONT)
            stamp.DrawLatex(self.PLOT_ATLAS_STAMP_LEFT, top, 'ATLAS')
            stamp.SetTextFont(self.PLOT_ATLAS_STAMP_TEXT_FONT)
            stamp.DrawLatex(self.PLOT_ATLAS_STAMP_ATLAS_LABEL_LEFT, top,
                            atlas_label)
            top -= (self.PLOT_ATLAS_STAMP_TEXT_SIZE_WITH_RATIO if self.
                    _ratio_plot else self.PLOT_ATLAS_STAMP_TEXT_SIZE) * 1.3

        # Draw the luminosity and sqrt(s)
        if luminosity is not None or sqrt_s is not None:
            text = ''
            if sqrt_s is not None:
                text += '#sqrt{{s}} = {0:.0f} TeV'.format(sqrt_s / 1.0e6)
                if luminosity is not None:
                    text += ', '
            if luminosity is not None:
                if luminosity >= 1000.0:
                    text += '{0:.1f} fb^{{-1}}'.format(luminosity / 1000.0)
                elif luminosity > 100.0:
                    text += '{0:.2f} fb^{{-1}}'.format(luminosity / 1000.0)
                else:
                    text += '{0:.1f} pb^{{-1}}'.format(luminosity)

            stamp.DrawLatex(self.PLOT_ATLAS_STAMP_LEFT, top, text)
            top -= (self.PLOT_ATLAS_STAMP_TEXT_SIZE_WITH_RATIO if self.
                    _ratio_plot else self.PLOT_ATLAS_STAMP_TEXT_SIZE) * 1.3

        # If requested, draw the custom label or the 'ATLAS' label,
        # preferring the former
        if custom_label is not None:
            # Draw each line of text, decreasing top for each step
            for text in [t for t in custom_label if t is not None]:
                stamp.DrawLatex(self.PLOT_ATLAS_STAMP_LEFT, top, text)
                top -= (self.PLOT_ATLAS_STAMP_TEXT_SIZE_WITH_RATIO if self.
                        _ratio_plot else self.PLOT_ATLAS_STAMP_TEXT_SIZE) * 1.3

    def draw_pave(self, texts, position):
        """Draw a text box at the position and fill it with text.

        Args:
            texts: String or N-tuple of strings.
            positon: Absolute position of the form (x1, x2, y1, y2) as an
                     N-tuple of floats, or a string with one of the values
                     "{top,bottom}{left,right}"
        """
        if isinstance(position, basestring):
            if position == 'topleft':
                position = (0.15, 0.35, 0.85, 0.75)
            elif position == 'topright':
                position = (0.95, 0.75, 0.95, 0.85)
            elif position == 'bottomleft':
                position = (0.05, 0.25, 0.05, 0.15)
            elif position == 'topright':
                position = (0.95, 0.75, 0.05, 0.15)

        # Switch to the context of the main plot
        self._plot.cd()

        # Create the pave
        self._pave = TPaveText(position[0], position[2], position[1],
                               position[3], 'NDC')
        SetOwnership(self._pave, False)

        # Add the text
        if isinstance(texts, basestring):
            self._pave.AddText(texts)
        else:
            for t in texts:
                self._pave.AddText(t)

        # Draw the pave
        self._pave.Draw()

    def draw_legend(self, use_functions=False, legend_entries=None):
        """Draws a legend onto the plot with the specified histograms.

        It is recommended that you construct the Plot with plot_header = True
        in order to make space for the legend.

        Args:
            drawables: The elements to include in the legend (via AddEntry)
            use_functions: Add associated functions to the legend
        """
        # Check if we already have a legend
        if hasattr(self, '_legend'):
            raise RuntimeError('legend already exists on this plot')

        # Check if the plot has been drawn
        if not self._drawn:
            raise RuntimeError('plot must be drawn before the legend')

        # Remove None-valued drawables
        drawables = tuple((d for d in self._drawables if d is not None))

        # Remove TLine objects
        drawables = tuple((d for d in drawables if not is_line(d)))

        # If we shouldn't add functions to the legend, remove them
        if not use_functions:
            drawables = tuple((d for d in drawables if not isinstance(d, TF1)))

        # Use only certain entries
        if legend_entries is not None:

            def get_drawable_by_title(title):
                for d in drawables:
                    if d.GetTitle() == title:
                        return d
                return None

            drawables = tuple((get_drawable_by_title(e.GetTitle())
                               for e in legend_entries if e is not None))
            drawables = tuple((d for d in drawables if d is not None))

        # Switch to the context of the main plot
        self._plot.cd()

        # Create the legend
        self._legend = TLegend(self.PLOT_LEGEND_LEFT,
                               (self.PLOT_LEGEND_BOTTOM_WITH_RATIO if
                                self._ratio_plot else self.PLOT_LEGEND_BOTTOM),
                               self.PLOT_LEGEND_RIGHT,
                               (self.PLOT_LEGEND_TOP_WITH_RATIO
                                if self._ratio_plot else self.PLOT_LEGEND_TOP))
        SetOwnership(self._legend, False)

        # Style it
        self._legend.SetTextSize(
            (self.PLOT_LEGEND_TEXT_SIZE_WITH_RATIO
             if self._ratio_plot else self.PLOT_LEGEND_TEXT_SIZE))
        self._legend.SetBorderSize(0)
        self._legend.SetFillStyle(0)  # transparent
        self._legend.SetNColumns(self.PLOT_LEGEND_N_COLUMNS)

        # Create a chained list of all drawables.  We decompose THStack
        # objects in reverse order, i.e. top-to-bottom.
        drawables = \
            list(chain(*(drawable_iterable(h, True, True)
                         for h
                         in drawables)))

        # Add anything to this list that we created internally
        drawables.extend(self._legend_extras)

        # Because ROOT draws legend entries from left-to-right across rows and
        # not top-to-bottom along columns, we need to do a bit of a pivot on
        # the list so that the histograms appear in the vertical order of the
        # stack
        if self.PLOT_LEGEND_PIVOT_COLUMNS:
            n_entries = len(drawables)
            n_col = self.PLOT_LEGEND_N_COLUMNS
            n_row = int(ceil(float(n_entries) / n_col))
            self._legend.SetY1(self._legend.GetY2() - n_row *
                               (self.PLOT_LEGEND_ROW_SIZE_WITH_RATIO if self.
                                _ratio_plot else self.PLOT_LEGEND_ROW_SIZE))
            legend_order = []
            for r in xrange(0, n_row):
                for c in xrange(0, n_col):
                    if (r * n_col + c) == n_entries:
                        # Don't need an outer break, this would only happen on the
                        # last row if n_row * n_col != n_entries
                        break
                    legend_order.append(drawables[r + c * n_row])
        else:
            legend_order = drawables

        # Add the drawables
        for drawable in legend_order:
            SetOwnership(drawable, False)
            title = drawable.GetTitle()
            # HACK: Convention: legend for drawables with a non-default
            # marker style (data) to be drawn as line with point, and with
            # empty fill (signal) to be drawn as line
            if drawable.GetMarkerStyle() != 0:
                self._legend.AddEntry(drawable, title, 'ep')
            elif drawable.GetFillColor() == 0:
                self._legend.AddEntry(drawable, title, 'l')
            else:
                self._legend.AddEntry(drawable, title, 'f')

        # Draw the legend
        self._legend.Draw()
コード例 #7
0
def efficiencyAll():
    #signals = {'XZHeebb':['eebb'],'XZHmmbb':['mmbb'],'XZHnnbb':['nnbb'],'XZHee0b':['ee0b'],'XZHmm0b':['mm0b'],'XZHnn0b':['nn0b'],'XZHVBFeebbVBF':['eebbVBF'],'XZHVBFmmbbVBF':['mmbbVBF'],'XZHVBFnnbbVBF':['nnbbVBF'],'XZHVBFee0bVBF':['ee0bVBF'],'XZHVBFmm0bVBF':['mm0bVBF'],'XZHVBFnn0bVBF':['nn0bVBF']}
    labels = {'XZHeebb' : "eeb#bar{b}",'XZHmmbb' : "#mu#mub#bar{b}",'XZHnnbb' : "#nu#nub#bar{b}",'XZHee0b' : "ee0b",'XZHmm0b' : "#mu#mu0b",'XZHnn0b' : "#nu#nu0b",'XZHVBFeebbVBF' : "eeb#bar{b}VBF",'XZHVBFmmbbVBF' : "#mu#mub#bar{b}VBF",'XZHVBFnnbbVBF' : "#nu#nub#bar{b}VBF",'XZHVBFee0bVBF' : "ee0bVBF",'XZHVBFmm0bVBF' : "#mu#mu0bVBF",'XZHVBFnn0bVBF' : "#nu#nu0bVBF"}
    colors = {'XZHeebb' : 2, 'XZHmmbb' : 4, 'XZHnnbb' : 2,'XZHee0b' : 3, 'XZHmm0b' : 6, 'XZHnn0b' : 4,'XZHVBFeebbVBF' : 2, 'XZHVBFmmbbVBF' : 4, 'XZHVBFnnbbVBF' : 2,'XZHVBFee0bVBF' : 3, 'XZHVBFmm0bVBF' : 6, 'XZHVBFnn0bVBF' : 4}
    styles = {'XZHeebb' : 1, 'XZHmmbb' : 1, 'XZHnnbb' : 1,'XZHee0b' : 1, 'XZHmm0b' : 1, 'XZHnn0b' : 1,'XZHVBFeebbVBF' : 1, 'XZHVBFmmbbVBF' : 1, 'XZHVBFnnbbVBF' : 1,'XZHVBFee0bVBF' : 1, 'XZHVBFmm0bVBF' : 1, 'XZHVBFnn0bVBF' : 1}
    marker = {'XZHeebb' : 22, 'XZHmmbb' : 20, 'XZHnnbb' : 22,'XZHee0b' : 22, 'XZHmm0b' : 20, 'XZHnn0b' : 20, 'XZHVBFeebbVBF' : 22, 'XZHVBFmmbbVBF' : 20, 'XZHVBFnnbbVBF' : 22,'XZHVBFee0bVBF' : 22, 'XZHVBFmm0bVBF' : 20, 'XZHVBFnn0bVBF' : 20}
    genPoints = [800, 1000, 1200, 1400, 1600, 1800, 2000, 2500, 3000, 3500, 4000, 4500, 5000]
    eff = {}

    for signal_samples in ['ZlepHinc','ZinvHinc','ZinvHincVBF','ZlepHincVBF']:
        if signal_samples == 'ZinvHinc':
            signals = {'XZHnnbb':['nnbb'],'XZHnn0b':['nn0b']}
            sign_list = ['XZHnnbb','XZHnn0b']
        elif signal_samples == 'ZlepHinc':
            signals = {'XZHeebb':['eebb'],'XZHmmbb':['mmbb'],'XZHee0b':['ee0b'],'XZHmm0b':['mm0b']}
            sign_list = ['XZHeebb','XZHmmbb','XZHee0b', 'XZHmm0b']
        elif signal_samples == 'ZinvHincVBF':
            signals = {'XZHVBFnnbbVBF':['nnbbVBF'],'XZHVBFnn0bVBF':['nn0bVBF']}
            sign_list = ['XZHVBFnnbbVBF', 'XZHVBFnn0bVBF']
        elif signal_samples == 'ZlepHincVBF':
            signals = {'XZHVBFeebbVBF':['eebbVBF'],'XZHVBFmmbbVBF':['mmbbVBF'],'XZHVBFee0bVBF':['ee0bVBF'],'XZHVBFmm0bVBF':['mm0bVBF']}
            sign_list = ['XZHVBFeebbVBF', 'XZHVBFmmbbVBF','XZHVBFee0bVBF', 'XZHVBFmm0bVBF']
        for sign, channels in signals.iteritems():
      
            treeSign = {}
            ngenSign = {}
            nevtSign = {}
            eff[sign] = TGraphErrors()
            eff[sign].SetTitle(sign)
            eff[sign].SetMarkerColor(colors[sign])
            eff[sign].SetMarkerSize(1.25)
            eff[sign].SetLineColor(colors[sign])
            eff[sign].SetLineWidth(2)
            eff[sign].SetLineStyle(styles[sign])
            eff[sign].SetMarkerStyle(marker[sign])

            for i, m in enumerate(genPoints):
                neff = 0.
                for channel in channels:
                    if signal_samples == 'ZinvHinc':
                        file_list = ['Ntuples2016/XZH/ZprimeToZHToZinvHall_narrow_M%s'%m,'Ntuples2017/XZH/ZprimeToZHToZinvHall_narrow_M%s'%m,'Ntuples2018/XZH/ZprimeToZHToZinvHall_narrow_M%s'%m]
                    elif signal_samples == 'ZlepHinc':
                        file_list = ['Ntuples2016/XZH/ZprimeToZHToZlepHinc_narrow_M%s'%m,'Ntuples2017/XZH/ZprimeToZHToZlepHinc_narrow_M%s'%m,'Ntuples2018/XZH/ZprimeToZHToZlepHinc_narrow_M%s'%m]
                    elif signal_samples == 'ZinvHincVBF':
                        file_list = ['Ntuples2016/XZHVBF/Zprime_VBF_Zh_Zinvhinc_narrow_M-%s'%m,'Ntuples2017/XZHVBF/Zprime_VBF_Zh_Zinvhinc_narrow_M-%s'%m,'Ntuples2018/XZHVBF/Zprime_VBF_Zh_Zinvhinc_narrow_M-%s'%m]
                    elif signal_samples == 'ZlepHincVBF':
                        file_list = ['Ntuples2016/XZHVBF/Zprime_VBF_Zh_Zlephinc_narrow_M-%s'%m,'Ntuples2017/XZHVBF/Zprime_VBF_Zh_Zlephinc_narrow_M-%s'%m,'Ntuples2018/XZHVBF/Zprime_VBF_Zh_Zlephinc_narrow_M-%s'%m]
                    #if not 'VBF' in channel:
                    #    signMass = "XZH_M%d" % m
                    #else:
                    #    signMass = "XZHVBF_M%d" % m
                    ngenSign[m] = 0.
                    nevtSign[m] = 0.
                    #for j, ss in enumerate(sample[signMass]['files']):
                    for j, ss in enumerate(file_list):
                        sfile = TFile(NTUPLEDIR + ss + ".root", "READ")
                        if not sfile.Get("Events")==None:
                            ngenSign[m] += sfile.Get("Events").GetEntries()
                            # From trees
                            treeSign[m] = sfile.Get("tree")
                            nevtSign[m] += treeSign[m].GetEntries(selection[channel] + selection['SR'])
                        else:
                            ngenSign[m] = -1
                            print "Failed reading file", NTUPLEDIR + ss + ".root"
                        sfile.Close()
                    if nevtSign[m] == 0 or ngenSign[m] < 0: continue
                    # Gen Br
                    #print "m:",m
                    #print "nevtSign:",nevtSign[m]
                    #print "ngenSign:",ngenSign[m]
                    neff += nevtSign[m]/ngenSign[m]
                if 'ln' in sign or 'll' in sign: neff *= 1.5
                n = eff[sign].GetN()
                eff[sign].SetPoint(n, m, neff)
                eff[sign].SetPointError(n, 0, 0)

        n = 0. #max([eff[x].GetN() for x in channels])
        maxEff = 0.
        #sign_list = ['XZHeebb','XZHmmbb','XZHnnbb', 'XZHee0b', 'XZHmm0b', 'XZHnn0b','XZHVBFeebbVBF', 'XZHVBFmmbbVBF', 'XZHVBFnnbbVBF','XZHVBFee0bVBF', 'XZHVBFmm0bVBF', 'XZHVBFnn0bVBF']
        leg = TLegend(0.15, 0.15, 0.95, 0.35)
        #leg = TLegend(0.15, 0.7, 0.95, 0.8)
        leg.SetBorderSize(0)
        leg.SetFillStyle(0) #1001
        leg.SetFillColor(0)
        for sign in sign_list:
            #if eff[sign].GetN() > 0:
            leg.AddEntry(eff[sign], labels[sign], "pl")
            n += 1
        leg.SetNColumns(int(n/3))
        leg.SetY1(leg.GetY2()-n*0.045/leg.GetNColumns())
    
        n_error = max([eff[x].GetN() for x in sign_list])
    
        # Total efficiency
        eff["sum"] = TGraphErrors(n_error)
        eff["sum"].SetMarkerStyle(24)
        eff["sum"].SetMarkerColor(1)
        eff["sum"].SetLineWidth(2)
        for i in range(n_error):
            tot, mass = 0., 0.
            for sign in sign_list:
                if eff[sign].GetN() > i:
                    tot += eff[sign].GetY()[i]
                    mass = eff[sign].GetX()[i]
            if tot > maxEff: maxEff = tot
            eff["sum"].SetPoint(i, mass, tot)

        #legS = TLegend(0.55, 0.85-0.045, 0.95, 0.85)
        legS = TLegend(0.55, 0.35-0.045, 0.95, 0.35)
        legS.SetBorderSize(0)
        legS.SetFillStyle(0) #1001
        legS.SetFillColor(0)
        legS.AddEntry(eff['sum'], "%s Total efficiency"%signal_samples, "pl")

        c1 = TCanvas("c1", "Signal Efficiency", 1200, 800)
        c1.cd(1)
        c1.GetPad(0).SetTicks(1, 1)
        c1.SetLogy()
        first = sign_list[0]
        #if eff['XZHeebb'].GetN()!=0:
        #    first = 'XZHeebb'
        #else:
        #    first = 'XZHnnbb'
        eff[first].Draw("APL")
        for sign, channels in signals.iteritems():
            eff[sign].Draw("APL" if i==0 else "SAME, PL")
        eff["sum"].Draw("SAME, PL")
        leg.Draw()
        legS.Draw()

        setHistStyle(eff[first], 1.1)
        eff[first].SetTitle(";m_{X} (GeV);Acceptance #times efficiency")
        eff[first].SetMinimum(0.)
        eff[first].SetMaximum(max(1., maxEff*1.5)) #0.65
        eff[first].GetXaxis().SetTitleSize(0.045)
        eff[first].GetYaxis().SetTitleSize(0.045)
        eff[first].GetXaxis().SetLabelSize(0.045)
        eff[first].GetYaxis().SetLabelSize(0.045)
        eff[first].GetYaxis().SetTitleOffset(1.1)
        eff[first].GetXaxis().SetTitleOffset(1.05)
        eff[first].GetXaxis().SetRangeUser(750,5500)
        eff[first].GetYaxis().SetRangeUser(0., 0.4)
        drawCMS(-1,YEAR, "Simulation")
        """
        latex = TLatex()
        latex.SetNDC()
        latex.SetTextSize(0.05)
        latex.SetTextColor(1)
        latex.SetTextFont(42)
        latex.SetTextAlign(13)
        latex.DrawLatex(0.83, 0.99, "(13 TeV)")
        latex.SetTextFont(62)
        latex.SetTextSize(0.06)
        latex.DrawLatex(0.15, 0.90, "CMS")
        latex.SetTextSize(0.05)
        latex.SetTextFont(52)
        """
        #c1.Print("plotsSignal/Efficiency/Efficiency.pdf")
        #c1.Print("plotsSignal/Efficiency/Efficiency.png")
        c1.Print("plotsSignal/Efficiency/%s_Efficiency.pdf"%signal_samples)
        c1.Print("plotsSignal/Efficiency/%s_Efficiency.png"%signal_samples)
コード例 #8
0
def DeepCSV_pt_distribution(
        year):  ## everything below is jsut copy&past from above
    from root_numpy import root2array, fill_hist, array2root
    import numpy.lib.recfunctions as rfn
    from aliases import alias_deepCSV, WP_deepCSV
    ### Preliminary Operations ###
    treeRead = True
    var = 'jpt_1'
    channel = 'preselection'
    cut = alias_deepCSV['preselection']
    unit = ''
    if "GeV" in variable[var]['title']: unit = ' GeV'
    isBlind = BLIND and 'SR' in channel
    isAH = False
    showSignal = True
    stype = "HVT model B"
    if len(sign) > 0 and 'AZh' in sign[0]: stype = "2HDM"
    elif len(sign) > 0 and 'monoH' in sign[0]: stype = "Z'-2HDM m_{A}=300 GeV"
    if treeRead:
        for k in sorted(alias_deepCSV.keys(), key=len, reverse=True):
            if k in cut:
                cut = cut.replace(
                    k, alias_deepCSV[k].format(WP=WP_deepCSV[BTAGGING][year]))

    print "Plotting from", ("tree" if treeRead else
                            "file"), var, "in", channel, "channel with:"
    print "  cut    :", cut

    ### Create and fill MC histograms ###
    # Create dict
    file = {}
    tree = {}
    hist = {}
    N_signal_tot = 0.
    N_signal_tag = 0.

    ### Create and fill MC histograms ###
    for i, s in enumerate(back + sign):

        if variable[var]['nbins'] > 0:
            hist[s] = TH1F(
                s, ";jet p_{T};Events / ( " + str(
                    (variable[var]['max'] - variable[var]['min']) /
                    variable[var]['nbins']) + unit + " );" +
                ('log' if variable[var]['log'] else ''),
                variable[var]['nbins'], variable[var]['min'],
                variable[var]['max'])
        else:
            hist[s] = TH1F(
                s,
                ";jet p_{T};Events" + ('log' if variable[var]['log'] else ''),
                len(variable[var]['bins']) - 1,
                array('f', variable[var]['bins']))
        hist[s].Sumw2()

        for j, ss in enumerate(sample[s]['files']):
            if not 'data' in s:
                if year == "run2" or year in ss:
                    arr = root2array(NTUPLEDIR + ss + ".root",
                                     branches=["jpt_1", "eventWeightLumi"],
                                     selection=cut + " && jdeepCSV_1>" +
                                     str(WP_deepCSV[BTAGGING][year]))
                    if 'signal' in ss.lower():
                        N_signal_tag += len(arr['jpt_1'][arr['jpt_1'] > 3500])
                    print "imported " + NTUPLEDIR + ss + ".root"
                    fill_hist(hist[s],
                              arr["jpt_1"],
                              weights=arr["eventWeightLumi"])
                    arr = None

                    arr = root2array(NTUPLEDIR + ss + ".root",
                                     branches=["jpt_2", "eventWeightLumi"],
                                     selection=cut + " && jdeepCSV_2>" +
                                     str(WP_deepCSV[BTAGGING][year]))
                    print "imported " + NTUPLEDIR + ss + ".root"
                    if 'signal' in ss.lower():
                        N_signal_tag += len(arr['jpt_2'][arr['jpt_2'] > 3500])
                    fill_hist(hist[s],
                              arr["jpt_2"],
                              weights=arr["eventWeightLumi"])
                    arr = None

                    if 'signal' in ss.lower():
                        arr = root2array(NTUPLEDIR + ss + ".root",
                                         branches=["jpt_1", "eventWeightLumi"],
                                         selection=cut)
                        N_signal_tot += len(arr['jpt_1'][arr['jpt_1'] > 3500])
                        arr = None

                        arr = root2array(NTUPLEDIR + ss + ".root",
                                         branches=["jpt_2", "eventWeightLumi"],
                                         selection=cut)
                        N_signal_tot += len(arr['jpt_2'][arr['jpt_2'] > 3500])
                        arr = None

        hist[s].Scale(sample[s]['weight'] if hist[s].Integral() >= 0 else 0)
        hist[s].SetFillColor(sample[s]['fillcolor'])
        hist[s].SetFillStyle(sample[s]['fillstyle'])
        hist[s].SetLineColor(sample[s]['linecolor'])
        hist[s].SetLineStyle(sample[s]['linestyle'])

    if channel.endswith('TR') and channel.replace('TR', '') in topSF:
        hist['TTbarSL'].Scale(topSF[channel.replace('TR', '')][0])
        hist['ST'].Scale(topSF[channel.replace('TR', '')][0])

    hist['BkgSum'] = hist['data_obs'].Clone(
        "BkgSum") if 'data_obs' in hist else hist[back[0]].Clone("BkgSum")
    hist['BkgSum'].Reset("MICES")
    hist['BkgSum'].SetFillStyle(3003)
    hist['BkgSum'].SetFillColor(1)
    for i, s in enumerate(back):
        hist['BkgSum'].Add(hist[s])

    # Create data and Bkg sum histograms
    if options.blind or 'SR' in channel:
        hist['data_obs'] = hist['BkgSum'].Clone("data_obs")
        hist['data_obs'].Reset("MICES")
    # Set histogram style
    hist['data_obs'].SetMarkerStyle(20)
    hist['data_obs'].SetMarkerSize(1.25)

    for i, s in enumerate(back + sign + ['BkgSum']):
        addOverflow(hist[s], False)  # Add overflow
    for i, s in enumerate(sign):
        hist[s].SetLineWidth(3)
    for i, s in enumerate(sign):
        sample[s][
            'plot'] = True  #sample[s]['plot'] and s.startswith(channel[:2])

    if isAH:
        for i, s in enumerate(back):
            hist[s].SetFillStyle(3005)
            hist[s].SetLineWidth(2)
        #for i, s in enumerate(sign):
        #    hist[s].SetFillStyle(0)
        if not var == "Events":
            sfnorm = hist[data[0]].Integral() / hist['BkgSum'].Integral()
            print "Applying SF:", sfnorm
            for i, s in enumerate(back + ['BkgSum']):
                hist[s].Scale(sfnorm)
        if BLIND and var.endswith("Mass"):
            for i, s in enumerate(data + back + ['BkgSum']):
                first, last = hist[s].FindBin(65), hist[s].FindBin(135)
                for j in range(first, last):
                    hist[s].SetBinContent(j, -1.e-4)
        if BLIND and var.endswith("Tau21"):
            for i, s in enumerate(data):
                first, last = hist[s].FindBin(0), hist[s].FindBin(0.6)
                for j in range(first, last):
                    hist[s].SetBinContent(j, -1.e-4)

    # Create stack
    if variable[var]['nbins'] > 0:
        bkg = THStack(
            "Bkg",
            ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events / ( " + str(
                (variable[var]['max'] - variable[var]['min']) /
                variable[var]['nbins']) + unit + " )")
    else:
        bkg = THStack("Bkg",
                      ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events; ")
    for i, s in enumerate(back):
        bkg.Add(hist[s])

    # Legend
    leg = TLegend(0.65, 0.6, 0.95, 0.9)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    if len(data) > 0:
        leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pe")
    for i, s in reversed(list(enumerate(['BkgSum'] + back))):
        leg.AddEntry(hist[s], sample[s]['label'], "f")
    if showSignal:
        for i, s in enumerate(sign):
            if sample[s]['plot']:
                leg.AddEntry(hist[s], sample[s]['label'], "fl")

    leg.SetY1(0.9 - leg.GetNRows() * 0.05)

    # --- Display ---
    c1 = TCanvas("c1",
                 hist.values()[0].GetXaxis().GetTitle(), 800,
                 800 if RATIO else 600)

    if RATIO:
        c1.Divide(1, 2)
        setTopPad(c1.GetPad(1), RATIO)
        setBotPad(c1.GetPad(2), RATIO)
    c1.cd(1)
    c1.GetPad(bool(RATIO)).SetTopMargin(0.06)
    c1.GetPad(bool(RATIO)).SetRightMargin(0.05)
    c1.GetPad(bool(RATIO)).SetTicks(1, 1)

    log = variable[var]['log']  #"log" in hist['BkgSum'].GetZaxis().GetTitle()
    if log: c1.GetPad(bool(RATIO)).SetLogy()

    # Draw
    bkg.Draw("HIST")  # stack
    hist['BkgSum'].Draw("SAME, E2")  # sum of bkg
    if not isBlind and len(data) > 0: hist['data_obs'].Draw("SAME, PE")  # data
    if 'sync' in hist: hist['sync'].Draw("SAME, PE")
    #data_graph.Draw("SAME, PE")
    if showSignal:
        smagn = 1.  #if treeRead else 1.e2 #if log else 1.e2
        for i, s in enumerate(sign):
            #        if sample[s]['plot']:
            hist[s].Scale(smagn)
            hist[s].Draw(
                "SAME, HIST"
            )  # signals Normalized, hist[s].Integral()*sample[s]['weight']
        textS = drawText(0.80, 0.9 - leg.GetNRows() * 0.05 - 0.02,
                         stype + " (x%d)" % smagn, True)
    #bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset()*1.075)
    bkg.GetYaxis().SetTitleOffset(0.9)
    #bkg.GetYaxis().SetTitleOffset(2.)
    bkg.SetMaximum((5. if log else 1.25) * max(
        bkg.GetMaximum(),
        hist['data_obs'].GetBinContent(hist['data_obs'].GetMaximumBin()) +
        hist['data_obs'].GetBinError(hist['data_obs'].GetMaximumBin())))
    #if bkg.GetMaximum() < max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum()): bkg.SetMaximum(max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum())*1.25)
    bkg.SetMinimum(
        max(
            min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin(
            )), hist['data_obs'].GetMinimum()), 5.e-1) if log else 0.)
    if log:
        bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4)
        #bkg.GetYaxis().SetMoreLogLabels(True)
    bkg.GetXaxis().SetRangeUser(variable[var]['min'], variable[var]['max'])

    #if log: bkg.SetMinimum(1)
    leg.Draw()
    #drawCMS(LUMI[year], "Preliminary")
    drawCMS(LUMI[year], "", suppressCMS=True)
    drawRegion('XVH' + channel, True)
    drawAnalysis(channel)

    setHistStyle(bkg, 1.2 if RATIO else 1.1)
    setHistStyle(hist['BkgSum'], 1.2 if RATIO else 1.1)

    if RATIO:
        c1.cd(2)
        err = hist['BkgSum'].Clone("BkgErr;")
        err.SetTitle("")
        err.GetYaxis().SetTitle("Data / MC")
        err.GetYaxis().SetTitleOffset(0.9)

        err.GetXaxis().SetRangeUser(variable[var]['min'], variable[var]['max'])
        for i in range(1, err.GetNbinsX() + 1):
            err.SetBinContent(i, 1)
            if hist['BkgSum'].GetBinContent(i) > 0:
                err.SetBinError(
                    i, hist['BkgSum'].GetBinError(i) /
                    hist['BkgSum'].GetBinContent(i))
        setBotStyle(err)
        errLine = err.Clone("errLine")
        errLine.SetLineWidth(1)
        errLine.SetFillStyle(0)
        res = hist['data_obs'].Clone("Residues")
        for i in range(0, res.GetNbinsX() + 1):
            if hist['BkgSum'].GetBinContent(i) > 0:
                res.SetBinContent(
                    i,
                    res.GetBinContent(i) / hist['BkgSum'].GetBinContent(i))
                res.SetBinError(
                    i,
                    res.GetBinError(i) / hist['BkgSum'].GetBinContent(i))
        if 'sync' in hist:
            res.SetMarkerColor(2)
            res.SetMarkerStyle(31)
            res.Reset()
            for i in range(0, res.GetNbinsX() + 1):
                x = hist['data_obs'].GetXaxis().GetBinCenter(i)
                if hist['sync'].GetBinContent(hist['sync'].FindBin(x)) > 0:
                    res.SetBinContent(
                        i, hist['data_obs'].GetBinContent(
                            hist['data_obs'].FindBin(x)) /
                        hist['sync'].GetBinContent(hist['sync'].FindBin(x)))
                    res.SetBinError(
                        i, hist['data_obs'].GetBinError(
                            hist['data_obs'].FindBin(x)) /
                        hist['sync'].GetBinContent(hist['sync'].FindBin(x)))
        setBotStyle(res)
        #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5)
        #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2)
        err.Draw("E2")
        errLine.Draw("SAME, HIST")
        if not isBlind and len(data) > 0:
            res.Draw("SAME, PE0")
            #res_graph.Draw("SAME, PE0")
            if len(err.GetXaxis().GetBinLabel(
                    1)) == 0:  # Bin labels: not a ordinary plot
                drawRatio(hist['data_obs'], hist['BkgSum'])
                drawStat(hist['data_obs'], hist['BkgSum'])

    c1.Update()

    if gROOT.IsBatch():
        if channel == "": channel = "nocut"
        varname = var.replace('.', '_').replace('()', '')
        if not os.path.exists("plots/" + channel):
            os.makedirs("plots/" + channel)
        suffix = ''
        if "b" in channel or 'mu' in channel: suffix += "_" + BTAGGING
        c1.Print("plots/MANtag_study/deepCSV_plots/pt_" + year + suffix +
                 ".png")
        c1.Print("plots/MANtag_study/deepCSV_plots/pt_" + year + suffix +
                 ".pdf")

    # Print table
    printTable(hist, sign)

    print 'deepCSV efficiency:', N_signal_tag / N_signal_tot
コード例 #9
0
def plot(var, cut, year, norm=False, nm1=False):
    ### Preliminary Operations ###
    treeRead = not cut in [
        "nnqq", "en", "enqq", "mn", "mnqq", "ee", "eeqq", "mm", "mmqq", "em",
        "emqq", "qqqq"
    ]  # Read from tree
    channel = cut
    unit = ''
    if "GeV" in variable[var]['title']: unit = ' GeV'
    isBlind = BLIND and 'SR' in channel
    isAH = False  #'qqqq' in channel or 'hp' in channel or 'lp' in channel
    showSignal = False if 'SB' in cut or 'TR' in cut else True  #'SR' in channel or channel=='qqqq'#or len(channel)==5
    stype = "HVT model B"
    if len(sign) > 0 and 'AZh' in sign[0]: stype = "2HDM"
    elif len(sign) > 0 and 'monoH' in sign[0]: stype = "Z'-2HDM m_{A}=300 GeV"
    if treeRead:
        for k in sorted(alias.keys(), key=len, reverse=True):
            if BTAGGING == 'semimedium':
                if k in cut:
                    if ADDSELECTION:
                        cut = cut.replace(
                            k, aliasSM[k] + SELECTIONS[options.selection])
                    else:
                        cut = cut.replace(k, aliasSM[k])

            else:
                if k in cut:
                    if ADDSELECTION:
                        cut = cut.replace(
                            k, alias[k].format(WP=working_points[BTAGGING]) +
                            SELECTIONS[options.selection])
                    else:
                        cut = cut.replace(
                            k, alias[k].format(WP=working_points[BTAGGING]))

    # Determine Primary Dataset
    pd = sample['data_obs']['files']

    print "Plotting from", ("tree" if treeRead else
                            "file"), var, "in", channel, "channel with:"
    print "  dataset:", pd
    print "  cut    :", cut

    if var == 'jj_deltaEta_widejet':
        if "jj_deltaEta_widejet<1.1 && " in cut:
            print
            print "omitting jj_deltaEta_widejet<1.1 cut to draw the deltaEta distribution"
            print
            cut = cut.replace("jj_deltaEta_widejet<1.1 && ", "")
        else:
            print
            print "no 'jj_deltaEta_widejet<1.1 && ' in the cut string detected, so it cannot be ommited explicitly"
            print

    ### Create and fill MC histograms ###
    # Create dict
    file = {}
    tree = {}
    hist = {}

    ### Create and fill MC histograms ###
    for i, s in enumerate(data + back + sign):
        if treeRead:  # Project from tree
            tree[s] = TChain("tree")
            for j, ss in enumerate(sample[s]['files']):
                if not 'data' in s or ('data' in s and ss in pd):
                    if year == "run2" or year in ss:
                        tree[s].Add(NTUPLEDIR + ss + ".root")
            if variable[var]['nbins'] > 0:
                hist[s] = TH1F(
                    s, ";" + variable[var]['title'] + ";Events / ( " + str(
                        (variable[var]['max'] - variable[var]['min']) /
                        variable[var]['nbins']) + unit + " );" +
                    ('log' if variable[var]['log'] else ''),
                    variable[var]['nbins'], variable[var]['min'],
                    variable[var]['max'])
            else:
                hist[s] = TH1F(
                    s, ";" + variable[var]['title'] + ";Events" +
                    ('log' if variable[var]['log'] else ''),
                    len(variable[var]['bins']) - 1,
                    array('f', variable[var]['bins']))
            hist[s].Sumw2()
            cutstring = "(eventWeightLumi)" + ("*(" + cut +
                                               ")" if len(cut) > 0 else "")
            tree[s].Project(s, var, cutstring)
            if not tree[s].GetTree() == None:
                hist[s].SetOption("%s" % tree[s].GetTree().GetEntriesFast())
        else:  # Histogram written to file
            for j, ss in enumerate(sample[s]['files']):
                if not 'data' in s or ('data' in s and ss in pd):
                    file[ss] = TFile(NTUPLEDIR + ss + ".root", "R")
                    if file[ss].IsZombie():
                        print "WARNING: file", NTUPLEDIR + ss + ".root", "does not exist"
                        continue
                    tmphist = file[ss].Get(cut + "/" + var)
                    if tmphist == None: continue
                    if not s in hist.keys(): hist[s] = tmphist
                    else: hist[s].Add(tmphist)
        hist[s].Scale(sample[s]['weight'] if hist[s].Integral() >= 0 else 0)
        hist[s].SetFillColor(sample[s]['fillcolor'])
        hist[s].SetFillStyle(sample[s]['fillstyle'])
        hist[s].SetLineColor(sample[s]['linecolor'])
        hist[s].SetLineStyle(sample[s]['linestyle'])

    if channel.endswith('TR') and channel.replace('TR', '') in topSF:
        hist['TTbarSL'].Scale(topSF[channel.replace('TR', '')][0])
        hist['ST'].Scale(topSF[channel.replace('TR', '')][0])

    hist['BkgSum'] = hist['data_obs'].Clone(
        "BkgSum") if 'data_obs' in hist else hist[back[0]].Clone("BkgSum")
    hist['BkgSum'].Reset("MICES")
    hist['BkgSum'].SetFillStyle(3003)
    hist['BkgSum'].SetFillColor(1)
    for i, s in enumerate(back):
        hist['BkgSum'].Add(hist[s])

    if options.norm:
        for i, s in enumerate(back + ['BkgSum']):
            hist[s].Scale(hist[data[0]].Integral() / hist['BkgSum'].Integral())

    # Create data and Bkg sum histograms
    if options.blind or 'SR' in channel:
        hist['data_obs'] = hist['BkgSum'].Clone("data_obs")
        hist['data_obs'].Reset("MICES")
    # Set histogram style
    hist['data_obs'].SetMarkerStyle(20)
    hist['data_obs'].SetMarkerSize(1.25)

    for i, s in enumerate(data + back + sign + ['BkgSum']):
        addOverflow(hist[s], False)  # Add overflow
    for i, s in enumerate(sign):
        hist[s].SetLineWidth(3)
    for i, s in enumerate(sign):
        sample[s][
            'plot'] = True  #sample[s]['plot'] and s.startswith(channel[:2])

    if isAH:
        for i, s in enumerate(back):
            hist[s].SetFillStyle(3005)
            hist[s].SetLineWidth(2)
        #for i, s in enumerate(sign):
        #    hist[s].SetFillStyle(0)
        if not var == "Events":
            sfnorm = hist[data[0]].Integral() / hist['BkgSum'].Integral()
            print "Applying SF:", sfnorm
            for i, s in enumerate(back + ['BkgSum']):
                hist[s].Scale(sfnorm)
        if BLIND and var.endswith("Mass"):
            for i, s in enumerate(data + back + ['BkgSum']):
                first, last = hist[s].FindBin(65), hist[s].FindBin(135)
                for j in range(first, last):
                    hist[s].SetBinContent(j, -1.e-4)
        if BLIND and var.endswith("Tau21"):
            for i, s in enumerate(data):
                first, last = hist[s].FindBin(0), hist[s].FindBin(0.6)
                for j in range(first, last):
                    hist[s].SetBinContent(j, -1.e-4)

    if SYNC and var == "jj_mass_widejet" and year in ["2016", "2017", "2018"]:
        #iFile = TFile("sync/JetHT_run" + year + "_red_cert_scan.root", "READ")
        #hist['sync'] = iFile.Get("Mjj")
        if year == '2016':
            iFile = TFile("sync/2016/2016_07Aug2017_1246_1p1.root", "READ")
            hist['sync'] = iFile.Get("h_mjj_data")
        elif year == '2017':
            iFile = TFile(
                "sync/2017/histos_Run2017BCDEF_17Nov2017_JEC2017_mjj1530_cemf_lt_0p8_deltaETA_lt_1p1.root",
                "READ")
            hist['sync'] = iFile.Get("h_mjj_data")
        elif year == '2018':
            iFile = TFile(
                "sync/2018/Double_sideband_inputs_18v10_preliminary_v2.root",
                "READ")
            hist['sync'] = iFile.Get("h_mjj")

#        hist['sync'] = tmp.Rebin(len(dijet_bins)-1, "sync", array('d', dijet_bins))
#        hist['sync'] = tmp.Rebin(100, "sync")
        hist['sync'].SetMarkerStyle(31)
        hist['sync'].SetMarkerSize(1.25)
        hist['sync'].SetMarkerColor(2)
        print "Imported and drawing sync file"

    # Create stack
    if variable[var]['nbins'] > 0:
        bkg = THStack(
            "Bkg",
            ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events / ( " + str(
                (variable[var]['max'] - variable[var]['min']) /
                variable[var]['nbins']) + unit + " )")
    else:
        bkg = THStack("Bkg",
                      ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events; ")
    for i, s in enumerate(back):
        bkg.Add(hist[s])

    # Legend
    leg = TLegend(0.65, 0.6, 0.95, 0.9)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    if len(data) > 0:
        leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pe")
    for i, s in reversed(list(enumerate(['BkgSum'] + back))):
        leg.AddEntry(hist[s], sample[s]['label'], "f")
    if showSignal:
        for i, s in enumerate(sign):
            if sample[s]['plot']:
                leg.AddEntry(hist[s], sample[s]['label'], "fl")

    leg.SetY1(0.9 - leg.GetNRows() * 0.05)

    # --- Display ---
    c1 = TCanvas("c1",
                 hist.values()[0].GetXaxis().GetTitle(), 800,
                 800 if RATIO else 600)

    if RATIO:
        c1.Divide(1, 2)
        setTopPad(c1.GetPad(1), RATIO)
        setBotPad(c1.GetPad(2), RATIO)
    c1.cd(1)
    c1.GetPad(bool(RATIO)).SetTopMargin(0.06)
    c1.GetPad(bool(RATIO)).SetRightMargin(0.05)
    c1.GetPad(bool(RATIO)).SetTicks(1, 1)

    log = variable[var]['log']  #"log" in hist['BkgSum'].GetZaxis().GetTitle()
    if log: c1.GetPad(bool(RATIO)).SetLogy()

    # Draw
    bkg.Draw("HIST")  # stack
    hist['BkgSum'].Draw("SAME, E2")  # sum of bkg
    if not isBlind and len(data) > 0: hist['data_obs'].Draw("SAME, PE")  # data
    if 'sync' in hist: hist['sync'].Draw("SAME, PE")
    #data_graph.Draw("SAME, PE")
    if showSignal:
        smagn = 1.  #if treeRead else 1.e2 #if log else 1.e2
        for i, s in enumerate(sign):
            #        if sample[s]['plot']:
            hist[s].Scale(smagn)
            hist[s].Draw(
                "SAME, HIST"
            )  # signals Normalized, hist[s].Integral()*sample[s]['weight']
        textS = drawText(0.80, 0.9 - leg.GetNRows() * 0.05 - 0.02,
                         stype + " (x%d)" % smagn, True)
    #bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset()*1.075)
    bkg.GetYaxis().SetTitleOffset(0.9)
    #bkg.GetYaxis().SetTitleOffset(2.)
    bkg.SetMaximum((5. if log else 1.25) * max(
        bkg.GetMaximum(),
        hist['data_obs'].GetBinContent(hist['data_obs'].GetMaximumBin()) +
        hist['data_obs'].GetBinError(hist['data_obs'].GetMaximumBin())))
    #if bkg.GetMaximum() < max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum()): bkg.SetMaximum(max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum())*1.25)
    bkg.SetMinimum(
        max(
            min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin(
            )), hist['data_obs'].GetMinimum()), 5.e-1) if log else 0.)
    if log:
        bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4)
        #bkg.GetYaxis().SetMoreLogLabels(True)
    bkg.GetXaxis().SetRangeUser(variable[var]['min'], variable[var]['max'])

    #if log: bkg.SetMinimum(1)
    leg.Draw()
    #drawCMS(LUMI[year], "Preliminary")
    #drawCMS(LUMI[year], "Work in Progress", suppressCMS=True)
    drawCMS(LUMI[year], "", suppressCMS=True)
    drawRegion('XVH' + channel, True)
    drawAnalysis(channel)

    setHistStyle(bkg, 1.2 if RATIO else 1.1)
    setHistStyle(hist['BkgSum'], 1.2 if RATIO else 1.1)

    if RATIO:
        c1.cd(2)
        err = hist['BkgSum'].Clone("BkgErr;")
        err.SetTitle("")
        if SYNC:
            err.GetYaxis().SetTitle("Nano/Mini")
        else:
            err.GetYaxis().SetTitle("Data / MC")
        err.GetYaxis().SetTitleOffset(0.9)

        err.GetXaxis().SetRangeUser(variable[var]['min'], variable[var]['max'])
        for i in range(1, err.GetNbinsX() + 1):
            err.SetBinContent(i, 1)
            if hist['BkgSum'].GetBinContent(i) > 0:
                err.SetBinError(
                    i, hist['BkgSum'].GetBinError(i) /
                    hist['BkgSum'].GetBinContent(i))
        setBotStyle(err)
        errLine = err.Clone("errLine")
        errLine.SetLineWidth(1)
        errLine.SetFillStyle(0)
        res = hist['data_obs'].Clone("Residues")
        for i in range(0, res.GetNbinsX() + 1):
            if hist['BkgSum'].GetBinContent(i) > 0:
                res.SetBinContent(
                    i,
                    res.GetBinContent(i) / hist['BkgSum'].GetBinContent(i))
                res.SetBinError(
                    i,
                    res.GetBinError(i) / hist['BkgSum'].GetBinContent(i))
        if 'sync' in hist:
            res.SetMarkerColor(1)
            res.SetMarkerStyle(20)
            res.Reset()
            for i in range(0, res.GetNbinsX() + 1):
                x = hist['data_obs'].GetXaxis().GetBinCenter(i)
                if hist['sync'].GetBinContent(hist['sync'].FindBin(x)) > 0:
                    res.SetBinContent(
                        i, hist['data_obs'].GetBinContent(
                            hist['data_obs'].FindBin(x)) /
                        hist['sync'].GetBinContent(hist['sync'].FindBin(x)))
                    res.SetBinError(
                        i, hist['data_obs'].GetBinError(
                            hist['data_obs'].FindBin(x)) /
                        hist['sync'].GetBinContent(hist['sync'].FindBin(x)))
        setBotStyle(res)
        #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5)
        #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2)
        err.Draw("E2")
        errLine.Draw("SAME, HIST")
        if not isBlind and len(data) > 0:
            res.Draw("SAME, PE0")
            #res_graph.Draw("SAME, PE0")
            if len(err.GetXaxis().GetBinLabel(
                    1)) == 0:  # Bin labels: not a ordinary plot
                drawRatio(hist['data_obs'], hist['BkgSum'])
                drawStat(hist['data_obs'], hist['BkgSum'])
        if SYNC: err.GetYaxis().SetRangeUser(0.9, 1.1)

    c1.Update()

    if gROOT.IsBatch():
        if channel == "": channel = "nocut"
        varname = var.replace('.', '_').replace('()', '')
        if not os.path.exists("plots/" + channel):
            os.makedirs("plots/" + channel)
        suffix = ''
        if "b" in channel or 'mu' in channel: suffix += "_" + BTAGGING
        if ADDSELECTION: suffix += "_" + options.selection
        c1.Print("plots/" + channel + "/" + varname + "_" + year + suffix +
                 ".png")
        c1.Print("plots/" + channel + "/" + varname + "_" + year + suffix +
                 ".pdf")

    # Print table
    printTable(hist, sign)

    #    if True:
    #        sFile = TFile("sync/data_2016.root", "RECREATE")
    #        sFile.cd()
    #        hist['data_obs'].

    if not gROOT.IsBatch(): raw_input("Press Enter to continue...")
コード例 #10
0
def plotPrePost(category, category2):
    if len(category) == 0:
        print "Please select a category with the -c option"
        exit()

    is2 = len(category2) > 0
    if 'l' in category:
        category, category2, is2 = category.replace('l',
                                                    'e'), category.replace(
                                                        'l', 'm'), True
    isAH = len(category) >= 5
    X_name = "JJ_mass" if isAH else "VH_mass"
    signalName = signal + category + "_M%d" % mass
    data = ["data_obs"]
    back = ["Bkg_" + category] if isAH else [
        "VV_" + category, "Top_" + category, "Vjets_" + category
    ]
    sign = [signalName] if len(signal) > 0 else []

    fitr = ["total", "total_signal", "total_background"]

    labels = {
        "data_obs": "Data",
        "VV_" + category: "VV, Vh",
        "Top_" + category: "t#bar{t}, t+X",
        "Vjets_" + category: "V+jets",
        "Bkg_" + category: "Bkg fit",
        signalName: "Signal"
    }
    if 'ee' in category or 'mm' in category:
        labels["Vjets_" + category] = "Z(ll)+jets"
    elif 'en' in category or 'mn' in category:
        labels["Vjets_" + category] = "W(l#nu)+jets"
    elif 'nn' in category:
        labels["Vjets_" + category] = "Z(#nu#nu),W(l#nu)+jets"

    lastBin = 3500. if 'ee' in category or 'mm' in category or 'nn' in category else 4500.

    #    xs = 0.
    #    if 'XWH' in signal or 'XVH' in signal: xs += HVT['B3']['W']['XS'][mass]*HVT['B3']['W']['BR'][mass]
    #    if 'XZH' in signal or 'XVH' in signal: xs += HVT['B3']['Z']['XS'][mass]*HVT['B3']['Z']['BR'][mass]
    xs = getCrossSection(signal, category, mass)
    #if len(signal2)>0: xs[signal2] = getCrossSection(signal2, category, 0)

    histData, histPre, histPost, graphPre, graphPost = None, {}, {}, {}, {}

    if readData:
        dataFile = TFile("workspace/" + category + ".root", "READ")
        workspace = dataFile.Get("VH_2016")
        variable = workspace.var(X_name)
        dataset = workspace.data(data[0])
        if is2:
            dataFile2 = TFile("workspace/" + category2 + ".root", "READ")
            workspace2 = dataFile2.Get("VH_2016")
            dataset2 = workspace2.data(data[0])
            dataset.append(dataset2)
        data2 = dataset.createHistogram(variable, variable,
                                        variable.getBinning().numBins() / 10,
                                        1)
        data1 = data2.ProjectionX()
        data1.SetMarkerStyle(20)
        data1.SetMarkerSize(1.25)
        data1.SetLineColor(1)
        histData = data1
        graphData = convertHistToGraph(histData, True)
        width = histData.GetXaxis().GetBinWidth(1)

    inFile = TFile(options.fileName, "READ")
    if inFile == None:
        print "File", options.fileName, "not found"
        return
    if not inFile.GetDirectory("shapes_prefit/" + category):
        print "Category", category, "not recognized"
        return

    for i, h in enumerate(back + sign + fitr):
        histPre[h] = inFile.Get("shapes_prefit/" + category + "/" + h)
        if is2:
            histPre[h].Add(
                inFile.Get("shapes_prefit/" + category2 + "/" +
                           h.replace(category, category2)))
        histPre[h].SetName(h + '_pre')
        histPre[h].SetLineColor(getColor(h, category))
        histPre[h].SetFillColor(getColor(h, category))
        histPre[h].SetLineWidth(3)
        if h in back: histPre[h].SetFillStyle(1001)
        elif h in fitr: histPre[h].SetFillStyle(3002)
        elif h in sign:
            histPre[h].SetTitle("m_{%s'} = %d GeV" % (signal[1], mass))
            histPre[h].SetOption("HVT model B g_{V}=3")
            histPre[h].SetFillStyle(1)
            histPre[h].SetLineStyle(3)
            histPre[h].SetLineWidth(6)
            if xs > 0.: histPre[h].Scale(xs * 1000.)
    #    histPre[h].Rebin(10)
        if readData:
            histPre[h].Scale(width)
            histPre[h].GetYaxis().SetTitle("Events / ( %d GeV )" % width)
        if 'nn' in category:
            histPre[h].GetXaxis().SetTitle(
                histPre[h].GetXaxis().GetTitle().replace(
                    'm_{VH}', 'm^{T}_{VH}'))

    for i, h in enumerate(back + sign + fitr):
        histPost[h] = inFile.Get("shapes_fit_b/" + category + "/" + h)
        if is2:
            histPost[h].Add(
                inFile.Get("shapes_fit_b/" + category2 + "/" +
                           h.replace(category, category2)))
        histPost[h].SetName(h + '_post')
        histPost[h].SetLineColor(getColor(h, category))
        histPost[h].SetFillColor(getColor(h, category))
        histPost[h].SetLineWidth(3)
        if h in back: histPost[h].SetFillStyle(1001)
        elif h in fitr: histPost[h].SetFillStyle(3002)
        elif h in sign:
            histPost[h].SetTitle("m_{%s'} = %d GeV" % (signal[1], mass))
            histPost[h].SetOption("HVT model B g_{V}=3")
            histPost[h].SetFillStyle(1)
            histPost[h].SetLineStyle(3)
            histPost[h].SetLineWidth(6)
            if xs > 0.: histPost[h].Scale(xs * 1000.)
    #    histPost[h].Rebin(10)
        if readData:
            histPost[h].Scale(width)
            histPost[h].GetYaxis().SetTitle("Events / ( %d GeV )" % width)
        if 'nn' in category:
            histPost[h].GetXaxis().SetTitle(
                histPost[h].GetXaxis().GetTitle().replace(
                    'm_{VH}', 'm^{T}_{VH}'))

    # Set errors ot zero to have smooth curves
    for i, h in enumerate(back + sign):
        for i in range(histPre[h].GetNbinsX()):
            histPre[h].SetBinError(i + 1, 0.)
        for i in range(histPost[h].GetNbinsX()):
            histPost[h].SetBinError(i + 1, 0.)

    stackPre = THStack(
        "Pre", ";" + histPre['total'].GetXaxis().GetTitle() + ";" +
        histPre['total'].GetYaxis().GetTitle())
    for i, s in enumerate(back):
        stackPre.Add(histPre[s])

    stackPost = THStack(
        "Post", ";" + histPost['total'].GetXaxis().GetTitle() + ";" +
        histPost['total'].GetYaxis().GetTitle())
    for i, s in enumerate(back):
        stackPost.Add(histPost[s])

    for i, h in enumerate(back):
        tmpPre = histPre[back[i]].Clone(back[i] + "_stack_pre")
        for j in range(i):
            tmpPre.Add(histPre[back[j]])
        graphPre[back[i]] = convertHistToGraph(tmpPre)
        tmpPost = histPost[back[i]].Clone(back[i] + "_stack_post")
        for j in range(i):
            tmpPost.Add(histPost[back[j]])
        graphPost[back[i]] = convertHistToGraph(tmpPost)

    # Additional signal, if present
    inFiles = {}
    for i, f in enumerate(
            signals):  #combine/test/mlfit_monoHnn_MZ3000_MA300.root
        signalName2 = f.replace('combine/test/mlfit_', '').replace('.root', '')
        if 'AZh' in signalName2:
            signalName2 = signalName2.replace('AZh', 'AZh' + category)
        signalSmpl2 = signalName2.replace(category, '')
        signal2 = signalName2.split('_M')[0].replace(category, '')
        try:
            mass2, mass2A = int(signalName2.split('_M')[1]), 0
        except:
            mass2, mass2A = int(
                signalName2.split('_MZ')[1].split('_MA')[0]), int(
                    signalName2.split('_MA')[1])
        #
        inFiles[signalName2] = TFile(f, "READ")
        histSign2 = inFiles[signalName2].Get("shapes_prefit/" + category +
                                             "/" + signalName2)
        if is2:
            histSign2.Add(inFiles[signalName2].Get(
                "shapes_prefit/" + category2 + "/" +
                signalName2.replace(category, category2)))
        histSign2.SetName(signalName2 + '_pre')
        if signal2.startswith('X'):
            histSign2.SetTitle("m_{V'} = %d GeV" % mass2)
        elif signal2.startswith('A'):
            histSign2.SetTitle("m_{A} = %d GeV" % mass2)
        elif not mass2 == 0:
            histSign2.SetTitle("m_{Z'} = %d GeV" % mass2)
        if i == 0:
            histSign2.SetOption(
                "Z'-2HDM\nm_{A}=300 GeV" if 'monoH' in signalName2 else
                "Type-II 2HDM\ncos(#beta-#alpha) = 0.25\ntan#beta = 1")
        histSign2.SetLineColor(getColor(signalName2, category))
        histSign2.SetFillColor(getColor(signalName2, category))
        histSign2.SetFillStyle(1)
        histSign2.SetLineStyle(5 + i)
        histSign2.SetLineWidth(5)
        if readData: histSign2.Scale(width)
        xs2 = getCrossSection(signalName2, category, 0)
        if xs2 > 0.: histSign2.Scale(xs2 * 1000.)
        sign += [signalName2]
        histPre[signalName2] = histSign2
        histPost[signalName2] = histSign2

    leg = TLegend(0.6 - 0.005, 0.6, 0.925, 0.9)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    if readData:
        leg.AddEntry(graphData, labels[data[0]], "pe")
    for i, s in reversed(list(enumerate(back))):
        leg.AddEntry(histPre[s], labels[s], "f")
    #for i, s in enumerate(sign):
    #    leg.AddEntry(histPre[s], labels[s], "fl")

    ###

    c1 = TCanvas("c1", "Pre-Post Fit", 800, 800)
    c1.Divide(1, 2)
    c1.cd(1)
    setTopPad(c1.GetPad(1), RATIO)
    setBotPad(c1.GetPad(2), RATIO)
    c1.GetPad(1).SetTopMargin(0.06)
    c1.GetPad(1).SetRightMargin(0.05)
    c1.GetPad(1).SetBottomMargin(0.01)
    c1.GetPad(1).SetTicks(1, 1)

    #histData.Draw("APE" if d==0 else "SAME, PE")
    #for i, h in enumerate(back): graphPost[h].Draw("ACL" if i==0 else "C")
    stackPost.Draw("C")
    setHistStyle(stackPost, 1.2)
    stackPost.SetMaximum(stackPost.GetMaximum() * 5.)
    stackPost.SetMinimum(max(stackPost.GetMinimum(), 0.2))
    stackPost.GetXaxis().SetRangeUser(stackPost.GetXaxis().GetXmin(), lastBin)
    histPost['total'].SetLineWidth(1)
    histPost['total'].Draw("SAME, E3")
    histPre['total_background'].SetLineColor(921)
    histPre['total_background'].SetLineStyle(2)
    histPre['total_background'].SetLineWidth(3)
    histPre['total_background'].SetFillColor(1)
    histPre['total_background'].SetFillStyle(0)
    histPre['total_background'].Draw("SAME, HIST")
    for i, s in enumerate(sign):
        histPre[s].Draw("SAME, L")
    if readData:
        graphData.Draw("SAME, PE0")

    leg.AddEntry(histPost['total'], "Bkg. unc.", "f")
    leg.AddEntry(histPre['total_background'], "Pre-fit", "l")
    for i, s in enumerate(sign):
        for o in histPre[s].GetOption().split('\n'):
            if len(o) > 0.: leg.AddEntry(None, o, "")
        leg.AddEntry(histPre[s], histPre[s].GetTitle(), "l")
    leg.SetY1(0.9 - leg.GetNRows() * 0.060)
    leg.Draw()
    #    if len(sign)>0:
    #        latex = TLatex()
    #        latex.SetNDC()
    #        latex.SetTextSize(0.045)
    #        latex.SetTextFont(42)
    #        latex.DrawLatex(0.67, leg.GetY1()-0.045, "HVT model B g_{V}=3")
    drawCMS(LUMI, "")  #Preliminary
    drawRegion('XVH' + options.category, True)
    drawAnalysis(category)

    c1.GetPad(1).SetLogy()

    c1.cd(2)
    err = histPost['total'].Clone("BkgErr;")
    err.SetTitle("")
    err.Reset("MICES")
    err.GetYaxis().SetTitle("(N^{data}-N^{bkg})/#sigma")
    setBotStyle(err, 4 + 1)
    err.GetXaxis().SetTitleSize(0.16)
    #err.GetXaxis().SetTitleOffset(1.25);
    err.GetYaxis().SetTitleOffset(0.33)
    err.GetXaxis().SetRangeUser(err.GetXaxis().GetXmin(), lastBin)
    err.GetYaxis().SetRangeUser(-5., 5.)
    err.SetLineWidth(2)
    err.SetLineStyle(2)
    err.SetFillStyle(0)
    err.Draw("L")  #"E2"
    if readData:
        pulls = makeResidHist(graphData, histPost['total'])
        #setBotStyle(pulls, RATIO, False)
        pulls.Draw("SAME, PE0")
        #drawRatio(hist['data_obs'], hist['BkgSum'])
        #drawStat(hist['data_obs'], hist['BkgSum'])
        chi2, nbins, npar = 0., 0, 0
        for i in range(0, pulls.GetN()):
            if graphData.GetY()[i] > 1.e-3:
                nbins = nbins + 1
                chi2 += pulls.GetY()[i]**2
        #drawChi2(chi2, nbins-npar, True)

    c1.Update()

    if not gROOT.IsBatch(): raw_input("Press Enter to continue...")

    c1.Print("plotsPrePost/BkgSR_" + options.category + ".png")
    c1.Print("plotsPrePost/BkgSR_" + options.category + ".pdf")

    ###

    if VERBOSE:
        c2 = TCanvas("c2", "Pre-Post Fit", 1200, 600)
        c2.Divide(2, 1)
        c2.cd(1)
        c2.GetPad(1).SetTopMargin(0.06)
        c2.GetPad(1).SetRightMargin(0.05)
        c2.GetPad(1).SetBottomMargin(0.10)
        c2.GetPad(1).SetTicks(1, 1)

        #histData.Draw("APE" if d==0 else "SAME, PE")
        #for i, h in enumerate(back): graphPre[h].Draw("FL" if i==0 else "SAME, FL")
        stackPre.Draw("HIST")
        histPre['total'].Draw("SAME, E2")
        stackPre.SetMaximum(stackPre.GetMaximum() * 5.)
        stackPre.SetMinimum(max(stackPre.GetMinimum(), 0.01))
        histData.Draw("SAME, PE")

        leg.Draw()
        drawCMS(LUMI, "Preliminary")
        drawRegion('XVH' + category, True)
        drawAnalysis(category)

        c2.cd(2)
        c2.GetPad(2).SetTopMargin(0.06)
        c2.GetPad(2).SetRightMargin(0.05)
        c2.GetPad(2).SetBottomMargin(0.10)
        c2.GetPad(2).SetTicks(1, 1)

        #histData.Draw("APE" if d==0 else "SAME, PE")
        #for i, h in enumerate(back): graphPost[h].Draw("ACL" if i==0 else "C")
        stackPost.Draw("HIST")
        histPost['total'].Draw("SAME, E2")
        stackPost.SetMaximum(stackPost.GetMaximum() * 5.)
        stackPost.SetMinimum(max(stackPost.GetMinimum(), 0.01))
        histData.Draw("SAME, PE")

        leg.Draw()
        drawCMS(LUMI, "Preliminary")
        drawRegion('XVH' + category, True)
        drawAnalysis(category)

        c2.GetPad(1).SetLogy()
        c2.GetPad(2).SetLogy()

        c2.Print("combine/test/" + signalName + "_prepost.png")
        c2.Print("combine/test/" + signalName + "_prepost.pdf")

        c2.Close()
コード例 #11
0
def train_and_apply():

    np.random.seed(1)
    ROOT.gROOT.SetBatch()

    #Extract data from root file
    tree = uproot.open("out_all.root")["outA/Tevts"]
    branch_mc = [
        "MC_B_P", "MC_B_eta", "MC_B_phi", "MC_B_pt", "MC_D0_P", "MC_D0_eta",
        "MC_D0_phi", "MC_D0_pt", "MC_Dst_P", "MC_Dst_eta", "MC_Dst_phi",
        "MC_Dst_pt", "MC_Est_mu", "MC_M2_miss", "MC_mu_P", "MC_mu_eta",
        "MC_mu_phi", "MC_mu_pt", "MC_pis_P", "MC_pis_eta", "MC_pis_phi",
        "MC_pis_pt", "MC_q2"
    ]
    branch_rec = [
        "B_P", "B_eta", "B_phi", "B_pt", "D0_P", "D0_eta", "D0_phi", "D0_pt",
        "Dst_P", "Dst_eta", "Dst_phi", "Dst_pt", "Est_mu", "M2_miss", "mu_P",
        "mu_eta", "mu_phi", "mu_pt", "pis_P", "pis_eta", "pis_phi", "pis_pt",
        "q2"
    ]
    nvariable = len(branch_mc)
    x_train = tree.array(branch_mc[0], entrystop=options.maxevents)
    for i in range(1, nvariable):
        x_train = np.vstack(
            (x_train, tree.array(branch_mc[i], entrystop=options.maxevents)))
    x_test = tree.array(branch_rec[0], entrystop=options.maxevents)
    for i in range(1, nvariable):
        x_test = np.vstack(
            (x_test, tree.array(branch_rec[i], entrystop=options.maxevents)))
    x_train = x_train.T
    x_test = x_test.T
    x_test = array2D_float(x_test)
    #Different type of reconstruction variables

    #BN normalization
    gamma = 0
    beta = 0.2

    ar = np.array(x_train)
    a = K.constant(ar[:, 0])
    mean = K.mean(a)
    var = K.var(a)
    x_train = K.eval(K.batch_normalization(a, mean, var, gamma, beta))
    for i in range(1, nvariable):
        a = K.constant(ar[:, i])
        mean = K.mean(a)
        var = K.var(a)
        a = K.eval(K.batch_normalization(a, mean, var, gamma, beta))
        x_train = np.vstack((x_train, a))
    x_train = x_train.T

    ar = np.array(x_test)
    a = K.constant(ar[:, 0])
    mean = K.mean(a)
    var = K.var(a)
    x_test = K.eval(K.batch_normalization(a, mean, var, gamma, beta))
    for i in range(1, nvariable):
        a = K.constant(ar[:, i])
        mean = K.mean(a)
        var = K.var(a)
        a = K.eval(K.batch_normalization(a, mean, var, gamma, beta))
        x_test = np.vstack((x_test, a))
    x_test = x_test.T

    #Add noise, remain to be improved
    noise = np.random.normal(loc=0.0, scale=0.01, size=x_train.shape)
    x_train_noisy = x_train + noise
    noise = np.random.normal(loc=0.0, scale=0.01, size=x_test.shape)
    x_test_noisy = x_test + noise
    x_train = np.clip(x_train, -1., 1.)
    x_test = np.clip(x_test, -1., 1.)
    x_train_noisy = np.clip(x_train_noisy, -1., 1.)
    x_test_noisy = np.clip(x_test_noisy, -1., 1.)

    # Network parameters
    input_shape = (x_train.shape[1], )
    batch_size = 128
    latent_dim = 2

    # Build the Autoencoder Model
    # First build the Encoder Model
    inputs = Input(shape=input_shape, name='encoder_input')
    x = inputs

    # Shape info needed to build Decoder Model
    shape = K.int_shape(x)

    # Generate the latent vector
    latent = Dense(latent_dim, name='latent_vector')(x)

    # Instantiate Encoder Model
    encoder = Model(inputs, latent, name='encoder')
    encoder.summary()

    # Build the Decoder Model
    latent_inputs = Input(shape=(latent_dim, ), name='decoder_input')
    x = Dense(shape[1])(latent_inputs)
    x = Reshape((shape[1], ))(x)
    outputs = Activation('tanh', name='decoder_output')(x)

    # Instantiate Decoder Model
    decoder = Model(latent_inputs, outputs, name='decoder')
    decoder.summary()

    # Autoencoder = Encoder + Decoder
    # Instantiate Autoencoder Model
    autoencoder = Model(inputs, decoder(encoder(inputs)), name='autoencoder')
    autoencoder.summary()

    autoencoder.compile(loss='mse', optimizer='adam')

    # Train the autoencoder
    autoencoder.fit(x_train_noisy,
                    x_train,
                    validation_data=(x_test_noisy, x_test),
                    epochs=options.epochs,
                    batch_size=batch_size)

    # Predict the Autoencoder output from corrupted test imformation
    x_decoded = autoencoder.predict(x_test_noisy)

    # Draw Comparision Plots
    c = TCanvas("c", "c", 700, 700)
    fPads1 = TPad("pad1", "Run2", 0.0, 0.29, 1.00, 1.00)
    fPads2 = TPad("pad2", "", 0.00, 0.00, 1.00, 0.29)
    fPads1.SetBottomMargin(0.007)
    fPads1.SetLeftMargin(0.10)
    fPads1.SetRightMargin(0.03)
    fPads2.SetLeftMargin(0.10)
    fPads2.SetRightMargin(0.03)
    fPads2.SetBottomMargin(0.25)
    fPads1.Draw()
    fPads2.Draw()
    fPads1.cd()
    nbin = 50
    min = -1.
    max = 1.
    variable = "P^{B}"
    lbin = (max - min) / nbin
    lbin = str(float((max - min) / nbin))
    xtitle = branch_rec[options.branch - 1]
    ytitle = "Events/" + lbin + "GeV"
    h_rec = TH1D("h_rec", "" + ";%s;%s" % (xtitle, ytitle), nbin, min, max)
    h_rec.Sumw2()
    h_pre = TH1D("h_pre", "" + ";%s;%s" % (xtitle, ytitle), nbin, min, max)
    h_pre.Sumw2()
    for i in range(x_test_noisy.shape[0]):
        h_rec.Fill(x_test_noisy[i][options.branch - 1])
        h_pre.Fill(x_decoded[i][options.branch - 1])
    h_rec = UnderOverFlow1D(h_rec)
    h_pre = UnderOverFlow1D(h_pre)
    maxY = TMath.Max(h_rec.GetMaximum(), h_pre.GetMaximum())
    h_rec.SetLineColor(2)
    h_rec.SetFillStyle(0)
    h_rec.SetLineWidth(2)
    h_rec.SetLineStyle(1)
    h_pre.SetLineColor(3)
    h_pre.SetFillStyle(0)
    h_pre.SetLineWidth(2)
    h_pre.SetLineStyle(1)
    h_rec.SetStats(0)
    h_pre.SetStats(0)
    h_rec.GetYaxis().SetRangeUser(0, maxY * 1.1)
    h_rec.Draw("HIST")
    h_pre.Draw("same HIST")
    h_rec.GetYaxis().SetTitleSize(0.06)
    h_rec.GetYaxis().SetTitleOffset(0.78)
    theLeg = TLegend(0.5, 0.45, 0.95, 0.82, "", "NDC")
    theLeg.SetName("theLegend")
    theLeg.SetBorderSize(0)
    theLeg.SetLineColor(0)
    theLeg.SetFillColor(0)
    theLeg.SetFillStyle(0)
    theLeg.SetLineWidth(0)
    theLeg.SetLineStyle(0)
    theLeg.SetTextFont(42)
    theLeg.SetTextSize(.05)
    theLeg.AddEntry(h_rec, "Reconstruction", "L")
    theLeg.AddEntry(h_pre, "Prediction", "L")
    theLeg.SetY1NDC(0.9 - 0.05 * 6 - 0.005)
    theLeg.SetY1(theLeg.GetY1NDC())
    fPads1.cd()
    theLeg.Draw()
    title = TLatex(
        0.91, 0.93, "AE prediction compare with reconstruction, epochs=" +
        str(options.epochs))
    title.SetNDC()
    title.SetTextSize(0.05)
    title.SetTextFont(42)
    title.SetTextAlign(31)
    title.SetLineWidth(2)
    title.Draw()
    fPads2.cd()
    h_Ratio = h_pre.Clone("h_Ratio")
    h_Ratio.Divide(h_rec)
    h_Ratio.SetLineColor(1)
    h_Ratio.SetLineWidth(2)
    h_Ratio.SetMarkerStyle(8)
    h_Ratio.SetMarkerSize(0.7)
    h_Ratio.GetYaxis().SetRangeUser(0, 2)
    h_Ratio.GetYaxis().SetNdivisions(504, 0)
    h_Ratio.GetYaxis().SetTitle("Pre/Rec")
    h_Ratio.GetYaxis().SetTitleOffset(0.35)
    h_Ratio.GetYaxis().SetTitleSize(0.13)
    h_Ratio.GetYaxis().SetTitleSize(0.13)
    h_Ratio.GetYaxis().SetLabelSize(0.11)
    h_Ratio.GetXaxis().SetLabelSize(0.1)
    h_Ratio.GetXaxis().SetTitleOffset(0.8)
    h_Ratio.GetXaxis().SetTitleSize(0.14)
    h_Ratio.SetStats(0)
    axis1 = TGaxis(min, 1, max, 1, 0, 0, 0, "L")
    axis1.SetLineColor(1)
    axis1.SetLineWidth(1)
    for i in range(1, h_Ratio.GetNbinsX() + 1, 1):
        D = h_rec.GetBinContent(i)
        eD = h_rec.GetBinError(i)
        if D == 0: eD = 0.92
        B = h_pre.GetBinContent(i)
        eB = h_pre.GetBinError(i)
        if B < 0.1 and eB >= B:
            eB = 0.92
            Err = 0.
        if B != 0.:
            Err = TMath.Sqrt((eD * eD) / (B * B) + (D * D * eB * eB) /
                             (B * B * B * B))
            h_Ratio.SetBinContent(i, D / B)
            h_Ratio.SetBinError(i, Err)
        if B == 0.:
            Err = TMath.Sqrt((eD * eD) / (eB * eB) + (D * D * eB * eB) /
                             (eB * eB * eB * eB))
            h_Ratio.SetBinContent(i, D / 0.92)
            h_Ratio.SetBinError(i, Err)
        if D == 0 and B == 0:
            h_Ratio.SetBinContent(i, -1)
            h_Ratio.SetBinError(i, 0)
        h_Ratio.Draw("e0")
        axis1.Draw()

    c.SaveAs(branch_rec[options.branch - 1] + "_comparision.png")
コード例 #12
0
         nc = int((lg - 1) / 12 + 1)
     leg.SetNColumns(nc)
     extra = (2 + 0.4 * int(lg / nc + 1))
     dv = (vmax - vmin) / 10.
     if valmin is None:
         grall[0].SetMinimum(vmin - dv)
     else:
         grall[0].SetMinimum(valmin)
     if valmax is None:
         grall[0].SetMaximum(vmax + dv * extra)
     else:
         grall[0].SetMaximum(valmax)
     if nc > 8:
         nc = 8
     leg.SetX1(0.9 - 0.1 * nc)
     leg.SetY1(0.9 - 0.8 * (extra - 1) / (11 + extra))
     leg.Draw()
 else:
     gr = TGraph(tree.GetSelectedRows(), tree.GetV2(), tree.GetV1())
     gr.SetTitle(gtitle)
     gr.SetMarkerStyle(20)
     gr.SetMarkerSize(1.3)
     gr.SetMarkerColor(2)
     gr.SetLineColor(2)
     gr.GetXaxis().SetNoExponent(kTRUE)
     if one_run:
         gr.GetXaxis().SetTitle("Lumi")
     else:
         gr.GetXaxis().SetTitle("Runs")
     if valmin is not None:
         grall[0].SetMinimum(valmin)
コード例 #13
0
        legDistr.AddEntry(hRawYieldFDVsCut[iPt], 'Non-prompt', 'f')
        legDistr.AddEntry(hRawYieldsVsCutReSum[iPt], 'Prompt + non-prompt',
                          'l')
        legEff.AddEntry(hEffPromptVsCut[iPt], 'Prompt', 'lpe')
        legEff.AddEntry(hEffFDVsCut[iPt], 'Non-prompt', 'lpe')
        legFrac.AddEntry(hPromptFracVsCut[iPt], 'Prompt', 'lpe')
        legFrac.AddEntry(hFDFracVsCut[iPt], 'Non-prompt', 'lpe')

        deltaY = 0.
        if compareToFc:
            legFrac.AddEntry(gPromptFracFcVsCut[iPt], 'Prompt #it{f}_{c}',
                             'fp')
            legFrac.AddEntry(gFDFracFcVsCut[iPt], 'Non-prompt #it{f}_{c}',
                             'fp')
            deltaY += 0.1
            legFrac.SetY1(0.83 - deltaY)
        if compareToNb:
            legFrac.AddEntry(gPromptFracNbVsCut[iPt], 'Prompt #it{N}_{b}',
                             'fp')
            legFrac.AddEntry(gFDFracNbVsCut[iPt], 'Non-prompt #it{N}_{b}',
                             'fp')
            deltaY += 0.1
            legFrac.SetY1(0.83 - deltaY)

    cEff.append(TCanvas(f'cEff_{ptString}', '', 800, 800))
    cEff[iPt].DrawFrame(0.5, hEffPromptVsCut[iPt].GetMinimum() / 5,
                        nSets + 0.5, 1., f'{commonString};efficiency')
    cEff[iPt].SetLogy()
    hEffPromptVsCut[iPt].DrawCopy('same')
    hEffFDVsCut[iPt].DrawCopy('same')
    legEff.Draw()
コード例 #14
0
def hvt(benchmark=['B3', 'A1']):

    hxs = {}
    hw = {}
    gxs = {}
    gw = {}
    mg = TMultiGraph()

    for m in massPoints:

        hxs[m] = TH2F("hxs_M%d" % m, ";;", 50, -0.04, 3.96, 100, 0., 2.)
        hw[m] = TH2F("hw_M%d" % m, ";;", 50, -0.04, 3.96, 50, 0., 2.)

    for m in massPoints:
        file = TFile.Open("HVT/scanHVT_M%s.root" % m, "READ")
        tree = file.Get("tree")
        for entry in range(
                tree.GetEntries()):  # Fill mass points only if NOT excluded
            tree.GetEntry(entry)
            gH, gF = tree.gv * tree.ch, tree.g * tree.g * tree.cq / tree.gv
            XsBr = tree.CX0 * tree.BRbb * 1000.  # in fb
            if XsBr < observed[m]: hxs[m].Fill(gH, gF)
            if tree.total_widthV0 / float(m) < width: hw[m].Fill(gH, gF)

        for b in range(hxs[m].GetNbinsX() * hxs[m].GetNbinsY()):
            hxs[m].SetBinContent(b, 1. if hxs[m].GetBinContent(b) > 0. else 0.)
            hw[m].SetBinContent(b, 1. if hw[m].GetBinContent(b) > 0. else 0.)

        #hxs[m].Smooth(20)
        #hw[m].Smooth(20)

        gxs[m] = getCurve(hxs[m])
        for i, g in enumerate(gxs[m]):
            g.SetLineColor(massColors[m])
            g.SetFillColor(massColors[m])
            g.SetFillStyle(massFill[m])  #(3345 if i>1 else 3354)
            g.SetLineWidth(503 * (1 if i < 2 else -1))
            mg.Add(g)

        #if m==3000:
        if m == massPoints[-1]:
            gw[m] = getCurve(hw[m])
            for i, g in enumerate(gw[m]):
                g.SetPoint(0, 0., g.GetY()[0])
                g.SetLineWidth(501 * (1 if i < 2 else -1))
                g.SetLineColor(920 + 2)
                g.SetFillColor(920 + 1)
                g.SetFillStyle(3003)
                mg.Add(g)

    if options.root:
        outFile = TFile("plotsLimit/Model.root", "RECREATE")
        outFile.cd()
        for m in massPoints:
            mg[m].Write("X_M%d" % m)
        mgW.Write("width")
        outFile.Close()
        print "Saved histogram in file plotsLimit/Model.root, exiting..."
        exit()

    ### plot ###

    c1 = TCanvas("c1", "HVT Exclusion Limits", 800, 600)
    c1.cd()
    c1.GetPad(0).SetTopMargin(0.06)
    c1.GetPad(0).SetRightMargin(0.05)
    c1.GetPad(0).SetTicks(1, 1)
    mg.Draw("AC")
    #mg.GetXaxis().SetTitle("g_{V} c_{H}")
    mg.GetXaxis().SetTitle("Higgs and vector boson coupling g_{H}")
    mg.GetXaxis().SetRangeUser(-3., 3.)
    mg.GetXaxis().SetLabelSize(0.045)
    mg.GetXaxis().SetTitleSize(0.045)
    mg.GetXaxis().SetTitleOffset(1.)
    #mg.GetYaxis().SetTitle("g^{2} c_{F} / g_{V}")
    mg.GetYaxis().SetTitle("Fermion coupling g_{F}")
    mg.GetYaxis().SetLabelSize(0.045)
    mg.GetYaxis().SetTitleSize(0.045)
    mg.GetYaxis().SetTitleOffset(1.)
    mg.GetYaxis().SetRangeUser(-1.2, 1.2)
    mg.GetYaxis().SetNdivisions(505)
    #    hxs[3500].Draw("CONTZ")

    drawCMS(LUMI, "Preliminary", False)
    #    drawAnalysis("XVH"+category, False)
    #    latex = TLatex()
    #    latex.SetNDC()
    #    latex.SetTextFont(62)
    #    latex.SetTextSize(0.06)
    #    latex.DrawLatex(0.10, 0.925, "CMS")

    # model B
    g_model = {}
    for i, b in enumerate(benchmark):
        g_model[i] = TGraph(1)
        g_model[i].SetTitle(models_name[b])
        g_model[i].SetPoint(0, models_point[b][0], models_point[b][1])
        g_model[i].SetMarkerStyle(models_style[b])
        g_model[i].SetMarkerColor(models_color[b])
        g_model[i].SetMarkerSize(1.5)
        g_model[i].Draw("PSAME")

    # text
    latex = TLatex()
    latex.SetTextSize(0.045)
    latex.SetTextFont(42)
    latex.SetTextColor(630)
    #    for b in benchmark: latex.DrawLatex(models_point[b][0]+0.02, models_point[b][1]+0.02, models_name[b])
    latex.SetTextColor(920 + 2)
    latex.DrawLatex(-2.8, -0.875,
                    "#frac{#Gamma_{Z'}}{m_{Z'}} > %.0f%%" % (width * 100, ))

    #leg = TLegend(0.68, 0.60, 0.95, 0.94)
    leg = TLegend(0.68, 0.34, 0.95, 0.66)
    leg.SetBorderSize(1)
    leg.SetFillStyle(1001)
    leg.SetFillColor(0)
    for m in massPoints:
        leg.AddEntry(gxs[m][0], "m_{Z'} = %.1f TeV" % (m / 1000.), "fl")
    for i, b in enumerate(benchmark):
        leg.AddEntry(g_model[i], g_model[i].GetTitle(), "P")
    leg.SetY1(leg.GetY2() - leg.GetNRows() * 0.050)
    leg.SetMargin(0.35)
    leg.Draw()

    gxs_ = gxs[massPoints[0]][0].Clone("gxs_")
    gxs_.SetLineColor(1)
    #    gxs_.SetFillColor(1)

    latex.SetNDC()
    latex.SetTextColor(1)
    latex.SetTextSize(0.04)
    latex.SetTextFont(52)
    latex.DrawLatex(0.15, 0.95, "q#bar{q} #rightarrow Z' #rightarrow b#bar{b}")

    c1.Print("plots/model/HVT.png")
    c1.Print("plots/model/HVT.pdf")
    c1.Print("plots/model/HVT.root")
    c1.Print("plots/model/HVT.C")

    #g = 0.646879, cH = 0.976246, cF = 1.02433
    print "model B = [", 3 * 0.976246, ",", 0.646879 * 0.646879 * 1.02433 / 3, "]"

    if not gROOT.IsBatch(): raw_input("Press Enter to continue...")
コード例 #15
0
def limit2HDM():
    global signals
    signals = range(800, 2000 + 1, 50)
    multF = HTOBB
    THEORY = ['T1', 'T2']

    mass, val = fillValues("./combine/AZh/AZh_M%d.txt")
    Obs0s = TGraph()
    Exp0s = TGraph()
    Exp1s = TGraphAsymmErrors()
    Exp2s = TGraphAsymmErrors()

    massB, valB = fillValues("./combine/BBAZh/BBAZh_M%d.txt")
    Obs0sB = TGraph()
    Exp0sB = TGraph()
    Exp1sB = TGraphAsymmErrors()
    Exp2sB = TGraphAsymmErrors()

    for i, m in enumerate(mass):
        if not m in val:
            print "Key Error:", m, "not in value map"
            continue

        n = Exp0s.GetN()
        Obs0s.SetPoint(n, m, val[m][0] * multF)
        Exp0s.SetPoint(n, m, val[m][3] * multF)
        Exp1s.SetPoint(n, m, val[m][3] * multF)
        Exp1s.SetPointError(n, 0., 0., val[m][3] * multF - val[m][2] * multF,
                            val[m][4] * multF - val[m][3] * multF)
        Exp2s.SetPoint(n, m, val[m][3] * multF)
        Exp2s.SetPointError(n, 0., 0., val[m][3] * multF - val[m][1] * multF,
                            val[m][5] * multF - val[m][3] * multF)

        Obs0sB.SetPoint(n, m, valB[m][0] * multF)
        Exp0sB.SetPoint(n, m, valB[m][3] * multF)
        Exp1sB.SetPoint(n, m, valB[m][3] * multF)
        Exp1sB.SetPointError(n, 0., 0.,
                             valB[m][3] * multF - valB[m][2] * multF,
                             valB[m][4] * multF - valB[m][3] * multF)
        Exp2sB.SetPoint(n, m, valB[m][3] * multF)
        Exp2sB.SetPointError(n, 0., 0.,
                             valB[m][3] * multF - valB[m][1] * multF,
                             valB[m][5] * multF - valB[m][3] * multF)

    col = 629
    Exp2s.SetLineWidth(2)
    Exp2s.SetLineStyle(1)
    Obs0s.SetLineWidth(3)
    Obs0s.SetMarkerStyle(0)
    Obs0s.SetLineColor(1)
    Exp0s.SetLineStyle(2)
    Exp0s.SetLineWidth(3)
    Exp0s.SetLineColor(1)
    #    Exp1s.SetFillColorAlpha(col, 0.4) #kGreen+1
    #    Exp1s.SetLineColorAlpha(col, 0.4)
    #    Exp2s.SetFillColorAlpha(col, 0.2) #kOrange
    #    Exp2s.SetLineColorAlpha(col, 0.2)
    Exp1s.SetFillColor(417)
    Exp1s.SetLineColor(417)
    Exp2s.SetFillColor(800)
    Exp2s.SetLineColor(800)

    colB = 922
    Exp2sB.SetLineWidth(2)
    Obs0sB.SetLineStyle(9)
    Obs0sB.SetLineWidth(3)
    Obs0sB.SetMarkerStyle(0)
    Obs0sB.SetLineColor(colB)
    Exp0sB.SetLineStyle(8)
    Exp0sB.SetLineWidth(3)
    Exp0sB.SetLineColor(colB)
    Exp1sB.SetFillColorAlpha(colB, 0.4)  #kGreen+1
    Exp1sB.SetLineColorAlpha(colB, 0.4)
    Exp2sB.SetFillColorAlpha(colB, 0.2)  #kOrange
    Exp2sB.SetLineColorAlpha(colB, 0.2)

    Exp2s.GetXaxis().SetTitle("m_{A} (GeV)")
    Exp2s.GetXaxis().SetTitleSize(Exp2s.GetXaxis().GetTitleSize() * 1.25)
    Exp2s.GetXaxis().SetNoExponent(True)
    Exp2s.GetXaxis().SetMoreLogLabels(True)
    Exp2s.GetYaxis().SetTitle(
        "#sigma(A) #bf{#it{#Beta}}(A #rightarrow Zh) #bf{#it{#Beta}}(h #rightarrow bb) (fb)"
    )
    Exp2s.GetYaxis().SetTitleOffset(1.5)
    Exp2s.GetYaxis().SetNoExponent(True)
    Exp2s.GetYaxis().SetMoreLogLabels()

    Theory = {}
    #for t in THEORY:
    #    Theory[t] = TGraphAsymmErrors()
    #    for m in sorted(THDM[t]['ggA'].keys()):
    #        if m < mass[0] or m > mass[-1]: continue
    #        Xs, Xs_Up, Xs_Down = 0., 0., 0.
    #        Xs = THDM[t]['ggA'][m]
    #        Xs_Up = Xs*(1.+math.sqrt((THDM['PDF']['ggA'][m][0]-1.)**2 + (THDM['QCD']['ggA'][m][0]-1.)**2))
    #        Xs_Down = Xs*(1.-math.sqrt((1.-THDM['PDF']['ggA'][m][1])**2 + (1.-THDM['QCD']['ggA'][m][1])**2))
    #        n = Theory[t].GetN()
    #        Theory[t].SetPoint(n, m, Xs)
    #        Theory[t].SetPointError(n, 0., 0., (Xs-Xs_Down), (Xs_Up-Xs))

    #    Theory[t].SetLineColor(theoryLineColor[t])
    #    Theory[t].SetFillColor(theoryFillColor[t])
    #    Theory[t].SetFillStyle(theoryFillStyle[t])
    #    Theory[t].SetLineWidth(2)
    #        #Theory[t].SetLineStyle(7)

    c1 = TCanvas("c1", "Exclusion Limits", 800, 600)
    c1.cd()
    #SetPad(c1.GetPad(0))
    c1.GetPad(0).SetTopMargin(0.06)
    c1.GetPad(0).SetRightMargin(0.05)
    c1.GetPad(0).SetLeftMargin(0.12)
    c1.GetPad(0).SetTicks(1, 1)
    c1.GetPad(0).SetLogy()
    Exp2s.Draw("A3")
    Exp1s.Draw("SAME, 3")
    Exp0s.Draw("SAME, L")
    #    Exp2sB.Draw("SAME, 3")
    #    Exp1sB.Draw("SAME, 3")
    Exp0sB.Draw("SAME, L")
    if not options.blind:
        Obs0s.Draw("SAME, L")
        Obs0sB.Draw("SAME, L")
    for t in THEORY:
        Theory[t].Draw("SAME, L3")
        Theory[t].Draw("SAME, L3X0Y0")
    #setHistStyle(Exp2s)


#    Exp2s.GetXaxis().SetTitleSize(0.045)
#    Exp2s.GetYaxis().SetTitleSize(0.04)
#    Exp2s.GetXaxis().SetLabelSize(0.04)
#    Exp2s.GetYaxis().SetLabelSize(0.04)
#    Exp2s.GetXaxis().SetTitleOffset(1)
#    Exp2s.GetYaxis().SetTitleOffset(1.25)
    Exp2s.GetXaxis().SetTitleSize(0.050)
    Exp2s.GetYaxis().SetTitleSize(0.050)
    Exp2s.GetXaxis().SetLabelSize(0.045)
    Exp2s.GetYaxis().SetLabelSize(0.045)
    Exp2s.GetXaxis().SetTitleOffset(0.90)
    Exp2s.GetYaxis().SetTitleOffset(1.25)
    Exp2s.GetYaxis().SetMoreLogLabels(True)
    Exp2s.GetYaxis().SetNoExponent(True)
    Exp2s.GetYaxis().SetRangeUser(0.5, 1.e3)
    Exp2s.GetXaxis().SetRangeUser(mass[0], mass[-1])
    drawAnalysis('AZh')
    drawRegion('AZHsl', True)
    drawCMS(LUMI, "")  #Preliminary
    #drawCMS(LUMI, "Work in Progress", suppressCMS=True)

    # legend
    leg = TLegend(0.6, 0.90, 0.99, 0.90)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    leg.SetHeader("95% CL upper limits")
    leg.AddEntry(None, "gg #rightarrow A #rightarrow Zh",
                 "")  #"95% CL upper limits"
    leg.AddEntry(Obs0s, "Observed", "l")
    leg.AddEntry(Exp0s, "Expected", "l")
    leg.AddEntry(Exp1s, "#pm 1 std. deviation", "f")
    leg.AddEntry(Exp2s, "#pm 2 std. deviation", "f")
    leg.AddEntry(None, "", "")
    leg.AddEntry(None, "bbA #rightarrow Zh", "")
    leg.AddEntry(Obs0sB, "Observed", "l")
    leg.AddEntry(Exp0sB, "Expected", "l")
    leg.SetY1(leg.GetY2() - leg.GetNRows() * 0.045)
    leg.Draw()

    #    latex = TLatex()
    #    latex.SetNDC()
    #    latex.SetTextSize(0.040)
    #    latex.SetTextFont(42)
    #    latex.DrawLatex(0.65, leg.GetY1()-0.045, "cos(#beta-#alpha)=0.25, tan(#beta)=1")

    #    legB = TLegend(0.12, 0.4-4*0.3/5., 0.65, 0.4)
    legB = TLegend(0.15, 0.27, 0.68, 0.27)
    legB.SetBorderSize(0)
    legB.SetFillStyle(0)  #1001
    legB.SetFillColor(0)
    for t in THEORY:
        legB.AddEntry(Theory[t], theoryLabel[t], "fl")
    legB.AddEntry(None, "cos(#beta-#alpha)=0.25, tan(#beta)=1", "")
    legB.SetY1(legB.GetY2() - legB.GetNRows() * 0.045)
    legB.Draw()

    c1.GetPad(0).RedrawAxis()
    leg.Draw()

    c1.Update()

    if not gROOT.IsBatch(): raw_input("Press Enter to continue...")

    c1.Print("plotsLimit/Exclusion/THDM.png")
    c1.Print("plotsLimit/Exclusion/THDM.pdf")
コード例 #16
0
def efficiency(year):
    import numpy as np
    from root_numpy import tree2array, fill_hist
    from aliases import AK8veto, electronVeto, muonVeto
    genPoints = [
        1800, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500, 6000, 7000, 8000
    ]
    eff = {}
    vetoes = {"AK8": AK8veto, "electron": electronVeto, "muon": muonVeto}
    VETO = "AK8"  ##could change the veto to investigate here
    if SEPARATE: eff_add = {}

    #channels = ['none', 'qq', 'bq', 'bb', 'mumu']
    channels = ['qq', 'bq', 'bb', 'mumu']

    for channel in channels:
        treeSign = {}
        ngenSign = {}
        nevtSign = {}
        eff[channel] = TGraphErrors()
        if SEPARATE:
            nevtSign_add = {}
            eff_add[channel] = TGraphErrors()

        for i, m in enumerate(genPoints):
            signName = "ZpBB_M" + str(m)
            ngenSign[m] = 0.
            nevtSign[m] = 0.
            if SEPARATE: nevtSign_add[m] = 0.
            for j, ss in enumerate(sample[signName]['files']):
                if year == "run2" or year in ss:
                    sfile = TFile(NTUPLEDIR + ss + ".root", "READ")
                    ngenSign[m] += sfile.Get("Events").GetBinContent(1)
                    treeSign[m] = sfile.Get("tree")
                    if BTAGGING == 'semimedium':
                        #if SEPARATE:
                        #    temp_array = tree2array(treeSign[m], branches='BTagAK4Weight_deepJet', selection=aliasSM[channel].replace(vetoes[VETO], ""))
                        #else:
                        temp_array = tree2array(
                            treeSign[m],
                            branches='BTagAK4Weight_deepJet',
                            selection=aliasSM[channel])
                        temp_hist = TH1F('pass', 'pass', 1, 0, 1)
                        fill_hist(temp_hist,
                                  np.zeros(len(temp_array)),
                                  weights=temp_array)
                        nevtSign[m] += temp_hist.GetBinContent(1)
                        temp_array = None
                        temp_hist.Reset()
                        if SEPARATE:
                            temp_array = tree2array(
                                treeSign[m],
                                branches='BTagAK4Weight_deepJet',
                                selection=aliasSM[channel].replace(
                                    vetoes[VETO], ""))
                            temp_hist = TH1F('pass', 'pass', 1, 0, 1)
                            fill_hist(temp_hist,
                                      np.zeros(len(temp_array)),
                                      weights=temp_array)
                            nevtSign[m] += temp_hist.GetBinContent(1)
                            temp_array = None
                            temp_hist.Reset()
                    else:
                        #if SEPARATE:
                        #    temp_array = tree2array(treeSign[m], branches='BTagAK4Weight_deepJet', selection=alias[channel].format(WP=working_points[BTAGGING]).replace(vetoes[VETO], ""))
                        #else:
                        temp_array = tree2array(
                            treeSign[m],
                            branches='BTagAK4Weight_deepJet',
                            selection=alias[channel].format(
                                WP=working_points[BTAGGING]))
                        temp_hist = TH1F('pass', 'pass', 1, 0, 1)
                        fill_hist(temp_hist,
                                  np.zeros(len(temp_array)),
                                  weights=temp_array)
                        nevtSign[m] += temp_hist.GetBinContent(1)
                        temp_array = None
                        temp_hist.Reset()
                        if SEPARATE:
                            temp_array = tree2array(
                                treeSign[m],
                                branches='BTagAK4Weight_deepJet',
                                selection=alias[channel].format(
                                    WP=working_points[BTAGGING]).replace(
                                        vetoes[VETO], ""))
                            temp_hist = TH1F('pass', 'pass', 1, 0, 1)
                            fill_hist(temp_hist,
                                      np.zeros(len(temp_array)),
                                      weights=temp_array)
                            nevtSign_add[m] += temp_hist.GetBinContent(1)
                            temp_array = None
                            temp_hist.Reset()

                    sfile.Close()
                    print channel, ss, ":", nevtSign[m], "/", ngenSign[
                        m], "=", nevtSign[m] / ngenSign[m]
            if nevtSign[m] == 0 or ngenSign[m] < 0: continue
            n = eff[channel].GetN()
            eff[channel].SetPoint(n, m, nevtSign[m] / ngenSign[m])
            eff[channel].SetPointError(n, 0,
                                       math.sqrt(nevtSign[m]) / ngenSign[m])
            if SEPARATE:
                eff_add[channel].SetPoint(n, m, nevtSign_add[m] / ngenSign[m])
                eff_add[channel].SetPointError(
                    n, 0,
                    math.sqrt(nevtSign_add[m]) / ngenSign[m])

        eff[channel].SetMarkerColor(color[channel])
        eff[channel].SetMarkerStyle(20)
        eff[channel].SetLineColor(color[channel])
        eff[channel].SetLineWidth(2)

        if SEPARATE:
            eff_add[channel].SetMarkerColor(color[channel] +
                                            color_shift[channel])
            eff_add[channel].SetMarkerStyle(21)
            eff_add[channel].SetLineColor(color[channel] +
                                          color_shift[channel])
            eff_add[channel].SetLineWidth(2)
            eff_add[channel].SetLineStyle(7)

        if channel == 'qq' or channel == 'none': eff[channel].SetLineStyle(3)

    n = max([eff[x].GetN() for x in channels])
    maxEff = 0.

    # Total efficiency
    eff["sum"] = TGraphErrors(n)
    eff["sum"].SetMarkerStyle(24)
    eff["sum"].SetMarkerColor(1)
    eff["sum"].SetLineWidth(2)

    if SEPARATE:
        eff_add["sum"] = TGraphErrors(n)
        eff_add["sum"].SetMarkerStyle(25)
        eff_add["sum"].SetMarkerColor(1)
        eff_add["sum"].SetLineWidth(2)
        eff_add["sum"].SetLineStyle(7)

    for i in range(n):
        tot, mass = 0., 0.
        if SEPARATE: tot_add = 0.
        for channel in channels:
            if channel == 'qq' or channel == 'none':
                continue  #not sure if I should include 2mu category in sum
            if eff[channel].GetN() > i:
                tot += eff[channel].GetY()[i]
                if SEPARATE: tot_add += eff_add[channel].GetY()[i]
                mass = eff[channel].GetX()[i]
                if tot > maxEff: maxEff = tot
        eff["sum"].SetPoint(i, mass, tot)
        if SEPARATE: eff_add["sum"].SetPoint(i, mass, tot_add)

    if SEPARATE:
        leg = TLegend(0.15, 0.50, 0.95, 0.8)
    else:
        leg = TLegend(0.15, 0.60, 0.95, 0.8)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    leg.SetNColumns(len(channels) / 4)
    for i, channel in enumerate(channels):
        if eff[channel].GetN() > 0:
            leg.AddEntry(eff[channel], getChannel(channel), "pl")
            if SEPARATE:
                leg.AddEntry(eff_add[channel],
                             getChannel(channel) + " no " + VETO + "-veto",
                             "pl")
    if SEPARATE:
        leg.SetY1(leg.GetY2() -
                  len([x for x in channels if eff[x].GetN() > 0]) * 0.045)
    else:
        leg.SetY1(leg.GetY2() -
                  len([x for x in channels if eff[x].GetN() > 0]) / 2. * 0.045)
    if SEPARATE:
        legS = TLegend(0.5, 0.8 - 0.045, 0.9, 0.85)
    else:
        legS = TLegend(0.5, 0.85 - 0.045, 0.9, 0.85)
    legS.SetBorderSize(0)
    legS.SetFillStyle(0)  #1001
    legS.SetFillColor(0)
    legS.AddEntry(eff['sum'],
                  "Total b tag efficiency (1 b tag + 2 b tag + 2 #mu)", "pl")
    if SEPARATE:
        legS.AddEntry(eff_add['sum'],
                      "Total b tag efficiency, no " + VETO + "-veto", "pl")
    c1 = TCanvas("c1", "Signal Efficiency", 1200, 800)
    c1.cd(1)
    eff['sum'].Draw("APL")
    if SEPARATE: eff_add['sum'].Draw("SAME, PL")
    for i, channel in enumerate(channels):
        eff[channel].Draw("SAME, PL")
        if SEPARATE: eff_add[channel].Draw("SAME, PL")
    leg.Draw()
    legS.Draw()
    setHistStyle(eff["sum"], 1.1)
    eff["sum"].SetTitle(";m_{Z'} (GeV);Acceptance #times efficiency")
    eff["sum"].SetMinimum(0.)
    eff["sum"].SetMaximum(max(1., maxEff * 1.5))  #0.65
    if SEPARATE:
        eff_add["sum"].SetTitle(";m_{Z'} (GeV);Acceptance #times efficiency")
        eff_add["sum"].SetMinimum(0.)
        eff_add["sum"].SetMaximum(1.)

    eff["sum"].GetXaxis().SetTitleSize(0.045)
    eff["sum"].GetYaxis().SetTitleSize(0.045)
    eff["sum"].GetYaxis().SetTitleOffset(1.1)
    eff["sum"].GetXaxis().SetTitleOffset(1.05)
    eff["sum"].GetXaxis().SetRangeUser(1500, 8000)
    c1.SetTopMargin(0.05)
    #drawCMS(-1, "Simulation Preliminary", year=year) #Preliminary
    #drawCMS(-1, "Work in Progress", year=year, suppressCMS=True)
    drawCMS(-1, "", year=year, suppressCMS=True)
    drawAnalysis("")

    if SEPARATE:
        c1.Print("plots/Efficiency/" + year + "_" + BTAGGING + "_no" + VETO +
                 "veto.pdf")
        c1.Print("plots/Efficiency/" + year + "_" + BTAGGING + "_no" + VETO +
                 "veto.png")
    else:
        c1.Print("plots/Efficiency/" + year + "_" + BTAGGING + ".pdf")
        c1.Print("plots/Efficiency/" + year + "_" + BTAGGING + ".png")

    # print
    print "category",
    for m in range(0, eff["sum"].GetN()):
        print " & %d" % int(eff["sum"].GetX()[m]),
    print "\\\\", "\n\\hline"
    for i, channel in enumerate(channels + ["sum"]):
        if channel == 'sum': print "\\hline"
        print getChannel(channel).replace("high ", "H").replace(
            "low ", "L").replace("purity", "P").replace("b-tag", ""),
        for m in range(0, eff[channel].GetN()):
            print "& %.1f" % (100. * eff[channel].GetY()[m]),
        print "\\\\"
コード例 #17
0
def Direct_Estimator(var, cut, year):
    from root_numpy import root2array, fill_hist, array2root
    import numpy.lib.recfunctions as rfn
    ### Preliminary Operations ###
    treeRead = not cut in [
        "nnqq", "en", "enqq", "mn", "mnqq", "ee", "eeqq", "mm", "mmqq", "em",
        "emqq", "qqqq"
    ]  # Read from tree
    channel = cut
    unit = ''
    if "GeV" in variable[var]['title']: unit = ' GeV'
    isBlind = BLIND and 'SR' in channel
    isAH = False  #'qqqq' in channel or 'hp' in channel or 'lp' in channel
    showSignal = False if 'SB' in cut or 'TR' in cut else True  #'SR' in channel or channel=='qqqq'#or len(channel)==5
    stype = "HVT model B"
    if len(sign) > 0 and 'AZh' in sign[0]: stype = "2HDM"
    elif len(sign) > 0 and 'monoH' in sign[0]: stype = "Z'-2HDM m_{A}=300 GeV"
    if treeRead:
        for k in sorted(alias.keys(), key=len, reverse=True):
            if BTAGGING == 'semimedium':
                if k in cut:
                    cut = cut.replace(k, aliasSM[k])

            else:
                if k in cut:
                    cut = cut.replace(
                        k, alias[k].format(WP=working_points[BTAGGING]))

    print "Plotting from", ("tree" if treeRead else
                            "file"), var, "in", channel, "channel with:"
    print "  cut    :", cut

    if var == 'jj_deltaEta_widejet':
        if "jj_deltaEta_widejet<1.1 && " in cut:
            print
            print "omitting jj_deltaEta_widejet<1.1 cut to draw the deltaEta distribution"
            print
            cut = cut.replace("jj_deltaEta_widejet<1.1 && ", "")
        else:
            print
            print "no 'jj_deltaEta_widejet<1.1 && ' in the cut string detected, so it cannot be ommited explicitly"
            print

    ### Create and fill MC histograms ###
    # Create dict
    file = {}
    tree = {}
    hist = {}

    ### Create and fill MC histograms ###
    for i, s in enumerate(back + sign):
        if True:  #FIXME

            if variable[var]['nbins'] > 0:
                hist[s] = TH1F(
                    s, ";" + variable[var]['title'] + ";Events / ( " + str(
                        (variable[var]['max'] - variable[var]['min']) /
                        variable[var]['nbins']) + unit + " );" +
                    ('log' if variable[var]['log'] else ''),
                    variable[var]['nbins'], variable[var]['min'],
                    variable[var]['max'])
            else:
                hist[s] = TH1F(
                    s, ";" + variable[var]['title'] + ";Events" +
                    ('log' if variable[var]['log'] else ''),
                    len(variable[var]['bins']) - 1,
                    array('f', variable[var]['bins']))
            hist[s].Sumw2()

            for j, ss in enumerate(sample[s]['files']):
                if not 'data' in s:
                    if year == "run2" or year in ss:
                        arr = root2array(
                            NTUPLEDIR + ss + ".root",
                            branches=[
                                var, "jpt_1", "jpt_2", "eventWeightLumi",
                                "TMath::Abs(jflavour_1)==5 && TMath::Abs(jflavour_2)==5",
                                "TMath::Abs(jflavour_1)==5 && TMath::Abs(jflavour_2)!=5",
                                "TMath::Abs(jflavour_1)!=5 && TMath::Abs(jflavour_2)==5",
                                "TMath::Abs(jflavour_1)!=5 && TMath::Abs(jflavour_2)!=5"
                            ],
                            selection=cut if len(cut) > 0 else "")
                        print "imported " + NTUPLEDIR + ss + ".root"
                        arr.dtype.names = [
                            var, "jpt_1", "jpt_2", "eventWeightLumi", "bb",
                            "bq", "qb", "qq"
                        ]
                        MANtag_eff1 = np.array(map(MANtag_eff, arr["jpt_1"]))
                        MANtag_eff2 = np.array(map(MANtag_eff, arr["jpt_2"]))
                        MANtag_mis1 = np.array(map(MANtag_mis, arr["jpt_1"]))
                        MANtag_mis2 = np.array(map(MANtag_mis, arr["jpt_2"]))
                        MANtag_weight = np.multiply(
                            arr["eventWeightLumi"],
                            np.multiply(arr['bb'],
                                        np.multiply(MANtag_eff1, MANtag_eff2))
                            + np.multiply(
                                arr['bq'], np.multiply(MANtag_eff1,
                                                       MANtag_mis2)) +
                            np.multiply(arr['qb'],
                                        np.multiply(MANtag_mis1,
                                                    MANtag_eff2)) +
                            np.multiply(arr['qq'],
                                        np.multiply(MANtag_mis1, MANtag_mis2)))
                        fill_hist(hist[s], arr[var], weights=MANtag_weight)
                        deepCSV_eff1 = np.array(map(deepCSV_eff, arr["jpt_1"]))
                        deepCSV_eff2 = np.array(map(deepCSV_eff, arr["jpt_2"]))
                        deepCSV_mis1 = np.array(map(deepCSV_mis, arr["jpt_1"]))
                        deepCSV_mis2 = np.array(map(deepCSV_mis, arr["jpt_2"]))
                        deepCSV_weight = np.multiply(
                            arr["eventWeightLumi"],
                            np.multiply(
                                arr['bb'],
                                np.multiply(deepCSV_eff1, deepCSV_eff2)) +
                            np.multiply(
                                arr['bq'],
                                np.multiply(deepCSV_eff1, deepCSV_mis2)) +
                            np.multiply(
                                arr['qb'],
                                np.multiply(deepCSV_mis1, deepCSV_eff2)) +
                            np.multiply(
                                arr['qq'],
                                np.multiply(deepCSV_mis1, deepCSV_mis2)))

                        if var == "jj_mass_widejet" and options.save and not "data" in ss:
                            arr = rfn.append_fields(arr,
                                                    "MANtag_weight",
                                                    MANtag_weight,
                                                    usemask=False)
                            arr = rfn.append_fields(arr,
                                                    "deepCSV_weight",
                                                    deepCSV_weight,
                                                    usemask=False)
                            array2root(arr,
                                       NTUPLEDIR + "MANtag/" + ss + "_" +
                                       BTAGGING + ".root",
                                       treename="tree",
                                       mode='recreate')
                            print "saved as", NTUPLEDIR + "MANtag/" + ss + "_" + BTAGGING + ".root"
                        arr = None

        hist[s].Scale(sample[s]['weight'] if hist[s].Integral() >= 0 else 0)
        hist[s].SetFillColor(sample[s]['fillcolor'])
        hist[s].SetFillStyle(sample[s]['fillstyle'])
        hist[s].SetLineColor(sample[s]['linecolor'])
        hist[s].SetLineStyle(sample[s]['linestyle'])

    if channel.endswith('TR') and channel.replace('TR', '') in topSF:
        hist['TTbarSL'].Scale(topSF[channel.replace('TR', '')][0])
        hist['ST'].Scale(topSF[channel.replace('TR', '')][0])

    hist['BkgSum'] = hist['data_obs'].Clone(
        "BkgSum") if 'data_obs' in hist else hist[back[0]].Clone("BkgSum")
    hist['BkgSum'].Reset("MICES")
    hist['BkgSum'].SetFillStyle(3003)
    hist['BkgSum'].SetFillColor(1)
    for i, s in enumerate(back):
        hist['BkgSum'].Add(hist[s])

    # Create data and Bkg sum histograms
    if options.blind or 'SR' in channel:
        hist['data_obs'] = hist['BkgSum'].Clone("data_obs")
        hist['data_obs'].Reset("MICES")
    # Set histogram style
    hist['data_obs'].SetMarkerStyle(20)
    hist['data_obs'].SetMarkerSize(1.25)

    for i, s in enumerate(back + sign + ['BkgSum']):
        addOverflow(hist[s], False)  # Add overflow
    for i, s in enumerate(sign):
        hist[s].SetLineWidth(3)
    for i, s in enumerate(sign):
        sample[s][
            'plot'] = True  #sample[s]['plot'] and s.startswith(channel[:2])

    if isAH:
        for i, s in enumerate(back):
            hist[s].SetFillStyle(3005)
            hist[s].SetLineWidth(2)
        #for i, s in enumerate(sign):
        #    hist[s].SetFillStyle(0)
        if not var == "Events":
            sfnorm = hist[data[0]].Integral() / hist['BkgSum'].Integral()
            print "Applying SF:", sfnorm
            for i, s in enumerate(back + ['BkgSum']):
                hist[s].Scale(sfnorm)
        if BLIND and var.endswith("Mass"):
            for i, s in enumerate(data + back + ['BkgSum']):
                first, last = hist[s].FindBin(65), hist[s].FindBin(135)
                for j in range(first, last):
                    hist[s].SetBinContent(j, -1.e-4)
        if BLIND and var.endswith("Tau21"):
            for i, s in enumerate(data):
                first, last = hist[s].FindBin(0), hist[s].FindBin(0.6)
                for j in range(first, last):
                    hist[s].SetBinContent(j, -1.e-4)

    # Create stack
    if variable[var]['nbins'] > 0:
        bkg = THStack(
            "Bkg",
            ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events / ( " + str(
                (variable[var]['max'] - variable[var]['min']) /
                variable[var]['nbins']) + unit + " )")
    else:
        bkg = THStack("Bkg",
                      ";" + hist['BkgSum'].GetXaxis().GetTitle() + ";Events; ")
    for i, s in enumerate(back):
        bkg.Add(hist[s])

    # Legend
    leg = TLegend(0.65, 0.6, 0.95, 0.9)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    if len(data) > 0:
        leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pe")
    for i, s in reversed(list(enumerate(['BkgSum'] + back))):
        leg.AddEntry(hist[s], sample[s]['label'], "f")
    if showSignal:
        for i, s in enumerate(sign):
            if sample[s]['plot']:
                leg.AddEntry(hist[s], sample[s]['label'], "fl")

    leg.SetY1(0.9 - leg.GetNRows() * 0.05)

    # --- Display ---
    c1 = TCanvas("c1",
                 hist.values()[0].GetXaxis().GetTitle(), 800,
                 800 if RATIO else 600)

    if RATIO:
        c1.Divide(1, 2)
        setTopPad(c1.GetPad(1), RATIO)
        setBotPad(c1.GetPad(2), RATIO)
    c1.cd(1)
    c1.GetPad(bool(RATIO)).SetTopMargin(0.06)
    c1.GetPad(bool(RATIO)).SetRightMargin(0.05)
    c1.GetPad(bool(RATIO)).SetTicks(1, 1)

    log = variable[var]['log']  #"log" in hist['BkgSum'].GetZaxis().GetTitle()
    if log: c1.GetPad(bool(RATIO)).SetLogy()

    # Draw
    bkg.Draw("HIST")  # stack
    hist['BkgSum'].Draw("SAME, E2")  # sum of bkg
    if not isBlind and len(data) > 0: hist['data_obs'].Draw("SAME, PE")  # data
    if 'sync' in hist: hist['sync'].Draw("SAME, PE")
    #data_graph.Draw("SAME, PE")
    if showSignal:
        smagn = 1.  #if treeRead else 1.e2 #if log else 1.e2
        for i, s in enumerate(sign):
            #        if sample[s]['plot']:
            hist[s].Scale(smagn)
            hist[s].Draw(
                "SAME, HIST"
            )  # signals Normalized, hist[s].Integral()*sample[s]['weight']
        textS = drawText(0.80, 0.9 - leg.GetNRows() * 0.05 - 0.02,
                         stype + " (x%d)" % smagn, True)
    #bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset()*1.075)
    bkg.GetYaxis().SetTitleOffset(0.9)
    #bkg.GetYaxis().SetTitleOffset(2.)
    bkg.SetMaximum((5. if log else 1.25) * max(
        bkg.GetMaximum(),
        hist['data_obs'].GetBinContent(hist['data_obs'].GetMaximumBin()) +
        hist['data_obs'].GetBinError(hist['data_obs'].GetMaximumBin())))
    #if bkg.GetMaximum() < max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum()): bkg.SetMaximum(max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum())*1.25)
    bkg.SetMinimum(
        max(
            min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin(
            )), hist['data_obs'].GetMinimum()), 5.e-1) if log else 0.)
    if log:
        bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4)
        #bkg.GetYaxis().SetMoreLogLabels(True)
    bkg.GetXaxis().SetRangeUser(variable[var]['min'], variable[var]['max'])

    #if log: bkg.SetMinimum(1)
    leg.Draw()
    #drawCMS(LUMI[year], "Preliminary")
    drawCMS(LUMI[year], "Work in Progress", suppressCMS=True)
    drawRegion('XVH' + channel, True)
    drawAnalysis(channel)

    setHistStyle(bkg, 1.2 if RATIO else 1.1)
    setHistStyle(hist['BkgSum'], 1.2 if RATIO else 1.1)

    if RATIO:
        c1.cd(2)
        err = hist['BkgSum'].Clone("BkgErr;")
        err.SetTitle("")
        err.GetYaxis().SetTitle("Data / MC")
        err.GetYaxis().SetTitleOffset(0.9)

        err.GetXaxis().SetRangeUser(variable[var]['min'], variable[var]['max'])
        for i in range(1, err.GetNbinsX() + 1):
            err.SetBinContent(i, 1)
            if hist['BkgSum'].GetBinContent(i) > 0:
                err.SetBinError(
                    i, hist['BkgSum'].GetBinError(i) /
                    hist['BkgSum'].GetBinContent(i))
        setBotStyle(err)
        errLine = err.Clone("errLine")
        errLine.SetLineWidth(1)
        errLine.SetFillStyle(0)
        res = hist['data_obs'].Clone("Residues")
        for i in range(0, res.GetNbinsX() + 1):
            if hist['BkgSum'].GetBinContent(i) > 0:
                res.SetBinContent(
                    i,
                    res.GetBinContent(i) / hist['BkgSum'].GetBinContent(i))
                res.SetBinError(
                    i,
                    res.GetBinError(i) / hist['BkgSum'].GetBinContent(i))
        if 'sync' in hist:
            res.SetMarkerColor(2)
            res.SetMarkerStyle(31)
            res.Reset()
            for i in range(0, res.GetNbinsX() + 1):
                x = hist['data_obs'].GetXaxis().GetBinCenter(i)
                if hist['sync'].GetBinContent(hist['sync'].FindBin(x)) > 0:
                    res.SetBinContent(
                        i, hist['data_obs'].GetBinContent(
                            hist['data_obs'].FindBin(x)) /
                        hist['sync'].GetBinContent(hist['sync'].FindBin(x)))
                    res.SetBinError(
                        i, hist['data_obs'].GetBinError(
                            hist['data_obs'].FindBin(x)) /
                        hist['sync'].GetBinContent(hist['sync'].FindBin(x)))
        setBotStyle(res)
        #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5)
        #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2)
        err.Draw("E2")
        errLine.Draw("SAME, HIST")
        if not isBlind and len(data) > 0:
            res.Draw("SAME, PE0")
            #res_graph.Draw("SAME, PE0")
            if len(err.GetXaxis().GetBinLabel(
                    1)) == 0:  # Bin labels: not a ordinary plot
                drawRatio(hist['data_obs'], hist['BkgSum'])
                drawStat(hist['data_obs'], hist['BkgSum'])

    c1.Update()

    if gROOT.IsBatch():
        if channel == "": channel = "nocut"
        varname = var.replace('.', '_').replace('()', '')
        if not os.path.exists("plots/" + channel):
            os.makedirs("plots/" + channel)
        suffix = ''
        if "b" in channel or 'mu' in channel: suffix += "_" + BTAGGING
        c1.Print("plots/MANtag_study/" + channel + "/" + varname + "_" + year +
                 suffix + ".png")
        c1.Print("plots/MANtag_study/" + channel + "/" + varname + "_" + year +
                 suffix + ".pdf")

    # Print table
    printTable(hist, sign)
コード例 #18
0
def plot(var, cut, norm=False, nm1=False):
    ### Preliminary Operations ###
    fileRead = os.path.exists(options.file)
    treeRead = not any(x==cut for x in ['0l', '1e', '1m', '2e', '2m', '1e1m', 'Gen', 'Trigger'])#(var in variable.keys()) # Read from tree
    binLow = ""
    binHigh = ""
    binName = ""
    if "binned" in cut:
        binLow = cut[cut.find("LowVal")+6:cut.find("HighVal")-1]
        binHigh = cut[cut.find("HighVal")+7:]
        binName = "bin_"+binLow+"_"+binHigh
        cut = cut[:cut.find("binned")]
    useformula = True
    if 'formula' in variable[var]:
        print variable[var]['formula']
        useformula = True
    channel = cut
    plotdir = cut
    plotname = var
    weight = "eventWeightLumi" #*(2.2/35.9)
    isBlind = BLIND and 'SR' in channel
    showSignal = True#('SR' in channel)
    cutSplit = cut.split()
    for s in cutSplit:
        if s in selection.keys():
            plotdir = s
            cut  = cut.replace(s, selection[s])
            if not binLow == "":
                cut = cut + " && " + var + " > " + binLow + " && " + var + " < " + binHigh
   #if treeRead and cut in selection: cut  = cut.replace(cut, selection[cut])
    
    # Determine Primary Dataset
    pd = []
    print cut
    if any(w in cut for w in ['1l', '1m', '2m', 'isWtoMN', 'isZtoMM', 'isTtoEM']): pd += [x for x in sample['data_obs']['files'] if 'SingleMuon' in x]
    if any(w in cut for w in ['1l', '1e', '2e', 'isWtoEN', 'isZtoEE']): pd += [x for x in sample['data_obs']['files'] if 'SingleElectron' in x]
    if any(w in cut for w in ['0l', 'isZtoNN']): pd += [x for x in sample['data_obs']['files'] if 'MET' in x]
    if len(pd)==0: raw_input("Warning: Primary Dataset not recognized, continue?")
    
    print "Plotting from", ("tree" if treeRead else "file"), var, "in", channel, "channel with:"
    print "  dataset:", pd
    print "  cut    :", cut
    print "  cut    :", weight
    
    ### Create and fill MC histograms ###
    # Create dict
    file = {}
    tree = {}
    hist = {}
    
    ### Create and fill MC histograms ###
    for i, s in enumerate(data+back+sign):
        if fileRead:
            fileName = options.file if not s=='data_obs' else "rootfiles_"+options.name+"/"+channel+binName+".root"
            histName = "shapes_fit_b/"+channel+"/"+s if not s=='data_obs' else s
            file[s] = TFile(fileName, "READ")
            tmphist = file[s].Get(histName)
            if tmphist==None:
                tmphist = hist[back[0]].Clone(s)
                tmphist.Reset("MICES")
                print "Histogram", histName, "not found in file", fileName
            if s=='data_obs': hist[s] = tmphist
            else:
                hist[s] = hist['data_obs'].Clone(s)
                #hist[s].Reset("MICES")
                hist[s].SetMarkerSize(0)
                for i in range(tmphist.GetNbinsX()+1): hist[s].SetBinContent(i+1, tmphist.GetBinContent(i+1))
        elif treeRead: # Project from tree
            tree[s] = TChain("tree")
            for j, ss in enumerate(sample[s]['files']):
                if not 'data' in s or ('data' in s and ss in pd):
                    tree[s].Add(NTUPLEDIR + ss + ".root")
            if not binLow == "":
                hist[s] = TH1F(s, ";"+variable[var]['title']+";Events;"+('log' if variable[var]['log'] else ''), 1, float(binLow), float(binHigh))
            elif binLow == "" and  variable[var]['nbins']>0: 
                hist[s] = TH1F(s, ";"+variable[var]['title']+";Events;"+('log' if variable[var]['log'] else ''), variable[var]['nbins'], variable[var]['min'], variable[var]['max'])
            else: 
                hist[s] = TH1F(s, ";"+variable[var]['title']+";Events;"+('log' if variable[var]['log'] else ''), len(variable[var]['bins'])-1, array('f', variable[var]['bins']))
            hist[s].Sumw2()
            redFactorString = ""
            redFactorValue = ""
            if isBlind and 'data' in s:
                redFactorString = " && Entry$ % 15 == 0"
            if isBlind and 'data' not in s:
                redFactorValue = " / 15"
            cutstring = "("+weight+redFactorValue+")" + ("*("+cut+redFactorString+")" if len(cut)>0 else "")
            if '-' in s: cutstring = cutstring.replace(cut, cut + "&& nBQuarks==" + s.split('-')[1][0])
            if useformula == True:
                tree[s].Project(s, variable[var]['formula'], cutstring)
            else:
                tree[s].Project(s, var, cutstring)         
            if not tree[s].GetTree()==None: hist[s].SetOption("%s" % tree[s].GetTree().GetEntriesFast())
        else: # Histogram written to file
            for j, ss in enumerate(sample[s]['files']):
                if not 'data' in s or ('data' in s and ss in pd):
                    file[ss] = TFile(NTUPLEDIR + ss + ".root", "R")
                    if file[ss].IsZombie():
                        print "WARNING: file", NTUPLEDIR + ss + ".root", "does not exist"
                        continue
                    tmphist = file[ss].Get(cut+"/"+var)
                    if tmphist==None: continue
                    if not s in hist.keys(): hist[s] = tmphist
                    else: hist[s].Add(tmphist)
        if hist[s].Integral() < 0: hist[s].Scale(0)
        hist[s].SetFillColor(sample[s]['fillcolor'])
        hist[s].SetFillStyle(sample[s]['fillstyle'])
        hist[s].SetLineColor(sample[s]['linecolor'])
        hist[s].SetLineStyle(sample[s]['linestyle'])
        #if 'WJetsToLNu' in s and 'SL' in channel and 'WR' in channel: hist[s].Scale(1.30)
        #if 'TTbar' in s and 'SL' in channel and 'TR' in channel: hist[s].Scale(0.91)
    
    
    hist['BkgSum'] = hist[back[0]].Clone("BkgSum")
    hist['BkgSum'].Reset("MICES")
    for i, s in enumerate(back): hist['BkgSum'].Add(hist[s], 1)
    if fileRead:
        #hist['BkgSum'] = file[back[0]].Get("shapes_fit_b/"+channel+"/"+"total_background")
        tmphist = file[back[0]].Get("shapes_prefit/"+channel+"/"+"total_background")
        hist['PreFit'] = hist['BkgSum'].Clone("PreFit")
        for i in range(tmphist.GetNbinsX()+1): hist['PreFit'].SetBinContent(i+1, tmphist.GetBinContent(i+1))
        hist['PreFit'].SetLineStyle(2)
        hist['PreFit'].SetLineColor(923)
        hist['PreFit'].SetLineWidth(3)
        hist['PreFit'].SetFillStyle(0)
    hist['BkgSum'].SetFillStyle(3003)
    hist['BkgSum'].SetFillColor(1)
    

    # Create data and Bkg sum histograms
#    if options.blind or 'SR' in channel:
#        hist['data_obs'] = hist['BkgSum'].Clone("data_obs")
#        hist['data_obs'].Reset("MICES")
    # Set histogram style
    hist[data[0]].SetMarkerStyle(20)
    hist[data[0]].SetMarkerSize(1.25)
    
    for i, s in enumerate(data+back+sign+['BkgSum']): addOverflow(hist[s], False) # Add overflow
    for i, s in enumerate(sign): hist[s].SetLineWidth(3)
    #for i, s in enumerate(sign): sample[s]['plot'] = True#sample[s]['plot'] and s.startswith(channel[:2])
    
    
    if norm:
        for i, s in enumerate(sign):
            hist[s].Scale(hist['BkgSum'].Integral()/hist[s].Integral())
#        for i, s in enumerate(back):
#            hist[s].SetFillStyle(3005)
#            hist[s].SetLineWidth(2)
#        #for i, s in enumerate(sign):
#        #    hist[s].SetFillStyle(0)
#        if not var=="Events":
#            sfnorm = hist[data[0]].Integral()/hist['BkgSum'].Integral()
#            print "Applying SF:", sfnorm
#            for i, s in enumerate(back+['BkgSum']): hist[s].Scale(sfnorm)
    
    # Create stack
    bkg = THStack("Bkg", ";"+hist['BkgSum'].GetXaxis().GetTitle()+";Events")
    for i, s in enumerate(back): bkg.Add(hist[s])
    
    # Legend
    leg = TLegend(0.65, 0.6, 0.95, 0.9)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0) #1001
    leg.SetFillColor(0)
    if len(data) > 0:
        leg.AddEntry(hist[data[0]], sample[data[0]]['label'], "pe")
    for i, s in reversed(list(enumerate(['BkgSum']+back))):
        leg.AddEntry(hist[s], sample[s]['label'], "f")
    if 'PreFit' in hist: leg.AddEntry(hist['PreFit'], sample['PreFit']['label'], "l")
    if showSignal:
        for i, s in enumerate(sign):
            if sample[s]['plot']: leg.AddEntry(hist[s], sample[s]['label'], "fl")
        
    leg.SetY1(0.9-leg.GetNRows()*0.05)
    
    
    # --- Display ---
    c1 = TCanvas("c1", hist.values()[0].GetXaxis().GetTitle(), 800, 800 if RATIO else 600)
    
    if RATIO:
        c1.Divide(1, 2)
        setTopPad(c1.GetPad(1), RATIO)
        setBotPad(c1.GetPad(2), RATIO)
    c1.cd(1)
    c1.GetPad(bool(RATIO)).SetTopMargin(0.06)
    c1.GetPad(bool(RATIO)).SetRightMargin(0.05)
    c1.GetPad(bool(RATIO)).SetTicks(1, 1)
    
    log = ("log" in hist['BkgSum'].GetZaxis().GetTitle())
    if log: c1.GetPad(bool(RATIO)).SetLogy()
        
    # Draw
    bkg.Draw("HIST") # stack
    hist['BkgSum'].Draw("SAME, E2") # sum of bkg
    if not isBlind and len(data) > 0: hist[data[0]].Draw("SAME, PE") # data
    #data_graph.Draw("SAME, PE")
    if 'PreFit' in hist: hist['PreFit'].Draw("SAME, HIST")
    if showSignal:
        for i, s in enumerate(sign):
            if sample[s]['plot']: hist[s].Draw("SAME, HIST") # signals Normalized, hist[s].Integral()*sample[s]['weight']
    bkg.GetYaxis().SetTitleOffset(bkg.GetYaxis().GetTitleOffset()*1.075)
    bkg.SetMaximum((5. if log else 1.25)*max(bkg.GetMaximum(), hist[data[0]].GetBinContent(hist[data[0]].GetMaximumBin())+hist[data[0]].GetBinError(hist[data[0]].GetMaximumBin())))
    if len(sign) > 0 and bkg.GetMaximum() < max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum()): bkg.SetMaximum(max(hist[sign[0]].GetMaximum(), hist[sign[-1]].GetMaximum())*1.25)
    bkg.SetMinimum(max(min(hist['BkgSum'].GetBinContent(hist['BkgSum'].GetMinimumBin()), hist[data[0]].GetMinimum()), 5.e-1)  if log else 0.)
    if log:
        bkg.GetYaxis().SetNoExponent(bkg.GetMaximum() < 1.e4)
        bkg.GetYaxis().SetMoreLogLabels(True)
    
    leg.Draw()
    drawCMS(LUMI, "Preliminary")
    drawRegion(channel, True)
    drawAnalysis("DM"+channel[:2])
    drawOverflow()
    
    setHistStyle(bkg, 1.2 if RATIO else 1.1)
    setHistStyle(hist['BkgSum'], 1.2 if RATIO else 1.1)
       
    if RATIO:
        c1.cd(2)
        err = hist['BkgSum'].Clone("BkgErr;")
        err.SetTitle("")
        err.GetYaxis().SetTitle("Data / Bkg")
        for i in range(1, err.GetNbinsX()+1):
            err.SetBinContent(i, 1)
            if hist['BkgSum'].GetBinContent(i) > 0:
                err.SetBinError(i, hist['BkgSum'].GetBinError(i)/hist['BkgSum'].GetBinContent(i))
        setBotStyle(err)
        errLine = err.Clone("errLine")
        errLine.SetLineWidth(1)
        errLine.SetFillStyle(0)
        res = hist[data[0]].Clone("Residues")
        for i in range(0, res.GetNbinsX()+1):
            if hist['BkgSum'].GetBinContent(i) > 0: 
                res.SetBinContent(i, res.GetBinContent(i)/hist['BkgSum'].GetBinContent(i))
                res.SetBinError(i, res.GetBinError(i)/hist['BkgSum'].GetBinContent(i))
        setBotStyle(res)
        #err.GetXaxis().SetLabelOffset(err.GetXaxis().GetLabelOffset()*5)
        #err.GetXaxis().SetTitleOffset(err.GetXaxis().GetTitleOffset()*2)
        err.Draw("E2")
        if 'PreFit' in hist:
            respre = hist['PreFit'].Clone("ResiduesPreFit")
            respre.Divide(hist['BkgSum'])
            respre.Draw("SAME, HIST")
        errLine.Draw("SAME, HIST")
        if not isBlind and len(data) > 0:
            res.Draw("SAME, PE0")
            #res_graph.Draw("SAME, PE0")
            if len(err.GetXaxis().GetBinLabel(1))==0: # Bin labels: not a ordinary plot
                drawRatio(hist['data_obs'], hist['BkgSum'])
                drawStat(hist['data_obs'], hist['BkgSum'])
    
    c1.Update()
        
    if gROOT.IsBatch() and options.saveplots: # and (treeRead and channel in selection.keys()):
        if not os.path.exists("plots_"+options.name+"/"+plotdir): os.makedirs("plots_"+options.name+"/"+plotdir)
        c1.Print("plots_"+options.name+"/"+plotdir+"/"+plotname+binName+".png")
        c1.Print("plots_"+options.name+"/"+plotdir+"/"+plotname+binName+".pdf")
    
    # Print table
    printTable(hist, sign)
    
    if not gROOT.IsBatch(): raw_input("Press Enter to continue...")

    if gROOT.IsBatch() and not fileRead and (var == 'MET_pt' or (channel.startswith('SL') and var == 'MET_sign') or (channel.endswith('ZR') and var == 'FakeMET_pt')):
        saveHist(hist, channel+binName)
コード例 #19
0
def efficiency(stype, Zlep=True):
    genPoints = [800, 1000, 1200, 1400, 1600, 1800, 2000, 2500, 3000, 3500, 4000, 4500]
    eff = {}
    
    channels = [x for x in channelList if len(x)<5]

    for channel in channels:
        treeSign = {}
        ngenSign = {}
        nevtSign = {}
        eff[channel] = TGraphErrors()

        for i, m in enumerate(genPoints):
            signName = "%s_M%d" % (stype, m) #"%s_M%d" % (channel[:3], m)
            ngenSign[m] = 0.
            nevtSign[m] = 0.
            for j, ss in enumerate(sample[signMass]['files']):
                if 'nn' in channel and not 'Zinv' in ss: continue
                if ('en' in channel or 'mn' in channel) and not 'Wlep' in ss: continue
                if ('ee' in channel or 'mm' in channel) and not 'Zlep' in ss: continue
                if Zlep and 'Zinv' in ss: continue
                if not Zlep and 'Zlep' in ss: continue

                sfile = TFile(NTUPLEDIR + ss + ".root", "READ")
                if not sfile.Get("Events")==None:
                    ngenSign[m] += sfile.Get("Events").GetEntries()
                    # From trees
                    treeSign[m] = sfile.Get("tree")
                    nevtSign[m] += treeSign[m].GetEntries(selection[channel] + selection['SR'])
                else:
                    ngenSign[m] = -1
                    print "Failed reading file", NTUPLEDIR + ss + ".root"
                sfile.Close()
            if nevtSign[m] == 0 or ngenSign[m] < 0: continue
            # Gen Br
            n = eff[channel].GetN()
            eff[channel].SetPoint(n, m, nevtSign[m]/ngenSign[m])
            eff[channel].SetPointError(n, 0, math.sqrt(nevtSign[m])/ngenSign[m])

        eff[channel].SetMarkerColor(color[channel])
        eff[channel].SetMarkerStyle(20)
        eff[channel].SetLineColor(color[channel])
        eff[channel].SetLineWidth(2)
        if channel.count('b')==1: eff[channel].SetLineStyle(3)

    n = max([eff[x].GetN() for x in channels])
    maxEff = 0.

    # Total efficiency
    eff["sum"] = TGraphErrors(n)
    eff["sum"].SetMarkerStyle(24)
    eff["sum"].SetMarkerColor(1)
    eff["sum"].SetLineWidth(2)
    for i in range(n):
        tot, mass = 0., 0.
        for channel in channels:
            if eff[channel].GetN() > i:
                tot += eff[channel].GetY()[i]
                mass = eff[channel].GetX()[i]
                if tot > maxEff: maxEff = tot
        eff["sum"].SetPoint(i, mass, tot)


    leg = TLegend(0.15, 0.60, 0.95, 0.8)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0) #1001
    leg.SetFillColor(0)
    leg.SetNColumns(len(channels)/4)
    for i, channel in enumerate(channels):
        if eff[channel].GetN() > 0: leg.AddEntry(eff[channel], getChannel(channel), "pl")
    leg.SetY1(leg.GetY2()-len([x for x in channels if eff[x].GetN() > 0])/2.*0.045)

    legS = TLegend(0.55, 0.85-0.045, 0.95, 0.85)
    legS.SetBorderSize(0)
    legS.SetFillStyle(0) #1001
    legS.SetFillColor(0)
    legS.AddEntry(eff['sum'], "Total efficiency", "pl")

    c1 = TCanvas("c1", "Signal Efficiency", 1200, 800)
    c1.cd(1)
    eff['sum'].Draw("APL")
    for i, channel in enumerate(channels): eff[channel].Draw("SAME, PL")
    leg.Draw()
    legS.Draw()
    setHistStyle(eff["sum"], 1.1)
    eff["sum"].SetTitle(";m_{"+stype[1]+"'} (GeV);Acceptance #times efficiency")
    eff["sum"].SetMinimum(0.)
    eff["sum"].SetMaximum(max(1., maxEff*1.5)) #0.65
    eff["sum"].GetXaxis().SetTitleSize(0.045)
    eff["sum"].GetYaxis().SetTitleSize(0.045)
    eff["sum"].GetYaxis().SetTitleOffset(1.1)
    eff["sum"].GetXaxis().SetTitleOffset(1.05)
    eff["sum"].GetXaxis().SetRangeUser(750, 5500)
    if stype=='XWH' or (stype=='XZH' and Zlep): line = drawLine(750, 2./3., 4500, 2./3.)
    drawCMS(-1,YEAR, "Simulation") #Preliminary
    drawAnalysis("ZH")

    suffix = ""
    if stype=='XZH' and Zlep: suffix = "ll"
    elif stype=='XZH' and not Zlep: suffix = "nn"
    elif stype=='XWH': suffix = "ln"

    c1.Print("plotsSignal/Efficiency/"+stype+suffix+".pdf")
    c1.Print("plotsSignal/Efficiency/"+stype+suffix+".png")

    # print
    print "category",
    for m in range(0, eff["sum"].GetN()):
        print " & %d" % int(eff["sum"].GetX()[m]),
    print "\\\\", "\n\\hline"
    for i, channel in enumerate(channels+["sum"]):
        if channel=='sum': print "\\hline"
        print getChannel(channel).replace("high ", "H").replace("low ", "L").replace("purity", "P").replace("b-tag", ""),
        for m in range(0, eff[channel].GetN()):
            print "& %.1f" % (100.*eff[channel].GetY()[m]),
        print "\\\\"
コード例 #20
0
def dijet(category):

    channel = 'bb'
    stype = channel
    isSB = True  # relict from using Alberto's more complex script
    isData = not ISMC
    nTupleDir = NTUPLEDIR

    samples = data if isData else back
    pd = []
    for sample_name in samples:
        if YEAR == 'run2':
            pd += sample[sample_name]['files']
        else:
            pd += [x for x in sample[sample_name]['files'] if YEAR in x]
    print "datasets:", pd
    if not os.path.exists(PLOTDIR): os.makedirs(PLOTDIR)
    if BIAS: print "Running in BIAS mode"

    order = 0
    RSS = {}

    X_mass = RooRealVar("jj_mass_widejet", "m_{jj}", X_min, X_max, "GeV")
    weight = RooRealVar("MANtag_weight", "", -1.e9, 1.e9)

    variables = RooArgSet(X_mass)
    variables.add(RooArgSet(weight))

    if VARBINS:
        binsXmass = RooBinning(len(abins) - 1, abins)
        X_mass.setBinning(RooBinning(len(abins_narrow) - 1, abins_narrow))
        plot_binning = RooBinning(
            int((X_mass.getMax() - X_mass.getMin()) / 100), X_mass.getMin(),
            X_mass.getMax())
    else:
        X_mass.setBins(int((X_mass.getMax() - X_mass.getMin()) / 10))
        binsXmass = RooBinning(int((X_mass.getMax() - X_mass.getMin()) / 100),
                               X_mass.getMin(), X_mass.getMax())
        plot_binning = binsXmass

    baseCut = ""

    print stype, "|", baseCut

    print " - Reading from Tree"
    treeBkg = TChain("tree")
    for ss in pd:
        if os.path.exists(nTupleDir + ss + "_" + BTAGGING + ".root"):
            treeBkg.Add(nTupleDir + ss + "_" + BTAGGING + ".root")
        else:
            print "found no file for sample:", ss
    setData = RooDataSet("setData", "Data (QCD+TTbar MC)", variables,
                         RooFit.Cut(baseCut), RooFit.WeightVar(weight),
                         RooFit.Import(treeBkg))

    nevents = setData.sumEntries()
    dataMin, dataMax = array('d', [0.]), array('d', [0.])
    setData.getRange(X_mass, dataMin, dataMax)
    xmin, xmax = dataMin[0], dataMax[0]

    lastBin = X_mass.getMax()
    if VARBINS:
        for b in narrow_bins:
            if b > xmax:
                lastBin = b
                break

    print "Imported", (
        "data" if isData else "MC"
    ), "RooDataSet with", nevents, "events between [%.1f, %.1f]" % (xmin, xmax)
    #xmax = xmax+binsXmass.averageBinWidth() # start form next bin

    # 1 parameter
    print "fitting 1 parameter model"
    p1_1 = RooRealVar("CMS" + YEAR + "_" + category + "_p1_1", "p1", 7.0, 0.,
                      2000.)
    modelBkg1 = RooGenericPdf("Bkg1", "Bkg. fit (2 par.)",
                              "1./pow(@0/13000, @1)", RooArgList(X_mass, p1_1))
    normzBkg1 = RooRealVar(
        modelBkg1.GetName() + "_norm", "Number of background events", nevents,
        0., 5. * nevents)  #range dependent of actual number of events!
    modelExt1 = RooExtendPdf(modelBkg1.GetName() + "_ext",
                             modelBkg1.GetTitle(), modelBkg1, normzBkg1)
    fitRes1 = modelExt1.fitTo(setData, RooFit.Extended(True), RooFit.Save(1),
                              RooFit.SumW2Error(not isData),
                              RooFit.Strategy(2), RooFit.Minimizer("Minuit2"),
                              RooFit.PrintLevel(1 if VERBOSE else -1))
    fitRes1.Print()
    RSS[1] = drawFit("Bkg1", category, X_mass, modelBkg1, setData, binsXmass,
                     [fitRes1], normzBkg1.getVal())

    # 2 parameters
    print "fitting 2 parameter model"
    p2_1 = RooRealVar("CMS" + YEAR + "_" + category + "_p2_1", "p1", 0., -100.,
                      1000.)
    p2_2 = RooRealVar("CMS" + YEAR + "_" + category + "_p2_2", "p2",
                      p1_1.getVal(), -100., 600.)
    modelBkg2 = RooGenericPdf("Bkg2", "Bkg. fit (3 par.)",
                              "pow(1-@0/13000, @1) / pow(@0/13000, @2)",
                              RooArgList(X_mass, p2_1, p2_2))
    normzBkg2 = RooRealVar(modelBkg2.GetName() + "_norm",
                           "Number of background events", nevents, 0.,
                           5. * nevents)
    modelExt2 = RooExtendPdf(modelBkg2.GetName() + "_ext",
                             modelBkg2.GetTitle(), modelBkg2, normzBkg2)
    fitRes2 = modelExt2.fitTo(setData, RooFit.Extended(True), RooFit.Save(1),
                              RooFit.SumW2Error(not isData),
                              RooFit.Strategy(2), RooFit.Minimizer("Minuit2"),
                              RooFit.PrintLevel(1 if VERBOSE else -1))
    fitRes2.Print()
    RSS[2] = drawFit("Bkg2", category, X_mass, modelBkg2, setData, binsXmass,
                     [fitRes2], normzBkg2.getVal())

    # 3 parameters
    print "fitting 3 parameter model"
    p3_1 = RooRealVar("CMS" + YEAR + "_" + category + "_p3_1", "p1",
                      p2_1.getVal(), -2000., 2000.)
    p3_2 = RooRealVar("CMS" + YEAR + "_" + category + "_p3_2", "p2",
                      p2_2.getVal(), -400., 2000.)
    p3_3 = RooRealVar("CMS" + YEAR + "_" + category + "_p3_3", "p3", -2.5,
                      -500., 500.)
    modelBkg3 = RooGenericPdf(
        "Bkg3", "Bkg. fit (4 par.)",
        "pow(1-@0/13000, @1) / pow(@0/13000, @2+@3*log(@0/13000))",
        RooArgList(X_mass, p3_1, p3_2, p3_3))
    normzBkg3 = RooRealVar(modelBkg3.GetName() + "_norm",
                           "Number of background events", nevents, 0.,
                           5. * nevents)
    modelExt3 = RooExtendPdf(modelBkg3.GetName() + "_ext",
                             modelBkg3.GetTitle(), modelBkg3, normzBkg3)
    fitRes3 = modelExt3.fitTo(setData, RooFit.Extended(True), RooFit.Save(1),
                              RooFit.SumW2Error(not isData),
                              RooFit.Strategy(2), RooFit.Minimizer("Minuit2"),
                              RooFit.PrintLevel(1 if VERBOSE else -1))
    fitRes3.Print()
    RSS[3] = drawFit("Bkg3", category, X_mass, modelBkg3, setData, binsXmass,
                     [fitRes3], normzBkg3.getVal())

    # 4 parameters
    print "fitting 4 parameter model"
    p4_1 = RooRealVar("CMS" + YEAR + "_" + category + "_p4_1", "p1",
                      p3_1.getVal(), -2000., 2000.)
    p4_2 = RooRealVar("CMS" + YEAR + "_" + category + "_p4_2", "p2",
                      p3_2.getVal(), -2000., 2000.)
    p4_3 = RooRealVar("CMS" + YEAR + "_" + category + "_p4_3", "p3",
                      p3_3.getVal(), -50., 50.)
    p4_4 = RooRealVar("CMS" + YEAR + "_" + category + "_p4_4", "p4", 0.1, -50.,
                      50.)
    modelBkg4 = RooGenericPdf(
        "Bkg4", "Bkg. fit (5 par.)",
        "pow(1 - @0/13000, @1) / pow(@0/13000, @2+@3*log(@0/13000)+@4*pow(log(@0/13000), 2))",
        RooArgList(X_mass, p4_1, p4_2, p4_3, p4_4))
    normzBkg4 = RooRealVar(modelBkg4.GetName() + "_norm",
                           "Number of background events", nevents, 0.,
                           5. * nevents)
    modelExt4 = RooExtendPdf(modelBkg4.GetName() + "_ext",
                             modelBkg4.GetTitle(), modelBkg4, normzBkg4)
    fitRes4 = modelExt4.fitTo(setData, RooFit.Extended(True), RooFit.Save(1),
                              RooFit.SumW2Error(not isData),
                              RooFit.Strategy(2), RooFit.Minimizer("Minuit2"),
                              RooFit.PrintLevel(1 if VERBOSE else -1))
    fitRes4.Print()
    RSS[4] = drawFit("Bkg4", category, X_mass, modelBkg4, setData, binsXmass,
                     [fitRes4], normzBkg4.getVal())

    # Normalization parameters are should be set constant, but shape ones should not
    #    if BIAS:
    #        p1_1.setConstant(True)
    #        p2_1.setConstant(True)
    #        p2_2.setConstant(True)
    #        p3_1.setConstant(True)
    #        p3_2.setConstant(True)
    #        p3_3.setConstant(True)
    #        p4_1.setConstant(True)
    #        p4_2.setConstant(True)
    #        p4_3.setConstant(True)
    #        p4_4.setConstant(True)
    normzBkg1.setConstant(True)
    normzBkg2.setConstant(True)
    normzBkg3.setConstant(True)
    normzBkg4.setConstant(True)

    #*******************************************************#
    #                                                       #
    #                         Fisher                        #
    #                                                       #
    #*******************************************************#

    # Fisher test
    with open(PLOTDIR + "/Fisher_" + category + ".tex", 'w') as fout:
        fout.write(r"\begin{tabular}{c|c|c|c|c}")
        fout.write("\n")
        fout.write(r"function & $\chi^2$ & RSS & ndof & F-test \\")
        fout.write("\n")
        fout.write("\hline")
        fout.write("\n")
        CL_high = False
        for o1 in range(1, 5):
            o2 = min(o1 + 1, 5)
            fout.write("%d par & %.2f & %.2f & %d & " %
                       (o1 + 1, RSS[o1]["chi2"], RSS[o1]["rss"],
                        RSS[o1]["nbins"] - RSS[o1]["npar"]))
            if o2 > len(RSS):
                fout.write(r"\\")
                fout.write("\n")
                continue  #order==0 and
            CL = fisherTest(RSS[o1]['rss'], RSS[o2]['rss'], o1 + 1., o2 + 1.,
                            RSS[o1]["nbins"])
            fout.write("CL=%.3f " % (CL))
            if CL > 0.10:  # The function with less parameters is enough
                if not CL_high:
                    order = o1
                    #fout.write( "%d par are sufficient " % (o1+1))
                    CL_high = True
            else:
                #fout.write( "%d par are needed " % (o2+1))
                if not CL_high:
                    order = o2
            fout.write(r"\\")
            fout.write("\n")
        fout.write("\hline")
        fout.write("\n")
        fout.write(r"\end{tabular}")
    print "saved F-test table as", PLOTDIR + "/Fisher_" + category + ".tex"

    #print "-"*25
    #print "function & $\\chi^2$ & RSS & ndof & F-test & result \\\\"
    #print "\\multicolumn{6}{c}{", "Zprime_to_bb", "} \\\\"
    #print "\\hline"
    #CL_high = False
    #for o1 in range(1, 5):
    #    o2 = min(o1 + 1, 5)
    #    print "%d par & %.2f & %.2f & %d & " % (o1+1, RSS[o1]["chi2"], RSS[o1]["rss"], RSS[o1]["nbins"]-RSS[o1]["npar"]),
    #    if o2 > len(RSS):
    #        print "\\\\"
    #        continue #order==0 and
    #    CL = fisherTest(RSS[o1]['rss'], RSS[o2]['rss'], o1+1., o2+1., RSS[o1]["nbins"])
    #    print "%d par vs %d par CL=%f & " % (o1+1, o2+1, CL),
    #    if CL > 0.10: # The function with less parameters is enough
    #        if not CL_high:
    #            order = o1
    #            print "%d par are sufficient" % (o1+1),
    #            CL_high=True
    #    else:
    #        print "%d par are needed" % (o2+1),
    #        if not CL_high:
    #            order = o2
    #    print "\\\\"
    #print "\\hline"
    #print "-"*25
    #print "@ Order is", order, "("+category+")"

    #order = min(3, order)
    #order = 2
    if order == 1:
        modelBkg = modelBkg1  #.Clone("Bkg")
        modelAlt = modelBkg2  #.Clone("BkgAlt")
        normzBkg = normzBkg1  #.Clone("Bkg_norm")
        fitRes = fitRes1
    elif order == 2:
        modelBkg = modelBkg2  #.Clone("Bkg")
        modelAlt = modelBkg3  #.Clone("BkgAlt")
        normzBkg = normzBkg2  #.Clone("Bkg_norm")
        fitRes = fitRes2
    elif order == 3:
        modelBkg = modelBkg3  #.Clone("Bkg")
        modelAlt = modelBkg4  #.Clone("BkgAlt")
        normzBkg = normzBkg3  #.Clone("Bkg_norm")
        fitRes = fitRes3
    elif order == 4:
        modelBkg = modelBkg4  #.Clone("Bkg")
        modelAlt = modelBkg3  #.Clone("BkgAlt")
        normzBkg = normzBkg4  #.Clone("Bkg_norm")
        fitRes = fitRes4
    else:
        print "Functions with", order + 1, "or more parameters are needed to fit the background"
        exit()

    modelBkg.SetName("Bkg_" + YEAR + "_" + category)
    modelAlt.SetName("Alt_" + YEAR + "_" + category)
    normzBkg.SetName("Bkg_" + YEAR + "_" + category + "_norm")

    print "-" * 25

    # Generate pseudo data
    setToys = RooDataSet()
    setToys.SetName("data_toys")
    setToys.SetTitle("Data (toys)")
    if not isData:
        print " - Generating", nevents, "events for toy data"
        setToys = modelBkg.generate(RooArgSet(X_mass), nevents)
        #setToys = modelAlt.generate(RooArgSet(X_mass), nevents)
        print "toy data generated"

    if VERBOSE: raw_input("Press Enter to continue...")

    #*******************************************************#
    #                                                       #
    #                         Plot                          #
    #                                                       #
    #*******************************************************#

    print "starting to plot"
    c = TCanvas("c_" + category, category, 800, 800)
    c.Divide(1, 2)
    setTopPad(c.GetPad(1), RATIO)
    setBotPad(c.GetPad(2), RATIO)
    c.cd(1)
    frame = X_mass.frame()
    setPadStyle(frame, 1.25, True)
    if VARBINS: frame.GetXaxis().SetRangeUser(X_mass.getMin(), lastBin)
    signal = getSignal(
        category, stype,
        2000)  #replacing Alberto's getSignal by own dummy function

    graphData = setData.plotOn(frame, RooFit.Binning(plot_binning),
                               RooFit.Scaling(False), RooFit.Invisible())
    modelBkg.plotOn(frame, RooFit.VisualizeError(fitRes, 1, False),
                    RooFit.LineColor(602), RooFit.FillColor(590),
                    RooFit.FillStyle(1001), RooFit.DrawOption("FL"),
                    RooFit.Name("1sigma"))
    modelBkg.plotOn(frame, RooFit.LineColor(602), RooFit.FillColor(590),
                    RooFit.FillStyle(1001), RooFit.DrawOption("L"),
                    RooFit.Name(modelBkg.GetName()))
    modelAlt.plotOn(frame, RooFit.LineStyle(7), RooFit.LineColor(613),
                    RooFit.FillColor(609), RooFit.FillStyle(1001),
                    RooFit.DrawOption("L"), RooFit.Name(modelAlt.GetName()))
    if not isSB and signal[0] is not None:  # FIXME remove /(2./3.)
        signal[0].plotOn(
            frame,
            RooFit.Normalization(signal[1] * signal[2], RooAbsReal.NumEvent),
            RooFit.LineStyle(3), RooFit.LineWidth(6), RooFit.LineColor(629),
            RooFit.DrawOption("L"), RooFit.Name("Signal"))
    graphData = setData.plotOn(
        frame, RooFit.Binning(plot_binning), RooFit.Scaling(False),
        RooFit.XErrorSize(0 if not VARBINS else 1),
        RooFit.DataError(RooAbsData.Poisson if isData else RooAbsData.SumW2),
        RooFit.DrawOption("PE0"), RooFit.Name(setData.GetName()))
    fixData(graphData.getHist(), True, True, not isData)
    pulls = frame.pullHist(setData.GetName(), modelBkg.GetName(), True)
    chi = frame.chiSquare(setData.GetName(), modelBkg.GetName(), True)
    #setToys.plotOn(frame, RooFit.DataError(RooAbsData.Poisson), RooFit.DrawOption("PE0"), RooFit.MarkerColor(2))
    frame.GetYaxis().SetTitle("Events / ( 100 GeV )")
    frame.GetYaxis().SetTitleOffset(1.05)
    frame.Draw()
    #print "frame drawn"
    # Get Chi2
    #    chi2[1] = frame.chiSquare(modelBkg1.GetName(), setData.GetName())
    #    chi2[2] = frame.chiSquare(modelBkg2.GetName(), setData.GetName())
    #    chi2[3] = frame.chiSquare(modelBkg3.GetName(), setData.GetName())
    #    chi2[4] = frame.chiSquare(modelBkg4.GetName(), setData.GetName())

    frame.SetMaximum(frame.GetMaximum() * 10)
    frame.SetMinimum(max(frame.GetMinimum(), 1.e-1))
    c.GetPad(1).SetLogy()

    drawAnalysis(category)
    drawRegion(category, True)
    #drawCMS(LUMI, "Simulation Preliminary")
    drawCMS(LUMI, "Work in Progress", suppressCMS=True)

    leg = TLegend(0.575, 0.6, 0.95, 0.9)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    leg.AddEntry(setData.GetName(),
                 setData.GetTitle() + " (%d events)" % nevents, "PEL")
    leg.AddEntry(modelBkg.GetName(), modelBkg.GetTitle(),
                 "FL")  #.SetTextColor(629)
    leg.AddEntry(modelAlt.GetName(), modelAlt.GetTitle(), "L")
    if not isSB and signal[0] is not None:
        leg.AddEntry("Signal", signal[0].GetTitle(), "L")
    leg.SetY1(0.9 - leg.GetNRows() * 0.05)
    leg.Draw()

    latex = TLatex()
    latex.SetNDC()
    latex.SetTextSize(0.04)
    latex.SetTextFont(42)
    if not isSB:
        latex.DrawLatex(leg.GetX1() * 1.16,
                        leg.GetY1() - 0.04, "HVT model B (g_{V}=3)")
    #    latex.DrawLatex(0.67, leg.GetY1()-0.045, "#sigma_{X} = 1.0 pb")

    c.cd(2)
    frame_res = X_mass.frame()
    setPadStyle(frame_res, 1.25)
    frame_res.addPlotable(pulls, "P")
    setBotStyle(frame_res, RATIO, False)
    if VARBINS: frame_res.GetXaxis().SetRangeUser(X_mass.getMin(), lastBin)
    frame_res.GetYaxis().SetRangeUser(-5, 5)
    frame_res.GetYaxis().SetTitle("pulls(#sigma)")
    frame_res.GetYaxis().SetTitleOffset(0.3)
    frame_res.Draw()
    fixData(pulls, False, True, False)

    drawChi2(RSS[order]["chi2"], RSS[order]["nbins"] - (order + 1), True)
    line = drawLine(X_mass.getMin(), 0, lastBin, 0)

    if VARBINS:
        c.SaveAs(PLOTDIR + "/BkgSR_" + category + ".pdf")
        c.SaveAs(PLOTDIR + "/BkgSR_" + category + ".png")
    else:
        c.SaveAs(PLOTDIR + "/BkgSR_" + category + ".pdf")
        c.SaveAs(PLOTDIR + "/BkgSR_" + category + ".png")

    #*******************************************************#
    #                                                       #
    #                   Generate workspace                  #
    #                                                       #
    #*******************************************************#

    if BIAS:
        gSystem.Load("libHiggsAnalysisCombinedLimit.so")
        from ROOT import RooMultiPdf
        cat = RooCategory("pdf_index", "Index of Pdf which is active")
        pdfs = RooArgList(modelBkg, modelAlt)
        roomultipdf = RooMultiPdf("roomultipdf", "All Pdfs", cat, pdfs)
        normulti = RooRealVar("roomultipdf_norm",
                              "Number of background events", nevents, 0., 1.e6)

    normzBkg.setConstant(
        False
    )  ## newly put here to ensure it's freely floating in the combine fit

    # create workspace
    w = RooWorkspace("Zprime_" + YEAR, "workspace")
    # Dataset
    if isData: getattr(w, "import")(setData, RooFit.Rename("data_obs"))
    else: getattr(w, "import")(setToys, RooFit.Rename("data_obs"))
    #getattr(w, "import")(setData, RooFit.Rename("data_obs"))
    if BIAS:
        getattr(w, "import")(cat, RooFit.Rename(cat.GetName()))
        getattr(w, "import")(normulti, RooFit.Rename(normulti.GetName()))
        getattr(w, "import")(roomultipdf, RooFit.Rename(roomultipdf.GetName()))
    getattr(w, "import")(modelBkg, RooFit.Rename(modelBkg.GetName()))
    getattr(w, "import")(modelAlt, RooFit.Rename(modelAlt.GetName()))
    getattr(w, "import")(normzBkg, RooFit.Rename(normzBkg.GetName()))
    w.writeToFile(WORKDIR + "%s_%s.root" % (DATA_TYPE + "_" + YEAR, category),
                  True)
    print "Workspace", WORKDIR + "%s_%s.root" % (
        DATA_TYPE + "_" + YEAR, category), "saved successfully"
    if VERBOSE: raw_input("Press Enter to continue...")
コード例 #21
0
def addPlots(plots):
    from ROOT import RooCurve, RooHist, TLine, TLegend, RooPlot, RooAbsData, \
         SetOwnership, TGraphErrors
    #print plots
    outplot = plots[0].emptyClone(plots[0].GetName())
    newMax = 0.
    for item in range(0, int(plots[0].numItems())):
        itemName = plots[0].nameOf(item)
        firstItem = plots[0].getObject(item)
        if (type(firstItem) == RooCurve):
            fullCurve = clipCurve(firstItem)
            for plot in range(1, len(plots)):
                nextCurve = clipCurve(plots[plot].getCurve(itemName))
                fullCurve = RooCurve(fullCurve.GetName(), fullCurve.GetTitle(),
                                     fullCurve, nextCurve)
            fullCurve.addPoint(fullCurve.GetX()[fullCurve.GetN() - 1], 0)
            fullCurve.addPoint(fullCurve.GetX()[0], 0)
            fullCurve.SetLineColor(firstItem.GetLineColor())
            fullCurve.SetLineStyle(firstItem.GetLineStyle())
            fullCurve.SetFillColor(firstItem.GetFillColor())
            fullCurve.SetFillStyle(firstItem.GetFillStyle())
            outplot.addPlotable(fullCurve,
                                plots[0].getDrawOptions(itemName).Data())
            SetOwnership(fullCurve, False)

        if (type(firstItem) == RooHist):
            fullHist = firstItem
            for plot in range(1, len(plots)):
                nextHist = plots[plot].getHist(itemName)
                fullHist = addHists(fullHist, nextHist)
            fullHist.SetName(itemName)
            fullHist.SetTitle(firstItem.GetTitle())
            outplot.addPlotable(fullHist,
                                plots[0].getDrawOptions(itemName).Data())
            SetOwnership(fullHist, False)

        if (type(firstItem) == TGraphErrors):
            fullErrors = firstItem
            for plot in range(1, len(plots)):
                nextErrors = plots[plot].findObject(itemName)
                fullErrors = addErrors(fullErrors, nextErrors)
            fullErrors.SetName(itemName)
            fullErrors.SetTitle(firstItem.GetTitle())
            outplot.addObject(fullErrors,
                              plots[0].getDrawOptions(itemName).Data())
            SetOwnership(fullErrors, False)

        if (type(firstItem) == TLine):
            newLine = TLine(firstItem)
            newLine.SetY2(outplot.GetMaximum())
            SetOwnership(newLine, False)
            outplot.addObject(newLine)
            pass

        if (type(firstItem) == TLegend):
            newLeg = TLegend(firstItem)
            newLeg.SetY1NDC(0.92 - \
                            0.04*newLeg.GetListOfPrimitives().GetSize() - \
                            0.02)
            newLeg.SetY1(newLeg.GetY1NDC())
            SetOwnership(newLeg, False)
            outplot.addObject(newLeg)

    for plot in plots:
        newMax += plot.GetMaximum()

    outplot.SetMaximum(outplot.GetMaximum() * 1.3)
    outplot.GetYaxis().SetTitle("Events / GeV")
    ##     outplot.SetMaximum(newMax)
    ##         outplot.Print("v")

    return outplot
コード例 #22
0
def plot(var, cut, signal):
    sign = signal
    ### Preliminary Operations ###
    treeRead = not 'cutflow' in var
    #treeRead = False
    channel = cut
    if 'inc' in cut:
        eventWeightLuminame = 'eventWeightLumi_nobtag'
    else:
        eventWeightLuminame = 'eventWeightLumi'
    if "SB" in cut or "SR" in cut:
        channel_name = cut[:-2]
    else:
        channel_name = cut
    if year in ['2016', '2017', '2018']:
        NTUPLEDIR = "/work/pbaertsc/heavy_resonance/Ntuples%s/" % (year)
    else:
        NTUPLEDIR = "/work/pbaertsc/heavy_resonance/"
    #showSignal = False if 'SB' in cut or 'TR' in cut else True #'SR' in channel or channel=='qqqq'#or len(channel)==5
    if var in [
            'dijet_VBF_mass', 'deltaR_VBF', 'deltaR_HVBFjet1',
            'deltaR_HVBFjet2'
    ]:
        showSignal = False
    else:
        showSignal = True
    if treeRead:
        for k in sorted(selection.keys(), key=len, reverse=True):
            if k in cut: cut = cut.replace(k, selection[k])

    print "Plotting from", ("tree" if treeRead else
                            "file"), var, "in", channel, "channel with:"
    print "  cut    :", cut

    ### Create and fill MC histograms ###
    # Create dict
    file = {}
    tree = {}
    hist = {}
    ### Create and fill MC histograms ###
    tree[sign] = TChain("tree")
    for j, ss in enumerate(sample[sign]['files']):
        tree[sign].Add(NTUPLEDIR + ss + ".root")
    if variable[var]['nbins'] > 0:
        min_value = variable[var]['min']
        max_value = variable[var]['max']
        title = variable[var]['title']
        hist[sign] = TH1F(
            sign,
            ";" + title + ";Events;" + ('log' if variable[var]['log'] else ''),
            variable[var]['nbins'], min_value, max_value)
    else:
        hist[s] = TH1F(sign, ";" + variable[var]['title'],
                       len(variable[var]['bins']) - 1,
                       array('f', variable[var]['bins']))
    hist[sign].Sumw2()
    cutstring = "%s" % eventWeightLuminame + ("*(" + cut + ")")
    tree[sign].Project(sign, var, cutstring)
    if not tree[sign].GetTree() == None:
        hist[sign].SetOption("%s" % tree[sign].GetTree().GetEntriesFast())
    hist[sign].SetFillColor(sample[sign]['fillcolor'])
    hist[sign].SetFillStyle(sample[sign]['fillstyle'])
    hist[sign].SetLineColor(sample[sign]['linecolor'])
    hist[sign].SetLineStyle(sample[sign]['linestyle'])

    hist[sign].SetLineWidth(3)
    sample[sign]['plot'] = True

    # Legend
    leg = TLegend(0.6, 0.6, 0.9, 0.9)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    leg.AddEntry(hist[sign], sample[sign]['label'], "fl")

    leg.SetY1(0.9 - leg.GetNRows() * 0.05)

    # --- Display ---
    c1 = TCanvas("c1", hist.values()[0].GetXaxis().GetTitle(), 800, 800)

    c1.SetTopMargin(0.06)
    c1.SetRightMargin(0.05)
    c1.SetTicks(1, 1)
    hist[sign].Draw("HIST")

    leg.Draw()
    drawCMS(LUMI, year, "Preliminary")
    drawRegion('XVH' + channel, True)
    drawAnalysis(channel)

    c1.Update()

    if gROOT.IsBatch():
        varname = var.replace('.', '_').replace('()', '')
        if not os.path.exists(OUTPUTDIR + channel_name):
            os.makedirs(OUTPUTDIR + channel_name)
        c1.Print("%s" % OUTPUTDIR + channel_name + "/" + varname + sign +
                 ".png")
        c1.Print("%s" % OUTPUTDIR + channel_name + "/" + varname + sign +
                 ".pdf")