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"))
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"))
Exemple #3
0
def process(r0_paths, output_path):
    data = []

    for ipath, r0_path in enumerate(r0_paths):
        print(f"Processing: {ipath+1}/{len(r0_paths)}")
        pedestal_path = r0_path.replace(".tio", "_ped.tcal")

        regex_r0 = re.search(r".+_tc([\d.]+)_tmc([\d.]+).tio", r0_path)
        temperature_r0_chamber = float(regex_r0.group(1))
        temperature_r0_primary = float(regex_r0.group(2))

        regex_ped = re.search(r".+_tc([\d.]+)_tmc([\d.]+)_ped.tcal",
                              pedestal_path)
        temperature_pedestal_chamber = float(regex_ped.group(1))
        temperature_pedestal_primary = float(regex_ped.group(2))

        reader = TIOReader(r0_path, max_events=50000)
        pedestal = PedestalTargetCalib(reader.n_pixels, reader.n_samples,
                                       reader.n_cells)
        pedestal.load_tcal(pedestal_path)

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

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

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

        data.append(
            dict(
                temperature_r0_chamber=temperature_r0_chamber,
                temperature_r0_primary=temperature_r0_primary,
                temperature_pedestal_chamber=temperature_pedestal_chamber,
                temperature_pedestal_primary=temperature_pedestal_primary,
                mean=online_stats.mean,
                std=online_stats.std,
                hist=online_hist.hist,
                edges=online_hist.edges,
            ))

    with HDF5Writer(output_path) as writer:
        writer.write(data=pd.DataFrame(data))
Exemple #4
0
def process(tf_r0_paths, pedestal_path, tf_path):
    pedestal = PedestalTargetCalib.from_tcal(pedestal_path)

    # Parse amplitudes from filepath
    amplitudes = []
    readers = []
    for path in tf_r0_paths:
        regex_ped = re.search(r".+VPED_(\d+).tio", path)
        amplitudes.append(int(regex_ped.group(1)))
        readers.append(TIOReader(path))

    # Instance TF class from first file
    tf = TFDC(readers[0].n_pixels, readers[0].n_samples - 32,
              readers[0].n_cells, amplitudes)

    desc0 = "Generating TF"
    it = zip(amplitudes, readers)
    n_amp = len(amplitudes)
    for amplitude, reader in tqdm(it, total=n_amp, desc=desc0):
        amplitude_index = tf.get_input_amplitude_index(amplitude)
        for iwf, wfs in enumerate(reader):
            if wfs.missing_packets:
                continue

            # Skip to next file when enough hits are reached
            if iwf % 1000 == 0:
                if (tf.hits[..., amplitude_index] > 100).all():
                    break

            tf.add_to_tf(pedestal.subtract_pedestal(wfs, wfs.first_cell_id),
                         wfs.first_cell_id, amplitude_index)

    tf.save(tf_path)
