示例#1
0
def main():
    if len(sys.argv) > 1:
        fstate = sys.argv[1]
    else:
        fstate = ''
    decor = fstate
    if decor != '':
        decor += '_'

    sources = [('%s/fig_%sbkg.root' % (PATH, decor), 'bkg', kMagenta + 1),
               ('%s/fig_%shiggs.root' % (PATH, decor), 'higgs', kAzure + 1)]
    nsources = 2
    fixedSig = False
    fixedSigValue = 2.4

    # ---------------------------------------
    # get data histogram
    # ---------------------------------------
    filename = '%s/fig_%sdata.root' % (PATH, decor)
    fdata = TFile(filename)
    if not fdata.IsOpen():
        sys.exit("** can't open file %s" % filename)
    hdata = fdata.Get(HNAME)
    if not hdata: sys.exit("** can't find hbnn")

    # ---------------------------------------
    # get source histograms
    # ---------------------------------------
    f = []
    h = []
    for i, (filename, hname, color) in enumerate(sources):
        f.append(TFile(filename))
        if not f[-1].IsOpen():
            sys.exit("** can't open file %s" % f[-1])
        h.append(f[-1].Get(HNAME).Clone(hname))
        if not h[-1]: sys.exit("** can't find hbnn")

    if FAKEDATA: fakeData(hdata, h, 0.1065)
    print '\tcount(%s):\t%8.1f' % ('data', hdata.Integral())

    # ---------------------------------------
    # set up some standard graphics style
    # ---------------------------------------
    tdrstyle.setTDRStyle()
    #gStyle.SetCanvasPreferGL(True)
    gStyle.SetPadRightMargin(0.12)
    #gStyle.SetOptStat('ei')
    gStyle.SetOptStat('i')
    gStyle.SetStatFont(42)
    gStyle.SetStatFontSize(0.03)
    gStyle.SetStatBorderSize(1)
    gStyle.SetStatH(0.2)
    gStyle.SetStatW(0.3)
    gStyle.SetStatX(0.83)
    gStyle.SetStatY(0.93)

    # change the CMS_lumi variables (see CMS_lumi.py)
    iPeriod = 4
    iPos = 0
    CMS_lumi.relPosX = 0.12
    CMS_lumi.lumi_13TeV = "36.0 fb^{-1}"
    CMS_lumi.writeExtraText = 1
    CMS_lumi.extraText = "Preliminary"
    vdouble = vector("double")

    # ---------------------------------------
    # plot data
    # ---------------------------------------
    ymax = 1.2 * hdata.GetMaximum()
    ii = int(ymax / 20)
    ymax = (ii + 1) * 20

    hdata.SetMaximum(ymax)
    hdata.SetNdivisions(505, 'X')

    xbins = int(hdata.GetNbinsX())
    width = hdata.GetXaxis().GetBinWidth(1)
    xmin = int(hdata.GetXaxis().GetBinLowEdge(1))
    xmax = int(hdata.GetXaxis().GetBinLowEdge(xbins) + width)

    postfix = "_%s_%3.3d_%3.3d" % (HNAME, xmin, xmax)

    cname = "fig_%sdata_%s" % (decor, postfix)
    canvas = TCanvas(cname, cname, 10, 10, 500, 500)
    canvas.cd()
    hdata.Draw("ep gl")
    canvas.Update()
    canvas.SaveAs('.png')
    gSystem.ProcessEvents()
    sleep(0.5)
    del canvas

    # ---------------------------------------
    # plot sources
    # ---------------------------------------
    for i, (filename, hname, color) in enumerate(sources):
        cname = "fig_%s%s%s" % (decor, hname, postfix)
        canvas = TCanvas(cname, cname, 10, 10, 500, 500)
        canvas.cd()
        h[i].SetNdivisions(505, 'X')
        h[i].Draw("hist gl")
        canvas.Update()
        canvas.SaveAs('.png')
        gSystem.ProcessEvents()
        sleep(0.5)
        del canvas

    # get counts
    nbin = h[0].GetNbinsX()
    datum = 0.0
    bkg = 0.0
    sig = 0.0

    fb = []
    fs = []
    D = []
    for ii in xrange(nbin):
        bkg += h[0].GetBinContent(ii + 1)
        sig += h[1].GetBinContent(ii + 1)
        datum += hdata.GetBinContent(ii + 1)
        theory = bkg + sig
        if theory > 0:
            fb.append(bkg)
            fs.append(sig)
            D.append(datum)

            print '%4d\t%10.2f\t%10.2f\t%10.0f' % (len(D), fb[-1], fs[-1],
                                                   D[-1])
            bkg = 0.0
            sig = 0.0
            datum = 0.0
            theory = bkg + sig

    theory = bkg + sig
    if theory > 0:
        fb[-1] += bkg
        fs[-1] += sig
        D[-1] += datum
        print '%4d\t%10.2f\t%10.2f\t%10.0f' % (len(D), fb[-1], fs[-1], D[-1])
    total = sum(D)

    # ---------------------------------------
    # fit
    # ---------------------------------------
    t = []
    bguess = sum(fb)
    t.append(bguess)
    fb = map(lambda x: x / bguess, fb)

    sguess = sum(fs)
    t.append(sguess)
    fs = map(lambda x: x / sguess, fs)

    if fixedSig: sguess = fixedSigValue

    # ---------------------------------------
    def Func(npar, grad, fval, xval, flag):
        fval[0] = 0.0
        B = xval[0]
        S = xval[1]
        for i in xrange(len(D)):
            theory = B * fb[i] + S * fs[i]
            x = D[i] - theory
            fval[0] += x * x / theory

    # ---------------------------------------

    minuit = TMinuit(len(sources))
    minuit.SetPrintLevel(1)
    minuit.SetFCN(Func)
    minuit.SetErrorDef(1)

    status = Long()

    bstep = bguess / 1000
    bmin = 0.0
    bmax = 2 * total
    minuit.mnparm(0, "B", bguess, bstep, bmin, bmax, status)
    if status != 0:
        sys.exit("** mnparam status code: %d" % status)

    if fixedSig: sguess = fixedSigValue
    sstep = sguess / 1000
    smin = 0.0
    smax = 2 * total
    minuit.mnparm(1, "S", sguess, sstep, smin, smax, status)
    if status != 0:
        sys.exit("** mnparam status code: %d" % status)

    if fixedSig: minuit.FixParameter(1)

    args = array('d')
    args.append(MAXITER)
    args.append(TOLERANCE)
    minuit.mnexcm(METHOD, args, 2, status)
    if status != 0:
        sys.exit("** mnexcm %s failed" % METHOD)

    # Print results
    print "-" * 80
    print "total observed count: %d" % total
    print "%-10s\t%10s" % ('source', "    estimated count")
    x1 = 0.0
    x2 = 0.0
    mode = [0] * len(sources)
    error = [0] * len(sources)
    for index, (fname, src, color) in enumerate(sources):
        value = Double()
        dvalue = Double()
        minuit.GetParameter(index, value, dvalue)
        mode[index] = value
        error[index] = dvalue
        print "%-10s\t%10.1f +/-%- 10.1f" % \
              (src, mode[index], error[index])
        x1 += mode[index]
        x2 += error[index]**2
    x2 = sqrt(x2)
    print '-' * 35
    print "%-10s\t%10.1f +/-%- 10.1f" % ('total', x1, x2)

    for index, (fname, src, color) in enumerate(sources):
        h[index].SetLineColor(color + 2)
        h[index].SetFillColor(color)
        h[index].Scale(mode[index] / h[index].Integral())
        h[index].SetMaximum(ymax)

    hdata.SetMinimum(1.e-3)
    hdata.SetMaximum(ymax)

    # compute GOF measure
    chisq = 0.0
    B = mode[0]
    S = mode[1]
    print B, S
    for ii in xrange(len(D)):
        bkg = B * fb[ii]
        sig = S * fs[ii]
        print '%4d\t%10.2f\t%10.2f\t%10.0f' % (ii + 1, bkg, sig, D[ii])
        theory = bkg + sig
        x = D[ii] - theory
        chisq += x * x / theory
    ndf = len(D) - len(sources)
    print '\nchisq/NDF = %10.1f/%d = %10.2f\n' % (chisq, ndf, chisq / ndf)

    ht = h[0].Clone('theory')
    ht.Add(h[1])
    ht.SetMaximum(ymax)

    hs = THStack('hs', '')
    hs.Add(h[0])
    hs.Add(h[1])
    hs.SetMaximum(ymax)

    lg = mklegend(0.35, 0.72, 0.25, 0.2)
    lg.SetHeader('events(%s)' % FINALSTATES[fstate])

    lg.SetTextSize(0.04)
    lg.AddEntry(hdata, 'data  %4.0f' % hdata.Integral(), 'p')
    for ii, (fname, src, color) in enumerate(sources):
        src = capitalize(src)
        lg.AddEntry(h[ii],  '%-5s   %6.1f #pm %-6.1f (#color[4]{%6.1f})' \
                        % (src, mode[ii], error[ii], t[ii]), 'f')

    scribe = Scribe(0.43, 0.68)

    cname = "fig_%sresult%s" % (decor, postfix)
    cfit = TCanvas(cname, cname, 520, 10, 500, 575)
    cfit.cd()

    pad1 = TPad("pad1", "pad1", 0, 0.3, 1.0, 1.0)
    pad1.SetBottomMargin(0)
    pad1.Draw()
    pad1.cd()

    hdata.SetStats(0)
    hdata.SetMaximum(ymax)
    hdata.Draw('ep')
    hs.Draw('histsame')
    hdata.Draw('epsame')

    lg.Draw()
    scribe.write('#chi^{2}/NDF = %5.1f / %d = %5.2f\n' % \
                     (chisq, ndf, chisq/ndf))

    xbins = int(h[0].GetNbinsX())
    width = h[0].GetXaxis().GetBinWidth(1)
    xmin = int(h[0].GetXaxis().GetBinLowEdge(1))
    xmax = int(h[0].GetXaxis().GetBinLowEdge(xbins) + width)
    scribe.write('#font[12]{m}_{4#font[12]{l}} #in [%d, %d] GeV' %
                 (xmin, xmax))

    CMS_lumi.CMS_lumi(cfit, iPeriod, iPos)
    cfit.SaveAs('.png')
    cfit.Update()
    gSystem.ProcessEvents()

    # plot lower pad (within current canvas)
    cfit.cd()
    pad2 = TPad("pad2", "pad2", 0, 0.0, 1, 0.3)
    pad2.SetTopMargin(0.05)
    pad2.SetBottomMargin(0.28)
    pad2.SetGridy()
    pad2.Draw()

    pad2.cd()
    hdata.SetStats(0)
    hdata.GetXaxis().SetLabelSize(0.12)
    hdata.GetXaxis().SetTitleSize(0.12)

    hdata.GetYaxis().SetLabelSize(0.12)
    hdata.GetYaxis().SetTitleSize(0.12)
    hdata.SetNdivisions(505, 'Y')

    hratio = hdata.Clone("ratio")
    hratio.Divide(ht)
    hratio.SetMinimum(0)
    hratio.SetMaximum(3)
    hratio.Draw('ep')
    x = array('d')
    x.append(xmin)
    x.append(xmax)
    y = array('d')
    y.append(1)
    y.append(1)
    hline = TGraph(2, x, y)
    hline.SetLineColor(kRed + 1)
    hline.SetLineWidth(3)
    hline.Draw('lsame')
    cfit.Update()
    gSystem.ProcessEvents()
    cfit.SaveAs('.png')

    sleep(5)
