""" Example code by Jason demonstrating some pygama convenience functions. """ import numpy as np import matplotlib.pyplot as plt import pygama.analysis.histograms as pgh import pygama.analysis.peak_fitting as pga np.random.seed(0) # fix the seed s/t we can reproduce the plot n = 10 data = np.random.normal(0, 1, n) hist, bins, var = pgh.get_hist(data, range=(-5, 5), dx=1) pgh.plot_hist(hist, bins, var, label="data") pars, cov = pga.fit_hist(pga.gauss, hist, bins, var=var, guess=[0, 1, n]) pgh.print_fit_results(pars, cov, ['mu', 'sig', 'A']) pgh.plot_func(pga.gauss, pars, label="chi2 fit") nbnd = (-np.inf, np.inf) pos = (0, np.inf) pars, cov = pga.fit_hist(pga.gauss, hist, bins, var=var, guess=[0, 1, n], bounds=[nbnd, pos, pos], poissonLL=True) pgh.print_fit_results(pars, cov, ['mu', 'sig', 'A']) pgh.plot_func(pga.gauss, pars, label="poissonLL fit")
def peak(df, runDB, calDB, r, line, p=[1, 0], plotit=False): cal = 0.04998 # calDB["cal_pass1"]["1"]["p1cal"] meta_dir = os.path.expandvars(runDB["meta_dir"]) tier_dir = os.path.expandvars(runDB["tier2_dir"]) df['e_cal'] = p[0] * (cal * df['e_ftp']) + p[1] #h = df.hist('e_cal',bins=2000) #plt.yscale('log') df = df.loc[(df.index > 1000) & (df.index < 500000)] def gauss(x, mu, sigma, A=1): """ define a gaussian distribution, w/ args: mu, sigma, area (optional). """ return A * (1. / sigma / np.sqrt(2 * np.pi)) * np.exp(-(x - mu)**2 / (2. * sigma**2)) line_min = 0.995 * line line_max = 1.005 * line nbin = 60 res = 6.3e-4 * line + 0.85 # empirical energy resolution curve from experience hist, bins, var = pgh.get_hist(df['e_cal'], range=(line_min, line_max), dx=(line_max - line_min) / nbin) if plotit: pgh.plot_hist(hist, bins, var=hist, label="data", color='blue') pars, cov = pga.fit_hist(gauss, hist, bins, var=hist, guess=[line, res, 50]) pgu.print_fit_results(pars, cov, gauss) if plotit: pgu.plot_func(gauss, pars, label="chi2 fit", color='red') FWHM = '%.2f' % Decimal( pars[1] * 2. * np.sqrt(2. * np.log(2))) # convert sigma to FWHM FWHM_uncertainty = '%.2f' % Decimal( np.sqrt(cov[1][1]) * 2. * np.sqrt(2. * np.log(2))) peak = '%.2f' % Decimal(pars[0]) peak_uncertainty = '%.2f' % Decimal(np.sqrt(cov[0][0])) residual = '%.2f' % abs(line - float(peak)) if plotit: label_01 = 'Peak = ' + str(peak) + r' $\pm$ ' + str(peak_uncertainty) label_02 = 'FWHM = ' + str(FWHM) + r' $\pm$ ' + str(FWHM_uncertainty) labels = [ label_01, label_02, ] plt.xlim(line_min, line_max) plt.xlabel('Energy (keV)', ha='right', x=1.0) plt.ylabel('Counts', ha='right', y=1.0) plt.tight_layout() plt.hist(df['e_cal'], range=(line_min, line_max), bins=nbin) plt.legend(labels, frameon=False, loc='upper right', fontsize='small') plt.savefig(meta_dir + '/plots/lineFit_' + str(r) + '.png') return peak, FWHM
def peak(line): if (len(sys.argv) != 2): print('Usage: fit_calibrated_peaks.py [run number]') sys.exit() # take calibration parameter for the 'calibration.py' output with open("calDB.json") as f: calDB = json.load(f) cal = calDB["cal_pass1"]["1"]["p1cal"] with open("runDB.json") as f: runDB = json.load(f) meta_dir = os.path.expandvars(runDB["meta_dir"]) tier_dir = os.path.expandvars(runDB["tier_dir"]) #df = pd.read_hdf("{}/Spectrum_{}_2.hdf5".format(meta_dir,sys.argv[1]), key="df") df = pd.read_hdf("{}/t2_run{}.h5".format(tier_dir, sys.argv[1])) df['e_cal'] = cal * df['e_ftp'] df = df.loc[(df.index > 1000) & (df.index < 500000)] def gauss(x, mu, sigma, A=1): """ define a gaussian distribution, w/ args: mu, sigma, area (optional). """ return A * (1. / sigma / np.sqrt(2 * np.pi)) * np.exp(-(x - mu)**2 / (2. * sigma**2)) line_min = 0.995 * line line_max = 1.005 * line nbin = 60 res = 6.3e-4 * line + 0.85 # empirical energy resolution curve from experience hist, bins, var = pgh.get_hist(df['e_cal'], range=(line_min, line_max), dx=(line_max - line_min) / nbin) pgh.plot_hist(hist, bins, var=hist, label="data", color='blue') pars, cov = pga.fit_hist(gauss, hist, bins, var=hist, guess=[line, res, 50]) pgu.print_fit_results(pars, cov, gauss) pgu.plot_func(gauss, pars, label="chi2 fit", color='red') FWHM = '%.2f' % Decimal(pars[1] * 2) FWHM_uncertainty = '%.2f' % Decimal(np.sqrt(cov[1][1]) * 2) peak = '%.2f' % Decimal(pars[0]) peak_uncertainty = '%.2f' % Decimal(np.sqrt(cov[0][0])) residual = '%.2f' % abs(line - float(peak)) label_01 = 'Peak = ' + str(peak) + r' $\pm$ ' + str(peak_uncertainty) label_02 = 'FWHM = ' + str(FWHM) + r' $\pm$ ' + str(FWHM_uncertainty) labels = [ label_01, label_02, ] plt.xlim(line_min, line_max) plt.xlabel('Energy (keV)', ha='right', x=1.0) plt.ylabel('Counts', ha='right', y=1.0) plt.tight_layout() plt.hist(df['e_cal'], range=(line_min, line_max), bins=nbin) plt.legend(labels, frameon=False, loc='upper right', fontsize='small') return peak, FWHM
print("File info: ", f.keys()) chn = 0 for ged in f.keys(): try: dset = f[ged]['raw'] print("key: ", ged, "Data info: ", dset.keys()) except: conf = f[ged] print("Header info: ", conf.keys()) energies = dset['energy'][()] #energies = f[ged]['raw/energy'][()] #energies = dset['energy'][dset['channel'][()]==chn] maxe = np.amax(energies) h, b, v = pgh.get_hist(energies, bins=3500, range=(maxe / 4, maxe)) pgh.plot_hist(h, b, label=ged) #bin_max = b[np.where(h == h.max())][0] #print("chn %d Raw energy max %d, histogram max %d at %d " % (chn,maxe,h.max(),bin_max )) #min_ene = int(bin_max*0.95) #max_ene = int(bin_max*1.05) #hist, bins, var = pgh.get_hist(energies, bins=500, range=(min_ene, max_ene)) #pgh.plot_hist(hist, bins, label="chn %d" % chn ) #chn = chn + 1 plt.xlabel("uncalibrated energy", ha='right', x=1) plt.ylabel("counts", ha='right', y=1) plt.yscale('log') plt.legend() plt.show() #plt.savefig("./peak_chn%d.pdf" % chn ) #plt.cla()
def peak_352(): if (len(sys.argv) != 2): print('Usage: fit_bkg_peaks.py [run number]') sys.exit() with open("runDB.json") as f: runDB = json.load(f) meta_dir = os.path.expandvars(runDB["meta_dir"]) #df = pd.read_hdf("{}/Spectrum_280-329.hdf5".format(meta_dir), key="df") df = pd.read_hdf("{}/Spectrum_{}.hdf5".format(meta_dir, sys.argv[1]), key="df") def gauss(x, mu, sigma, A=1): """ define a gaussian distribution, w/ args: mu, sigma, area (optional). """ return A * (1. / sigma / np.sqrt(2 * np.pi)) * np.exp(-(x - mu)**2 / (2. * sigma**2)) def radford_peak(x, mu, sigma, hstep, htail, tau, bg0, a=1): """ David Radford's HPGe peak shape function """ # make sure the fractional amplitude parameters stay reasonable... if htail < 0 or htail > 1: return np.zeros_like(x) if hstep < 0 or hstep > 1: return np.zeros_like(x) bg_term = bg0 #+ x*bg1 if np.any(bg_term < 0): return np.zeros_like(x) # compute the step and the low energy tail step = a * hstep * erfc((x - mu) / (sigma * np.sqrt(2))) le_tail = a * htail le_tail *= erfc((x - mu) / (sigma * np.sqrt(2)) + sigma / (tau * np.sqrt(2))) le_tail *= np.exp((x - mu) / tau) le_tail /= (2 * tau * np.exp(-(sigma / (np.sqrt(2) * tau))**2)) # add up all the peak shape components return (1 - htail) * gauss(x, mu, sigma, a) + bg_term + step + le_tail hist, bins, var = pgh.get_hist(df['e_cal'], range=(345, 360), dx=0.5) pgh.plot_hist(hist, bins, var=hist, label="data") pars, cov = pga.fit_hist(radford_peak, hist, bins, var=hist, guess=[352, 1.05, 0.001, 0.02, 500, 1000, 40000]) pgu.print_fit_results(pars, cov, radford_peak) pgu.plot_func(radford_peak, pars, label="chi2 fit", color='red') #x_vals = np.arange(345,360,0.5) #plt.plot(x_vals, radford_peak(x_vals, 353, 1.05, .001, 0.02, 500, 1000, 40000)) FWHM = '%.2f' % Decimal(pars[1] * 2) FWHM_uncertainty = '%.2f' % Decimal(np.sqrt(cov[1][1]) * 2) peak = '%.2f' % Decimal(pars[0]) peak_uncertainty = '%.2f' % Decimal(np.sqrt(cov[0][0])) residual = '%.2f' % (351.93 - float(peak)) #chi_2_element_list = [] #for i in range(len(hist)): #chi_2_element = abs((radford_peak(bins[i], *pars) - hist[i])**2/radford_peak(bins[i], *pars)) #chi_2_element_list.append(chi_2_element) #chi_2 = sum(chi_2_element_list) #reduced_chi_2 = '%.2f' % Decimal(chi_2/len(hist)) label_01 = '351.93 keV peak fit' label_02 = 'FWHM = ' + str(FWHM) + r' $\pm$ ' + str(FWHM_uncertainty) label_03 = 'Peak = ' + str(peak) + r' $\pm$ ' + str(peak_uncertainty) label_04 = 'Residual = ' + str(residual) + r' $\pm$ ' + str( peak_uncertainty) colors = ['red', 'red', 'red', 'red'] lines = [Line2D([0], [0], color=c, lw=2) for c in colors] labels = [label_01, label_02, label_03, label_04] plt.xlim(345, 360) plt.ylim(0, plt.ylim()[1]) plt.xlabel('Energy (keV)', ha='right', x=1.0) plt.ylabel('Counts', ha='right', y=1.0) plt.tight_layout() #plt.semilogy() plt.legend(lines, labels, frameon=False, loc='upper right', fontsize='small') plt.show()