| """Load hits_xy.npy + dataset.npz from this Hugging Face dataset repo and |
| save a PNG of the rasterized hit image for the closest real event to a |
| requested (PID, momentum, eta, phi). |
| |
| The data sits on a fixed grid (momentum in 0.1 GeV/c steps, eta/phi in |
| 0.1 steps) and each particle species was independently randomly sampled |
| from it, so an exact match to your input essentially never exists -- |
| this finds the closest real event instead and prints its true kinematics. |
| |
| Usage: |
| python view_hits.py --pid 2212 --mom 30 --eta 2.0 --phi 3.14 --out proton.png |
| """ |
| import argparse |
|
|
| import matplotlib |
| matplotlib.use("Agg") |
| import matplotlib.pyplot as plt |
| import numpy as np |
| from huggingface_hub import hf_hub_download |
|
|
| REPO = "deepaksamuel-cuk/simhits" |
| PID2LABEL = {11: 0, 211: 1, 321: 2, 2212: 3} |
| NAMES = ["electron", "pion", "kaon", "proton"] |
| FULL_IMG = 384 |
| FULL_WINDOW = 3540.0 |
|
|
|
|
| def rasterize(pts, img_size=FULL_IMG, window=FULL_WINDOW): |
| """pixel value = log1p(hit count) -- multiple hits in the same cell |
| are summed (np.add.at), never overwritten or capped.""" |
| img = np.zeros((img_size, img_size), dtype=np.float32) |
| if len(pts) == 0: |
| return img |
| rel = (pts - (-window / 2)) * (img_size / window) |
| ij = np.floor(rel).astype(np.int64) |
| ok = (ij[:, 0] >= 0) & (ij[:, 0] < img_size) & (ij[:, 1] >= 0) & (ij[:, 1] < img_size) |
| ij = ij[ok] |
| np.add.at(img, (ij[:, 1], ij[:, 0]), 1.0) |
| return np.log1p(img) |
|
|
|
|
| def find_nearest_event(label, mom, eta, phi, pid, p, eta_q, phi_q): |
| """Nearest event of the requested species in normalized (p, eta, phi) |
| space -- phi handled circularly so 0 and 2*pi are adjacent.""" |
| c = PID2LABEL[abs(pid)] |
| cand = np.where(label == c)[0] |
| dphi = np.angle(np.exp(1j * (phi[cand] - phi_q))) |
| d2 = (((mom[cand] - p) / 60.0) ** 2 |
| + ((eta[cand] - eta_q) / 2.0) ** 2 |
| + (dphi / np.pi) ** 2) |
| return int(cand[np.argmin(d2)]) |
|
|
|
|
| def main(): |
| ap = argparse.ArgumentParser(description=__doc__, |
| formatter_class=argparse.RawDescriptionHelpFormatter) |
| ap.add_argument("--pid", type=int, required=True, choices=list(PID2LABEL), |
| help="11=electron, 211=pion, 321=kaon, 2212=proton") |
| ap.add_argument("--mom", type=float, required=True, help="momentum [GeV/c]") |
| ap.add_argument("--eta", type=float, required=True, help="pseudorapidity") |
| ap.add_argument("--phi", type=float, required=True, help="azimuth [rad]") |
| ap.add_argument("--out", default="hit_image.png", help="output PNG path") |
| args = ap.parse_args() |
|
|
| dataset_path = hf_hub_download(REPO, "dataset.npz", repo_type="dataset") |
| hits_path = hf_hub_download(REPO, "hits_xy.npy", repo_type="dataset") |
| d = np.load(dataset_path) |
| label, mom, eta, phi, offsets = (d["label"], d["mom"], d["eta"], |
| d["phi"], d["offsets"]) |
| hits = np.load(hits_path, mmap_mode="r") |
|
|
| j = find_nearest_event(label, mom, eta, phi, args.pid, args.mom, args.eta, args.phi) |
| a, b = offsets[j], offsets[j + 1] |
| pts = np.asarray(hits[a:b], dtype=np.float32) |
| img = rasterize(pts) |
|
|
| name = NAMES[PID2LABEL[abs(args.pid)]] |
| print(f"nearest match: {name} (PID {args.pid}) -- " |
| f"p={mom[j]:.2f} GeV/c, eta={eta[j]:.2f}, phi={phi[j]:.2f} rad, " |
| f"{len(pts)} hits (event idx {j})") |
|
|
| half = FULL_WINDOW / 2 |
| fig, ax = plt.subplots(figsize=(6, 6)) |
| ax.imshow(img, origin="lower", cmap="Blues", extent=[-half, half, -half, half]) |
| ax.set_xlabel("x [mm]") |
| ax.set_ylabel("y [mm]") |
| ax.set_title(f"{name}: p={mom[j]:.2f} GeV/c, eta={eta[j]:.2f}, phi={phi[j]:.2f} rad") |
| fig.tight_layout() |
| fig.savefig(args.out, dpi=150) |
| print("wrote", args.out) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|