Example #1
0
def main():
    pm = PixelMasks()
    dead = np.where(np.logical_or(pm.dead, np.repeat(pm.bad_hv, 4)))[0]

    bright_path = get_astri_2019("d2019-04-23_nudges/bright_50pe/charge.h5")
    with HDF5Reader(bright_path) as reader:
        df = reader.read("data").groupby(['nudge',
                                          'pixel']).mean().reset_index()
        mapping = reader.get_mapping()

    df_0 = df.loc[df['nudge'] == 0]
    isin_dead = df_0['pixel'].isin(dead)
    avg = df_0.loc[~isin_dead].mean()
    ff = avg['onsky_calib'] / df_0['onsky_calib'].values
    ff[dead] = 1

    np.testing.assert_allclose(ff.mean(), 1, rtol=1e-2)

    df = pd.DataFrame(dict(
        pixel=np.arange(ff.size),
        ff=ff,
    ))

    output_dir = get_astri_2019("d2019-04-23_nudges/results/extract_ff")
    cm = CameraImage.from_mapping(mapping)
    cm.image = ff
    cm.add_colorbar()
    cm.highlight_pixels(pm.dead, 'red')
    cm.highlight_pixels(np.repeat(pm.bad_hv, 4), 'black')
    cm.highlight_pixels(pm.low, 'blue')
    cm.save(join(output_dir, "ff_camera.pdf"))

    outpath = get_calib_data("ff_coeff.dat")
    df.to_csv(outpath, sep='\t', index=False)
    print(f"Created ff_coeff file: {outpath}")
