Skip to content

DETECTOR_OUT_*_MEAN: central output of mean values (polarisation monitors, Monitor_nD signal=) - #3193

Open
willend wants to merge 5 commits into
mainfrom
detector-out-mean
Open

willend wants to merge 5 commits into
mainfrom
detector-out-mean

Conversation

@willend

@willend willend commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Free-form text area

Follow-up to / replacement for #3057. It keeps the idea from there (merge the raw sums over MPI nodes first, then form the mean), but does it once in the runtime instead of in each monitor.

The detector output treats all data as intensities. For a mean this gives wrong values or errors with MPI, wrong errors in serial, and wrong results after intermediate saves.

Runtime: new macros DETECTOR_OUT_0D/1D/2D_MEAN, taking the sums over events of 1, w, w², w·v and w·v².

  • The sums are copied, merged over nodes, and turned into mean and error. The component's own arrays are not changed.
  • The error is sqrt(variance/Neff), Neff = (Σw)²/Σw², with the N-1 correction.
  • The header total and Detector: line give the mean over all bins, instead of the sum of bins.
  • All data headers now have mean: yes or mean: no (ASCII line / NeXus attribute). Data without it is from an older version.
  • No existing signature is changed. Intensity data is not affected, apart from this header line.

Uses the macros / functions: Pol_monitor, MeanPolLambda_monitor, PSD_Pol_monitor, and Monitor_nD with signal= (McStas and McXtrace). Manuals: both updated.

Numerical changes (all monitors above; no change in ray-tracing or RNG use). Fully polarised beam, 10 000 rays, serial / 2 MPI nodes:

main this PR
PSD_Pol_monitor, per bin 1 ± 1.0 / 2 ± 1.4, N doubled with MPI 1 ± 1e-12 / same
Pol_monitor 1 ± 1e-4 / 1 ± 0.017 1 ± 1e-12 / same
MeanPolLambda_monitor, per bin 1 ± 4e-4 / 1 ± 0.020 1 ± 1e-12 / same
Monitor_nD signal=lambda 5.002 ± 0.050 / 10.02 ± 0.10 5.002 ± 0.0058 / 5.011 ± 0.0058

Header totals of 1D/2D means also change, e.g. 7.42 to 0.341 for media in He3_spin_filter.

Known limits

  • The error can be up to about 1.3 times too large if weight and value are strongly correlated.
  • The error of a bin with one event is set to its value, as for intensities.
  • Monitor_nD with log of the signal, and 2D list with signal=, keep the old path and say mean: no.
  • PSD_monitor_coh (McXtrace) is not additive either, and is not touched. It also says mean: no.

Declaration of use of AI-tools

  • Please add a checkmark here if you used AI-tools during the work for this contribution
  • Further, please describe how / where and for what the tools were used: Claude Code (Claude Opus 5.5) wrote the code, documentation and this text, and ran the tests, directed by @willend.
  • AI-assisted commits must carry a Co-Authored-By line naming the tool, see CONTRIBUTING.md

Development OS / boundary conditions

macOS arm64 (clang), plus Linux x86_64 in a container (gcc 14). No new dependencies.

Tested:

  • mctest of the 7 instruments using the polarisation monitors, serial and 2 MPI nodes: all pass. The non-mean detector totals are identical to main.
  • mctest/mxtest of Test_Monitor_nD* and libtest in both flavours: same detector totals as main. Test_Monitor_nD_list_MPI did not link for McXtrace, on main too (NeXus library missing in my environment).
  • A test instrument with all the monitors: the header totals agree between 0D, 1D and 2D, serial and MPI; NeXus output; intermediate saves (USR2).
  • McXtrace signal=energy, serial and 2 MPI nodes.
  • Linux: compile, serial, MPI, ASan and UBSan. Only the four small leaks (160 bytes) that main also has.
  • Existing readers of the data headers need no change, see the comment.
  • Both PDF manuals build.

Not tested: Windows (MSVC), GPU/OpenACC, HTML manuals.


PR Checklist for contributing to McStas/McXtrace

  • My contribution includes patches to an existing component file

    • I have used the mcdoc utility and rendered a reasonable documentation page for the component (component headers are not changed)
    • I have ensured that basic use of the component is OK (e.g. an instrument using it compiles?)
    • I have used the mccode-clangformat tool to apply the standard McCode component indentation scheme
    • I have used the mcrun --c-lint "linter" and followed advice to remove most / all warnings that are raised (stopped after 3 minutes; a plain cppcheck gave no warnings in the new runtime code)
  • My work touches / adds to the runtime lib code (.c,.h etc in multiple locations

    • I have added reasoning and documentation for the change below
    • I am attaching test output in the comments
    • Unit test: mccode-r is in every instrument and has no Test_lib_ of its own. Test_lib_monitor_nd_lib exists in both flavours, but does not test signal=. The mean output is run by the existing polarisation tests.

🤖 Generated with Claude Code


Tip for reviewers: Visit https://blueblazin.github.io/blockviz/#github=https://github.com/mccode-dev/McCode/pull/3193 for a visual look at code changes

willend and others added 5 commits November 25, 2026 10:38
Output of a mean from sums over events, merged over MPI nodes before the mean is formed. The sums are not changed. Header total is the mean over all bins, marked mean: yes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fixes mean and error with MPI and on intermediate saves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Not for log of the signal and 2D with list, which are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Data without it is from an older version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@willend

willend commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

8d9eb73: data headers now always have mean: yes or mean: no (ASCII line / NeXus attribute), instead of mean: yes for means only. Data without it is then known to be from an older version, e.g. when merging datasets. Both manuals updated.

Existing readers need no change. Tested on macOS, mostly against the same data with the marker removed:

Reader Result
mcplotloader (run, scan, NeXus run and scan) identical
mcplot-matplotlib (same) identical PNGs
mcplot-html (run, scan, NeXus run) identical apart from directory names
mcplot-plopp, mcplotdiff (matplotlib, html) run without errors
mcrun scan, --scan_split, --optimize OK
mcsimdetectors, mcdisplay reader for --simdir identical
mctest (17 tests) pass
Matlab mcplot OK, gets one extra field mean

mcplot-pyqtgraph gave no errors, but was stopped with its window open. ROOT: only checked by reading the source. Not tested with McXtrace data; the changed code is shared.

Output still on the old paths says mean: no, although it is a mean: Monitor_nD with log of the signal, and 2D list with signal=. So does the phase output of PSD_monitor_coh (McXtrace), which is not additive either, and any third-party monitor not using the new macros.

🤖 Generated with Claude Code

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

⌂ Proxy