示例#2
0
def main():

    # Load PoissonGammaFit class
    gSystem.AddDynamicPath('%s/lib' % os.environ['PGAMMAFIT_PATH'])
    gSystem.Load("libpgammafit")

    if len(sys.argv) > 1:
        fstate = sys.argv[1]
    else:
        fstate = '_all'

    # ---------------------------------------
    # set up some standard graphics style
    # ---------------------------------------
    tdrstyle.setTDRStyle()
    #gStyle.SetCanvasPreferGL(True)
    gStyle.SetPadRightMargin(0.12)
    #gStyle.SetOptStat('ei')
    gStyle.SetOptStat('i')
    gStyle.SetStatFont(42)
    gStyle.SetStatFontSize(0.03)
    gStyle.SetStatBorderSize(1)
    gStyle.SetStatH(0.2)
    gStyle.SetStatW(0.3)
    gStyle.SetStatX(0.83)
    gStyle.SetStatY(0.93)

    # change the CMS_lumi variables (see CMS_lumi.py)
    iPeriod = 4
    iPos = 0
    CMS_lumi.relPosX = 0.12
    CMS_lumi.lumi_13TeV = "36.0 fb^{-1}"
    CMS_lumi.writeExtraText = 1
    CMS_lumi.extraText = "Preliminary"
    vdouble = vector("double")

    # Get distribution of sources and unfold into 1-d vectors, A[j]
    f = []
    h = []
    A = []  # vector<vector<double> > # counts and uncertainties
    t = []  # totals
    for i,(filename,hname,c) in \
      enumerate([('../fig_bkg.root', 'bkg', 225.60),
                 ('../fig_higgs.root', 'higgs',27.0)]):
        f.append(TFile(filename))
        h.append(f[-1].Get('hm4l').Clone(hname))
        if not h[-1]: sys.exit("** can't find hbnn")
        h[-1].SetNdivisions(505, 'X')
        t.append(c)

        # plot
        canvas = TCanvas("fig%s_%s" % (fstate, hname), hname, 10, 10, 500, 500)
        canvas.cd()
        h[-1].Draw("hist gl")
        canvas.Update()
        canvas.SaveAs('.png')
        gSystem.ProcessEvents()
        sleep(3)
        del canvas

        # get counts and associated uncertainties and normalize
        A.append(getContents(h[-1]))

    # get data

    # Get histogram of observed counts
    filename = '../fig_data.root'
    f.append(TFile(filename))
    hdata = f[-1].Get('hm4l')
    if not hdata: sys.exit("** can't find hbnn")
    ymax = 60
    hdata.SetMaximum(ymax)
    hdata.SetNdivisions(505, 'X')

    canvas = TCanvas("fig%s_data" % fstate, "bnn", 10, 10, 500, 500)
    canvas.cd()
    hdata.Draw("ep gl")
    canvas.Update()
    canvas.SaveAs('.png')
    gSystem.ProcessEvents()
    sleep(3)
    del canvas

    # Construct a PoissonGamma object
    D, dD = getContents(hdata)
    pgfit = PoissonGammaFit(D)

    # Add sources
    for a, da in A:
        pgfit.add(a, da)

    # Find mode of posterior density
    # make some reasonable guesses for fit
    total = sum(D)
    guess = vdouble(2, total / 3)
    pgfit.execute(guess)

    if not pgfit.good():
        sys.exit('''
        ** :( boo hoo - fit failed!
        ** check histograms. If ok, try better starting guesses for
        ** the source counts in the signal region and/or try rebinning.
        ''')

    # Get mode and width of posterior density, which, because PoissonGammaFit
    # uses a flat prior for the yields, are the same as those of the
    # Poisson/gamma marginal density.

    mode = pgfit.mode()
    error = pgfit.width()
    logevidence = pgfit.logEvidence()

    # Print results
    print "-" * 80
    print "total observed count: %d" % total
    print "%-10s\t%10s" % ('source', "    estimated count")
    x1 = 0.0
    x2 = 0.0
    for index, src in enumerate(['bkg', 'higgs']):
        print "%-10s\t%10.1f +/-%- 10.1f" % \
              (src, mode[index], error[index])
        x1 += mode[index]
        x2 += error[index]**2
    x2 = sqrt(x2)
    print '-' * 35
    print "%-10s\t%10.1f +/-%- 10.1f" % ('total', x1, x2)

    h[0].SetLineColor(kMagenta + 1)
    h[0].SetFillColor(kMagenta + 1)
    h[0].Scale(mode[0] / h[0].Integral())
    h[0].SetMaximum(ymax)
    hdata.SetMaximum(ymax)

    h[1].SetLineColor(kCyan + 1)
    h[1].SetFillColor(kCyan + 1)
    h[1].Scale(mode[1] / h[1].Integral())

    # compute GOF measure
    nbin = h[0].GetNbinsX()
    chisq = 0.0
    datum = 0.0
    theory = 0.0
    variance = 0.0
    ndf = 0
    for ii in xrange(nbin):
        d = hdata.GetBinContent(ii + 1)
        e0 = h[0].GetBinError(ii + 1)
        c0 = h[0].GetBinContent(ii + 1)
        e0 = h[0].GetBinError(ii + 1)
        c1 = h[1].GetBinContent(ii + 1)
        e1 = h[1].GetBinError(ii + 1)

        datum += d
        theory += c0 + c1
        variance += c0 + c1 + e0**2 + e1**2
        if theory > 8:
            chisq += (theory - datum)**2 / variance
            ndf += 1
            print '%4d\t%10.1f\t%10.0f' % (ndf, theory, datum)
            datum = 0.0
            theory = 0.0
            variance = 0.0
    ndf -= 2
    print '\nchisq/NDF = %10.1f/%d = %10.2f\n' % (chisq, ndf, chisq / ndf)

    hs = THStack('hs', '')
    hs.SetMaximum(ymax)
    hs.Add(h[0])
    hs.Add(h[1])

    lg = mklegend(0.25, 0.65, 0.25, 0.2)
    lg.SetHeader('events 4#mu + 2e2#mu, '\
                     '#font[12]{m}_{4#font[12]{l}} #in [70,170] GeV')
    lg.SetTextSize(0.04)
    lg.AddEntry(hdata, 'data  %4.0f' % hdata.Integral(), 'p')
    lg.AddEntry(h[0],  'bkg   %6.1f #pm %-6.1f (#color[4]{%6.1f})' \
                    % (mode[0], error[0], t[0]), 'l')
    lg.AddEntry(h[1],  'Higgs %6.1f #pm %-6.1f (#color[4]{%6.1f})'  \
                    % (mode[1], error[1], t[1]), 'l')

    scribe = Scribe(0.31, 0.60)

    cfit = TCanvas("fig%s_result" % fstate, "", 520, 10, 500, 500)
    cfit.cd()
    gStyle.SetOptStat('')
    hdata.SetMaximum(ymax)
    hdata.Draw('ep')
    hs.Draw('histsame')
    hdata.Draw('epsame')
    lg.Draw()
    scribe.write('#chi^{2}/NDF = %5.1f/%d = %5.2f\n' % \
                     (chisq, ndf, chisq/ndf))
    CMS_lumi.CMS_lumi(cfit, iPeriod, iPos)
    cfit.SaveAs('.png')
    cfit.Update()
    gSystem.ProcessEvents()

    sleep(5)
