diff --git a/multi_pixel_plots.py b/multi_pixel_plots.py new file mode 100644 index 0000000000000000000000000000000000000000..5cb02cdcad09153f9c4acb5d6c219399a72f03b0 --- /dev/null +++ b/multi_pixel_plots.py @@ -0,0 +1,417 @@ +import matplotlib.pyplot as plt +import numpy as np +import pandas as pd +import os +import json +import pickle + +from sklearn.preprocessing import PolynomialFeatures +from sklearn.linear_model import LinearRegression + +q = 15 +if q == 25: + exrange = [220, 550] +else: + exrange = [220, 425] +nl1a = 3200 +countlim = .98*nl1a +callim = 5000000 +path = 'results/multipix_compiled/' +pixels = [f for f in os.listdir(path) if not '.' in f]#[:15] +df = pd.DataFrame() +temp = [] +for pix in pixels: + i = int(pix.split('c')[0][1:]) + j = int(pix.split('c')[1]) + pixpath = path + pix + f'/Qinj_scan_L1A_504_{q}.pkl' + if os.path.exists(pixpath): + try: + data = pickle.load(open(pixpath, 'rb')) + if len(np.unique(data.hits)) > 1: + data['row'] = [i]*len(data) + data['col'] = [j]*len(data) + df = pd.concat([df, data]) + print(f'Successfully pulled data for Row {i} Col {j}.') + temp.append(pix) + except: + print(f'Found but failed to retrieve data for Row {i} Col {j}.') +pixels = temp +print(df[df.hits > 0].head()) + +fitdata = {} +fig, ax = plt.subplots(figsize = (9,7)) +if len(pixels) <= 5: + subset = pixels +else: + #subset = np.random.choice(pixels, 5, replace = False) + subset = ['r2c5', 'r3c8', 'r13c11', 'r1c15', 'r8c9'] +calgrid = np.zeros([16, 16]) +calstdgrid = np.zeros([16, 16]) +calgridtxt = [[i for i in range(16)] for i in range(16)] +calstdgridtxt = [[i for i in range(16)] for i in range(16)] +for i in range(16): + for j in range(16): + data = df[(df.row == i)&(df.col == j)] + cal = [] + for n in range(len(data)): + if data.hits.iloc[n] == nl1a: + cal += data.cal.iloc[n] + print(i, j, len(cal)) + if len(cal) == 0: + calgrid[i, j] = 5000 + calstdgrid[i, j] = 5000 + calgridtxt[j][i] = '-' + calstdgridtxt[j][i] = '-' + else: + cals = np.unique(cal) + calcounts = [cal.count(c) for c in cals] + calgrid[i, j] = cals[np.argsort(-np.array(calcounts))[0]] + #calgrid[i, j] = np.mean(cal) + calstdgrid[i, j] = np.std(cal) + calgridtxt[j][i] = str(int(np.mean(cal))) + calstdgridtxt[j][i] = str(np.round(np.std(cal), 2)) + +for i in range(16): + for j in range(16): + if calgridtxt[i][j] == '-': + calgrid[j, i] = np.min(calgrid) + calstdgrid[j, i] = np.min(calstdgrid) + +fig, ax = plt.subplots(figsize = (9, 7)) +ax.set_title(f'CAL Code Means Qinj = {q}') +ax.set_xlabel('Col') +ax.set_ylabel('Row') +clim = .6*(np.max(calgrid) - np.min(calgrid)) + np.min(calgrid) +im = ax.imshow(calgrid, cmap = 'Purples') +for i in range(16): + for j in range(16): + if calgrid[j, i] > clim: + color = 'white' + else: + color = 'black' + ax.text(i, j, calgridtxt[i][j], fontsize = 8, ha = 'center', va = 'center', color = color) +cbar = ax.figure.colorbar(im, ax = ax) +plt.savefig(path + f'DAC_v_CAL_{q}.pdf') +plt.savefig(path + f'DAC_v_CAL_{q}.png') + +fig, ax = plt.subplots(figsize = (9, 7)) +ax.set_title(f'CAL Code STD Qinj = {q}') +ax.set_xlabel('Col') +ax.set_ylabel('Row') +clim = .6*(np.max(calstdgrid) - np.min(calstdgrid)) + np.min(calstdgrid) +im = ax.imshow(calstdgrid, cmap = 'Purples') +for i in range(16): + for j in range(16): + if calstdgrid[j, i] > clim: + color = 'white' + else: + color = 'black' + ax.text(i, j, calstdgridtxt[i][j], fontsize = 8, ha = 'center', va = 'center', color = color) +cbar = ax.figure.colorbar(im, ax = ax) +plt.savefig(path + f'DAC_v_CALSTD_{q}.pdf') +plt.savefig(path + f'DAC_v_CALSTD_{q}.png') +plt.close() + +fig, ax = plt.subplots(figsize = (9, 7)) +ax.set_title(f'Time of Arrival Standard Deviation v. Threshold DAC\nQinj = {q}') +ax.set_xlabel('Threshold DAC') +ax.set_ylabel('TOA Std. Dev.') +for pix in subset: + i = int(pix.split('c')[0][1:]) + j = int(pix.split('c')[1]) + data = df[(df.row == i)&(df.col == j)] + + x = [] + y = [] + for n in range(len(data)): + subtoa = np.array(data.toa.iloc[n]) + subcal = np.array(data.cal.iloc[n]) + if len(subtoa) > countlim: + subtoa = subtoa[(np.abs(subcal - calgrid[i, j]) < callim).nonzero()[0]] + if len(subtoa) > countlim and np.std(subtoa) < 2: + x.append(data.vth.iloc[n]) + y.append(np.std(subtoa)) + if len(x) > 10: + transformer = PolynomialFeatures(degree = 4) + model = LinearRegression() + X = transformer.fit_transform(np.array(x).reshape(-1, 1)) + model.fit(X, y) + X = transformer.fit_transform(np.array(data.vth).reshape(-1, 1)) + fitdata[pix] = {'x': data.vth, 'y':model.predict(X), 'ydat':y} + ax.plot(x, y, 'o', label = f'Row {i} Col {j}') + + +plt.plot(exrange, [0.5, 0.5], label = 'TOA SD = 0.5') +plt.plot(exrange, [1, 1], label = 'TOA SD = 1') + +ax.set_ylim([0, 2]) +ax.set_xlim(exrange) +plt.legend() +plt.savefig(path + f'DAC_v_TOASD_multi_{q}.png') +plt.savefig(path + f'DAC_v_TOASD_multi_{q}.pdf') +plt.close() + +plt.figure(figsize = (9,7)) +plt.title(f'Hits v. Threshold DAC\nQinj = {q}') +plt.xlabel('Threshold DAC') +plt.ylabel('Hits') + +fig, ax = plt.subplots(figsize = (9,7)) + +ax.set_title(f'Time of Arrival Standard Deviation Fits v. Threshold DAC\nQinj = {q}') +ax.set_xlabel('Threshold DAC') +ax.set_ylabel('TOA Std. Dev. Prediction') + +for pix in fitdata: + i = pix.split('c')[0][1:] + j = pix.split('c')[1] + ax.plot(fitdata[pix]['x'], fitdata[pix]['y'], label = f'Row {i} Col {j}') +ax.set_ylim([0, 2]) +ax.set_xlim(exrange) +plt.plot(exrange, [0.5, 0.5], label = 'TOA SD = 0.5') +plt.plot(exrange, [1, 1], label = 'TOA SD = 1') + +plt.legend() +plt.savefig(path + f'DAC_v_TOTSD_Fit_multi_{q}.png') + +fig, ax = plt.subplots(figsize = (9,7)) + +ax.set_title(f'CAL Code v. Threshold DAC\nQinj = {q}') +ax.set_xlabel('Threshold DAC') +ax.set_ylabel('TOA Std. Dev. Prediction') + +for pix in fitdata: + i = int(pix.split('c')[0][1:]) + j = int(pix.split('c')[1]) + data = df[(df.row == i)&(df.col == j)] + x = [] + y = [] + err = [] + for n in range(len(data)): + if data.hits.iloc[n] > 10: + x.append(data.vth.iloc[n]) + y.append(np.mean(data.cal.iloc[n])) + err.append(np.std(data.cal.iloc[n])) + ax.errorbar(x, y, err, capsize = 5, alpha = 0.7, label = f'Row {i} Col {j}') +#ax.set_ylim([0]) +ax.set_xlim(exrange) +#ax2 = ax.secondary_yaxis('right', functions = (forward, backward)) +#ax2.set_ylabel('Jitter (ps)') +#plt.plot(exrange, [0.5, 0.5], label = 'TOA SD = 0.5') +#plt.plot(exrange, [1, 1], label = 'TOA SD = 1') + +plt.legend() +plt.savefig(path + f'DAC_v_CAL_multi_{q}.png') +plt.savefig(path + f'DAC_v_CAL_multi_{q}.pdf') + +mins = np.zeros([16, 16]) - 50000 +minstxt = [['u' for i in range(16)] for i in range(16)] +minsconv = np.zeros([16, 16]) - 50000 +minsconvtxt = [['u' for i in range(16)] for i in range(16)] +widths = np.zeros([16, 16]) + 5000 +widthstxt = [['u' for i in range(16)] for i in range(16)] +datmin = np.zeros([16, 16]) - 50000 +dattxt = [['u' for i in range(16)] for i in range(16)] + +for i in range(16): + for j in range(16): + data = df[(df.row == i)&(df.col == j)] + pix = f'r{i}c{j}' + if pix in pixels and not pix in fitdata: + x = [] + ydat = [] + for n in range(len(data)): + subtoa = np.array(data.toa.iloc[n]) + subcal = np.array(data.cal.iloc[n]) + if len(subtoa) > countlim: + subtoa = subtoa[(np.abs(subcal - calgrid[i, j]) < callim).nonzero()[0]] + if len(subtoa) > countlim and np.std(subtoa) < 2: + x.append(data.vth.iloc[n]) + ydat.append(np.std(subtoa)) + if len(x) > 10: + transformer = PolynomialFeatures(degree = 4) + model = LinearRegression() + X = transformer.fit_transform(np.array(x).reshape(-1, 1)) + model.fit(X, ydat) + X = transformer.fit_transform(np.array(data.vth).reshape(-1, 1)) + y = model.predict(X) + + elif pix in pixels and pix in fitdata: + y = fitdata[pix]['y'] + ydat = fitdata[pix]['ydat'] + + if pix in pixels: + try: + ydiff = [y[n] - y[n-1] for n in range(1, len(y))] + lim = 0.05 + starter = [diff > -lim for diff in ydiff] + ender = [diff < lim for diff in ydiff] + startidx = starter.index(True) + ender.reverse() + endidx = len(ender) - ender.index(True) + if np.min(y) > 2 or np.min(y) < 0: + stupidshittodosomenign + mins[i, j] = np.min(y) + minstxt[j][i] = f'{np.round(mins[i, j], 2)}' + widths[i, j] = int(data.vth.iloc[endidx] - data.vth.iloc[startidx]) + widthstxt[j][i] = f'{int(widths[i, j])}' + datmin[i, j] = np.min(ydat) + dattxt[j][i] = f'{np.round(datmin[i, j], 2)}' + print(mins[i, j], minstxt[j][i], widths[i, j], widthstxt[j][i]) + except: + minstxt[j][i] = 'F' + widthstxt[j][i] = 'F' + dattxt[j][i] = 'F' + + else: + minstxt[j][i] = 'M' + widthstxt[j][i] = 'M' + dattxt[j][i] = 'M' + +minmin = np.max(mins) +widthmin = np.min(widths) +datminmin = np.max(datmin) +for i in range(16): + for j in range(16): + if minstxt[j][i] == 'M' or minstxt[j][i] == 'F': + mins[i, j] = minmin + widths[i,j] = widthmin + datmin[i, j] = datminmin + +fig, ax = plt.subplots(figsize = (9, 7)) +ax.set_title(f'TOA Std Dev Fit Minima Qinj = {q}') +ax.set_xlabel('Col') +ax.set_ylabel('Row') +im = ax.imshow(mins, cmap = 'RdYlGn_r')#, cmap = 'Purples') +clim = .6*(np.max(mins) - np.min(mins)) + np.min(mins) +for i in range(16): + for j in range(16): + if mins[j, i] > clim: + color = 'white' + else: + color = 'black' + print(mins[i, j], clim, color) + ax.text(i, j, minstxt[i][j], fontsize = 8, ha = 'center', va = 'center')#, color = color) +cbar = ax.figure.colorbar(im, ax = ax) +plt.savefig(path + f'DAC_v_TOASD_Fit_Minima_{q}.pdf') +plt.savefig(path + f'DAC_v_TOASD_Fit_Minima_{q}.png') + +fig, ax = plt.subplots(figsize = (9, 7)) +ax.set_title(f'TOA Std Dev Fit Widths Qinj = {q}') +ax.set_xlabel('Col') +ax.set_ylabel('Row') +clim = .6*(np.max(widths) - np.min(widths)) + np.min(widths) +im = ax.imshow(widths, cmap = 'RdYlGn')#, cmap = 'Purples') +for i in range(16): + for j in range(16): + if widths[j, i] > clim: + color = 'white' + else: + color = 'black' + ax.text(i, j, widthstxt[i][j], fontsize = 8, ha = 'center', va = 'center')#, color = color) +cbar = ax.figure.colorbar(im, ax = ax) +plt.savefig(path + f'DAC_v_TOASD_Fit_Widths_{q}.pdf') +plt.savefig(path + f'DAC_v_TOASD_Fit_Widths_{q}.png') + +fig, ax = plt.subplots(figsize = (9, 7)) +ax.set_title(f'TOA Std Dev Data Minima Qinj = {q}') +ax.set_xlabel('Col') +ax.set_ylabel('Row') +clim = .6*(np.max(widths) - np.min(widths)) + np.min(widths) +im = ax.imshow(datmin, cmap = 'RdYlGn_r')#, cmap = 'Purples') +for i in range(16): + for j in range(16): + if widths[j, i] > clim: + color = 'white' + else: + color = 'black' + ax.text(i, j, dattxt[i][j], fontsize = 8, ha = 'center', va = 'center')#, color = color) +cbar = ax.figure.colorbar(im, ax = ax) +plt.savefig(path + f'DAC_v_TOASD_Data_Minima_{q}.pdf') +plt.savefig(path + f'DAC_v_TOASD_Data_Minima_{q}.png') + +tmins = mins*0x3/calgrid*1000 +tminstxt = [['-' if minstxt[i][j] == '-' else str(np.round(tmins[j, i], 2)) for i in range(16)] for j in range(16)] +twidths = widths*0x3/calgrid*1000 +twidthstxt = [['-' if widthstxt[i][j] == '-' else str(np.round(twidths[j, i], 2)) for i in range(16)] for j in range(16)] +tdatmin = datmin*0x3/calgrid*1000 +tdattxt = [['-' if dattxt[i][j] == '-' else str(np.round(tdatmin[j, i], 2)) for i in range(16)] for j in range(16)] + +fig, ax = plt.subplots(figsize = (9, 7)) +ax.set_title(f'True TOA Std Dev Data Minima Qinj = {q}') +ax.set_xlabel('Col') +ax.set_ylabel('Row') +clim = .6*(np.max(tdatmin) - np.min(tdatmin)) + np.min(tdatmin) +im = ax.imshow(tdatmin, cmap = 'RdYlGn')#, cmap = 'Purples') +for i in range(16): + for j in range(16): + if widths[j, i] > clim: + color = 'white' + else: + color = 'black' + ax.text(i, j, tdattxt[i][j], fontsize = 8, ha = 'center', va = 'center')#, color = color) +cbar = ax.figure.colorbar(im, ax = ax) +plt.savefig(path + f'DAC_v_TTOASD_Data_Minima_{q}.pdf') +plt.savefig(path + f'DAC_v_TTOASD_Data_Minima_{q}.png') + + +fig, ax = plt.subplots(figsize = (9, 7)) +ax.set_title(f'True TOA Std Dev Minima Qinj = {q}') +ax.set_xlabel('Col') +ax.set_ylabel('Row') +clim = .6*(np.max(tmins) - np.min(tmins)) + np.min(tmins) +im = ax.imshow(twidths, cmap = 'RdYlGn')#, cmap = 'Purples') +for i in range(16): + for j in range(16): + if widths[j, i] > clim: + color = 'white' + else: + color = 'black' + ax.text(i, j, tminstxt[i][j], fontsize = 8, ha = 'center', va = 'center')#, color = color) +cbar = ax.figure.colorbar(im, ax = ax) +plt.savefig(path + f'DAC_v_TTOASD_Fit_Minima_{q}.pdf') +plt.savefig(path + f'DAC_v_TTOASD_Fit_Minima_{q}.png') + + +fig, ax = plt.subplots(figsize = (9, 7)) +ax.set_title(f'True TOA Std Dev Fit Widths Qinj = {q}') +ax.set_xlabel('Col') +ax.set_ylabel('Row') +clim = .6*(np.max(twidths) - np.min(twidths)) + np.min(twidths) +im = ax.imshow(twidths, cmap = 'RdYlGn')#, cmap = 'Purples') +for i in range(16): + for j in range(16): + if widths[j, i] > clim: + color = 'white' + else: + color = 'black' + ax.text(i, j, twidthstxt[i][j], fontsize = 8, ha = 'center', va = 'center')#, color = color) +cbar = ax.figure.colorbar(im, ax = ax) +plt.savefig(path + f'DAC_v_TTOASD_Fit_Widths_{q}.pdf') +plt.savefig(path + f'DAC_v_TTOASD_Fit_Widths_{q}.png') + + + +for i in range(16): + for j in range(16): + tmins = 17 + +plt.figure(figsize = (9,7)) +plt.title(f'Hits v. Threshold DAC\nQinj = {q}') +plt.xlabel('Threshold DAC') +plt.ylabel('Hits') + + +for pix in fitdata: + i = int(pix.split('c')[0][1:]) + j = int(pix.split('c')[1]) + + data = df[(df.row == i)&(df.col==j)] + x = data.vth + y = data.hits + plt.plot(x, y, 'o', label = f'Row {i} Col {j}') +plt.ylim([0, 2]) +plt.legend() +plt.savefig(path + f'DAC_v_Hits_multi_{q}.png') + + diff --git a/single_pixel_plots.py b/single_pixel_plots.py new file mode 100644 index 0000000000000000000000000000000000000000..7bffd6c77c35d92962d3fb9df9e20a5043aa76ce --- /dev/null +++ b/single_pixel_plots.py @@ -0,0 +1,474 @@ +import numpy as np +import matplotlib.pyplot as plt +import pandas as pd +import awkward as ak + +from sklearn.preprocessing import PolynomialFeatures +from sklearn.linear_model import LinearRegression + +import pickle +import json +import os +from tqdm import tqdm +import argparse +from scipy import stats + +parser = argparse.ArgumentParser() +parser.add_argument('--loc', action = 'store', default = '4,3') +args = parser.parse_args() + +if args.loc == 'broadcast': + loc_title = 'Broadcasted' + pix_path = 'broadcast/' +else: + i = int(args.loc.split(',')[0]) + j = int(args.loc.split(',')[1]) + loc_title = f'Row {i} Col {j}' + pixpath = f'r{i}c{j}/' + +#loc_title = '' +delay = 504 +nl1a = 3200 +nbins = 10 +fileform = f'Qinj_scan_L1A_{delay}' +low_cut = 0 +high_cut = 500 +labfontsize = 20 +titfontsize = 25 +#path = 'results/08072023_roberto/' +path = 'results/multipix_compiled/' + pixpath +store = path + 'full_qinj_plots/' +files = [f for f in os.listdir(path) if fileform in f] +if not os.path.isdir(store): + os.mkdir(store) +interest = []#[250, 255, 260, 292, 299, 340] +#interest = [815, 820, 825, 873] +figsize = (9, 7) +errorbarsize = (12, 7) + +#Loading In Data +if len(files) == 0: + print('No files here.') + stupid.phrase.to.stop.code + +print(f'Loading the following files from {path}') + +df = pd.DataFrame() +for f in files: + print(f) + charge = int(f.split('_')[4].split('.')[0]) + sub = pickle.load(open(path+f, 'rb')) + sub['charge'] = [charge]*len(sub.vth) + if not len(np.unique(sub.hits)) == 1: + df = pd.concat([df, sub]) + +print() +print(df.head()) +if df.empty: + stupid.phrase.to.stop.code + +#Slide 3 S Curve Plots +#Hits v. Threshold DAC Values, All QSel + +print('Working on Hits v. Threshold DAC') +fig = plt.figure(figsize = figsize) +plt.title(f'Hits v. Threshold DAC {loc_title}\nDelay = {delay}, # of L1A triggers: {nl1a}', fontsize = titfontsize) +plt.xlabel('Threshold DAC Values', fontsize = labfontsize) +plt.ylabel('Hits', fontsize = labfontsize) + +for q in tqdm(np.unique(df.charge)): + idx = df.charge == q + x = df.vth[idx] + y = df.hits[idx] + plt.plot(x, y, 'o-', label = f'Qinj = {q}') +#plt.ylim([3100, 3300]) +plt.legend() +plt.savefig(f'{store}/DAC_v_Hits.pdf') +plt.savefig(f'{store}/DAC_v_Hits.png') +plt.close() + +#Slide 4 S Curve Plots +#Hits v. Threshold DAC, Individual QSel + +for q in tqdm(np.unique(df.charge)): + fig = plt.figure(figsize = figsize) + plt.title(f'Hits v. Threshold DAC for Qinj = {q} {loc_title}\nDelay = {delay}, # of L1A triggers: {nl1a}') + plt.xlabel('Threshold DAC Values') + plt.ylabel('Hits') + + idx = df.charge == q + x = df.vth[idx] + y = df.hits[idx] + plt.plot(x, y, 'o-') + plt.savefig(f'{store}/DAC_v_Hits_q{q}.pdf') + plt.savefig(f'{store}/DAC_v_Hits_q{q}.png') + plt.close() + + +temp = pd.DataFrame() +for q in np.unique(df.charge): + sub = df[df.charge == q] + lim = np.max(sub.hits)*.9 + idx = [df.charge.iloc[i] == q and df.vth.iloc[i] < high_cut and df.vth.iloc[i] > low_cut for i in range(len(df.hits))] + #idx = [df.charge.iloc[i] == q and df.hits.iloc[i] > lim and df.vth.iloc[i] < high_cut and df.vth.iloc[i] > low_cut for i in range(len(df.hits))] + sub = df[idx] + temp = pd.concat([temp, sub]) + +del df +df = temp + +print('Working on Hits v. Threshold DAC Redux') +fig = plt.figure(figsize = figsize) +plt.title(f'Hits v. Threshold DAC {loc_title}\nDelay = {delay}, # of L1A triggers: {nl1a}') +plt.xlabel('Threshold DAC Values') +plt.ylabel('Hits') + +for q in tqdm(np.unique(df.charge)): + idx = df.charge == q + x = df.vth[idx] + y = df.hits[idx] + plt.plot(x, y, 'o-', label = f'Qinj = {q}') + +plt.legend() +plt.savefig(f'{store}/DAC_v_Hits_redux.pdf') +plt.savefig(f'{store}/DAC_v_Hits_redux.png') +plt.close() + +for q in tqdm(np.unique(df.charge)): + fig = plt.figure(figsize = figsize) + plt.title(f'Hits v. Threshold DAC for Qinj = {q} {loc_title}\nDelay = {delay}, # of L1A triggers: {nl1a}') + plt.xlabel('Threshold DAC Values') + plt.ylabel('Hits') + + idx = df.charge == q + x = df.vth[idx] + y = df.hits[idx] + plt.plot(x, y, 'o-') + plt.savefig(f'{store}/DAC_v_Hits_q{q}_redux.pdf') + plt.savefig(f'{store}/DAC_v_Hits_q{q}_redux.png') + plt.close() + + +fig, ax = plt.subplots(figsize = figsize) +charges = [] +firstbest = [] +for q in np.unique(df.charge): + data = df[df.charge == q] + best = np.max(data.hits) + if best == nl1a: + charges.append(q) + firstbest.append(data.vth[data.hits == np.max(data.hits)].iloc[-1]) + +ax.scatter(charges, firstbest, label = 'Data', color = 'r') +model = stats.linregress(charges, firstbest) +x = np.linspace(np.min(charges), np.max(charges), 1000) +y = x*model.slope + model.intercept +ax.plot(x, y, label = 'Fit', color = 'b') + +''' +p = 0.05 +ts = np.abs(stats.t.ppf(p/2, len(charges) - 2)) +slopeerr = ts*model.slope +interr = ts*model.intercept +print(model.slope, model.intercept, ts) +high = x*(model.slope + slopeerr) + (model.intercept+interr) +low = x*(model.slope - slopeerr) + (model.intercept-interr) +ax.fill_between(x, low, high, label = '95% Conf. Int.', alpha = 0.4, color = 'c') +''' + +ax.set_title('S-Curve End Points') +ax.set_xlabel('Qinj') +ax.set_ylabel('Threshold DAC') +plt.savefig(f'{store}/Threshold_DAC_Limit.png') +plt.savefig(f'{store}/Threshold_DAC_Limit.png') + + +#Slide 5 TOT, etc. Distributions at indivudial QSel, 5 different Threshold DACs + +print('Working on TOT, TOA, and Cal hists for individual settings') +for q in tqdm(np.unique(df.charge)): + idx = [df.charge.iloc[i] == q and df.hits.iloc[i] > 0 for i in range(len(df.hits))] + for d in tqdm(interest, leave = False, desc = f'Working on QSel = {q}'): + #if not d in df.vth: d = np.random.choice(df.vth, 1) + idx = [df.charge.iloc[i] == q and df.vth.iloc[i] == d for i in range(len(df.vth))] + + fig = plt.figure(figsize = figsize) + tot = df.tot[idx].iloc[0] + plt.hist(tot, bins = nbins, density = True)#bins = range(np.min(tot), np.max(tot) + 1), density = True) + plt.title(f'TOT Values for Threshold DAC = {d} {loc_title}\nEntries: {nl1a}, Delay = {delay}, Qinj = {q}') + plt.xlabel('TOT Values') + plt.ylabel('Frequency') + plt.yscale('log') + plt.savefig(f'{store}/TOT_vth_{d}_q{q}.pdf') + plt.savefig(f'{store}/TOT_vth_{d}_q{q}.png') + plt.close() + + fig = plt.figure(figsize = figsize) + toa = df.toa[idx].iloc[0] + plt.hist(toa, bins = nbins, density = True)#bins = range(np.min(toa), np.max(toa) + 1), density = True) + plt.title(f'TOA Values for Threshold DAC = {d} {loc_title}\nEntries: {nl1a}, Delay = {delay}, Qinj = {q}') + plt.xlabel('TOA Values') + plt.ylabel('Frequency') + plt.yscale('log') + plt.savefig(f'{store}/TOA_vth_{d}_q{q}.pdf') + plt.savefig(f'{store}/TOA_vth_{d}_q{q}.png') + plt.close() + + fig = plt.figure(figsize = figsize) + cal = df.cal[idx].iloc[0] + plt.hist(cal, bins = nbins, density = True)#bins = range(np.min(cal), np.max(cal) + 1), density = True) + plt.title(f'CaL Values for Threshold DAC = {d} {loc_title}\nEntries: {nl1a}, Delay = {delay}, Qinj = {q}') + plt.xlabel('CAL Values') + plt.ylabel('Frequency') + plt.savefig(f'{store}/CAL_vth_{d}_q{q}.pdf') + plt.savefig(f'{store}/CAL_vth_{d}_q{q}.png') + plt.close() + +#Slide ^ + +print('Working on TOA v. DAC') + +for q in tqdm(np.unique(df.charge)): + chargeidx = df.charge == q + hitsidx = df.hits[chargeidx] > 100 + + vth = df.vth[chargeidx][hitsidx] + + toa = df.toa[chargeidx][hitsidx] + toaavg = [np.mean(dat) for dat in toa] + toastd = [np.std(dat) for dat in toa] + + tot = df.tot[chargeidx][hitsidx] + totavg = [np.mean(dat) for dat in tot] + totstd = [np.std(dat) for dat in tot] + + cal = df.cal[chargeidx][hitsidx] + calavg = [np.mean(dat) for dat in cal] + calstd = [np.std(dat) for dat in cal] + + fig = plt.figure(figsize = errorbarsize) + plt.errorbar(vth, toaavg, toastd, fmt = 'o-', capsize = 3) + plt.xlabel('Threshold DAC', fontsize = labfontsize) + plt.ylabel('TOA Mean', fontsize = labfontsize) + plt.title(f'Mean TOA vs. Theshold DAC for Delay = {delay} {loc_title}, Qinj = {q}', fontsize = titfontsize) + plt.savefig(f'{store}/DAC_v_TOA_q{q}.pdf') + plt.savefig(f'{store}/DAC_v_TOA_q{q}.png') + plt.close() + + fig = plt.figure(figsize = errorbarsize) + plt.errorbar(vth, totavg, totstd, fmt = 'o-', capsize = 3) + plt.xlabel('Threshold DAC', fontsize = labfontsize) + plt.ylabel('TOA Mean', fontsize = labfontsize) + plt.title(f'Mean TOA vs. Theshold DAC for Delay = {delay} {loc_title}, Qinj = {q}', fontsize = titfontsize) + plt.savefig(f'{store}/DAC_v_TOT_q{q}.pdf') + plt.savefig(f'{store}/DAC_v_TOT_q{q}.png') + plt.close() + + fig = plt.figure(figsize = errorbarsize) + plt.errorbar(vth, calavg, calstd, fmt = 'o-', capsize = 3) + plt.xlabel('Threshold DAC', fontsize = labfontsize) + plt.ylabel('CAL Mean', fontsize = labfontsize) + plt.title(f'Mean CAL vs. Theshold DAC for Delay = {delay} {loc_title}, Qinj = {q}', fontsize = labfontsize) + plt.savefig(f'{store}/DAC_v_CAL_q{q}.pdf') + plt.savefig(f'{store}/DAC_v_CAL_q{q}.png') + plt.close() + + +fig = plt.figure(figsize = errorbarsize) +for q in tqdm(np.unique(df.charge)): + chargeidx = df.charge == q + hitsidx = df.hits[chargeidx] > 10#== nl1a + vth = df.vth[chargeidx][hitsidx] + toa = df.toa[chargeidx][hitsidx] + toaavg = [np.mean(dat) for dat in toa] + toastd = [np.std(dat) for dat in toa] + if len(toa) > 2: + plt.errorbar(vth, toaavg, toastd, alpha = 0.7, capsize = 5, label = f'Qinj = {q}') +plt.xlabel('Threshold DAC', fontsize = labfontsize) +plt.ylabel('TOA Mean', fontsize = labfontsize) +plt.legend() +plt.title(f'Mean TOA vs. Theshold DAC for Delay = {delay} {loc_title}', fontsize = titfontsize) +plt.savefig(f'{store}/DAC_v_TOA.pdf') +plt.savefig(f'{store}/DAC_v_TOA.png') +plt.close() + +fig = plt.figure(figsize = errorbarsize) +for q in tqdm(np.unique(df.charge)): + chargeidx = df.charge == q + hitsidx = df.hits[chargeidx] > 10#== nl1a + vth = df.vth[chargeidx][hitsidx] + tot = df.tot[chargeidx][hitsidx] + totavg = [np.mean(dat) for dat in tot] + totstd = [np.std(dat) for dat in tot] + if len(tot) > 2: + plt.errorbar(vth, totavg, totstd, alpha = 0.7, capsize = 5, label = f'Qinj = {q}') +plt.xlabel('Threshold DAC', fontsize = labfontsize) +plt.ylabel('TOT Mean', fontsize = labfontsize) +plt.legend() +plt.title(f'Mean TOT vs. Theshold DAC for Delay = {delay} {loc_title}', fontsize = titfontsize) +plt.savefig(f'{store}/DAC_v_TOT.pdf') +plt.savefig(f'{store}/DAC_v_TOT.png') +plt.close() + +fig = plt.figure(figsize = errorbarsize) +for q in tqdm(np.unique(df.charge)): + chargeidx = df.charge == q + hitsidx = df.hits[chargeidx] > 100 + vth = df.vth[chargeidx][hitsidx] + cal = df.cal[chargeidx][hitsidx] + calavg = [np.mean(dat) for dat in cal] + calstd = [np.std(dat) for dat in cal] + if len(cal) > 2: + plt.errorbar(vth, calavg, calstd, alpha = 0.7, capsize = 5, label = f'Qinj = {q}') +plt.xlabel('Threshold DAC', fontsize = labfontsize) +plt.ylabel('CAL Mean', fontsize = labfontsize) +plt.title(f'Mean CAL vs. Theshold DAC for Delay = {delay} {loc_title}', fontsize = titfontsize) +plt.savefig(f'{store}/DAC_v_CAL.pdf') +plt.savefig(f'{store}/DAC_v_CAL.png') +plt.close() + + +# Polynomial Regression of TOA SD data + +for q in np.unique(df.charge): + idx = df.charge == q + vth = df.vth[idx] + toa = df.toa[idx] + x = [] + y = [] + + for i in range(len(vth)): + print(df.hits[idx].iloc[i], len(toa.iloc[i])) + if len(toa.iloc[i]) > .98*nl1a:# and np.std(toa.iloc[i]) < 2:#> .98*nl1a : + x.append(vth.iloc[i]) + y.append(np.std(toa.iloc[i])) + + if len(x) > 10: + fig = plt.figure(figsize = figsize) + + transformer = PolynomialFeatures(degree = 4) + model = LinearRegression(fit_intercept = True) + + plt.plot(x, y, 'o-', alpha = 0.8, label = 'Data') + + X = transformer.fit_transform(np.array(x).reshape(-1, 1)) + model.fit(X, y) + + x = np.array(vth).reshape(-1, 1) + X = transformer.fit_transform(x) + y = model.predict(X) + + plt.plot(x, y, label = 'Fit') + + ydiff = [y[i] - y[i-1] for i in range(1, len(y))] + lim = 0.05 + starter = [diff > -lim for diff in ydiff] + ender = [diff < lim for diff in ydiff] + startidx = starter.index(True) + ender.reverse() + endidx = len(ender) - ender.index(True) + x = [vth[startidx]]*10 + [np.nan] + [vth[endidx]]*10 + y = np.linspace(0, 2, 10).tolist() + [0] + np.linspace(0, 2, 10).tolist() + plt.plot(x, y, label = 'Boundaries') + + plt.plot([x[0], x[len(x) - 1]], [0.5, 0.5], label = 'TOA SD = 0.5') + plt.plot([x[0], x[len(x) - 1]], [1, 1], label = 'TOA SD = 1') + + plt.ylim([0, 2]) + plt.xlabel('Threshold DAC') + plt.ylabel('CAL Std Dev.') + plt.legend() + plt.title(f'Standard Deviation of TOA vs. Theshold DAC for Delay = {delay}\n {loc_title}, Qinj = {q}') + plt.savefig(f'{store}/DAC_v_TOASD_q{q}.pdf') + plt.savefig(f'{store}/DAC_v_TOASD_q{q}.png') + plt.close() + + + +fig = plt.figure(figsize = figsize) +for q in np.unique(df.charge): + idx = df.charge == q + vth = df.vth[idx] + toa = df.toa[idx] + x = [] + y = [] + + for i in range(len(vth)): + print(df.hits[idx].iloc[i], len(toa.iloc[i])) + if len(toa.iloc[i]) > .98*nl1a:# and np.std(toa.iloc[i]) < 2:#> .98*nl1a : + x.append(vth.iloc[i]) + y.append(np.std(toa.iloc[i])) + + if len(x) > 10: + plt.plot(x, y, 'o-', alpha = 0.8, label = f'Qinj = {q}') + +plt.plot([x[0], x[len(x) - 1]], [0.5, 0.5], label = 'TOA SD = 0.5') +plt.plot([x[0], x[len(x) - 1]], [1, 1], label = 'TOA SD = 1') + +plt.ylim([0, 2]) +plt.xlabel('Threshold DAC') +plt.ylabel('CAL Std Dev.') +plt.legend() +plt.title(f'Standard Deviation of TOA vs. Theshold DAC for Delay = {delay}\n {loc_title}') +plt.savefig(f'{store}/DAC_v_TOASD.pdf') +plt.savefig(f'{store}/DAC_v_TOASD.png') +plt.close() + + +''' +# TOA v. TOT + +for q in np.unique(df.charge): + x = [] + y = [] + a = [] + idx = [df.hits.iloc[i] > 0 and df.charge.iloc[i] == q for i in range(len(df.hits))] + keptvths = [] + for d in np.unique(df.vth[idx]): + idx = [df.charge.iloc[i] == q and df.vth.iloc[i] == d for i in range(len(df.charge))] + lim = np.max(df.hits[idx]) + idx = [df.charge.iloc[i] == q and df.vth.iloc[i] == d and df.hits.iloc[i] == lim for i in range(len(df.charge))] + if any(idx): + x.append([np.mean(df.tot[idx].iloc[0])]) + y.append([np.mean(df.toa[idx].iloc[0])]) + a.append(len(df.toa[idx].iloc[0])) + keptvths.append(d) + a = np.array(a) + a = a - np.min(a) + a = a/np.max(a) + fig, ax = plt.subplots(figsize = figsize) + color = plt.cm.get_cmap('winter') + c = [i/len(x) for i in range(len(x))] + pos = ax.scatter(x, y, c = c, vmin = 0, vmax = 1, alpha = a, cmap = color) + cbar = plt.colorbar(pos, ax = ax, label = 'Threshold DAC') + cbar.set_ticks([0, 1]) + cbar.set_ticklabels([np.min(keptvths), np.max(keptvths)]) + ax.set_title(f'Mean TOT values v. Mean TOA Values {loc_title}\nDelay = {delay}, Qinj = {q}') + ax.set_xlabel('Mean TOT') + ax.set_ylabel('Mean TOA') + plt.savefig(f'{store}/TOT_v_TOA_{q}.png') + plt.savefig(f'{store}/TOT_v_TOA_q{q}.pdf') + plt.close() + +fig, ax = plt.subplots(figsize = figsize) +for q in np.unique(df.charge): + x = [] + y = [] + idx = [df.charge.iloc[i] == q for i in range(len(df.hits))] + lim = np.max(df.hits[idx]) + idx = [df.charge.iloc[i] == q and df.hits.iloc[i] == lim for i in range(len(df.hits))] + sub = df[idx] + for i in range(len(sub.vth)): + if np.abs(sub.hits.iloc[i] - np.max(sub.hits))/np.max(sub.hits) < 0.05: + x += sub.tot.iloc[i] + y += sub.toa.iloc[i] + plt.plot(x, y, 'o-', label = f'Qinj = {q}') + +ax.set_title(f'TOT values v. TOA Values {loc_title}\nDelay = {delay}') +ax.set_xlabel('Time over Threshold') +ax.set_ylabel('Time of Arrival') +plt.legend() +plt.savefig(f'{store}/TOT_v_TOA_full.png') +plt.savefig(f'{store}/TOT_v_TOA_full.pdf') +plt.close() +''' diff --git a/test_ETROC.py b/test_ETROC.py index eb0494e50a18594ef6a604580404f045ba1c44e2..ce1b75b08c934d35047db9075754f68b5573b1ea 100644 --- a/test_ETROC.py +++ b/test_ETROC.py @@ -147,6 +147,8 @@ if __name__ == '__main__': argParser.add_argument('--scan', action='store', default=['full'], choices=['none', 'full', 'simple', 'internal'], help="Which threshold scan to run with ETROC2") argParser.add_argument('--mode', action='store', default=['dual'], choices=['dual', 'single'], help="Port mode for ETROC2") argParser.add_argument('--internal_data', action='store_true', help="Set up internal data generation") + argParser.add_argument('--loc', action = 'store', default = '') + argParser.add_argument('--temp', action = 'store_true') args = argParser.parse_args() @@ -372,6 +374,19 @@ if __name__ == '__main__': print('Link not good, but continuing') rb_0.get_link_status(link, slave=slave) break + if args.temp: + N_temps = 10000 + temps = {'time':[], 'temp':[]} + starttime = time.time() + for t in tqdm(range(N_temps)): + try: + temps['temp'].append(etroc.rb.SCA.read_adc(0x02)) + temps['time'].append(time.time() - starttime) + except: + print('Failed at {time.time() - starttime}s') + with open('results/temp_data.json', 'w') as outfile: + json.dump(temps, outfile) + ## Bloc below to be retired ## Keeping it for one more iteration @@ -807,14 +822,20 @@ if __name__ == '__main__': if args.qinj_vth_scan: fifo.reset() delay = 10 - i = 4 - j = 3 + if len(args.loc) == 0: + i = 4 + j = 3 + else: + i = int(args.loc.split('c')[0][1:]) + j = int(args.loc.split('c')[1]) + L1Adelay = 501 - vth_axis = np.linspace(415, 820, 406) + vth_axis = range(220, 500)#np.linspace(415, 820, 406) #charges = [1,5,10,15,20,25,30,32] #charges = [5,10,15,20,25,30,32] - charges = [4,6,8,12] + #charges = [4, 6, 8, 12, 15, 20, 25, 30]#[4,6,8,12] + charges = [8] results =[[] for i in range(0,len(charges))] TOA = [[] for i in range(0,len(charges))] TOT = [[] for i in range(0,len(charges))] @@ -855,7 +876,8 @@ if __name__ == '__main__': 'tot' : TOT[k-1], 'cal' : CAL[k-1]}) #print(scan_df.info()) - scan_df.to_pickle(f"results/Qinj_scan_L1A_504_{q}.pkl") + store = f'results/09052023_multipix_uptd/r{i}c{j}/' + scan_df.to_pickle(f"{store}Qinj_scan_L1A_504_{q}.pkl") fig, ax = plt.subplots()