Example #1
0
def plotFit():

    axPeak = 2.464

    # load workspace
    f = TFile("./data/splitWorkspace.root")
    fitWorkspace = f.Get("fitWorkspace")
    fData = fitWorkspace.allData().front()
    fitResult = fitWorkspace.allGenericObjects().front()
    nPars = fitResult.floatParsFinal().getSize()
    fEnergy = fitWorkspace.var("energy_keV")
    modelPDF = fitWorkspace.pdf("model")
    # fitWorkspace.Print()

    pdfNames = ["ext-axion"]

    # -- create spectrum rooplot --
    nCol = float(gStyle.GetNumberOfColors())
    binSize = 0.04
    nBins = int((eHi - eLo) / binSize + 0.5)
    fSpec = fEnergy.frame(RF.Range(eLo, eHi), RF.Bins(nBins))
    fData.plotOn(fSpec)
    modelPDF.plotOn(fSpec, RF.LineColor(ROOT.kRed), RF.Name("FullModel"))
    chiSquare = fSpec.chiSquare(nPars)
    modelPDF.plotOn(fSpec, RF.Components(pdfNames[0]),
                    RF.LineColor(ROOT.kBlue), RF.Name(pdfNames[0]))

    # -- make a fake Gaussian --
    # gaus = ROOT.TF1("g","gaus",-3, eHi)
    # gaus.SetParameters(10., axPeak, getSigma(axPeak))
    # gaus.SetParameter(0,10.)
    # gaus.SetParameter(1, axPeak)
    # gaus.SetParameter(2, getSigma(axPeak))
    # erf
    # rrvGaus = ROOT.RooRealVar("ax","ax",-3.,3.)
    # rarGaus = RF.bindFunction("gaus", ROOT.TMath.Erf, rrvGaus)
    # rarGaus.Print()
    # frame2 = rrvGaus.frame(RF.Title("mygaus"))
    # rarGaus.plotOn(frame2, RF.LineColor(ROOT.kGreen), RF.LineStyle(ROOT.kDashed), RF.Name("axGaus"))
    # c0 = TCanvas("c0","",800,600)
    # frame2.Draw()
    # c0.Print("./plots/testGaus.pdf")

    # -- print fit results --
    print "-- SHIFTFIT RESULTS -- "
    print "%-10s = %.3f" % ("chiSq", chiSquare)
    fitValsFinal = getRooArgDict(fitResult.floatParsFinal())

    bkgVal = 0.
    for name in sorted(fitValsFinal):
        fitVal = fitValsFinal[name]
        if "amp-" in name:
            error = fitWorkspace.var(name).getError()
            print "%-10s = best %-7.3f  error %.3f (w/o profile)" % (
                name, fitVal, error)
        elif "mu-" in name:
            # compare the energy offset
            pkName = name[3:]
            pct = 100 * (1 - fitVal / axPeak)
            print "%-10s : fit %-6.3f  lit %-6.3f  (%.3f%%)" % (name, fitVal,
                                                                axPeak, pct)
        elif "sig-" in name:
            # compare the sigma difference
            pkName = name[4:]
            pct = 100 * (1 - fitVal / getSigma(axPeak))
            print "%-10s : fit %-6.3f  func %-6.3f  (%.3f%%)" % (
                name, fitVal, getSigma(axPeak), pct)
        else:
            print "%s = %.4f (%.4f)" % (name, fitVal, fitVal / nBins)
            bkgVal = fitVal / nBins
            continue

    # -- make spectrum plot --
    c = TCanvas("c", "Bob Ross's Canvas", 1400, 1000)
    c.SetRightMargin(0.2)
    fSpec.SetTitle(" ")
    fSpec.Draw()

    ymax = fSpec.GetMaximum()
    l1 = ROOT.TLine(axPeak, 0., axPeak, ymax)
    l1.SetLineColor(ROOT.kBlue)
    l1.SetLineWidth(2)
    l1.Draw("same")

    leg = TLegend(0.83, 0.6, 0.97, 0.9)
    leg.AddEntry(fSpec.findObject("FullModel"),
                 "model #chi^{2}=%.3f" % chiSquare, "l")
    leg.AddEntry(fSpec.findObject("FullModel"), "cts/bin=%.2f" % bkgVal, "")
    leg.AddEntry(fSpec.findObject(pdfNames[0]), "axion gaussian", "l")
    leg.AddEntry(l1, "axion-%.2f" % axPeak, "l")
    leg.Draw("same")

    c.Print("./plots/shiftFit.pdf")

    # -- get FC Limit --
    # Calculate the confidence interval for the axion peak, which is too low to use profile likelihood.
    # TFeldmanCousins version:
    # https://root.cern.ch/root/html/tutorials/math/FeldmanCousins.C.html
    # RooStats version:
    # https://root.cern.ch/root/html/tutorials/roostats/StandardFeldmanCousinsDemo.C.html
    # FC Paper: https://arxiv.org/pdf/physics/9711021.pdf
    f = TFeldmanCousins(0.95)
    N_obs = 1.
    N_bkg = bkgVal / 3.
    ul = f.CalculateUpperLimit(N_obs, N_bkg)
    ll = f.GetLowerLimit()
    print "For %.2f events observed, and %.2f background events," % (N_obs,
                                                                     N_bkg)
    print "F-C method gives UL: %.2f and LL %.2f (90%% CL)" % (ul, ll)