Beispiel #1
0
def KS_test(sys, region):
    rf = tesf
    sf = tesf

    gsys = {}

    for group in groups:
        rf.cd()
        print group.name
        if group.name == 'Wlnu':
            if 'OS' in region: wregion = 'WCR_OS'
            elif 'SS' in region: wregion = 'WCR_SS'
            nom = hist.get_dataW(rf, mu, entry, wregion, verbose=False)
        else:
            nom = rf.Get('h_upsilon_%s_%s' % (group.name, region))
        #print group.name, nom.Integral()
        #continue
        for i, s in enumerate(sys):
            #if 'PU' in s: continue
            for ud in ['UP', 'DOWN']:
                #ud = ud.lower()
                if not s + '_' + ud in gsys:
                    gsys[s + '_' + ud] = []
                if not nom.Integral():
                    gsys[s + '_' + ud].append('-')
                    continue
                if group.name == 'Wlnu':
                    sf.cd()
                    shift = hist.get_dataW(rf,
                                           mu,
                                           entry,
                                           wregion,
                                           verbose=False,
                                           sf=sf,
                                           sysf=s + '_' + ud)
                else:
                    sf.cd()
                    shift = sf.Get('h_upsilon_%s_%s_%s_%s' %
                                   (group.name, region, s, ud))
                print shift.Integral()
                gsys[s + '_' + ud].append(nom.KolmogorovTest(shift))

    print row('', [group.name for group in groups],
              fcol=15,
              col=15,
              fmt='latex')

    for s in sys:
        #if 'PU' in s: continue
        for ud in ['UP', 'DOWN']:
            #ud = ud.lower()
            what = s + '_' + ud
            for i, k in enumerate(gsys[what]):
                if k < 0.9:
                    gsys[what][i] = '\\textcolor{red}{%.4f}' % k
                    whatp = '\\textcolor{red}{%s}' % what
                else:
                    whatp = what
            print row(whatp, gsys[what], fcol=15, col=10, fmt='latex')
Beispiel #2
0
def addSRdata(region='SR_OS', pol=''):
    # first get QCD estimate with data driven W
    if pol:
        groups = mu.MC + [getattr(mu, pol)]
    else:
        groups = mu.MC + [mu.signal]

    data = hist.get_QCDW(rf,
                         mu,
                         entry,
                         'SR_SS',
                         rqcd=rQCD,
                         verbose=False,
                         groups=groups)
    for group in groups:
        if group.name == 'Wlnu':
            h = hist.get_dataW(rf,
                               mu,
                               entry,
                               'WCR_OS',
                               verbose=False,
                               groups=groups)
        else:
            h = hist.get_OS(rf, entry, group, region)
        data.Add(h)
    data.SetName('h_upsilon_SRData%s_SR_OS' % pol)
    for bin in xrange(data.GetNbinsX() + 1):
        data.SetBinError(bin, math.sqrt(data.GetBinContent(bin)))
    print ErrorFloat(*hist.IntegralError(data))
    #check similar errors
    #d1 = rf.Get('h_visMass_Muons_SR_OS')
    #print ErrorFloat(*hist.IntegralError(d1))
    data.Write()
Beispiel #3
0
def addWsys(mu, region):
    # nominal W estimate from WCR
    h = hist.get_dataW(rf,
                       mu,
                       entry,
                       'WCR_%s' % region,
                       verbose=False,
                       groups=groups)
    ## get slope
    if mu.name == 'mu':
        if 'OS' in region:
            p = -0.0269
        elif 'SS' in region:
            p = 0.0930
    if mu.name == 'el':
        if 'OS' in region:
            p = -0.0494
        elif 'SS' in region:
            p = 0.0626
    hup = h.Clone('up')
    hup.SetName('h_upsilon_WData_SR_%s_Wshape_UP' % (region))
    hdown = h.Clone('down')
    hdown.SetName('h_upsilon_WData_SR_%s_Wshape_DOWN' % (region))
    for bin in xrange(hup.GetNbinsX() + 1):
        bc = hup.GetBinCenter(bin)
        hup.SetBinContent(bin, (1 + (bc - 0.25) * p) * hup.GetBinContent(bin))
        hdown.SetBinContent(bin,
                            (1. - (bc - 0.25) * p) * hdown.GetBinContent(bin))
        #print hup.GetBinContent(bin)

    hup.Write()
    hdown.Write()