示例#3
0
def main():
    if len(sys.argv) > 1:
        fstate = sys.argv[1]
    else:
        fstate = ''
    decor = fstate
    if decor != '':
        decor += '_'

    sources = ('%s/fig_%sbkg.root' % (PATH, decor), 'bkg', kMagenta + 1)

    # ---------------------------------------
    # get data histogram
    # ---------------------------------------
    filename = '%s/fig_%sdata.root' % (PATH, decor)
    fdata = TFile(filename)
    if not fdata.IsOpen():
        sys.exit("** can't open file %s" % filename)
    hdata = fdata.Get(HNAME)
    if not hdata: sys.exit("** can't find hbnn")

    # ---------------------------------------
    # get source histogram
    # ---------------------------------------
    filename, hname, color = sources
    fsrc = TFile(filename)
    if not fsrc.IsOpen():
        sys.exit("** can't open file %s" % fsrc)
    hsrc = fsrc.Get(HNAME).Clone(hname)
    if not hsrc: sys.exit("** can't find histogram")

    # ---------------------------------------
    # set up some standard graphics style
    # ---------------------------------------
    tdrstyle.setTDRStyle()
    #gStyle.SetCanvasPreferGL(True)
    gStyle.SetPadRightMargin(0.12)
    #gStyle.SetOptStat('ei')
    gStyle.SetOptStat('i')
    gStyle.SetStatFont(42)
    gStyle.SetStatFontSize(0.03)
    gStyle.SetStatBorderSize(1)
    gStyle.SetStatH(0.2)
    gStyle.SetStatW(0.3)
    gStyle.SetStatX(0.83)
    gStyle.SetStatY(0.93)

    # change the CMS_lumi variables (see CMS_lumi.py)
    iPeriod = 4
    iPos = 0
    CMS_lumi.relPosX = 0.12
    CMS_lumi.lumi_13TeV = "36.0 fb^{-1}"
    CMS_lumi.writeExtraText = 1
    CMS_lumi.extraText = "Preliminary"
    vdouble = vector("double")

    # ---------------------------------------
    # plot data
    # ---------------------------------------
    ymax = 1.2 * hdata.GetMaximum()
    ii = int(ymax / 10)
    ymax = (ii + 1) * 10

    hdata.SetMaximum(ymax)
    hdata.SetNdivisions(505, 'X')

    xbins = int(hdata.GetNbinsX())
    width = hdata.GetXaxis().GetBinWidth(1)
    xmin = int(hdata.GetXaxis().GetBinLowEdge(1))
    xmax = int(hdata.GetXaxis().GetBinLowEdge(xbins) + width)

    postfix = "_%s_%3.3d_%3.3d" % (HNAME, xmin, xmax)

    cname = "fig_%sdata_%s" % (decor, postfix)
    canvas = TCanvas(cname, cname, 10, 10, 500, 500)
    canvas.cd()
    hdata.Draw("ep gl")
    canvas.Update()
    canvas.SaveAs('.png')
    gSystem.ProcessEvents()
    sleep(1)
    del canvas

    # ---------------------------------------
    # plot source
    # ---------------------------------------
    cname = "fig_%s%s%s" % (decor, hname, postfix)
    canvas = TCanvas(cname, cname, 10, 10, 500, 500)
    canvas.cd()
    hsrc.SetNdivisions(505, 'X')
    hsrc.Draw("hist gl")
    canvas.Update()
    canvas.SaveAs('.png')
    gSystem.ProcessEvents()
    sleep(1)
    del canvas

    # get counts
    datum = 0.0
    bkg = 0.0

    fb = []
    D = []
    for ii in xrange(xbins):
        bkg += hsrc.GetBinContent(ii + 1)
        datum += hdata.GetBinContent(ii + 1)

        if bkg > 10:
            fb.append(bkg)
            D.append(datum)

            print '%4d\t%10.2f\t%10.0f' % (len(D), bkg, datum)
            bkg = 0.0
            datum = 0.0

    # ---------------------------------------
    # fit
    # ---------------------------------------
    t = []
    bguess = sum(fb)
    fb = map(lambda x: x / bguess, fb)

    def Func(npar, grad, fval, xval, flag):
        fval[0] = 0.0
        B = xval[0]
        for i in xrange(len(D)):
            theory = B * fb[i]
            x = D[i] - theory
            fval[0] += x * x / theory

    minuit = TMinuit(1)
    minuit.SetPrintLevel(1)
    minuit.SetFCN(Func)
    minuit.SetErrorDef(1)

    # Find mode of posterior density
    # make some reasonable guesses for fit
    total = sum(D)
    status = Long()

    bstep = bguess / 1000
    bmin = 0.0
    bmax = 2 * total
    minuit.mnparm(0, "B", bguess, bstep, bmin, bmax, status)
    if status != 0:
        sys.exit("** mnparam status code: %d" % status)

    args = array('d')
    args.append(MAXITER)
    args.append(TOLERANCE)
    minuit.mnexcm(METHOD, args, 2, status)
    if status != 0:
        sys.exit("** mnexcm %s failed" % METHOD)

    # Print results
    print "-" * 80
    print "total observed count: %d" % total
    print "%-10s\t%10s" % ('source', "    estimated count")
    value = Double()
    dvalue = Double()
    minuit.GetParameter(0, value, dvalue)
    mode = value
    error = dvalue
    print "%-10s\t%10.1f +/-%- 10.1f" % \
      ("", mode, error)

    color = sources[-1]
    hsrc.SetLineColor(color + 2)
    hsrc.SetFillColor(color)
    hsrc.Scale(mode / hsrc.Integral())
    hsrc.SetMaximum(ymax)

    # compute GOF measure
    chisq = 0.0
    for i in xrange(len(D)):
        theory = mode * fb[i]
        x = D[i] - theory
        chisq += x * x / theory
    ndf = len(D) - 1

    print '\nchisq/NDF = %10.1f/%d = %10.2f\n' % (chisq, ndf, chisq / ndf)

    lg = mklegend(0.35, 0.72, 0.25, 0.2)
    lg.SetHeader('events(%s)' % FINALSTATES[fstate])

    lg.SetTextSize(0.04)
    lg.AddEntry(hdata, 'data  %4.0f' % hdata.Integral(), 'p')
    fname, src, color = sources
    src = capitalize(src)
    lg.AddEntry(hsrc,  '%-5s   %6.1f #pm %-6.1f (#color[4]{%6.1f})' \
                    % (src, mode, error, bguess), 'f')

    scribe = Scribe(0.43, 0.68)

    cname = "fig_%sresult%s" % (decor, postfix)
    cfit = TCanvas(cname, cname, 520, 10, 500, 500)
    cfit.cd()
    gStyle.SetOptStat('')

    hdata.SetMaximum(ymax)
    hdata.Draw('ep')
    hsrc.Draw('histsame')
    hdata.Draw('epsame')

    lg.Draw()
    scribe.write('#chi^{2}/NDF = %5.1f / %d = %5.2f\n' % \
                     (chisq, ndf, chisq/ndf))
    scribe.write('#font[12]{m}_{4#font[12]{l}} #in [%d, %d] GeV' %
                 (xmin, xmax))

    CMS_lumi.CMS_lumi(cfit, iPeriod, iPos)
    cfit.SaveAs('.png')
    cfit.Update()
    gSystem.ProcessEvents()

    sleep(5)
