Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- from scipy.optimize import curve_fit
- datapath = '/Volumes/TOSHIBA EXT/23-07-19 - Test Pulses (22deg)/'
- data_path_output = '/Users/marialauraperez/Desktop/SummerCERN/Results/TestPulses-22deg/'
- first_charges = np.arange(500, 1600, 100)
- second_charges = np.arange(2000, 12000, 1000)
- charges = np.concatenate((first_charges, second_charges))
- ToT_data = list()
- mean_ToT = list()
- energies = charges*3.62/1000
- for i in range(len(charges)):
- matrix = np.genfromtxt(datapath+str(charges[i])+'e.txt_iToT.txt')
- ToT_data.append(matrix)
- mean_ToT.append(np.mean(matrix)/100)
- # isotope_energies = [5.89, 22.16, 59.54]
- # isotope_ToTs = [14.21, 48.76, 118.28]
- theoretical_XRF = [4.512, 5.9, 6.405, 8.046, 15.775, 17.48, 25.271, 59.5]
- ToT_XRF = [12.05, 15.82, 16.91, 20.98, 38.83, 42.64, 58.97, 118.54]
- def surrogate_function(x, a, b, c, t):
- y = a*x + b - c/(x-t)
- y[np.where(x < t)] = 0
- y *= y > 0
- return y
- poptmean, pcovn = curve_fit(surrogate_function, energies, mean_ToT)
- x = np.linspace(1.5, 65, 100)
- plt.figure()
- plt.scatter(energies, mean_ToT, s=10, label='TP Data', color='navy')
- plt.plot(x, surrogate_function(x, *poptmean), label='TP Fit: a = '+str(np.round(poptmean[0], 3))
- + '; b = ' + str(np.round(poptmean[1], 3))
- + '; c = '+str(np.round(poptmean[2], 3))
- + '; t = '+str(np.round(poptmean[3], 3)), color='blue')
- plt.scatter(theoretical_XRF, ToT_XRF, s=10, label='Source Data', color='red')
- plt.xlabel('Energy [keV]')
- plt.ylabel('ToT value')
- plt.title('Source and test pulse calibrations (whole detector)', fontweight='bold')
- plt.legend()
- plt.savefig('/Users/marialauraperez/Desktop/total_per_source/Source+TestPulse_Calibs22deg.png')
- plt.show()
- # plt.figure()
- # plt.scatter(energies, mean_ToT, s=10, label='Data', color='navy')
- # plt.plot(x, surrogate_function(x, *poptmean), label='Fit: a = '+str(np.round(poptmean[0], 3))
- # + '; b = ' + str(np.round(poptmean[1], 3))
- # + '; c = '+str(np.round(poptmean[2], 3))
- # + '; t = '+str(np.round(poptmean[3], 3)), color='blue')
- # plt.xlabel('Test pulse height [keV]')
- # plt.ylabel('Mean ToT value')
- # plt.title('Calibration with test pulses', fontweight='bold')
- # plt.legend()
- # plt.savefig(data_path_output+'22degTestPulse_AvgCalib.png')
- def get_calibration_matrices(energy_vals, ToT_matrices):
- nrows = len(ToT_matrices[0][:, 0])
- ncols = len(ToT_matrices[0][0, :])
- a = np.zeros(np.shape(ToT_matrices[0]))
- b = a.copy()
- c = a.copy()
- t = a.copy()
- test_vals_y = list()
- pixel_i = list()
- pixel_j = list()
- test_popt = list()
- for i in range(nrows):
- for j in range(ncols):
- entry = list()
- for k in range(len(ToT_matrices)):
- entry.append(ToT_matrices[k][i, j]/100)
- try:
- popt, pcov = curve_fit(surrogate_function, energy_vals, entry, p0=poptmean)
- a[i, j] = popt[0]
- b[i, j] = popt[1]
- c[i, j] = popt[2]
- t[i, j] = popt[3]
- if (i is 20 or i is 148 or i is 50) and (j is 130 or j is 210 or j is 90):
- test_vals_y.append(entry)
- pixel_i.append(i)
- pixel_j.append(j)
- test_popt.append(popt)
- except RuntimeError:
- print('Error: calibration not performed in pixel (', str(i)+', '+str(j)+' )')
- plt.figure()
- plt.scatter(energy_vals, entry)
- plt.show()
- return a, b, c, t, pixel_i, pixel_j, test_vals_y, test_popt
- # a_matrix, b_matrix, c_matrix, t_matrix, pixels_x, pixels_y, test_vals, test_params = \
- # get_calibration_matrices(energies, ToT_data)
- #
- # plt.figure()
- # for k in range(len(pixels_x)):
- # plt.scatter(energies, test_vals[k], s=10, label='('+str(pixels_x[k])+', '+str(pixels_y[k])+')')
- # plt.plot(x, surrogate_function(x, *test_params[k]), label='Fit')
- # plt.xlabel('Test pulse height [keV]')
- # plt.ylabel('ToT value')
- # plt.title('Calibration per pixel with test pulses', fontweight='bold')
- # plt.legend(fontsize='small', ncol=2)
- # plt.savefig(data_path_output+'22degTestPulse_CalibsPixels.png')
- #
- # np.savetxt(data_path_output+'A_TestPulse22deg.txt', a_matrix, fmt='%.5f')
- # np.savetxt(data_path_output+'B_TestPulse22deg.txt', b_matrix, fmt='%.5f')
- # np.savetxt(data_path_output+'C_TestPulse22deg.txt', c_matrix, fmt='%.5f')
- # np.savetxt(data_path_output+'T_TestPulse22deg.txt', t_matrix, fmt='%.5f')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement