Monitors and Twiss conversion

xtrack_tools.monitors.get_monitor_names_at_pattern(line, pattern)[source]

Return line element names matching a regex pattern.

Parameters:
  • line (xt.Line) – Line whose element names should be searched.

  • pattern (str) – Regular expression matched against each element name.

Returns:

Matching element names in line order.

Raises:

ValueError – If the pattern matches no elements.

Return type:

list[str]

xtrack_tools.monitors.replace_thick_monitors_with_thin_markers(line, monitor_names, length_tol=1e-12)[source]

Insert thin markers at the centers of thick monitor elements.

Parameters:
  • line (xt.Line) – Line to modify in place.

  • monitor_names (list[str]) – Element names intended for multi_element_monitor_at.

  • length_tol (float) – Elements with absolute length at or below this threshold are treated as already thin and left unchanged.

Returns:

A tuple (thin_monitor_names, alias_map) where thin_monitor_names can be passed directly to multi_element_monitor_at and alias_map maps any inserted thin-marker names back to the original monitor names.

Raises:

ValueError – If any requested monitor name is not present in the line.

Return type:

tuple[list[str], dict[str, str]]

xtrack_tools.monitors.tbt_data_to_dataframes(tbt_data, tracked_line)[source]

Convert TbtData into per-particle tracking DataFrames.

Each returned DataFrame contains the monitor name, turn index (1-based), and transverse coordinates for a single particle.

Parameters:
  • tbt_data (TbtData) – Turn-by-turn data, one matrix per tracked particle.

  • tracked_line (xt.Line) – Tracked line containing multi-element monitor data for px/py.

Returns:

A list of DataFrames, one per particle.

Return type:

list[pd.DataFrame]

xtrack_tools.monitors.line_to_dataframes(tracked_line)[source]

Convert tracked multi-element monitor data into per-particle DataFrames.

Parameters:

tracked_line (xt.Line) – Line containing recorded multi-element monitor data.

Returns:

A list of DataFrames, one per tracked particle.

Raises:

ValueError – If the line has no recorded multi-element monitor data.

Return type:

list[pd.DataFrame]

xtrack_tools.monitors.process_tracking_data(monitored_line, ramp_turns=1000, flattop_turns=100, add_variance_columns=True)[source]

Trim ramp turns from tracking data and optionally add variance columns.

Parameters:
  • monitored_line (xt.Line) – Line with monitor data already collected.

  • ramp_turns (int) – Number of initial turns to discard.

  • flattop_turns (int) – Number of turns to keep after the ramp.

  • add_variance_columns (bool) – Whether to add default variance and kick-plane columns.

Returns:

A single DataFrame containing processed tracking data for the first particle.

Return type:

pd.DataFrame

xtrack_tools.monitors.xsuite_tws_to_ng(tws, *, momentum='pt')[source]

Convert an xsuite Twiss object to an NG-compatible tfs DataFrame.

Besides renaming the optics columns this puts the dispersion columns into MAD-NG’s convention, which differs from xsuite’s in two ways:

  • xsuite’s ddx/ddpx/ddy/ddpy are plain second derivatives, while MAD-NG’s chrom=true twiss folds the Taylor \(1/2\) into them.

  • xsuite differentiates with respect to delta = dp/p, MAD-NG with respect to the energy coordinate pt. The two differ by factors of the relativistic beta0 (~0.52 on the PSB at 160 MeV, i.e. nowhere near 1).

With \(\delta = \delta(p_t) = p_t/\beta_0 - p_t^2/(2\beta_0^2\gamma_0^2)\) the returned columns satisfy, for momentum="pt",

\[x(p_t) = x_{co} + p_t\,D_x + p_t^2\,D_x^{(2)},\]

i.e. exactly the form the consumers evaluate (no extra factors), with

\[D_x = \frac{d_x}{\beta_0}, \qquad D_x^{(2)} = \frac{dd_x}{2\beta_0^2} - \frac{d_x}{2\beta_0^2\gamma_0^2}.\]
Parameters:
  • tws – xsuite Twiss object.

  • momentum (str) – Conjugate variable of the returned dispersion columns. "pt" (default) gives full MAD-NG conventions: dx/dpx/ dy/dpy are derivatives w.r.t. pt and ddx/ddpx/ ddy/ddpy are the second-order coefficients w.r.t. pt (Taylor 1/2 already folded in). "delta" keeps xsuite’s delta = dp/p as the conjugate variable — first-order columns are passed through unchanged and the second-order ones are still turned into coefficients (ddx/2), so the same consumer formula applies with delta substituted for pt. Use it only for pipelines that stay in delta end to end.

Returns:

A tfs.TfsDataFrame with NG-compatible column names and headers.

Return type:

DataFrame