#!/usr/bin/env python3
"""
verify_P104.py -- Addendum 104: Absolute W-Boson Mass.

This verifier checks the W/Z mass radiative-correction arithmetic in
104_Addendum_AbsoluteWMass.tex.

The main corrected W-mass values are reproducible: leptonic-only gives about
78.66 GeV, leptonic+top gives about 79.04 GeV, and adding the hadronic
Delta-alpha input gives about 80.19 GeV. The audit flags localized issues in
the leptonic-running wording and in the claimed size of the top-mass residual.
"""

import math
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))
from verify_common import CheckResult, Verifier


class ModernVerifier(Verifier):
    """Output adapter: modern check-line format. Tolerance logic is
    inherited unchanged from verify_common.Verifier; only printing and
    the footer differ."""

    def __init__(self, name: str):
        self.name = name
        self.results = []
        print(name)

    def record(self, label, ok, computed="", claimed="", detail=""):
        self.results.append(CheckResult(label, ok, computed, claimed, detail))
        n = len(self.results)
        expected = (not ok) and ("Expected" in str(detail))
        desc = f"{label} -- {detail}" if expected else label
        print(f"  [{'PASS' if ok else 'FAIL'}] {n:>2}. {desc}")
        if computed != "" or claimed != "":
            print(f"       computed: {computed}")
            print(f"       claimed : {claimed}")
        if detail and not expected:
            print(f"       {detail}")
        return ok

    def summary(self):
        passed = sum(r.ok for r in self.results)
        failed = len(self.results) - passed
        print(f"\n{'='*60}\nRESULT: {passed} PASS / {failed} FAIL")
        return 1 if failed else 0



v = ModernVerifier("P104 -- Absolute W-Boson Mass")

ALPHA = 1 / 137.036
MZ = 91.1876
MW_TREE = 77.41
MW_PDG = 80.377
SW2 = 0.23192
GF = 1.1664e-5
MT_TOE = 176.101
MT_PDG = 172.69
DA_LEPT = 0.03142
DA_HAD = 0.02764

ME = 0.000511
MMU = 0.105658
MTAU = 1.77686

logs = [math.log(MZ / m) for m in (ME, MMU, MTAU)]
sigma_lept = sum(logs)
delta_alpha_leading_log = 2 * ALPHA * sigma_lept / (3 * math.pi)
delta_alpha_with_constant = ALPHA / (3 * math.pi) * sum(2 * math.log(MZ / m) - 5 / 3 for m in (ME, MMU, MTAU))

drho_toe = 3 * GF * MT_TOE**2 / (8 * math.pi**2 * math.sqrt(2))
drho_pdg = 3 * GF * MT_PDG**2 / (8 * math.pi**2 * math.sqrt(2))

mw_lept = MW_TREE / math.sqrt(1 - DA_LEPT)
mw_lept_top = MW_TREE * math.sqrt(1 + drho_toe) / math.sqrt(1 - DA_LEPT)
mw_full = MW_TREE * math.sqrt(1 + drho_toe) / math.sqrt(1 - DA_LEPT - DA_HAD)
mw_full_pdg_top = MW_TREE * math.sqrt(1 + drho_pdg) / math.sqrt(1 - DA_LEPT - DA_HAD)

v.check("ln(MZ/me)", logs[0], 12.093, rel=1e-4)
v.check("ln(MZ/mmu)", logs[1], 6.760, rel=1e-4)
v.check("ln(MZ/mtau)", logs[2], 3.938, rel=1e-4)
v.check("Sigma_lept", sigma_lept, 22.791, rel=3e-5)
v.check("leading-log Delta alpha lept", delta_alpha_leading_log, 0.03529, rel=2e-4)
v.check(
    "displayed leading-log formula gives proposition value 0.03142",
    delta_alpha_leading_log,
    0.03142,
    rel=1e-3,
    detail="Expected fail: 0.03142 requires the standard -5/3 constants, not the displayed pure leading-log formula.",
)
v.check("one-loop Delta alpha with -5/3 constants", delta_alpha_with_constant, 0.03142, rel=5e-5)
v.check(
    "three-loop correction scale alpha^2/pi^2",
    ALPHA**2 / math.pi**2,
    5e-5,
    rel=1e-1,
    detail="Expected fail: alpha^2/pi^2 is about 5.4e-6, one order of magnitude smaller.",
)

