StatMonitor
Introduction
StatMonitor is a beam statistics monitor that records bunch statistics turn-by-turn at a specified longitudinal position, including centroid position, beam size, emittance, Twiss parameters, higher-order moments, and beam loss. It is the core tool for evaluating beam quality evolution and diagnosing beam dynamics behavior.
Code location:
PASS/commands/monitor/statistic.pyClass name:
StatMonitor, registered name"statmonitor"Key features:
Computes bunch statistics in 6D phase space turn-by-turn (first through fourth order moments);
Derives emittance and Twiss parameters (beta, alpha, gamma) from second-order moments;
Records beam loss count and loss percentage;
CPU uses numpy vectorized computation, GPU uses CUDA kernel functions + warp reduction;
Appends data to CSV each turn, converts to TFS format uniformly on the final turn;
Only surviving particles (
tag > 0) are counted; lost particles are excluded.
Working Principle
Statistics Computation
For \(N\) surviving particles in the bunch (\(\text{tag} > 0\)), the moments of each order are defined as:
First-order moment (centroid):
Second-order moment:
Covariance:
Beam size (standard deviation):
Similarly, \(\sigma_{p_x}\), \(\sigma_y\), \(\sigma_{p_y}\), \(\sigma_z\), \(\sigma_{\delta}\) are computed.
Longitudinal statistics use the bunch-relative coordinate \(z_{\mathrm{rel}}\). Before calculating moments, the program folds a temporary copy into \([-C/2,C/2)\) to prevent equivalent ring-period representations from inflating the mean and variance. This statistical fold is not written back to the tracked particle coordinates. Under the ring-coordinate convention, the laboratory centroid can be reconstructed from \(z_{\mathrm{center}}+\langle z_{\mathrm{rel}}\rangle\).
Emittance and Twiss Parameters
The 2D emittance is derived from second-order moments:
where \(\sigma_{x,p_x} = \langle x \, p_x \rangle - \langle x \rangle \langle p_x \rangle\) is the covariance.
Twiss parameters:
Invariant verification:
The formulas for the vertical direction (y) are identical in form; simply replace the subscript x with y.
Higher-Order Moments
Skewness (third standardized moment):
Kurtosis (fourth standardized moment):
Beam Loss
where \(N_{\text{total}}\) is the initial number of macro particles in the bunch, and \(N_{\text{alive}}\) is the current number of surviving particles.
GPU Implementation
The GPU version uses the CUDA kernel function calc_all_stats, employing a grid stride loop to traverse particles. Each thread accumulates 22 statistics in registers, then through warp reduction (__shfl_down_sync) and block reduction, writes the global result via atomicAdd. The maximum number of blocks is 512 (due to atomicAdd contention overhead).
Interface Parameters
Property |
JSON key |
Type |
Default |
Description |
|---|---|---|---|---|
|
|
float |
Required |
Longitudinal position of the monitor in the beamline |
|
|
str |
Required |
Monitor name (automatically filled from the sequence key name) |
|
|
str |
|
Command type identifier |
Note
StatMonitor has no additional configuration parameters. The statistics target all surviving particles in the bunch at that position (tag > 0); no particle indices need to be specified.
Output Files
A pair of files is generated for each bunch at each monitor position:
CSV (appended turn-by-turn):
{hms}_stat_beam{bid}_bunch{bid}_Np_{Np}_s_{s:.4f}.csvTFS (converted from CSV on the final turn):
{hms}_stat_beam{bid}_bunch{bid}_Np_{Np}_s_{s:.4f}.tfs
The output directory is output_dir_stat.
TFS file header:
@ Name PASS Statistic Data
@ Time 2026-07-14 00:11:03
Output columns (35 columns total):
Column name |
Group |
Description |
|---|---|---|
|
Basic |
Turn number |
|
Centroid |
Horizontal position mean \(\langle x \rangle\) |
|
Centroid |
Horizontal momentum mean \(\langle p_x \rangle\) |
|
Beam size |
Horizontal position standard deviation \(\sigma_x\) |
|
Beam size |
Horizontal momentum standard deviation \(\sigma_{p_x}\) |
|
Centroid |
Vertical position mean |
|
Centroid |
Vertical momentum mean |
|
Beam size |
Vertical position standard deviation |
|
Beam size |
Vertical momentum standard deviation |
|
Centroid |
Mean of the folded bunch-relative coordinate \(\langle z_{\mathrm{rel}}\rangle\) |
|
Centroid |
Momentum deviation mean |
|
Beam size |
Standard deviation of the folded bunch-relative coordinate |
|
Beam size |
Momentum deviation standard deviation |
|
Emittance |
Horizontal 2D emittance \(\varepsilon_x\) |
|
Emittance |
Vertical 2D emittance \(\varepsilon_y\) |
|
Twiss |
Horizontal beta function |
|
Twiss |
Vertical beta function |
|
Twiss |
Horizontal alpha function |
|
Twiss |
Vertical alpha function |
|
Twiss |
Horizontal gamma function |
|
Twiss |
Vertical gamma function |
|
Verification |
Horizontal invariant \(\gamma_x \beta_x - \alpha_x^2\) (should equal 1) |
|
Verification |
Vertical invariant (should equal 1) |
|
Longitudinal reference |
Laboratory longitudinal center of the bunch, \(z_{\mathrm{center}}\) |
|
Correlation |
\(\langle x \, z \rangle\) |
|
Correlation |
\(\langle x \, y \rangle\) |
|
Correlation |
\(\langle y \, z \rangle\) |
|
Correlation |
\(\langle x \, z \rangle / (\sigma_x \, \sigma_z)\) normalized correlation |
|
Loss |
Number of lost particles |
|
Loss |
Loss percentage |
|
Higher-order moments |
Horizontal skewness |
|
Higher-order moments |
Horizontal kurtosis |
|
Higher-order moments |
Vertical skewness |
|
Higher-order moments |
Vertical kurtosis |
|
Energy |
Bunch kinetic energy |
Usage Example
The following JSON snippet places a statistics monitor at \(s = 0.0\) m:
"SM1": {
"S (m)": 0.0,
"Command": "StatMonitor"
}
The statistics monitor requires no additional parameters; only the position and command type need to be specified. During simulation, the bunch statistics at that position are recorded turn-by-turn.
Multi-position Monitoring
Multiple statistics monitors can be placed at different positions to compare the variation of bunch statistics along the beamline:
"SM_start": {
"S (m)": 0.0,
"Command": "StatMonitor"
},
"SM_mid": {
"S (m)": 100.0,
"Command": "StatMonitor"
},
"SM_end": {
"S (m)": 250.0,
"Command": "StatMonitor"
}
Application Scenarios
Beam quality assessment: Monitor the evolution of emittance, beam size, and centroid position turn-by-turn to evaluate whether beam quality is stable or degrading
Emittance measurement: Compute emittance and Twiss parameters from second-order moments, and compare with design values for verification
Beam loss diagnostics: Monitor beam loss rate through
beamLossTotalandlossPercent, identifying the turn and position where losses occurNonlinear effect identification: Use higher-order moment information (skewness and kurtosis) to determine the degree to which the beam distribution deviates from Gaussian, identifying nonlinear resonances or dispersion coupling
Momentum-spread monitoring:
sigmadpandsigmaZdescribe longitudinal beam quality within a bunch; usezCenteras well when comparing absolute azimuths across bunchesCorrelation diagnostics: Correlation quantities such as
xzAveragecan be used to diagnose dispersion coupling or transverse-longitudinal coupling