Comparative study of nonideal beam effects in high gain harmonic generation and self-seeded free electron lasers

In this paper we investigate and compare the properties of two narrow-bandwidth free-electron laser (FEL) schemes, one using self-seeding and the other high gain harmonic generation (HGHG). The two systems have been thoroughly studied analytically and numerically in the past. The aim of this work is to compare their performances when the FEL is driven by an electron beam with nonideal properties, thus including effects such as shot-to-shot energy ﬂuctuations and nonlinear energy chirp. In both cases nonlinearities produce a bandwidth larger than the Fourier transform limited value. However, our analysis indicates that, for approximately the same output power levels, the self-seeding scheme is less affected than the HGHG scheme by quadratic energy chirps in the electron beam longitudinal phase space. This is conﬁrmed by a speciﬁc numerical example corresponding to SPARX parameters where the electron beam was optimized to minimize the FEL gain length. The work has been carried out with the aid of the time dependent FEL codes GENESIS 1.3 (3D) and PERSEO (1D).


I. INTRODUCTION
The unique characteristic of a free-electron laser (FEL), with respect to other sources of electromagnetic radiation based on the emission from relativistic electrons, is the large number of photons in the coherent volume. For SASE FELs this number is typically of the order of 10 9 , compared with less than 1 for spontaneous radiation sources. However, while a SASE source is practically diffraction limited [1], its bandwidth is determined by the FEL cooperation length [2]. This means that the bandwidth is larger than the Fourier transform limit, except for the case when the electron bunch is short with respect to the cooperation length, and only a single spike is present in the radiation pulse.
When the electron bunch is longer than the cooperation length, and many spikes are present in the output pulse, the number of photons in a coherent volume could be further increased by reducing the bandwidth to the transform limit or near to it. Several schemes have been proposed to achieve this goal and control the bandwidth, making it narrower than that achievable with SASE.
The self-seeded FEL consists of two undulators separated by a monochromator and a magnetic chicane. The FEL process in the first undulator is started by shot noise and is interrupted well before saturation. While the SASE radiation is sent through a monochromator, the electron beam passes through a magnetic chicane which destroys the microbunching introduced by the SASE and compensates the delay introduced by the monochromator. The monochromatic radiation and the demodulated electron beam are then sent through the second undulator for a seeded FEL process reaching saturation. The second undulator operates as a seeded FEL with the seed signal at the fundamental resonant frequency. Note that the self-seeded FEL does not rely on an external radiation source to seed 1 The acronym HHG must not be confused with HGHG. HHG stands for higher harmonic generation and refers to the harmonic generation of a high intensity pump laser in a gas. HHG sources are the coherent radiation sources typically considered for seeding FELs at short wavelengths such as those considered in this paper. the FEL process and it can be scaled, in principle, to any arbitrary wavelength.
In the HGHG, on the other hand, the FEL is seeded with a subharmonic of the output wavelength. The frequency up-conversion process is deeply rooted in the FEL amplification and electron beam bunching process and may be exploited to extend the wavelength operating range of externally seeded FELs [10]. The FEL in the HGHG case is composed of two undulators separated by a magnetic chicane. The first undulator, called modulator, is seeded by an external coherent source. The FEL interaction in the first undulator introduces an energy modulation in the electron beam. The dispersive section transforms the energy modulation in a density modulation on higher harmonics of the seed wavelength. The second undulator, called radiator, is tuned to one of these harmonics. The bunching factor generated by the dispersive section triggers the FEL process in the second undulator.
We evaluate the minimum achievable bandwidth that can be reached in the two cases, and compare it with the transform limit bandwidth. The calculation is performed first using an ideal electron beam and ideal external seeding laser. We define the ideal beam and seeding laser as having the following characteristics: (i) The electron energy distribution is flat along the electron bunch, with a small spread around the central value. The central value is also assumed to be constant from pulse to pulse, or, in other words, having zero fluctuations. (ii) We assume that the electron bunch has no coupling between the transverse and longitudinal phase space. (iii) The seeding laser central wavelength has no fluctuations and there is no chirp in wavelength along the pulse. We also assume zero phase noise for the laser pulse, and that the laser bandwidth is equal to the expected Fourier transform limit.
We later study the effect of deviations from the ideal case for both the self-seeding and HGHG schemes. We will show that these deviations make the FEL bandwidth larger than the transform limit, and produce large pulse-to-pulse intensity fluctuations. The results of these realistic beam studies also show that the photon pulses produced in the self-seeding and HHG seeding schemes have similar characteristics, with the self-seeding having a lower sensitivity to the beam deviation from the ideal case. We note that for the HGHG configuration we consider only the simplest HGHG system, with one frequency multiplication stage. Systems with several multiplication stages, with more undulators tuned at the intermediate frequencies and more chicanes, are more sensitive to nonideal beam effects.
This study points out the importance of evaluating the FEL photon pulse characteristics using a realistic beam model. In our case after introducing and studying the effects of the deviations from the ideal case, we do a more complete study using an electron beam phase-space distribution obtained from a full start-to-end simulation, which includes most of the known relevant physical phenomena.
The beam considered in our start-to-end simulations has not been optimized for HGHG or self-seeded operation. However, the scope of this paper is not to discuss optimization methods for the production of electron beams suitable for narrow-bandwidth FEL operation but rather that of describing some nonideal effects that are particularly relevant for the production of narrow-bandwidth radiation.