def main():
    r0_path = "/Users/Jason/Downloads/tempdata/Pedestal_23deg_r0.tio"
    r1_int_path = "/Users/Jason/Downloads/tempdata/Pedestal_23deg_r1_int.tio"
    r1_rnd_path = "/Users/Jason/Downloads/tempdata/Pedestal_23deg_r1_rnd.tio"
    tcal_path = "/Users/Jason/Downloads/tempdata/Pedestal_23deg_ped.tcal"

    reader_r0 = TIOReader(r0_path, max_events=10000)
    reader_r1_int = TIOReader(r1_int_path, max_events=10000)
    reader_r1_rnd = TIOReader(r1_rnd_path, max_events=10000)

    # Generate Pedestal
    pedestal_tc = PedestalTargetCalib(reader_r0.n_pixels, reader_r0.n_samples,
                                      reader_r0.n_cells)
    pedestal_tc.load_tcal(tcal_path)

    l_int = []
    l_rnd = []

    # Subtract Pedestals
    desc = "Subtracting pedestal"
    z = zip(reader_r0, reader_r1_int, reader_r1_rnd)
    it = tqdm(z, total=reader_r0.n_events, desc=desc)
    for wfs_r0, wfs_r1_int, wfs_r1_rnd in it:
        if wfs_r0.missing_packets:
            continue

        wfs_r1_flt = pedestal_tc.subtract_pedestal(wfs_r0,
                                                   wfs_r0.first_cell_id)

        # offset = 700
        # scale = 13.6
        # wfs_r1_flt = (wfs_r1_flt + offset) * scale
        # wfs_r1_int = (wfs_r1_int + offset) * scale
        # wfs_r1_rnd = (wfs_r1_rnd + offset) * scale

        l_int.append(wfs_r1_flt - wfs_r1_int)
        l_rnd.append(wfs_r1_flt - wfs_r1_rnd)

    l_int = np.array(l_int).ravel()
    l_rnd = np.array(l_rnd).ravel()

    plt.hist(l_int, bins=20, histtype='step', label='int')
    plt.hist(l_rnd, bins=20, histtype='step', label='rnd')
    plt.legend(loc='best')
    plt.xlabel("Difference to float ped-sub ADC")
    plt.ylabel("N")
    plt.show()
    def __init__(self, paths):
        lookup = []
        for path in paths:
            regex_r0 = re.search(r".+_tc([\d.]+)_tmc([\d.]+)_ped.tcal", path)
            temperature_chamber = float(regex_r0.group(1))
            temperature_primary = float(regex_r0.group(2))
            pedestal = PedestalTargetCalib.from_tcal(path)
            lookup.append(
                dict(
                    temperature_chamber=temperature_chamber,
                    temperature_primary=temperature_primary,
                    pedestal=pedestal,
                ))

        self.lookup = pd.DataFrame(lookup)
        self.lookup = self.lookup.set_index("temperature_primary")
Exemple #7
0
def process(input_paths):
    for path in input_paths:
        pedestal_path = path.replace(".tio", "_ped.tcal")
        reader = TIOReader(path)

        pedestal = PedestalTargetCalib(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)
        pedestal.save_tcal(pedestal_path)
Exemple #8
0
def process(tf_r0_paths, pedestal_path, tf_path, tf_class):
    pedestal = PedestalTargetCalib.from_tcal(pedestal_path)

    # Parse amplitudes from filepath
    amplitudes = []
    readers = []
    for path in tf_r0_paths:
        regex_ped = re.search(r".+_(-?\d+)_r0.tio", path)
        amplitudes.append(int(regex_ped.group(1)))
        readers.append(TIOReader(path))

    # Instance TF class from first file
    tf = tf_class(readers[0].n_pixels, readers[0].n_samples - 32,
                  readers[0].n_cells, amplitudes)
    if tf_class == TFACCrossCorrelation:
        # # Estimate range of peak positions
        # r = readers[np.abs(np.array(amplitudes) - 500).argmin()]
        # peak = r[0].mean(0).argmax()
        # tf.set_trange(peak - 5 - 32, peak + 5 - 32)
        tf.set_trange(6, 16)

    desc0 = "Generating TF"
    it = zip(amplitudes, readers)
    n_amp = len(amplitudes)
    for amplitude, reader in tqdm(it, total=n_amp, desc=desc0):
        amplitude_index = tf.get_input_amplitude_index(amplitude)
        for iwf, wfs in enumerate(tqdm(reader, total=reader.n_events)):
            if wfs.missing_packets:
                print("Skipping event")
                continue

            # Skip to next file when enough hits are reached
            if iwf % 1000 == 0:
                if (tf.hits[..., amplitude_index] > 100).all():
                    break

            cells = get_cell_ids_for_waveform(wfs.first_cell_id,
                                              reader.n_samples, reader.n_cells)
            wfs = wfs[:, 32:]
            wfs.first_cell_id = cells[32]
            tf.add_to_tf(pedestal.subtract_pedestal(wfs, wfs.first_cell_id),
                         wfs.first_cell_id, amplitude_index)

    tf.save(tf_path)