示例#4
0
def main():

    # Load PoissonGammaFit class
    gSystem.AddDynamicPath('%s/lib' % os.environ['PGAMMAFIT_PATH'])
    gSystem.Load("libpgammafit")

    if len(sys.argv) > 1:
        fstate = sys.argv[1]
    else:
        fstate = ''
    decor = fstate
    if decor != '':
        decor += '_'

    sources = [('%s/fig_%sbkg.root' % (PATH, decor), 'bkg', kMagenta + 1),
               ('%s/fig_%shiggs.root' % (PATH, decor), 'higgs', kAzure + 1)]

    # get data histogram
    filename = '%s/fig_%sdata.root' % (PATH, decor)
    fdata = TFile(filename)
    if not fdata.IsOpen():
        sys.exit("** can't open file %s" % filename)
    hdata = fdata.Get(HNAME)
    if not hdata: sys.exit("** can't find hbnn")

    # get source histograms
    f = []
    h = []
    for i, (filename, hname, color) in enumerate(sources):
        f.append(TFile(filename))
        if not f[-1].IsOpen():
            sys.exit("** can't open file %s" % f[-1])
        h.append(f[-1].Get(HNAME).Clone(hname))
        if not h[-1]: sys.exit("** can't find hbnn")

    if FAKEDATA: fakeData(hdata, h, 0.1065)
    print '\tcount(%s):\t%8.1f' % ('data', hdata.Integral())

    # get counts
    A = [0] * len(h)  # vector<vector<double> > # counts and uncertainties
    t = [0] * len(h)  # totals
    for i, (filename, hname, color) in enumerate(sources):
        A[i] = getContents(h[i])
        t[i] = h[i].Integral()
        print '\tcount(%s):\t%8.1f' % (hname, t[i])

    # ---------------------------------------
    # set up some standard graphics style
    # ---------------------------------------
    tdrstyle.setTDRStyle()
    #gStyle.SetCanvasPreferGL(True)
    gStyle.SetPadRightMargin(0.12)
    #gStyle.SetOptStat('ei')
    gStyle.SetOptStat('i')
    gStyle.SetStatFont(42)
    gStyle.SetStatFontSize(0.03)
    gStyle.SetStatBorderSize(1)
    gStyle.SetStatH(0.2)
    gStyle.SetStatW(0.3)
    gStyle.SetStatX(0.83)
    gStyle.SetStatY(0.93)

    # change the CMS_lumi variables (see CMS_lumi.py)
    iPeriod = 4
    iPos = 0
    CMS_lumi.relPosX = 0.12
    CMS_lumi.lumi_13TeV = "36.0 fb^{-1}"
    CMS_lumi.writeExtraText = 1
    CMS_lumi.extraText = "Preliminary"
    vdouble = vector("double")

    # plot data

    ymax = 1.2 * hdata.GetMaximum()
    ii = int(ymax / 10)
    ymax = (ii + 1) * 10

    hdata.SetMaximum(ymax)
    hdata.SetNdivisions(505, 'X')

    xbins = int(hdata.GetNbinsX())
    width = hdata.GetXaxis().GetBinWidth(1)
    xmin = int(hdata.GetXaxis().GetBinLowEdge(1))
    xmax = int(hdata.GetXaxis().GetBinLowEdge(xbins) + width)

    postfix = "_%s_%3.3d_%3.3d" % (HNAME, xmin, xmax)

    cname = "fig_%sdata_%s" % (decor, postfix)
    canvas = TCanvas(cname, cname, 10, 10, 500, 500)
    canvas.cd()
    hdata.Draw("ep gl")
    canvas.Update()
    canvas.SaveAs('.png')
    gSystem.ProcessEvents()
    sleep(3)
    del canvas

    for i, (filename, hname, color) in enumerate(sources):
        cname = "fig_%s%s%s" % (decor, hname, postfix)
        canvas = TCanvas(cname, cname, 10, 10, 500, 500)
        canvas.cd()
        h[i].SetNdivisions(505, 'X')
        h[i].Draw("hist gl")
        canvas.Update()
        canvas.SaveAs('.png')
        gSystem.ProcessEvents()
        sleep(3)
        del canvas

    # ---------------------------------------
    # Construct a PoissonGamma object
    # ---------------------------------------
    D, dD = getContents(hdata)
    pgfit = PoissonGammaFit(D)

    # Add sources
    for a, da in A:
        pgfit.add(a, da)

    # Find mode of posterior density
    # make some reasonable guesses for fit
    total = sum(D)
    guess = vdouble(len(sources), total / len(sources))
    pgfit.execute(guess)

    if not pgfit.good():
        sys.exit('''
        ** :( boo hoo - fit failed!
        ** check histograms. If ok, try better starting guesses for
        ** the source counts in the signal region and/or try rebinning.
        ''')

    # Get mode and width of posterior density, which, because PoissonGammaFit
    # uses a flat prior for the yields, are the same as those of the
    # Poisson/gamma marginal density.

    mode = pgfit.mode()
    error = pgfit.width()
    logevidence = pgfit.logEvidence()

    # Print results
    print "-" * 80
    print "total observed count: %d" % total
    print "%-10s\t%10s" % ('source', "    estimated count")
    x1 = 0.0
    x2 = 0.0
    for index, (fname, src, color) in enumerate(sources):
        print "%-10s\t%10.1f +/-%- 10.1f" % \
              (src, mode[index], error[index])
        x1 += mode[index]
        x2 += error[index]**2
    x2 = sqrt(x2)
    print '-' * 35
    print "%-10s\t%10.1f +/-%- 10.1f" % ('total', x1, x2)

    for index, (fname, src, color) in enumerate(sources):
        h[index].SetLineColor(color + 2)
        h[index].SetFillColor(color)
        h[index].Scale(mode[index] / h[index].Integral())
        h[index].SetMaximum(ymax)
    hdata.SetMaximum(ymax)

    # compute GOF measure
    nbin = h[0].GetNbinsX()
    chisq = 0.0
    datum = 0.0
    theory = 0.0
    variance = 0.0
    ndf = 0
    for ii in xrange(nbin):
        d = hdata.GetBinContent(ii + 1)
        e0 = h[0].GetBinError(ii + 1)
        c0 = h[0].GetBinContent(ii + 1)
        e0 = h[0].GetBinError(ii + 1)
        c1 = h[1].GetBinContent(ii + 1)
        e1 = h[1].GetBinError(ii + 1)

        datum += d
        theory += c0 + c1
        variance += c0 + c1 + e0**2 + e1**2
        if datum > 8:
            chisq += (theory - datum)**2 / variance
            ndf += 1
            print '%4d\t%10.1f\t%10.0f' % (ndf, theory, datum)
            datum = 0.0
            theory = 0.0
            variance = 0.0
    ndf -= 2
    print '\nchisq/NDF = %10.1f/%d = %10.2f\n' % (chisq, ndf, chisq / ndf)

    hs = THStack('hs', '')
    hs.Add(h[0])
    hs.Add(h[1])
    hs.SetMaximum(ymax)

    lg = mklegend(0.35, 0.72, 0.25, 0.2)
    lg.SetHeader('events(%s)' % FINALSTATES[fstate])

    lg.SetTextSize(0.04)
    lg.AddEntry(hdata, 'data  %4.0f' % hdata.Integral(), 'p')
    for ii, (fname, src, color) in enumerate(sources):
        src = capitalize(src)
        lg.AddEntry(h[ii],  '%-5s   %6.1f #pm %-6.1f (#color[4]{%6.1f})' \
                        % (src, mode[ii], error[ii], t[ii]), 'f')

    scribe = Scribe(0.43, 0.68)

    cname = "fig_%sresult%s" % (decor, postfix)
    cfit = TCanvas(cname, cname, 520, 10, 500, 500)
    cfit.cd()
    gStyle.SetOptStat('')

    hdata.SetMaximum(ymax)
    hdata.Draw('ep')
    hs.Draw('histsame')
    hdata.Draw('epsame')

    lg.Draw()
    scribe.write('#chi^{2}/NDF = %5.1f / %d = %5.2f\n' % \
                     (chisq, ndf, chisq/ndf))

    xbins = int(h[0].GetNbinsX())
    width = h[0].GetXaxis().GetBinWidth(1)
    xmin = int(h[0].GetXaxis().GetBinLowEdge(1))
    xmax = int(h[0].GetXaxis().GetBinLowEdge(xbins) + width)
    scribe.write('#font[12]{m}_{4#font[12]{l}} #in [%d, %d] GeV' %
                 (xmin, xmax))

    CMS_lumi.CMS_lumi(cfit, iPeriod, iPos)
    cfit.SaveAs('.png')
    cfit.Update()
    gSystem.ProcessEvents()

    sleep(5)