Beispiel #4
0
def print_region(mu,reg, pdg=False):#,regions,groups,entry=Drawer.upsilon_CR,eff=False,twiki=False, fmt = None, f = False):
    a = []
    rf = ROOT.TFile('%s/%s_regions.root' % (OUTPUT_DIR, mu.name))
    regions = getattr(mu,reg)
    groups = [mu.data, mu.signal, mu.LH, mu.RH,  mu.ttbar, mu.Zlljet, mu.Zltau, mu.Wlnu]
    entry = Drawer.upsilon_CR
    fmt =  ''
    if pdg: fmt = 'latex'

    print row('',[group.name for group in groups],fcol=fcol,col=col,fmt=fmt)
    for i,region in enumerate(regions):
        cutgroup = []
        n = []
        mc = 0
        nEW = 0
        for group in groups:
            if 'Wlnu' in group.name:
                wreg = region.name.replace('QCD','WCR')
                wreg = wreg.replace('SR','WCR')
                #print region
                h = hist.get_dataW(rf,mu,entry, wreg, where = reg)
                #print h.Integral()
            else:
                h = rf.Get('h_%s_%s_%s' % (entry.name,group.name,region.name))
            norm = h.Integral(0,h.GetNbinsX()+1)
            if group.isData:
                cutgroup.append(int(norm))
            else:
                #print h.GetName()
                v,e = hist.IntegralError(h, of = True)
                if pdg:
                    cutgroup.append( '%g +/- %g' % pdg_simple.pdg_round(v,e))
                else:
                    cutgroup.append( '%.2f +/- %.2f' % (v,e))
            
            n.append(norm)
            if group.isData: 
                nData = norm
            elif 'LH' in group.name:
                nL = norm
            elif 'RH' in group.name:
                nR = norm
            elif 'Ztautau' in group.name:
                nS = norm
            elif 'Wlnu' not in group.name:
                nEW += norm

            if i == 0: a = list(n); l=a
        #if twiki: print twikirow(region.name,cutgroup)
        print row(region.name,cutgroup,fcol=fcol,col=col, fmt = fmt)
Beispiel #5
0
def addWSR(region='OS', sys=''):

    h = hist.get_dataW(rf,
                       mu,
                       entry,
                       'WCR_%s' % region,
                       verbose=False,
                       groups=groups,
                       sf=rf,
                       sysf=sys)
    if not sys:
        h.SetName('h_upsilon_WData_SR_%s' % region)
    else:
        h.SetName('h_upsilon_WData_SR_%s_%s' % (region, sys))
    #print h.GetName(), h.Integral()
    h.Write()
Beispiel #6
0
def stat_error_wcr(mu):
    rf = ROOT.TFile('%s/%s_regions.root' % (OUTPUT_DIR,mu.name))
    k_entry = Drawer.upsilon_CR
    hist.get_dataW(rf,mu,k_entry,'WCR_OS')
