Q3D-Calibration/process.py

70 lines
1.9 KiB
Python

import csv
import numpy as np
from qdx import Bind
from qdx.utils import readFileData, get_hist
from tqdm import tqdm
from matplotlib import pyplot as plt
# Initialization
n, m = 5, 8
binds = [[Bind(i, j) for j in range(m)] for i in range(n)]
# Read Calibration Data
pbar = tqdm(desc="Bind Initialization", total=n * m)
data = list(csv.reader(open("coef1.csv", "r")))
data = np.array(data, dtype=np.float64)
for i in range(n):
for j in range(m):
bind = binds[i][j]
bind(data[j + i * m][2:])
pbar.update(1)
pbar.close()
# Read Data
total = len(open("task3.csv", "r").readlines()) * n * m
file_list = csv.reader(open("task3.csv", "r"))
pX = np.array([])
eng = np.array([])
pX_full = np.array([])
eng_full = np.array([])
pbar = tqdm(desc="Task - Mg25(d,p)Mg26*", total=total)
for row in file_list:
ldata, rdata = readFileData(row[0], n, m)
for i in range(n):
for j in range(m):
bind = binds[i][j]
x = bind.predict_px(ldata[j + i * m], rdata[j + i * m]) + float(row[1])
e = bind.predict_energy(ldata[j + i * m], rdata[j + i * m])
edge_l = 5 + 130 * i + float(row[1]) - 35
edge_r = edge_l + 65
idx = np.where((x >= edge_l) & (x <= edge_r))[0]
pX = np.hstack((pX, x[idx]))
eng = np.hstack((eng, e[idx]))
pX_full = np.hstack((pX_full, x))
eng_full = np.hstack((eng_full, e))
pbar.update(1)
pbar.close()
# Draw
fig = plt.figure(figsize=(20, 8), dpi=200)
ax1 = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 2)
count, center = get_hist(pX, delta=0.1)
ax1.scatter(pX, eng, s=0.05, color="k")
py, = ax2.step(center, count, where="post", color="k")
ax1.set_xticks(np.arange((np.min(pX) // 50) * 50, (np.max(pX) // 50 + 1) * 50, 50))
ax2.set_xticks(np.arange((np.min(pX) // 50) * 50, (np.max(pX) // 50 + 1) * 50, 50))
fig.savefig("Task3.png")
plt.close()