def get_wf(self,channel,index,verbose=False): """Pulls a given waveform from the tier 1 data """ if(verbose): print(F"Getting wf {index} directly for chan {channel}...") t1_file = os.path.join(self.t1_data_dir,"t1_run{}.h5".format(self.run)) g4 = dl.Gretina4MDecoder(t1_file) if self.df1 is None: self.load_df1() if self.df2 is None: self.load_df2() row1 = self.df1.loc[index] row2 = self.df2.loc[index] wf = g4.parse_event_data(row1) wf.training_set_index = index wf.amplitude = row2.trap_max wf.bl_slope = row2.bl_slope wf.bl_int = row2.bl_int wf.t0_estimate = row2.t0est wf.tp_50 = row2.tp_50 wf.window_waveform( time_point=self.wf_batch_conf[channel].align_percent, early_samples=200, num_samples=self.wf_batch_conf[channel].num_samples #200 ) return wf
def save_pulsers(num_to_save=10): run_number = 11499 proc = DataProcessor(DataProcessor) file_name = os.path.join(proc.t1_data_dir, "t1_run{}.h5".format(run_number)) df_gretina = pd.read_hdf(file_name, key="ORGretina4MWaveformDecoder") g4 = dl.Gretina4MDecoder(file_name) df_gretina = df_gretina[df_gretina.energy > 0.1E9] channels = df_gretina.channel.unique() plt.ion() plt.figure(figsize=(14, 7)) for chan in channels: if chan % 2 == 1: continue if not is_mj(chan): continue plt.clf() ax0 = plt.subplot(1, 2, 1) plt.xlabel("Time [ns]") plt.ylabel("ADC [arb]") ax1 = plt.subplot(1, 2, 2) ax1.set_xlim(10**6, 0.5 * 10**8) plt.title("Channel {}".format(chan)) df_chan = df_gretina[df_gretina.channel == chan] if len(df_chan) == 0: print("channel {} appears empty?".format(chan)) continue chan_wfs = [] for i, (index, row) in enumerate(df_chan.iterrows()): wf = g4.parse_event_data(row) mean_sub_data = wf.data - np.mean(wf.data[:750]) # if np.amax(mean_sub_data) < 200: continue wf_window = wf.window_waveform(100, 50, 500, method="value") if len(wf_window) == 0: continue chan_wfs.append(wf) ax0.plot(wf_window, c="b", alpha=0.2) top_idx = np.argmax(mean_sub_data > 0.9 * mean_sub_data.max()) xf, power = signal.periodogram(mean_sub_data[top_idx:], fs=1E8, detrend="linear") ax1.plot(xf, power, alpha=0.5, color="b") if len(chan_wfs) >= num_to_save: break if len(chan_wfs) < num_to_save: continue plt.savefig("pulser_data/chan{}_pulsers.png".format(chan)) np.savez("pulser_data/chan{}_pulsers.npz".format(chan), wfs=chan_wfs)
def save_training_data(self, runList, file_name, chanList=None, settle_time=20): ''' settle_time in ms is minimum time since previous event (on same channel) ''' if chanList is None: chanList = self.detectorChanList training_df = [] for runNumber in runList: t1_file = os.path.join(self.t1_data_dir,"t1_run{}.h5".format(runNumber)) t2_file = os.path.join(self.t2_data_dir, "t2_run{}.h5".format(runNumber)) df = pd.read_hdf(t2_file,key="data") tier1 = pd.read_hdf(t1_file,key=self.data_key) tier1 = tier1.drop({"channel", "energy", "timestamp"}, axis=1) df = df.join(tier1, how="inner") df_train = df.loc[df.is_training==1] training_df.append(df_train ) df_train = pd.concat(training_df, axis=0) df_train.to_hdf(file_name, key="data", mode='w') #TODO: if the multisampling params changed in the middle of this run range, you're hosed. g4 = dl.Gretina4MDecoder(t1_file) if True: for channel, df_chan in df_train.groupby("channel"): n_bins = 10 dt_bins = np.linspace(df_chan.drift_time.min(), df_chan.drift_time.max(), n_bins+1) f2 = plt.figure(figsize=(12,8)) for b_lo, b_hi in zip(dt_bins[:-1], dt_bins[1:]): df_bin = df_chan[(df_chan.drift_time >= b_lo) & (df_chan.drift_time<b_hi)] for i, (index, row) in enumerate(df_bin.iterrows()): if i>=50: break wf=g4.parse_event_data(row) wf_full = wf.get_waveform() wf_full -= row["bl_int"] + np.arange(len(wf_full))*row["bl_slope"] wf_full /= row[self.ecal_name] # t95_int = int(row[self.dt_max_param]) t95_int = np.argmax(wf_full > 0.95) wf_plot = wf.data[t95_int-200:t95_int+100] if i == 0: p = plt.plot(wf_plot, alpha=0.1) else: plt.plot(wf_plot, c=p[0].get_color(), alpha=0.1) # plt.show() # exit() try: os.mkdir("training_plots") except OSError: pass f2.savefig("training_plots/DS{}-{}_chan{}_waveforms".format(self.dataset,self.subset,channel)) plt.close(f2)
def plot_logic_signals(): run_number = 11499 proc = DataProcessor(DataProcessor) file_name = os.path.join(proc.t1_data_dir, "t1_run{}.h5".format(run_number)) df_gretina = pd.read_hdf(file_name, key="ORGretina4MWaveformDecoder") df_gretina = df_gretina[df_gretina.energy > 0.5E9] # plot the energy spectrum # plt.figure() # plt.hist(df_gretina.energy) # plt.show() # exit() plt.ion() plt.figure() channels = df_gretina.channel.unique() g4 = dl.Gretina4MDecoder(file_name) for chan in channels: if chan % 2 == 1: continue if not is_mj(chan): continue # plt.clf() plt.xlabel("Time [ns]") plt.ylabel("ADC [arb]") plt.title("Channel {}".format(chan)) df_chan = df_gretina[df_gretina.channel == chan] for i, (index, row) in enumerate(df_chan.iterrows()): wf = g4.parse_event_data(row) mean_sub_data = wf.data - np.mean(wf.data[:750]) if np.amax(mean_sub_data) < 3000: continue plt.plot(wf.time, mean_sub_data, c="b", alpha=0.2) square = square_model(mean_sub_data[-1], np.argmax(mean_sub_data > 3000), 1.7, 0.025, len(mean_sub_data)) plt.plot(wf.time, square, c="r") if i > 25: break inp = input("q to quit, else to continue") if inp == "q": exit()
def plot_waveforms(file_name, num_waveforms=5): df_gretina = pd.read_hdf(file_name, key="ORGretina4MWaveformDecoder") g4 = dl.Gretina4MDecoder(file_name) plt.figure() plt.xlabel("Time [ns]") plt.ylabel("ADC [arb]") for i, (index, row) in enumerate(df_gretina.iterrows()): wf = g4.parse_event_data(row) wf_sub = wf.data - np.mean(wf.data[:500]) plt.plot(wf.time, wf_sub, ls="steps", c="b", alpha=0.1) if i >= num_waveforms: break
def save_subset(self, channel, n_waveforms, training_data_file_name, output_file_name, exclude_list = [], do_plot=True): df_train = pd.read_hdf(training_data_file_name,key="data") df_train = df_train[df_train.channel == channel] first_dt = df_train.drift_time.min() last_dt = df_train.drift_time.max() n_bins_time = n_waveforms dt_bins = np.linspace(first_dt, last_dt, n_bins_time+1) wfs_per_bin = 1 wfs_saved = [] for b_lo, b_hi in zip(dt_bins[:-1], dt_bins[1:]): df_bin = df_train[(df_train.drift_time >= b_lo) & (df_train.drift_time<b_hi)] for i, (index, row) in enumerate(df_bin.iterrows()): if index in exclude_list: continue t1_file = os.path.join(self.t1_data_dir, "t1_run{}.h5".format(row.runNumber)) g4 = dl.Gretina4MDecoder(t1_file) wf=g4.parse_event_data(row) wf.training_set_index = index wf.amplitude = row.trap_max wf.bl_slope = row.bl_slope wf.bl_int = row.bl_int wf.t0_estimate = row.t0est wf.tp_50 = row.tp_50 wfs_saved.append(wf) break np.savez(output_file_name, wfs=wfs_saved) if do_plot: try: os.mkdir("training_plots") except OSError: pass f, ax = plt.subplots(1,2, figsize=(12,8)) # print ("Channel {} set:".format(channel)) for wf in wfs_saved: # print(" index {}".format(wf.training_set_index)) wf_window = wf.window_waveform() ax[0].plot( wf_window ) ax[1].plot( wf_window / wf.amplitude ) plt.savefig("training_plots/DS{}-{}_chan{}_{}wf_set.png".format(self.dataset,self.subset, channel, n_waveforms))
def plot_pulser_ffts(): run_number = 11499 proc = DataProcessor(DataProcessor) file_name = os.path.join(proc.t1_data_dir, "t1_run{}.h5".format(run_number)) df_gretina = pd.read_hdf(file_name, key="ORGretina4MWaveformDecoder") g4 = dl.Gretina4MDecoder(file_name) df_gretina = df_gretina[df_gretina.energy > 0.5E9] channels = df_gretina.channel.unique() plt.ion() plt.figure(figsize=(14, 7)) ax1 = plt.gca() ax1.set_xlim(10**6, 0.5 * 10**8) pulser_count = 50 for chan in channels: if chan % 2 == 1: continue if not is_mj(chan): continue df_chan = df_gretina[df_gretina.channel == chan] avg_wf = np.zeros(900) avg_count = 0 for i, (index, row) in enumerate(df_chan.iterrows()): wf = g4.parse_event_data(row) mean_sub_data = wf.data - np.mean(wf.data[:750]) if np.amax(mean_sub_data) < 200: continue top_idx = np.argmax(mean_sub_data > 1000) + 15 wf_top = mean_sub_data[top_idx:top_idx + 900] avg_wf += wf_top avg_count += 1 if avg_count >= pulser_count: break if avg_count < pulser_count: continue avg_wf /= pulser_count xf, power = signal.periodogram(avg_wf, fs=1E8, detrend="linear") plt.semilogx(xf, power, label="channel {}".format(chan)) plt.legend(loc=2) inp = input("q to quit, else to continue") if inp == "q": exit()
def save_pulsers(num_to_save = 10): run_number = 848 file_name = "t1_run{}.h5".format(run_number) df_gretina = pd.read_hdf(file_name, key="ORGretina4MWaveformDecoder") g4 = dl.Gretina4MDecoder(file_name) df_gretina = df_gretina[df_gretina.energy > 0.5E9] channels = df_gretina.channel.unique() plt.ion() plt.figure() for chan in channels: plt.clf() plt.xlabel("Time [ns]") plt.ylabel("ADC [arb]") plt.title("Channel {}".format(chan)) df_chan = df_gretina[df_gretina.channel == chan] chan_wfs = [] for i, (index, row) in enumerate(df_chan.iterrows()): if index < 10: continue wf = g4.parse_event_data(row) wf.bl_int = np.mean(wf.data[:200]) wf.bl_slope=0 mean_sub_data = wf.data - wf.bl_int if np.amax(mean_sub_data) < 5000: continue if np.count_nonzero( mean_sub_data > 0.5*mean_sub_data.max()) < 10: continue # plt.plot(mean_sub_data) wf_window = wf.window_waveform(1000, 10, 100, method="value") chan_wfs.append( wf ) plt.plot(wf_window, c="b", alpha=0.2 ) if len(chan_wfs) >= num_to_save: break if len(chan_wfs) < num_to_save: continue plt.savefig("pulser_data/chan{}_pulsers.png".format(chan)) np.savez("pulser_data/chan{}_pulsers.npz".format(chan), wfs=chan_wfs)
def plot_logic_signals(run_number): file_name = "t1_run{}.h5".format(run_number) df_gretina = pd.read_hdf(file_name, key="ORGretina4MWaveformDecoder") plt.ion() plt.figure() channels = df_gretina.channel.unique() print(channels) g4 = dl.Gretina4MDecoder(file_name) for chan in channels: # plt.clf() plt.title("Channel {}".format(chan)) df_chan = df_gretina[df_gretina.channel == chan] for i, (index, row) in enumerate(df_chan.iterrows()): wf = g4.parse_event_data(row) mean_sub_data = wf.data - np.mean(wf.data[:400]) #max val if np.count_nonzero( mean_sub_data > 0.5 * mean_sub_data.max()) < 10: continue align_data = align(mean_sub_data, 100) plt.plot(align_data, c="b", alpha=0.2) square = square_model(align_data[-1], 98, 2, 2.75, 0.01, len(align_data)) plt.plot(square, c="r") if i > 25: break inp = input("q to quit, else to continue") if inp == "q": exit()
def main(): runList = [848] plt.ion() f = plt.figure(figsize=(12,9)) rc_num, rc_den = filt.rc_decay(72) overshoot_num, overshoot_den = filt.gretina_overshoot(2, -3.5) for runNumber in runList: t1_file = "t1_run{}.h5".format(runNumber) df = pd.read_hdf(t1_file,key="ORGretina4MWaveformDecoder") g4 = dl.Gretina4MDecoder(t1_file) chanList = [49]#np.unique(df["channel"]) for chan in chanList: df_chan = df[df.channel == chan] for i, (index, row) in enumerate(df_chan.iterrows()): if i<10: continue wf = g4.parse_event_data(row) wf_dat = wf.data - np.mean(wf.data[:200]) if np.amax(wf_dat) < 200: continue if np.count_nonzero( wf_dat > 0.5*wf_dat.max()) < 10: continue max_idx = np.argmax(wf_dat>20) wf_corr, model, t0 = fit_tail(wf_dat, max_idx) plt.plot(wf_dat) plt.plot(wf_corr) plt.plot(np.arange(max_idx+t0, max_idx+t0+len(model)), model, c="r") plt.axvline(max_idx,c="r", ls=":") inp = input("q to continue, else to quit") if inp == "q": exit()
def main(): runList = np.arange(11515, 11516) proc = DataProcessor() plt.ion() f = plt.figure(figsize=(12, 9)) rc_num, rc_den = filt.rc_decay(72) overshoot_num, overshoot_den = filt.gretina_overshoot(2, -3.5) # print(overshoot_num, overshoot_den) # exit() ds_inf = pd.read_csv("ds1_run_info.csv") for runNumber in runList: t1_file = os.path.join(proc.t1_data_dir, "t1_run{}.h5".format(runNumber)) df = pd.read_hdf(t1_file, key="ORGretina4MWaveformDecoder") g4 = dl.Gretina4MDecoder(t1_file) chanList = np.unique(df["channel"]) chanList = [580] pz_fun = [] masses = [] resolutions = [] for chan in chanList: print("Channel {}".format(chan)) try: ds_inf_det = ds_inf[(ds_inf.LG == chan) | (ds_inf.HG == chan)] det_mass = ds_inf_det.iloc[0].Mass det_res = ds_inf_det.iloc[0].Resolution det_ctres = ds_inf_det.iloc[0].ct_resolution trap_factor = (det_res - det_ctres) / det_ctres except IndexError: print("...couldn't find channel info") if chan % 2 == 1: continue if not is_mj(chan): continue plt.clf() ax1 = plt.subplot(2, 2, 2) ax2 = plt.subplot(2, 2, 4) ax3 = plt.subplot(2, 2, 3) ax4 = plt.subplot(2, 2, 1) df_chan = df[(df.channel == chan) & (df.energy > 0.2E9)] e_min = df_chan.energy.min() e_max = df_chan.energy.max() e_cut = 0.9 * (e_max - e_min) + e_min df_cut = df_chan[df_chan.energy > e_cut] bl_idx = 800 baseline = np.zeros(bl_idx) flat_top = np.zeros(800) # plt.figure() num_wfs = 0 for i, (index, row) in enumerate(df_cut.iterrows()): wf = g4.parse_event_data(row) wf_dat = wf.data - np.mean(wf.data[:bl_idx]) try: wf_corr, energy, gof = fit_tail(wf_dat) align_idx = np.argmax(wf_corr / energy > 0.999) + 20 flat_top_wf = wf_corr[align_idx:align_idx + 800] - energy baseline += wf_dat[:bl_idx] flat_top += flat_top_wf num_wfs += 1 except ValueError as e: print(e) continue ax1.plot(wf_corr[:800], c="b", alpha=0.1) ax2.plot(flat_top_wf, c="b", alpha=0.1) ax4.plot(wf_corr[align_idx - 400:align_idx + 805] / energy, c="b", alpha=0.1) # plt.plot(flat_top_wf, c="b", alpha=0.1) # if num_wfs < 5: continue flat_top /= num_wfs baseline /= num_wfs pz_fun.append(np.sum(flat_top**2)) masses.append(det_res) ax1.set_title("Baseline") ax1.plot(baseline, c="r") ax2.set_title("Decay (PZ corrected)") ax2.plot(flat_top, c="r") # plt.plot(flat_top, c="r") plt.title("Channel {} (mass {}, res {})".format( chan, det_mass, det_res)) # ax1.plot(baseline, label="baseline") # ax1.plot(flat_top - np.mean(flat_top), label="flat top") xf, power = signal.periodogram(baseline, fs=1E8, detrend="linear", scaling="spectrum") x_idx = np.argmax(xf > 0.2E7) ax3.semilogx(xf[x_idx:], power[x_idx:], label="baseline") max_pwr = power.max() # ax2.plot(flat_top) xf, power = signal.periodogram(flat_top, fs=1E8, detrend="constant", scaling="spectrum") x_idx = np.argmax(xf > 0.2E7) ax3.semilogx(xf[x_idx:], power[x_idx:], label="flat top") ax3.legend() plt.savefig("fft_plots/channel{}_fft.png".format(chan)) # inp = input("q to continue, else to quit") # if inp == "q": exit() plt.figure() plt.scatter(masses, pz_fun) inp = input("q to continue, else to quit") if inp == "q": exit()