Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:55

0001 #!/usr/bin/env bash
0002 
0003 set -e
0004 
0005 export OPTICKS_MAX_BOUNCE=32
0006 
0007 SEED=42
0008 NSIGMA=3
0009 PASS=true
0010 
0011 check_within_nsigma() {
0012     local label=$1
0013     local n1=$2
0014     local n2=$3
0015     local nsigma=$4
0016     local diff=$(( n1 - n2 ))
0017     if [ "$diff" -lt 0 ]; then diff=$(( -diff )); fi
0018     # For two independent Poisson counts: sigma = sqrt(n1 + n2)
0019     local threshold=$(awk "BEGIN {printf \"%d\", $nsigma * sqrt($n1 + $n2) + 1}")
0020     if [ "$diff" -le "$threshold" ]; then
0021         echo "PASSED: $label |$n1 - $n2| = $diff <= ${nsigma}-sigma threshold ($threshold)"
0022     else
0023         echo "FAILED: $label |$n1 - $n2| = $diff > ${nsigma}-sigma threshold ($threshold)"
0024         PASS=false
0025     fi
0026 }
0027 
0028 check_nonzero() {
0029     local label=$1
0030     local actual=$2
0031     if [ "$actual" -gt 0 ]; then
0032         echo "PASSED: $label ($actual) > 0"
0033     else
0034         echo "FAILED: $label ($actual) should be > 0"
0035         PASS=false
0036     fi
0037 }
0038 
0039 # ---- Test: GPU vs CPU hit comparison with 8x8 SiPM + CsI + optical grease ----
0040 echo "=== Test: GPUPhotonSource with 8x8SiPM_w_CSI_optial_grease.gdml ==="
0041 echo "Config: 1000 photons inside crystal at (-8.7, -8.7, 4.0)mm, 420nm, disc r=0.5mm"
0042 echo "Running GPUPhotonSource with seed $SEED ..."
0043 
0044 OUTPUT=$(USER=fakeuser GEOM=fakegeom GPUPhotonSource \
0045     -g "$OPTICKS_HOME/tests/geom/8x8SiPM_w_CSI_optial_grease.gdml" \
0046     -c 8x8SiPM_crystal \
0047     -m "$OPTICKS_HOME/tests/run.mac" \
0048     -s "$SEED" 2>&1)
0049 
0050 G4_HITS=$(echo "$OUTPUT" | grep "Geant4: NumHits:" | awk '{print $NF}')
0051 OPTICKS_HITS=$(echo "$OUTPUT" | grep "Opticks: NumHits:" | tail -1 | awk '{print $NF}')
0052 
0053 echo ""
0054 echo "Geant4 (CPU):  NumHits: $G4_HITS"
0055 echo "Opticks (GPU): NumHits: $OPTICKS_HITS"
0056 
0057 # Both should produce hits
0058 check_nonzero "Geant4 hits"  "$G4_HITS"
0059 check_nonzero "Opticks hits" "$OPTICKS_HITS"
0060 
0061 # GPU and CPU should agree within 3-sigma (Poisson statistics)
0062 echo ""
0063 echo "=== Comparing GPU vs CPU hit counts (${NSIGMA}-sigma) ==="
0064 check_within_nsigma "Opticks vs Geant4" "$OPTICKS_HITS" "$G4_HITS" "$NSIGMA"
0065 
0066 # ---- Summary ----
0067 echo ""
0068 echo "=== Summary ==="
0069 echo "GPU (Opticks): $OPTICKS_HITS hits"
0070 echo "CPU (Geant4):  $G4_HITS hits"
0071 if [ "$G4_HITS" -gt 0 ]; then
0072     PCT=$(awk "BEGIN {printf \"%.1f\", ($OPTICKS_HITS/$G4_HITS)*100}")
0073     echo "GPU/CPU ratio: ${PCT}%"
0074 fi
0075 echo ""
0076 if [ "$PASS" = true ]; then
0077     echo "All tests passed"
0078     exit 0
0079 else
0080     echo "Some tests FAILED"
0081     exit 1
0082 fi