示例#1
0
def ts10(wspace=None,
         data=None,
         snapSb=None,
         snapB=None,
         snapfHat=None,
         verbose=False):
    wspace.loadSnapshot(snapSb)
    results = utils.rooFitResults(common.pdf(wspace), data)
    if verbose:
        print "S+B"
        print "---"
        results.Print("v")
    sbLl = -results.minNll()
    utils.delete(results)

    wspace.loadSnapshot(snapB)
    results = utils.rooFitResults(common.pdf(wspace), data)
    if verbose:
        print " B "
        print "---"
        results.Print("v")
    bLl = -results.minNll()
    utils.delete(results)

    out = -2.0 * (sbLl - bLl)
    if verbose: print "TS:", out
    return out
示例#2
0
def ntupleOfFitToys(wspace=None,
                    data=None,
                    nToys=None,
                    cutVar=("", ""),
                    cutFunc=None,
                    toyNumberMod=5):
    results = utils.rooFitResults(common.pdf(wspace), data)
    wspace.saveSnapshot("snap", wspace.allVars())

    obs = collect(wspace, results, extraStructure=True)

    toys = []
    for i, dataSet in enumerate(common.pseudoData(wspace, nToys)):
        if not (i % toyNumberMod): print "iToy = %d" % i
        wspace.loadSnapshot("snap")
        #dataSet.Print("v")
        results = utils.rooFitResults(common.pdf(wspace), dataSet)

        wspace.allVars().assignValueOnly(
            dataSet.get()
        )  #store this toy's observations, needed for (a) computing chi2 in collect(); (b) making "snapA"
        if all(cutVar) and cutFunc and cutFunc(
                getattr(wspace, cutVar[0])(cutVar[1]).getVal()):
            wspace.saveSnapshot("snapA", wspace.allVars())
            return obs, results, i

        toys.append(collect(wspace, results))
        utils.delete(results)
    return obs, toys
示例#3
0
def pValue(wspace, data, nToys = 100, note = "", plots = True) :
    def lMax(results) :
        return math.exp(-results.minNll())
    
    def indexFraction(item, l) :
        totalList = sorted(l+[item])
        assert totalList.count(item)==1
        return totalList.index(item)/(0.0+len(totalList))
        
    results = utils.rooFitResults(pdf(wspace), data) #fit to data
    #wspace.saveSnapshot("snap", wspace.allVars(), False)
    #results.Print()
    lMaxData = lMax(results)
    dataset = pdf(wspace).generate(wspace.set("obs"), nToys) #make pseudo experiments with final parameter values

    graph = r.TGraph()
    lMaxs = []
    for i in range(int(dataset.sumEntries())) :
        argSet = dataset.get(i)
        pseudoData = r.RooDataSet("pseudoData%d"%i, "title", argSet)
        data.reset()
        data.add(argSet)
        #data.Print("v")
        #wspace.loadSnapshot("snap")
        #wspace.var("A").setVal(initialA())
        #wspace.var("k").setVal(initialk())
        results = utils.rooFitResults(pdf(wspace), data)
        lMaxs.append(lMax(results))
        graph.SetPoint(i, i, indexFraction(lMaxData, lMaxs))
        #utils.delete(results)
    
    out = indexFraction(lMaxData, lMaxs)
    if plots : plotting.pValuePlots(pValue = out, lMaxData = lMaxData, lMaxs = lMaxs, graph = graph, note = note)
    return out
示例#4
0
文件: calc.py 项目: elaird/ra1stats
def expectedLimit(dataset, modelConfig, wspace, smOnly, cl, nToys, plusMinus, note = "", makePlots = False) :
    assert not smOnly

    #fit to SM-only
    wspace.var("f").setVal(0.0)
    wspace.var("f").setConstant(True)
    results = utils.rooFitResults(common.pdf(wspace), dataset)

    #generate toys
    toys = common.pseudoData(wspace, nToys)

    #restore signal model
    wspace.var("f").setVal(1.0)
    wspace.var("f").setConstant(False)

    #save snapshot
    snapName = "snap"
    wspace.saveSnapshot(snapName, wspace.allVars())

    #fit toys
    l = limits(wspace, snapName, modelConfig, smOnly, cl, toys)

    q,hist = quantiles(l, plusMinus, histoName = "upperLimit", histoTitle = ";upper limit on XS factor;toys / bin", histoBins = (50, 1, -1), cutZero = True) #enable auto-range
    nSuccesses = hist.GetEntries()

    obsLimit = limits(wspace, snapName, modelConfig, smOnly, cl, [dataset])[0]

    if makePlots : plotting.expectedLimitPlots(quantiles = q, hist = hist, obsLimit = obsLimit, note = note)
    return q,nSuccesses
