示例#1
0
def _get_f(couplelist, dpplist, bpm_name, value):
    """
    calculates the linear regression of 'value' for each
    dpp in dpplist

    :param couplelist: list of getcouple files (for each dpp)
    :param dpplist: list of all dpp values available
    :param bpm_name: name of bpm
    :param value: name of column (e.g. F1001R)
    """
    lst = []
    x = []
    for dpp in dpplist:
        x.append(dpp)
        couplefile = couplelist[dpp]
        lst.append(getattr(couplefile, value)[couplefile.indx[bpm_name]])
    lreg = linreg.linreg(x, lst)
    return lreg[0], lreg[3]
示例#2
0
def _get_f(couplelist, dpplist, bpm_name, value):
    '''
    calculates the linear regression of 'value' for each
    dpp in dpplist

    :param couplelist: list of getcouple files (for each dpp)
    :param dpplist: list of all dpp values available
    :param bpm_name: name of bpm
    :param value: name of column (e.g. F1001R)
    '''
    lst = []
    x = []
    for dpp in dpplist:
        x.append(dpp)
        couplefile = couplelist[dpp]
        lst.append(getattr(couplefile, value)[couplefile.indx[bpm_name]])
    lreg = linreg.linreg(x, lst)
    return lreg[0], lreg[3]
示例#3
0
def _do_lin_reg_bet(fileobj, listx, listy, bpms, plane, zero, twiss):
    """
    Calculates stuff and writes to the file in a table
    Closes the file afterwards

    :param fileobj: _chromFileWriter for output table
    :param listx: List of variables...
    :param listy: List of variables...
    :param bpms: List of BPMs
    :param plane: Which plane (H/V)
    :param zero: Twiss for dp/p = 0
    :param twiss: Twiss
    """
    for bpm in bpms:
        name = bpm[1]
        sloc = bpm[0]
        indx = []
        b = []
        a = []
        bm = []
        am = []
        if "H" in plane:
            beta0 = zero.BETX[zero.indx[name]]
            alfa0 = zero.ALFX[zero.indx[name]]
            alfa0err = zero.STDALFX[zero.indx[name]]

            beta0m = twiss.BETX[twiss.indx[name]]
            alfa0m = twiss.ALFX[twiss.indx[name]]

            wmo = twiss.WX[twiss.indx[name]]
            pmo = twiss.PHIX[twiss.indx[name]]
        else:

            beta0 = zero.BETY[zero.indx[name]]
            alfa0 = zero.ALFY[zero.indx[name]]
            alfa0err = zero.STDALFY[zero.indx[name]]

            beta0m = twiss.BETY[twiss.indx[name]]
            alfa0m = twiss.ALFY[twiss.indx[name]]

            wmo = twiss.WY[twiss.indx[name]]
            pmo = twiss.PHIY[twiss.indx[name]]
        for dpp in listx:
            _file = listy[dpp]
            ix = _file.indx[name]
            indx.append(ix)
            if "H" in plane:
                b.append(_file.BETX[ix])
                a.append(_file.ALFX[ix])

                bm.append(_file.BETXMDL[_file.indx[name]])
                am.append(_file.ALFXMDL[_file.indx[name]])
            else:
                b.append(_file.BETY[ix])
                a.append(_file.ALFY[ix])

                bm.append(_file.BETYMDL[_file.indx[name]])
                am.append(_file.ALFYMDL[_file.indx[name]])

        bfit = linreg.linreg(listx, b)
        afit = linreg.linreg(listx, a)

        bfitm = linreg.linreg(listx, bm)
        afitm = linreg.linreg(listx, am)

        # measurement
        dbb = bfit[0] / beta0
        dbberr = bfit[3] / beta0
        da = afit[0]
        daerr = afit[3]
        A = dbb
        Aerr = dbberr
        B = da - alfa0 * dbb
        Berr = math.sqrt(daerr ** 2 + (alfa0err * dbb) ** 2 + (alfa0 * dbberr) ** 2)
        w = 0.5 * math.sqrt(A ** 2 + B ** 2)
        werr = 0.5 * math.sqrt((Aerr * A / w) ** 2 + (Berr * B / w) ** 2)
        phi = math.atan2(B, A) / 2.0 / math.pi
        phierr = 1.0 / (1.0 + (A / B) ** 2) * math.sqrt((Aerr / B) ** 2 + (A / B ** 2 * Berr) ** 2) / 2.0 / math.pi

        # model
        dbbm = bfitm[0] / beta0m
        dbberrm = bfitm[3] / beta0m
        dam = afitm[0]
        daerrm = afitm[3]
        Am = dbbm
        Aerrm = dbberrm
        Bm = dam - alfa0m * dbbm
        Berrm = math.sqrt(daerrm ** 2 + (alfa0m * dbberrm) ** 2)
        wm = 0.5 * math.sqrt(Am ** 2 + Bm ** 2)
        werrm = 0.5 * math.sqrt((Aerrm * Am / wm) ** 2 + (Berrm * Bm / wm) ** 2)
        phim = math.atan2(Bm, Am) / 2.0 / math.pi
        phierrm = (
            1.0 / (1.0 + (Am / Bm) ** 2) * math.sqrt((Aerrm / Bm) ** 2 + (Am / Bm ** 2 * Berrm) ** 2) / 2.0 / math.pi
        )

        fileobj.writeLine(locals().copy())
