No two shots at the MeV electron diffraction setup at Brookhaven's ATF are quite the same. It shoots short electron bunches through a thin crystal and records where they scatter. Each bunch points a little differently and carries a little more or less energy, and over many shots that blurs the pattern you came for. Xi Yang at Brookhaven could read the beam off the pattern by fitting Gaussians to the spots, at about 0.4 s a frame, too slow once the gun fires more than a few times a second. Xi's idea was to train a network on frames the fit had already labeled and let it read every new shot. I built and trained the CNNs, and I'm first author on the two Scientific Reports papers that came out of it (2021 and 2022).
- 0.11 µradRMS error on the beam's horizontal pointing, read off real shots one at a time
- 0.39 msfor the CNN to read one frame on a GPU, in the paper's speed test
- 20 minto retrain on one GPU after a sample change, from a few thousand labeled shots
One shot
Electrons that scatter off the crystal land as bright spots on a phosphor screen. Where the spots sit depends on where the beam points and on its energy. How wide they are depends on its divergence and energy spread. So in principle one frame holds all six numbers.

A small CNN
We started in simulation. Xi tracked the beam from the gun to the sample with GPT, and Xi and Lijun Wu turned each simulated beam into a diffraction pattern with Lijun's code. That gave 10,000 patterns of a strontium titanate crystal, each labeled with the six numbers that made it.
I went with a LeNet-style network, about as plain as a CNN gets. Five rounds of convolution and pooling shrink the frame to a stack of 6×6 maps, then one dense layer feeds a linear output per number.

Energy spread
The first network learned all six at once. Pointing and divergence came out within 2% of their range on patterns it had never seen. Energy was only so-so. Energy spread was close to a guess.

The network wasn't really at fault. A spot's width, measured against how far out the spot sits, comes from both the divergence and the energy spread.
is the beam's divergence, the Bragg angle of that spot, and the spread in its energy .
In this set the divergence was far bigger. In the example we worked through in the paper, the two terms split 95% to 5% before they go under the root. The spread is in the picture, just buried.
So we made a second set where only the energy spread changed and the divergence was squeezed to a few microradians. Now each spot stretches along the radius, and the same network read the spread to about 0.2% of its range. The real beamline can't squeeze the divergence that far. We proposed condenser lenses and an aperture for it and never tried them.

Shots from 2020
Then the real thing, a few thousand shots the BNL group recorded in 2020 on a MoTe2 sample. The detector only catches the first ring of spots, and a Gaussian fit places a spot far better than it measures its width. So only the pointing in x and y and the energy jitter had labels (Xi's fits, shot by shot). Divergence and energy spread had nothing to learn from, so I cut the output layer to three and trained again.
On shots it hadn't seen, the readings sit right on the fitted labels. Vertical pointing came out about as well as horizontal, and the energy jitter to about 3 × 10−5 RMS.

How much data does it need? I retrained on smaller and bigger sets, and the error flattened out after three or four thousand shots.
The ring, unrolled
Training still took long enough that I wanted it shorter. Nearly everything useful sits in the ring of spots, about 38% of the pixels. So I unrolled the ring into a strip, radius one way and angle the other, and trained a smaller network on that. The input shrank by 80%, and training took less than half as long.

It cost accuracy, and the errors roughly doubled. I had unrolled around the center of the image, but pointing jitter shifts the whole pattern, so the strip scrambles where the spots sit relative to each other. Pointing and energy are read from exactly that layout, and a network as shallow as mine couldn't learn it back.
Back to the knobs
Xi's next question was whether a second network could go one step further back, from the beam at the sample to the machine settings that made it. The 2022 paper chains the two. The first, reading the detector, is my CNN. The second maps the six beam numbers to six settings, from where the laser lands on the cathode to the gun phase and the solenoid current. It trained on 10,000 of Xi's GPT runs.

A one-layer network did worst on the RF amplitude, probably because space charge makes that part of the map strongly nonlinear. I built a four-layer network instead, and it was better on all six. Two settings still missed the precision the machine would need. The gun-phase error was 3.7° against a target of 3°, and the RF amplitude fell short too.
Offline
Nothing here ran on the machine. The 2021 network was trained and tested on shots recorded in 2020, so "real time" in that paper is what the timing says it could do. The 2022 paper is simulation from start to end, and its plan for the real machine (four hours of data, ten minutes of training) is an estimate. Nor was a virtual diagnostic a new idea. Sanchez-Gonzalez and others did it for X-ray pulses at an FEL in 2017, and our first title even echoes theirs.
Numbers and figures are from Zhang et al., Sci. Rep. 11, 13890 (2021) and Sci. Rep. 12, 4240 (2022), both open access under CC BY 4.0; the figures are cropped.