def generator_rootdatafile_baselinesub(filename, batch_size=1, n_sample=4320, tree_name="waveforms", branch_name="wf1", n_wf_baseline=1000, margin_lsb=8, samples_around=4): gen_baseline = generator_rootdatafile(filename, batch_size=n_wf_baseline, n_sample=n_sample, tree_name=tree_name, branch_name=branch_name) waveforms = next(gen_baseline) baseline = get_baseline(waveforms, margin_lsb=margin_lsb, samples_around=samples_around) assert np.isfinite(baseline) del waveforms return generator_rootdatafile(filename, batch_size=batch_size, n_sample=n_sample, tree_name=tree_name, branch_name=branch_name, baseline=baseline)
def get_baselines(self, n_waveform=1000, margin_lsb=8, samples_around=4): wfs_for_baseline = next( self.get_generator_waveform(batch_size=n_waveform)) baselines = [] for pix in range(self.n_channel): baselines.append( get_baseline(wfs_for_baseline[:, :, pix], margin_lsb=margin_lsb, samples_around=samples_around)) if not np.isfinite(baselines[-1]): print('ERROR in baseline calculation for pixel', pix) baselines[-1] = 0 print('baseline for pixel', pix, ':', baselines[-1]) self.reset() return np.array(baselines)
def g2_data(model, root_datafile, batch_size=100, n_sample=4320, margin_lsb=8, samples_around=5, shifts=range(-500, 501), xlim=None, parallel_iterations=10, plot="show", g2_datafile=None, n_bin_per_sample=8): from matplotlib import pyplot as plt #get baselines gen_baseline_pix1 = generator_rootdatafile(root_datafile, batch_size=10000, n_sample=n_sample, tree_name="waveforms", branch_name="wf1") waveforms_pix1 = next(gen_baseline_pix1) baseline_pix1 = get_baseline(waveforms_pix1, margin_lsb=margin_lsb, samples_around=samples_around) assert np.isfinite(baseline_pix1) del waveforms_pix1 gen_baseline_pix2 = generator_rootdatafile(root_datafile, batch_size=1000, n_sample=n_sample, tree_name="waveforms", branch_name="wf2") waveforms_pix2 = next(gen_baseline_pix2) baseline_pix2 = get_baseline(waveforms_pix2, margin_lsb=margin_lsb, samples_around=samples_around) assert np.isfinite(baseline_pix2) del waveforms_pix2 # create correlator wf_correlation = Correlation(shifts=shifts, n_batch=batch_size, n_sample=n_sample, sample_type=tf.float32, scope="wf", parallel_iterations=parallel_iterations) # compute g2 for wf print("compute g2 on waveforms") gen_wf_pix1 = generator_rootdatafile(root_datafile, batch_size=batch_size, n_sample=n_sample, tree_name="waveforms", branch_name="wf1", baseline=baseline_pix1) gen_wf_pix2 = generator_rootdatafile(root_datafile, batch_size=batch_size, n_sample=n_sample, tree_name="waveforms", branch_name="wf2", baseline=baseline_pix2) sum_wf1, sum_wf2, sum_wf11, sum_wf12, sum_wf22, count_wf = wf_correlation( gen_wf_pix1, gen_wf_pix2) del wf_correlation # create correlator pe_correlation = Correlation(shifts=shifts, n_batch=batch_size, n_sample=n_sample * n_bin_per_sample, sample_type=tf.float32, scope="pe", parallel_iterations=parallel_iterations) sum_pe1, sum_pe2, sum_pe11, sum_pe12, sum_pe22, count_pe = None, None, None, None, None, None if model is not None: # create extractor extractor = Extractor(n_sample) extractor.load(model) # compute g2 for extracted pe print("compute g2 on extracted pe") gen_pe_pred_pix1 = extractor.predict_wf_generator( generator_rootdatafile(root_datafile, batch_size=batch_size, n_sample=n_sample, tree_name="waveforms", branch_name="wf1", baseline=baseline_pix1)) gen_pe_pred_pix2 = extractor.predict_wf_generator( generator_rootdatafile(root_datafile, batch_size=batch_size, n_sample=n_sample, tree_name="waveforms", branch_name="wf2", baseline=baseline_pix2)) sum_pe1, sum_pe2, sum_pe11, sum_pe12, sum_pe22, count_pe = pe_correlation( gen_pe_pred_pix1, gen_pe_pred_pix2) del extractor del pe_correlation if g2_datafile is not None: np.savez( g2_datafile, shift_in_sample=shifts, n_sample_wf=n_sample, sum1_wf=sum_wf1, sum2_wf=sum_wf2, sum12_wf=sum_wf12, sum11_wf=sum_wf11, sum22_wf=sum_wf22, shift_in_bins=shifts, n_sample_pb=n_sample * 8, sum1_pb=sum_pe1, sum2_pb=sum_pe2, sum12_pb=sum_pe12, sum11_pb=sum_pe11, sum22_pb=sum_pe22, baseline_pix0=baseline_pix1, baseline_pix1=baseline_pix2, ) # plot if plot is not None: print("plotting") n_sample_tot = count_wf[np.array(shifts) == 0][0] n_wf_tot = int(n_sample_tot / n_sample) time_tot_us = n_sample_tot * 4e-3 title = "g2 with {} waveforms ({:.3f} ms)".format( n_wf_tot, time_tot_us * 1e-3) fig = plt.figure(figsize=(8, 6)) g2_wf = count_wf * sum_wf12 / (sum_wf1 * sum_wf2) g2_pe = count_pe * sum_pe12 / (sum_pe1 * sum_pe2) shift_wf_ns = 4. * np.array(shifts) shift_pe_ns = .5 * np.array(shifts) plt.plot(shift_wf_ns, g2_wf, label='g2 from waveforms') if model is not None: plt.plot(shift_pe_ns, g2_pe, label='g2 from CNN') plt.xlabel('delay [ns]') plt.ylabel(r'$g^2(\tau)$') if xlim is not None: plt.xlim(xlim) plt.legend() plt.grid() plt.title(title) plt.tight_layout() if plot == "show": plt.show() else: fig.savefig(plot) print(plot, "image created")
def g2_wf_data(root_datafile, batch_size=100, n_sample=4320, margin_lsb=8, samples_around=5, shifts=range(-500, 501), xlim=None, parallel_iterations=100, plot="show", method="tf"): from matplotlib import pyplot as plt gen_baseline_pix1 = generator_rootdatafile(root_datafile, batch_size=1000, n_sample=n_sample, tree_name="waveforms", branch_name="wf1") waveforms_pix1 = next(gen_baseline_pix1) baseline_pix1 = get_baseline(waveforms_pix1, margin_lsb=margin_lsb, samples_around=samples_around) assert np.isfinite(baseline_pix1) del waveforms_pix1 gen_baseline_pix2 = generator_rootdatafile(root_datafile, batch_size=1000, n_sample=n_sample, tree_name="waveforms", branch_name="wf2") waveforms_pix2 = next(gen_baseline_pix2) baseline_pix2 = get_baseline(waveforms_pix2, margin_lsb=margin_lsb, samples_around=samples_around) assert np.isfinite(baseline_pix2) del waveforms_pix2 # compute g2 for wf print("compute g2 on waveforms") gen_wf_pix1 = generator_rootdatafile(root_datafile, batch_size=batch_size, n_sample=n_sample, tree_name="waveforms", branch_name="wf1", baseline=baseline_pix1) gen_wf_pix2 = generator_rootdatafile(root_datafile, batch_size=batch_size, n_sample=n_sample, tree_name="waveforms", branch_name="wf2", baseline=baseline_pix2) # create correlator if method == "tf": wf_correlation = Correlation(shifts=shifts, n_batch=batch_size, n_sample=n_sample, sample_type=tf.float32, scope="wf", parallel_iterations=parallel_iterations) sum_wf1, sum_wf2, _, sum_wf12, _, count_wf = wf_correlation( gen_wf_pix1, gen_wf_pix2) del wf_correlation elif method == "numpy": sum_wf1, sum_wf2, sum_wf12, _, _, count_wf = correlate_numpy( gen_wf_pix1, gen_wf_pix2, shifts=shifts) else: ValueError("method must be \"tf\" or \"numpy\"") # plot print("plotting") n_sample_tot = count_wf[np.array(shifts) == 0][0] n_wf_tot = int(n_sample_tot / n_sample) time_tot_us = n_sample_tot * 4e-3 title = "g2 with {} waveforms ({:.3f} ms)".format(n_wf_tot, time_tot_us * 1e-3) fig = plt.figure(figsize=(4, 3)) g2_wf = count_wf * sum_wf12 / (sum_wf1 * sum_wf2) shift_wf_ns = 4. * np.array(shifts) plt.plot(shift_wf_ns, g2_wf, label='g2 from waveforms') plt.xlabel('delay [ns]') plt.ylabel(r'$g^2(\tau)$') if xlim is not None: plt.xlim(xlim) plt.legend() plt.grid() plt.title(title) plt.tight_layout() if plot == "show": plt.show() else: fig.savefig(plot) print(plot, "image created")
def g2_andrii_toy(model, datafile_pix1, datafile_pix2, batch_size=100, n_sample=2500, margin_lsb=8, samples_around=5, shifts=range(-500, 501), xlim=None, parallel_iterations=10, plot="show", n_bin_per_sample=8): #get baselines gen_baseline_pix1 = generator_andrii_toy(datafile_pix1, batch_size=1000, n_sample=n_sample, n_bin_per_sample=n_bin_per_sample) waveforms_pix1, _ = next(gen_baseline_pix1) baseline_pix1 = get_baseline(waveforms_pix1, margin_lsb=margin_lsb, samples_around=samples_around) assert np.isfinite(baseline_pix1) del waveforms_pix1 gen_baseline_pix2 = generator_andrii_toy(datafile_pix2, batch_size=1000, n_sample=n_sample, n_bin_per_sample=n_bin_per_sample) waveforms_pix2, _ = next(gen_baseline_pix2) baseline_pix2 = get_baseline(waveforms_pix2, margin_lsb=margin_lsb, samples_around=samples_around) assert np.isfinite(baseline_pix2) del waveforms_pix2 # create correlator wf_correlation = Correlation(shifts=shifts, n_batch=batch_size, n_sample=n_sample, sample_type=tf.float32, scope="wf", parallel_iterations=parallel_iterations) # compute g2 for wf print("compute g2 on waveforms") gen_wf_pix1 = generator_wf( generator_andrii_toy(datafile_pix1, batch_size=batch_size, n_sample=n_sample, baseline=baseline_pix1)) gen_wf_pix2 = generator_wf( generator_andrii_toy(datafile_pix2, batch_size=batch_size, n_sample=n_sample, baseline=baseline_pix2)) sum_wf1, sum_wf2, _, sum_wf12, _, count_wf = wf_correlation( gen_wf_pix1, gen_wf_pix2) del wf_correlation # create correlator pe_correlation = Correlation(shifts=shifts, n_batch=batch_size, n_sample=n_sample * n_bin_per_sample, sample_type=tf.float32, scope="pe", parallel_iterations=parallel_iterations) # compute g2 for MC truth print("compute g2 on MC truth") gen_pe_truth_pix1 = generator_pe_truth( generator_andrii_toy(datafile_pix1, batch_size=batch_size, n_sample=n_sample, baseline=baseline_pix1)) gen_pe_truth_pix2 = generator_pe_truth( generator_andrii_toy(datafile_pix2, batch_size=batch_size, n_sample=n_sample, baseline=baseline_pix2)) sum_pe1_truth, sum_pe2_truth, _, sum_pe12_truth, _, count_pe_truth = pe_correlation( gen_pe_truth_pix1, gen_pe_truth_pix2) # create extractor extractor_toy_andrii = Extractor(n_sample) extractor_toy_andrii.load(model) # compute g2 for extracted pe print("compute g2 on extracted pe") gen_pe_pred_pix1 = extractor_toy_andrii.predict_generator( generator_andrii_toy(datafile_pix1, batch_size=batch_size, n_sample=n_sample, baseline=baseline_pix1)) gen_pe_pred_pix2 = extractor_toy_andrii.predict_generator( generator_andrii_toy(datafile_pix2, batch_size=batch_size, n_sample=n_sample, baseline=baseline_pix2)) sum_pe1, sum_pe2, _, sum_pe12, _, count_pe = pe_correlation( gen_pe_pred_pix1, gen_pe_pred_pix2) del extractor_toy_andrii del pe_correlation # plot print("plotting") n_sample_tot = count_wf[np.array(shifts) == 0][0] n_wf_tot = int(n_sample_tot / n_sample) time_tot_us = n_sample_tot * 4e-3 title = "g2 with " + str(n_wf_tot) + " waveforms" title += " (" + str(time_tot_us * 1e-3) + " ms)" fig = plt.figure(figsize=(8, 6)) g2_wf = count_wf * sum_wf12 / (sum_wf1 * sum_wf2) g2_pe = count_pe * sum_pe12 / (sum_pe1 * sum_pe2) g2_pe_truth = count_pe_truth * sum_pe12_truth / (sum_pe1_truth * sum_pe2_truth) shift_wf_ns = 4. * np.array(shifts) shift_pe_ns = .5 * np.array(shifts) plt.plot(shift_wf_ns, g2_wf, '+-', label='g2 from waveforms') plt.plot(shift_pe_ns, g2_pe, '+-', label='g2 from CNN') plt.plot(shift_pe_ns, g2_pe_truth, '+-', label='g2 from MC truth') plt.xlabel('delay [ns]') plt.ylabel(r'$g^2(\tau)$') if xlim is not None: plt.xlim(xlim) plt.legend() plt.grid() plt.title(title) if plot == "show": plt.show() else: fig.savefig(plot) print(plot, "image created")