A Contrast Calibration Protocol for X-ray Speckle Visibility Spectroscopy

: X-ray free electron lasers, with their ultrashort highly coherent pulses, opened up the opportunity of probing ultrafast nano- and atomic-scale dynamics in amorphous and disordered material systems via speckle visibility spectroscopy. However, the anticipated count rate in a typical experiment is usually low. Therefore, visibility needs to be extracted via photon statistics analysis, i.e., by estimating the probabilities of multiple photons per pixel events using pixelated detectors. Considering the realistic X-ray detector responses including charge cloud sharing between pixels, pixel readout noise, and gain non-uniformity, speckle visibility extraction relying on photon assignment algorithms are often computationally demanding and suffer from systematic errors. In this paper, we present a systematic study of the commonly-used algorithms by applying them to an experimental data set containing small-angle coherent scattering with visibility levels ranging from below 1% to ∼ 60%. We also propose a contrast calibration protocol and show that a computationally lightweight algorithm can be implemented for high-speed correlation evaluation.


Introduction
The advent of X-ray free electron laser facilities (FELs) opened up a new window of opportunity in extending X-ray photon correlation spectroscopy (XPCS) to the nano/atomic time scale with its unprecedented brightness, nearly full transverse coherence, and the femtosecond pulse duration [1][2][3][4][5]. Two-pulse XPCS was proposed nearly two decades ago as the corresponding technique at X-ray FELs to study ultrafast dynamics from the sample of interest by examining the speckle visibility of summed-speckle patterns as a function of the temporal separation of the two coherent femtosecond pulses [6]. In such measurements, speckle visibility, or contrast, directly relates to the intermediate scattering function of the sample [7], which is a fundamental quantity describing dynamic properties, and a key observable where theory and experiments can be compared at [8]. The methodology of accurate contrast determination therefore lies at the center of this technique. Experimentally, speckle patterns resulting from the scattering of the nano/atomic scale structures are often at the discrete photon limit, with a count rate typically less than 0.1 photons per pixel. As a result, contrast extraction relies on photon statistics and locating photons within detector pixels [9]. However, due to the imperfections in the X-ray detectors including charge sharing, readout noise, and pixel gain non-uniformity [10], the photon assignment algorithms are often biased, leading to systematic errors for contrast determination. Exact fitting based on the pixel readout accounting for the detailed detector parameters including the charge cloud shape and size is possible, but can be time-consuming and non-universal resulting from the large parameter space in order to mimic the detector performance.
In this paper, we present an experimental study of the commonly used contrast extraction algorithms. The sensitivity and precision of the algorithms were tested with a collection of experimental speckle patterns with different contrasts. We furthermore developed and tested a universal calibration routine utilizing the scattering recorded with unfocused-beam illumination (∼100 s of microns for typical X-ray FEL beamlines) as the zero-contrast baseline, and show that a computationally lightweight algorithm can realize accurate live contrast extraction during the experiment to provide crucial feedback.

Experimental Setup
The experiment was performed at the X-ray Pump Probe instrument [11] of the Linac Coherent Light Source [1]. The incident X-ray FEL pulses were monochromatized at 9.83 keV using a C * (111) monochromator [12]. We controlled the focused beam size at the sample plane by a combination use of slits and X-ray focusing optics, i.e., beryllium compound refractive lens (CRL) with a ∼3.85 m focal length. In the focusing geometry, the slits both upstream and downstream of the CRLs were used to define the numerical aperture of the beam entering the lens and clean up upstream small-angle scatterings. The slits moreover mitigated the decoherence effect from the monochromator diamond crystal surface miscut [12,13]. At the sample position, the beam profile was characterized using a high-resolution microscope (Nikon CFI S Plan Fluor 20×) imaging a 20 µm Ce:LuAG scintillator screen. With the upstream slit sizes set to 50 and 100 µm, the full-widths athalf-maximum (FWHM) intensity of the focal spots were measured to be 10 and 5 µm, respectively. The beam size can be enlarged by either translating the lens stack longitudinally to position the sample out of the focus or by removing the lens and using only the slits.
In total, we acquired speckle patterns under nine different scattering conditions: four were realized by varying CRL focusing conditions, four by using slits opening of different sizes, and finally one in the unfocused condition with slits fully open as a baseline measurement. For the four measurements with focused beams, the 5 and 10 µm conditions have been mentioned above, while the other two (18, 38 µm) were achieved by translating the lens stack so that the sample was 70 mm downstream and 230 mm upstream of the focal position, respectively. Silica nanospheres contained inside a quartz capillary with a nominal inner diameter of 1 mm were used as the scattering sample. An ePix100 detector (pixel size, 50 µm) [14,15] was placed L ≈ 9.5 m downstream of the sample to record the small-angle coherent scattering. The detector has a gain non-uniformity of around 5% which is software-corrected to be around 0.5%.
When adjusting the beam size at the sample location using the slits or focusing optics, the sample diffraction volume changes accordingly, leading to a change of speckle size at the detector plane. For example, displayed in Figure 1b,c are the speckle patterns in the two focusing conditions mentioned above, where the speckle size is visually shrunken when the focal spot size expands by a factor of 2. On the other hand, the detector pixel angular resolution was fixed, and this therefore led to a change in the contrast levels in the measured speckle patterns [16] as plotted in Figure 1d, where the oversampling ratio is defined as the speckle size (FWHM) normalized by the detector pixel size. Here, the theoretical calculation (gray curve) following Ref. [16] and the simulation (red dots) following Ref. [17] agree well with each other. It is also worth noting here that this plot only shows the contrast reduction factor from the oversampling ratio, whereas many other parameters including the beam coherence properties may also change when adjusting the beam sizes and contribute to further contrast changes.

