Introduction

The XTCAV (X-band Transverse deflecting mode CAVity) is a detector that is used to determine the x-ray power vs. time of each LCLS shot. It can also be used to give information about the relative energies and arrival times of x-ray pulses that are closely spaced in time (fs delays). Unfortunately, some data analysis is necessary to extract this "power profile" from the raw detector signal. This documentation will go through how to do this.

For most experiments, the analysis should be basically turn-key using the code we provide at LCLS. The "Quickstart" section should be enough if you just want to do that as fast as possible. Many experiments that rely heavily on the XTCAV, however, operate in unique electron-beam modes (multibunch, etc) that require more manual fine-tuning of the analysis. To support those cases, we go through a little of the theory behind the XTCAV & analysis routines used to extract the information of interest.

Some knowledge of the physical setup is necessary to understand any data processing results. The XTCAV works by sending the electron beam at the end of the LCLS (after the undulators, so after x-rays have been generated) through a transverse accelerator that "kicks" the electrons, causing the front (early) part of the beam to head one direction, and the back (late) to head the other way. Then, the beam is sent through a magnetic field, which bends it; electrons with high kinetic energy bend a little less than those with low KE. Here is a little schematic:

In this way, the electrons get sorted by both time and energy. The beam hits a fluorescent screen that is imaged by a camera – this image is the raw data we process. A typical XTCAV images looks like this, with "time" on the x-axis and "energy" on the y-axis:

You may notice there are vertical streaks in this image. Those streaks reflect the energy some electrons lost when they generated x-rays in the XFEL undulator! If we compare this image to a comparable image that did not generate x-rays ("lasing off" run, more on that later) then we can "reconstruct" the x-ray power as a function of time. For example, in the next figure, panel (a) shows a lasing off shot, and panel (b) shows a similar electron bunch that lased, a lasing on shot. Finally, panel (c) shows the power reconstruction produced by comparing these two shots.

Contents

Quickstart

All XTCAV analysis is a three step process involving "dark", "lasing off", and "lasing on" runs. The first two are references; the last is how we commonly refer to runs that contain data of interest. Simple scripts are provided as part of the psana python environment for processing each.

Dark Runs

Dark runs are used to simply make pedestals (aka "darks") for the XTCAV camera and other diagnostics. These runs are recorded without any electrons going through the accelerator. We take the average of a series of dark run images to get the general pixel values of the camera background. We can then subtract this "average image" from the lasing on and off runs to get a closer approximation of the actual energy of the electron bunches.

LCLS provides an "xtcavDark" script that automatically populates the experiment calib dir with dark images that are picked up in subsequent analysis. Just provide an experiment and run number, and optionally the maximum number of shots to process:

xtcavDark amox23616 104 --max_shots 400

Output

dark background reference
  Experiment: amox23616
  Run: 104
  Valid shots to process: 400
100.0 % done, 400 / 400
Maximum number of images processed


Lasing Off Runs

Lasing off runs provide an estimate of what the electron beam would have looked like if it did not lose any energy to generate x-rays. To provide this reference, a "lasing off" run is generated that has electons but no x-rays. The electrons are "kicked" so that they cannot generate x-rays (lase) as they travel through the undulator. These electron bunches are captured on the XTCAV camera. The standard analysis clusters together these lasing off images into an automatically or manually chosen number of groups. Each cluster of lasing off images is averaged to reduce noise, and then compared to "lasing on" bunches (next step, below). Clustering and subsequent matching to lasing on bunches is done by comparing the electron current as a function of time (projection of the XTCAV image onto the x-axis, the time axis), which is physically expected to be invariant, whether lasing occurred or not.

A script to generate lasing off references is provided with an interface very similar to the one for dark runs. It has quite a few options and should handle nearly all use cases.

NOTE: in this example only 200 shots are used, but generally larger numbers of shots are important here (typically >1000) so that the lasing-off analysis has enough statistics for the various "clusters" of lasing-off behavior.

NOTE: see this page for guidance on how to set lasing-off analysis parameters:  Old XTCAV Documentation#Lasing-offAnalysisParameters

xtcavLasingOff amox23616 131 --max_shots 200


Output

Lasing off reference
	 Experiment: amox23616
	 Runs: 131
	 Number of bunches: 1
	 Valid shots to process: 200
	 Dark reference run: None
100.0 % done, 200 / 200
Averaging lasing off profiles into  12  groups.


Lasing On Runs

