Skip to content

Photon history summary analysis script#245

Open
ggalgoczi wants to merge 12 commits intomainfrom
photon_history_summary_analysis_script
Open

Photon history summary analysis script#245
ggalgoczi wants to merge 12 commits intomainfrom
photon_history_summary_analysis_script

Conversation

@ggalgoczi
Copy link
Contributor

Added new script that creates a histogram of photon histories so users can understand what happened to the photons besides getting the hits. The GPU code saves photon state as NumPy arrays where each photon is a (4, 4) float32 matrix: four quads of four floats. photon.npy holds the final state of every photon, hit.npy is the subset that triggered detectors, inphoton.npy is the initial state before simulation, and record.npy stores the full step-by-step history (up to 32 bounces per photon). seq.npy is a compressed version of the step history using 4-bit nibbles packed into uint64 pairs.

he script analyzes GPU optical photon simulation output to debug where photons went and why: which were detected, which were absorbed, which got trapped bouncing forever, and whether the physics (wavelength shifting, energy conservation)
  worked correctly. Without it, the simulation is a black box that only reports a hit count.
Update with photon history summary script
@ggalgoczi ggalgoczi requested a review from plexoos March 26, 2026 14:11
@ggalgoczi ggalgoczi self-assigned this Mar 26, 2026
@ggalgoczi ggalgoczi added the enhancement New feature or request label Mar 26, 2026
@plexoos plexoos requested a review from Copilot March 26, 2026 15:53
@plexoos
Copy link
Member

plexoos commented Mar 26, 2026

@codex review

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new analysis utility in ana/ to summarize Opticks GPU photon outcomes and histories from saved .npy event outputs, and documents how to enable and use the output-saving event mode.

Changes:

  • Added ana/photon_history_summary.py to load photon.npy/hit.npy/inphoton.npy/record.npy/seq.npy and print outcome/history/sequence/step-count summary tables plus optional per-photon traces.
  • Added README documentation describing output file locations, photon/seq layouts, and example invocation commands for the analysis script.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 7 comments.

File Description
ana/photon_history_summary.py New CLI script that loads event arrays and prints tables/traces to help debug photon terminal flags, cumulative histories, sequence histories, and record step counts.
README.md New documentation section explaining prerequisites, output folder structure, and how to run the photon history analysis.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


Usage::

python ana/photon_table.py /tmp/MISSING_USER/opticks/GEOM/GEOM/GPUPhotonSourceMinimal/ALL0_no_opticks_event_name/A000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ana/photon_table.py does not exist

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1ecc176ba7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@plexoos
Copy link
Member

plexoos commented Mar 26, 2026

Please move this script to optiphy/ or optiphy/ana because that is the python package we support and install.

Moved from ana to optiphy/ana
Move photon summary script from ana to optiphy/ana
@ggalgoczi
Copy link
Contributor Author

Please move this script to optiphy/ or optiphy/ana because that is the python package we support and install.

Sure, done

@ggalgoczi
Copy link
Contributor Author

@plexoos good from my side to merge

@plexoos
Copy link
Member

plexoos commented Mar 26, 2026

Thanks! I’ll take a closer look later. Does this script generate plots? If so, do you have any example plots for at least one of our test geometries?

@ggalgoczi
Copy link
Contributor Author

No plots, this script is strictly for photon history debugging. Creates summarizes of what happened to photons so the developer can see if photons escaped or died etc. Example output:

======================================================================
PHOTON OUTCOMES (by terminal flag)

Hitmask: SD (0x40)

Flag                     Count       %  Boundary indices
---------------------- ------- -------  ------------------------------
SURFACE_DETECT             990   99.0%  bnd=[2]
BOUNDARY_REFLECT            10    1.0%  bnd=[1]

Total photons: 1000   Hits: 990   Lost: 10   Hit rate: 99.0%
Truncated at MaxBounce (32 steps): 10

======================================================================
PHOTON HISTORIES (by cumulative flagmask)

Flagmask                                   Count       %
---------------------------------------- ------- -------
TO|RE|SD|BT                                  950   95.0%
TO|RE|SD|BR|BT                                40    4.0%
TO|RE|BR                                      10    1.0%

======================================================================
WAVELENGTH ANALYSIS

Final photons:  min=405.2  max=676.0  mean=486.6  std=37.3 nm
Input photons:  min=350.0  max=350.0  mean=350.0  std=0.0 nm
Wavelength shifted: 1000/1000
Hit wavelength: min=405.2  max=676.0  mean=486.7  std=37.4 nm
Energy conservation violations: 0

======================================================================
POSITION / TIME

Radius:  min=20.00  max=28.00  mean=27.92 mm
Time:    min=0.134  max=5.680  mean=0.666 ns

======================================================================
STEP SEQUENCE HISTORIES (top 15)

   #    Count       %  Sequence
----  ------- -------  ----------------------------------------
   0      917   91.7%  TO RE BT SD
   1       36    3.6%  TO RE BR BT SD
   2       33    3.3%  TO RE RE BT SD
   3        7    0.7%  TO RE RE BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR
   4        2    0.2%  TO RE RE BR BR RE BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR
   5        2    0.2%  TO RE BR RE BT SD
   6        1    0.1%  TO RE BR BR BR BT SD
   7        1    0.1%  TO RE BR BR BT SD
   8        1    0.1%  TO RE RE RE BR BR BR BR BR BR BR BR BR BR RE BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR

======================================================================
RECORD STEP COUNTS

 Steps    Count       %  Note
------  ------- -------  --------------------
     4      917   91.7%
     5       69    6.9%
     6        3    0.3%
     7        1    0.1%
    32       10    1.0% <-- max (truncated)

