Tracking Fit Tour

A tour of ArcByArcFitter and ACDMarkerFitter: what each part of the code does, in the order it runs. The other fitters differ only in where the particles start and which readings are compared (Fit Families). Other model backends are described in Model backends. Underlined words are defined in the Glossary at the bottom of the page.

The idea

  1. Measure. BPMs record the beam position (x, y) every turn.

  2. Simulate. MAD-NG tracks each measured turn through the model. It returns the position at each BPM and its derivative with respect to each knob.

  3. Fit. The optimiser (Adam, L-BFGS or Levenberg-Marquardt) changes the knobs until the model positions match the measured positions.

Package map

accelmo.config

OptimiserConfig, WorkerRuntime, TrackingDataConfig and TrackingParameters.

accelmo.tracking

The tracking fitters.

  • fitter.py: the fitter classes

  • config/tracking.py: the plan for each mode

  • data_manager.py: reads the measurement files

  • session.py, dispatch/: start the workers

  • worker.py: the tracking worker

  • sgd/loop.py: the optimisation loop

accelmo.fitting

Parts shared with the closed-orbit fitters.

  • setup.py: model, BPM list, starting knobs

  • worker.py: base worker

  • protocol.py: messages between processes

  • reduction.py: sums the worker results

accelmo.machine

Accelerator models.

  • accelerators/: LHC, SPS, PSB, FCC

  • mad/: MAD-NG setup and tracking scripts

accelmo.optimisers

Update rules: adam.py, lbfgs.py.

Not used by the tracking fitters: poco, analysis, runlog, monitor.

tracking ──▶ fitting ──▶ machine
   │
   └─▶ optimisers

Fitter input

The fitter reads one parquet file per measurement with the columns name, turn, bunch_number, x, px, y, py, var_x, var_y, var_px, var_py.

Arc-by-arc vs ACD marker

Both fitters run the same code. They differ in which parts of the ring are tracked.

ring ■ AC dipole start end ✕ s₂ e₂
_after _before whole ring

Left, ArcByArcFitter: tracks every start BPM to every end BPM, forward (teal) and backward (orange), for one turn. With acd_excited=True, a range containing the AC dipole is tracked round the other side of the ring (dashed).

Right, ACDMarkerFitter: two tracks round the whole ring: forward from <acd>_after, backward from <acd>_before.

ArcByArcFitter

ACDMarkerFitter

Plan class

TrackingPlan or ACDArcByArcTrackingPlan

ACDTrackingPlan

Tracks per batch

starts × ends × 2

2

Start point

first BPM of the range

ACD marker

BPMs compared

BPMs inside the range

all BPMs

Both setup functions live in tracking/config/tracking.py.

Trace one fit

From fitter = ArcByArcFitter(...) to fitter.run(). Paths are relative to src/accelmo/.

1. Construction (main process)

Step

Code

What it does

Pick the plan

tracking/fitter.py

The fitter calls its setup function, which returns the plan.

Load the model

fitting/setup.py

MachineSetup loads the machine in MAD-NG and reads the knob names, BPM list and starting knob values.

Filter BPMs

tracking/ranges.py

Removes bad BPMs from the start and end lists.

Load the data

tracking/data_manager.py

DataManager reads the parquet files and splits the turns into training batches and validation turns.

Create the loop

tracking/fitter.py

SGDLoop creates the optimiser and learning-rate schedule.

2. Start the workers (main process)

Step

Code

What it does

run()

tracking/fitter.py

Calls start_session(), which creates a TrackingSession.

Build payloads

tracking/session.py

For each tracking range and each batch, collects the BPMs, the measured data and the weights into one payload.

Spawn

tracking/session.py

Starts one TrackingWorker process per payload, plus validation workers. Each gets a Start(knobs) message.

3. Worker startup (worker process, MAD-NG)

Step

Code

What it does

Wait for messages

fitting/worker.py

Receives Start, then answers messages until Stop.

Load MAD-NG

fitting/worker.py

Loads its own copy of the machine and rotates the sequence to start at its first BPM or marker.

Make knobs differentiable

tracking/worker.py

Adds each knob as a TPSA parameter.

Load particles

tracking/worker.py

Creates one particle per measured turn, at the measured start coordinates.

4. Optimisation loop (all processes)

Step

Code

What it does

Send knobs

tracking/sgd/loop.py

Sends Evaluate(knobs, batch) to every worker.

Track

backend/madng/scripts.py

MAD-NG tracks the batch and stores x, y and their knob derivatives at each BPM.

Loss and gradient

tracking/worker.py

loss = Σ w·(model − measured)². Sends back the loss and gradient as a GradReply.

Step

tracking/sgd/loop.py

Sums the gradients from all workers and updates the knobs with Adam or L-BFGS. With a LevenbergMarquardtConfig solver the fit runs the Levenberg-Marquardt loop over every batch instead.

End of epoch

tracking/sgd/loop.py

Computes the validation loss, saves the best knobs, checks whether to stop, and prints one log line.

5. Finish (main process, workers)

Step

Code

What it does

Uncertainties

tracking/uncertainty.py

Each worker sends its part of the uncertainty matrices, then stops.

Return the result

tracking/fitter.py

Returns a FitResult with the fitted knobs and their uncertainties.

Messages between processes

Defined in fitting/protocol.py. The main process talks to every worker with three messages: Start, Evaluate and Stop.

MAIN PROCESS EACH WORKER Send starting knobs Load MAD-NG Start Batch step shown in the next diagram next batch when finished Send stop Shut down Stop

Batch step

Runs once per batch.

MAIN PROCESS EACH WORKER Send current knobs Track the batch in MAD-NG Evaluate Compute loss + gradient Sum gradients, update knobs loss, gradient

Glossary

AC dipole

A magnet that drives the beam to oscillate. Short form: ACD.

batch

A group of measured turns. Each turn is one particle in a worker.

BPM

Beam position monitor. Measures the beam’s x and y position each turn.

bunch

A group of particles in the beam. Each bunch is processed separately.

epoch

One pass over all batches.

kick

The moment the beam is excited. Turns before it are dropped.

knob

A fitted parameter, usually a magnet strength.

MAD-NG

The accelerator simulation code. Each worker runs its own copy.

parquet

The file format of the measurement data.

payload

Everything one worker needs to start: its BPMs, measured data and weights.

plan

The class that sets the tracking ranges for a mode.

TPSA

A number that carries its derivatives with it. Tracking with TPSA gives derivatives without extra runs.

turn

One trip of the beam around the ring.

validation worker

A worker that tracks held-out turns and reports only a loss.

worker

A separate process that tracks part of the data.