Runs with data of interest are commonly referred to as "lasing on". For each shot, we choose an aggregated lasing off profile with the most similar electron current. That reference is used to compute the energy that was lost to x-rays. There are two methods by which one can perform that calculation (physical details below): one can either compare the "energy loss" directly – ie subtract the mean electron energy in the lasing off run from the lasing on run -- or look at the increased energy spread in lasing on run vs. lasing off. Both are motivated by FEL theory.

We provide a third simple script that can be used to test these methods:

xtcavLasingOn amox23616 137 --max_shots 5


Output

Using file 104-end.data for dark reference
Using file 131-end.data for lasing off reference
Agreement: 91.4276%; Maximum power: 6.64857; GW Pulse Delay: 18.512 
Agreement: 91.1052%; Maximum power: 5.82139; GW Pulse Delay: 20.6758 
Agreement: 93.136%; Maximum power: 4.88641; GW Pulse Delay: 18.0389 
Agreement: 94.1173%; Maximum power: 6.76523; GW Pulse Delay: 16.2321 
Agreement: 92.9344%; Maximum power: 7.14828; GW Pulse Delay: 11.2065 


For real analysis, however, you likely want to incorporate some python code into whatever analysis masterpiece you are composing.


psana
from xtcav2.LasingOnCharacterization import LasingOnCharacterization
XTCAVRetrieval = LasingOnCharacterization() 


for evt in ds.events():
	XTCAVRetrieval.processEvent(evt)
	t, power = XTCAVRetrieval.xRayPower()

An example script which generates some plots:

import psana
from xtcav2.LasingOnCharacterization import LasingOnCharacterization
import matplotlib.pyplot as plt
import sys

ds = psana.DataSource('exp=xppc00120:run='+sys.argv[1]+':smd')
 
XTCAVRetrieval = LasingOnCharacterization()
for evt in ds.events():
    XTCAVRetrieval.processEvent(evt)
    # method 1: center-of-mass
    t, powerCOM = XTCAVRetrieval.xRayPower(method='COM')
    if t is None: continue
    # method 2: RMS
    t, powerRMS = XTCAVRetrieval.xRayPower(method='RMS')
    agreement = XTCAVRetrieval.reconstructionAgreement()
    plt.title('agreement: %4.2f'%agreement)
    plt.xlabel('Time (fs)')
    plt.ylabel('Power (GW)')
    # a useful named-tuple with details of the shot analysis
    #res = XTCAVRetrieval.fullResults()

    # [0] index needed for potential multi-bunch analysis
    plt.plot(t[0],powerCOM[0,:],label='COM')
    plt.plot(t[0],powerRMS[0,:],label='RMS')
    plt.legend()
    plt.show()

Theory & Implementation

The current XTCAV code implements two reconstruction methods – that is, ways to compare the lasing on and lasing off XTCAV images to compute the final power-vs-time profile. These methods are:

  1. The "energy loss" method, also known as "COM" for center of mass, which is the way the method is actually implemented. This method computes the FEL power directly from the energy lost by the electrons, using a simple conservation of energy argument:

         P(t) = (<Eoff>(t) - <Eon>(t)) x I(t)

    Here, <Eoff>(t) is the average energy of the electrons in the lasing off shot, <Eon>(t) is the average energy in the on shot, and I(t) is the beam current. Practically, the current is just the projection of the XTCAV camera intensity on the time axis. The average energies are determined by taking the center of mass of the XTCAV image (hence, COM) in time – ie, just the average electron energy vs. time. The difference between the off and on shots (energy loss) gives the final power.

  2. The "energy spread" method, also known as "RMS" for root-mean-square method. This method computes the increased spread of the electron energies in the lasing on shot, which is predicted by FEL theory:

        P(t) = [ σ2on (t) - σ2off (t) ] x I2/3(t)

    You will notice that in the lasing on shots (e.g. in the early figures in this article), the electrons are more spread out after lasing. This lets us compute the power they lost to lasing through the associated FEL physics model. The RMS, denoted σ here, is simply the RMS deviation of the electron energies at each point in time. The 2/3 exponent on the beam current is a small correction due to these formulas scaling with the FEL efficiency (Pierce) parameter ρ which has a 1/3-power dependence on several slice properties of the beam. Beam current is one of them and is known by this measurement.
     
In practice, the methods used to compute the reconstruction are quite simple. The XTCAV code has a number of image processing steps used to clean up the data before processing that are the truly tricky bit. For details, see the code on github: https://github.com/slaclab/xtcav2
For details on these equations, the accelerator directorate has published an informal note on the theory and operating parameters of the XTCAV: xtcav-users-v0p4.pdf – recommended reading.