II. THE IDEAL ELECTRON BEAM AND LASER CASE
A. Electron beam and undulator parameters The comparison between HGHG and self-seeding is carried out using the electron beam parameters of the 1 nC, 1.5 GeV working point of the SPARX FEL [11], shown in Table I. The ideal FEL characteristics are given in Table II.
We operate the FEL at a wavelength of 6 nm; for the HGHG case we assume a 30 nm seed with 5th harmonic conversion. For the self-seeded scheme both undulators have a 2.8 cm period, while in the HGHG case the period is 4.2 cm for the modulator and 2.8 cm for the radiator. For a 55 m rms bunch length the transform limited relative bandwidth at the chosen wavelength is 2 Â 10 À5 full width at half maximum (FWHM).

B. Results in the ideal case
For the self-seeding option (see Fig. 1), we assume a monochromator with a FWHM bandwidth of 3 Â 10 À5 and 20% transmissivity. The R 56 element of the dispersive section must be larger than about 50 m to completely demodulate the FEL induced bunching at the exit of the first undulator.
Since the emission of SASE radiation is a stochastic process [2], the radiation after the monochromator suffers from intrinsic statistical fluctuations. To fully describe the FEL process, the results of simulations have to be averaged over several independent runs.
The length of the first undulator is chosen so that the average power after the monochromator is at least 2 orders of magnitude larger than the equivalent shot-noise power [12]. A choice of 410 periods (L 1 ¼ 11:5 m) for the first undulator gives an average peak power after the monochromator of 60 kW, well above the shot-noise level (roughly 300 W). Saturation in the second undulator is reached in L sat ¼ 13 m, giving a total undulator length of L tot ¼ 24:5 m. The results of 1D time dependent simulations with PERSEO [13] are shown in Figs. 2, 4, and 5.
The saturation energy is % 1:4 mJ, in a full width at half maximum (FWHM) bandwidth of Á= FWHM ¼ 4:2 Â 10 À5 . The intensity fluctuations at the second undulator entrance are close to 100% but decrease as the FEL amplifier approaches the nonlinear regime until reaching a value of 15% at saturation.
In the HGHG case (see Fig. 3), the seed source has a central wavelength of 30 nm. To tune the modulator to such wavelength, the undulator period is 4.2 cm and the undulator parameter is K ¼ 4:41. We assume a seed power of 100 kW, which provides an energy modulation amplitude of 4 p after 130 undulator periods (L mod ¼ 3:6 m). The optimum value for the dispersive section strength is R 56 ¼ 10 À5 m, which gives a fifth harmonic bunching factor of b 5 ¼ 0:1.
The coherence of the output signal is determined by the coherence length of the seed. In the ideal case we assume a Fourier transform limited pulse with a length equal to the electron pulse for the HHG seed. The Fourier limit of a pulse at 6 nm (the output wavelength) with rms length equal to the electron beam leads to a relative FWHM spectral width of 2 Â 10 À5 . This is an ''equivalent'' seed spectral width and is the minimum spectral width achievable with a seed pulse of this length (the actual seed relative bandwidth is 5 times broader since the seed is tuned to the 5th subharmonic of the final wavelength and Á= % =L coh , with L coh equal to the coherence length).
Saturation in the radiator is reached in L sat ¼ 8 m, resulting in a total undulator length of L tot ¼ 11:6 m. The saturation energy is % 1 mJ over a relative FWHM bandwidth of Á= FWHM ¼ 6:8 Â 10 À5 (see Figs. 4 and 5).

