Ejemplo n.º 1
0
def fitSpectrum(detector, element, params, logy=True):
    data = P3SemiCon.fromPath('../data/part3/%s-%s.mca' % (element, detector))
    printTotalSpectrum(data, element, detector, logy)

    fitresults = []
    for i, peak in enumerate(params):
        c = TCanvas('cpeakl_%s-%s_%d' % (element, detector, i), '', 1280, 720)
        g = data.makeGraph('g%s-%s_%d' % (element, detector, i), 'Kanal k', 'Counts N')
        prepareGraph(g)
        g.GetXaxis().SetRangeUser(peak[2][0], peak[2][1])
        g.SetMinimum(peak[3][0])
        g.SetMaximum(peak[3][1])
        g.Draw('AP')

        fit = None
        paramnames = []
        if len(peak[0]) == 5:
            fit = Fitter('fit%d' % i, 'pol1(0) + 1/(sqrt(2*pi*[4]^2))*gaus(2)')
            paramnames = ['a', 'b', 'A', 'k_{c}', 's']
        elif len(peak[0]) == 4:
            fit = Fitter('fit%d' % i, '[0] + 1/(sqrt(2*pi*[3]^2))*gaus(1)')
            paramnames = ['a', 'A', 'k_{c}', 's']
        elif len(peak[0]) == 3:
            fit = Fitter('fit%d' % i, '1/(sqrt(2*pi*[2]^2))*gaus(0)')
            paramnames = ['A', 'k_{c}', 's']

        l = None
        if len(peak[0]) > 0:
            for j, param in enumerate(peak[0]):
                fit.setParam(j, paramnames[j], param)
            fit.fit(g, *peak[1])

            fitname = ''
            if len(peak[0]) == 5:
                fitname = 'N(k) = a + b*k + #frac{A}{#sqrt{2#pi*#sigma^{2}}} exp(- #frac{1}{2} (#frac{k-k_{c}}{#sigma})^{2})'
            elif len(peak[0]) == 4:
                fitname = 'N(k) = a + #frac{A}{#sqrt{2#pi*#sigma^{2}}} exp(- #frac{1}{2} (#frac{k-k_{c}}{#sigma})^{2})'
            elif len(peak[0]) == 3:
                fitname = 'N(k) = #frac{A}{#sqrt{2#pi*#sigma^{2}}} exp(- #frac{1}{2} (#frac{k-k_{c}}{#sigma})^{2})'
            fit.saveData('../calc/part3/fit_%s-%s_%02d.txt' % (element, detector, i), 'w')
            results = []
            for j, param in fit.params.iteritems():
                results.append((param['value'], param['error']))
            fitresults.append(results)

            # legend
            l = TLegend(0.675, 0.5, 0.995, 0.85)
            l.SetTextSize(0.025)
            l.AddEntry(g, 'Messwerte', 'p')
            l.AddEntry(fit.function, 'Fit mit', 'l')
            l.AddEntry(0, fitname, '')
            l.AddEntry(0, '', '')
            fit.addParamsToLegend(l, chisquareformat='%.2f')
            l.Draw()

        c.Update()
        if PRINTGRAPHS:
            c.Print('../img/part3/%s-%s_%02d.pdf' % (element, detector, i), 'pdf')
    return fitresults
Ejemplo n.º 2
0
def printTotalSpectrum(data, element, detector, logy=True):
    c = TCanvas('total_%s-%s' % (element, detector), '', 1280, 720)
    c.SetLogy(logy)
    g = data.makeGraph('g%s-%s' % (element, detector), 'Kanal k', 'Counts N')
    prepareGraph(g)
    g.GetXaxis().SetRangeUser(0, 2500)
    g.SetMinimum(0.9)
    g.Draw('APX')
    c.Update()
    if PRINTGRAPHS:
        c.Print('../img/part3/%s-%s_spectrum.pdf' % (element, detector), 'pdf')