Beispiel #7
0
def signal_region(mu):
    #rf = ROOT.TFile('~/panalysis/output/%s_thesis/test/%s_regions.root' % (mu.name,mu.name))
    rf = ROOT.TFile('%s/%s_regions.root' % (OUTPUT_DIR, mu.name))
    region = 'SR_OS'
    k_entry = Drawer.upsilon_CR
    entry = Drawer.upsilon
    verbose = False
    fmt = 'latex'
    kW_OS = hist.calc_kX(rf,mu,k_entry,'WCR','Wlnu',OS='OS', verbose = verbose)
    kW_OS_L = hist.calc_kX(rf,mu,k_entry,'WCR','Wlnu',OS='OS', verbose = verbose, groups = mu.MC+[mu.LH])
    kW_OS_R = hist.calc_kX(rf,mu,k_entry,'WCR','Wlnu',OS='OS', verbose = verbose, groups = mu.MC+[mu.RH])
    pdg_simple.testpdg(kW_OS.val,kW_OS.err)
    print 'kWOS %g +/ %g' % ( pdg_simple.pdg_round(kW_OS.val,kW_OS.err))

    EST = [ErrorFloat(0),ErrorFloat(0),ErrorFloat(0)]
    BG = [ErrorFloat(0),ErrorFloat(0),ErrorFloat(0)]
    for group in [mu.data,mu.Zlljet,mu.Zltau,mu.ttbar]:
        h = rf.Get('h_%s_%s_%s' % (entry.name,group.name,region))

        nG = ErrorFloat(*hist.IntegralError(h))
        if group.isData:
            print row( group.latex, [int(nG.val),'',''], fmt=fmt)
        else:
            print row( group.latex, ['%i +/- %i'%(nG.val,nG.err),'',''], fmt=fmt, makeint=True)
        if not group.isData:
            for i in xrange(len(EST)):
                EST[i]+=nG
                BG[i]+=nG
                
    print row('', ['SM','Left-Handed','Right-Handed'], fmt = fmt)
    signal = []
    SB = [ErrorFloat(0),ErrorFloat(0),ErrorFloat(0)]
    for i,group in enumerate([mu.signal, mu.LH, mu.RH]):
        h = rf.Get('h_%s_%s_%s' % (entry.name,group.name,region))
        nG = ErrorFloat(*hist.IntegralError(h))
        signal.append('%i +/- %i'%(nG.val,nG.err))
        EST[i]+=nG
        SB[i]=nG
    print row(mu.signal.latex, signal, fmt=fmt, makeint = True)
    wjets = []
    for i,group in enumerate([mu.signal, mu.LH, mu.RH]):
        h =  hist.get_dataW(rf, mu, Drawer.upsilon, 'WCR_OS', verbose = False, groups = mu.MC + [group])
        print '**'
        nG = ErrorFloat(*hist.IntegralError(h))
        wjets.append('%g +/- %g'%( pdg_simple.pdg_round((nG).val,(nG).err)))
        EST[i]+=nG
        BG[i]+=nG

    print row(mu.Wlnu.latex, wjets, fmt=fmt)
        #print row(group.latex, [nG])
    QCD = []
    for i,group in enumerate([mu.signal, mu.LH, mu.RH]):
        kW = hist.calc_kX(rf,mu,k_entry,'WCR','Wlnu',OS='OS', verbose = verbose, groups = mu.MC+[group])
        kWSS = hist.calc_kX(rf,mu,k_entry,'WCR','Wlnu',OS='SS', verbose = verbose, groups = mu.MC+[group])
        rqcd = hist.calc_rQCD(rf,mu,k_entry,'QCD',kW_OS = kW, kW_SS = kWSS, verbose = verbose, groups = mu.MC+[group])
        print '%g +/- %g' % pdg_simple.pdg_round(rqcd.val,rqcd.err)
        qcd = hist.get_QCD(rf,mu,entry,'SR_SS', kW = kWSS, rqcd = rqcd, groups = mu.MC +[group])

        nG = ErrorFloat(*hist.IntegralError(qcd))
        QCD.append('%g +/- %g'% pdg_simple.pdg_round(nG.val,nG.err))
        EST[i]+=nG
        BG[i]+=nG        
    print row('Multijet', QCD, fmt=fmt)

    for i in xrange(len(EST)):
        EST[i] = '%i +/- %i'%(EST[i].val,EST[i].err)
        SB[i] = SB[i]/BG[i]

    print row('Total Estimate', EST, fmt = fmt)
    print row('S:B', SB, fmt = fmt)
Beispiel #8
0
def NormStat(sys, region='SR_OS'):
    gsys = {}

    for group in groups:
        rf.cd()
        if group.name == 'Wlnu':
            if 'OS' in region: wregion = 'WCR_OS'
            elif 'SS' in region: wregion = 'WCR_SS'
            nom = hist.get_dataW(rf, mu, entry, wregion, verbose=False)
        else:
            nom = rf.Get('h_upsilon_%s_%s' % (group.name, region))
        #print group.name,nom.Integral()
        #continue
        for i, s in enumerate(syst):
            if 'PU' in s: continue
            for ud in ['UP', 'DOWN']:
                ud = ud.lower()
                if not s + '_' + ud in gsys:
                    gsys[s + '_' + ud] = []
                if not nom.Integral():
                    gsys[s + '_' + ud].append('-')
                    continue
                if group.name == 'Wlnu':
                    sf.cd()
                    shift = hist.get_dataW(rf,
                                           mu,
                                           entry,
                                           wregion,
                                           verbose=False,
                                           sf=sf,
                                           sysf=s + '_' + ud)
                else:
                    sf.cd()
                    shift = sf.Get('h_upsilon_%s_%s_%s_%s' %
                                   (group.name, region, s, ud))
                sigmastat = abs(shift.Integral() - nom.Integral()) / math.sqrt(
                    nom.Integral())
                gsys[s + '_' + ud].append(sigmastat)

    print row('', [group.name for group in groups],
              fcol=15,
              col=15,
              fmt='latex')

    for s in syst:
        if 'PU' in s: continue
        for ud in ['UP', 'DOWN']:
            ud = ud.lower()
            what = s + '_' + ud
            whatp = what
            imax = -1
            kmax = 0
            for i, k in enumerate(gsys[what]):
                if k > 0.01:
                    gsys[what][i] = '\\textcolor{red}{%.4f}' % k
                    whatp = '\\textcolor{red}{%s}' % what
                    if k > kmax:
                        kmax = k
                        imax = i
                if k <= 0.: gsys[what][i] = '-'
            if imax >= 0:
                gsys[what][imax] = gsys[what][imax].replace('red', 'blue')

            print row(whatp, gsys[what], fcol=15, col=10, fmt='latex')