Example #2
0
def calculate_t_cor(pixel, t_pulse):
    t_pulse_camera = np.zeros(2048)
    t_pulse_camera[pixel] = t_pulse
    pm = PixelMasks()
    t_pulse_camera[pm.all_mask] = np.nan
    t_pulse_camera[t_pulse_camera == 0] = np.nan
    t_cor_tm = np.nanmean(t_pulse_camera.reshape((32, 64)), 0)
    t_cor = t_cor_tm.mean() - t_cor_tm
    t_cor = np.tile(t_cor, 32).ravel()
    t_cor[:2048 // 2] -= 0.76
    t_cor[2048 // 2:] += 0.76
    return t_cor
Example #3
0
def main():
    pm = PixelMasks()
    dead = np.where(np.logical_or(pm.dead, np.repeat(pm.bad_hv, 4)))[0]

    ref_path = CameraConfiguration("1.1.0").GetReferencePulsePath()
    cc = CrossCorrelation(1, 96, reference_pulse_path=ref_path)

    spe_path = get_astri_2019('d2019-04-23_nudges/spe_+0.h5')
    with pd.HDFStore(spe_path) as store:
        coeff = store['coeff_pixel']
        coeff = coeff.loc[~coeff['pixel'].isin(dead)]

    spe = cc.get_pulse_height(np.median(coeff['spe']))
    spe_sigma = cc.get_pulse_height(np.median(coeff['spe_sigma']))
    opct = np.median(coeff['opct'])

    output_dir = get_plot("d190716_simtel_cfg")
    generate_spectrum.call(output_dir, spe, spe_sigma, opct, 0, 0)
Example #4
0
def main():
    pm = PixelMasks()
    dead = np.where(np.logical_or(pm.dead, np.repeat(pm.bad_hv, 4)))[0]

    poi = 811
    assert poi not in dead

    spe_config = join(DIR, "spe_config.yml")
    df_spe = pd.read_csv(join(DIR, "spe_runlist.txt"), sep='\t')
    for _, row in df_spe.iterrows():
        nudge = row['nudge']
        spe_paths = [
            join(DATA, f"spe_0.5pe/{row['spe_0.5pe']}"),
            join(DATA, f"spe_0.8pe/{row['spe_0.8pe']}"),
            join(DATA, f"spe_1.1pe/{row['spe_1.1pe']}"),
            join(DATA, f"spe_1.7pe/{row['spe_1.7pe']}"),
            join(DATA, f"spe_2.4pe/{row['spe_2.4pe']}"),
        ]
        output_path = join(DATA, f"spe_{nudge:+d}.pdf")
        process(spe_paths, spe_config, output_path, poi)
def main():
    pixel_mask_path = get_astri_2019(
        # "d2019-04-23_nudges/mc/mc_chec_pixel_mask_190521.dat"
        "d2019-04-23_nudges/mc_191011/mc_chec_pixel_mask.dat")
    pm = PixelMasks(pixel_mask_path)
    dead = np.where(pm.all_mask)[0]

    path = get_astri_2019("d2019-04-23_nudges/mc/charge.h5")
    output_path = get_calib_data("charge2photons_simtel.yml")
    pde = 0.25

    with HDF5Reader(path) as reader:
        df = reader.read("data")
        df = df.loc[df['mc_true'] > 5]
        isin_dead = df['pixel'].isin(dead)
        df = df.loc[~isin_dead]

    measured = df['onsky'].values
    true = df['mc_true'].values / pde

    coeff = polyfit(true, measured, [1])
    _, charge2photons = coeff

    print(f"charge2photons = {charge2photons}")

    results_dir = get_astri_2019(
        "d2019-04-23_nudges/results/extract_charge2photons_mc")

    p_calib = CalibPlotter()
    p_calib.plot(true, measured, coeff)
    p_calib.save(join(results_dir, "calib.pdf"))

    output = dict(charge2photons=float(charge2photons), )
    with open(output_path, 'w') as outfile:
        yaml.dump(output, outfile, default_flow_style=False)
    print(f"Created charge2photons file: {output_path}")
def main():
    pm = PixelMasks()
    dead = np.where(np.logical_or(pm.dead, np.repeat(pm.bad_hv, 4)))[0]

    bright_path = get_astri_2019("d2019-04-23_nudges/bright_50pe/charge.h5")
    with HDF5Reader(bright_path) as reader:
        df_bright = reader.read("data")
        isin_dead = df_bright['pixel'].isin(dead)
        df_bright = df_bright.loc[~isin_dead]

    spe_path = get_astri_2019("d2019-04-23_nudges/results/process_spe/spe.csv")
    df_spe = pd.read_csv(spe_path)

    illumination = calculate_illumination(df_bright, df_spe)

    d_list = []

    for nudge, group in df_bright.groupby("nudge"):
        temperatures = group['temperature'].values
        assert (temperatures == temperatures[0]).all()
        temperature = temperatures[0]

        calibrator = OnskyAmplitudeCalibrator(nudge, temperature)

        pixel = group['pixel'].values
        charge = group['onsky_calib'].values
        charge_cal = calibrator(charge, pixel)

        group_avg = group.groupby('pixel').mean()
        pixel_avg = group_avg.index.values
        charge_avg = group_avg['onsky_calib'].values
        charge_avg_cal = calibrator(charge_avg, pixel_avg)

        rmse = np.sqrt(
            np.sum((illumination - charge_cal)**2) / charge_cal.size)

        d_list.append(
            dict(
                nudge=nudge,
                mean=charge_cal.mean(),
                std=charge_cal.std(),
                mean_avg=charge_avg_cal.mean(),
                std_avg=charge_avg_cal.std(),
                rmse=rmse,
            ))

    df = pd.DataFrame(d_list)

    output_dir = get_astri_2019("d2019-04-23_nudges/results/plot_residuals")

    p_cal = CalibratedvsNudge("DAC Nudge", "Calibrated Charge (photons)")
    p_cal.plot(df['nudge'], df['mean'], df['std'])
    p_cal.ax.axhline(illumination, color='red', alpha=0.2, lw=0.2)
    p_cal.save(join(output_dir, "calibrated_vs_nudge.pdf"))

    p_avg = CalibratedvsNudge("DAC Nudge", "Average Charge (photons)")
    p_avg.plot(df['nudge'], df['mean_avg'], df['std_avg'])
    p_avg.ax.axhline(illumination, color='red', alpha=0.2, lw=0.2)
    p_avg.save(join(output_dir, "calibrated_vs_nudge_avg.pdf"))

    p_rmse = CalibratedvsNudge("DAC Nudge", f"RMSE {illumination:.2f} photons")
    p_rmse.plot(df['nudge'], df['rmse'], None)
    p_rmse.save(join(output_dir, "rmse_vs_nudge.pdf"))
Example #7
0
def main():
    paths = dict(
        d190502=
        "/Volumes/ICYBOX/astri_onsky_archive/d2019-06-10_ledflashers/flasher_comparison_May-June/unit0pattern-low_r1.tio",
        d190508=
        "/Volumes/ICYBOX/astri_onsky_archive/d2019-06-10_ledflashers/flasher_comparison_May-June/Run13270_r1.tio",
        d190610=
        "/Volumes/ICYBOX/astri_onsky_archive/d2019-06-10_ledflashers/flasher_comparison_May-June/Run13401_r1.tio",
    )
    readers = {
        key: TIOReader(path, max_events=100)
        for key, path in paths.items()
    }
    waveforms = {key: reader[:].mean(0) for key, reader in readers.items()}
    mapping = get_superpixel_mapping(readers['d190502'].mapping)

    pixelmasks = dict(d190428T2149=PixelMasks(
        get_calib_data("pixel_masks/d190428T2149.dat")), )

    # Calculate coeff
    waveforms_coeff = waveforms['d190502']
    n_pixels, n_samples = waveforms_coeff.shape
    n_sp = n_pixels // 4
    waveforms_sp = waveforms_coeff.reshape((n_sp, 4, n_samples)).sum(1)
    coeff = (waveforms_sp.max(1).mean() / waveforms_sp.max(1))[:, None]

    sp_waveforms = {
        key: wfs.reshape((n_sp, 4, n_samples)).sum(1)
        for key, wfs in waveforms.items()
    }
    sp_waveforms_cal = {key: wfs * coeff for key, wfs in sp_waveforms.items()}

    trigger_off = dict()
    for key, wfs in sp_waveforms_cal.items():
        amplitude = wfs.max(1)
        median = np.median(amplitude)
        threshold = median * 1.10
        trigger_off[key] = amplitude > threshold

    trigger_off['d190610'][[
        355, 356, 357, 358, 359, 382, 375, 460, 459, 457, 458, 465, 289, 489,
        502, 254, 247, 154, 144, 56, 46, 39, 24, 25, 76
    ]] = True

    for key, sp in trigger_off.items():
        print(f"{key}: f{np.where(sp)}")

    plots = GetPlot(mapping)
    plots['d190428T2149'].highlight_pixels(trigger_off['d190502'])
    plots['d190428T2149'].plot_waveforms(sp_waveforms['d190502'])
    plots['d190428T2149'].save(get_plot("d190611_led_flasher/led/d190502.pdf"))
    plots['d190428T2149'].plot_waveforms(sp_waveforms_cal['d190502'])
    plots['d190428T2149'].save(
        get_plot("d190611_led_flasher/coeff/d190502.pdf"))
    plots['d190428T2149'].reset_pixel_highlight()

    plots['d190428T2149'].highlight_pixels(trigger_off['d190508'])
    plots['d190428T2149'].plot_waveforms(sp_waveforms['d190508'])
    plots['d190428T2149'].save(get_plot("d190611_led_flasher/led/d190508.pdf"))
    plots['d190428T2149'].plot_waveforms(sp_waveforms_cal['d190508'])
    plots['d190428T2149'].save(
        get_plot("d190611_led_flasher/coeff/d190508.pdf"))
    plots['d190428T2149'].reset_pixel_highlight()

    plots['d190428T2149'].highlight_pixels(trigger_off['d190610'])
    plots['d190428T2149'].plot_waveforms(sp_waveforms['d190610'])
    plots['d190428T2149'].save(get_plot("d190611_led_flasher/led/d190610.pdf"))
    plots['d190428T2149'].plot_waveforms(sp_waveforms_cal['d190610'])
    plots['d190428T2149'].save(
        get_plot("d190611_led_flasher/coeff/d190610.pdf"))
    plots['d190428T2149'].reset_pixel_highlight()

    hv_off = [24, 453]
    hv_off_array = np.zeros(n_sp, dtype=bool)
    hv_off_array[hv_off] = True
    trigger_off_array = np.zeros(n_sp, dtype=bool)
    trigger_off_array[trigger_off['d190610']] = True
    df = pd.DataFrame(
        dict(
            superpixel=np.arange(n_sp),
            hv_on=(~hv_off_array).astype(int),
            trigger_off=trigger_off_array.astype(int),
        ))
    df.to_csv(get_plot("d190611_led_flasher/hv_list.txt"),
              sep='\t',
              index=False)
Example #8
0
def main():
    path = "/Users/Jason/Data/d2019-04-23_nudges/bright_50pe/Run09095_r1.tio"
    reader = TIOReader(path)
    n_events = reader.n_events
    n_pixels = reader.n_pixels
    n_samples = reader.n_samples
    pixel_array = np.arange(n_pixels)
    time_calibrator = TimeCalibrator()
    extractor = Timing(n_pixels, n_samples)
    df_list = []
    for wfs in tqdm(reader, total=n_events):
        iev = wfs.iev
        shifted = time_calibrator(wfs)

        params = extractor.process(wfs)
        params_shifted = extractor.process(shifted)

        df_list.append(
            pd.DataFrame(
                dict(
                    iev=iev,
                    pixel=pixel_array,
                    t_pulse=params['t_pulse'],
                    t_pulse_corrected=params_shifted['t_pulse'],
                )))

    df = pd.concat(df_list, ignore_index=True)

    pm = PixelMasks()
    dead = np.where(pm.all_mask)
    mask = ~np.isin(df['pixel'].values, dead)
    df = df.iloc[mask]
    df_ev = df.groupby('iev').mean()
    mean_timing = np.repeat(df_ev['t_pulse'], n_pixels - pm.all_mask.sum())
    mean_timing_c = np.repeat(df_ev['t_pulse'], n_pixels - pm.all_mask.sum())
    df['t_pulse'] -= mean_timing.values
    df['t_pulse_corrected'] -= mean_timing_c.values
    df_pix = df.groupby('pixel').mean()
    pixel = df_pix.index.values
    t_pulse = df_pix['t_pulse'].values
    t_pulse_corrected = df_pix['t_pulse_corrected'].values

    image = np.full(n_pixels, np.nan)
    image[pixel] = t_pulse
    mean = np.nanmean(image)
    std = np.nanstd(image)
    ci = CameraImage.from_mapping(reader.mapping)
    ci.image = image
    ci.add_colorbar()
    ci.ax.set_title(f"MEAN = {mean:.2f}, STDDEV = {std:.2f}")
    ci.save(join(DIR, "before.pdf"))

    image = np.full(n_pixels, np.nan)
    image[pixel] = t_pulse_corrected
    mean = np.nanmean(image)
    std = np.nanstd(image)
    ci = CameraImage.from_mapping(reader.mapping)
    ci.image = image
    ci.add_colorbar()
    ci.ax.set_title(f"MEAN = {mean:.2f}, STDDEV = {std:.2f}")
    ci.save(join(DIR, "after.pdf"))
def main():
    pm = PixelMasks()
    dead = np.where(np.logical_or(pm.dead, np.repeat(pm.bad_hv, 4)))[0]

    bright_path = get_astri_2019("d2019-04-23_nudges/bright_50pe/charge.h5")
    with HDF5Reader(bright_path) as reader:
        df_bright = reader.read("data").groupby(['nudge', 'pixel'
                                                 ]).mean().reset_index()
        isin_dead = df_bright['pixel'].isin(dead)
        df_bright = df_bright.loc[~isin_dead]

    spe_path = get_astri_2019("d2019-04-23_nudges/results/process_spe/spe.csv")
    df_spe = pd.read_csv(spe_path)

    illumination = calculate_illumination(df_bright, df_spe)
    cc2height = calculate_cc2height(df_bright)
    print(f"charge2height = {cc2height}")

    nudges = np.unique(df_bright['nudge'])
    # nudges = np.delete(nudges, np.where(np.isin(nudges, [5, 25])))
    mv2pe = np.zeros(nudges.size)
    charge2photons = np.zeros(nudges.size)
    pde = np.zeros(nudges.size)

    for inudge, nudge in enumerate(nudges):
        df_bright_nudge = df_bright.loc[df_bright['nudge'] == nudge].mean()
        df_spe_nudge = df_spe.loc[df_spe['nudge'] == nudge]

        cc2pe = df_spe_nudge['cc2pe'].iloc[0]
        mv2pe[inudge] = cc2pe / cc2height

        charge_onsky = df_bright_nudge['onsky_calib']
        charge2photons[inudge] = charge_onsky / illumination

        charge_cc = df_bright_nudge['cc']
        cc2photons = charge_cc / illumination
        pde[inudge] = cc2photons / cc2pe

    mask = ~np.isin(nudges, [5, 25])
    mv2pe_coeff = polyfit(nudges[mask], mv2pe[mask], 3)
    charge2photons_coeff = polyfit(nudges, charge2photons, 3)
    min_ = nudges.min()
    max_ = nudges.max()

    output_dir = get_astri_2019(
        "d2019-04-23_nudges/results/extract_charge2photons")

    p_mv2pe = Mv2pePlotter()
    p_mv2pe.plot(nudges[mask], mv2pe[mask], mv2pe_coeff, min_, max_)
    p_mv2pe.save(join(output_dir, "mv2pe.pdf"))

    p_mv2pe_cc = Mv2pePlotter()
    p_mv2pe_cc.plot(nudges, charge2photons, charge2photons_coeff, min_, max_)
    p_mv2pe_cc.ax.set_ylabel("Charge (mVns) per photon")
    p_mv2pe_cc.save(join(output_dir, "charge2photons.pdf"))

    p_pde = PDEPlotter()
    p_pde.plot(nudges[mask], pde[mask])
    p_pde.save(join(output_dir, "pde.pdf"))

    output = dict(
        charge2photons_coeff=charge2photons_coeff.tolist(),
        nudge_min=-40,  #int(nudges.min()),
        nudge_max=int(nudges.max()),
    )
    outpath = get_calib_data('charge2photons.yml')
    with open(outpath, 'w') as outfile:
        yaml.dump(output, outfile, default_flow_style=False)
    print(f"Created charge2photons file: {outpath}")
Example #10
0
def main():
    pm = PixelMasks()
    dead = np.where(np.logical_or(pm.dead, np.repeat(pm.bad_hv, 4)))[0]

    spe_paths = glob(join(DATA, "spe_*.h5"))
    pattern = re.compile(r"(?:.+?)/spe_(.+?).h5")

    d_list = []

    for path in spe_paths:
        nudge = int(re.search(pattern, path).group(1))

        with pd.HDFStore(path) as store:
            metadata = store.get_storer('metadata').attrs.metadata
            coeff = store['coeff_pixel']
            errors = store['errors_pixel']

        coeff = coeff.loc[~coeff['pixel'].isin(dead)]
        errors = errors.loc[~errors['pixel'].isin(dead)]
        coeff_average = coeff.mean()
        errors_average = errors.mean()
        n_illuminations = metadata['n_illuminations']

        cc = np.zeros(n_illuminations)
        lambda_ = np.zeros(n_illuminations)
        lambda_error = np.zeros(n_illuminations)
        for ill in range(n_illuminations):
            eped = coeff_average['eped']
            cc[ill] = coeff_average[f'charge_average{ill}'] - eped
            lambda_[ill] = coeff_average[f'lambda_{ill}']
            lambda_error[ill] = errors_average[f'lambda_{ill}']
        cc2pe_ped, cc2pe = polyfit(lambda_, cc, [1], w=1 / lambda_error)

        p_ccvslambda = CCVsLambda()
        p_ccvslambda.plot(cc, lambda_, lambda_error, cc2pe_ped, cc2pe)
        plot_path = join(RESULTS, f"cc_vs_lambda_{nudge:+d}.pdf")
        p_ccvslambda.save(plot_path)

        d_list.append(
            dict(
                nudge=nudge,
                opct=coeff_average['opct'],
                opct_err=errors_average['opct'],
                spe=coeff_average['spe'],
                spe_err=errors_average['spe'],
                spe_sigma=coeff_average['spe_sigma'],
                spe_sigma_err=errors_average['spe_sigma'],
                eped=coeff_average['eped'],
                eped_err=errors_average['eped'],
                eped_sigma=coeff_average['eped_sigma'],
                eped_sigma_err=errors_average['eped_sigma'],
                cc2pe=cc2pe,
                cc2pe_ped=cc2pe_ped,
            ))

    df = pd.DataFrame(d_list)

    output_path = join(RESULTS, "spe.csv")
    df.to_csv(output_path, index=False)

    nudge = df['nudge'].values

    y = df['opct'].values
    yerr = df['opct_err'].values
    ylabel = "Optical Crosstalk"
    plot_path = join(RESULTS, f"opct.pdf")
    p_params = ParamPlotter(ylabel)
    p_params.plot(nudge, y, yerr)
    p_params.save(plot_path)

    y = df['spe'].values
    yerr = df['spe_err'].values
    ylabel = "Gain (cc mVns)"
    plot_path = join(RESULTS, f"spe.pdf")
    p_params = ParamPlotter(ylabel)
    p_params.plot(nudge, y, yerr)
    p_params.save(plot_path)

    y = df['eped'].values
    yerr = df['eped_err'].values
    ylabel = "Eped (cc mVns)"
    plot_path = join(RESULTS, f"eped.pdf")
    p_params = ParamPlotter(ylabel)
    p_params.plot(nudge, y, yerr)
    p_params.save(plot_path)

    y = df['cc2pe_ped'].values
    yerr = None
    ylabel = "cc2pe Pedestal (cc mVns)"
    plot_path = join(RESULTS, f"cc2pe_pedestal.pdf")
    p_params = ParamPlotter(ylabel)
    p_params.plot(nudge, y, yerr)
    p_params.save(plot_path)

    y = df['cc2pe'].values
    yerr = None
    ylabel = "cc2pe (cc mVns / p.e.)"
    plot_path = join(RESULTS, f"cc2pe.pdf")
    p_params = ParamPlotter(ylabel)
    p_params.plot(nudge, y, yerr)
    p_params.save(plot_path)