"""One-off export of Tier 4 event-level/pdg-share plots for
checkpoints/scan/h1024_n8_d0.1_lr0.0003/best.pt into the knowledge-base
attachments folder. Not part of the package; run manually."""

from pathlib import Path

import giant.analysis as a

FILE = "/home/lars/Programming/giant/pbwo4_10k_9_predicted_local.parquet"
OUT = Path("/home/lars/knowledge-base/meta/attachments")
PREFIX = "giant-h1024n8d0.1lr3e-4"

print("=== computing event observables ===")
obs = a.compute_event_observables_pl(FILE)
table = obs.event_table
print("n events:", table.height)

print("=== total energy / total length ===")
fig = a.plot_total_energy(obs)
fig.savefig(OUT / f"{PREFIX}-event-total-energy.png", dpi=150, bbox_inches="tight")
fig = a.plot_total_length(obs)
fig.savefig(OUT / f"{PREFIX}-event-total-length.png", dpi=150, bbox_inches="tight")

print("=== mean/median energy & length per step ===")
fig = a.plot_mean_energy_per_step(obs)
fig.savefig(OUT / f"{PREFIX}-event-mean-energy-per-step.png", dpi=150, bbox_inches="tight")
fig = a.plot_mean_length_per_step(obs)
fig.savefig(OUT / f"{PREFIX}-event-mean-length-per-step.png", dpi=150, bbox_inches="tight")

print("=== longitudinal / transverse profiles ===")
fig = a.plot_longitudinal_profile(obs)
fig.savefig(OUT / f"{PREFIX}-event-longitudinal-profile.png", dpi=150, bbox_inches="tight")
fig = a.plot_transverse_profile(obs)
fig.savefig(OUT / f"{PREFIX}-event-transverse-profile.png", dpi=150, bbox_inches="tight")

print("=== shower-max depth ===")
fig = a.plot_shower_max_depth(obs)
fig.savefig(OUT / f"{PREFIX}-event-shower-max-depth.png", dpi=150, bbox_inches="tight")

print("=== pdg contribution shares ===")
pdg_table = a.pdg_contribution_table_pl(FILE)
fig = a.plot_pdg_energy_share(pdg_table)
fig.savefig(OUT / f"{PREFIX}-pdg-energy-share.png", dpi=150, bbox_inches="tight")
fig = a.plot_pdg_length_share(pdg_table)
fig.savefig(OUT / f"{PREFIX}-pdg-length-share.png", dpi=150, bbox_inches="tight")

print("=== summary stats ===")
import numpy as np  # noqa: E402

for label, real_col, gen_col in [
    ("total_edep", "real_total_edep", "gen_total_edep"),
    ("total_length", "real_total_length", "gen_total_length"),
    ("mean_edep", "real_mean_edep", "gen_mean_edep"),
    ("mean_length", "real_mean_length", "gen_mean_length"),
    ("median_edep", "real_median_edep", "gen_median_edep"),
    ("median_length", "real_median_length", "gen_median_length"),
    ("centroid_depth", "real_centroid_depth", "gen_centroid_depth"),
    ("transverse_rms", "real_transverse_rms", "gen_transverse_rms"),
    ("max_depth", "real_max_depth", "gen_max_depth"),
]:
    real = table[real_col].to_numpy()
    gen = table[gen_col].to_numpy()
    print(
        f"{label}: real mean={real.mean():.4g} std={real.std():.4g} sigma/mu={real.std() / real.mean():.4f} | "
        f"gen mean={gen.mean():.4g} std={gen.std():.4g} sigma/mu={gen.std() / gen.mean():.4f} | "
        f"mean_diff%={100 * (gen.mean() - real.mean()) / real.mean():.2f}"
    )

print("n_steps per event: mean", table["n_steps"].to_numpy().mean())

print("=== pdg shares table ===")
print(pdg_table.to_pandas().to_string())

print("DONE")
