Usage

Installation

uv sync
uv sync --extra dev

Python 3.11 or newer and xsuite>=0.67.2 (xtrack 0.118.6 or newer) are required. The core install needs numpy, packaging, pandas, tfs-pandas, turn-by-turn and xsuite. The madng extra adds pymadng, plot adds matplotlib (plot_kicker_tracking), dev adds pytest, ruff, pre-commit, pymadng and matplotlib, and docs adds Sphinx and the Furo theme.

Build an xsuite environment

create_xsuite_environment runs MAD-X on a sequence file, writes the result to a JSON cache and returns an xt.Environment with the particle reference set. The cache is reused while it is newer than the sequence file; rerun_madx=True regenerates it.

from pathlib import Path

from xtrack_tools import create_xsuite_environment

env = create_xsuite_environment(
    sequence_file=Path("lhcb1.seq"),
    kinetic_energy=6800.0,
    seq_name="lhcb1",
)
line = env["lhcb1"]

The thick elements are configured to reproduce the MAD-NG exact maps: bends use bend-kick-bend, quadrupoles use mat-kick-mat-exact, and sextupoles, octupoles and multipoles use drift-kick-drift-exact. The kicked elements use the yoshida-6 integrator with num_multipole_kicks set to the MAD-NG nslice (default 32). Bends carrying multipole components raise ValueError, and an xtrack older than 0.118.6 raises RuntimeError.

initialise_env additionally sets tune knobs, magnet strengths and corrector settings on the environment.

Action-angle initial conditions

create_initial_conditions converts action-angle values to coordinates at a Twiss row. kick_plane is "x", "y" or "xy", and the inputs may be scalars or arrays.

import numpy as np
import xtrack_tools as xtt

coords = xtt.create_initial_conditions(
    action=np.array([1e-6, 2e-6]),
    angle=np.array([0.25, 0.75]),
    twiss_data=twiss_df,
    kick_plane="xy",
    starting_bpm="BPM.1",
)

Tracking without an AC dipole

run_tracking_without_ac_dipole tracks particles launched from action-angle values or explicit coordinates. With use_diagonal_kicks=False each action-angle pair produces two particles, one kicked in x and one in y. run_tracking succeeds only if every tracked particle survives.

tracked_line = xtt.run_tracking_without_ac_dipole(
    line=line,
    tws=tws,
    flattop_turns=128,
    action_list=[1e-6, 2e-6],
    angle_list=[0.25, 0.75],
    use_diagonal_kicks=False,
)

process_tracking_data removes the first ramp_turns, renumbers the remaining turns from 1 and keeps exactly flattop_turns.

tracking_df = xtt.process_tracking_data(
    monitored_line=tracked_line,
    ramp_turns=1000,
    flattop_turns=128,
)

AC-dipole tracking

run_acd_track builds the environment, inserts horizontal and vertical AC dipoles at a marker, tracks the ramp and flat top, and returns (tracking_df, twiss_table, monitored_line). horizontal_excitation and vertical_excitation are keyword-only. With state_markers=True the markers {acd_marker}_before and {acd_marker}_after are inserted and monitored, so the data carries the pre-kick and post-kick momenta.

tracking_df, twiss, line = xtt.run_acd_track(
    sequence_file=Path("psb3_saved.seq"),
    acd_marker="BR3.HACMAP",
    sequence_name="psb3",
    kinetic_energy=0.16,
    driven_tunes=[0.27, 0.322],
    horizontal_excitation=1.0,
    vertical_excitation=1.0,
)

insert_ac_dipole, run_acd_twiss and run_ac_dipole_tracking expose the individual steps.

Kicker tracking

xtrack_tools.kicker.run_kicker_track tracks one particle with an xt.Exciter placed at a named tkicker. The particle rides the closed orbit until kick_turn (0-based) and oscillates freely afterwards. It returns (tracking_df, twiss_table, baseline_line, s_kicker, kick_turn). run_free_track_from_kick and plot_kicker_tracking (needs matplotlib) are in the same module.

Seeded magnet errors

The helpers in xtrack_tools.errors modify a line in place with reproducible draws and return a mapping of element name to the applied value.

  • apply_relative_bend_field_errors: scales the dipole field k0 of every powered bend by 1 + rel_error, with the geometry unchanged.

  • apply_vertical_quad_misalignment: shifts every quadrupole vertically (shift_y).

  • apply_quad_tilt: rolls every quadrupole about s (rot_s_rad).

  • correct_closed_orbit: flattens the closed orbit with the named monitors and correctors.

Twiss and monitor conversion

xsuite_tws_to_ng converts an xsuite Twiss to MAD-NG column names and dispersion conventions. tbt_data_to_dataframes and line_to_dataframes convert monitor data to DataFrames, and get_monitor_names_at_pattern selects monitors by regular expression.

Development

uv sync --extra dev
pre-commit install
uv run pytest

The slowest test (test_create_xsuite_environment) takes about 60 s; the full suite takes about 3 minutes.

Documentation

uv sync --extra docs
uv run python -m sphinx -b html docs docs/_build/html