示例#4
0
def _do_lin_reg_bet(fileobj, listx, listy, bpms, plane, zero, twiss):
    '''
    Calculates stuff and writes to the file in a table
    Closes the file afterwards

    :param fileobj: _chromFileWriter for output table
    :param listx: List of variables...
    :param listy: List of variables...
    :param bpms: List of BPMs
    :param plane: Which plane (H/V)
    :param zero: Twiss for dp/p = 0
    :param twiss: Twiss
    '''
    for bpm in bpms:
        name = bpm[1]
        sloc = bpm[0]
        indx = []
        b = []
        a = []
        bm = []
        am = []
        if "H" in plane:
            beta0 = zero.BETX[zero.indx[name]]
            alfa0 = zero.ALFX[zero.indx[name]]
            try:
                alfa0err = zero.STDALFX[zero.indx[name]]
            except AttributeError:
                alfa0err = zero.ERRALFX[zero.indx[name]]

            beta0m = twiss.BETX[twiss.indx[name]]
            alfa0m = twiss.ALFX[twiss.indx[name]]

            wmo = twiss.WX[twiss.indx[name]]
            pmo = twiss.PHIX[twiss.indx[name]]
        else:

            beta0 = zero.BETY[zero.indx[name]]
            alfa0 = zero.ALFY[zero.indx[name]]
            try:
                alfa0err = zero.STDALFY[zero.indx[name]]
            except AttributeError:
                alfa0err = zero.ERRALFY[zero.indx[name]]

            beta0m = twiss.BETY[twiss.indx[name]]
            alfa0m = twiss.ALFY[twiss.indx[name]]

            wmo = twiss.WY[twiss.indx[name]]
            pmo = twiss.PHIY[twiss.indx[name]]
        for dpp in listx:
            _file = listy[dpp]
            ix = _file.indx[name]
            indx.append(ix)
            if "H" in plane:
                b.append(_file.BETX[ix])
                a.append(_file.ALFX[ix])

                bm.append(_file.BETXMDL[_file.indx[name]])
                am.append(_file.ALFXMDL[_file.indx[name]])
            else:
                b.append(_file.BETY[ix])
                a.append(_file.ALFY[ix])

                bm.append(_file.BETYMDL[_file.indx[name]])
                am.append(_file.ALFYMDL[_file.indx[name]])

        bfit = linreg.linreg(listx, b)
        afit = linreg.linreg(listx, a)

        bfitm = linreg.linreg(listx, bm)
        afitm = linreg.linreg(listx, am)

        # measurement
        dbb = bfit[0] / beta0
        dbberr = bfit[3] / beta0
        da = afit[0]
        daerr = afit[3]
        A = dbb
        Aerr = dbberr
        B = da - alfa0 * dbb
        Berr = math.sqrt(daerr**2 + (alfa0err * dbb)**2 + (alfa0 * dbberr)**2)
        w = 0.5 * math.sqrt(A**2 + B**2)
        werr = 0.5 * math.sqrt((Aerr * A / w)**2 + (Berr * B / w)**2)
        phi = math.atan2(B, A) / 2. / math.pi
        phierr = 1. / (1. + (A / B)**2) * math.sqrt(
            (Aerr / B)**2 + (A / B**2 * Berr)**2) / 2. / math.pi

        #model
        dbbm = bfitm[0] / beta0m
        dbberrm = bfitm[3] / beta0m
        dam = afitm[0]
        daerrm = afitm[3]
        Am = dbbm
        Aerrm = dbberrm
        Bm = dam - alfa0m * dbbm
        Berrm = math.sqrt(daerrm**2 + (alfa0m * dbberrm)**2)
        wm = 0.5 * math.sqrt(Am**2 + Bm**2)
        werrm = 0.5 * math.sqrt((Aerrm * Am / wm)**2 + (Berrm * Bm / wm)**2)
        phim = math.atan2(Bm, Am) / 2. / math.pi
        phierrm = 1. / (1. + (Am / Bm)**2) * math.sqrt(
            (Aerrm / Bm)**2 + (Am / Bm**2 * Berrm)**2) / 2. / math.pi

        fileobj.writeLine(locals().copy())