#!/usr/bin/env python3
from __future__ import annotations
import json, glob, os
from pathlib import Path
import matplotlib.pyplot as plt

root = Path(__file__).resolve().parents[1]
results = root/'results'
figs = root/'figures'
figs.mkdir(exist_ok=True)
rows=[]
for f in sorted(results.glob('*summary.json')):
    j=json.load(open(f))
    rows.append({
        'beta': j['beta'],
        'plaquette': j['means']['plaquette'],
        'plaquette_err': j['means']['plaquette_stderr'],
        'sigma_lat': j['sigma_lattice_units_crude'],
        'V1': j['effective_potential'].get('1'),
        'V2': j['effective_potential'].get('2'),
    })
rows=sorted(rows,key=lambda r:r['beta'])

betas=[r['beta'] for r in rows]
plaq=[r['plaquette'] for r in rows]
plaq_err=[r['plaquette_err'] for r in rows]
sigma=[r['sigma_lat'] for r in rows]

plt.figure(figsize=(6,4))
plt.errorbar(betas,plaq,yerr=plaq_err,marker='o',capsize=3)
plt.xlabel(r'$\beta$')
plt.ylabel('mean plaquette')
plt.title('SU(3) pure-gauge smoke test')
plt.tight_layout()
plt.savefig(figs/'plaquette_vs_beta.png',dpi=200)
plt.close()

plt.figure(figsize=(6,4))
plt.plot(betas,sigma,marker='o')
plt.xlabel(r'$\beta$')
plt.ylabel(r'crude $\sigma a^2$ from $V(2)-V(1)$')
plt.title('Crude string-slope proxy, tiny lattice')
plt.tight_layout()
plt.savefig(figs/'sigma_proxy_vs_beta.png',dpi=200)
plt.close()

print('wrote plots to', figs)
