#!/bin/bash
#PBS -N wct-smoke
#PBS -A neutrinoGPU::debug2
#PBS -q debug
#PBS -l select=1
#PBS -l walltime=00:59:00
#PBS -l filesystems=flare:home
#PBS -j oe
#PBS -o /lus/flare/projects/neutrinoGPU/yuhw/aurora-build-logs/smoke.out
# Aurora port of issues/27-polaris-production/scripts/smoke.pbs (Recipe A'):
#   A. operating-point resync + config gate (procedure doc sections 3-4)
#   B. one event of the 1-step chain (smoke-1evt.sh), then N events in parallel
# qsub -v "RECO1=<file>,FCL=<fcl>,NEVT=<n>,DO_RESYNC=0,TAG=<tag>" smoke.pbs
# Default input: one Gen2 MC reco1 file (MC fcl).  Data: FCL=wcls-img-clus-matching-pr-flash-data.fcl.
set -u
Y=/lus/flare/projects/neutrinoGPU/yuhw
S=$Y/wire-cell-toolkit-ai-helper/issues/29-aurora-production/scripts
S27=$Y/wire-cell-toolkit-ai-helper/issues/27-polaris-production/scripts   # resync/compile-both are env-driven and reused as is
RECO1=${RECO1:-/lus/flare/projects/neutrinoGPU/twester/sbnd_gen2_prod1/Gen2_2026/reco1/000000/000000/reco1-detsim-g4-gen-Gen2_2026-00d8-4843-8a75-5672.root}
NEVT=${NEVT:-8}
FCL=${FCL:-wcls-img-clus-matching-pr-flash.fcl}
DO_RESYNC=${DO_RESYNC:-0}   # 0 since toolkit 7a85e5c5 / wcp-porting-validation f04d8937 (2026-09-21): pr-operating-point.jsonnet is gone; the two-chain gate (G4b) replaces the resync
TAG=${TAG:-smoke}
CORES=${CORES:-2}          # cores per lar process (taskset); cores 0 and 52 are reserved on Aurora
RUN=${RUN_DIR:-$Y/production-prep/$TAG-$(date +%Y%m%d-%H%M)}   # RUN_DIR: a fixed run dir (e.g. for a chained compare job)
mkdir -p $RUN
export Y SL7_IMAGE=${SL7_IMAGE:-} APPTAINER_MODE=${APPTAINER_MODE:-}; [ -n "$SL7_IMAGE" ] || unset SL7_IMAGE
echo "=== START $(date) on $(hostname) job ${PBS_JOBID%%.*} RUN=$RUN RECO1=$RECO1 FCL=$FCL"

if [ "$DO_RESYNC" = 1 ]; then
echo "=== A. operating-point resync + config gate"
cd $RUN && SL7_SETUP=$S/setup-aurora-ap.sh $S/in-aurora-sl7.sh bash -c "
  which wcsonnet lar; git -C $Y/wire-cell-toolkit rev-parse --short HEAD
  $S27/resync-operating-point-polaris.sh $RUN/pr-opsync; echo RESYNC_RC=\$?
  git -C $Y/wcp-porting-validation status --short sbnd/pr-operating-point.jsonnet
"
fi

