#!/usr/bin/env python3
"""
verify_P085.py -- Addendum 85: Higgs Mass from J3(O).

This verifier checks the numerical Higgs-quartic and Higgs-mass claims in
85_Addendum_HiggsMass.tex.

Most final NLO arithmetic is consistent: lambda_H^(0)=1/8 gives 123.11 GeV,
the orbit-normalised correction 4*lambda^2/5 gives about 125.52 GeV, and the
residual is about 3 sigma experimentally.

Flagged issues:
  * An intermediate "leading-order" theorem computes 1/256, not 1/8, before
    self-correcting to a separate assertion.
  * The Addendum 39 mass cross-check is not 125.26 GeV when evaluated with the
    paper's stated v_EW=246.22 GeV.
  * The NNLO coefficient needed to close exactly is about 2.19, not about 4.1;
    the proposed c4=21/5 undershoots the measured Higgs mass.
"""

import math
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))
class Verifier:
    """Output shim: identical tolerance semantics to verify_common.Verifier,
    modern [PASS]/[FAIL] check-line output format."""

    def __init__(self, name):
        self.PASS = 0
        self.FAIL = 0
        self.n = 0
        print(name)

    def _mark(self, ok, desc):
        self.n += 1
        if ok:
            self.PASS += 1
        else:
            self.FAIL += 1
        print(f"  [{'PASS' if ok else 'FAIL'}] {self.n:>2}. {desc}")

    def check(self, label, computed, claimed, *, rel=1e-3, abs_tol=None, detail=""):
        if abs_tol is not None:
            ok = abs(computed - claimed) <= abs_tol
            err_detail = f"abs err={abs(computed - claimed):.6g}, tol={abs_tol:.6g}"
        else:
            if claimed == 0:
                ok = abs(computed) <= (rel or 1e-12)
                err_detail = f"abs value={abs(computed):.6g}, tol={rel:.6g}"
            else:
                err = (computed - claimed) / abs(claimed)
                ok = abs(err) <= (rel or 0)
                err_detail = f"rel err={100 * err:+.6g}%, tol={100 * (rel or 0):.6g}%"
        self._mark(ok, label + (f" -- {detail}" if detail else ""))
        print(f"        computed: {computed}   claimed: {claimed}   ({err_detail})")
        return ok

    def record(self, label, ok, computed="", claimed="", detail=""):
        ok = bool(ok)
        self._mark(ok, label + (f" -- {detail}" if detail else ""))
        if computed != "" or claimed != "":
            print(f"        computed: {computed}   claimed: {claimed}")
        return ok

    def summary(self):
        print(f"\n{'='*60}\nRESULT: {self.PASS} PASS / {self.FAIL} FAIL")
        return 0 if self.FAIL == 0 else 1


v = Verifier("P085 -- Higgs Mass")

V_EW = 246.22
M_H_PDG = 125.20
SIGMA_H = 0.11
LAM = math.sin(math.pi / 14)
MU0 = 4 * math.pi**3 + math.pi**2 + math.pi


def higgs_mass(lambda_h):
    return V_EW * math.sqrt(2 * lambda_h)


lambda_sm = M_H_PDG**2 / (2 * V_EW**2)
lambda_lo = 1 / 8
m_lo = higgs_mass(lambda_lo)
lambda_single = lambda_lo * (1 + LAM**2 / 4)
m_single = higgs_mass(lambda_single)
lambda_overshoot = lambda_lo * (1 + 2 * LAM**2)
m_overshoot = higgs_mass(lambda_overshoot)
lambda_nlo = lambda_lo * (1 + 4 * LAM**2 / 5)
m_nlo = higgs_mass(lambda_nlo)
lambda_a39 = 9 * math.pi**2 / (5 * MU0)
m_a39 = higgs_mass(lambda_a39)