Mean steps: 4.4  Max allowed: 32

--trace 0,227 (hit photon + trapped photon):

photon[0]
  Final: pos=(-3.65, -9.93, -25.92) r=28.00mm  t=1.112ns  wl=488.2nm
  Flag:  SURFACE_DETECT  bnd=2  flagmask=TO|RE|SD|BT
  Seq:   TO RE BT SD
  Step  Flag                Bnd         Position (x,y,z)                r      Time       WL
  ----  ------------------  ---  ------------------------------  --------  --------  -------
     0  TORCH                 0  (     0.00,     0.00,     0.00)      0.00     0.000    350.0
     1  BULK_REEMIT           1  (     0.00,     0.00,     0.00)      0.00     0.980    488.2
     2  BOUNDARY_TRANSMIT     1  (    -2.61,    -7.10,   -18.52)     20.00     1.086    488.2
     3  SURFACE_DETECT        2  (    -3.65,    -9.93,   -25.92)     28.00     1.112    488.2

photon[227]
  Final: pos=(8.67, -11.42, 13.94) r=20.00mm  t=4.811ns  wl=476.9nm
  Flag:  BOUNDARY_REFLECT  bnd=1  flagmask=TO|RE|BR
  Seq:   TO RE RE BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR BR
  Step  Flag                Bnd         Position (x,y,z)                r      Time       WL
  ----  ------------------  ---  ------------------------------  --------  --------  -------
     0  TORCH                 0  (     0.00,     0.00,     0.00)      0.00     0.000    350.0
     1  BULK_REEMIT           1  (     0.00,     0.00,     0.00)      0.00     0.051    449.7
     2  BULK_REEMIT           1  (    12.77,    -6.15,     7.25)     15.92     1.138    476.9
     3  BOUNDARY_REFLECT      1  (    19.95,     0.52,    -1.27)     20.00     1.207    476.9
     4  BOUNDARY_REFLECT      1  (     3.99,    12.22,   -15.32)     20.00     1.335    476.9
     5  BOUNDARY_REFLECT      1  (   -17.85,     5.92,    -6.81)     20.00     1.464    476.9
     6  BOUNDARY_REFLECT      1  (   -13.40,    -9.10,    11.73)     20.00     1.593    476.9
     7  BOUNDARY_REFLECT      1  (    10.78,   -10.72,    13.00)     20.00     1.722    476.9
     8  BOUNDARY_REFLECT      1  (    19.09,     3.44,    -4.87)     20.00     1.850    476.9
     9  BOUNDARY_REFLECT      1  (    -0.71,    12.53,   -15.57)     20.00     1.979    476.9
    10  BOUNDARY_REFLECT      1  (   -19.46,     3.17,    -3.34)     20.00     2.108    476.9
    11  BOUNDARY_REFLECT      1  (    -9.55,   -10.86,    13.81)     20.00     2.236    476.9
    12  BOUNDARY_REFLECT      1  (    14.43,    -8.90,    10.62)     20.00     2.365    476.9
    13  BOUNDARY_REFLECT      1  (    17.16,     6.17,    -8.21)     20.00     2.494    476.9
    14  BOUNDARY_REFLECT      1  (    -5.38,    12.15,   -14.95)     20.00     2.623    476.9
    15  BOUNDARY_REFLECT      1  (   -20.00,     0.24,     0.33)     20.00     2.751    476.9
    16  BOUNDARY_REFLECT      1  (    -5.17,   -12.03,    15.12)     20.00     2.880    476.9
    17  BOUNDARY_REFLECT      1  (    17.27,    -6.58,     7.65)     20.00     3.009    476.9
    18  BOUNDARY_REFLECT      1  (    14.28,     8.56,   -11.09)     20.00     3.137    476.9
    19  BOUNDARY_REFLECT      1  (    -9.74,    11.09,   -13.49)     20.00     3.266    476.9
    20  BOUNDARY_REFLECT      1  (   -19.41,    -2.71,     3.97)     20.00     3.395    476.9
    21  BOUNDARY_REFLECT      1  (    -0.50,   -12.52,    15.59)     20.00     3.524    476.9
    22  BOUNDARY_REFLECT      1  (    19.15,    -3.89,     4.25)     20.00     3.652    476.9
    23  BOUNDARY_REFLECT      1  (    10.60,    10.47,   -13.35)     20.00     3.781    476.9
    24  BOUNDARY_REFLECT      1  (   -13.56,     9.41,   -11.29)     20.00     3.910    476.9
    25  BOUNDARY_REFLECT      1  (   -17.75,    -5.50,     7.39)     20.00     4.039    476.9
    26  BOUNDARY_REFLECT      1  (     4.20,   -12.32,    15.19)     20.00     4.167    476.9
    27  BOUNDARY_REFLECT      1  (    19.97,    -0.99,     0.62)     20.00     4.296    476.9
    28  BOUNDARY_REFLECT      1  (     6.33,    11.79,   -14.86)     20.00     4.425    476.9
    29  BOUNDARY_REFLECT      1  (   -16.63,     7.21,    -8.45)     20.00     4.553    476.9
    30  BOUNDARY_REFLECT      1  (   -15.10,    -7.99,    10.40)     20.00     4.682    476.9
    31  BOUNDARY_REFLECT      1  (     8.67,   -11.42,    13.94)     20.00     4.811    476.9

--lost (lost photons summary):

LOST PHOTONS (10 non-detected, hitmask=SD)

BOUNDARY_REFLECT      : 10

Note: 10 lost photon(s) hit the 32-step bounce limit (trapped by total internal reflection?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants