Ejemplo n.º 1
0
def create_new_mapping(input_path, output_path):
    """
    Create a TargetCalib Mapping file with the latest pixel positions,
    but the prod3 pixel ordering
    """
    create_directory(os.path.dirname(output_path))

    df = pd.read_csv(input_path, sep='\t')
    df_new = df.sort_values(['row', 'col'])
    df_new['pixel'] = np.arange(2048)

    sp_ordering = df_new.groupby('superpixel').min().sort_values(
        'pixel').index.values
    lookup = np.argsort(sp_ordering)
    df_new['superpixel'] = lookup[df_new['superpixel'].values]

    slot_ordering = df_new.groupby('slot').min().sort_values(
        'pixel').index.values
    lookup = np.argsort(slot_ordering)
    df_new['slot'] = lookup[df_new['slot'].values]

    for itm in range(32):
        lookup = np.argsort(df_new.loc[df_new['slot'] == itm]['pixel'].values)
        df_new.loc[df_new['slot'] == itm, 'tmpix'] = lookup

    df_new.to_csv(output_path, sep='\t', float_format='%.7f', index=False)
def main():
    description = (
        "Generate the pedestals from an R0 file, subtract it from another "
        "R0 file, and plot the comparison of residuals from different "
        "pedestal methods")
    parser = argparse.ArgumentParser(description=description,
                                     formatter_class=Formatter)
    parser.add_argument('-f',
                        '--file',
                        dest='r0_path',
                        required=True,
                        help='R0 file to obtain residuals from')
    parser.add_argument('-p',
                        '--pedestal',
                        dest='pedestal_r0_path',
                        required=True,
                        help='R0 file to generate pedestal from')
    parser.add_argument('-o',
                        '--output',
                        dest='output_dir',
                        required=True,
                        help='directort to store output plots')
    args = parser.parse_args()

    r0_path = args.r0_path
    pedestal_r0_path = args.pedestal_r0_path
    output_dir = args.output_dir

    create_directory(output_dir)
    reader_ped = TIOReader(pedestal_r0_path, max_events=100000)
    reader_res = TIOReader(r0_path, max_events=1000)

    # Generate Pedestals
    pedestal = PedestalTargetCalib(reader_ped.n_pixels, reader_ped.n_samples,
                                   reader_ped.n_cells)
    desc = "Generating pedestal"
    for wfs in tqdm(reader_ped, total=reader_ped.n_events, desc=desc):
        if wfs.missing_packets:
            continue
        pedestal.add_to_pedestal(wfs, wfs.first_cell_id)

    channel_stats = PixelStats(reader_res.n_pixels)

    # Subtract Pedestals
    desc = "Subtracting pedestal"
    for wfs in tqdm(reader_res, total=reader_res.n_events, desc=desc):
        if wfs.missing_packets:
            continue

        subtracted_tc = pedestal.subtract_pedestal(wfs, wfs.first_cell_id)
        channel_stats.add_to_stats(subtracted_tc)

    # Plot results
    p_channel_std = ChannelStd()
    p_channel_std.plot(channel_stats.std)
    p_channel_std.save(join(output_dir, "channel_std.pdf"))

    p_ci_stats = CameraStats(reader_res.mapping)
    p_ci_stats.set_image(channel_stats.mean, channel_stats.std)
    p_ci_stats.save(join(output_dir, f"ci_stats.pdf"))
Ejemplo n.º 3
0
def main():
    parser = argparse.ArgumentParser(
        description="Generate Camera configuration files")
    parser.add_argument('-o', dest='output', help='Output directory')
    args = parser.parse_args()
    output_dir = args.output

    create_directory(output_dir)

    n_samples = 128
    width = 14
    sigma = width / 2.355

    for opct in np.linspace(0, 0.5, 6):
        for mv_per_pe in np.linspace(0.4, 4, 5):
            for nsb in np.linspace(0, 50, 5):
                pulse = GaussianPulse(20, sigma, 40, mv_per_pe=mv_per_pe)
                spe = SiPMPrompt(opct=opct, normalise_charge=False)
                coupling = ACOffsetCoupling(pulse_area=pulse.area,
                                            spectrum_average=spe.average)
                camera = Camera(
                    mapping=SSTCameraMapping(),
                    photoelectron_spectrum=spe,
                    photoelectron_pulse=pulse,
                    coupling=coupling,
                    n_waveform_samples=n_samples,
                    continuous_readout_duration=n_samples,
                    readout_noise=GaussianNoise(stddev=0.5),
                    digitisation_noise=GaussianNoise(stddev=1),
                )
                camera.attach_metadata("nsb", nsb)
                name = f"camera_{opct:.2f}_{mv_per_pe:.2f}_{nsb:.2f}.pkl"
                camera.save(join(output_dir, name))