C. Comparison in the ideal case
In this ideal example the performances of the two schemes are almost equivalent in terms of bandwidth. The HGHG scheme has a slightly larger spectral width but it is not affected by intrinsic shot-to-shot fluctuations and reaches saturation in a shorter undulator.
A source of spectral broadening in this idealized case is represented by the frequency chirp introduced by the Gaussian dependence of the current. The longitudinal dependence of the current introduces a correlation between the imaginary part of the FEL gain and position, which results in a nonlinear phase modulation (i.e. a frequency modulation) of the output pulse. An estimate of this effect can be given in the frame of the 1D model of the FEL [14]. If we neglect the effect of slippage, the phase of the electric field is given by where s is the position along the beam, p ¼ À 0 0 is the relative energy offset ( 0 is the resonant energy), k w ¼ 2 w is the undulator wave number ( w is the undulator period), Ã is the unstable root of the usual FEL cubic equation, and is the 1D FEL parameter [14]. Taking the derivative of both sides of Eq. (1) with respect to s and recalling that / I 1=3 , we obtain We recall that Eq. (1) describes the phase of the complex phasor representing the electric field. Since the FEL radiation is a narrow-bandwidth signal, we have dc ðsÞ ds ¼ À 2ðsÞ 2 , where ðsÞ is the local wavelength shift. For a Gaussian beam IðsÞ ¼ I 0 exp½À 1 2 ðs 2 = 2 s Þ which gives a total frequency modulation amplitude of where N w is the number of undulator periods. In the selfseeding case this chirp is introduced only in the second undulator, since the bandwidth of the FEL pulse at the second undulator entrance is determined by the monochromator bandwidth. In the HGHG scheme contributions from both the undulators are added, since any phase chirp on the radiation pulse is transferred to the energy modulation and thus to the harmonic bunching. Note that the frequency modulation amplitude (normalized to the central wavelength) is proportional to the radiation wavelength, which means that the contributions from the modulator are usually bigger than those from the radiator (this is because the induced frequency chirp is multiplied by the harmonic factor n in the harmonic conversion process). As a result, the bandwidth of the HGHG scheme is increased with respect to the input seed equivalent width and is larger than that obtained with the self-seeded scheme.
In the next section we analyze two of the effects associated to a real electron beam, i.e., a nonlinear energy chirp in the longitudinal phase space distribution and shot-toshot energy fluctuations.

