示例#1
0
文件: eval_taus.py 项目: Bigben37/FP1
def printGraph(datas, phi, name='', fit=False):
    """make graph with measured taus for one specific angle phi

    Arguments:
    datas -- datalists (sorted by series in dictonary)
    phi   -- angle
    name  -- additional name for file name
    fit   -- if true fit data with linear model (default=False)
    """
    # setup canvas and legend
    c = TCanvas('c_%d' % phi, '', 1280, 720)
    if fit:
        l = TLegend(0.6, 0.15, 0.85, 0.5)
    else:
        l = TLegend(0.65, 0.15, 0.85, 0.35)
    l.SetTextSize(0.03)
    # make and draw graphs, graphs are organized by TMultiGraph
    graphs = TMultiGraph()
    for s, datalist in datas.iteritems():
        data = DataErrors.fromLists(*zip(*datalist))
        g = data.makeGraph('g_%d_%d' % (phi, s))
        g.SetMarkerColor(seriescolors[s])
        g.SetLineColor(seriescolors[s])
        l.AddEntry(g, serieslabels[s], 'p')
        graphs.Add(g)
    graphs.Draw('AP')
    gPad.Update()
    setMultiGraphTitle(graphs, 'Druck p / mPa', '#tau / ns')

    # fit data with linear fit
    if fit:
        fit = Fitter('fit_%d' % phi, 'pol1(0)')
        fit.function.SetNpx(1000)
        fit.setParam(0, '#tau_{0}', 119)
        fit.setParam(1, 'm', 0.5)
        fit.fit(graphs, 0, 225, 'M')
        fit.saveData('../calc/fit_tau_%02d%s.txt' % (phi, name), 'w')
        l.AddEntry(fit.function, 'Fit mit #tau(p) = #tau_{0} + m * p', 'l')
        fit.addParamsToLegend(l, [('%.1f', '%.1f'), ('%.2f', '%.2f')], chisquareformat='%.2f',
                              advancedchi=True, units=['ns', 'ns / mPa'])

    # draw legend and print canvas to file
    l.Draw()
    c.Update()
    c.Print('../img/taus_%02d%s.pdf' % (phi, name), 'pdf')

    # return required fit parameter
    if fit:
        return fit.params[0]['value'], fit.params[0]['error']
示例#2
0
def makeGraph(xlist, ylist, slist, xerror, yerror, xlabel, ylabel, name, fit=False, fitlabel=""):
    """ make graph with optional linear fit

    Arguments:
    xlist    -- list with x-values
    ylist    -- list with y-values
    slist    -- list with measurement series
    xerror   -- absolute error on x-values
    yerror   -- absolute error on y-values
    xlabel   -- title of x-axis
    ylabel   -- title of y-axis
    name     -- appendix to file name
    fit      -- if True fits data with linear model (default=False)
    fitlabel -- fit function as string for legend
    """
    xmin = min(xlist)
    xmax = max(xlist)

    datas = dict()
    for x, y, s in zip(xlist, ylist, slist):
        if s in datas:
            datas[s].append((x, y, xerror, yerror))
        else:
            datas[s] = [(x, y, xerror, yerror)]

    c = TCanvas("c_%s" % name, "", 1280, 720)
    graphs = TMultiGraph()
    for s, datalist in datas.iteritems():
        data = DataErrors.fromLists(*zip(*datalist))
        g = data.makeGraph("g_%s_%d" % (name, s))
        g.SetMarkerColor(seriescolors[s])
        g.SetLineColor(seriescolors[s])
        graphs.Add(g)
    graphs.Draw("AP")
    gPad.Update()
    setMultiGraphTitle(graphs, xlabel, ylabel)

    if fit:
        fit = Fitter("fit_%s" % name, "pol1(0)")
        fit.setParam(0, "a")
        fit.setParam(1, "b")
        fit.fit(graphs, xmin - 1, xmax + 1)
        fit.saveData("../calc/fit_%s.txt" % name, "w")

    if fit:
        yheight = 0.3
    else:
        yheight = 0.15
    if ylist[0] < ylist[-1]:  # /
        l = TLegend(0.15, 0.85 - yheight, 0.4, 0.85)
    else:  # \
        l = TLegend(0.6, 0.85 - yheight, 0.85, 0.85)
    l.SetTextSize(0.03)
    for i, graph in enumerate(graphs.GetListOfGraphs()):
        l.AddEntry(graph, serieslabels[i], "p")
    if fit:
        l.AddEntry(fit.function, "Fit mit %s" % fitlabel, "l")
        fit.addParamsToLegend(
            l, [("%.2f", "%.2f"), ("%.3f", "%.3f")], chisquareformat="%.2f", advancedchi=True, units=["U", "#Omega"]
        )
    l.Draw()

    c.Update()
    c.Print("../img/graph_%s.pdf" % name, "pdf")