Beispiel #1
0
def aggregate_fill(beam, fill_list=[], from_cache=False):
    """ Create an aggregate fill of the fill ids. If reading from cache, 
        the fill_list can be empty. """

    if not fill_list and not from_cache:
        raise Exception(
            "'fill_list' can't be empty if not to read from cache'")
    elif not beam in (1, 2):
        raise Exception("'beam' must be 1 or 2, is '{}'".format(beam))

    fill = Fill(nbr=-beam, beam=beam, fetch=False)
    if from_cache:
        fill.load_cache()
        return fill

    fills = []
    for nbr in fill_list:
        _fill = Fill(nbr)
        _fill.beta_coll_merge()
        fills.append(_fill)

    # This is cumbersome to refactor... TODO
    var = [
        "synch_coll_b{}".format(beam), "beta_coll_b{}".format(beam), "energy",
        "intensity_b{}".format(beam)
    ]
    for v in var:
        merged = merge_variable(fills, v)
        fill.data[v] = merged["average"]
    return fill
Beispiel #2
0
def plot_aggregate_fill(beam, fill_list):
    # We can't reuse the 'aggregate_fill' function above, as we want to plot more
    # than for a normal fill (min, max, average)
    fills = []
    for nbr in fill_list:
        fill = Fill(nbr, beam=beam)
        fill.beta_coll_merge()
        fills.append(fill)

    var = [
        "synch_coll_b{}".format(beam), "beta_coll_b{}".format(beam), "energy"
    ]
    merged = {}
    for v in var:
        merged[v] = merge_variable(fills, v)

    fig, ax = plt.subplots()
    ax.set_yscale("log")
    e_ax = ax.twinx()

    v = "synch_coll_b{}".format(beam)
    ax.plot(*merged[v]["average"], zorder=10, color='red', label='OML')
    ax.fill_between(*merged[v]["max"],
                    merged[v]["min"].y,
                    color='red',
                    alpha='0.3')

    v = "beta_coll_b{}".format(beam)
    ax.plot(*merged[v]["average"], zorder=9, color='green', label='TL')
    ax.fill_between(*merged[v]["max"],
                    merged[v]["min"].y,
                    color='green',
                    alpha='0.2')

    e_ax.plot(*merged["energy"]["average"],
              zorder=5,
              color='black',
              label='energy')
    e_ax.set_ylabel("Reference energy (GeV)")
    ax.set_xlabel("t (s)")
    ax.set_ylabel("Gy/s")
    ax.legend(loc="upper right")
    ax.set_xlim(fills[0].blm_ir3().x[fills[0].OML_period()] +
                np.array([-5, +120]))
    # ax.axvspan(0.0, 13.55, facecolor='b', zorder=0, alpha=0.1)
    plt.title("Aggregate fill 2016 (beam {})".format(beam))
    plt.show()
Beispiel #3
0
def comp_blm_ir3_vs_ir7(file):
    fills = fills_from_file(file, "OML")
    ok = 0
    notok = 0
    sdata = {
        'max': [],
        'mean': [],
    }
    bdata = {'max': [], 'mean': []}
    for nbr in fills:
        fill = Fill(nbr)
        fill.beta_coll_merge()

        smin, smax = fill.OML_period()
        tmin, tmax = fill.blm_ir3().x[[smin, smax]]
        bmin = fill.blm_ir7().index_for_time(tmin)
        bmax = fill.blm_ir7().index_for_time(tmax)

        bsubset = fill.blm_ir7().y[bmin:bmax]
        ssubset = fill.blm_ir3().y[smin:smax]

        sdata['max'].append(max(ssubset))
        sdata['mean'].append(np.mean(ssubset))
        bdata['max'].append(max(bsubset))
        bdata['mean'].append(np.mean(bsubset))

    fig, ax = plt.subplots()
    ax.set_xlabel("Synchrotron (IR3) TCP")
    ax.set_ylabel("Betatron (IR7) TCPs")

    ax.scatter(sdata['max'], bdata['max'], color='r')
    slope, intercept, r_value, p_value, std_err = stats.linregress(
        sdata['max'], bdata['max'])
    # print(slope, intercept, r_value, p_value, std_err)
    xval = [0, 1]
    max_yval = [slope * x + intercept for x in xval]
    ax.plot(xval, max_yval, color='r', label='max')

    ax.scatter(sdata['mean'], bdata['mean'], color='b')
    slope, intercept, r_value, p_value, std_err = stats.linregress(
        sdata['mean'], bdata['mean'])
    # print(slope, intercept, r_value, p_value, std_err)
    mean_yval = [slope * x + intercept for x in xval]
    ax.plot(xval, mean_yval, color='b', label='mean')

    ax.plot([0, 1], [0, 1], color='black', label='delimiter')

    for v in ['max', 'mean']:
        count = 0
        for i, sd in enumerate(sdata[v]):
            if bdata[v][i] > sd:
                count += 1
        print(v, "over: ", count,
              "({}%)".format(int(float(count) / len(sdata[v]) * 100)))

    plt.title(
        'Losses due to synchrotron vs betatron oscillations\n for {}'.format(
            file))
    ax.legend(loc='upper right')
    ax.set_ylim([0, 0.5])
    ax.set_xlim([0, 0.5])
    plt.show()