Differences from Previous XTCAV code

In June 2018, we pushed an update to the XTCAV code that is referred to at LCLS as "xtcav2". Yes, I know what you are thinking: Chris O'Grady and TJ Lane truly excel in the creative naming department and deserve a raise as a result. In many ways, the user interface should look the same, but there were a lot of improvements under the hood. Many thanks to our talented masters student Clara Meister for tackling that beast!

  • Changes to the user interface:
    • All parameters should be set during the initialization of `DarkBackgroundReference`, `LasingOffReference`, and `LasingOnCharacterization`.
    • There is no longer a `.Generate()` call. A reference will be created upon initialization of the `DarkBackgroundReference` and `LasingOffReference` classes. The reference will be saved to the psana data directory (default location unless the `calibration_path` parameter is set). If you would like to access the generated reference profiles, you can either load them from the saved file or access the member variable `averaged_profiles` within the `LasingOffReference` class.
    • Some parameters, such as roi_waist_threshold, have been deprecated. The current code automatically chooses this value for you based on the `snr_filter` (see documentation above). Other parameter names have changed so that naming convention is consistent across the entire xtcav package (again, see documentation above). 
  • Changes to analysis:
    • Improved profile clustering algorithm
    • Image is no longer median filtered before analysis
    • Power profile for lasing off image is not averaged between RMS and COM methods
  • Other changes include: improved code readability, integration with psana detector interface, and speed up in processing time.

Important Questions (Both Frequently and Infrequently Asked!)

What is the resolution of the XTCAV reconstruction?

From Tim Maxwell:

Time resolution is around 1.1 fs RMS for soft x-rays, 2.5 fs fwhm for hard x-rays. This doesn't include the "slippage resolution." That is, when using the full undulator, then by the end the x-rays can have slipped out of the electron slice by ~3 fs for soft x-rays. Obviously not a small number if trying to make (eg) 5 fs pulses. They've (ACR) been advised to not use the full undulator when shorter pulses are more important than number of photons.

How Do I Know When I Can Trust My Results?

GREAT QUESTION. The first, most obvious thing to do is compare the RMS ("energy spread") and COM ("energy loss") reconstruction methods. If these agree (90+% correlation), then you are on a good track... though other validation is desirable. If they disagree, however, you know you are hosed. For now, given what we know, we recommend throwing away any shot for which the two methods do not agree, unless you have some good reason to do otherwise.

If you want to know gross values, such as pulse duration, the timing between two pulses, etc, then if you have reconstruction agreement, you can be quite confident your analysis will work with ~3 fs precision in time and ~10 eV precision in energy, though exact values depend strongly on the XTCAV settings – discuss with ACR if you care! If you are studying the power structure of the pulse, then the honest answer is that we do not know exactly how good the reconstruction is – this is an open research question. We are currently working on ways to experimentally validate existing reconstruction methods and improve them. This is seen as a high priority by LCLS management and is funded through the FEL R&D program (as of time of writing, Oct 2018).

I see a "phase error".  What do I do?

This warning can cause events to be thrown away, impacting analysis:

The phase of the bunch with the RF field is far from 0 or 180 degrees

To disable this throwing away of events, set an environment variable:

export XTCAV_IGNORE_PHASE_WARNING=1

When this happens ACR should also be notified so they can adjust the phase on their end.

Tim Maxwell wrote about this issue on Dec. 11, 2022:

"I also see that the operating phase is indeed getting close to 0. I suspect the wording of the error message itself just had it the wrong way around. Seems to be the phase shifted after changing to low charge. We can also walk back the offset so that it no longer throws the error. (The physical phase is still actually +90 currently, so this is okay.)

You could make it only a warning if the phase is approaching 0 or 180. In this case, the risk is the time axis may be reversed. At some point it could be noted to ACR to please adjust the phase offset for the cavity. 

In the case of lasing off references that may mean contaminating reconstructions if it's ambiguous though. Not noticing the warning, potentially getting it wrong, then matching all later events to a backwards image. 

(Or the risk that the cavity would become unstable. But ACR would certainly notice this and fix it.)

Thinking of how we can just do this better on our side as well to not be an issue."

Who Do I Contact for More Help?

On XTCAV physics, parameters, limitations, improvements, etc: Tim Maxwell <tmaxwell@slac.stanford.edu>

On the workings of the XTCAV code in psana: Chris O'Grady <cpo@slac.stanford.edu>

Additional Resources



  • No labels