def corr_inj_OML(fills): inj_t = {"b1": [], "b2": []} oml = {"b1": [], "b2": []} for i, nbr in enumerate(fills): for b in (1, 2): f = Fill(nbr, beam=b) inj = next( (item for item in f.meta['beamModes'] if item['mode'] == 'INJPHYS'), None) preramp = next( (item for item in f.meta['beamModes'] if item['mode'] == 'PRERAMP'), None) if preramp is None or inj is None: raise Exception("Invalid fill {}".format(nbr)) if b == 1 and f.blm_ir3().y.max() > 0.3 or b == 2 and f.blm_ir3( ).y.max() > 0.11: continue dt = preramp['endTime'] - inj['startTime'] inj_t['b{}'.format(b)].append(dt) oml['b{}'.format(b)].append(f.blm_ir3().y.max()) colors = plt.rcParams["axes.prop_cycle"].by_key()["color"] fig, ax = plt.subplots(nrows=2, ncols=1) ax[0].scatter(inj_t['b1'], oml['b1'], label='beam 1') k1, m1, r1, p1, err1 = stats.linregress(inj_t['b1'], oml['b1']) ax[0].plot(ax[0].get_xlim(), [k1 * x + m1 for x in ax[0].get_xlim()], label='fit b1, r={:.3f}'.format(r1)) ax[1].scatter(inj_t['b2'], oml['b2'], label='beam 2', c=colors[1]) k2, m2, r2, p2, err2 = stats.linregress(inj_t['b2'], oml['b2']) ax[1].plot(ax[1].get_xlim(), [k2 * x + m2 for x in ax[1].get_xlim()], label='fit b2, r={:.3f}'.format(r2), c=colors[1]) for a in ax: a.legend(loc="upper right") a.set_xlabel("t") a.set_ylabel("OML peak") print("Fit 1: {}*x + {}, r={}".format(k1, m1, r1)) print("Fit 2: {}*x + {}, r={}".format(k2, m2, r2)) plt.show()
def plot_aggregate_fill_overlayed(beam, fill_list): """ 'beam' must be iterable: (1,), (2,), or (1, 2) """ fig, axes = plt.subplots(2, sharex=True, sharey=True) for b in beam: ls = '--' if b == 2 and len(beam) == 2 else '-' for nbr in fill_list: fill = Fill(nbr, beam=b) c = np.random.rand(3) axes[0].plot(*fill.blm_ir3(), color=c, alpha=0.3, linestyle=ls) axes[1].plot(*fill.blm_ir7(), color=c, alpha=0.3, linestyle=ls) aggr = aggregate_fill(b, fill_list) axes[0].plot(*aggr.blm_ir3(), color='black', label='IR3 Aggregate B{}'.format(b), zorder=5, linestyle=ls) axes[1].plot(*aggr.blm_ir7(), color='black', label='IR7 Aggregate B{}'.format(b), zorder=4, linestyle=ls) axes[0].set_xlim(aggr.blm_ir3().x[aggr.OML_period()] + np.array([-5, +120])) axes[0].set_ylabel("Losses (Gy/s)") axes[1].set_ylabel("Losses (Gy/s)") for ax in axes: ax.set_yscale("log") ax.set_xlabel("t (s)") ax.legend(loc="upper right") fig.suptitle("Overlay plot (beam {})".format(",".join(map(str, beam)))) # plt.title("Overlay plot (beam {})".format(beam)) plt.show()
def histogram_OML_peak(fills): OML_peak = {1: [], 2: []} for nbr in fills: for beam in (1, 2): fill = Fill(nbr, beam=beam) OML_peak[beam].append(np.log10(fill.blm_ir3().y.max())) fig, ax = plt.subplots() ax.hist([OML_peak[1], OML_peak[2]], label=["Beam 1", "Beam 2"]) ax.legend(loc="upper right") ax.set_xlabel("BLM peak signal [$10^x$]") ax.set_ylabel("Fill count") plt.title("Peak IR3 TCP BLM signal distribution") plt.show()
def histogram_max_spike(fills): spike_time = {1: [], 2: []} for nbr in fills: for b in (1, 2): fill = Fill(nbr, beam=b) losses = fill.blm_ir3() ispike = np.argmax(losses.y) spike_time[b].append(losses.x[ispike]) fig, ax = plt.subplots() ax.hist([spike_time[1], spike_time[2]], label=["Beam 1", "Beam 2"]) ax.legend(loc="upper right") ax.set_xlabel("Delta t (s) from start of ramp till maximum OML") ax.set_ylabel("Fill count") plt.show()
def histogram_intensity_lost_during_OML(fills, beam): agg_fill = aggregate_fill(beam=beam, from_cache=True) t_agg = agg_fill.blm_ir3().x[agg_fill.OML_period()] lost_agg_period = np.empty(len(fills)) lost_fill_period = np.empty(len(fills)) total_lost_during_ramp = np.empty(len(fills)) for i, nbr in enumerate(fills): fill = Fill(nbr) a_index = fill.intensity().index_for_time(t_agg) int_agg = fill.intensity().y[a_index] lost_agg_period[i] = int_agg[0] - int_agg[1] f_index = fill.intensity().index_for_time( fill.blm_ir3().x[fill.OML_period()]) int_fill = fill.intensity().y[f_index] lost_fill_period[i] = int_fill[0] - int_fill[1] total_lost_during_ramp[i] = fill.intensity().y[0] - fill.intensity( ).y[-1] fig, ax = plt.subplots() ax.hist((lost_agg_period, lost_fill_period), label=("Aggregate OML period", "Fill OML period"), edgecolor='white') # ax.hist((lost_agg_period,), label=("Aggregate OML period",) , edgecolor='white') ax.legend(loc='upper right') ax.xaxis.set_major_formatter( FuncFormatter(lambda x, pos: "{0:.2f}".format(x * 100.0))) ax.set_xlabel("Intensity lost (%)") ax.set_ylabel("# fills") plt.title( "Intensity during off-momentum loss peak at start of ramp (beam {})". format(beam)) # ax.hist(lost_fill_period, edgecolor='white') fig2, ax2 = plt.subplots() ax2.hist(total_lost_during_ramp, edgecolor='white') ax2.xaxis.set_major_formatter( FuncFormatter(lambda x, pos: "{0:.2f}".format(x * 100.0))) ax2.set_xlabel("Intensity lost (%)") ax2.set_ylabel("# fills") plt.title("Total intensity lost during whole ramp (beam {})".format(beam)) plt.show()
def comp_blm_ir3_vs_intensity(file): fills = fills_from_file(file, "OML") intensity = [] mean_loss = [] max_loss = [] discarded = 0 for nbr in fills: fill = Fill(nbr, False) fill.fetch() smin, smax = fill.OML_period() ssubset = fill.blm_ir3().y[smin:smax] maxint = max(fill.intensity().y) if maxint < 1.8e14: discarded += 1 continue mean_loss.append(np.mean(ssubset)) max_loss.append(max(ssubset)) intensity.append(maxint) fig = plt.figure() ax1 = fig.add_subplot(121) ax2 = fig.add_subplot(122, sharey=ax1) ax1.set_xlabel("Mean momentum (IR3) TCP") ax1.set_ylabel("Intensity") ax1.scatter(mean_loss, intensity, color='b', label='mean') ax1.set_xlim([0, 1.1 * max(mean_loss)]) ax1.set_ylim([1.5e14, 1.1 * max(intensity)]) ax1.legend(loc="lower right") ax2.set_xlabel("Max momentum (IR3) TCP") ax2.set_ylabel("Intensity") ax2.scatter(max_loss, intensity, color='r', label='max') ax2.set_xlim([0, 1.1 * max(max_loss)]) ax2.legend(loc="lower right") percent_used = int( round(float(len(intensity)) / (len(intensity) + discarded) * 100)) fig.suptitle( "Intensity vs OML for {} (only intenities > 1.8e14, {}% of total)\n". format(file, percent_used)) plt.show()
def bar_graph_crossover_point(file): """ Bar graph displaying the 'time till max spike' + 'time where OML > TM' for all fills in file """ fills = fills_from_file(file, "OML") oml_dom_duration = [] time_till_spike = [] total_duration = [] odd = [] for nbr in fills: fill = Fill(nbr) crossover = fill.crossover_point() oml = fill.blm_ir3() ispike = np.argmax(oml.y) tspike = oml.x[ispike] time_till_spike.append(tspike) oml_dom_duration.append(crossover["t"] - tspike) total_duration.append(crossover['t']) if crossover['t'] > 40 or tspike > 12: # why am I using this? odd.append(nbr) print("odd looking fills: ", odd) fig, ax = plt.subplots() ax.bar(range(len(oml_dom_duration)), time_till_spike, label='Time to max peak (s)') ax.bar(range(len(oml_dom_duration)), oml_dom_duration, bottom=time_till_spike, label='Crossover point (s)') ax.legend(loc="upper right") ax.set_xlabel("Fill nbr") ax.set_ylabel("Duration (s)") plt.show()
def comp_blm_ir3_vs_abort_gap(file): fills = fills_from_file(file, "OML") abort_gap = [] average_loss = [] max_loss = [] for nbr in fills: fill = Fill(nbr, False) fill.fetch() smin, smax = fill.OML_period() # Only looking until t_co instead -- will not affect max smax = fill.crossover_point()['i'] tmax = fill.blm_ir3().x[smax] tmin = fill.blm_ir3().x[smin] # tmax = find_crossover_point(fill)['t'] ag_average = moving_average(fill.abort_gap().y, 5) agmin = fill.abort_gap().index_for_time(tmin) agmax = fill.abort_gap().index_for_time(tmax) ssubset = fill.blm_ir3().y[smin:smax] average_loss.append(np.average(ssubset)) max_loss.append(max(ssubset)) abort_gap.append(ag_average[agmin] - ag_average[agmax]) fig = plt.figure() ax1 = fig.add_subplot(121) ax2 = fig.add_subplot(122, sharey=ax1) # fig1, ax1 = plt.subplots() ax1.set_xlabel("Average BLM") ax1.set_ylabel("∆ abort gap intensity") ax1.scatter(average_loss, abort_gap, color='b', label='average') ax1.set_xlim([0, 1.1 * max(average_loss)]) ax1.set_ylim([0, 1.1 * max(abort_gap)]) xval = [0, 1] slope, intercept, r_value, p_value, std_err = stats.linregress( average_loss, abort_gap) print("Average fit") print( "\tk ={:>10.3E}\n\tm ={:>10.3E}\n\tr ={:>10.7f}\n\tp ={:>10.3E}\n\te^2={:>10.3E}" .format(slope, intercept, r_value, p_value, std_err)) yfit = [slope * x + intercept for x in xval] ax1.plot(xval, yfit, color='gray') ax1.legend(loc="lower right") # fig2, ax2 = plt.subplots() ax2.set_xlabel("Max BLM") ax2.scatter(max_loss, abort_gap, color='r', label='max') ax2.set_xlim([0, 1.1 * max(max_loss)]) ax2.legend(loc="lower right") slope, intercept, r_value, p_value, std_err = stats.linregress( max_loss, abort_gap) print("Max fit") print( "\tk ={:>10.3E}\n\tm ={:>10.3E}\n\tr ={:>10.7f}\n\tp ={:>10.3E}\n\te^2={:>10.3E}" .format(slope, intercept, r_value, p_value, std_err)) yfit = [slope * x + intercept for x in xval] ax2.plot(xval, yfit, color='gray') fig.suptitle( "Correlation between abort gap intensity and BLM signal for TCP in IR3" ) plt.show()
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()