Analysis of Contrast Extraction Algorithms
In this section, we will analyze three different photon assignment algorithms by comparing the extracted contrast of the scattering data sets collected in the nine configurations mentioned in the previous section. The three algorithms are labeled as: GG (Greedy Guess), LSF (Least Squares Fit) [9], and PPC (Psana Photon Convertor) [18,19]. Details regarding the three algorithms can be found in Ref. [17]. In Appendix A, we list the computation times of the three algorithms, clearly showing that the PPC and GG methods are much more computationally economical.

Contrast Extraction Procedure
First, we provide the general procedure of deducing contrasts from the photon probabilities of the scatterings given by the algorithms. In the low count rate regime (mean photon occupancyk 1), shot noise contributes to the fluctuations in the measured speckle intensity. The photon occupancy probabilities in discrete speckle patterns with mean count ratek and contrast β = 1/M (M is known as the mode number of a speckle pattern) follow a negative binomial distribution [20]: Here, P(k|M,k) denotes the probability of k photons per pixel events, which will be referred to as k-photon events in the context. The equation yields an iterative function Specifically for k = 1, we have For low-intensity speckle patterns, each frame is dominated by the Poisson noise. Contrast can only be estimated with a large number of speckle patterns. At the same time, instead of having a stable count rate, the monochromatized self-amplified spontaneous emitted (SASE) X-ray FEL pulses have shot-to-shot intensity fluctuations following the Gamma distribution. We therefore evaluated the intensity dependence of the algorithms by grouping speckle patterns with similar mean scattering intensities. For each intensity group, we calculated the averaged count rate as well as one-/two-photon probabilities, from which we deduced the speckle contrast following Equation (3). The error σ β for each value was estimated from counting statistics by: Here, N frame and N pixel denote the total number of frames and the number of pixels in the chosen ROI for each frame separately. To extract the overall effective contrast β e , a weighted average of the individual contrasts was performed with the weight, i.e.,k 2 N frame , which is inversely proportional to the variance of the contrast, in order to optimize the log likelihood of the measurement [21]. The error bar for β e is then estimated as the weighted standard deviation of the contrasts from these individual intensity groups.
Following this procedure, the contrast values extracted from the photon assignment of the three algorithms are displayed in Figure 2 for all nine measurement conditions. Within each intensity group, all three algorithms show sufficient sensitivity to distinguish the trend of contrast change, e.g., the speckles from 250 µm slit beam and beam with slits fully open with well below 1% level of contrast difference. However, the three algorithms gave distinct values for the same data set. This confirms our simulation studies [17] and suggests that further calibration is required.
One other important observation relates to realistic measurements using monochromatized SASE X-ray FEL pulses having shot-to-shot intensity fluctuations. The scattering in each measurement condition therefore consists of a broad distribution of count rates as shown in Figure 2a,c. Note here that droplet-based algorithms including LSF and GG have a precise count of photons, and they therefore share the same count rate distribution. On the other hand, the PPC method, as an algorithm based on rounding of pixel readout, has a tendency to underestimate the total number of photons. This led to the difference between their scattering intensity histograms. In the count rate range from 0.02 to 0.2 photons/pixel, the GG algorithm showed consistency despite the intensity fluctuations, with the contrasts from individual intensity groups following the horizontal dashed lines denoting the overall effective contrasts. On the other hand, PPC and LSF, respectively, give an increasing and decreasing trend with increasing count rates. This discrepancy combined with varying pulse intensity distributions will lead to additional systematic errors. For example, though the individual contrasts from 250 µm slit beam and beam with wide opened slits show a difference for the PPC method, the extracted overall contrasts β e cannot distinguish between the two conditions as displayed in Table 1. The reduced signal to noise ratio comes from their deviations in the count rate distribution as displayed in Figure 2a, while the weights for extracting the overall contrasts only consider photon counting statistics. To improve the sensitivity, contrasts need to be extracted from a subset of the data within a much narrower intensity range, which in turn reduces usable data fraction and increases the time needed to reach sufficient photon statistics.

