示例#1
0
def fitBVoltage(bdata, name):
    """Fit ramp for b-field

    Arguments:
    bdata -- data
    name  -- filename
    """
    # make canvas and graphs
    c = TCanvas("fitBV_%s" % name, "", 1280, 720)
    g = bdata.makeGraph("gBV_%s" % name, "Zeit t / s", "Spannung U / V")
    prepareGraph(g)
    g.Draw("AP")
    g.Draw("PX")  # set points in foreground, errors in background

    # fit linear ramp, start and end value of x-axis (time) are obtained by getXStart and getXend
    xmin = getXStart(bdata, bdata.getLength() / 10, 0.25) + 0.1
    xmax = getXEnd(bdata, bdata.getLength() / 10, 0.25) - 0.1
    fit = Fitter("fitBV_%s" % name, "pol1(0)")
    fit.setParam(0, "a", 0)
    fit.setParam(1, "b", -2.0 / 3)
    fit.fit(g, xmin, xmax, "M")
    fit.saveData("../calc/fitBV_%s.txt" % name, "w")

    # draw legend
    l = TLegend(0.55, 0.6, 0.85, 0.85)
    l.SetTextSize(0.03)
    l.AddEntry(g, "Messdaten", "p")
    l.AddEntry(fit.function, "Fit mit U(t) = a + b*t", "l")
    fit.addParamsToLegend(
        l, [("%.4f", "%.4f"), ("%.4f", "%.4f")], chisquareformat="%.2f", advancedchi=True, units=["U", "U/s"]
    )
    l.Draw()

    # print canvas to file
    c.Update()
    c.Print("../img/fitBV_%s.pdf" % name, "pdf")

    return fit.function
示例#2
0
def fitLorentzPeak(name, phi, T):
    """fit lorentz peak

    Arguments:
    name -- filename
    phi  -- angle
    T    -- temperature
    """
    # read data
    data = HanleData.fromPath("../data/messungen/%s.tab" % name, 1)
    data.setYErrorFunc(lambda x: np.sqrt((0.01 * x) ** 2 + data.points[0][3] ** 2))  # bin error and relative error
    bdata = HanleData.fromPath("../data/messungen/%s.tab" % name, 2)
    # convert time to B-field and show only points with -0.2mT < x < 0.2mT
    data.convertTimeToB(fitBVoltage(bdata, name))
    data.filterX(-0.2, 0.2)

    # make canvas + graph
    canvas = TCanvas("c_%s" % name, "", 1280, 720)
    g = data.makeGraph("g_%s" % name, "Magnetfeld B / T", "Intensit#ddot{a}t I / V")
    prepareGraph(g)
    g.Draw("AP")
    g.Draw("PX")  # redraw points without errors to set points in front of errors

    # get initial fit parameters
    gJ = 1.4838
    muB = 9.27400968e-24
    hbar = 1.05457126e-34
    a = gJ * muB / hbar / 1e12  # in 1/(mT*ns)
    b = 0
    if phi == 45:
        xmin, xmax = -0.05, 0.05
        A = 0.02
        c = -0.5
    else:
        xmin, xmax = -0.1, 0.1
        A = -0.02
        c = 1
    if T == -8 and phi == 0:  # special case, wont fit with 0.01
        xmin, xmax = -0.075, 0.075
    phipar = np.deg2rad(phi)
    # fit data
    fit = Fitter("fit_%s" % name, fitFuncLorentz, (xmin, xmax, 6))
    fit.setParam(0, "#tau", 120)
    fit.setParam(1, "#phi", phipar)
    fit.setParam(2, "A", A)
    fit.setParam(3, "a", a, True)
    fit.setParam(4, "b", b)
    fit.setParam(5, "c", c)
    fit.fit(g, xmin, xmax, "M")
    fit.saveData("../calc/fit_%s.txt" % name, "w")

    # add legend
    if phi == 90:
        l = TLegend(0.11, 0.15, 0.4, 0.55)
    else:
        l = TLegend(0.11, 0.45, 0.4, 0.85)
    l.SetTextSize(0.03)
    l.AddEntry(g, "Messdaten", "p")
    l.AddEntry(fit.function, "Fit mit Lorentzkurve", "l")
    fit.addParamsToLegend(
        l,
        format=[("%.2f", "%.2f"), ("%.2e", "%.1e"), ("%.2e", "%.1e"), "%.2e", ("%.2e", "%.1e"), ("%.3f", "%.3f")],
        chisquareformat="%.2f",
        advancedchi=True,
        units=["ns", "rad", "V/ns", "1/(mT*ns)", "T", "V"],
    )
    l.Draw()

    # print to file
    canvas.Update()
    canvas.Print("../img/fit_%s.pdf" % name, "pdf")

    # return lifespan (tau) and angle (phi)
    return (fit.params[0]["value"], fit.params[0]["error"]), (normPhi(fit.params[1]["value"]), fit.params[1]["error"])