v.check("mu0 = 4*pi^3 + pi^2 + pi", MU0, 137.036, rel=3e-6)
v.check("lambda = sin(pi/14)", LAM, 0.22252, rel=5e-6)
v.check("lambda^2", LAM**2, 0.04952, rel=1e-4)
v.check("lambda^4", LAM**4, 0.00245, rel=2e-3)
v.check("SM lambda_H from m_H and v_EW", lambda_sm, 0.12937, rel=1e-3)

v.check("LO lambda_H = 1/8", lambda_lo, 0.12500, rel=1e-12)
v.check("LO Higgs mass v/2", m_lo, 123.11, rel=5e-5)
v.check("LO residual GeV", M_H_PDG - m_lo, 2.09, rel=1e-3)
v.check("LO residual percent", 100 * (m_lo - M_H_PDG) / M_H_PDG, -1.67, rel=2e-3)
v.check("lambda^2/3 residual scale", LAM**2 / 3, 0.0165, rel=2e-2)

intermediate_lo = 9 / 2304
v.check(
    "intermediate q4/B^2 generation route vs final LO 1/8",
    intermediate_lo,
    lambda_lo,
    rel=1e-3,
    detail="Expected fail: the text computes 9/2304 = 1/256, then abandons this route.",
)

v.check("single-loop lambda_H", lambda_single, 0.12655, rel=5e-4)
v.check("single-loop Higgs mass", m_single, 123.87, rel=5e-4)
v.check("single-loop residual GeV", M_H_PDG - m_single, 1.33, rel=1e-2)

v.check("improved 2*lambda^2 correction", 2 * LAM**2, 0.09903, rel=2e-4)
v.check("improved 2*lambda^2 lambda_H", lambda_overshoot, 0.13738, rel=5e-4)
v.check("improved 2*lambda^2 Higgs mass", m_overshoot, 129.06, rel=5e-4)

v.check("orbit-normalised correction 4*lambda^2/5", 4 * LAM**2 / 5, 0.039613, rel=2e-4)
v.check("NLO lambda_H = 1/8*(1+4lambda^2/5)", lambda_nlo, 0.12995, rel=5e-4)
v.check("NLO Higgs mass", m_nlo, 125.53, rel=5e-4)
v.check("NLO residual GeV", m_nlo - M_H_PDG, 0.33, rel=2e-2)
v.check("NLO residual percent", 100 * (m_nlo - M_H_PDG) / M_H_PDG, 0.26, rel=2e-2)
v.check("NLO residual sigma", (m_nlo - M_H_PDG) / SIGMA_H, 3.0, rel=2e-2)

v.check("Addendum 39 lambda_H = 9*pi^2/(5*mu0)", lambda_a39, 0.12964, rel=5e-4)
v.check(
    "Addendum 39 mass from stated v_EW",
    m_a39,
    125.26,
    abs_tol=0.05,
    detail="Expected fail: with v_EW=246.22 this formula gives about 125.37 GeV.",
)
v.check("A39-vs-P85 coupling agreement percent", 100 * (lambda_nlo - lambda_a39) / lambda_a39, 0.24, rel=3e-2)

target_factor = (M_H_PDG / (V_EW / 2)) ** 2
c4_required = (1 + 4 * LAM**2 / 5 - target_factor) / (LAM**4)
v.check(
    "NNLO c4 required for exact 125.20 GeV closure",
    c4_required,
    4.1,
    rel=1e-1,
    detail="Expected fail: exact closure with the stated formula requires c4 about 2.19.",
)
c4_candidate = 21 / 5
m_c4_candidate = (V_EW / 2) * math.sqrt(1 + 4 * LAM**2 / 5 - c4_candidate * LAM**4)
v.check(
    "NNLO candidate c4=21/5 mass",
    m_c4_candidate,
    M_H_PDG,
    abs_tol=0.11,
    detail="Expected fail: c4=21/5 predicts about 124.90 GeV, not 125.20 GeV.",
)

sys.exit(v.summary())