Calibration with the Zero-Contrast Baseline
As proposed in Ref. [17], the biased contrasts β e extracted from the three algorithms can be corrected with a linear coefficient. The calibrated contrast β c is related to the extracted contrast baseline (β b ) at zero-contrast point with We will demonstrate in this section such a contrast calibration routine by utilizing the unfocused beam scattering as the baseline. The unfocused beam at XPP is approximately 300 µm, which translates to a speckle size of 3.6 µm at the detector and therefore an oversampling ratio of 0.07. It gives a contrast reduction factor ∼0.5%, which is well below the typical contrast values for XPCS measurements at FELs [9,22]. This leads to a small systematic error that can be neglected when working at higher contrast levels and the contrast reduction ranges for dynamics interpretation. In the context of this manuscript, we consider the scattering from unfocused beam as the zero-contrast calibration point. As listed in Table 1, it is apparent that none of the three algorithms returns a contrast value close to 0 for the fully open slit condition. We next show experimental confirmation of this theory by using a detector pixel mask to eliminate charge sharing.
As shown in our earlier work [17], charge sharing is the dominant contribution leading to inaccuracy in photon statistics, while the effects from other imperfections of the stateof-the-art hard X-ray detectors including pixel gain non-uniformity and readout noise are comparatively negligible. By masking the pixel boundaries with an aperture array, one can eliminate photon absorption at locations with position ambiguities. A tungsten foil with a thickness of 50 µm was chosen for the mask fabrication. It has a transmission of 9.1 × 10 −5 for the photon energy of 9.83 keV, creating sufficient absorption contrast to be considered a binary mask. Shown in Figure 3a is the schematic of the mask. It consists of a periodic laser-machined aperture array (nominal diameter: 35 µm, pitch: 100 µm, 25 × 25) that is commensurate with detector pixel array. When the pin holes are well aligned to the pixel centers, the photons landing on the detector will no longer have position ambiguities, e.g., only the center area of every other detector pixel can receive a nonzero reading, as the apertures lie well within the 60% charge contribution region marked as gray in Figure 3b. Therefore, by directly rounding the pixel readout, the number of photons falling into individual pixels can be uniquely and accurately determined.
The mask was positioned close to the front of the detector at a distance of 7 mm from the sensor surface. The pixel histogram of the masked region was very sensitive to the position of the mask. With a step size of 2 µm, a 2D scan allows accurate determination of the mask alignment both horizontally and vertically (see Appendix B for details). Plotted with yellow line in Figure 3e is the optimized pixel histogram. It deviates from the purple curve which is simulated by assuming a perfect mask with the nominal values for the apertures. This is a result of the mask fabrication tolerance, which was also confirmed by the SEM image as shown in Figure 3d. By varying the aperture size and assigning a Gaussian distributed position error for individual apertures, we were able to replicate the observation. Shown in Figure 3e in '+' or 'x' marker are histograms of simulated data considering aperture astigmatism. The histogram, however, is sensitive to both the averaged aperture size and their position errors. Small deviations from the identified values will result in very different shapes of the photon peaks in the histogram, which are plotted in light/dark green for comparison. We further estimated the systematic errors of contrast extraction when the detector mask is applied by simulating the detector signal using the identified realistic parameters for the detector mask. For the zero-contrast reference speckle patterns, the extracted photon probabilities from rounded pixel values yielded a contrast −0.007 ± 0.002. Here, the error bar was estimated from the photon statistics in the simulation. It suggests that the mask led to a ∼0.7% contrast underestimation. Shown in Figure 3c are the contrasts extracted from the unfocused scattering data set when the mask was applied, and it gave a contrast of −0.006 ± 0.005. Considering the systematic error from the imperfect detector mask, we arrive at a contrast level of 0.1%. This demonstrates that the contrast level extracted under the unfocused condition β b can be used as an effective calibration reference. It is, however, worth noting here that this procedure will introduce additional errors due to error propagation from both β e and β b . The nanopowder sample was considered static during the course of the measurement. Therefore, one can retrieve the absolute contrast levels for benchmarking the calibration protocol by evaluating the sequential cross-correlations. We performed intensity crosscorrelation of adjacent frames using the same ROI as the visibility analysis. To improve the signal to noise ratio, only frame pairs both having count rates above 0.2 photons per pixel [23] were used. For calculating the correlation, we set the pixel readout threshold at half a nominal photon readout, i.e., 85 ADU, in order to mitigate the bias arising from the pixel readout noise. Considering the intensity fluctuations and shot noise, the correlation was extracted by performing the weighted average of the cross-correlations [24]. The weighting factors were proportional to the intensity multiplication of the pulse pair, which were measured independently using the beamline I 0 right upstream of the sample. Their error bars were estimated using the weighted variance.
After normalizing the correlation from each condition to that of the unfocused beam, the final contrast values were compared against the calibrated GG result as shown in Figure 4, showing good general agreement. The contrast level spanned over two orders of magnitude for our eight data sets and covered the typical contrast levels for recent speckle visibility spectroscopy measurements at FELs. Note here that by calibrating using the unfocused beam, it will also automatically eliminate the effect of the slow intensity change in the chosen ROI, as shown in Figure 1a, due to the structure factor of the sample. A detailed derivation can be found in the supplementary material of Ref. [25]. Estimated from the summed unfocused beam scattering, this gave an additional ∼0.3% contrast.  While the general agreement is encouraging, the ∼1% level deviations were still larger than statistical uncertainties considering the sufficient number of frames, detector pixels and speckles taken into the calculation [26]. This suggests additional sources of systematic bias beyond both the calibration of the GG algorithm and the correlation analysis, e.g., the cross-correlation could be susceptible to the shot-to-shot beam spatial property fluctuations of the focused FEL pulses. Further investigation is needed to improve the accuracy of the speckle visibility measurement at FELs, and this certainly will benefit from a more stable FEL source with minimized intensity and spatial fluctuations adding to the error of the visibility extraction. Our result shows that Equation (4) is not the sole optimization criterion for an XPCS measurement at FELs. The absolute magnitude of contrast should also be taken into consideration in the experiment design. A low starting contrast ≤1% will make the measurement more susceptible to source and optics instabilities, which mostly likely requires a more rigorous analysis procedure to produce a meaningful result. Thanks to the nearly fully transverse coherent X-ray beams, the typical XPCS measurements at X-ray FELs have a high starting contrast (≥5%) [9,22,25,27,28]. The geometry of the scattering setup, i.e., detector to sample distance, generally relies on focused beams to achieve an oversampling ratio close to or bigger than 1, while the unfocused beam will lead to an oversampling ratio 1. These factors therefore allow the implementation of this calibration protocol based on unfocused beams and provide on-the-fly feedback to experiments.

Conclusions
In summary, we presented in the paper a calibration protocol for correcting the systematic errors in the contrast determination due to the realistic performance characteristics of X-ray detectors and the intrinsic bias of the photon assignment algorithms. Using a pinhole mask, we have verified the procedure. It is a simple scaling of the results from the dropletbased 'Greedy Guess' algorithm by using the value from an incoherent speckle pattern. At an X-ray FEL, this value can be obtained by measuring an unfocused beam. Among the three evaluated photon assignment algorithms, the 'Greedy Guess' method shows superior performance when FEL source intensity fluctuations are large. The calibration procedure corrects the contrasts of a given speckle data set with a 1% level accuracy.
While the data presented were recorded using the ePix100 detector at the LCLS, the concept and methodology is universal and generally applicable to X-ray detectors used for visibility spectroscopy and XPCS experiments at most currently operating FEL facilities. It can be potentially relevant for visibility spectroscopy measurements at synchrotrons as well. This lightweight procedure demonstrated here moreover suggests that optimizing the optical contrast level in the experimental design is of key significance to enable live feedback towards a successful XPCS measurement.

Data Availability Statement:
The data presented in this study are available on request from the corresponding author. Table A1. Averaged single-frame computation time consumed for the three algorithms to extract photon positions in the same contrast extraction ROI as in Figure 1a using a single core of Intel(R) Xeon(R) CPU E5-2620 v4 processor. The computation time average was performed over 1000 consecutive frames with the incident X-ray beam in the tight focus condition (in focus, 5 µm). The GG and LSF algorithms can be sped up by reprogramming in C.  Figure A1 shows that the mask alignment can be optimized by using the detector pixel histogram. We note here that the measurement was done in a different condition (lower incoming X-ray beam pulse energy) as compared with Figure 3f.