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 export OPTICKS_MAX_SLOT=M1
0005 
0006 SEED=42
0007 PASS=true
0008 PHOTON_FILE=$(mktemp /tmp/test_photons_XXXXXX.txt)
0009 HITS_FILE=opticks_hits_output.txt
0010 
0011 cleanup() {
0012     rm -f "$PHOTON_FILE" "$HITS_FILE"
0013 }
0014 trap cleanup EXIT
0015 
0016 # ---- Test 1: Basic file loading and GPU simulation ----
0017 echo "=== Test 1: 10 photons from text file ==="
0018 
0019 cat > "$PHOTON_FILE" <<'EOF'
0020 # pos_x pos_y pos_z time mom_x mom_y mom_z pol_x pol_y pol_z wavelength
0021 -10.0 -30.0 -90.0  0.0  0.0 0.287348 0.957826  1.0 0.0 0.0  420.0
0022 -10.0 -30.0 -90.0  0.0  0.0 0.287348 0.957826  1.0 0.0 0.0  420.0
0023 -10.0 -30.0 -90.0  0.0  0.0 0.287348 0.957826  1.0 0.0 0.0  420.0
0024 -10.0 -30.0 -90.0  0.0  0.0 0.287348 0.957826  1.0 0.0 0.0  450.0
0025 -10.0 -30.0 -90.0  0.0  0.0 0.287348 0.957826  1.0 0.0 0.0  450.0
0026 -10.0 -30.0 -90.0  0.0  0.0 0.287348 0.957826  1.0 0.0 0.0  500.0
0027 -10.0 -30.0 -90.0  0.0  0.0 0.287348 0.957826  1.0 0.0 0.0  500.0
0028 -10.0 -30.0 -90.0  0.0  0.0 0.287348 0.957826  1.0 0.0 0.0  500.0
0029 -10.0 -30.0 -90.0  0.0  0.0 0.287348 0.957826  1.0 0.0 0.0  500.0
0030 -10.0 -30.0 -90.0  0.0  0.0 0.287348 0.957826  1.0 0.0 0.0  500.0
0031 EOF
0032 
0033 echo "Running GPUPhotonFileSource with seed $SEED ..."
0034 OUTPUT=$(USER=fakeuser GEOM=fakegeom GPUPhotonFileSource \
0035     -g "$OPTICKS_HOME/tests/geom/opticks_raindrop.gdml" \
0036     -p "$PHOTON_FILE" \
0037     -m "$OPTICKS_HOME/tests/run.mac" \
0038     -s "$SEED" 2>&1)
0039 
0040 LOADED=$(echo "$OUTPUT" | grep "Loaded" | awk '{print $2}')
0041 OPTICKS_HITS=$(echo "$OUTPUT" | grep "Opticks: NumHits:" | awk '{print $NF}')
0042 
0043 echo "Loaded:  $LOADED photons"
0044 echo "Opticks: NumHits: $OPTICKS_HITS"
0045 
0046 if [ "$LOADED" -eq 10 ]; then
0047     echo "PASSED: Loaded correct number of photons (10)"
0048 else
0049     echo "FAILED: Expected 10 loaded photons, got $LOADED"
0050     PASS=false
0051 fi
0052 
0053 if [ "$OPTICKS_HITS" -eq 10 ]; then
0054     echo "PASSED: All 10 photons produced hits"
0055 else
0056     echo "FAILED: Expected 10 hits, got $OPTICKS_HITS"
0057     PASS=false
0058 fi
0059 
0060 # Check output file exists and has correct number of lines
0061 if [ ! -f "$HITS_FILE" ]; then
0062     echo "FAILED: Output file $HITS_FILE not found"
0063     PASS=false
0064 else
0065     HIT_LINES=$(wc -l < "$HITS_FILE")
0066     if [ "$HIT_LINES" -eq 10 ]; then
0067         echo "PASSED: Output file has 10 hit lines"
0068     else
0069         echo "FAILED: Expected 10 lines in output, got $HIT_LINES"
0070         PASS=false
0071     fi
0072 
0073     # Check wavelengths are preserved (3 at 420, 2 at 450, 5 at 500)
0074     N420=$(grep -c " 420 " "$HITS_FILE" || true)
0075     N450=$(grep -c " 450 " "$HITS_FILE" || true)
0076     N500=$(grep -c " 500 " "$HITS_FILE" || true)
0077     echo "Wavelength counts: 420nm=$N420 450nm=$N450 500nm=$N500"
0078 
0079     if [ "$N420" -eq 3 ] && [ "$N450" -eq 2 ] && [ "$N500" -eq 5 ]; then
0080         echo "PASSED: Wavelengths preserved correctly"
0081     else
0082         echo "FAILED: Expected 420nm=3, 450nm=2, 500nm=5"
0083         PASS=false
0084     fi
0085 fi
0086 
0087 # ---- Test 2: Comments and blank lines are skipped ----
0088 echo ""
0089 echo "=== Test 2: Comments and blank lines ==="
0090 
0091 cat > "$PHOTON_FILE" <<'EOF'
0092 # This is a comment
0093 
0094 # Another comment
0095 -10.0 -30.0 -90.0  0.0  0.0 0.287348 0.957826  1.0 0.0 0.0  420.0
0096 
0097 -10.0 -30.0 -90.0  0.0  0.0 0.287348 0.957826  1.0 0.0 0.0  450.0
0098 # Trailing comment
0099 EOF
0100 
0101 echo "Running GPUPhotonFileSource ..."
0102 OUTPUT=$(USER=fakeuser GEOM=fakegeom GPUPhotonFileSource \
0103     -g "$OPTICKS_HOME/tests/geom/opticks_raindrop.gdml" \
0104     -p "$PHOTON_FILE" \
0105     -m "$OPTICKS_HOME/tests/run.mac" \
0106     -s "$SEED" 2>&1)
0107 
0108 LOADED=$(echo "$OUTPUT" | grep "Loaded" | awk '{print $2}')
0109 OPTICKS_HITS=$(echo "$OUTPUT" | grep "Opticks: NumHits:" | awk '{print $NF}')
0110 
0111 echo "Loaded:  $LOADED photons"
0112 echo "Opticks: NumHits: $OPTICKS_HITS"
0113 
0114 if [ "$LOADED" -eq 2 ]; then
0115     echo "PASSED: Correctly skipped comments and blank lines (loaded 2)"
0116 else
0117     echo "FAILED: Expected 2 photons, got $LOADED"
0118     PASS=false
0119 fi
0120 
0121 # ---- Test 3: Missing --photons argument should fail ----
0122 echo ""
0123 echo "=== Test 3: Missing --photons argument ==="
0124 
0125 if GPUPhotonFileSource \
0126     -g "$OPTICKS_HOME/tests/geom/opticks_raindrop.gdml" \
0127     -m "$OPTICKS_HOME/tests/run.mac" 2>&1; then
0128     echo "FAILED: Should have exited with error for missing --photons"
0129     PASS=false
0130 else
0131     echo "PASSED: Correctly failed when --photons not provided"
0132 fi
0133 
0134 # ---- Summary ----
0135 echo ""
0136 if [ "$PASS" = true ]; then
0137     echo "All tests passed"
0138     exit 0
0139 else
0140     echo "Some tests FAILED"
0141     exit 1
0142 fi