Ejemplo n.º 4
0
def main():
    parser = argparse.ArgumentParser(
        description="Generate Camera configuration files")
    parser.add_argument('-o', dest='output', help='Output directory')
    args = parser.parse_args()
    output_dir = args.output

    create_directory(output_dir)

    n_samples = 96
    spe = SiPMPrompt(opct=0.1, normalise_charge=False)
    camera_kwargs = dict(
        mapping=SSTCameraMapping(),
        photoelectron_spectrum=spe,
        n_waveform_samples=n_samples,
        continuous_readout_duration=n_samples,
        readout_noise=GaussianNoise(stddev=2),
        digitisation_noise=GaussianNoise(stddev=1),
    )

    for mv_per_pe in [0.5, 0.8, 1.1, 1.375, 2.25, 3.125, 4]:
        for width in [2, 4, 6, 8, 10, 14, 20]:
            sigma = width / 2.355
            pulse = GaussianPulse(30, sigma, 60, mv_per_pe=mv_per_pe)
            coupling = ACOffsetCoupling(pulse_area=pulse.area,
                                        spectrum_average=spe.average)
            camera = Camera(**camera_kwargs,
                            photoelectron_pulse=pulse,
                            coupling=coupling)

            name = f"width_{width:.2f}_height_{mv_per_pe:.2f}.pkl"
            camera.save(join(output_dir, name))
Ejemplo n.º 5
0
def main():
    parser = argparse.ArgumentParser(
        description="Generate Camera configuration files")
    parser.add_argument('-o', dest='output', help='Output directory')
    args = parser.parse_args()
    output_dir = args.output

    create_directory(output_dir)

    # Define camera
    n_samples = 128
    width = 8  # ns
    sigma = width / (2 * np.sqrt(2 * np.log(2)))
    pulse = GaussianPulse(mean=15, sigma=sigma, duration=30, mv_per_pe=4)
    camera_kwargs = dict(
        photoelectron_pulse=pulse,
        n_waveform_samples=n_samples,
        continuous_readout_duration=n_samples,
        digitisation_noise=GaussianNoise(stddev=1),
    )

    # from scipy.signal import find_peaks, peak_widths
    #
    # def _extract_widths(pulse_y):
    #     peaks, _ = find_peaks(pulse_y)
    #     return peak_widths(pulse_y, peaks)
    #
    # def extract_width(pulse_x, pulse_y):
    #     sample_width = pulse_x[1] - pulse_x[0]
    #     pulse_width = _extract_widths(pulse_y)[0][0] * sample_width
    #
    #     undershoot_widths = _extract_widths(-pulse_y)
    #     if len(undershoot_widths[0]) == 0 :
    #         undershoot_width = 0
    #     else:
    #         undershoot_width = undershoot_widths[0][-1] * sample_width
    #     return pulse_width, undershoot_width
    #
    # from matplotlib import pyplot as plt
    # plt.plot(pulse.time, pulse.amplitude)
    # plt.show()
    # width_pos, width_neg = extract_width(pulse.time, pulse.amplitude)

    # print(width_pos, width_neg)

    time_constant_list = [0, 5, 10, 20, 50, 100, 200, 1000]
    opct_list = np.linspace(0.01, 0.99, 50)
    for time_constant in time_constant_list:
        for opct in opct_list:
            spe = SiPMDelayed(opct=opct,
                              time_constant=time_constant,
                              normalise_charge=False)
            coupling = ACOffsetCoupling(pulse_area=pulse.area,
                                        spectrum_average=spe.average)
            camera = Camera(**camera_kwargs,
                            photoelectron_spectrum=spe,
                            coupling=coupling)
            name = f"opct{opct:.2f}_tc{time_constant:.0f}.pkl"
            camera.save(join(output_dir, name))
