#!/bin/zsh
# Cross-check campaign: Hessian + MC-replica fits, L0 and L1 closure.
set -u
MM="/opt/homebrew/bin/micromamba run -r $HOME/micromamba -n colibri-dev"
cd ~/colibri-runs
echo "=== $(date) start"

# fresh dirs
rm -rf lh_hessian_L0 lh_hessian_L1 lh_mc_L0 lh_mc_L1

# --- Hessian L0 and L1 concurrently (the long pole) ---
${=MM} les_houches_exe lh_hessian_L0.yaml > hessian_L0.log 2>&1 &
HPID0=$!
${=MM} les_houches_exe lh_hessian_L1.yaml > hessian_L1.log 2>&1 &
HPID1=$!

# --- MC replicas: 100 per level, 6-way parallel ---
for LVL in L0 L1; do
  echo "=== $(date) MC $LVL replicas"
  seq 1 100 | xargs -P 6 -I{} /opt/homebrew/bin/micromamba run -r $HOME/micromamba -n colibri-dev \
    les_houches_exe lh_mc_${LVL}.yaml -rep {} -o lh_mc_${LVL} > /dev/null 2>&1
  echo "=== $(date) MC $LVL postfit"
  ${=MM} mc_postfit lh_mc_${LVL} > mc_postfit_${LVL}.log 2>&1 || echo "mc_postfit $LVL FAILED"
done

echo "=== $(date) waiting for Hessian fits (pids $HPID0 $HPID1)"
wait $HPID0; echo "hessian L0 exit: $?"
wait $HPID1; echo "hessian L1 exit: $?"
echo "=== $(date) ALL DONE"