示例#5
0
文件: calc.py 项目: elaird/ra1stats
def clsCustom(wspace, data, nToys = 100, smOnly = None, testStatType = None, note = "", plots = True) :
    assert not smOnly

    toys = {}
    for label,f in {"b":0.0, "sb":1.0, "fHat":None}.iteritems() :
        if f!=None :
            wspace.var("f").setVal(f)
            wspace.var("f").setConstant()
        else :
            wspace.var("f").setVal(1.0)
            wspace.var("f").setConstant(False)
        results = utils.rooFitResults(common.pdf(wspace), data)
        wspace.saveSnapshot("snap_%s"%label, wspace.allVars())
        toys[label] = common.pseudoData(wspace, nToys)
        utils.delete(results)

    args = {"wspace": wspace, "testStatType": testStatType, "snapSb": "snap_sb", "snapB": "snap_b", "snapfHat": "snap_fHat"}
    obs = ts(data = data, **args)

    out = {}
    values = {}
    for label in ["b", "sb"] :
        values[label] = []
        for toy in toys[label] :
            values[label].append(ts(data = toy, **args))
        out["CL%s"%label] = 1.0-utils.indexFraction(obs, values[label])
    if plots : plotting.clsCustomPlots(obs = obs, valuesDict = values, note = "TS%d_%s"%(testStatType, note))

    out["CLs"] = out["CLsb"]/out["CLb"] if out["CLb"] else 9.9
    return out
示例#6
0
def ntupleOfFitToys(wspace = None, data = None, nToys = None, cutVar = ("",""), cutFunc = None ) :
    results = utils.rooFitResults(pdf(wspace), data)
    wspace.saveSnapshot("snap", wspace.allVars())

    obs = collect(wspace, results, extraStructure = True)

    toys = []
    for i,dataSet in enumerate(pseudoData(wspace, nToys)) :
        wspace.loadSnapshot("snap")
        #dataSet.Print("v")
        results = utils.rooFitResults(pdf(wspace), dataSet)

        if all(cutVar) and cutFunc and cutFunc(getattr(wspace,cutVar[0])(cutVar[1]).getVal()) :
            wspace.allVars().assignValueOnly(dataSet.get())
            wspace.saveSnapshot("snapA", wspace.allVars())
            return obs,results,i
        
        toys.append( collect(wspace, results) )
        utils.delete(results)
    return obs,toys
示例#7
0
文件: calc.py 项目: elaird/ra1stats
def ts40(wspace = None, data = None, snapSb = None, snapB = None, snapfHat = None, verbose = False) :
        wspace.loadSnapshot(snapB)
        results = utils.rooFitResults(common.pdf(wspace), data)
        if verbose :
            print " B "
            print "---"
            results.Print("v")
        out = -results.minNll()
        utils.delete(results)

        if verbose : print "TS:",out
        return out
示例#8
0
def ntupleOfFitToys(wspace = None, data = None, nToys = None, cutVar = ("",""), cutFunc = None, toyNumberMod = 5) :
    results = utils.rooFitResults(common.pdf(wspace), data)
    wspace.saveSnapshot("snap", wspace.allVars())

    obs = collect(wspace, results, extraStructure = True)

    toys = []
    for i,dataSet in enumerate(common.pseudoData(wspace, nToys)) :
        if not (i%toyNumberMod) : print "iToy = %d"%i
        wspace.loadSnapshot("snap")
        #dataSet.Print("v")
        results = utils.rooFitResults(common.pdf(wspace), dataSet)

        wspace.allVars().assignValueOnly(dataSet.get()) #store this toy's observations, needed for (a) computing chi2 in collect(); (b) making "snapA"
        if all(cutVar) and cutFunc and cutFunc(getattr(wspace,cutVar[0])(cutVar[1]).getVal()) :
            wspace.saveSnapshot("snapA", wspace.allVars())
            return obs,results,i
        
        toys.append( collect(wspace, results) )
        utils.delete(results)
    return obs,toys
示例#9
0
def ts10(wspace = None, data = None, snapSb = None, snapB = None, snapfHat = None, verbose = False) :
        wspace.loadSnapshot(snapSb)
        results = utils.rooFitResults(pdf(wspace), data)
        if verbose :
            print "S+B"
            print "---"            
            results.Print("v")
        sbLl = -results.minNll()
        utils.delete(results)
        
        wspace.loadSnapshot(snapB)
        results = utils.rooFitResults(pdf(wspace), data)
        if verbose :
            print " B "
            print "---"            
            results.Print("v")        
        bLl = -results.minNll()
        utils.delete(results)

        out = -2.0*(sbLl-bLl)
        if verbose : print "TS:",out
        return out