if [ "${DO_GATES:-0}" = 1 ]; then
echo "=== G. pre-flight gates G1-G6 of docs/aurora-1step-validation-plan.md (no regeneration of the operating point)"
cd $RUN && SL7_SETUP=$S/setup-aurora-run.sh $S/in-aurora-sl7.sh bash -c "
  echo '--- G1 build tree = run tree'; git -C $Y/wire-cell-toolkit rev-parse HEAD; git -C $Y/wire-cell-toolkit status --short | head -3; cat $Y/aurora-build-logs/wct-build-commit.txt
  git -C $Y/wcp-porting-validation rev-parse HEAD; git -C $Y/wcp-porting-validation status --short | head -3
  git -C $Y/wire-cell-data rev-parse HEAD; git -C $Y/wire-cell-data status --short | head -3
  echo WIRECELL_PATH=\$WIRECELL_PATH | tr : '\n'
  echo '--- G2 no build-tree RPATH'; for f in $Y/opt/lib/libWireCellClus.so $Y/opt/bin/wire-cell $Y/opt/larwirecell/v10_01_28/slf7.x86_64.e26.prof/lib/libWireCellAIML.so; do echo \"\$(basename \$f): build-dirs=\$(readelf -d \$f | grep -E 'RPATH|RUNPATH' | grep -c build)\"; done
  echo '--- G3 flash_by_gid:' \$(strings $Y/opt/lib/libWireCellClus.so | grep -c flash_by_gid)
  echo '--- G4 operating-point gate (compile-both PR_OP=sync)'; mkdir -p $RUN/g4 && PR_OP=sync $S27/compile-both-polaris.sh $RUN/g4 > $RUN/g4/compile-both.log 2>&1; echo \"G4_RC=\$? \$(tail -1 $RUN/g4/compile-both.log)\"
  echo '--- G4b two-chain gate (doc sbnd_xin/120: 1-step vs local PR job, same cfg tree)'; $S/two-chain-gate-aurora.sh $RUN/g4b 2>&1 | tail -8
  echo '--- G5 DL vertex import'; python3 -c 'import sparseconvnet, torch; print(\"sparseconvnet ok, torch\", torch.__version__)'; echo G5_RC=\$?
  echo '--- G6 spdlog/fmt'; wire-cell --version; ldd $Y/opt/lib/libWireCellUtil.so | grep -E 'spdlog|fmt'
  echo '--- md5 of the weights'; md5sum $Y/wire-cell-data/uboone/weights/XGB_nue_seed2_0923.xml $Y/wire-cell-data/uboone/scn_vtx/t48k-m16-l5-lr5d-res0.5-CP24.pth
"
fi

echo "=== B0. eventdump product check (the fcl reads simtpc2d:dnnsp wires, wienersummary, badmasks, opflashtpc0/1, ionandscint:priorSCE SEDs, MCTruth/MCParticle)"
mkdir -p $RUN/dump && cd $RUN/dump && SL7_SETUP=$S/setup-aurora-opt.sh $S/in-aurora-sl7.sh bash -c "
  lar -c eventdump.fcl -s $RECO1 -n 1 > dump.log 2>&1; echo dump_rc=\$?
  grep -E 'recob::Wires|recob::OpFlashs|FrameShift|SimEnergyDeposits_*ionandscint|simb::MCTruths|std::vector<simb::MCParticle>' dump.log | grep -vE 'Assns|LArG4Detector' | cut -c1-150 | sort -u | head -20
"

echo "=== B1. one event (nskip 0) fcl=$FCL"
cd $RUN && SL7_SETUP=$S/setup-aurora-run.sh $S/in-aurora-sl7.sh bash -c "
  echo scn_on_pythonpath=\$(echo \$PYTHONPATH | tr : '\n' | grep -c scn); echo WIRECELL_PATH=\$WIRECELL_PATH
  $S/smoke-1evt.sh $RUN/evt0 $RECO1 0 $FCL
"

echo "=== B2. $NEVT events in parallel (nskip 1..$NEVT), one process each, taskset $CORES cores (skipping core 0)"
cd $RUN && SL7_SETUP=$S/setup-aurora-run.sh $S/in-aurora-sl7.sh bash -c "
  t0=\$(date +%s)
  for k in \$(seq 1 $NEVT); do
    c=\$(( 1 + (k-1)*$CORES )); ( taskset -c \$c-\$((c+$CORES-1)) $S/smoke-1evt.sh $RUN/evt\$k $RECO1 \$k $FCL > $RUN/evt\$k.summary 2>&1 ) &
  done; wait
  echo parallel_wall_s=\$(( \$(date +%s) - t0 ))
  for k in \$(seq 1 $NEVT); do echo \"evt\$k: \$(grep -E '^rc=' $RUN/evt\$k.summary) \$(grep -E 'dl_vertex_failed|audit=' $RUN/evt\$k.summary | tr '\n' ' ') \$(grep -o 'trees: [0-9]*' $RUN/evt\$k.summary)\"; done
"
echo "=== END $(date)"
