Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import Cluster
  4. import Fit
  5.  
  6. output_data_path = '/Users/marialauraperez/Desktop/'
  7. input_data_path = '/Volumes/TOSHIBA EXT/XRF_20190723_22C/raw/'
  8. sources = ['CaXRF', 'AgXRF', 'Am241XRF_1']
  9. names_hists = ['Ca XRF', 'Ag XRF', 'Am241']
  10. names = [['Ca', 'Cu'], ['Ag'], ['Sn', 'Gd']]
  11. colors_hists = ['lightcoral', 'yellowgreen', 'dodgerblue']
  12. colors_peaks = [['maroon', 'red'], ['olive'], ['navy', 'blueviolet']]
  13. maximum = 70
  14. numbins = 70
  15. maxrows = 100000
  16.  
  17.  
  18. def load_parameter_matrices(suffix, path_to_data):
  19. # suffix: of the data
  20. # path_to_data: where txt matrices are
  21. parameter_matrices = []
  22. names = ['A', 'B', 'C', 'T']
  23. for i in range(len(names)):
  24. if i is 0:
  25. parameter_matrices.append(Cluster.remove_dead_pixels(np.genfromtxt(
  26. path_to_data + names[i] + suffix)))
  27. else:
  28. parameter_matrices.append(np.genfromtxt(path_to_data + names[i] + suffix))
  29. return parameter_matrices
  30.  
  31.  
  32. def histos_and_peaks(matrices, calibration_type, minargs, maxargs):
  33. # clusters, may calibrate, gives histograms and returns info of histogram
  34. # maximum: in histogram
  35. # maxrows: number of events to import
  36. # calibration_type: string, non calibrated, test pulse or default
  37. # if calibrate is false, everything is done with ToT values
  38. mus = list()
  39. x1 = np.linspace(0, maximum, 500)
  40. plt.figure()
  41. plt.title('Test spectra with Si-TPX3 (' + calibration_type + ')', fontweight='bold')
  42. for i in range(len(sources)):
  43. source = sources[i]
  44. print('Processing '+names_hists[i]+' data...')
  45. data = np.genfromtxt(input_data_path+source+'.t3pa', skip_header=1, delimiter='\t', unpack=True,
  46. max_rows=maxrows)
  47. indexes = np.ndarray.astype(data[1, :], int)
  48. ToA_vals = data[2, :]
  49. ToT_vals = data[3, :]
  50. x_vals, y_vals = np.unravel_index(indexes, [256, 256])
  51. if calibration_type is 'NonCalibrated':
  52. energies = ToT_vals
  53. else:
  54. energies = Cluster.calibrate(matrices, x_vals, y_vals, ToT_vals)
  55.  
  56. clusters = Cluster.db_cluster(x_vals, y_vals, ToA_vals, energies, eps=1.99, scale=[1.0, 1.0, 100.])
  57. x_cvals, y_cvals, t_cvals, e_cvals = Cluster.get_clustered_energies(clusters)
  58. del data
  59. del energies
  60. del clusters
  61. n, bins, patches = plt.hist(e_cvals, bins=numbins, range=[0, maximum], histtype='step',
  62. label=names_hists[i] + ' data', color=colors_hists[i])
  63. bins2 = np.zeros(len(bins)-1)
  64. for w in range(len(bins)-1):
  65. bins2[w] = (bins[w+1] + bins[w])/2
  66. # ('+str(len(x_cvals))+' clusters)
  67. for j in range(len(names[i])):
  68. name = names[i][j]
  69. print('Fitting '+name+' peak...')
  70. minarg = minargs[i][j]
  71. maxarg = maxargs[i][j]
  72. gauss = Fit.FitGauss().fit(x=bins2[minarg:maxarg], y=n[minarg:maxarg])
  73. y1 = Fit.gauss(x1, gauss[0])
  74. mu = gauss[0][1]
  75. mus.append(mu)
  76. plt.plot(x1, y1, label=name + r' fit ($\mu$ = ' + str(np.round(mu, 3)) + ')', color=colors_peaks[i][j],
  77. linestyle='--')
  78. plt.xlim(0, maximum)
  79. if calibration_type is 'NonCalibrated':
  80. plt.xlabel('ToT value')
  81. else:
  82. plt.xlabel('Energy [keV]')
  83. plt.ylabel('Total counts')
  84. plt.legend(fontsize='small')
  85. plt.savefig(output_data_path+calibration_type+'_Histos+Peaks.png')
  86. return mus
  87.  
  88.  
  89. matpath = '/Users/marialauraperez/Desktop/param_mats/'
  90. #param_mats_default = load_parameter_matrices('_SiTPX3.txt', matpath)
  91. #param_mats_stp = load_parameter_matrices('_SourceTestPulse.txt', matpath)
  92. #param_mats_tp = load_parameter_matrices('_TestPulse22deg.txt', matpath)
  93. param_mats_noam = load_parameter_matrices('_STP_NoAm.txt', matpath)
  94.  
  95. #mus_def = histos_and_peaks(param_mats_default, 'Default', [[0, 6], [12], [15, 35]], [[6, 11], [35], [35, 47]])
  96. #mus_stp = histos_and_peaks(param_mats_stp, 'STP', [[0, 6], [12], [15, 35]], [[6, 11], [35], [35, 47]])
  97. #mus_tp = histos_and_peaks(param_mats_tp, 'Test Pulses', [[0, 5], [12], [15, 30]], [[5, 10], [35], [30, 38]])
  98. mus_noam = histos_and_peaks(param_mats_noam, 'STP (No Am)', [[0, 6], [12], [15, 35]], [[6, 11], [35], [35, 47]])
  99.  
  100. #print(mus_def)
  101. #print(mus_stp)
  102. #print(mus_tp)
  103. print(mus_noam)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement