def fitter_timing(datadate,samplerate_name,shaping): if samplerate_name == 'INVALID': return("Failed") t_fitter,v_fitter,_ = rw('G:/data/watchman/'+datadate+'_watchman_spe/d2/d2_average.txt',1) uspl = us(t_fitter,v_fitter) filedir = 'G:/data/watchman/'+str(datadate)+'_watchman_spe/studies/phase/'+samplerate_name+'/' Nloops = len(os.listdir(filedir + 'phase=0/phase_'+shaping)) for i in range(Nloops): print(i) filename_exact = 'G:/data/watchman/'+str(datadate)+'_watchman_spe/d3/d3_raw_gained/D3--waveforms--%05d.txt' % i t,v,_ = rw(filename_exact,5) v = -1*v ET = t[0:400] EV = v[0:400] chi2_min = -1 x_min = -1 y_min = -1 shift_min = -1 shifts = np.arange(0,80/20000000000,1e-11) for shift in shifts: pre_OV = uspl(ET + shift) A = np.array([[np.sum(np.square(pre_OV)),np.sum(pre_OV),np.sum(np.multiply(pre_OV,EV))],[np.sum(pre_OV),len(EV),np.sum(EV)]]) A[0] = A[0]/A[0][0] A[1] = np.subtract(A[1],A[1][0]*A[0]) A[1] = A[1]/A[1][1] A[0] = np.subtract(A[0],A[0][1]*A[1]) x = A[0][2] y = A[1][2] OV = x*pre_OV + y chi2 = chi_squared(OV,EV) if chi2_min < 0: shift_min = shift chi2_min = chi2 x_min = x y_min = y elif chi2 < chi2_min: shift_min = shift chi2_min = chi2 x_min = x y_min = y v_fit = x_min*uspl(t_fitter + shift_min) + y_min t_cross = timing_extraction(t_fitter,v_fit) if t_cross <= -0.5e-10 or chi2_min >= 7.95e-5 or t_cross >= 0.5e-10: fig,ax = plt.subplots() ax.plot(t,v) ax.plot(t_fitter,v_fit) ax.axvline(0,color = 'Black') ax.axvline(t_cross,color = 'Red') ax.set_title(str(t_cross) + ', ' + str(chi2_min) + ', ' + str(i)) plt.get_current_fig_manager().window.showMaximized() plt.show(block = False) plt.pause(0.1) fig.savefig('G:/data/watchman/20190724_watchman_spe/studies/phase/Histograms/'+samplerate_name+'/Doubles/%05d.png' % i,dpi = 500) print("Was double!") plt.close() return("Passed")
def p3(new_fsps, datadate, numhead, scale, phase_array, n_box, n_delay, n_att, num_phases): #establishing directory names if new_fsps == 1000000000: sample_rate = '1 Gsps' elif new_fsps == 500000000: sample_rate = '500 Msps' elif new_fsps == 250000000: sample_rate = '250 Msps' else: sample_rate = 'trash' filename = 'G:/data/watchman/' + datadate + '_watchman_spe/d2/d2_average.txt' t, v, _ = rw(filename, numhead) uspl = us(t, v) t_cross_array = [] for i in range(len(phase_array)): print('ndel=' + str(n_delay) + ', natt=' + str(n_att) + ', ' + str(i)) start_value = t[0] + i / (new_fsps * num_phases) end_value = t[-1] t_array = np.arange(start_value, end_value, 1 / new_fsps) v_array = -1 * uspl(t_array) t_array = t_array - i / (new_fsps * num_phases) scale_height = scale / np.max(v_array) v_scaled = v_array * scale_height v_digit = digitize(v_scaled, 0) t_avg, v_avg = boxcar_wf(t_array, v_digit, n_box) v_delay = delay_wf(v_avg, n_delay) v_att = attenuate_wf(v_avg, n_att) v_sum = sum_wf(v_att, v_delay) t_cross, _, _ = zc_locator(t_avg, v_sum) t_cross_array.append(t_cross) t_cross_array = t_cross_array - t_cross_array[0] t_cross_array = np.flip(np.asarray(t_cross_array)) true_timing_array = np.flip(np.zeros(num_phases) - phase_array) fig, ax = plt.subplots() ax.plot(true_timing_array, t_cross_array) ax.set_title('Recovered Timing vs. True Timing') ax.set_xlabel('True Timing (Seconds)') ax.set_ylabel('Recovered Timing (Seconds)') plt.get_current_fig_manager().window.showMaximized() plt.show(block=False) savedir = 'G:/data/watchman/' + str( datadate ) + '_watchman_spe/studies/phase/' + sample_rate + '/Histograms/averages_splined/' if not os.path.exists(savedir): os.makedirs(savedir) filename = 'ndelay=' + str(n_delay) + '_natt=' + str( n_att) + '_Phases=%d.png' % num_phases savename = savedir + filename fig.savefig(savename, dpi=500) plt.close()
def establish_templates(new_fsps, datadate, numhead, scale_array, phase_array, noise, fsps, steps, scale_mean): #establishing directory names if new_fsps == 1000000000: sample_rate = '1 Gsps' elif new_fsps == 500000000: sample_rate = '500 Msps' elif new_fsps == 250000000: sample_rate = '250 Msps' else: sample_rate = 'trash' filename = 'G:/data/watchman/' + datadate + '_watchman_spe/d2/d2_average.txt' t, v, header = rw(filename, numhead) uspl = us(t, v) for i in range(len(phase_array)): print(i) writedir_noise = 'G:/data/watchman/' + datadate + '_watchman_spe/studies/phase/' + sample_rate + '/file_template/phase=' + str( i) + '/phase_raw_gained_analyzed_noised/' writedir_peaked = 'G:/data/watchman/' + datadate + '_watchman_spe/studies/phase/' + sample_rate + '/file_template/phase=' + str( i) + '/phase_raw_gained_analyzed_peaked/' #creating directories if they don't exist if not os.path.exists(writedir_noise): os.makedirs(writedir_noise) if not os.path.exists(writedir_peaked): os.makedirs(writedir_peaked) start_value = t[i] end_value = t[-1] t_array = np.arange(start_value, end_value, 1 / new_fsps) v_array = uspl(t_array) Nloops = len( scale_array) #establishing how many files to cycle through for j in range(Nloops): scale_height_noise = scale_mean / np.max(v_array) scale_height_peaked = scale_array[j] / np.max(v_array) v_scaled_noise = v_array * scale_height_noise v_scaled_peaked = v_array * scale_height_peaked print('%s/%s, File: %05d' % (i + 1, steps, j)) #printing number of file currently being processed #establishing read and write names writename_noise = writedir_noise + 'Phase--waveforms--%05d.txt' % j writename_peaked = writedir_peaked + 'Phase--waveforms--%05d.txt' % j #digitizing waveform values v_digit_noise = digitize(v_scaled_noise, noise) v_digit_peaked = digitize(v_scaled_peaked, noise) #saving downsampled and digitized waveforms ww(t_array - i / fsps, v_digit_noise, writename_noise, header) ww(t_array - i / fsps, v_digit_peaked, writename_peaked, header) return ()
def fitter_timing(datadate,samplerate_name,shaping): if samplerate_name == 'INVALID': return("Failed") t_fitter,v_fitter,_ = rw('G:/data/watchman/'+datadate+'_watchman_spe/d2/d2_average.txt',1) uspl = us(t_fitter,v_fitter) filedir = 'G:/data/watchman/'+str(datadate)+'_watchman_spe/studies/phase/'+samplerate_name+'/' Nloops = len(os.listdir(filedir + 'phase=0/phase_'+shaping)) for i in range(Nloops): filename_exact = 'G:/data/watchman/'+str(datadate)+'_watchman_spe/d3/d3_raw_gained/D3--waveforms--%05d.txt' % i t_exact,v_exact,_ = rw(filename_exact,5) j = 0 check_value = 0 while j < 80 and check_value == 0: print(str(i) + ', Phase=' + str(j)) filename = filedir + 'phase='+str(j)+'/phase_'+shaping+'/Phase--waveforms--%05d.txt' % i t,v,_ = rw(filename,5) v = -1*v ET = t[0:10] EV = v[0:10] chi2_min = -1 x_min = -1 y_min = -1 shift_min = -1 shifts = np.arange(0,80/20000000000,1e-11) for shift in shifts: pre_OV = uspl(ET + shift) A = np.array([[np.sum(np.square(pre_OV)),np.sum(pre_OV),np.sum(np.multiply(pre_OV,EV))],[np.sum(pre_OV),len(EV),np.sum(EV)]]) A[0] = A[0]/A[0][0] A[1] = np.subtract(A[1],A[1][0]*A[0]) A[1] = A[1]/A[1][1] A[0] = np.subtract(A[0],A[0][1]*A[1]) x = A[0][2] y = A[1][2] OV = x*pre_OV + y chi2 = chi_squared(OV,EV) if chi2_min < 0: shift_min = shift chi2_min = chi2 x_min = x y_min = y elif chi2 < chi2_min: shift_min = shift chi2_min = chi2 x_min = x y_min = y v_fit = x_min*uspl(t_fitter + shift_min) + y_min t_cross = timing_extraction(t_fitter,v_fit) timing_check = -1*j/20000000000 - t_cross if timing_check <= -1e-9 or chi2_min >= 5000 or timing_check >= 1e-9: t_exact = t_exact - j/20000000000 v_peak_ind = np.where(v == np.amax(v))[0][0] t_peak = t[v_peak_ind] t_exact_check = np.abs(t_exact - t_peak) t_exact_loc = np.where(t_exact_check == np.amin(t_exact_check))[0][0] v_exact = v[v_peak_ind]/v_exact[t_exact_loc]*v_exact fig,ax = plt.subplots() ax.plot(t,v) ax.plot(t_fitter,v_fit) ax.plot(t_exact,v_exact) ax.axvline(-1*j/20000000000,color = 'Black') ax.axvline(t_cross,color = 'Red') ax.set_title(str(t_cross) + ', ' + str(chi2_min) + ', ' + str(i)) plt.get_current_fig_manager().window.showMaximized() plt.show(block = False) plt.pause(0.1) fig.savefig('G:/data/watchman/20190724_watchman_spe/studies/phase/Histograms/'+samplerate_name+'/Doubles Copy/%05d.png' % i,dpi = 500) print("Was double!") plt.close() check_value += 1 j += 1 return("Passed")
def fitter_timing(datadate, numhead, samplerate, samplerate_name, shaping): if samplerate_name == 'INVALID': return ("Failed") phase_time = 1 / 20000000000 maxphase = int(20000000000 / samplerate + 0.5) t_fitter, v_fitter, _ = rw( 'G:/data/watchman/' + datadate + '_watchman_spe/d2/d2_average.txt', 1) uspl = us(t_fitter, v_fitter) filedir = 'G:/data/watchman/' + str( datadate ) + '_watchman_spe/studies/phase/' + samplerate_name + '/file_template/' Nloops = len(os.listdir(filedir + 'phase=0/phase_' + shaping)) difference_list = [] chi_list = [] for i in range(Nloops): print(i) j = random.randint(0, maxphase - 1) filename = filedir + 'phase=' + str( j) + '/phase_' + shaping + '/Phase--waveforms--%05d.txt' % i t, v, _ = rw(filename, numhead) v = -1 * v ET = t[0:10] EV = v[0:10] chi2_min = -1 x_min = -1 y_min = -1 shift_min = -1 shifts = np.arange(0, 80 / 20000000000, 1e-11) for shift in shifts: pre_OV = uspl(ET + shift) A = np.array([[ np.sum(np.square(pre_OV)), np.sum(pre_OV), np.sum(np.multiply(pre_OV, EV)) ], [np.sum(pre_OV), len(EV), np.sum(EV)]]) A[0] = A[0] / A[0][0] A[1] = np.subtract(A[1], A[1][0] * A[0]) A[1] = A[1] / A[1][1] A[0] = np.subtract(A[0], A[0][1] * A[1]) x = A[0][2] y = A[1][2] OV = x * pre_OV + y chi2 = chi_squared(OV, EV) if chi2_min < 0: shift_min = shift chi2_min = chi2 x_min = x y_min = y elif chi2 < chi2_min: shift_min = shift chi2_min = chi2 x_min = x y_min = y v_fit = x_min * uspl(t_fitter + shift_min) + y_min t_cross = timing_extraction(t_fitter, v_fit) #if (-1*j*phase_time - t_cross) < -1e-7: # _,ax = plt.subplots() # ax.plot(t,v) # ax.plot(t_fitter,v_fit) # ax.scatter(ET,EV) # ax.axvline(-1*j*phase_time,color = 'Black') # ax.axvline(t_cross,color = 'Red') # ax.set_title(str(-1*j*phase_time - t_cross) + ', ' + str(chi2_min) + ', ' + str(i) + ', ' + str(j)) # plt.get_current_fig_manager().window.showMaximized() # plt.show() difference_list.append((-1 * j * phase_time - t_cross)[0]) chi_list.append(chi2_min) difference_list = np.asarray(difference_list) chi_list = np.asarray(chi_list) true_mean = '%5g' % (np.mean(difference_list) * 1e12) true_std = '%5g' % (np.std(difference_list) * 1e12) bins = 30 #np.linspace(-2.2e-9,2.2e-9,num = 100) histo_data, bins_data = np.histogram(difference_list, bins=bins) binwidth = (bins_data[1] - bins_data[0]) binscenters = np.array([ 0.5 * (bins_data[i] + bins_data[i + 1]) for i in range(len(bins_data) - 1) ]) FontSize = 32 plt.rcParams.update({'font.size': FontSize}) _, ax = plt.subplots() ax.bar(binscenters, histo_data, width=binwidth) ax.set_xlabel('True Timing - Recovered Timing') ax.set_ylabel('Count') ax.set_title(samplerate_name + ' - Fitter Timings') ax.text(0.05, 0.95, 'Distribution Parameters:\nMean: ' + true_mean + ' ps\nStandard Deviation: ' + true_std + ' ps', transform=ax.transAxes, fontsize=FontSize, verticalalignment='top', bbox=dict(boxstyle='round', facecolor='White', alpha=0.5)) plt.get_current_fig_manager().window.showMaximized() plt.show() plt.close() bins = 30 #np.linspace(0,30200,num = 100) histo_data, bins_data = np.histogram(chi_list, bins=bins) binwidth = (bins_data[1] - bins_data[0]) binscenters = np.array([ 0.5 * (bins_data[i] + bins_data[i + 1]) for i in range(len(bins_data) - 1) ]) plt.rcParams.update({'font.size': FontSize}) plt.bar(binscenters, histo_data, width=binwidth, log=True) plt.xlabel('Chi Squared') plt.ylabel('Count') plt.title(samplerate_name + ' - Chi Squared Counts - Fitter Timings') plt.get_current_fig_manager().window.showMaximized() plt.show() plt.close() _, ax = plt.subplots() h = ax.hist2d(difference_list, chi_list, bins=30, norm=LogNorm()) plt.colorbar(h[3], ax=ax) ax.set_title(samplerate_name + ' - Chi Squared vs. Timing Corrections - Fitter Timings') ax.set_xlabel('Timing Corrections') ax.set_ylabel('Chi Squared') plt.get_current_fig_manager().window.showMaximized() plt.show() plt.close() return ("Passed")
def fitter_timing(datadate, samplerate, samplerate_name, shaping): if samplerate_name == 'INVALID': return ("Failed") phase_time = 1 / 20000000000 maxphase = int(20000000000 / samplerate + 0.5) t_fitter, v_fitter, _ = rw( 'G:/data/watchman/' + datadate + '_watchman_spe/d2/d2_average.txt', 1) uspl = us(t_fitter, v_fitter) filedir = 'G:/data/watchman/' + str( datadate) + '_watchman_spe/studies/phase/' + samplerate_name + '/' doubles_dir = 'G:/data/watchman/20190724_watchman_spe/studies/phase/Histograms/250 Msps/Doubles Combined/' doubles_array = os.listdir(doubles_dir) print(doubles_array) Nloops = len(os.listdir(filedir + 'phase=0/phase_' + shaping)) difference_list = [] chi_list = [] for i in range(Nloops): print(i) if '%05d.png' % i in doubles_array: print('Skipped!') else: j = random.randint(0, maxphase - 1) filename = filedir + 'phase=' + str( j) + '/phase_' + shaping + '/Phase--waveforms--%05d.txt' % i ##filename_exact = 'G:/data/watchman/'+str(datadate)+'_watchman_spe/d3/d3_raw_gained/D3--waveforms--%05d.txt' % i t, v, _ = rw(filename, 5) v = -1 * v ##t_exact,v_exact,_ = rw(filename_exact,5) ##t_exact -= 1*j*phase_time ##time_locator = t[4] ##t_min_array = abs(t_exact - time_locator) ##time_index = np.where(t_min_array == np.amin(t_min_array))[0][0] ##v_exact = v_exact * v[4]/v_exact[time_index] ET = t[0:10] EV = v[0:10] chi2_min = -1 x_min = -1 y_min = -1 shift_min = -1 shifts = np.arange(0, 80 / 20000000000, 1e-11) for shift in shifts: pre_OV = uspl(ET + shift) A = np.array([[ np.sum(np.square(pre_OV)), np.sum(pre_OV), np.sum(np.multiply(pre_OV, EV)) ], [np.sum(pre_OV), len(EV), np.sum(EV)]]) A[0] = A[0] / A[0][0] A[1] = np.subtract(A[1], A[1][0] * A[0]) A[1] = A[1] / A[1][1] A[0] = np.subtract(A[0], A[0][1] * A[1]) x = A[0][2] y = A[1][2] OV = x * pre_OV + y chi2 = chi_squared(OV, EV) if chi2_min < 0: shift_min = shift chi2_min = chi2 x_min = x y_min = y elif chi2 < chi2_min: shift_min = shift chi2_min = chi2 x_min = x y_min = y v_fit = x_min * uspl(t_fitter + shift_min) + y_min t_cross = timing_extraction(t_fitter, v_fit) ##if -1*j*phase_time - t_cross <= -1e-9 or chi2_min >= 2750 or -1*j*phase_time - t_cross >= 1e-9: ## fig,ax = plt.subplots() ## ax.plot(t,v) ## ax.plot(t_fitter,v_fit) ## ax.plot(t_exact,v_exact) ## ax.scatter(ET,EV) ## ax.axvline(-1*j*phase_time,color = 'Black') ## ax.axvline(t_cross,color = 'Red') ## ax.set_title(str(-1*j*phase_time - t_cross) + ', ' + str(chi2_min) + ', ' + str(i) + ', ' + str(j)) ## plt.get_current_fig_manager().window.showMaximized() ## plt.show(block = False) ## plt.pause(0.1) ## fig.savefig('G:/data/watchman/20190724_watchman_spe/studies/phase/Histograms/250 Msps/Doubles/%05d.png' % i,dpi = 500) ## print("Was double!") ## plt.close() ##else: ## difference_list.append((-1*j*phase_time - t_cross)[0]) ## chi_list.append(chi2_min) difference_list.append((-1 * j * phase_time - t_cross)[0]) chi_list.append(chi2_min) difference_list = np.asarray(difference_list) chi_list = np.asarray(chi_list) true_mean = '%5g' % (np.mean(difference_list) * 1e12) true_std = '%5g' % (np.std(difference_list) * 1e12) bins = np.linspace(-2.2e-9, 2.2e-9, num=100) histo_data, bins_data = np.histogram(difference_list, bins=bins) binwidth = (bins_data[1] - bins_data[0]) binscenters = np.array([ 0.5 * (bins_data[i] + bins_data[i + 1]) for i in range(len(bins_data) - 1) ]) FontSize = 32 plt.rcParams.update({'font.size': FontSize}) _, ax = plt.subplots() ax.bar(binscenters, histo_data, width=binwidth) ax.set_xlabel('True Timing - Recovered Timing') ax.set_ylabel('Count') ax.set_title(samplerate_name + ' - Fitter Timings, Doubles Removed') ax.text(0.05, 0.95, 'Distribution Parameters:\nMean: ' + true_mean + ' ps\nStandard Deviation: ' + true_std + ' ps', transform=ax.transAxes, fontsize=FontSize, verticalalignment='top', bbox=dict(boxstyle='round', facecolor='White', alpha=0.5)) plt.get_current_fig_manager().window.showMaximized() plt.show() plt.close() bins = np.linspace(0, 30200, num=100) histo_data, bins_data = np.histogram(chi_list, bins=bins) binwidth = (bins_data[1] - bins_data[0]) binscenters = np.array([ 0.5 * (bins_data[i] + bins_data[i + 1]) for i in range(len(bins_data) - 1) ]) plt.rcParams.update({'font.size': FontSize}) plt.bar(binscenters, histo_data, width=binwidth, log=True) plt.xlabel('Chi Squared') plt.ylabel('Count') plt.title(samplerate_name + ' - Chi Squared Counts - Fitter Timings, Doubles Removed') plt.get_current_fig_manager().window.showMaximized() plt.show() plt.close() _, ax = plt.subplots() h = ax.hist2d(difference_list, chi_list, bins=65, norm=LogNorm()) plt.colorbar(h[3], ax=ax) ax.set_title( samplerate_name + ' - Chi Squared vs. Timing Corrections - Fitter Timings, Doubles Removed' ) ax.set_xlabel('Timing Corrections') ax.set_ylabel('Chi Squared') plt.get_current_fig_manager().window.showMaximized() plt.show() plt.close() return ("Passed")