Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-15 07:42:42

0001 #!/bin/bash
0002 #
0003 # test_wavelength_shifting.sh
0004 # ============================
0005 # End-to-end test: GPU vs G4 wavelength shifting physics
0006 #
0007 # Emits UV photons (350nm) from inside a WLS sphere and compares
0008 # GPU (opticks) and G4 hit wavelength distributions, WLS conversion
0009 # rate, and arrival-time distributions using KS tests.
0010 #
0011 # Geometry: tests/geom/wls_test.gdml (introduced in #269)
0012 #   - WLS sphere r=20mm (absorbs UV, re-emits visible)
0013 #   - Detector shell r=28-30mm (100% efficiency)
0014 #   - Air outside the sphere
0015 #
0016 # Usage:
0017 #   ./tests/test_wavelength_shifting.sh [seed]
0018 #
0019 # Exit code 0 = PASS, 1 = FAIL
0020 #
0021 
0022 set -e
0023 
0024 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
0025 REPO_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
0026 SEED=${1:-42}
0027 GEOM_FILE="$REPO_DIR/tests/geom/wls_test.gdml"
0028 CONFIG="wls_test"
0029 
0030 # StandAloneGeant4Validation is provided by PR #271. SKIP rather than
0031 # FAIL when it isn't installed so this test is benign on branches where
0032 # #271 hasn't landed yet.
0033 if ! command -v StandAloneGeant4Validation >/dev/null 2>&1; then
0034     echo "SKIPPED: StandAloneGeant4Validation not installed (depends on PR #271)"
0035     exit 0
0036 fi
0037 
0038 source /opt/eic-opticks/eic-opticks-env.sh 2>/dev/null || true
0039 export OPTICKS_MAX_BOUNCE=100
0040 export OPTICKS_EVENT_MODE=HitPhoton
0041 export OPTICKS_MAX_SLOT=100000
0042 
0043 # Override USER/GEOM so opticks writes to a deterministic, test-only path
0044 # regardless of the developer's environment. Matches the convention used
0045 # by tests/test_GPURaytrace.sh and tests/test_GPUPhotonSource_8x8SiPM.sh.
0046 export USER=fakeuser
0047 export GEOM=fakegeom
0048 
0049 GPU_HIT_FILE="/tmp/${USER}/opticks/GEOM/${GEOM}/GPUPhotonSourceMinimal/ALL0_no_opticks_event_name/A000/hit.npy"
0050 G4_HIT_FILE="g4_hits.npy"
0051 
0052 # Clear any stale outputs from a prior run so we never compare against
0053 # leftover data if the binaries fail silently.
0054 rm -f "$GPU_HIT_FILE" "$G4_HIT_FILE"
0055 
0056 echo "=============================================="
0057 echo " WLS Test: GPU vs G4 Wavelength Shifting"
0058 echo "=============================================="
0059 echo "  Geometry: $GEOM_FILE"
0060 echo "  Config:   $CONFIG"
0061 echo "  Seed:     $SEED"
0062 echo ""
0063 
0064 # --- GPU run ---
0065 echo "[GPU] Running GPUPhotonSourceMinimal..."
0066 GPU_OUT=$(GPUPhotonSourceMinimal \
0067     -g "$GEOM_FILE" -c "$CONFIG" -m "$REPO_DIR/tests/run.mac" -s "$SEED" 2>&1)
0068 GPU_HITS=$(echo "$GPU_OUT" | grep "Opticks: NumHits" | head -1 | awk '{print $NF}')
0069 echo "[GPU] Hits: $GPU_HITS"
0070 
0071 # --- G4 run ---
0072 echo "[G4]  Running StandAloneGeant4Validation..."
0073 G4_OUT=$(StandAloneGeant4Validation \
0074     -g "$GEOM_FILE" -c "$CONFIG" -s "$SEED" 2>&1)
0075 G4_HITS=$(echo "$G4_OUT" | grep "Total accumulated hits" | awk '{print $NF}')
0076 echo "[G4]  Hits: $G4_HITS"
0077 
0078 # --- Compare ---
0079 echo ""
0080 echo "[COMPARE] Analyzing wavelength and time distributions..."
0081 echo ""
0082 
0083 python3 "$REPO_DIR/optiphy/ana/wls_test.py" "$GPU_HIT_FILE" "$G4_HIT_FILE"