Exemple #1
0
fcache = ROOT.TFile("cache.root", "RECREATE")
dataset = ROOT.RooDataSet("datasetPDF", "", cList, "weight")
for i in range(1, histoPDF.GetNbinsX() + 1):
    x = histoPDF.GetXaxis().GetBinCenter(i)
    w = histoPDF.GetBinContent(i)
    cList.setRealValue("GM", x)
    cList.setRealValue("weight", w)
    dataset.add(cList, w)

fitter = Fitter(['M'])
fitter.w.var("M").setVal((options.maxx - options.minx) / 2.0)
fitter.w.var("M").setMax(options.maxx)
fitter.w.var("M").setMin(options.minx)

fitter.gaussianSum("model", "M", dataset, "GM")

histo = data.drawTH1(options.var, options.cut, "1", options.binsx * 4,
                     options.minx, options.maxx)
fitter.importBinnedData(histo, "M")
fitter.fit()
scale = fitter.w.var("scale").getVal()
sigma = fitter.w.var("sigma").getVal()
fitter.projection("model", "data", "M", options.output + "_debug.png")

h = fitter.w.pdf("model").createHistogram("M", options.binsx)
h.SetName("histo")
histograms.append(h)

#scaleUp
fitter.w.var("scale").setVal(scale * 1.15)
#dataset = data.makeDataSet(options.genvar,options.genCut,-1)

for i in range(1, axis.GetNbins() + 1):
    cBinLo = axis.GetBinLowEdge(i)
    cBinHi = axis.GetBinUpEdge(i)
    cBinCenter = axis.GetBinCenter(i)
    dataset = data.makeDataSet(
        options.genvar, options.genCut + '*({cvar}>{lo}&&{cvar}<={hi})'.format(
            cvar=options.condVar, lo=cBinLo - 100, hi=cBinHi + 100), -1)
    histo = data.drawTH1(
        options.var, options.cut + '*({cvar}>{lo}&&{cvar}<={hi})'.format(
            cvar=options.condVar, lo=cBinLo, hi=cBinHi), "1", options.bins,
        options.mini, options.maxi)
    fitter = Fitter([options.var])
    fitter.importBinnedData(histo, [options.var], 'data')
    fitter.gaussianSum('model', options.var, dataset, options.genvar)
    fitter.fit('model', 'data', [ROOT.RooFit.SumW2Error(1)])
    chi = fitter.projection("model", "data", options.var,
                            "debugPlot_" + str(i) + "_" + options.output, 'x')
    print i, 'Chi2', chi
    for nuis in ['scale', 'sigma']:
        v, vErr = fitter.fetch(nuis)
        graph[nuis].SetPoint(i, cBinCenter, v)
        graph[nuis].SetPointError(i, 0, vErr)

f2 = ROOT.TFile(options.output, "RECREATE")
f2.cd()
for g in graph.keys():
    graph[g].Write(g)
f2.Close()