示例#10
0
    def bestFit(self, printPages = False) :
        args = {"wspace": self.wspace, "results": utils.rooFitResults(pdf(self.wspace), self.data),
                "lumi": self.inputData.lumi(), "htBinLowerEdges": self.inputData.htBinLowerEdges(),
                "htMaxForPlot": self.inputData.htMaxForPlot(), "smOnly": self.smOnly(), "note": self.note(),
                "signalExampleToStack": self.signalExampleToStack, "printPages": printPages}

        for item in ["REwk", "RQcd"] :
            args[item] = self.likelihoodSpec[item]

        args["mumuTerms"] = False #temporary
        args["hadControlLabels"] = [] #temporary
        
        plotter = plotting.validationPlotter(args)
        plotter.inputData = self.inputData
        plotter.go()
示例#11
0
def expectedLimit(dataset,
                  modelConfig,
                  wspace,
                  smOnly,
                  cl,
                  nToys,
                  plusMinus,
                  note="",
                  makePlots=False):
    assert not smOnly

    #fit to SM-only
    wspace.var("f").setVal(0.0)
    wspace.var("f").setConstant(True)
    results = utils.rooFitResults(common.pdf(wspace), dataset)

    #generate toys
    toys = common.pseudoData(wspace, nToys)

    #restore signal model
    wspace.var("f").setVal(1.0)
    wspace.var("f").setConstant(False)

    #save snapshot
    snapName = "snap"
    wspace.saveSnapshot(snapName, wspace.allVars())

    #fit toys
    l = limits(wspace, snapName, modelConfig, smOnly, cl, toys)

    q, hist = quantiles(l,
                        plusMinus,
                        histoName="upperLimit",
                        histoTitle=";upper limit on XS factor;toys / bin",
                        histoBins=(50, 1, -1),
                        cutZero=True)  #enable auto-range
    nSuccesses = hist.GetEntries()

    obsLimit = limits(wspace, snapName, modelConfig, smOnly, cl, [dataset])[0]

    if makePlots:
        plotting.expectedLimitPlots(quantiles=q,
                                    hist=hist,
                                    obsLimit=obsLimit,
                                    note=note)
    return q, nSuccesses
示例#12
0
def clsCustom(wspace,
              data,
              nToys=100,
              smOnly=None,
              testStatType=None,
              note="",
              plots=True):
    assert not smOnly

    toys = {}
    for label, f in {"b": 0.0, "sb": 1.0, "fHat": None}.iteritems():
        if f != None:
            wspace.var("f").setVal(f)
            wspace.var("f").setConstant()
        else:
            wspace.var("f").setVal(1.0)
            wspace.var("f").setConstant(False)
        results = utils.rooFitResults(common.pdf(wspace), data)
        wspace.saveSnapshot("snap_%s" % label, wspace.allVars())
        toys[label] = common.pseudoData(wspace, nToys)
        utils.delete(results)

    args = {
        "wspace": wspace,
        "testStatType": testStatType,
        "snapSb": "snap_sb",
        "snapB": "snap_b",
        "snapfHat": "snap_fHat"
    }
    obs = ts(data=data, **args)

    out = {}
    values = {}
    for label in ["b", "sb"]:
        values[label] = []
        for toy in toys[label]:
            values[label].append(ts(data=toy, **args))
        out["CL%s" % label] = 1.0 - utils.indexFraction(obs, values[label])
    if plots:
        plotting.clsCustomPlots(obs=obs,
                                valuesDict=values,
                                note="TS%d_%s" % (testStatType, note))

    out["CLs"] = out["CLsb"] / out["CLb"] if out["CLb"] else 9.9
    return out
示例#13
0
 def debug(self) :
     self.wspace.Print("v")
     plotting.writeGraphVizTree(self.wspace)
     #pars = utils.rooFitResults(pdf(wspace), data).floatParsFinal(); pars.Print("v")
     utils.rooFitResults(pdf(self.wspace), self.data).Print("v")
示例#14
0
 def qcdPlot(self):
     plotting.errorsPlot(
         self.wspace,
         utils.rooFitResults(workspace.pdf(self.wspace), self.data),
     )
示例#15
0
 def rooFitResults(self):
     return utils.rooFitResults(workspace.pdf(self.wspace), self.data)
示例#16
0
 def bestFit(self) :
     plotting.validationPlots(self.wspace, utils.rooFitResults(pdf(self.wspace), self.data), self.inputData, self.REwk, self.RQcd, self.smOnly())
示例#17
0
 def qcdPlot(self):
     plotting.errorsPlot(self.wspace,
                         utils.rooFitResults(workspace.pdf(self.wspace), self.data),
                         )
示例#18
0
 def rooFitResults(self):
     return utils.rooFitResults(workspace.pdf(self.wspace), self.data)
示例#19
0
 def debug(self) :
     self.wspace.Print("v")
     plotting.writeGraphVizTree(self.wspace)
     #pars = utils.rooFitResults(pdf(wspace), data).floatParsFinal(); pars.Print("v")
     utils.rooFitResults(pdf(self.wspace), self.data).Print("v")