def process(r0_path, pedestal, tf, output_path): reader = TIOReader(r0_path, max_events=1000) sum = np.zeros(reader.n_samples - 32) n = 0 for wfs in tqdm(reader, total=reader.n_events): 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] iev = wfs.iev first_cell_id = wfs.first_cell_id wfs = correct_overflow(wfs) wfs = pedestal.subtract_pedestal(wfs, first_cell_id) if tf is not None: wfs = tf.apply_tf(wfs, first_cell_id) if np.isnan(wfs).any(): continue sum += wfs.sum(0) n += wfs.shape[0] avg = sum / n avg /= avg.max() array = np.column_stack([np.arange(avg.size) * 1e-9, avg]).astype(np.float32) print(f"Creating file: {output_path}") np.savetxt(output_path, array)
def _add_to_tf(wfs, fci, amplitude_index, tf, hits, m2): n_pixels, n_samples = wfs.shape _, n_cells, _ = tf.shape cells = get_cell_ids_for_waveform(fci, n_samples, n_cells) for ipix in prange(n_pixels): for isam in prange(n_samples): sample = wfs[ipix, isam] idx = (ipix, cells[isam], amplitude_index) tf[idx], hits[idx], m2[idx] = welfords_online_algorithm( sample, tf[idx], hits[idx], m2[idx])
def _add_to_pedestal(wfs, fci, pedestal, hits, m2): _, n_samples = wfs.shape _, n_cells = pedestal.shape cells = get_cell_ids_for_waveform(fci, n_samples, n_cells) n_pixels, n_samples = wfs.shape for ipix in prange(n_pixels): for isam in prange(n_samples): sample = wfs[ipix, isam] idx = (ipix, cells[isam]) pedestal[idx], hits[idx], m2[idx] = welfords_online_algorithm( sample, pedestal[idx], hits[idx], m2[idx])
def _subtract_pedestal(wfs, fci, pedestal): subtracted = np.zeros(wfs.shape, dtype=np.float32) _, n_cells, n_samples = pedestal.shape cells = get_cell_ids_for_waveform(fci, n_samples, n_cells) n_pixels, n_samples = wfs.shape for ipix in prange(n_pixels): for isam in prange(n_samples): sample = wfs[ipix, isam] pedestal_value = pedestal[ipix, cells[isam], isam] if pedestal_value != 0: subtracted[ipix, isam] = sample - pedestal_value return subtracted
def _apply_tf(wfs, fci, tf, amplitudes): calibrated = np.zeros(wfs.shape, dtype=np.float32) n_pixels, n_samples = wfs.shape _, n_cells, _ = tf.shape cells = get_cell_ids_for_waveform(fci, n_samples, n_cells) for ipix in prange(n_pixels): for isam in prange(n_samples): sample = wfs[ipix, isam] cell = cells[isam] tf_i = tf[ipix, cell] amplitudes_i = amplitudes[ipix, cell] calibrated[ipix, isam] = np.interp(sample, tf_i, amplitudes_i) return calibrated
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 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)
def process(r0_paths, pedestal, tf, output_path): readers = [TIOReader(path) for path in r0_paths] df_list = [] channel = np.arange(readers[0].n_pixels) desc0 = "Extracting charges" for reader in tqdm(readers, total=len(readers), desc=desc0): regex = re.search(r".+Amplitude_([-\d]+)_.+_r0.tio", reader.path) amplitude = int(regex.group(1)) for wfs in tqdm(reader, total=reader.n_events): 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] iev = wfs.iev first_cell_id = wfs.first_cell_id wfs = correct_overflow(wfs) wfs = pedestal.subtract_pedestal(wfs, first_cell_id) if tf is not None: wfs = tf.apply_tf(wfs, first_cell_id) charge = wfs[:, 4:21].sum(1) df_list.append( pd.DataFrame( dict(amplitude=amplitude, iev=iev, fci=first_cell_id, channel=channel, charge=charge))) df = pd.concat(df_list, ignore_index=True) with HDF5Writer(output_path) as writer: writer.write(data=df)