Ejemplo n.º 6
0
def main():
    description = (
        "Generate the pedestals from an R0 file, subtract it from another "
        "R0 file, and plot the comparison of residuals from different "
        "pedestal methods"
    )
    parser = argparse.ArgumentParser(description=description,
                                     formatter_class=Formatter)
    parser.add_argument('-f', '--file', dest='r0_path', required=True,
                        help='R0 file to obtain residuals from')
    parser.add_argument('-p', '--pedestal', dest='pedestal_r0_path',
                        required=True,
                        help='R0 file to generate pedestal from')
    parser.add_argument('-o', '--output', dest='output_dir', required=True,
                        help='directort to store output plots')
    args = parser.parse_args()

    r0_path = args.r0_path
    pedestal_r0_path = args.pedestal_r0_path
    output_dir = args.output_dir

    create_directory(output_dir)
    reader_ped = TIOReader(pedestal_r0_path, max_events=100000)
    reader_res = TIOReader(r0_path, max_events=100000)

    # Generate Pedestals
    pedestal = PedestalTargetCalib(
        reader_ped.n_pixels, reader_ped.n_samples, reader_ped.n_cells
    )
    desc = "Generating pedestal"
    for wfs in tqdm(reader_ped, total=reader_ped.n_events, desc=desc):
        if wfs.missing_packets:
            continue
        pedestal.add_to_pedestal(wfs, wfs.first_cell_id)

    online_stats = OnlineStats()
    online_hist = OnlineHist(bins=100, range_=(-10, 10))

    # Subtract Pedestals
    desc = "Subtracting pedestal"
    for wfs in tqdm(reader_res, total=reader_res.n_events, desc=desc):
        if wfs.missing_packets:
            continue

        subtracted_tc = pedestal.subtract_pedestal(wfs, wfs.first_cell_id)
        online_stats.add_to_stats(subtracted_tc)
        online_hist.add(subtracted_tc)

    p_hist = HistPlot()
    p_hist.plot(
        online_hist.hist, online_hist.edges, online_stats.mean, online_stats.std,
    )
    p_hist.save(join(output_dir, "hist.pdf"))
Ejemplo n.º 7
0
def main():
    parser = argparse.ArgumentParser(description="Generate Camera configuration files")
    parser.add_argument('-o', dest='output', help='Output directory')
    args = parser.parse_args()
    output_dir = args.output

    create_directory(output_dir)

    # Define camera (CHEC-S)
    n_samples = 128
    camera_kwargs = dict(
        photoelectron_spectrum=SiPMGentileSPE(x_max=20, spe_sigma=0.12, opct=0.4),
        n_waveform_samples=n_samples,
        continuous_readout_duration=n_samples,
        readout_noise=GaussianNoise(stddev=0.15),
    )

    x = np.linspace(0, 128, 1000)

    n_sigma0 = 35
    n_sigma1 = 40
    ratio_values = [0.2, 0.4, 0.6, 0.8]
    sigma0_values = np.linspace(0.5, 20, n_sigma0)
    sigma1_values = np.linspace(0, 20, n_sigma1)

    pulse_width = np.zeros((n_sigma0, n_sigma1))
    undershoot_width = np.zeros((n_sigma0, n_sigma1))

    for ratio in tqdm(ratio_values):
        for isigma0, sigma0 in tqdm(enumerate(sigma0_values), total=n_sigma0):
            for isigma1, sigma1 in tqdm(enumerate(sigma1_values), total=n_sigma1):
                for scale in np.arange(0.1, 100, 0.04):  # Find scale required for ratio
                    y = pulse_(x, scale, sigma0, sigma1)

                    area_pos, area_neg = extract_area(x, y)
                    if np.sqrt((area_neg/area_pos - ratio)**2) < 0.05:
                        break
                    if sigma1 == 0:
                        scale = 1
                        break
                else:
                    continue

                y = pulse_(x, scale, sigma0, sigma1)
                width_pos, width_neg = extract_width(x, y)
                pulse_width[isigma0, isigma1] = width_pos
                undershoot_width[isigma0, isigma1] = width_neg if sigma1 > 0 else 0

                pulse = GenericPulse(x, y)
                camera = Camera(**camera_kwargs, photoelectron_pulse=pulse)
                name = f"undershoot_{ratio:.2f}_{sigma0:.2f}_{sigma1:.2f}.pkl"
                camera.save(join(output_dir, name))