v.check("TOE top mass squared", MT_TOE**2, 31011.6, rel=2e-6)
v.check("Delta rho top from TOE mt", drho_toe, 0.009718, rel=5e-5)
v.check("sqrt(1+Delta rho top)", math.sqrt(1 + drho_toe), 1.004862, rel=2e-5)
v.check("sqrt(1-Delta alpha lept)", math.sqrt(1 - DA_LEPT), 0.98416, rel=5e-6)
v.check("leptonic-only MW", mw_lept, 78.66, rel=1e-4)
v.check("leptonic-only residual percent", 100 * (mw_lept - MW_PDG) / MW_PDG, -2.14, rel=2e-3)
v.check("leptonic+top MW", mw_lept_top, 79.04, rel=5e-4)
v.check("leptonic+top residual percent", 100 * (mw_lept_top - MW_PDG) / MW_PDG, -1.67, rel=3e-3)
v.check("sqrt(1-Delta alpha lept-Delta alpha had)", math.sqrt(1 - DA_LEPT - DA_HAD), 0.96997, rel=6e-5)
v.check("full MW with hadronic Delta alpha", mw_full, 80.19, rel=2e-4)
v.check("full MW residual percent", 100 * (mw_full - MW_PDG) / MW_PDG, -0.23, rel=2e-2)
v.check(
    "abstract -0.21 percent final residual",
    100 * (mw_full - MW_PDG) / MW_PDG,
    -0.21,
    rel=2e-2,
    detail="Expected fail: the formula gives about -0.234%, consistent with the table's -0.23%, not -0.21%.",
)

v.check(
    "PDG top Delta rho quoted",
    drho_pdg,
    0.009393,
    rel=1e-3,
    detail="Expected fail: using G_F=1.1664e-5 and mt=172.69 gives about 0.009345.",
)
v.check(
    "MW shift from TOE top to PDG top",
    mw_full - mw_full_pdg_top,
    0.14,
    rel=5e-2,
    detail="Expected fail: in the paper's factorized formula the shift is about 0.015 GeV, not 0.14 GeV.",
)
v.check(
    "full MW using PDG top",
    mw_full_pdg_top,
    80.05,
    abs_tol=0.03,
    detail="Expected fail: the same formula gives about 80.17 GeV.",
)
v.check(
    "full residual using PDG top",
    100 * (mw_full_pdg_top - MW_PDG) / MW_PDG,
    -0.40,
    rel=5e-2,
    detail="Expected fail: the same formula gives about -0.25%.",
)

cosw = math.sqrt(1 - SW2)
mz_lept_top = 79.04 / cosw
mz_full = 80.19 / cosw
v.check("cos theta_W from sin^2=0.23192", cosw, 0.87644, rel=5e-5)
v.check("MZ lept+top from corrected MW", mz_lept_top, 90.19, rel=1e-4)
v.check("MZ full from corrected MW", mz_full, 91.52, rel=3e-4)
v.check("MZ full residual percent", 100 * (mz_full - 91.1876) / 91.1876, 0.36, rel=6e-2)

two_loop_estimate = ALPHA / math.pi * 80 * 0.1
v.check(
    "two-loop estimate alpha/pi * MW * 0.1",
    two_loop_estimate,
    0.06,
    rel=1e-1,
    detail="Expected fail: the displayed estimate evaluates to about 0.019 GeV.",
)
v.check("TOE top percent offset from PDG", 100 * (MT_TOE - MT_PDG) / MT_PDG, 1.98, rel=5e-3)

sys.exit(v.summary())
