def plot_aan_ratio(dates: list, aan: list, output_directory: str, name: str): # Plot ratio x_date = [] ratio = [] ratio_err = [] for i in range(len(dates[0])): for j in range(len(dates[1])): if dates[0][i] == dates[1][j]: x_date.append(dates[0][i]) if aan[1][j] == 0: pass else: ratio.append(aan[0][i] / aan[1][j]) break x_date = process_date(x_date) plt.plot(x_date, ratio, "k.") plt.axvline(98, color="r", ls="--") plt.axvline(0, color="b", ls="--") plt.xlabel("exposure days relative to 06/11/2019") plt.ylabel("Ratio AAN Ch0/Ch1") plt.title("Ratio of AAN vs time") plt.grid() # plt.xlim(np.amin(np.array(x_date)), np.amax(np.array(x_date))) # plt.ylim(0, 2) plt.savefig(output_directory + "/summary_plots/aan_ratio_vs_time" + name + ".pdf") plt.close()
def plot_par(date, par, output_directory: str, pmt_object: PMT_Object, name: str): date = process_date(date) try: start = np.where(date == 0)[0][0] except: start = np.where(date == 1)[0][0] mid = np.where(date == 98)[0][0] popt, pcov = curve_fit(f=model_day, xdata=date[mid + 1:], ydata=np.array(par[mid + 1:]), p0=[0.1, 1, 19], bounds=[[0, 0, 0], [1e5, 1e10, 100]]) plt.figure(num=None, figsize=(9, 5), dpi=80, facecolor='w', edgecolor='k') plt.plot(date[:start + 1], np.array(par[:start + 1]), "g.", label="Atmospheric He") plt.plot(date[start + 1:mid + 1], np.array(par[start + 1:mid + 1]), "b.", label="1% He") plt.plot(date[mid + 1:], np.array(par[mid + 1:]), "r.", label="10% He") plt.plot(date[mid + 1:], model_day(date[mid + 1:], *popt), 'k-', label='model') plt.axvline(date[start], 0, 100, ls='--', color='k') plt.axvline(date[mid], 0, 100, ls='--', color='k') plt.xlabel("exposure days relative to 06/11/2019") plt.ylabel("Afterpulse rate /%") plt.title(pmt_object.get_pmt_id() + " PAR vs exposure time") plt.grid() plt.ylim(0, 100) plt.legend(loc='upper left') plt.savefig(output_directory + "/summary_plots/" + pmt_object.get_pmt_id() + "_par_vs_time" + name + ".pdf") print('Param p{}: {:.2e} ± {:.1e}'.format(0, popt[0], np.sqrt(pcov[0, 0]))) print('Param p{}: {:.2e} ± {:.1e}'.format(1, popt[1], np.sqrt(pcov[1, 1]))) print('Param p{}: {:.2e} ± {:.1e}'.format(2, popt[2], np.sqrt(pcov[2, 2]))) # print('Chi2 is:', chi_2) plt.close()
def main(): plt.rcParams["figure.figsize"] = (20, 5) args = io_parse_arguments() directory = args.i output_filename = args.o try: filenames_file = open(directory + "filenames.txt", 'r') except FileNotFoundError as fnf_error: print(fnf_error) raise Exception("Error opening data file") file_names = np.loadtxt(directory + "filenames.txt", delimiter=',', dtype={ 'names': ['filename'], 'formats': ['S100'] }, unpack=True) success_rate = [0, 0] num_average = 7 re_bin = 10 number_total_weeks = 60 last_temp = [[], []] x_og = [] num_in_week = [[0 for i in range(number_total_weeks + 1)], [0 for i in range(number_total_weeks + 1)]] apulse_num_y = [[[] for i in range(10)], [[] for i in range(10)]] day_nums = [[], []] color = 'g-' #for i_file in tqdm.tqdm(range(file_names.size)): for i_file in range(file_names.size): #print(directory + "/" + file_names[i_file][0].decode("utf-8")) file = ROOT.TFile(directory + file_names[i_file][0].decode("utf-8"), "READ") date = file_names[i_file][0].decode("utf-8").split("_")[0] exposure = process_exposure(np.array([int(date)]))[0] day_num = process_date(np.array([int(date)]))[0] week_num = int(day_num / num_average) time = file_names[i_file][0].decode("utf-8").split("_")[1] if i_file == 0: name = date + "_" + 'GAO607_apulse_times_1400V' hist = file.Get(name) temp = [] the_bin = 0 #print(hist.GetNbinsX()) for i_bin in range(hist.GetNbinsX()): if the_bin == 0: temp0 = 0 temp0 += hist.GetBinContent(i_bin) the_bin += 1 if the_bin == re_bin: temp.append(temp0) x_og.append(i_bin * hist.GetBinWidth(i_bin)) the_bin = 0 for i in range(number_total_weeks + 1): last_temp[0].append(np.zeros_like(np.array(temp))) last_temp[1].append(np.zeros_like(np.array(temp))) if day_num < 0: continue for i_channel in range(2): #print(date + "_" + names[i_channel] + "_apulse_times_1400") hist = file.Get(date + "_" + names[i_channel] + "_apulse_times_1400V") hist_0 = file.Get(date + "_" + names[i_channel] + "_apulse_num_1400V") try: hist.GetNbinsX() except: del hist continue temp = [] the_bin = 0 x = [] for i_bin in range(hist.GetNbinsX()): if the_bin == 0: temp0 = 0 temp0 += hist.GetBinContent(i_bin) the_bin += 1 if the_bin == re_bin: temp.append(temp0) x.append(i_bin * hist.GetBinWidth(i_bin)) the_bin = 0 if i_channel == 0: if 0 < day_num < 98: color = 'b-' if day_num >= 98: color = 'r-' plt.plot(x, 100 * np.array(temp) / hist_0.GetEntries(), color) plt.plot(0, 0, 'g-', label="Atmosphere He") plt.plot(0, 0, 'b-', label="1% He") plt.plot(0, 0, 'r-', label="10% He") plt.axvline(1580, ls='--', color='r', label=r"$He^+$") plt.axvline(2680, ls='--', color='g', label=r"$H_2O^+$") plt.axvline(1290, ls='--', color='k', label=r"$H_2^+ He^{2+}$") plt.axvline(2560, ls='--', color='b', label=r"$CH_4^+$") plt.axvline(3850, ls='--', color='c', label=r"$CO_2^+$") plt.legend(loc="upper right") plt.ylim(0, 15) plt.xlim(800, 7000) plt.xlabel("afterpulse time in waveform /ns") plt.ylabel("normalised counts /%") plt.title("Ch{} Date:{} Exposure:{} atm-day DayNUM:{}".format( i_channel, date, round(exposure, 2), day_num)) plt.grid() #plt.yscale('log') plt.show(block=False) plt.pause(0.001) plt.close() last_temp[i_channel][week_num] += 100 * np.array( temp) / hist.GetEntries() num_in_week[i_channel][week_num] += 1 success_rate[i_channel] += 1 del hist for i_channel in range(2): hist = file.Get(date + "_" + names[i_channel] + "_apulse_num_1400V") try: hist.GetNbinsX() except: del hist continue for i_bin in range(len(apulse_num_y[i_channel])): if hist.GetEntries() > 0: apulse_num_y[i_channel][i_bin].append( hist.GetBinContent(i_bin) / hist.GetEntries()) else: pass day_nums[i_channel].append(day_num) del hist file.Close() del file for i_week in range(len(last_temp[0])): if i_week < 14: color = 'b-' elif i_week >= 14: color = 'r-' else: color = 'g-' plt.plot(x_og, last_temp[0][i_week] / num_in_week[0][i_week], color) plt.title("Ch:{} Week num:{}".format(0, i_week)) plt.ylim(0, 6) plt.plot(0, 0, 'g-', label="Atmosphere He") plt.plot(0, 0, 'b-', label="1% He") plt.plot(0, 0, 'r-', label="10% He") plt.axvline(1580, ls='--', color='r', label=r"$He^+$") plt.axvline(2680, ls='--', color='g', label=r"$H_2O^+$") plt.axvline(1290, ls='--', color='k', label=r"$H_2^+ He^{2+}$") plt.axvline(2560, ls='--', color='b', label=r"$CH_4^+$") plt.axvline(3850, ls='--', color='c', label=r"$CO_2^+$") plt.xlim(800, 7000) plt.xlabel("afterpulse time in waveform /ns") plt.ylabel("normalised counts /%") plt.legend(loc="upper right") plt.grid() plt.show(block=False) plt.pause(0.01) plt.close() #plt.rcParams["figure.figsize"] = (20, 20)''' '''for i_bin in range(1,len(apulse_num_y[0])): ylim = 0 if i_bin == 1: ylim = 100 else: ylim = 100/ (i_bin*2) plt.subplot(3,1,1) plt.plot(day_nums[0],100*np.array(apulse_num_y[0][i_bin]), 'k.') plt.axvline(98, color='r', ls='--') plt.axvline(0, color='b', ls='--') plt.title("Apulse num:{}".format(i_bin-1)) plt.grid() plt.ylim(0,ylim) plt.subplot(3,1,2) plt.plot(day_nums[1],100*np.array(apulse_num_y[1][i_bin]), 'k.') plt.axvline(98, color='r', ls='--') plt.axvline(0, color='b', ls='--') plt.ylabel("Percentage apulses /%") plt.grid() plt.ylim(0, ylim) plt.subplot(3,1,3) plt.plot(day_nums[0], np.array(apulse_num_y[0][i_bin])/np.array(apulse_num_y[1][i_bin]), 'k.') plt.axvline(98, color='r', ls='--') plt.axvline(0, color='b', ls='--') plt.xlabel("Exposure time /days") plt.grid() plt.ylabel("Ratio") plt.savefig('/Users/willquinn/Desktop/apulse_num_bin_'+str(i_bin)) plt.close()''' print("Success rate: {} Ch0 {} Ch1".format( success_rate[0] / file_names.size, success_rate[1] / file_names.size)) return
def main(): # Handle the input arguments: ############################## args = pmt_parse_arguments() input_directory = args.i # config_file_name = args.c output_directory = args.o ############################## filenames_txt = input_directory + "/filenames.txt" try: print(">>> Reading data from file: {}".format(filenames_txt)) date_file = open(filenames_txt, 'r') except FileNotFoundError as fnf_error: print(fnf_error) raise Exception("Error opening data file {}".format(filenames_txt)) filenames = np.loadtxt(filenames_txt, delimiter=',', dtype={ 'names': ['filename'], 'formats': ['S100']}, unpack=True) topology = [2, 1] pmt_array = PMT_Array(topology, "summary") pmt_array.set_pmt_id("GAO607", 0) pmt_array.set_pmt_id("GAO612", 1) '''# Set the cuts you wish to apply # If you don't do this the defaults are used if config_file_name is not None: pmt_array.apply_setting(config_file_name) # print_settings(pmt_array)''' # Set up the containers for the summary apulse_rates = [[] for i in range(pmt_array.get_pmt_total_number())] apulse_rates_err = [[] for i in range(pmt_array.get_pmt_total_number())] he_apulse_rates = [[] for i in range(pmt_array.get_pmt_total_number())] he_apulse_rates_err = [[] for i in range(pmt_array.get_pmt_total_number())] dates = [[] for i in range(pmt_array.get_pmt_total_number())] out_files = [output_directory+'/apulse_num_ch0.txt', output_directory+'/apulse_num_ch1.txt'] create_file(out_files[0]) create_file(out_files[1]) for i_file in tqdm.tqdm(range(filenames.size)): file = filenames[i_file][0].decode("utf-8") date = file.split("_")[0] voltage = int(file.split("_")[1].split("A")[1]) if voltage == 1400: pass else: continue apulse_info = read_file(date, voltage, input_directory + "/" + file, pmt_array, output_directory) for i_om in range(pmt_array.get_pmt_total_number()): if len(apulse_info[i_om]) > 0: pass else: continue apulse_rate = apulse_info[i_om][0]["apulse_rate"] he_apulse_rate = apulse_info[i_om][0]["he_apulse_rate"] apulse_rate_err = apulse_info[i_om][0]["apulse_rate_err"] he_apulse_rate_err = apulse_info[i_om][0]["he_apulse_rate_err"] apulse_rates[i_om].append(apulse_rate) apulse_rates_err[i_om].append(apulse_rate_err/10) he_apulse_rates[i_om].append(he_apulse_rate) he_apulse_rates_err[i_om].append(he_apulse_rate_err/10) dates[i_om].append(int(date)) write_to_file(out_files[i_om], '{},{},{},{},{}'.format(date, apulse_rate, apulse_rate_err, he_apulse_rate, he_apulse_rate_err)) # Plot individual summaries for i_om in range(pmt_array.get_pmt_total_number()): if len(apulse_rates[i_om]) > 0: pass else: continue date = process_date(dates[i_om]) try: start = np.where(date == 0)[0][0] except: start = np.where(date == 1)[0][0] mid = np.where(date == 98)[0][0] print(date) print("start:", start) plt.figure(num=None, figsize=(9, 5), dpi=80, facecolor='w', edgecolor='k') plt.errorbar(date[:start + 1], np.array(apulse_rates[i_om][:start + 1]), yerr=np.array(apulse_rates_err[i_om][:start + 1]), fmt="g.", label="Atmospheric He") plt.errorbar(date[start+1:mid + 1], np.array(apulse_rates[i_om][start+1:mid + 1]), yerr=np.array(apulse_rates_err[i_om][start+1:mid + 1]), fmt="b.", label="1% He") plt.errorbar(date[mid+1:], np.array(apulse_rates[i_om][mid+1:]), yerr=np.array(apulse_rates_err[i_om][mid+1:]), fmt="r.", label="10% He") plt.axvline(date[start], 0, 100, ls='--', color='k') plt.axvline(date[mid], 0, 100, ls='--', color='k') plt.xlabel("exposure days relative to 191106") plt.ylabel("Afterpulse rate /%") plt.title(pmt_array.get_pmt_object_number(i_om).get_pmt_id() + " afterpulse rate vs exposure time") plt.grid() plt.ylim(10,90) plt.legend(loc='upper left') plt.savefig(output_directory + "/summary_plots/" + pmt_array.get_pmt_object_number(i_om).get_pmt_id() + "_apulse_rate_vs_time") plt.close() plt.figure(num=None, figsize=(9, 5), dpi=80, facecolor='w', edgecolor='k') plt.errorbar(date[:start + 1], np.array(he_apulse_rates[i_om][:start + 1]), yerr=np.array(he_apulse_rates_err[i_om][:start + 1]), fmt="g.", label="Atmospheric He") plt.errorbar(date[start + 1:mid + 1], np.array(he_apulse_rates[i_om][start + 1:mid + 1]), yerr=np.array(he_apulse_rates_err[i_om][start + 1:mid + 1]), fmt="b.", label="1% He") plt.errorbar(date[mid + 1:], np.array(he_apulse_rates[i_om][mid + 1:]), yerr=np.array(he_apulse_rates_err[i_om][mid + 1:]), fmt="r.", label="10% He") plt.axvline(date[start], 0, 100, ls='--', color='k') plt.axvline(date[mid], 0, 100, ls='--', color='k') plt.xlabel("exposure days relative to 191106") plt.ylabel("Normalised apulse number") plt.title(pmt_array.get_pmt_object_number(i_om).get_pmt_id() + " afterpulse rate vs exposure time") plt.grid() # plt.ylim(150,300) plt.legend(loc='lower right') plt.savefig(output_directory + "/summary_plots/" + pmt_array.get_pmt_object_number(i_om).get_pmt_id() + "_he_apulse_rate_vs_time") plt.close() # Plot ratio x_date = [] ratio = [] ratio_err = [] gain_ratio = [] he_ratio = [] he_ratio_err = [] for i in range(len(dates[0])): for j in range(len(dates[1])): if dates[0][i] == dates[1][j]: x_date.append(dates[0][i]) if apulse_rates[1][j] == 0: pass else: ratio.append(apulse_rates[0][i] / apulse_rates[1][j]) if he_apulse_rates[1][j] == 0: pass else: he_ratio.append(he_apulse_rates[0][i] / he_apulse_rates[1][j]) break x_date = process_date(x_date) plt.plot(x_date, ratio, "k.") plt.axvline(98, color="r", ls="--") plt.axvline(0, color="b", ls="--") plt.xlabel("exposure days relative to 191106") plt.ylabel("Ratio apulse rate Ch0/Ch1") plt.title("Ratio of after pulse rates of CH 0 & 1 vs time") plt.grid() #plt.xlim(np.amin(np.array(x_date)), np.amax(np.array(x_date))) #plt.ylim(0, 2) plt.savefig(output_directory + "/summary_plots/apulse_rate_ratio_vs_time") plt.close() plt.plot(x_date, he_ratio, "k.") plt.axvline(98, color="r", ls="--") plt.axvline(0, color="b", ls="--") plt.xlabel("exposure days relative to 191106") plt.ylabel("Ratio apulse rate Ch0/Ch1") plt.title("Ratio of after pulse rates of CH 0 & 1 vs time") plt.grid() #plt.xlim(np.amin(np.array(x_date)), np.amax(np.array(x_date))) # plt.ylim(0, 2) plt.savefig(output_directory + "/summary_plots/he_apulse_rate_ratio_vs_time") plt.close()
def plot_base_drift(): data = pd.read_csv( "/Users/williamquinn/Desktop/PMT_Project/res_vs_time.csv", header=0, dtype={ 0: int, 1: int, 2: float, 3: float, 4: float, 5: int, 6: float, 7: float }, engine='python') data["time"] = process_date(data["date"]) data = data[data["base_mean"] > 900] fig1 = plt.figure(figsize=figsize, facecolor='white') #frame1 = fig1.add_axes((.125, .3, .55, .6)) data_ch0 = data[data["pmt"] == 0] exposed_fit = fit_straight_line(data_ch0['time'].values, data_ch0['base_mean'].values, data_ch0['base_sig'].values, guess=[0, 0]) plt.errorbar(data_ch0['time'].values, data_ch0['base_mean'].values, yerr=data_ch0['base_sig'].values, fmt='.', label='Exposed', markersize=3, capsize=1, linewidth=1, capthick=1) plt.plot([data_ch0['time'].values[0], data_ch0['time'].values[-1]], linear([data_ch0['time'].values[0], data_ch0['time'].values[-1]], *exposed_fit["popt"]), 'k-', zorder=10) data_ch1 = data[data["pmt"] == 1] control_fit = fit_straight_line(data_ch1['time'].values, data_ch1['base_mean'].values, data_ch1['base_sig'].values, guess=[0, 0]) plt.errorbar(data_ch1['time'].values, data_ch1['base_mean'].values, yerr=data_ch1['base_sig'].values, fmt='.', label='Control', markersize=3, capsize=1, linewidth=1, capthick=1) plt.plot([data_ch1['time'].values[0], data_ch1['time'].values[-1]], linear([data_ch1['time'].values[0], data_ch1['time'].values[-1]], *control_fit["popt"]), 'k-', zorder=10) plt.xlabel('Days from 1% He Onset') plt.ylabel('Voltage /mV') plt.fill_between([-100, 0], [1000, 1000], alpha=0.1, facecolor='green', label='Atmospheric He') plt.fill_between([0, 98], [1000, 1000], alpha=0.1, facecolor='blue', label='1% He') plt.fill_between([98, 500], [1000, 1000], alpha=0.1, facecolor='red', label='10% He') plt.xlim(-40, 430) plt.ylim(978, 981) plt.legend(loc='best', bbox_to_anchor=(1.0, 1)) plt.tight_layout() plt.savefig("/Users/williamquinn/Desktop/PMT_Project/baseline_drift.pdf") plt.close(fig1) plt.figure(figsize=figsize) plt.xlabel('Days from 1% He Onset') plt.ylabel('PCT Change /%') returns_ch0 = data_ch0['base_mean'].pct_change(1) * 100 returns_ch0.plot(label='Exposed') returns_ch1 = data_ch1['base_mean'].pct_change(1) * 100 returns_ch1.plot(label='Control') plt.legend(loc='best') plt.tight_layout() plt.savefig("/Users/williamquinn/Desktop/PMT_Project/baseline_pct.pdf") plt.close() dataframe = pd.concat([ pd.DataFrame(data_ch0['base_mean'].values).shift(1), pd.DataFrame(data_ch0['base_mean'].values) ], axis=1) dataframe.columns = ['t-1', 't+1'] result0 = dataframe.corr() print(result0) dataframe = pd.concat([ pd.DataFrame(data_ch1['base_mean'].values).shift(1), pd.DataFrame(data_ch1['base_mean'].values) ], axis=1) dataframe.columns = ['t-1', 't+1'] result1 = dataframe.corr() print(result1) plt.figure(figsize=figsize) lag_plot(data_ch0['base_mean'], label=r'Exposed $\rho$={:.2f}'.format(result0['t-1'][1]), alpha=0.5) plt.legend(loc='best') plt.tight_layout() plt.savefig( "/Users/williamquinn/Desktop/PMT_Project/baseline_cor_exposed.pdf") plt.figure(figsize=figsize) lag_plot(data_ch1['base_mean'], label=r'Control $\rho$={:.2f}'.format(result1['t-1'][1]), alpha=0.5) plt.legend(loc='best') plt.tight_layout() plt.savefig( "/Users/williamquinn/Desktop/PMT_Project/baseline_cor_control.pdf") '''autocorrelation_plot(data_ch0['base_mean']) plt.grid() plt.show()''' '''# create lagged dataset values = pd.DataFrame(data_ch0['base_mean'].values) dataframe = pd.concat([values.shift(1), values], axis=1) dataframe.columns = ['t-1', 't+1'] # split into train and test sets X = dataframe.values train, test = X[1:len(X) - 7], X[len(X) - 7:] train_X, train_y = train[:, 0], train[:, 1] test_X, test_y = test[:, 0], test[:, 1] # persistence model def model_persistence(x): return x # walk-forward validation predictions = [] for x in test_X: yhat = model_persistence(x) predictions.append(yhat) test_score = mean_squared_error(test_y, predictions) print('Test MSE: %.3f' % test_score) # plot predictions vs expected plt.plot(test_y, label='Test') plt.plot(predictions, label='Prediction') plt.legend(loc='best') plt.show()''' '''X = data_ch0['base_mean'].values train, test = X[1:len(X) - 7], X[len(X) - 7:] # train autoregression model = AutoReg(train, lags=7) model_fit = model.fit() print('Coefficients: %s' % model_fit.params) # make predictions predictions = model_fit.predict(start=len(train), end=len(train) + len(test) - 1, dynamic=False) for i in range(len(predictions)): print('predicted=%f, expected=%f' % (predictions[i], test[i])) rmse = np.sqrt(mean_squared_error(test, predictions)) print('Test RMSE: %.3f' % rmse) # plot results plt.plot(test, label='test') plt.plot(predictions, label='prediction') plt.legend() plt.show()''' '''data = pd.read_csv("/Users/williamquinn/Desktop/PMT_Project/res_vs_time.csv", header=0, dtype={0: int, 1: int, 2: float, 3: float, 4: float, 5: int, 6: float, 7: float}, engine='python') data['date'] = data['date'].astype(str) data = data[data["pmt"] == 0] f = [] for date in data['date'].values: new_date = date[4] + date[5] + '/' + date[2] + date[3] + '/' + '20' + date[0] + date[1] f.append(new_date) data['date'] = pd.DatetimeIndex(f, dayfirst=True) data.set_index('date', inplace=True) data = data[~data.index.duplicated()] data = data.asfreq('D') data["base_mean"] = data["base_mean"].fillna(data["base_mean"].mean())''' '''decomposed = seasonal_decompose(data['base_mean'], model='additive') decomposed.plot() # See note below about this plt.show()''' # data['stationary'] = data['base_mean'].diff() '''decomposed = seasonal_decompose(data['stationary'][1:], model='additive') decomposed.plot() # See note below about this plt.show()''' '''# create train/test datasets
def plot_res(): data = pd.read_csv( "/Users/williamquinn/Desktop/PMT_Project/res_vs_time.csv", header=0, dtype={ 0: int, 1: int, 2: float, 3: float, 4: float, 5: int, 6: float, 7: float }, engine='python') data["time"] = process_date(data["date"]) data = data[(data['date'] == 191023) | (data['date'] == 200212) | (data['date'] == 200429) | (data['date'] == 200717) | (data['date'] == 201015) | (data['date'] == 201216)] plt.figure(figsize=figsize, facecolor='white') sel_dates = [191023, 200212, 200429, 200717, 201015, 201216] chi_cut = 10 data_ch0 = data[data["pmt"] == 0] x, y, dy = data_ch0[data_ch0['chi_2'] / data_ch0['ndof'] < chi_cut]['time'].values, \ data_ch0[data_ch0['chi_2'] / data_ch0['ndof'] < chi_cut]['res'].values * 100, \ data_ch0[data_ch0['chi_2'] / data_ch0['ndof'] < chi_cut]['res_err'].values * 100 exposed_fit = fit_straight_line(x, y, dy, guess=[0, 0]) plt.errorbar(x, y, yerr=dy, fmt='k.', label='Exposed', markersize=3, capsize=1, linewidth=1, capthick=1) plt.plot([x[0], x[-1]], linear([x[0], x[-1]], *exposed_fit["popt"]), 'r-') chi_cut = 10 data_ch1 = data[data["pmt"] == 1] x, y, dy = data_ch1[data_ch1['chi_2'] / data_ch1['ndof'] < chi_cut]['time'].values, \ data_ch1[data_ch1['chi_2'] / data_ch1['ndof'] < chi_cut]['res'].values * 100, \ data_ch1[data_ch1['chi_2'] / data_ch1['ndof'] < chi_cut]['res_err'].values * 100 control_fit = fit_straight_line(x, y, dy, guess=[0, 0]) plt.errorbar(x, y, yerr=dy, fmt='.', label='Control', markersize=3, capsize=1, linewidth=1, capthick=1, color='grey', ecolor='grey') plt.plot([x[0], x[-1]], linear([x[0], x[-1]], *control_fit["popt"]), 'r-') plt.ylim(1.5, 5) plt.xlabel('Days from 1% He Onset') plt.ylabel('Resolution at 1MeV /%') plt.fill_between([-100, 0], [5, 5], alpha=0.1, facecolor='green', label='Atmospheric He') plt.fill_between([0, 98], [5, 5], alpha=0.1, facecolor='blue', label='1% He') plt.fill_between([98, 500], [5, 5], alpha=0.1, facecolor='red', label='10% He') plt.xlim(-30, 430) plt.legend(loc='best') plt.tight_layout() plt.savefig("/Users/williamquinn/Desktop/PMT_Project/res.pdf") plt.close()
def main(): args = io_parse_arguments() data_file_Ch0_name = args.i + "resolution_vs_date_Ch0.txt" data_file_Ch1_name = args.i + "resolution_vs_date_Ch1.txt" try: data_file_Ch0 = open(data_file_Ch0_name, 'r') data_file_Ch1 = open(data_file_Ch1_name, 'r') except FileNotFoundError as fnf_error: print(fnf_error) raise Exception("Error opening data file") data_Ch0 = np.loadtxt(data_file_Ch0_name, delimiter=',', dtype={ 'names': ('unix', 'date', 'timestamp', 'mu', 'mu_err', 'sigma', 'sigma_err', 'res', 'chi_err'), 'formats': ('i4', 'i4', 'i4', 'f4', 'f4', 'f4', 'f4', 'f4', 'f4')}, unpack=True) data_Ch1 = np.loadtxt(data_file_Ch1_name, delimiter=',', dtype={'names': ( 'unix', 'date', 'timestamp', 'mu', 'mu_err', 'sigma', 'sigma_err', 'res', 'chi_err'), 'formats': ('i4', 'i4', 'i4', 'f4', 'f4', 'f4', 'f4', 'f4', 'f4')}, unpack=True) data = [data_Ch0, data_Ch1] dates = [data_Ch0[1], data_Ch1[1]] day_nums = [process_date(data_Ch0[1]), process_date(data_Ch1[1])] exposures = [process_exposure(data_Ch0[1]), process_exposure(data_Ch1[1])] mus = [data_Ch0[3], data_Ch1[3]] mu_errs = [data_Ch0[4], data_Ch1[4]] sigs = [data_Ch0[5], data_Ch1[5]] sig_errs = [data_Ch0[6], data_Ch1[6]] res = [data_Ch0[7], data_Ch1[7]] res_errs = [res[0] * np.sqrt((sig_errs[0] / sigs[0]) ** 2 + (mu_errs[0] / mus[0]) ** 2), res[1] * np.sqrt((sig_errs[1] / sigs[1]) ** 2 + (mu_errs[1] / mus[1]) ** 2)] chis = [data_Ch0[8], data_Ch1[8]] p_guess = [0.02420914, 3] p_bounds = [[0, 0], [1, 4]] filter_lists = [[], []] for i in range(2): x = day_nums[i] filter_list = [] for chi_2 in chis[i]: if chi_2 < 10: filter_list.append(True) else: filter_list.append(False) for index,element in enumerate(x): if element < 0: filter_list[index] = False else: pass filter_lists[i] = filter_list x_array = np.linspace(np.amin(x[filter_list]), np.amax(x[filter_list]), 10000) popt, pcov = curve_fit(linear, x[filter_list], res[i][filter_list], sigma=res_errs[i][filter_list], p0=p_guess, bounds=p_bounds, maxfev=5000) chi_2 = chi2(linear(x[filter_list], *popt), res_errs[i][filter_list], res[i][filter_list], 2) print(popt, pcov, chi_2) fig, ax1 = plt.subplots() ax1.set_title("1MeV PMT Resolution Ch{}".format(i)) ax1.grid(True) color = 'tab:red' plt.errorbar(x[filter_list],res[i][filter_list], res_errs[i][filter_list], fmt='.', color=color) plt.plot(x_array, linear(x_array, *popt), 'g') if i ==0: plt.axvline(98, color='r', ls='--') plt.axvline(0, color='b', ls='--') ax1.text(0, 3, 'linear fit: y = ({:.4f}±{:.4f})*x + ({:.3f}±{:.3f}) chi2R: {:.3f}'.format(popt[0], np.sqrt( pcov[0][0]), popt[1], np.sqrt( pcov[1][1]), chi_2)) ax1.set_xlabel("Exposure Time /days") ax1.set_ylabel("1Mev Resolution /%", color=color) ax1.set_ylim(2, 4.25) ax1.tick_params(axis='y', labelcolor=color) color = 'tab:blue' ax2 = ax1.twinx() ax2.plot(x[filter_list], chis[i][filter_list], '.', color=color) ax2.set_ylabel('Chi2 of bismuth 1MeV fit', color=color) # we already handled the x-label with ax1 ax2.tick_params(axis='y', labelcolor=color) ax2.set_ylim(0, 10) fig.tight_layout() plt.savefig("/Users/willquinn/Desktop/resolution_vs_date_time_" + str(i)) plt.close() for i in range(2): x = exposures[i] filter_list = [] for chi_2 in chis[i]: if chi_2 < 10: filter_list.append(True) else: filter_list.append(False) for index,element in enumerate(x): if element < 0.01: filter_list[index] = False else: pass filter_lists[i] = filter_list x_array = np.linspace(np.amin(x[filter_list]), np.amax(x[filter_list]), 10000) popt, pcov = curve_fit(linear, x[filter_list], res[i][filter_list], sigma=res_errs[i][filter_list], p0=p_guess, bounds=p_bounds, maxfev=5000) chi_2 = chi2(linear(x[filter_list], *popt), res_errs[i][filter_list], res[i][filter_list], 2) print(popt, pcov, chi_2) fig, ax1 = plt.subplots() ax1.set_title("1MeV PMT Resolution Ch{}".format(i)) ax1.grid(True) color = 'tab:red' plt.errorbar(x[filter_list],res[i][filter_list], res_errs[i][filter_list], fmt='.', color=color) plt.plot(x_array, linear(x_array, *popt), 'g') if i == 0: plt.axvline(1/10 + 97/100, color='r', ls='--') plt.axvline(29/1000000, color='b', ls='--') ax1.text(0, 3, 'linear fit: y = ({:.4f}±{:.4f})*x + ({:.3f}±{:.3f}) chi2R: {:.3f}'.format(popt[0], np.sqrt( pcov[0][0]), popt[1], np.sqrt( pcov[1][1]), chi_2)) ax1.set_xlabel("Exposure Time /atm-days") ax1.set_ylabel("1Mev Resolution /%", color=color) ax1.set_ylim(2, 4.25) ax1.tick_params(axis='y', labelcolor=color) color = 'tab:blue' ax2 = ax1.twinx() ax2.plot(x[filter_list], chis[i][filter_list], '.', color=color) ax2.set_ylabel('Chi2 of bismuth 1MeV fit', color=color) # we already handled the x-label with ax1 ax2.tick_params(axis='y', labelcolor=color) ax2.set_ylim(0, 10) fig.tight_layout() plt.savefig("/Users/willquinn/Desktop/resolution_vs_date_exp_" + str(i)) plt.close() x_t = [] x_e = [] y = [] y_err = [] for i in range(day_nums[0].size): for j in range(day_nums[1].size): if day_nums[0][i] == day_nums[1][j]: x_t.append(day_nums[0][i]) x_e.append(exposures[0][i]) y.append(res[0][i]/res[1][j]) y_err.append(res[0][i]/res[1][j] * np.sqrt( (res_errs[0][i]/res[0][i])**2 + (res_errs[1][j]/res[1][j])**2 )) break plt.errorbar(x_t, y, yerr=y_err, fmt="k.") plt.axvline(98, color="r", ls="--") plt.axvline(0, color="b", ls="--") plt.xlabel("Exposure /days") plt.ylabel("Ratio res_Ch0/res_Ch1") plt.title("Ratio of resolution of CH 0 & 1 vs time") plt.grid() plt.xlim(np.amin(np.array(x_t)), np.amax(np.array(x_t))) plt.ylim(0,2) plt.savefig("/Users/willquinn/Desktop/resolution_ratio_time") '''x_ch0 = process_date(data_Ch0[1]) x_ch1 = process_date(data_Ch1[1]) x_array = np.linspace(-30, 145, 10000)''' '''plt.errorbar(x_ch0, data_Ch0[3], data_Ch0[4], fmt='.', color='b') plt.title("1MeV PMT Charge Ch0") plt.xlabel("Exposure Time atm-days") plt.ylabel("1Mev Charge /pC") plt.ylim(32, 42) #plt.xscale('log') plt.grid(True) plt.show() plt.errorbar(x_ch1, data_Ch1[3], data_Ch1[4], fmt='.', color='b') plt.title("1MeV PMT Charge Ch1") plt.xlabel("Exposure Time /days") plt.ylabel("1Mev Charge /pC") plt.ylim(32, 42) #plt.xscale('log') plt.grid(True) plt.show()''' '''popt_0, pcov_0 = curve_fit(linear, x_ch0, data_Ch0[7],
def main(): # Handle the input arguments: ############################## args = pmt_parse_arguments() input_directory = args.i # config_file_name = args.c output_directory = args.o ############################## out_files = [ output_directory + '/res_vs_time_ch0.txt', output_directory + '/res_vs_time_ch1.txt' ] create_file(out_files[0]) create_file(out_files[1]) filenames_txt = input_directory + "/filenames.txt" try: print(">>> Reading data from file: {}".format(filenames_txt)) date_file = open(filenames_txt, 'r') except FileNotFoundError as fnf_error: print(fnf_error) raise Exception("Error opening data file {}".format(filenames_txt)) filenames = np.loadtxt(filenames_txt, delimiter=',', dtype={ 'names': ['filename'], 'formats': ['S100'] }, unpack=True) topology = [2, 1] pmt_array = PMT_Array(topology, "summary") pmt_array.set_pmt_id("GAO607", 0) pmt_array.set_pmt_id("GAO612", 1) '''# Set the cuts you wish to apply # If you don't do this the defaults are used if config_file_name is not None: pmt_array.apply_setting(config_file_name) # print_settings(pmt_array)''' # Set up the containers for the summary resolutions = [[] for i in range(pmt_array.get_pmt_total_number())] resolutions_err = [[] for i in range(pmt_array.get_pmt_total_number())] dates = [[] for i in range(pmt_array.get_pmt_total_number())] gains = [[] for i in range(pmt_array.get_pmt_total_number())] gains_err = [[] for i in range(pmt_array.get_pmt_total_number())] baseline_means = [[] for i in range(pmt_array.get_pmt_total_number())] baseline_sigs = [[] for i in range(pmt_array.get_pmt_total_number())] fit_chi2 = [[] for i in range(pmt_array.get_pmt_total_number())] for i_file in tqdm.tqdm(range(filenames.size)): file = filenames[i_file][0].decode("utf-8") date = file.split("_")[0] voltage = int(file.split("_")[1].split("A")[1]) if voltage == 1000: pass else: continue fit_parameters = read_file(date, voltage, input_directory + "/" + file, pmt_array, output_directory) for i_om in range(pmt_array.get_pmt_total_number()): if len(fit_parameters[i_om]) > 0: pass else: continue mu = fit_parameters[i_om][0]["mu"] mu_err = fit_parameters[i_om][0]["mu_err"] sig = fit_parameters[i_om][0]["sig"] sig_err = fit_parameters[i_om][0]["sig_err"] chi_2 = fit_parameters[i_om][0]["chi2"] gain = fit_parameters[i_om][0]["gain"] baseline_mean = fit_parameters[i_om][0]["base_mu"] baseline_sig = fit_parameters[i_om][0]["base_sig"] res, res_err = get_resolution(mu, mu_err, sig, sig_err) resolutions[i_om].append(res) resolutions_err[i_om].append(res_err) dates[i_om].append(int(date)) gains[i_om].append(gain) baseline_means[i_om].append(baseline_mean) baseline_sigs[i_om].append(baseline_sig) fit_chi2[i_om].append(chi_2) write_to_file( out_files[i_om], '{},{},{},{},{}'.format(date, res, res_err, chi_2, gain)) # Plot individual summaries for i_om in range(pmt_array.get_pmt_total_number()): if len(resolutions[i_om]) > 0: pass else: continue date = process_date(dates[i_om]) try: start = np.where(date == 0)[0][0] except: start = np.where(date == 1)[0][0] mid = np.where(date == 98)[0][0] # print("start:",start) plt.plot(date[:start + 1], np.array(gains[i_om][:start + 1]) * 2, "g.", label="Atmospheric He") plt.plot(date[start + 1:mid + 1], np.array(gains[i_om][start + 1:mid + 1]) * 2, "b.", label="1% He") plt.plot(date[mid + 1:], np.array(gains[i_om][mid + 1:]) * 2, "r.", label="10% He") plt.axvline(date[start], 0, 100, ls='--', color='k') plt.axvline(date[mid], 0, 100, ls='--', color='k') plt.xlabel("exposure days relative to 190611") plt.ylabel("PMT gain at 1Mev /mV") plt.title( pmt_array.get_pmt_object_number(i_om).get_pmt_id() + " Gain at 1MeV vs exposure time") plt.grid() plt.ylim(150, 300) plt.legend(loc='lower right') plt.savefig(output_directory + "/summary_plots/" + pmt_array.get_pmt_object_number(i_om).get_pmt_id() + "_gains_vs_time.png") plt.close() plt.errorbar(date, baseline_means[i_om], yerr=baseline_sigs[i_om], fmt='k.-', ecolor='r') plt.grid() plt.xlabel("exposure days relative to 190611") plt.ylabel("Baseline mean /mV") plt.title( pmt_array.get_pmt_object_number(i_om).get_pmt_id() + " Baseline mean vs exposure time") plt.savefig(output_directory + "/summary_plots/" + pmt_array.get_pmt_object_number(i_om).get_pmt_id() + "_baseline_mean_vs_time.png") plt.close() '''plt.plot(date, baseline_sigs[i_om]) plt.xlabel("exposure days relative to 190611") plt.ylabel("Baseline std-dev") plt.title(pmt_array.get_pmt_object_number(i_om).get_pmt_id() + " Baseline std-dev vs exposure time") plt.savefig(output_directory + "/summary_plots/" + pmt_array.get_pmt_object_number(i_om).get_pmt_id() + "_baseline_sigma_vs_time") plt.close()''' res_filter = [] for i_date in range(len(date)): if 1 < resolutions[i_om][i_date] < 3.75 and fit_chi2[i_om][ i_date] < 10: res_filter.append(True) else: res_filter.append(False) popt, pcov = curve_fit( linear, np.array(date[start:])[res_filter[start:]], np.array(resolutions[i_om][start:])[res_filter[start:]], sigma=np.array(resolutions_err[i_om][start:])[res_filter[start:]], p0=[0.001, 2], bounds=[[0, 0], [0.002, 3.5]], maxfev=500000) x_array = np.linspace(date[start], np.amax(date), 2) chi_2 = chi2( np.array(resolutions[i_om][start:])[res_filter[start:]], np.array(resolutions_err[i_om][start:])[res_filter[start:]], linear(date[start:][res_filter[start:]], *popt), len(popt)) plt.errorbar(date[:start + 1], resolutions[i_om][:start + 1], yerr=resolutions_err[i_om][:start + 1], fmt="g.", label="Atmospheric He") plt.plot(np.array(date[start:])[res_filter[start:]], np.array(resolutions[i_om][start:])[res_filter[start:]], 'ko', label="used values") plt.errorbar(date[start + 1:mid + 1], resolutions[i_om][start + 1:mid + 1], yerr=resolutions_err[i_om][start + 1:mid + 1], fmt="b.", label="1% He") plt.errorbar(date[mid + 1:], resolutions[i_om][mid + 1:], yerr=resolutions_err[i_om][mid + 1:], fmt="r.", label="10% He") plt.plot(x_array, linear(x_array, *popt), 'k-') plt.xlabel( "exposure days relative to 190611 \n $y = (${:.1e}$ ± ${:.0e})$x + (${:.1e}$ ± ${:.0e}$)$ $\chi^2_R = {:.2}$" .format(popt[0], np.sqrt(pcov[0, 0]), popt[1], np.sqrt(pcov[1, 1]), chi_2)) plt.ylabel("Resolution at 1MeV /% $\sigma / \mu$") plt.title( pmt_array.get_pmt_object_number(i_om).get_pmt_id() + " Resolution vs exposure time") plt.axvline(date[start], 0, 100, ls='--', color='k') plt.axvline(date[mid], 0, 100, ls='--', color='k') plt.grid() plt.ylim(2, 4.5) plt.xlim(np.amin(date), np.amax(date)) plt.legend(loc='upper right') plt.tight_layout() plt.savefig(output_directory + "/summary_plots/" + pmt_array.get_pmt_object_number(i_om).get_pmt_id() + "_resolution_vs_time.png") plt.close() plt.plot(np.array(date[start:])[res_filter[start:]], np.array(fit_chi2[i_om][start:])[res_filter[start:]], 'ko', label="used values") plt.plot(date[:start + 1], fit_chi2[i_om][:start + 1], "g.", label="Atmospheric He") plt.plot(date[start + 1:mid + 1], fit_chi2[i_om][start + 1:mid + 1], "b.", label="1% He") plt.plot(date[mid + 1:], fit_chi2[i_om][mid + 1:], "r.", label="10% He") plt.xlabel("exposure days relative to 190611") plt.ylabel("$\chi^2_R$") plt.title( pmt_array.get_pmt_object_number(i_om).get_pmt_id() + " Resolution fit $\chi^2_R$ vs exposure time") plt.grid() plt.axvline(date[start], 0, 100, ls='--', color='k') plt.axvline(date[mid], 0, 100, ls='--', color='k') plt.legend(loc='upper right') plt.xlim(np.amin(date), np.amax(date)) plt.ylim(0, 10) plt.tight_layout() plt.savefig(output_directory + "/summary_plots/" + pmt_array.get_pmt_object_number(i_om).get_pmt_id() + "_resolution_vs_time_chi2.png") plt.close() # Plot ratio x_date = [] ratio = [] ratio_err = [] gain_ratio = [] for i in range(len(dates[0])): for j in range(len(dates[1])): if dates[0][i] == dates[1][j]: x_date.append(dates[0][i]) ratio.append(resolutions[0][i] / resolutions[1][j]) ratio_err.append( resolutions[0][i] / resolutions[1][j] * np.sqrt((resolutions_err[0][i] / resolutions[0][i])**2 + (resolutions_err[1][j] / resolutions[1][j])**2)) gain_ratio.append(gains[0][i] / gains[1][j]) break popt, pcov = curve_fit(linear, x_date, ratio, sigma=ratio_err, p0=[1, 1], bounds=[[0, 0], [10, 10]]) x_date = process_date(x_date) x_array = np.linspace(np.amin(x_date), np.amax(x_date), 2) chi_2 = chi2(ratio, ratio_err, linear(x_date, *popt), 2) plt.errorbar(x_date, ratio, yerr=ratio_err, fmt="k.") plt.plot( x_array, linear(x_array, *popt), "g-", label= "$y = (${:.1e}$ ± ${:.0e})$\\times x + (${:.1e}$ ± ${:.0e}$) \chi^2_R = {:.2}$" .format(popt[0], np.sqrt(pcov[0, 0]), popt[1], np.sqrt(pcov[1, 1]), chi_2)) plt.axvline(98, color="r", ls="--") plt.axvline(0, color="b", ls="--") plt.xlabel("exposure days relative to 190611") plt.ylabel("Ratio res_Ch0/res_Ch1") plt.title("Ratio of resolution of CH 0 & 1 vs time") plt.grid() plt.xlim(np.amin(np.array(x_date)), np.amax(np.array(x_date))) plt.ylim(0, 2) plt.savefig(output_directory + "/summary_plots/resolution_ratio_vs_time.png") plt.close() plt.plot(x_date, gain_ratio, "k.") plt.axvline(98, color="r", ls="--") plt.axvline(0, color="b", ls="--") plt.xlabel("exposure days relative to 190611") plt.ylabel("Ratio gain_Ch0/gain_Ch1") plt.title("Ratio of gain of CH 0 & 1 vs time") plt.grid() plt.xlim(np.amin(np.array(x_date)), np.amax(np.array(x_date))) plt.ylim(0.7, 1) plt.savefig(output_directory + "/summary_plots/gain_ratio_vs_time.png") plt.close() print("<<<< FINISHED >>>")
def plot_aapc_vs_charge(dates, ratios, output_directory: str, name: str): date_0 = process_date(dates[0]) date_1 = process_date(dates[1]) try: start = np.where(date_0 == 0)[0][0] except: start = np.where(date_0 == 1)[0][0] mid = np.where(date_0 == 98)[0][0] ratio_0 = np.array(ratios[0]) ratio_1 = np.array(ratios[1]) x = np.array(date_0[mid + 1:]) - date_0[mid + 1:][0] y = np.array(ratio_0[mid + 1:]) yerr = np.array(ratio_0[mid + 1:]) * 0.01 pars, errs, chi = root_fit(x, y, yerr) fig1 = plt.figure(num=None, figsize=(9, 5), dpi=80, facecolor='w', edgecolor='k') frame1 = fig1.add_axes((.1, .3, .8, .6)) frame1.set_xticklabels([]) plt.errorbar(date_0[:start + 1], ratio_0[:start + 1], zorder=0, yerr=ratio_0[:start + 1] * 0.01, fmt="g.", label="Atmospheric He") plt.errorbar(date_0[start + 1:mid + 1], ratio_0[start + 1:mid + 1], zorder=0, yerr=ratio_0[start + 1:mid + 1] * 0.01, fmt="b.", label="1% He") plt.errorbar(date_0[mid + 1:], ratio_0[mid + 1:], zorder=0, yerr=ratio_0[mid + 1:] * 0.01, fmt="r.", label="10% He") plt.errorbar(date_1, ratio_1, zorder=0, yerr=ratio_1 * 0.01, fmt="C1.", label="Control") plt.plot(date_0[mid + 1:], Model().func(x, pars), 'k-', label='model', zorder=10) plt.axvline(date_0[start], 0, 100, ls='--', color='k') plt.axvline(date_0[mid], 0, 100, ls='--', color='k') handles, labels = plt.gca().get_legend_handles_labels() patch = matplotlib.patches.Patch(color='white', label=r'$P_0 =$ {:.4e} ± {:.0e}'.format( pars[0], errs[0])) patch_1 = matplotlib.patches.Patch(color='white', label=r'$P_1 =$ {:.4e} ± {:.0e}'.format( pars[1], errs[1])) patch_2 = matplotlib.patches.Patch(color='white', label=r'$L =$ {:.0f} ± {:.0f}'.format( pars[2] / (3600 * 24), errs[2] / (3600 * 24))) patch_3 = matplotlib.patches.Patch( color='white', label=r'$\chi^2_R =$ {:.2f}'.format(chi)) handles.extend([patch, patch_1, patch_2, patch_3]) plt.ylabel("charge ratio") plt.title("Pulse charge apulse charge ratio") plt.grid() plt.legend(handles=handles, loc='upper left') plt.xlim(-30, 420) frame2 = fig1.add_axes((.1, .1, .8, .2)) plt.xlabel("exposure days relative to 06/11/2019") plt.axhline(0, ls='--', color='black') plt.ylabel("(model-data)/model") plt.grid() plt.xlim(-30, 420) plt.errorbar( date_0[mid + 1:], (Model().func(x, pars) - ratio_0[mid + 1:]) / Model().func(x, pars), yerr=ratio_0[mid + 1:] * 0.01 / Model().func(date_0[mid + 1:], pars), fmt="k.") plt.savefig(output_directory + "/summary_plots/" + "ap_charge_charge_vs_time" + name + ".pdf") plt.close()