示例#5
0
def main():
    if len(sys.argv) > 1:
        fstate = sys.argv[1]
    else:
        fstate = ''
    decor = fstate
    if decor != '':
        decor += '_'
        
    sources = [('%s/fig_%sbkg.root'   % (PATH, decor), 'bkg',   kMagenta+1),
               ('%s/fig_%shiggs.root' % (PATH, decor), 'higgs', kAzure+1)]
    
    # ---------------------------------------        
    # get data histogram
    # ---------------------------------------    
    filename = '%s/fig_%sdata.root' % (PATH, decor)
    fdata = TFile(filename)
    if not fdata.IsOpen():
        sys.exit("** can't open file %s" % filename)        
    hdata = fdata.Get(HNAME)
    if not hdata: sys.exit("** can't find hbnn")
    
    # ---------------------------------------        
    # get source histograms
    # ---------------------------------------    
    f = []
    h = []
    for i,(filename,hname,color) in enumerate(sources):
        f.append(TFile(filename))
        if not f[-1].IsOpen():
            sys.exit("** can't open file %s" % f[-1])
        h.append( f[-1].Get(HNAME).Clone(hname) )
        if not h[-1]: sys.exit("** can't find hbnn")
        
    if FAKEDATA: fakeData(hdata, h, 0.1065)
    print '\tcount(%s):\t%8.1f' % ('data', hdata.Integral())
    
    # ---------------------------------------
    # set up some standard graphics style
    # ---------------------------------------
    tdrstyle.setTDRStyle()
    #gStyle.SetCanvasPreferGL(True)    
    gStyle.SetPadRightMargin(0.12)
    #gStyle.SetOptStat('ei')
    gStyle.SetOptStat('i')
    gStyle.SetStatFont(42)
    gStyle.SetStatFontSize(0.03)
    gStyle.SetStatBorderSize(1)
    gStyle.SetStatH(0.2)
    gStyle.SetStatW(0.3)
    gStyle.SetStatX(0.83)
    gStyle.SetStatY(0.93)    
    
    # change the CMS_lumi variables (see CMS_lumi.py)
    iPeriod = 4
    iPos    = 0
    CMS_lumi.relPosX = 0.12
    CMS_lumi.lumi_13TeV = "36.0 fb^{-1}"
    CMS_lumi.writeExtraText = 1
    CMS_lumi.extraText = "Preliminary"
    vdouble = vector("double")


    # ---------------------------------------    
    # plot data
    # ---------------------------------------
    ymax  = 1.2*hdata.GetMaximum()
    ii    = int(ymax / 20)
    ymax  = (ii+1)*20
                
    hdata.SetMaximum(ymax)
    hdata.SetNdivisions(505, 'X')

    xbins = int(hdata.GetNbinsX())
    width = hdata.GetXaxis().GetBinWidth(1)
    xmin  = int(hdata.GetXaxis().GetBinLowEdge(1))
    xmax  = int(hdata.GetXaxis().GetBinLowEdge(xbins)+width)

    postfix = "_%s_%3.3d_%3.3d" % (HNAME, xmin, xmax)
    
    cname = "fig_%sdata_%s" % (decor, postfix)
    canvas = TCanvas(cname, cname, 10, 10, 500, 500)
    canvas.cd()
    hdata.Draw("ep gl")
    canvas.Update()
    canvas.SaveAs('.png')
    gSystem.ProcessEvents()        
    sleep(0.5)
    del canvas

    # ---------------------------------------
    # plot sources
    # ---------------------------------------    
    for i,(filename,hname,color) in enumerate(sources):        
        cname = "fig_%s%s%s" % (decor, hname, postfix)
        canvas = TCanvas(cname, cname, 10, 10, 500, 500)        
        canvas.cd()
        h[i].SetNdivisions(505, 'X')
        h[i].Draw("hist gl")
        canvas.Update()
        canvas.SaveAs('.png')
        gSystem.ProcessEvents()
        sleep(0.5)
        del canvas

    # create TFractionFitter object
    hsrcs = TObjArray(len(sources))
    hsrcs.Add(h[0])
    hsrcs.Add(h[1])

    fitter = TFractionFitter(hdata, hsrcs)

    status = fitter.Fit()
    
    if status == 0:
        sys.exit("** fit failed: status")

    # Print results
    total = int(hdata.Integral())
    print "-"*80
    print "total observed count: %d" % total    
    print "%-10s\t%10s" %  ('source', "    estimated count")
    x1 = 0.0
    x2 = 0.0
    mode   = [0]*len(sources)
    error  = [0]*len(sources)
    for index, (fname, src, color) in enumerate(sources):
        value  = Double()
        dvalue = Double()            
        fitter.GetResult(index, value, dvalue)
        mode[index] = value * total
        error[index]= dvalue * total
        print "%-10s\t%10.1f +/-%- 10.1f" % \
              (src, mode[index], error[index])
        x1 += mode[index]
        x2 += error[index]**2
    x2 = sqrt(x2)
    print '-'*35
    print "%-10s\t%10.1f +/-%- 10.1f" % ('total', x1, x2)

    t = []
    for index, (fname, src, color) in enumerate(sources):    
        h[index].SetLineColor(color+2)
        h[index].SetFillColor(color)
        t.append(h[index].Integral())
        h[index].Scale(mode[index]/h[index].Integral())
        h[index].SetMaximum(ymax)
        
    hdata.SetMinimum(1.e-3)
    hdata.SetMaximum(ymax)
    
    # compute GOF measure
    chisq = fitter.GetChisquare()
    ndf   = fitter.GetNDF()
    p     = fitter.GetProb()
    print '\nchisq/NDF = %10.1f/%d = %10.2f\t%8.3f\n' % (chisq, ndf,
                                                             chisq/ndf, p)

    ht = h[0].Clone('theory')
    ht.Add(h[1])
    ht.SetMaximum(ymax)
    
    hs = THStack('hs', '')
    hs.Add(h[0])
    hs.Add(h[1])
    hs.SetMaximum(ymax)
    
    lg = mklegend(0.35, 0.72, 0.25, 0.2)
    lg.SetHeader('events(%s)' % FINALSTATES[fstate])
                     
    lg.SetTextSize(0.04)
    lg.AddEntry(hdata, 'data  %4.0f' % hdata.Integral(), 'p')
    for ii, (fname, src, color) in enumerate(sources):
        src = capitalize(src)
        lg.AddEntry(h[ii],  '%-5s   %6.1f #pm %-6.1f (#color[4]{%6.1f})' \
                        % (src, mode[ii], error[ii], t[ii]), 'f')

    scribe = Scribe(0.43, 0.68)

    cname = "fig_%sresult%s" % (decor, postfix)
    cfit = TCanvas(cname, cname, 520, 10, 500, 575)
    cfit.cd()


    pad1  = TPad("pad1", "pad1", 0, 0.3, 1.0, 1.0)
    pad1.SetBottomMargin(0)
    pad1.Draw()
    pad1.cd()

    hdata.SetStats(0)
    hdata.SetMaximum(ymax)
    hdata.Draw('ep')
    hs.Draw('histsame')
    hdata.Draw('epsame')

    lg.Draw()
    scribe.write('#chi^{2}/NDF = %5.1f / %d = %5.2f\n' % \
                     (chisq, ndf, chisq/ndf))

    xbins = int(h[0].GetNbinsX())
    width = h[0].GetXaxis().GetBinWidth(1)
    xmin  = int(h[0].GetXaxis().GetBinLowEdge(1))
    xmax  = int(h[0].GetXaxis().GetBinLowEdge(xbins)+width)
    scribe.write('#font[12]{m}_{4#font[12]{l}} #in [%d, %d] GeV' % (xmin,xmax))
    
    CMS_lumi.CMS_lumi(cfit, iPeriod, iPos)                
    cfit.SaveAs('.png')
    cfit.Update()
    gSystem.ProcessEvents()


    # plot lower pad (within current canvas)    
    cfit.cd()
    pad2  = TPad("pad2", "pad2", 0, 0.0, 1, 0.3)
    pad2.SetTopMargin(0.05)
    pad2.SetBottomMargin(0.28)
    pad2.SetGridy()
    pad2.Draw()

    pad2.cd()
    hdata.SetStats(0)
    hdata.GetXaxis().SetLabelSize(0.12)
    hdata.GetXaxis().SetTitleSize(0.12)

    hdata.GetYaxis().SetLabelSize(0.12)
    hdata.GetYaxis().SetTitleSize(0.12)    
    hdata.SetNdivisions(505, 'Y')
    
    hratio = hdata.Clone("ratio")
    hratio.Divide(ht)
    hratio.SetMinimum(0)
    hratio.SetMaximum(3)
    hratio.Draw('ep')
    x = array('d'); x.append(xmin); x.append(xmax)
    y = array('d'); y.append(1); y.append(1)
    hline = TGraph(2, x, y)
    hline.SetLineColor(kRed+1)
    hline.SetLineWidth(3)
    hline.Draw('lsame')
    cfit.Update()
    gSystem.ProcessEvents()
    cfit.SaveAs('.png')
    sleep(5)
