Esempio n. 1
0
raw_data = data_dict['raw_data']
meta_data = data_dict['meta_data']

offset_index = -1

gaps = [10e-3, 9.94e-3]
streaker_offset = 372e-6
beam_offsets = [0, -(meta_data['offsets'][offset_index] - streaker_offset)]

tracker.set_simulator(raw_data['meta_data_begin'])

projx = raw_data['pyscan_result']['image'][offset_index].astype(
    np.float64).sum(axis=-2)
x_axis = raw_data['pyscan_result']['x_axis_m']
median_proj = misc.get_median(projx)
meas_screen = misc.proj_to_screen(median_proj, x_axis, True,
                                  meta_data['screen_x0'])

recon_kwargs['meas_screen'] = meas_screen
recon_kwargs['gaps'] = gaps
recon_kwargs['beam_offsets'] = beam_offsets
recon_kwargs['n_streaker'] = 1
recon_kwargs['method'] = 'centroid'
recon_kwargs['sig_t_range'] = np.array([5., 75.]) * 1e-15

#analysis.current_profile_rec_gauss(tracker, recon_kwargs)

profile_output = cProfile.run(
    'analysis.current_profile_rec_gauss(tracker, recon_kwargs)',
    filename='./profile.txt')

ms.show()
lims_neg = -0.5, 3
lims_pos = -2, 0.5
sp_pos.set_xlim(*lims_pos)
sp_neg.set_xlim(*lims_neg)

delta_offset = 0e-6

all_xt = []

for n_offset, offset in enumerate(offsets[:-1]):
    images = streaker_calib['raw_data']['pyscan_result']['image'][
        n_offset].astype(float)
    projections = images.sum(axis=-2)
    median_proj = misc.get_median(projections, method='gf_sigma')
    screen = misc.proj_to_screen(median_proj, x_axis, subtract_min=True)
    screen._xx = screen._xx - x0
    screen.cutoff(5e-2)
    screen.crop()
    screen.reshape(len_profile)

    beam_offsets = [0, -(offset - streaker_offset) + delta_offset]
    gaps = [10e-3, 10e-3]
    distance = (gaps[1] / 2. - abs(beam_offsets[1])) * np.sign(beam_offsets[1])

    forward_dict = tracker.matrix_forward(profile_from_blmeas, gaps,
                                          beam_offsets)
    screen_forward = forward_dict['screen']
    if offset == 0:
        continue
    elif offset > 0:
proj_x = np.sum(images, axis=-2).squeeze()
x_axis = result_dict['x_axis_m']
offsets = data_dict['streaker_offsets'].squeeze()
n_images = int(data_dict['n_images'])

centroids = np.zeros([len(offsets), n_images])
if n_images > 1:
    for n_o, n_i in itertools.product(range(len(offsets)), range(n_images)):
        this_proj = proj_x[n_o,n_i].copy()
        this_proj -= np.median(this_proj)
        this_proj[this_proj<0] = 0
        mask0 = np.abs(this_proj) < np.abs(this_proj).max()*0.05
        this_proj[mask0] = 0
        centroids[n_o,n_i] = np.sum(this_proj*x_axis) / np.sum(this_proj)
        if n_i == 0:
            screen = misc.proj_to_screen(this_proj, x_axis, True, 0)
            color = screen.plot_standard(sp_proj, label='%i' % (offsets[n_o]*1e3))[0].get_color()
            sp_proj.axvline(centroids[n_o,n_i]*1e3, color=color, ls='--')

    centroid_mean = np.median(centroids, axis=1)
    centroid_std = np.std(centroids, axis=1)
elif n_images == 1:
    for n_o in range(len(offsets)):
        centroids[n_o] = np.sum(proj_x[n_o]*x_axis) / np.sum(proj_x[n_o])
    centroid_mean = centroids.squeeze()
    centroid_std = None