Ejemplo n.º 8
0
def create_new_camera_cfg(tc_cfg_path, output_path):
    """
    Create a new sim_telarray camera cfg file using a TargetCalib mapping file
    """
    create_directory(os.path.dirname(output_path))

    mapping = Mapping(tc_cfg_path)
    mappingsp = mapping.GetMappingSP()

    with open(output_path, 'w') as f:
        write_PixType(f)
        write_pixel_positions(f, mapping)
        # write_trigger_groups_original(f, mapping, mappingsp)
        write_trigger_groups_unique(f, mapping, mappingsp)
    def __init__(self, mapping, output_dir):
        super().__init__()

        self.fig = plt.figure(figsize=(8, 6))
        self.ax = self.fig.add_subplot(1, 1, 1)

        self.ci = CameraImage.from_mapping(mapping, ax=self.ax)
        self.ci.add_colorbar("Pixel Amplitude (p.e.)", pad=-0.15)
        # self.ci.colorbar.set_label("Pixel Amplitude (p.e.)", labelpad=20)
        self.output_dir = output_dir
        self.source_point = None
        self.source_label = None
        self.alpha_line = None
        self.iframe = 0

        create_directory(output_dir)
Ejemplo n.º 10
0
def main():
    description = (
        "Generate the pedestals from an R0 file, subtract it from another "
        "R0 file, and plot the comparison of residuals from different "
        "pedestal methods"
    )
    parser = argparse.ArgumentParser(description=description,
                                     formatter_class=Formatter)
    parser.add_argument('-f', '--file', dest='r0_path', required=True,
                        help='R0 file to obtain residuals from')
    parser.add_argument('-o', '--output', dest='output_dir', required=True,
                        help='directort to store output plots')
    args = parser.parse_args()

    r0_path = args.r0_path
    channel = 0
    output_dir = args.output_dir

    create_directory(output_dir)
    reader = TIOReader(r0_path, max_events=100000)

    # Generate Pedestals
    pedestal = PedestalCellPosition(
        reader.n_pixels, reader.n_samples, reader.n_cells
    )
    desc = "Generating pedestal"
    for wfs in tqdm(reader, total=reader.n_events, desc=desc):
        if wfs.missing_packets:
            continue
        pedestal.add_to_pedestal(wfs, wfs.first_cell_id)

    # embed()

    for cell in range(reader.n_cells):
        if (pedestal.hits[channel, cell] == 0).all():
            continue
        p_cell_wf = CellWaveform()
        p_cell_wf.plot(
            pedestal.pedestal[channel, cell],
            pedestal.std[channel, cell],
            pedestal.hits[channel, cell],
            cell
        )
        p_cell_wf.save(
            join(output_dir, f"cell_pedestal_vs_position/{cell:04d}.pdf")
        )
Ejemplo n.º 11
0
def main():
    with pd.HDFStore("performance.h5", mode='r') as store:
        df_data = store['data']

    # Select fully sampled region
    df_data = df_data.loc[(df_data['pulse_width'] <= 20)
                          & (df_data['undershoot_width'] <= 30)]

    # 20% UNDERSHOOT
    df = df_data.loc[df_data['ratio'] == 0.2]

    create_directory("figures")
    plot_tricontourf(df, "figures/tricontourf.png")
    plot_width_ratio(df, "figures/widthratio.pdf")
    plot_1d_pulse_width(df, "figures/1d_pulse_width.pdf")
    plot_1d_undershoot_width(df, "figures/1d_undershoot_width.pdf")
    plot_best(df_data, "figures/best.pdf")