Beispiel #9
0
def charged_asymmetry(mu):
    rf = ROOT.TFile('~/panalysis/output/prodr/%s_ttbar/%s_regions.root' %
                    (mu.name, mu.name))
    entry = upsilon_20
    k_entry = upsilon_CR
    verbose = False
    region = 'TTBAR_OS'
    # scale factors
    k_entry = upsilon_CR

    groups = mu.MC + [mu.signal]
    kW_OS = hist.calc_kX(rf,
                         mu,
                         k_entry,
                         'WCR',
                         'Wlnu',
                         OS='OS',
                         verbose=verbose,
                         groups=groups)
    kW_SS = hist.calc_kX(rf,
                         mu,
                         k_entry,
                         'WCR',
                         'Wlnu',
                         OS='SS',
                         verbose=verbose,
                         groups=groups)
    rQCD = hist.calc_rQCD(rf,
                          mu,
                          k_entry,
                          'QCD',
                          kW_OS=kW_OS,
                          kW_SS=kW_SS,
                          verbose=verbose,
                          groups=groups)

    # Canvas
    if entry.blind: ratio = False
    ratio = False
    cname = '%s_%s_%s' % (mu.name, entry.name, region)
    #pname = '%s_%s_%s' % (mu.name,region.rstrip('_OS'), hand)
    if ratio:
        c = ROOT.TCanvas(cname, cname, 1500, 1000)
        #Split for ratio plot
        c.Divide(1, 2)
        c.cd(1).SetPad(0.0, 0.2, 1.0, 1.0)
        c.cd(2).SetPad(0.0, 0.0, 1.0, 0.2)
        c.cd(1)
    else:
        c = ROOT.TCanvas(cname, cname, 1200, 800)
    rb = 1
    # data
    if 'SR_OS' in region: dregion = 'SR_OS'
    if 'SR_SS' in region: dregion = 'SR_SS'
    data = rf.Get('h_%s_%s_%s' % (entry.name, mu.data.name, dregion)).Clone()
    data.SetMarkerStyle(20)
    data.SetMarkerSize(2)
    data.SetLabelSize(0.04, "y")
    data.Rebin(rb)
    # Blind data
    data.Reset()
    if entry.xmax:
        data.SetAxisRange(entry.xmin or 0, entry.xmax)
    # stat. unc. on stack
    stacksum = data.Clone("tmp")
    stacksum.Reset()

    # Legend
    x0 = 0.73
    y0 = 0.55
    step = 0.045

    nl = 4 + len(mu.MC) - 1 * entry.blind
    l = ROOT.TLegend(x0, y0, x0 + .16, y0 + nl * step)
    l.SetTextSize(0.0345)
    l.SetBorderSize(0)
    l.SetFillColor(0)

    # Data
    # Stack
    st = ROOT.THStack(cname, "%s; %s; " % (cname, entry.xtitle))

    # QCD ESTIMATE
    #if region == 'SR_SS': rQCD = ErrorFloat(1.,0.)
    qcd = hist.get_QCDW(rf,
                        mu,
                        entry,
                        'SR_SS',
                        rqcd=rQCD,
                        verbose=False,
                        groups=groups)
    qcd.SetLineWidth(0)
    qcd.SetLineColor(3)
    qcd.SetFillColor(3)
    qcd.SetMarkerStyle(0)
    qcd.SetMarkerColor(3)
    qcd.Rebin(rb)

    if entry.xmax:
        qcd.SetAxisRange(entry.xmin or 0, entry.xmax)

    st.Add(qcd)
    stacksum.Add(qcd)

    if verbose:
        total_est = ErrorFloat(*hist.IntegralError(qcd))
        print 'Data: %.0f +/- %.0f' % (hist.IntegralError(data)[0],
                                       hist.IntegralError(data)[1])
        print 'NQCD: %.0f +/- %.0f' % (hist.IntegralError(qcd)[0],
                                       hist.IntegralError(qcd)[1])

    mc_leg = []

    n_total = ErrorFloat(
        *hist.IntegralError(hist.get_OS(rf, entry, mu.signal, region)))

    for group in groups:
        entry.add_group(group)
        if group.name == 'Wlnu':
            h = hist.get_dataW(rf,
                               mu,
                               entry,
                               'WCR_OS',
                               verbose=False,
                               groups=groups)
        else:
            h = hist.get_OS(rf, entry, group, region)
        h.Rebin(rb)

        if entry.xmax:
            h.SetAxisRange(entry.xmin or 0, entry.xmax)

        h.SetLineColor(entry[group.name]['fill'])
        h.SetLineWidth(0)
        h.SetMarkerStyle(0)
        h.SetMarkerColor(entry[group.name]['fill'])
        h.SetFillColor(entry[group.name]['fill'])
        h.SetLabelSize(0.04, "y")

        st.Add(h)
        stacksum.Add(h)
        mc_leg.append((h, group.legend, hist.IntegralError(h)[0],
                       hist.IntegralError(h)[1]))

        if verbose:
            print '%s: %.0f +/- %.0f' % (group.name, hist.IntegralError(h)[0],
                                         hist.IntegralError(h)[1])
            total_est += ErrorFloat(*hist.IntegralError(h))

    if verbose:
        print 'Total Estimated: %.0f +/- %.0f' % (total_est.val, total_est.err)

    for li in reversed(mc_leg):
        l.AddEntry(li[0], '%s' % (li[1]))

    l.AddEntry(
        qcd, 'Multijet'
    )  #: %.0f +/- %.0f' % (hist.IntegralError(qcd)[0],hist.IntegralError(qcd)[1]))
    data.SetXTitle(entry.xtitle)
    data.GetXaxis().SetTitleOffset(1.25)

    stacksum.SetFillStyle(3354)
    stacksum.SetLineColor(1)
    stacksum.SetFillColor(12)
    stacksum.SetMarkerStyle(1)

    maxi = max(data.GetMaximum(), st.GetMaximum())
    maxi = 6250
    mini = 0.
    data.SetMinimum(1.2 * mini)
    data.SetMaximum(maxi)

    bw = data.GetXaxis().GetBinWidth(1)
    data.GetYaxis().SetTitle('Events / %s %s' % (bw, entry.units))

    if not entry.blind:
        data.Draw("PE")
        st.Draw("HIST,SAME")
        stacksum.Draw("E2,SAME")
        data.Draw("SAME, PE")
    else:
        data.Reset()
        data.Draw()
        st.SetTitle('')

        st.SetMinimum(1.2 * st.GetMinimum())
        st.SetMaximum(1.5 * st.GetMaximum())
        st.Draw("HIST,SAME")
        stacksum.Draw("E2,SAME")

    ROOT.gPad.RedrawAxis()
    l.Draw()

    atlas = ROOT.ATLASLabel(0.32, 0.86, "   Work in Progress")
    td = ROOT.TPaveText(0.54, 0.68, 0.67, 0.85, "NDC")
    td.SetTextAlign(13)
    td.SetBorderSize(0)
    td.SetFillColor(0)
    td.SetTextSize(0.04)

    td.AddText('#sqrt{s} = 8 TeV')
    td.AddText('#int Ldt = 20.3 fb^{-1}')
    td.Draw()
    # channel
    tm = ROOT.TPaveText(0.19, 0.83, 0.22, 0.90, "NDC")
    tm.SetBorderSize(0)
    tm.SetFillColor(0)
    tm.SetTextSize(0.05)
    if mu.name == 'mu':
        tm.AddText('#mu#tau_{had}')
    elif mu.name == 'el':
        tm.AddText('e#tau_{had}')
    tm.Draw()

    c.Update()
    c.Print('../plots/ttbar/%s.eps' % (cname))