streaker = data_dict['streaker']
semigap = data_dict['meta_data'][streaker+':GAP']/2.*1e-3

wall0, wall1 = -semigap, semigap
#
#    sp = subplot(sp_ctr, title='Offset=%.2f mm' % (streaker_offsets[n_offset]*1e3), scix=True, sciy=True, xlabel='x axis')
#    sp_ctr += 1
#
#    for n_image in range(images.shape[1]):
#        image = images[n_offset, n_image]
#        projx = image.sum(axis=-2)
#        sp.plot(x_axis-screen0, projx)

screen_cutoff = 5e-2

rms_arr = np.zeros([n_offsets, n_images])
for n_offset in range(n_offsets):
    for n_image in range(n_images):
        proj = np.sum(images[n_offset, n_image], axis=-2)
        screen = misc.proj_to_screen(proj, x_axis, True, 555e-6)
        screen.cutoff2(screen_cutoff)
        screen.crop()
        screen.reshape(int(1e3))
        rms_arr[n_offset, n_image] = screen.rms()

sp = plot_handles[0].twinx()
sp.set_ylabel('RMS beamsizes [mm]')

rms_median = np.median(rms_arr, axis=1)


def fit_func(xx, mean, strength):
    return (xx - mean)**2 * strength + rms_median[6]

sp_ctr += 1

sp_screen_neg = subplot(sp_ctr,
                        title='Screen distributions',
                        xlabel='x (mm)',
                        ylabel='intensity (arb. units)')
sp_ctr += 1

for n_offset, offset in enumerate(offsets):

    label = '%.2f mm' % (offset * 1e3)
    projections = projx[n_offset]
    median_proj = misc.get_median(projections, method='std')

    screen = misc.proj_to_screen(median_proj,
                                 x_axis,
                                 True,
                                 x_offset=screen_center)
    screen.cutoff2(screen_cutoff)
    screen.crop()
    screen.reshape(len_profile)

    if offset == 0:
        for sp_screen in sp_screen_pos, sp_screen_neg:
            screen.plot_standard(sp_screen, label=label)
        continue
    elif offset > 0:
        sp_screen = sp_screen_pos
    elif offset < 0:
        sp_screen = sp_screen_neg

    color = screen.plot_standard(sp_screen, label=label)[0].get_color()
for streaker_offset_mm in all_offsets:

    if streaker_offset_mm > 0:
        sp_screen, sp_profile = sp_screen0, sp_profile0
    if streaker_offset_mm < 0:
        sp_screen, sp_profile = sp_screen1, sp_profile1

    streaker_offset = streaker_offset_mm * 1e-3

    file_off0 = offset_file_dict['OFF'][streaker_offset_mm]
    file_off = data_dir + os.path.basename(file_off0)
    dict_off = loadH5Recursive(file_off)['pyscan_result']
    all_proj = dict_off['image'].astype(float).sum(axis=-2)
    x_axis = dict_off['x_axis'].astype(float)
    median_proj = misc.get_median(all_proj)
    meas_screen = misc.proj_to_screen(median_proj, x_axis, True, screen_x0)

    gauss_kwargs2 = copy.deepcopy(gauss_kwargs)
    gauss_kwargs2['beam_offsets'] = [0, -(streaker_offset - streaker_center)]
    del gauss_kwargs2['meas_screen_intensity']
    del gauss_kwargs2['meas_screen_x']
    gauss_kwargs2['meas_screen'] = meas_screen
    gauss_kwargs2['sig_t_range'] = np.arange(20, 50.01, 5) * 1e-15
    gauss_dict = tracker.find_best_gauss(**gauss_kwargs2)

    profile = gauss_dict['reconstructed_profile']
    screen = gauss_dict['reconstructed_screen']

    _label = '%.2f mm %i fs' % (streaker_offset_mm, profile.rms() * 1e15)
    profile.plot_standard(sp_profile, label=_label, center='Gauss')