Ejemplo n.º 12
0
    def __init__(self, dl1_path, totalrows, monitor_path=None):
        print("Creating HDF5 file: {}".format(dl1_path))
        create_directory(os.path.dirname(dl1_path))
        if os.path.exists(dl1_path):
            remove(dl1_path)

        self.totalrows = totalrows
        self.metadata = {}
        self.n_bytes = 0
        self.df_list = []
        self.df_list_n_bytes = 0
        self.monitor = None

        self.store = pd.HDFStore(dl1_path,
                                 complevel=9,
                                 complib='blosc:blosclz')

        if monitor_path:
            self.monitor = MonitorWriter(monitor_path, self.store)
Ejemplo n.º 13
0
def main():
    parser = argparse.ArgumentParser(
        description="Generate Camera configuration files")
    parser.add_argument('-o', dest='output', help='Output directory')
    args = parser.parse_args()
    output_dir = args.output

    create_directory(output_dir)

    # Define camera
    n_samples = 128
    width = 8  # ns
    sigma = width / (2 * np.sqrt(2 * np.log(2)))
    pulse = GaussianPulse(mean=15, sigma=sigma, duration=30, mv_per_pe=4)
    mapping = SSTCameraMapping()
    camera_kwargs = dict(
        mapping=mapping,
        photoelectron_pulse=pulse,
        n_waveform_samples=n_samples,
        continuous_readout_duration=n_samples,
        digitisation_noise=GaussianNoise(stddev=1),
    )

    self_opct_l = [0.08, 0.15, 0.3]
    reflected_opct_l = [0, 0.08, 0.15, 0.3]
    reflected_scale_l = [0.6, 1.0, 1.5, 2.3, 5]
    for self_opct in self_opct_l:
        for reflected_opct in reflected_opct_l:
            for reflected_scale in reflected_scale_l:
                spe = SiPMReflectedOCT(
                    opct=self_opct,
                    reflected_opct=reflected_opct,
                    reflected_scale=reflected_scale,
                    normalise_charge=False,
                    mapping=mapping,
                )
                coupling = ACOffsetCoupling(pulse_area=pulse.area,
                                            spectrum_average=spe.average)
                camera = Camera(**camera_kwargs,
                                photoelectron_spectrum=spe,
                                coupling=coupling)
                name = f"refl_opct_{self_opct:.2f}_{reflected_opct:.2f}_{reflected_scale:.2f}.pkl"
                camera.save(join(output_dir, name))
Ejemplo n.º 14
0
    def __init__(self, path):
        """
        Helper class to write dataframes and metadata to a HDF5 file

        Parameters
        ----------
        path : str
            Path to store the HDF5
        """
        create_directory(os.path.dirname(path))
        print("Creating HDF5 file: {}".format(path))

        self.keys = set()
        self.metadata = defaultdict(lambda: defaultdict(dict))
        self.df_list = defaultdict(list)
        self.df_list_n_bytes = defaultdict(int)
        self.n_bytes = defaultdict(int)

        self.store = pd.HDFStore(
            path, mode='w', complevel=9, complib='blosc:blosclz'
        )
Ejemplo n.º 15
0
def get_data(path):
    data_path = os.path.join(_DATA, path)
    create_directory(os.path.dirname(data_path))
    return data_path