III. EFFECT OF NONLINEAR ENERGY CHIRP
A nonlinear energy chirp in the electron beam is responsible for spectral broadening in the radiation pulse. In both schemes spectral broadening is due to the imaginary part of the gain varying with energy along the bunch and resulting in a nonlinear phase chirp in the radiation pulse. Using the same approach as in Sec. II, following the one-dimensional model of the FEL and neglecting the effect of slippage, we obtain the following expression for the phase of the electric field [14]: With respect to the discussion in the previous section, we now allow the beam energy to have a dependence on s.
Taking the derivative of Eq. (4) with respect to s and linearizing <fÃðpÞg, we can express the local frequency offset as In a quadratically chirped beam dp ds varies with position, resulting in a frequency modulated radiation pulse with a broader bandwidth than the transform limited case (note that we have neglected the frequency chirp induced by the current profile). Higher-order terms in the Taylor expansion of <fÃðpÞg give rise to a nonlinear phase chirp even with a linearly chirped electron beam. However, higherorder terms have been neglected because, to second order, the effect of the imaginary part of the unstable root becomes dominant. HGHG suffers from an additional broadening effect due to the quadratically chirped beam passing through a dispersive section: compression in the dispersive section locally shortens the beam by an amount that is proportional to the derivative of energy with respect to s. Since the number of periods of the energy modulation is constant, the bunching frequency is locally offset by an amount equal to [15,16] ðsÞ ¼ R 56 dp ds : It is worth noting that the 2N w dp ds term in Eq. (5) contains the R 56 matrix element of the undulator (2N w ), and accounts for the same physical effect occurring in the dispersive section (local frequency offset due to compression), while the remaining term is due to the FEL gain.
In the self-seeded scheme, the starting bandwidth in the second undulator is set by the monochromator and the frequency chirp is introduced only in the second undulator. On the other hand, in the HGHG scheme there is a contribution from both undulators, since the nonlinear phase chirp introduced in the modulator is transferred to the energy modulation and is multiplied by the harmonic conversion factor n during the up-conversion process. The local frequency offset for the HGHG scheme is then where N mod and N rad are, respectively, the number of periods of the modulator and the radiator.
To give an idea of the importance of the different terms in Eqs. (5) and (7) we recall a fundamental result of HGHG theory (see [5][6][7]). The optimum value of R 56 for maximum harmonic bunching in an HGHG is given by where Á p is the relative energy modulation amplitude. In order to prevent the energy spread effect from inhibiting gain in the radiator, the energy modulation amplitude has to satisfy the following condition: Á p ( which, combined with (8), gives To compare the magnitude of this term with respect to the term 4 3 N w dp ds , recall that in a SASE FEL the number of undulator periods needed to reach saturation is of order 1=, thus in a seeded FEL (for both self-seeding or HGHG) which is significantly smaller than the expression in Eq. (9). Finally, the term n 4 3 N mod is typically of the same order of magnitude as 4 3 N rad since the smaller number of periods usually needed for the modulator is compensated by the factor n. However, this term becomes significantly bigger if several HGHG stages are cascaded and n is further increased.
From these considerations follows that, under typical experimental conditions, for the same beam parameters, the HGHG configuration will suffer more from the effects of nonlinear longitudinal phase space than the self-seeding.
Inserting the parameters of Sec. II in Eqs. (5) and (7), we obtain ðsÞ HGHG % 5 Â ðsÞ self-seeding : (11) Figure 6 shows the FWHM bandwidth as a function of the amplitude of the quadratic energy chirp for the schemes described in Sec. II (the spectra are calculated with the PERSEO FEL code). In the example considered, the spectral broadening is almost 5 times bigger for the HGHG scheme, a result that is consistent with the analytical estimate. The consequence of this sensitivity is that particular care has to be given to the optimization of the electron beam longitudinal phase space for seeded operation [17].

IV. EFFECT OF BEAM ENERGY FLUCTUATIONS
The effect of beam shot-to-shot energy fluctuations has been investigated with time dependent simulations with PERSEO. Figure 7 shows the pulse energy at saturation (at a fixed point along the undulator) as a function of the beam detuning. The performances of the two schemes with respect to beam energy fluctuations are comparable. This is due to the fact that the only difference between the two schemes is the trigger of the FEL instability. The radiation power produced by an FEL amplifier in the saturation regime is largely insensitive to the fluctuations of the starting value of the field intensity (see Fig. 2). The intensity fluctuations at saturation, thus, depend only on the fluctuations of the saturation power with the beam energy and are comparable in the two schemes.
In both cases, with the parameters of Sec. II and assuming that the beam energy at the undulator entrance fluctuates by 0.1%, the resulting intensity fluctuations are 50%.

V. START-TO-END SIMULATIONS
The performances of the two schemes have been investigated using a start-to-end simulation for the SPARX FEL to evaluate the effect of nonideal beam characteristics. This electron beam was optimized to minimize the gain length and maximize the output power for SASE operation and presents a strong nonlinear energy chirp. The results at the 1.5 GeV, 1 nC working point [11] are shown in Fig. 8.
In the self-seeded scheme the first undulator is made of seven sections of 75 periods. Assuming a 20% efficiency and 3 Â 10 À5 bandwidth for the monochromator, the average radiation power at the second undulator entrance is 60 kW. The magnetic chicane has been designed to have an R 56 of 100 m and a bending angle of 50 mrad, giving an offset from the axis of 30 cm.
The simulations have been carried out with GENESIS 1.3 [18] and ELEGANT [19] (see Appendix B for an overview of the simulation techniques). The results of the start-to-end simulations are reported in Figs. 9, 11, and 12.
The FEL saturates in the second undulator in L sat ¼ 20:5 m, giving a total undulator length of approximately L w;tot ¼ 35:2 m. Including the magnetic chicane (L mag ¼ 16 m), the total length of the self-seeded system is L tot ¼ 51:2 m. The saturation energy is % 0:9 mJ in a FWHM bandwidth Á FWHM ¼ 5 Â 10 À5 . For the HGHG case, the parameters of the seed source are reported on Table III. In the seeded case the modelization of the source should be included in the start-to-end simulation. The higher-order harmonics result from the strong nonlinear polarization induced on rare gases atoms, such as Ar, Xe, Ne, and He, by the focused intense elec-   tromagnetic field of a pump laser, typically a Ti:Sa laser with an energy of few tens of mJ. The emitted pulse has a length of few tens of femtoseconds and is composed by a sequence of short bursts separated by one-half of the fundamental laser period (400 nm) and the spectrum contains the odd harmonics of the original laser. The FEL amplification of harmonics generated in gas has been studied in literature [20,21]. The pulse length increases up to saturation and the fast time structure of the seed is filtered by the FEL gain bandwidth. The typical pulse length optimizing the efficiency in the frequency upconversion process is too short to significantly improve the linewidth of the FEL. For this reason we assume that the seed is filtered in a monochromator before injection in the FEL amplifier. With this assumption it is reasonable to use a Fourier limited pulse with a reduced linewidth and a reduced pulse energy [22]. The radiator has an undulator period of 2.8 cm and is composed of sections of 75 periods. The modulator is made of three sections of 55 periods each, with period of 4.2 cm (L mod ¼ 7 m). With this setup we obtain an 8% harmonic bunching factor at the radiator entrance. The results of the start-to-end simulations with GENESIS 1.3 are reported in Figs. 10-12. The FEL saturates in the radiator in L sat ¼ 10:5 m, giving a total length of L tot ¼ 17:5 m. The saturation energy is % 400 J in a FWHM bandwidth of Á FWHM ¼ 5:8 Â 10 À4 . Note that the bandwidth is almost 6 times bigger than the Fourier transform limit (with a seed bandwidth of 5 Â 10 À4 , the transform limited output bandwidth for the fifth harmonic would be 10 À4 ).
The double spike structure exhibited by the HGHG spectrum (see Fig. 12) is caused by the abrupt change in the slope of the energy profile of the beam. While a smooth parabolic profile would give a single broadband spike, a sudden change in slope can result in the splitting of the spectrum in two spikes.
The pulse energy at saturation is higher in the selfseeding case. This is because the HHG seed pulse is sig-nificantly shorter than the electron beam, while the monochromatized SASE pulse for the self-seeded scheme has approximately the same length.
The electron beam longitudinal phase space is very nonlinear (see Fig. 8), resulting in a significant spectral broadening in both cases (see the strong quadratic dependence of the field phase in Figs. 9 and 10). As predicted in Sec. IV, the broadening effect is much bigger in the HGHG scheme and the FWHM bandwidth is 10 times bigger than the self-seeding scheme.   It could be argued that the broader bandwidth of the HGHG pulse is caused by the shorter duration of the pulse. However, the HGHG pulse is only shorter by a factor 3, which would account for an increase in bandwidth of 3 with respect to the self-seeding. The much larger bandwidth observed in simulations suggests that the line broadening is mainly due to the nonlinearity of the phase space.

VI. CONCLUSIONS
We have addressed specific problems related to narrowing the FEL bandwidth with self-seeding and with HGHG with external seeding with high order harmonics generated in gas. It should be noted that for both schemes narrowing the bandwidth and increasing the brightness can be only one of the goals. They are also being studied and considered for other purposes, as ultrashort pulse generation (self-seeding, see [23]), reducing the saturation length of the FEL amplifier (HGHG) or allowing a few fs synchronization of the FEL with an external laser system for pump and probe experiments (HGHG see [24]).
In the ideal case the self-seeding and HGHG schemes produce a very similar bandwidth whereas in the start-toend case the results are significantly different. The simulations carried out in the start-to-end case using a realistic electron beam phase space optimized for SPARX SASE operation and state of the art seed parameters show that the performances of the self-seeded scheme exceed those of HGHG by a factor 25 in terms of spectral power density, due to the spectral broadening associated to nonlinear longitudinal phase space and to the short duration of the seed pulse.
As mentioned in the Introduction, the electron distribution used in the start-to-end simulation has not been optimized for HGHG or self-seeded operation. The electron beam energy chirp may be reduced by operating with lower compression and by paying special attention to the optimization of the longitudinal phase space. However, the scope of this work is not that of studying optimization methods for narrow-bandwidth FELs but rather that of comparatively describing the behavior of these schemes with respect to nonideal beam conditions and pointing out the extent to which such optimizations are needed in either scheme.
Similarly, the limitations on the seed pulse duration can be overcome by an increased pulse energy and a tighter monochromatization prior to injection. Progress in the HHG sources brightness in the wavelength range considered in this paper would provide significant improvement in the implementation of such sources in seeding FELs.
In conclusion, the performances of the two schemes have been analyzed and compared taking into account the main nonideal effects that affect narrow-bandwidth FEL operation. Both schemes are equally sensitive to beam energy fluctuations. The HGHG scheme, however, is more sensitive to quadratic chirp in the beam energy profile for a specific set of parameters corresponding to the 1 nC, 1.5 GeV SPARX electron beam, in which the electron beam was optimized for peak gain (and thus shortest undulator length). It is quite possible that both reoptimizing the electron beam to reduce the chirp and increasing the external laser seed power to reduce the required R56 dispersion strength could significantly lower the output bandwidth for the HGHG scheme, albeit at a price of reduced output power. Furthermore, this work emphasizes the importance of start-to-end simulations as a tool for reliably predicting the performances of narrow-bandwidth FELs.

ACKNOWLEDGMENTS
The authors would like to acknowledge Dr. A. Zholents and Dr. E. Hemsing for useful discussions and suggestions. The authors are also grateful to Dr. C. Vaccarezza and the SPARX project team for sharing the results of the start-toend simulations. Finally, the authors wish to thank L. Poletto for many useful discussions on short pulse monochromatization and monochromator efficiency. This work was supported by grants from Department of Energy Basic Energy Science Contract No. DOE BES 4-444025-RS-21830.

APPENDIX A: THE SATURATION REGIME
Sideband instability in the saturation regime is a wellknown phenomenon that affects both seeded and SASE FELs. When the FEL is driven by a quadratically chirped electron beam, however, the spectral structure undergoes significant changes in the saturation regime that are solely due to the longitudinal phase space curvature.
The FEL physics in the saturation regime is highly nonlinear and an analytical description of it is not possible, but we will try here to give an intuitive explanation of the phenomena involved.
It is well understood that, if the electron beam is injected above resonance, the FEL saturates at higher power than the case of perfectly resonant FEL interaction [12]. If the beam has a convex longitudinal phase space, the head and the tail of the beam saturate at higher power than the core of the beam. Since the beam has a quadratic energy chirp, the head and the tail of the beam emit at different frequencies. The two effects combined cause the growth of sidebands in the spectrum, corresponding to the frequencies of the head and tail of the radiation pulse.
This effect is extremely important in self-seeded FELs since operation in deep saturation guarantees the attenuation of the intrinsic intensity fluctuations. Figure 13 reports the spectrum of the self-seeded scheme at different points in the saturation regime showing the development of sidebands.
A comparison of the data in Figs. 13 and 14 suggests that maximum suppression of intensity fluctuations cannot be reached without allowing sideband growth. However, a satisfactory level of intensity fluctuations is reached already at z ¼ 20:5 m with a low power level in the sidebands.
In general, optimal design and operation of self-seeded FELs requires a balance between the amount of intensity fluctuations acceptable and the level of power allowed in the sidebands.

APPENDIX B: SIMULATION TECHNIQUES FOR SELF-SEEDED FELS
The simulation of self-seeded FELs is a complex task since it involves tracking the particle beam through the magnetic elements between the two undulators as well as the simulation of the monochromatization process of the radiation pulse.
As a first approximation, one could neglect any collective effect in the chicane and model the effect of the magnetic bypass through a transport matrix. However, this method is seriously flawed since the demodulation of the microbunches through a strong R 56 results in a particle distribution that has the wrong shot-noise statistics, with an rms bunching factor of % 1= ffiffiffiffiffiffiffiffiffiffiffiffiffi N macro p with N macro equal to the number of macroparticles in each slice of the beam (usually defined to be of the same length as the radiation wavelength). In practice, the effect of quiet loading is lost when the longitudinal position of the particles is remixed by the longitudinal dispersion. Thus, a different approach is needed to produce the right shot-noise statistics.
One possibility is to regenerate the longitudinal position of the macroparticles with a proper loading algorithm that produces the right shot-noise statistics, while keeping the same slice energy spread. This method was used for the 1D simulations with PERSEO. The main drawback of this technique is that any information on the phase of the electrons is lost when reloading the particle position. In practice, the effect of residual bunching from the first undulator is neglected and it is assumed that the only contribution to the beam microbunching comes from the shot noise. This assumption is reasonable as long as the R 56 of the magnetic chicane between the two undulators is high enough to suppress the density modulation induced in the first undulator (see Appendix C).
In the case of 3D simulations, to properly include the effect of beam warming due to the FEL process in the first undulator and the effect of coherent synchrotron radiation (CSR) in the magnetic chicane, the FEL code particle output has to be matched to a particle tracking code. This is rather complicated because the simulation of the FEL process usually requires a very big number of particles to achieve a satisfactory resolution in the electron distribution to resolve the motion on the scale of a radiation wavelength. Furthermore, several FEL codes (such as PERSEO and GENESIS 1.3) use a fixed number of particles for each slice of the beam and keep track of the current profile separately. On the other hand, particle tracking codes usually require less particles but use a number of particles that is proportional to the local density of the beam.
The start-to-end simulations presented in this paper have been performed matching the FEL code GENESIS 1.3 to the particle tracking code ELEGANT (the 1D CSR model for bends and drifts embedded in ELEGANT was used in the simulations). The following procedure was implemented with a C program (gen2ele): the current profile is extracted from the GENESIS 1.3 output. The particle output of GENESIS 1.3 (.dpa file) is then undersampled extracting from each slice a number of particles proportional to the slice current. Finally, the particles are printed on an elegant compatible file. The same procedure could be used to match the GENESIS 1.3 output to any particle tracking code. This method has the same drawback as the previous one, since  any information on the phase of the electrons is lost during the undersampling process and the shot noise has to be regenerated when the beam is used in a new GENESIS 1.3 run for the second undulator. The monochromator has been simulated in a very simple way: the output radiation file from the first undulator simulation is Fourier analyzed in the z direction filtering and losses are applied and the spectrum is then antitransformed. For the 3D simulations, the spectral analysis was carried out with the aid of the FFTW3 fast Fourier transform library [25].

APPENDIX C: THE MAGNETIC CHICANE FOR THE SELF-SEEDED SCHEME
The design of the magnetic chicane for the self-seeded scheme has to take into account several constraints. The deflection angles for the electron beam have to be small in order to avoid the degradation of the beam quality due to CSR. The R 56 element of the chicane's transport matrix has to be big enough in order to effectively demodulate the electron beam after the SASE FEL process in the first undulator. To address this issue in a quantitative way, we recall the following expression for the bunching factor of an energy modulated electron beam passing through a dispersive section (see, for example, [7] where k rad is the radiation wave number, p is the relative slice energy spread, and Áp is the energy modulation amplitude normalized to the resonant energy. With the parameters of the SPARX electron beam in the 1 nC working point ( p % 2 Â 10 À4 ), operating the FEL at 6 nm, a choice of R 56 ¼ 100 m gives p k rad R 56 % 20, sufficient to demodulate the electron beam. The optics design was carried out with the MAD-X [26] simulation code. The layout of the magnetic chicane is reported in Fig. 15. The bending angle after the first dipole magnet is 50 mrad, which results in a horizontal offset of 30 cm, enough to compensate for the delay introduced by a grazing incidence monochromator.