Exemple #9
0
    def __init__(self, paths):
        self.pedestal_instance = None

        temperature_list = []
        pedestal_list = []
        for path in paths:
            regex_r0 = re.search(r".+_tc([\d.]+)_tmc([\d.]+)_ped.tcal", path)
            temperature_primary = float(regex_r0.group(2))
            pedestal = PedestalTargetCalib.from_tcal(path)

            temperature_list.append(temperature_primary)
            pedestal_list.append(pedestal._pedestal)
            if self.pedestal_instance is None:
                self.pedestal_instance = pedestal

        self.interp_f = PchipInterpolator(np.array(temperature_list),
                                          np.array(pedestal_list),
                                          axis=0,
                                          extrapolate=True)
    def __init__(self, paths):
        self.pedestal_instance = None

        temperature_list = []
        pedestal_list = []
        for path in paths:
            regex_r0 = re.search(r".+_tc([\d.]+)_tmc([\d.]+)_ped.tcal", path)
            temperature_primary = float(regex_r0.group(2))
            pedestal = PedestalTargetCalib.from_tcal(path)

            temperature_list.append(temperature_primary)
            pedestal_list.append(pedestal._pedestal)
            if self.pedestal_instance is None:
                self.pedestal_instance = pedestal

        self.interp_f = interp1d(temperature_list,
                                 pedestal_list,
                                 axis=0,
                                 fill_value="extrapolate")
Exemple #11
0
def process(pedestal_paths, output_path):
    pedestal_paths = sort_file_list(pedestal_paths)

    data = []
    reference_values = None
    reference_spread = None

    for path in pedestal_paths:
        regex_ped = re.search(r".+_tc([\d.]+)_tmc([\d.]+)_ped.tcal", path)
        temperature_pedestal_primary = float(regex_ped.group(2))

        pedestal = PedestalTargetCalib.from_tcal(path)
        values = pedestal.pedestal
        spread = pedestal.std

        if reference_values is None:
            reference_values = values
            reference_spread = spread

        delta = values - reference_values
        delta_mean = delta.mean()
        delta_std = delta.std()
        delta_channel_mean = delta[0].mean()
        delta_channel_std = delta[0].std()

        # delta_spread = spread - reference_spread
        spread_mean = spread.mean()
        spread_std = spread.std()

        data.append(
            dict(
                temperature=temperature_pedestal_primary,
                delta_mean=delta_mean,
                delta_std=delta_std,
                delta_channel_mean=delta_channel_mean,
                delta_channel_std=delta_channel_std,
                spread_mean=spread_mean,
                spread_std=spread_std,
            ))

    with HDF5Writer(output_path) as writer:
        writer.write(data=pd.DataFrame(data))
def process(pedestal_path, output_dir):
    pedestal = PedestalTargetCalib.from_tcal(pedestal_path)

    std_mean = np.mean(pedestal.std)
    std_std = np.std(pedestal.std)
    std_hist, std_edges = np.histogram(pedestal.std, bins=100)

    p_hist = HistPlot()
    p_hist.plot(std_hist, std_edges, std_mean, std_std)
    p_hist.save(join(output_dir, "hist.pdf"))

    p_hist2d_pedestal = Hist2D()
    p_hist2d_pedestal.plot(pedestal.pedestal[0], pedestal.hits[0],
                           "Pedestal Mean (ADC)")
    p_hist2d_pedestal.save(join(output_dir, "hist2d_mean.pdf"))

    p_hist2d_std = Hist2D()
    p_hist2d_std.plot(pedestal.std[0], pedestal.hits[0],
                      "Pedestal Stddev (ADC)")
    p_hist2d_std.save(join(output_dir, "hist2d_std.pdf"))
def process(path):
    pedestal_path = path.replace("_r0.tio", "_ped.tcal")
    reader = TIOReader(path)

    pedestal = PedestalTargetCalib(reader.n_pixels, reader.n_samples - 32,
                                   reader.n_cells)
    desc = "Generating pedestal"
    for wfs in tqdm(reader, total=reader.n_events, desc=desc):
        if wfs.missing_packets:
            continue
        cells = get_cell_ids_for_waveform(wfs.first_cell_id, reader.n_samples,
                                          reader.n_cells)
        wfs = wfs[:, 32:]
        wfs.first_cell_id = cells[32]
        pedestal.add_to_pedestal(wfs, wfs.first_cell_id)
    pedestal.save_tcal(pedestal_path)
def get_pedestal(pedestal_path):
    return PedestalTargetCalib.from_tcal(pedestal_path)
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"))