Ejemplo n.º 16
0
def main():
    description = (
        "Generate the pedestals from an R0 file, subtract it from another "
        "R0 file, and plot the comparison of residuals from different "
        "pedestal methods")
    parser = argparse.ArgumentParser(description=description,
                                     formatter_class=Formatter)
    parser.add_argument('-f',
                        '--file',
                        dest='r0_path',
                        required=True,
                        help='R0 file to obtain residuals from')
    parser.add_argument('-p',
                        '--pedestal',
                        dest='pedestal_r0_path',
                        required=True,
                        help='R0 file to generate pedestal from')
    parser.add_argument('-o',
                        '--output',
                        dest='output_dir',
                        required=True,
                        help='directort to store output plots')
    args = parser.parse_args()

    r0_path = args.r0_path
    pedestal_r0_path = args.pedestal_r0_path
    output_dir = args.output_dir

    create_directory(output_dir)
    reader_ped = TIOReader(pedestal_r0_path)
    reader_res = TIOReader(r0_path, max_events=1000)

    # Generate Pedestals
    pedestal_info = (reader_ped.n_pixels, reader_ped.n_samples,
                     reader_ped.n_cells)
    pedestal_tc = PedestalTargetCalib(*pedestal_info)
    pedestal_bp = PedestalBlockphase(*pedestal_info)
    desc = "Generating pedestal"
    for wfs in tqdm(reader_ped, total=reader_ped.n_events, desc=desc):
        if wfs.missing_packets:
            continue
        pedestal_tc.add_to_pedestal(wfs, wfs.first_cell_id)
        pedestal_bp.add_to_pedestal(wfs, wfs.first_cell_id)

    pstats_tc = PixelStats(reader_res.n_pixels)
    pstats_bp = PixelStats(reader_res.n_pixels)
    stats_tc = OnlineStats()
    stats_bp = OnlineStats()
    hist_tc = OnlineHist(100, (-10, 10))
    hist_bp = OnlineHist(100, (-10, 10))

    wf_list_tc = []
    wf_list_bp = []
    fci = []

    # Subtract Pedestals
    desc = "Subtracting pedestal"
    for wfs in tqdm(reader_res, total=reader_res.n_events, desc=desc):
        if wfs.missing_packets:
            continue

        subtracted_tc = pedestal_tc.subtract_pedestal(wfs, wfs.first_cell_id)
        subtracted_bp = pedestal_bp.subtract_pedestal(wfs, wfs.first_cell_id)

        pstats_tc.add_to_stats(subtracted_tc)
        stats_tc.add_to_stats(subtracted_tc)
        hist_tc.add(subtracted_tc)

        pstats_bp.add_to_stats(subtracted_bp)
        stats_bp.add_to_stats(subtracted_bp)
        hist_bp.add(subtracted_bp)

        wf_list_tc.append(subtracted_tc)
        wf_list_bp.append(subtracted_bp)
        fci.append(wfs.first_cell_id)

    # Plot results
    label_tc = pedestal_tc.__class__.__name__
    label_bp = pedestal_bp.__class__.__name__

    p_pix_stats = StatsPlot()
    p_pix_stats.plot(pstats_tc.mean, pstats_tc.std, label_tc)
    p_pix_stats.plot(pstats_bp.mean, pstats_bp.std, label_bp)
    p_pix_stats.save(join(output_dir, "pix_stats.pdf"))

    p_ci_stats = Camera2(reader_res.mapping)
    p_ci_stats.set_image(pstats_tc.mean, pstats_tc.std)
    p_ci_stats.save(join(output_dir, f"ci_stats_{label_tc}.pdf"))
    p_ci_stats.set_image(pstats_bp.mean, pstats_bp.std)
    p_ci_stats.save(join(output_dir, f"ci_stats_{label_bp}.pdf"))

    p_hist = HistPlot()
    p_hist.plot(hist_tc.hist, hist_tc.edges, stats_tc.mean, stats_tc.std,
                label_tc)
    p_hist.plot(hist_bp.hist, hist_bp.edges, stats_bp.mean, stats_bp.std,
                label_bp)
    p_hist.save(join(output_dir, "hist.pdf"))

    p_wf_tc = WaveformPlot()
    p_wf_bp = WaveformPlot()
    wfs_tc = np.stack(wf_list_tc)
    wfs_bp = np.stack(wf_list_bp)
    avg_tc = np.average(wfs_tc, axis=0)
    avg_bp = np.average(wfs_bp, axis=0)
    for iev in range(len(wf_list_tc)):
        ev_avg_tc = np.average(wf_list_tc[iev] - avg_tc, axis=0)
        ev_avg_bp = np.average(wf_list_bp[iev] - avg_bp, axis=0)
        p_wf_tc.plot(ev_avg_tc, fci[iev])
        p_wf_bp.plot(ev_avg_bp, fci[iev])
    p_wf_tc.save(join(output_dir, f"wfs_{label_tc}.pdf"))
    p_wf_bp.save(join(output_dir, f"wfs_{label_bp}.pdf"))
Ejemplo n.º 17
0
 def create_directory(directory):
     create_directory(directory)
Ejemplo n.º 18
0
def get_plot(path):
    plot_path = os.path.join(_PLOT, path)
    create_directory(os.path.dirname(plot_path))
    return plot_path