示例#6
0
def main():

    # Load PoissonGammaFit class
    gSystem.AddDynamicPath('$PGAMMAFIT_PATH/lib')
    gSystem.Load("libpgammafit")

    if len(sys.argv) > 1:
        fstate = sys.argv[1]
    else:
        fstate = ''
    decor = fstate
    if decor != '':
        decor += '_'

    sources = [('%s/histos_%sbkg.root' % (PATH, decor), 'bkg', kMagenta + 1),
               ('%s/histos_%shiggs.root' % (PATH, decor), 'higgs', kAzure + 1)]

    # ---------------------------------------
    # get data histogram
    # ---------------------------------------
    filename = '%s/histos_%sdata.root' % (PATH, decor)
    fdata = TFile(filename)
    if not fdata.IsOpen():
        sys.exit("** can't open file %s" % filename)
    hdata = fdata.Get(HNAME)
    if not hdata: sys.exit("** can't find histogram")
    hdata.SetMarkerColor(kBlack)

    # ---------------------------------------
    # get source histograms
    # ---------------------------------------
    f = []
    h = []
    for i, (filename, hname, color) in enumerate(sources):
        f.append(TFile(filename))
        if not f[-1].IsOpen():
            sys.exit("** can't open file %s" % f[-1])
        h.append(f[-1].Get(HNAME).Clone(hname))
        if not h[-1]: sys.exit("** can't find %s" % HNAME)
        h[-1].SetFillColor(color)
        h[-1].SetFillStyle(3001)

    print '\tcount(%s):\t%8.1f' % ('data', hdata.Integral())

    # ---------------------------------------
    # set up some standard graphics style
    # ---------------------------------------
    tdrstyle.setTDRStyle()
    #gStyle.SetCanvasPreferGL(True)
    gStyle.SetPadRightMargin(0.12)
    #gStyle.SetOptStat('ei')
    gStyle.SetOptStat('i')
    gStyle.SetStatFont(42)
    gStyle.SetStatFontSize(0.03)
    gStyle.SetStatBorderSize(1)
    gStyle.SetStatH(0.2)
    gStyle.SetStatW(0.3)
    gStyle.SetStatX(0.83)
    gStyle.SetStatY(0.93)

    # change the CMS_lumi variables (see CMS_lumi.py)
    iPeriod = 4
    iPos = 0
    CMS_lumi.relPosX = 0.12
    CMS_lumi.lumi_13TeV = "36.0 fb^{-1}"
    CMS_lumi.writeExtraText = 1
    CMS_lumi.extraText = "Preliminary"
    vdouble = vector("double")

    # ---------------------------------------
    # plot data
    # ---------------------------------------
    ymax = 1.5 * hdata.GetMaximum()
    ii = int(ymax / 20)
    ymax = (ii + 1) * 20

    hdata.SetMaximum(ymax)
    hdata.SetNdivisions(505, 'X')

    xbins, xmin, xmax, width = getm4lrange(fdata)

    postfix = "_%s_%3.3d_%3.3d" % (HNAME, xmin, xmax)
    if len(sources) == 1: postfix += "_%s" % sources[0][1]

    cname = "fig_%sdata%s" % (decor, postfix)
    canvas = TCanvas(cname, cname, 10, 10, 500, 500)
    canvas.cd()
    hdata.Draw("ep gl")
    canvas.Update()
    canvas.SaveAs('.png')
    gSystem.ProcessEvents()
    sleep(0.5)
    del canvas

    # ---------------------------------------
    # plot sources
    # ---------------------------------------
    for i, (filename, hname, color) in enumerate(sources):
        cname = "fig_%s%s%s" % (decor, hname, postfix)
        canvas = TCanvas(cname, cname, 10, 10, 500, 500)
        canvas.cd()
        h[i].SetNdivisions(505, 'X')
        h[i].Draw("hist gl")
        canvas.Update()
        canvas.SaveAs('.png')
        gSystem.ProcessEvents()
        sleep(0.5)
        del canvas

    # get counts
    A = [0] * len(h)  # vector<vector<double> > # counts and uncertainties
    t = [0] * len(h)  # totals
    for i, (filename, hname, color) in enumerate(sources):
        A[i] = getContents(h[i])
        t[i] = h[i].Integral()
        print '\tcount(%s):\t%8.1f' % (hname, t[i])
    # ---------------------------------------
    # Construct a PoissonGamma object
    # ---------------------------------------
    D, dD = getContents(hdata)
    pgfit = PoissonGammaFit(D)

    # Add sources
    for a, da in A:
        pgfit.add(a, da)

    # Find mode of posterior density
    # make some reasonable guesses for fit
    guess = vdouble(len(sources))
    total = sum(D)
    aguess = total / len(sources)
    for i, (filename, hname, color) in enumerate(sources):
        guess[i] = aguess
    pgfit.execute(guess)

    if not pgfit.good():
        sys.exit('''
        ** :( boo hoo - fit failed!
        ** check histograms. If ok, try better starting guesses for
        ** the source counts in the signal region and/or try rebinning.
        ''')

    # Get mode and width of posterior density, which, because PoissonGammaFit
    # uses a flat prior for the yields, are the same as those of the
    # Poisson/gamma marginal density.

    mode = pgfit.mode()
    error = pgfit.width()
    logevidence = pgfit.logEvidence()

    result = "result_%sdata%s.txt" % (decor, postfix)
    rout = open(result, 'w')
    # Print results
    print "-" * 80
    record = "data\t\t     %d" % total
    print record
    rout.write('%s\n' % record)

    record = "%-10s\t%10s" % ('source', "    estimated count")
    print record
    rout.write('%s\n' % record)

    x1 = 0.0
    x2 = 0.0
    for index, (fname, src, color) in enumerate(sources):
        record = "%-10s\t%10.1f +/-%- 10.1f" % \
              (src, mode[index], error[index])
        print record
        rout.write('%s\n' % record)
        x1 += mode[index]
        x2 += error[index]**2
    x2 = sqrt(x2)
    record = '-' * 35
    print record
    rout.write('%s\n' % record)

    record = "%-10s\t%10.1f +/-%- 10.1f" % ('total', x1, x2)
    print record
    rout.write('%s\n' % record)

    for index, (fname, src, color) in enumerate(sources):
        h[index].SetLineColor(color + 2)
        h[index].SetFillColor(color)
        h[index].Scale(mode[index] / h[index].Integral())
        h[index].SetMaximum(ymax)

    hdata.SetMinimum(1.e-3)
    hdata.SetMaximum(ymax)

    # compute GOF measure

    chisq, ndf, KS, ht = GOF(hdata, h)
    print
    rout.write('\n')

    record = 'chisq/NDF = %4.1f/%d = %6.2f' % (chisq, ndf, chisq / ndf)
    print record
    rout.write('%s\n' % record)

    record = 'KS-prob   = %6.3f' % KS
    print record
    print
    rout.write('%s\n' % record)
    rout.close()

    ht.SetMaximum(ymax)

    hs = THStack('hs', '')
    hs.Add(h[0])
    if len(sources) > 1: hs.Add(h[1])
    hs.SetMaximum(ymax)

    lg = mklegend(0.35, 0.72, 0.25, 0.2)
    lg.SetHeader('events(%s) %s' % \
                     (FINALSTATES[fstate],
                    '#font[12]{m}_{4#font[12]{l}} #in [%d, %d] GeV' %\
                     (xmin,xmax)))

    lg.SetTextSize(0.04)
    lg.AddEntry(hdata, 'data  %4.0f' % hdata.Integral(), 'p')
    for ii, (fname, src, color) in enumerate(sources):
        src = capitalize(src)
        lg.AddEntry(h[ii],  '%-5s   %6.1f #pm %-6.1f (#color[4]{%6.1f})' \
                        % (src, mode[ii], error[ii], t[ii]), 'f')

    scribe = Scribe(0.43, 0.68)

    cname = "fig_%sresult%s" % (decor, postfix)
    cfit = TCanvas(cname, cname, 520, 10, 500, 575)
    cfit.cd()

    pad1 = TPad("pad1", "pad1", 0, 0.3, 1.0, 1.0)
    pad1.SetBottomMargin(0)
    pad1.Draw()
    pad1.cd()

    hdata.SetStats(0)
    hdata.SetMaximum(ymax)
    hdata.Draw('ep')

    lg.Draw()
    scribe.write('KS-prob = %6.3f' % KS)
    scribe.write('#chi^{2}/NDF = %5.1f / %d = %5.2f\n' % \
                     (chisq, ndf, chisq/ndf))

    hs.Draw('histsame')
    hdata.Draw('epsame')

    CMS_lumi.CMS_lumi(cfit, iPeriod, iPos)
    cfit.Update()
    gSystem.ProcessEvents()

    # plot lower pad (within current canvas)
    cfit.cd()
    pad2 = TPad("pad2", "pad2", 0, 0.0, 1, 0.3)
    pad2.SetTopMargin(0.05)
    pad2.SetBottomMargin(0.28)
    pad2.SetGridy()
    pad2.Draw()

    pad2.cd()
    hdata.SetStats(0)
    hdata.GetXaxis().SetLabelSize(0.12)
    hdata.GetXaxis().SetTitleSize(0.12)

    hdata.GetYaxis().SetLabelSize(0.12)
    hdata.GetYaxis().SetTitleSize(0.12)
    hdata.SetNdivisions(505, 'Y')

    hratio = hdata.Clone("ratio")
    hratio.Divide(ht)
    hratio.SetMinimum(0)
    hratio.SetMaximum(3)
    hratio.Draw('ep')
    drawLine(hratio)
    hratio.Draw('epsame')

    cfit.Update()
    gSystem.ProcessEvents()
    cfit.SaveAs('.png')

    sleep(5)