Space-charge distortion of transverse profiles measured by electron-based Ionization Profile Monitors and correction methods

Measurements of transverse profiles using Ionization Profile Monitors (IPMs) for high brightness beams are affected by the electromagnetic field of the beam. This interaction may cause a distortion of the measured profile shape despite strong external magnetic field applied to impose limits on the transverse movement of electrons. The mechanisms leading to this distortion are discussed in detail. The distortion itself is described by means of analytic calculations for simplified beam distributions and a full simulation model for realistic distributions. Simple relation for minimum magnetic field scaling with beam parameters for avoiding profile distortions is presented. Further, application of machine learning algorithms to the problem of reconstructing the actual beam profile from distorted measured profile is presented. The obtained results show good agreement for tests on simulation data. The performance of these algorithms indicate that they could be very useful for operations of IPMs on high brightness beams or IPMs with weak magnetic field.


I. INTRODUCTION
The principle of beam profile measurement using Ionization Profile Monitors (IPMs) is seemingly very simple. The beam particles ionize the residual gas. The products of the ionization -electrons or ions -are extracted towards a position-sensitive detector using the guiding electric field (also referred to as 'clearing' or 'external' field) provided by electrodes.
The distribution of the particles on the detector ideally corresponds to a projection of the transverse distribution of the beam density. This simple and straightforward principle, illustrated in Fig. 1, is non-destructive for the beam and thus an appealing application for synchrotrons and storage rings. In this context IPMs are installed in many hadron accelerators [1] and even investigated for usage on electron machines [2]. Figure 1: Sketch of an IPM together with read-out system. The electric and magnetic guiding fields are aligned, perpendicular to the beam direction as well as to the read-out system [3].
However already for medium-intensity beams the movement of electrons or ions is easily affected by the transient beam electric field. Because of their lower mass, electrons are removed much faster from the influence of bunch fields, however the effect of this fields on their trajectories is proportionally larger. Usually ion-based devices are preferred due to simpler assembly (no need of ion-trapping [4]), smaller impact of fringe fields of neighbouring magnets and lack of background signal from electrons generated from various other sources [5].
However the influence of beam fields plays a major role for high-brightness beams and thus electron-based IPMs are preferred over ion-based devices, mainly because the electron movement can be easily confined to small gyroradius by applying external magnetic field.
Another argument in favor of electron-based devices is the fact that they are more suitable for time-resolved measurements because the electrons have much smaller time-of-flights to reach the detector.
Distortions of the measured beam profile using ion-based IPMs are either mitigated by increasing the electric guiding field or corrected by empirical mathematical models [6][7][8][9].
For operation of electron-based IPMs with magnetic field it was first proposed to tune the magnetic field strength such that the electrons perform exactly one gyration between the beam and the detector [10]. However this method does not work for high-brightness beams, in which electrons get a significant momentum transfer not only transverse to the detector plane but also in the direction towards the detector. Therefore increasing the magnetic field in order to reduce the magnitude of gyroradius is the typical counteraction against the distortion of the measured beam profiles in that case. This is also because retrospective profile correction methods, similar to the ones developed for ion-based IPMs, do not exist for electron-based devices. One reason for that is the more complex particle movement in the presence of a combination of electric and magnetic fields. This movement and therefore the profile distortion, strongly depend on the beam fields and hence on bunch charge, transverse and longitudinal bunch shape as well as bunch spacing.
The first beam space-charge induced distortions in an electron-based IPM operated with magnetic guiding field were observed in the Large Hadron Collider (LHC) at beam energies of 4 TeV and magnetic guiding field of 0.2 T [11]. Following studies have analyzed this phenomenon and estimated a magnetic field strength of 1 T required to suppress profile distortions [12][13][14]. However such large field strengths pose a technological as well as financial challenge, especially because of the relatively large gap between the poles needed to fit the detector vacuum chamber.
Besides beam space-charge induced profile distortion there are various other phenomena which can have an effect on measured profiles. These are briefly discussed below however not addressed any further throughout this study: 1. Ionization by bunch field. At extreme field strengths the gas ionization cross-section is modified due to the Stark shift of energy levels and at even higher fields the gas gets ionized by the collective bunch electric field [15], rather than by interaction with single beam particles.
2. Burnout of the rest gas. Under some conditions, the ionization rate of the rest gas can be higher than the replenishment of gas in the volume of the beam leading to non-proportionality between beam density and amount of ionization events. This can be counterbalanced by, for instance, using gas jet [16]. 3. Guiding field non-uniformity. If the electric and magnetic guiding fields are nonuniform, it will additionally impact the resulting particle trajectories [17]. Also if the guiding fields are not perfectly aligned with respect to each other this introduces an additional disturbance. 4. Gas ionization from synchrotron radiation and the influence of wake fields on particle trajectories may constitute additional sources of profile distortion however these effects have not been observed or studied up to now.
Some of the mentioned effects emerge from the influence of the particle beam and depend mainly on the amplitude of the beam electric field and the duration of the corresponding pulse. An overview of various effects in dependence on these two parameters is shown in Fig. 2 alongside the beam parameters of several hadron accelerators, the most powerful laser system up-to-date (LFEX), 3rd generation light source (ALBA) and advanced free electrons laser (SwissXFEL). The peak fields in waist of LFEX laser beam reaches 1 × 10 7 MV m −1 , well beyond the limit of gas ionization by bunch electric field. In comparison, the highest field in an accelerator -a focused SwissXFEL beam -will be reaching about 2 × 10 4 MV m −1 . All hadron machines are currently below 10 MV m −1 but the bunch lengths are about 3 orders of magnitude larger than those of LFEX and XFEL. Fig. 2 also indicates two effects related to beam space-charge interaction: electrons being trapped by the beam fields exceeding the electric guiding field and electron velocities reaching into the relativistic regime.
Depending on the acquisition system in use, additional distortion of the measured distributions may occur. The electron or ion detection is usually based on amplification of the signal using Multi-Channel Plate (MCP) and either conversion of resulting electrons to light using a phosphor screen followed by a camera or direct conversion to electric signals using anode-strips. Such systems may suffer from non-uniformity of MCP gain, Phosphor deterioration or point-spread function of the optical system positioned after the Phosphor.
Recent developments focus on use of hybrid silicon pixel detector instead of MCP/Phosphor or MCP/anode-strip combination and hence are able to sidestep these issues [18].

II. PROFILE DISTORTION
In the following sections we will focus on profile distortion effects related to the electron movement which is governed by their interaction with the present electromagnetic fields.
For the scope of this contribution, we assume perfectly uniform and aligned guiding fields and do not consider gas target and acquisition system related effects which were mentioned in the previous section. The coordinate system used throughout this study is x, along the measured profile (horizontal), y, towards the acquisition system (vertical) and z, along the beam (longitudinal).
For illustration purposes we refer in this section to the data obtained for an example case which has been studied by means of simulations. The corresponding parameters are given in Table I and correspond to a possible LHC flat top configuration (nominal value of bunch charge is about 1.3 · 10 11 , but charges above 2 · 10 11 are expected for HL-LHC). The LHC beam case is well studied [12][13][14][19][20][21]. As another example we will shortly discuss the SIS-100 IPM system for proton beam at flat top. The electron movement is initialized by the ionization process and further influenced by the interaction with the guiding fields and the electromagnetic field of the particle beam (space-charge interaction). The relevant effects can be summarized by the following three aspects : • Ionization momenta -The initial momenta of electrons, obtained during the ionization process, clearly influence their further trajectories. Due to the interaction with the magnetic guiding field an initial momentum component along the beam axis results in a transverse displacement of the subsequent gyromotion while a transverse momentum component results in a longitudinal displacement. This effect becomes more pronounced for small guiding field strengths as well as for small beam sizes where the magnitude of the gyromotion increases relative to the beam dimensions. For relativistic beams the electrons are mainly ejected transverse to the beam direction [22].
• Space-charge interaction -The interaction of electrons with the electric field of the particle beam may significantly alter their trajectories, resulting in noticeable distortions of measured profiles. This interaction cannot be treated analytically without introducing significant approximations and hence must be studied by the means of numerical simulations. As this effect correlates with the magnitude of the beam electric field, it becomes especially significant for high intensity and high energy beams.
However also small magnetic guiding fields may not be sufficient to counteract the beam field interaction and hence provoke an increase in gyroradii.
• Gyromotion -The electrons perform a spiral movement under the influence of the external magnetic guiding field. Depending on the magnitude of the gyroradius an electron may experience a significant displacement with respect to its center of gyration. This effect is especially significant for small beam sizes since the magnitude of the displacement increases relative to the beam size.
Often the magnetic guiding field is sufficient for suppressing profile distortion however in extreme cases the space-charge interaction can become so strong, with electric fields of up to a few MV m −1 (typical extraction fields are around 50 kV m −1 ), that electrons are (a) trapped inside the space-charge region since the beam electric field outweighs the electric guiding field around the center of the bunch, and (b) forced on significantly different trajectories, resulting in a vast increase of gyroradii up to a few millimeter. Since the above effects depend on the various parameters of the particle beam it is often not obvious to determine an appropriate strength for the magnetic guiding field which allows to sufficiently suppress the gyroradius increase.
Having in mind the above-mentioned effects we may subdivide the IPM volume into two regions: • Space-charge region -This is the region close to the beam center where the interaction with the beam's electromagnetic field has a major influence. The gyro-velocity of electrons are subject to a perpetual oscillation induced by the ExB-drift along the beam and the magnitude of this oscillation is altered when the beam center approaches and recedes from the positions of the electrons. Because of the complex shape of the electromagnetic fields the electron motion exhibits other electromagnetic drifts as well as for example drifts due to the time dependence of the beam field (polarization drift) [23]. For estimating the dimensions of this region one can consider the ExB-drift velocity as an indicator for space-charge action. The region in which this ExB-drift velocity is more than 1% of the undisturbed gyro-velocity, is of the order of a few millimeters.
• Detector region -This is the region close to the acquisition system where the beam Note that as the bunch recedes from the electrons' positions the space-charge region shrinks in time. While this region can be quite large during the bunch center passing, the transverse electric field quickly diminishes according to the bunch's line density. Therefore also electrons that are ionized near the bunch's center will eventually end up performing a pure gyromotion as in the detector region. Fig. 3 shows the bunch electric field as it decreases towards the detector (y) and along the beam axis (z) and hence illustrates the separation in space-charge and detector region. While the two dependencies are shown separately, their collective effect results in a much stronger decrease as the bunch recedes from the electrons' positions and hence the effective space-charge region is smaller than indicated by the single dependencies. This is because the beam velocity is significantly larger than the electrons' velocities along the beam axis and hence the spatial dependence (z) can be thought of as a time-dependence (t).
Considering an electron's initial position during ionization and final position during detection, x 0 and x f respectively, the overall displacement ∆x = x f − x 0 can be ascribed to three different effects: 1. ∆x 1 -Displacement of the electron's gyro-center x c 0 with respect to the initial position, Figure 3: Evolution of the transverse bunch electric field E x at x = σ x in direction towards the detector (y) and during beam passage (z, t), for the parameters in Table I. The x-position is fixed to 1 σ x and y-and z-position are fixed to zero if not varied.
due to its initial velocity along the beam: ∆x 1 = x c 0 − x 0 . Electrons that are ejected transverse to the longitudinal beam axis, in horizontal direction, merely suffer from a displacement of their gyrocenter along that axis, i.e. not affecting their horizontal position which determines the transverse profile. If they are ejected along the beam however there will be a corresponding displacement transverse to it. This type of displacement is typically around a few tens of microns and can be assessed with differential ionization cross sections which usually depend on the projectiles charge state and energy. In relativistic cases transverse ejection dominates and so the resulting displacement is mainly along the beam [22].
2. ∆x 2 -Displacement of the space-charge altered gyrocenter x c s with respect to the initial gyrocenter: ∆x 2 = x c s − x c 0 . This shift is found to be induced by the magnetic field of the beam. In addition the longitudinal electric field of the beam induces an ExB-drift in horizontal direction which adds up as an additional displacement of the gyrocenter.
However for relativistic beams the exposure to the longitudinal field is short and hence this drift tends to be negligible. The net effect of this type of displacement is around a few microns even for high beam currents. 3. ∆x 3 -Displacement of the final position with respect to the stable gyrocenter due to the nature of the gyromotion: ∆x 3 = x f − x c s . The electrons perform a gyromotion in the plane above the detector and in case this motion spans over multiple elements of the detecting system, the electrons might be detected on any of them. If the moment of detection is not correlated to the phase of the gyromotion then the resulting displacement is random. Since this type of displacement is proportional to the resulting gyroradii it depends on the magnitude of the preceding space-charge interaction and can be around some hundreds of microns up to a few millimeter.
These effects only consider displacements in horizontal direction, along the measured profile. In fact the electrons experience an additional significant displacement in longitudinal direction, along the beam, due to the ExB-drift induced by the transverse electric field of the beam E x . However such drifts can be neglected in case the physical situation is similar along the beam axis (i.e. uniform guiding fields, similar gas pressure).    Table I). The term "undisturbed trajectory" refers to the case if there were no space-charge interaction, i.e. only pure gyromotion. Because the initial and final gyroradius is about 100 µm the corresponding part of the trajectory appears as a flat ellipse due to large scale of the z-drift. The "shifted gyrocenter" is induced by the transverse electric field of the beam and acts as a starting point for the polarization drift; this shift is described by equation Eq. (6). "Bunch center passing" indicates the moment when the electron and the bunch center are aligned with respect to z-position. The "initial" and "final gyrocenter" are separated by a distance of 0.5 µm.  In order to get an analytic estimate of the interaction we assume a simplified case in which the longitudinal electric field is zero and the transverse electric field is approximated by a linearly increasing field E x = E · x where E < 0 (this corresponds to the electric field inside a uniformly charged cylinder bunch of positive ions). For a Gaussian bunch shape this approximation is also well applicable at the center of the bunch as can be seen from Fig. 5 (neglecting the y-and z-dependence of the field). We also consider no bunch magnetic field since it is small compared to the magnetic guiding field B. The electric guiding field is acting in y-direction and hence has no influence on the motion in the xz-plane. In this simplified scenario the particle motion in the xz-plane is obtained as: where we have used the following abbreviations (q is the elementary charge and m is the Eq. (1) shows that the particle performs an oscillating movement in x-direction while Eq. (2) shows a similar oscillation as well as an ExB-drift in z-direction. The absolute value of the gyro-velocity is given by |v xz (t)| = ẋ(t) 2 +ż(t) 2 . From Eq. (3) and Eq. (4) we can see that this velocity expression contains terms sin 2 (Ωt), cos 2 (Ωt) and sin(Ωt) · cos(Ωt) which are π-periodic as well as terms sin(Ωt) and cos(Ωt) which are 2π-periodic. Fig. 7 shows the gyro-velocity for an example particle subject to the realistic electric field of a Gaussian bunch. The expected twofold periodicity is clearly visible while an additional damping effect occurs due to the longitudinal field dependence when the beam recedes from the particle's position. The minimum and maximum of the velocity become apparent when considering the two-dimensional trajectory (x(t), z(t)). At one turn-around point the velocities from ExB-drift and the oscillating movement are aligned and add up, resulting in a large velocity while at the other turn-around point they are opposite resulting in a smaller velocity (or even a backward drift). Such behavior can as well be observed from simulations as described below. Fig. 8 shows the velocities of various particles for different starting points x 0 subject to the realistic field of a Gaussian charge distribution. One can observe a variation of the amplitude modulation as well as different periods of the oscillation depending on the starting point. This is due to the non-linear shape of the Gaussian-bunch electric field for positions farther away from the bunch center (compare the electric field plot in Fig. 5).  Table I).
Analyzing Eq. (1) to Eq. (4) we can infer various parameters of the resulting electron motion.

Shift of gyration center
From Eq. (1) one can read the center of oscillation which contains an additional term corresponding to the transverse bunch electric field. This shift however is compensated due to the time-dependence of the electric field and the corresponding polarization drift as shown in Appendix A. The same effect can be observed from simulations as well. This results in the initial and final gyrocenter to be aligned with respect to each other as shown in Fig. 4. Note that a displacement of the gyrocenter with respect  Table I).
to the initial position x 0 however occurs due to a non-zero initial velocity component along the beam v z0 . This shift is not compensated for by any other effects.

Displacement due to gyromotion
In the detector region, without the influence of the beam electric field, the electrons perform a pure gyromotion with gyroradius proportional to the planar velocity. Because of the velocity oscillations in the space-charge region, described by Eq. (3) and Eq. (4) as well as The resulting gyromotion implies possible displacements to positions different from the center of the gyromotion. This effect is discussed in detail in [13,14].

Time-of-flight
In order to obtain an estimate for the final gyroradius of electrons we need to derive an estimate for their time-of-flight until they reach away from the influence of disturbing Figure 9: Distribution of gyro-velocity ratios, final over initial velocity, due to space-charge interaction (parameters corresponding to Table I).
beam fields. Since the above analytic considerations are valid only inside the bunch, we set the space-charge region as the region inside the bunch and the detector region as the region outside the bunch. This assumption neglects modifications of the particles' velocities outside the bunch while in reality they may remain affected by the bunch electric field at these positions as the space-charge region may extend farther out. A more complex description would be to treat the decreasing part of the field outside the bunch by another linear field model, similarly to the one inside, and to reuse the above derived equations for that case by applying appropriate coordinate transformations. However since this approach is significantly more complex we stick with the former approach as it turns out to be sufficient as indicated by the following simulation results.
For estimating the time-of-flights one cannot simply rely on the time the bunch needs to recede from the electron but one needs to consider the electron leaving the bunch volume (i.e. the space-charge region) in the transverse direction as well. In order to obtain a more accurate estimate for this time-of-flight in the vertical direction we need to take the bunch electric field and the electric guiding field into account. We do so by simplifying the longitudinal time dependence by an exponential dependency instead of the Gaussian dependency, in order to relax the explicit time dependence. The spatial dependence is linear as before. The corresponding equation of motion is: where E g is the electric guiding field strength.
For a detailed derivation of the resulting motion (y(t),ẏ(t)) consider Appendix B.
In order to estimate the time-of-flights we need to find the roots of the functions f 1 (t) = x(t) 2 + y(t) 2 − R where R is the radius of the cylinder bunch and x(t) is given by Eq. (1) and   Table I. The simulations were performed using the Virtual-IPM simulation tool [24]. The estimated distributions show agreement in the shape as well as the mean and variance and hence suggest the applicability of the derived equations. These equations, even though obtained for simplified bunch shape, can be used to further study the phenomenon of profile distortion as shown in the following section.

C. Magnetic field tuning and scaling for realistic beams
Since IPMs find application in different circumstances including a variety of beam parameters it is useful to establish a relation between the beam parameters and the magnetic field required to suppress profile distortion. Such a relation allows for uncomplicated checks when designing new devices. Different criteria for a sufficient field strength are possible such as absolute thresholds ensuring that most of the electrons' gyroradii are below an acceptable threshold (e.g. requiring a certain fraction to be below some percentage of the bunch width or to be smaller than the detector resolution) or relative thresholds that ensure that the increase of measured profile standard deviation remains below some percent threshold. For the further investigations we will use the latter criterion. We establish a fit of the three most significant beam parameters relevant for profile distortion, the transverse and longitudinal bunch size and the bunch charge: where a, b, c, d, e, f are fit parameters, σ 2 t is the variance of the (symmetric) transverse beam distribution to be measured in units units of millimeter, σ 2 z is the variance of the bunch Simulation parameters corresponding to Table I  because this would be useful in investigation of emittance blow in modern colliders [25]. In a subsequent fit this threshold is converted to a parameter.
For verifying the relationship and to infer the parameters we use the analytic considerations from the previous section as well as simulations with three different bunch shapes: Gaussian, Uniform and Parabolic Ellipsoid. For simplicity the bunch shapes are considered to be rotational symmetric around the beam axis (σ t ≡ σ x = σ y ). The charge density distributions of these shapes are given by: We only consider relativistic beams since for low energy beams the ionization cross sections take on substantially different shapes [26] and the generated electrons are longer exposed to the beam electric field due to the reduced velocity and hence effects from the longitudinal electric field start to play a role. Hence for the fitting relativistic β ≥ 0.99 is assumed.
The relationship Eq. (8) is fitted with simulation data obtained with the Virtual-IPM simulation tool, for the parameters given in Table II. These parameters correspond to the Gaussian bunch shape. For the other bunch shapes the parameters are scaled where appropriate in order to minimize the squared difference between initial beam distributions; for more details consider Appendix D. The resulting scaling factors are given in Table II as well.  Table III shows the fit parameters obtained for the various bunch shapes as well as the quality of fit. They show agreement among each other and also with the results obtained for the analytic derivations. Applying the formula to the example case in Table I we obtain a minimum required field of 0.975 T which is in conformity with the estimated value obtained from previously run simulations with a different simulation tool [13]. Using the same formula for set of beam parameters corresponding to the planned IPM on SIS-100 proton beam (29.9 GeV energy, 2 × 10 13 charges, σ t = 2.17 mm, σ z = 15 ns), the minimum required field is estimated to 76 mT while the current design foresees a field of 84 mT [27].
A previous derivation of minimum required magnetic field strength that was done for related circumstances, however considering the gyroradius increase as an intermediate quantity instead of the final profile standard deviation, shows a similar dependence on the beam parameters [12].  In order to assess the effect of a chosen threshold for standard deviation increase τ = σ m σ −1 t − 1, with measured (simulated) standard devation σ m , on the minimum required magnetic field we include this parameter as well in a subsequent fit of the form: The results are similar to the ones obtained before and are shown in Table IV. Another way to include the threshold in the formula is to model the remaining fit parameters as dependent on the threshold: This formulation captures potential variations in the beam parameter dependence for varying thresholds τ . The results however, as shown in Table V, confirm that the effect of varying threshold is mainly a scaling factor. These results are in agreement with the τ f scaling shown from Eq. (10) and Table IV.

III. PROFILE CORRECTION
The first remedy against the space-charge profile distortion is to increase the applied magnetic guiding field, such that the gyroradii are bounded by user defined limits as shown by Eq. (8), Eq. (10) and Eq. (11) with corresponding parameters given by, respectively, Table III, Table IV and Table V. These relations are fit for the broad parameter range in Table II. Most magnetic IPM designs fall into this category and further analysis is not required. However for some extreme scenarios very large magnetic fields are required [13], which are both expensive to obtain and occupy significant space in already cramped synchrotrons. As technology advances more applications for high energy and high brightness beams are to be expected, putting current magnetic IPM designs to test. For these scenarios, correction mechanisms to obtain a measure of actual profile from the distorted profile, have been studied.
Foremost of them was the study of a hardware electron "sieve", which aims to filter electrons based on their gyroradius before they reach the acquisition system, and a numerical reconstruction on the resulting "sieved" profiles can be performed [14]. Though the study with simulations gave promising results, the sieve was found to be rather complex and thick structure difficult to integrate in machine vacuum. Another approach was parametric curve fitting of the distorted profile with analytic functions and correlation of the beam width with those parameters [13]. The fit result however suffered from too few available data points and hence complicating establishing a general dependence between the parameters.
Lately, approaches to record the inverse mapping between distorted profile to the original profile or the second moment of the original profile as a function of space charge parameters has been introduced either in form of look-up tables (LUT) [28] and supervised learning [20].
All the aforementioned correction methods rely on well understood and benchmarked IPM simulations. In this section, the problem of profile correction is generally introduced. Following that, several sub-approaches in the supervised learning scheme are mentioned and an extension to a full profile reconstruction from distorted profiles with arbitrary initial profile shapes is shown.

A. Problem description
In case of negligible space-charge interaction the distortion can be described via convolution of beam profile P beam with a point-spread function (PSF) to obtain the measured profile P measured [13]. This PSF depends on the initial velocities of electrons and can assessed by means of differential ionization cross sections. The PSF itself describes the electron transport in the IPM. It corresponds to the probability that an electron is detected on a certain position displaced from its ionization position and can be obtained from considering the time that an electron spends above the various bins of the detector [14]. This PSF is independent of the position along the initial profile.
If the PSF depends on the position along the profile -as in case of space-charge influence on electron movement -a convolution cannot be used to describe the profile deformation anymore. As a more general transformation a matrix multiplication can be used instead: where M ij is the probability that an electron which was generated at position j is collected at position i. Comparing with convolution for the space-charge free case, the matrix M contains the PSF as columns, shifted across the rows with the PSF center at the diagonal . A fast iterative procedure to correct measured profiles using the simulated M −1 matrices is discussed in [28]. A newer approach of supervised machine learning is to deduce a set of rules for mapping measured profiles to their original counterparts.
The idea is to infer a corresponding set of rules by providing distorted profiles alongside other relevant beam parameters and to map these to the original distribution of the beam. Fig. 11 shows a schema of profile correction with the aid of simulation data.

B. Data generation by IPM simulations
A recent joint effort between laboratories led to the development of a generic simulation tool called "Virtual-IPM" [24]. The Virtual-IPM tool has been used for simulating the movement of electrons inside the IPM, in presence of transient beam fields. Table VI shows the parameter ranges that are used for the simulations in order to span the relevant parameter space. The single bunches are modeled by three-dimensional Gaussian charge distributions. The electric field of bunches is computed via an analytic formula for a twodimensional Gaussian charge distribution in the transverse plane [29] while the longitudinal dependency is taken into account by rescaling the field with the beam's line density. The longitudinal field component is neglected. This approximation is justified because the beam is highly relativistic and the longitudinal dimension of bunches is significantly larger than their transverse dimensions and therefore the electric field is mainly acting in the transverse plane. The advantage of the analytic formula, as compared to a numerical Poisson solver, is that it is much faster in computing the electric field and it does not suffer from discretization effects. Both the electric and the magnetic field of the beam are taken into account.
The external electric and magnetic guiding fields are modeled to be uniform within the field cage. The initial velocities of electrons are generated according to a double differential cross section for a Hydrogen target [22]. The passage of only a single bunch is simulated because the extraction times for electrons are only a few nanoseconds for the given electric guiding field while the bunch spacing is 25 ns.
The output of the simulations is summarized by histograms with 9.9 mm range and 55 µm bin size, representing the electrons' positions at the moment of ionization, in the following referred to as initial, and at the moment of detection, in the following referred to as final.
55 µm resolution corresponds to a hybrid-pixel detector type which has been successfully operated in the PS IPMs [18]. Eq. (10) has the interesting property that for a given measured standard deviation of σ m and a given magnetic field B the formula encodes the corresponding beam profile standard deviation σ t which gives rise to the distortion via the threshold parameter τ . Specifically the beam profile standard deviation is the root of the function on the interval (0, σ m ), which can be computed by means of numerical methods.
We test this method on the simulation data prepared according to Table VI by using bisection method [30] with 0.1 µm tolerance. The resulting residuals have an overall mean and standard deviation of, respectively, 0.357 µm and 3.21 µm. The residuals plot Fig. 12 shows that the quality varies with beam profile standard deviation. Nevertheless most results are within 2% accuracy.
Previous attempts of reconstructing the initial beam profile standard deviation value from  Table VI. measured profiles using Machine Learning methods have shown a better performance while being fitted on a much smaller parameter range [20]. Eq. (10) on the other hand has been fitted on a range which spans multiple orders of magnitude.

D. Reconstruction of complete profiles with machine learning
Relevant for the presented problem are supervised regression models for predicting continuous variables. In general a supervised machine learning (ML) model represents an algorithm f , a mapping from input x to output y p , called decision function, that is specified by a set of parameters θ ≡ {θ i }. The inputs and outputs are exactly opposite to the data preparation stage as shown in Figure 8. While the structure of such an algorithm (e.g. the number of parameters |θ|) depends on its hyper-parameters and is fixed, the goal is to tune θ i such that the corresponding function f (x | θ) describes the output best. The quality of this description is typically assessed by a so called loss function L(x, y | θ) → R + which measures the deviation of the predictions y p from the target output values y. Minimizing this loss function is used as a criterion for optimization of the model parameters θ. In order to find a suitable ML algorithm for a given problem their performances are compared.
For doing so the data set is split in three subsets corresponding to training, validation and testing. The training set is used to fit the particular ML algorithms in order to determine the optimal parameters θ. The validation set is used for assessing the performance of an algorithm once it is fitted. Validating on a distinct data set prevents effects of over-fitting the algorithm to that particular training set and hence ensures generalization of the algorithm. The test similarly aims to prevent a tuning bias towards the validation set, resulting from the multiple iterations corresponding to hyperparameter tuning. The test set is used to assess the final performance that is to be expected once a ML algorithm with a specific configuration has been chosen.

Full profile reconstruction
Previous works have studied the usage of machine learning models for establishing a relationship between measured profile and original beam profile standard deviation [19,20]. A very recent approach investigated reconstruction of the complete profile shape from measured profiles [21]. This approach used an additional global data transformation that is derived from the training data in order to converge during the fitting procedure. Here we present a novel approach that works only with per-profile normalization, providing a more consistent way of data preparation.
a. Model architecture In order to establish a mapping between measured profiles together with bunch length and bunch intensity data to the original beam profiles we recall Eq. (12). This equations states that the process of profile distortion can be described by a matrix multiplication M of the beam profile P beam . In general this matrix is dependent on the beam parameters and hence the beam profile itself: M → M (σ z , N p , P beam ). We suppose the matrix is invertible and that the inverse matrix M −1 similarly depends on the measured profile: M −1 → M −1 (σ z , N p , P measured ). This means that the process of profile distortion does not lose any information and the original profile shape is still encoded in the measured data. Then the original profile can be obtained by means of a matrix multiplication M −1 with the measured profile P measured . Here we identify the task of the neural network as the generation of the inverse matrix in dependence of the measured profile as well as the additional beam parameters bunch length and bunch intensity.   Table VII. For identifying optimal configurations we monitored the final validation loss as well the ratio of initial to final validation loss in order to account for different starting losses. In a first step consensus about the batch size was found. With this optimal value fixed during a second and third iteration the optimal number of nodes and learning rate was identified respectively. The number of nodes however was found to have no significant influence on performance, if large enough, as shown in Fig. 15. The final configuration was batch size 236 (evenly divides the training set of 13 452 samples), learning rate 8.882 × 10 −3 and number of nodes in the first dense layer equal to 42. We used He-uniform initializer [31] and mean squared error (MSE) as the loss function.  b. Data preparation Data preparation is an important aspect of any machine learning analysis and helps the fitting procedure to converge. Also it needs to be ensured that the way of data preparation is compatible with real application requirements. The previous study on full profile reconstruction [21] used per-profile normalization followed by a per-feature normalization which was computed over the full training set. In the current study we focus on a more intuitive approach that only uses per-profile normalization. For data preparation we use the following steps: viation of resulting profile standard deviation was ∆σ x /σ x = 2.01 % ± 1.00 % which is about one order of magnitude larger than what has been obtained from previous attempts with machine learning models directly targeting the profile standard deviation [20]. Table VIII shows the performance on the test set compared to the previously developed algorithm, the latter of which clearly performs better.
The neural network has been trained on Gaussian profiles only however in real world applications the actual beam shapes might deviate from this ideal scenario. Hence an impor- Generalized Gaussian, used for instance in linac beam profile description [28]: with shape parameters α, β, µ; β = 2, α = √ 2σ x corresponds to a normal distribution. Γ denotes the gamma function [32].
Q-Gaussian, used in studies of tails of hadron beams: with shape parameters β, q; q = 1, β = σ −2 x /2 corresponds to a normal distribution. C q is a normalization factor to provide unit integral.
The parameters of the non-Gaussian shapes correspond to the ranges shown in Table VI but are rescaled in order to match the Gaussian distribution in their limits. For the Generalized Gaussian α x,y,z = σ x,y,z and for the Q-Gaussian β x,y,z = σ −2 x,y,z /2. Fig. 16 shows an example profile for each profile shape together with the predicted reconstruction. Note that the Q-Gaussian profiles for q = 2 are not affected by distortion however they are significantly wider than the training profiles.
The overall performance, as shown in Table IX and Fig. 17, decreases about one order of magnitude as compared to the Gaussian profiles. The new approach however shows improved performance as compared with the previous approach. Especially on the very wide Q-Gaussian (q = 2) profile shapes, the new approach doesn't suffer from "blowing up" the results as much, but instead manages to preserve the undistorted profiles to a better degree. The obtained results imply that the neural network identifies the underlying distortion mechanism rather than memorizing a specific profile shape which it has been trained on.
The results were obtained using various scientific Python packages, including Keras [33], Matplotlib [34], NumPy [35], Pandas [36], SciPy [37], Tensorflow [38] and Virtual-IPM [24].  Uniform, Gaussian and Parabolic shapes. Following that we addressed the problem of reconstruction of the real beam profile from distorted measurement. First an approach based on the previously developed formulas for minimum magnetic field is presented which allows to infer the beam profile standard deviation from measured profile standard deviation on a broad range of beam parameters. The second approach addressed the problem of complete profile shape reconstruction and was realized by using a dedicated machine learning model involving neural network. The performance of this method was tested on various realistic profile shapes, including Gaussian, Q-Gaussian and Generalized Gaussian shapes. By fitting the model only with Gaussian profiles we showed that the approach generalizes as well to beam shapes significantly different than normal distributions.

A. COMPENSATION OF GYRATION CENTER SHIFT
Eq. (6) indicates the shift of the gyration center with respect to the undisturbed case (for which E = 0). Using Eq. (5) this becomes: with ω =qB the gyrofrequency for the undisturbed case E = 0.
For the derivation of Eq. (1) -Eq. (4) the bunch electric field was assumed constant, i.e. E = const. In order to incorporate the time-dependence due to bunch movement we can consider a series of partial solutions (x n (t), z n (t)) each of which is valid on an interval [t n , t n+1 ] for which the time dependence remains approximately constant. These partial solutions are of the form: x n (t) = a n Ω n sin(Ω n t) − b n Ω n cos(Ω n t) + c ṅ x n (t) = a n cos(Ω n t) + b n sin(Ω n t) where E n ≡ E(t n ) and Note that c 0 represents the (shifted) gyration center.
In order to link the solutions and their coefficients together we require continuity at the boundary of time intervals: (x n (t n ),ẋ n (t n ), z n (t n ),ż n (t n )) ! = (x n−1 (t n ),ẋ n−1 (t n ), z n−1 (t n ),ż n−1 (t n )) Using Eq. (17) The matrix on the left hand side has determinant −Ωω −1 = 0 and hence a unique solution for the coefficients (a n , b n , c n , d n ) exists. Inverting the matrix we obtain: Ωn sin(Ω n t n ) cos(Ω n t n ) 0 ω Ωn sin(Ω n t n ) ω 2 −Ω 2 n Ωn cos(Ω n t n ) sin(Ω n t n ) 0 − ω Ωn cos(Ω n t n ) The parameter c n encodes the gyration center shift and is given by the third line of the inverse matrix: Inserting Eq. (17) Since for n → ∞ we have Ω n → ω it follows that c n → x 0 − v z0 ω −1 which is the undisturbed gyration center. Hence the initial shift, encoded in c 0 , is compensated for t → ∞. Actually this limit is already reached for E → 0 which is the case once the bunch has receded from the electron's position.

B. DERIVATION OF TIME-OF-FLIGHT FOR UNIFORM CHARGE DISTRIBU-TION
We define a ≡ qEg m , b ≡ q|E| m and thus obtain: We solve the differential equation for t ≥ 0 (denoted as y > ) since for t ≤ 0 (denoted as y < ) we can then reuse this solution together with the transformation t → −t.
By using the relations d dx we confirm that the terms which contain the derivatives of the Meijer G-function cancel each other. We then obtain forẏ(t): For the t ≤ 0 case we apply t → −t and reuse the above solution. Note that the derivativeu(t) changes its sign due to the transformation. For t 0 < 0 we obtain k > 1 , k > 2 from the continuity condition at t = 0: y < (0) ! = y > (0) andẏ < (0) ! =ẏ > (0)).

C. METHODS FOR MINIMUM MAGNETIC FIELD COMPUTATION
For the computation of minimum magnetic field strength from analytic formulae we used the following methods. For each configuration (beam parameters + IPM parameters including magnetic field) 1000 particles were sampled from the initial beam distribution (sampling x-and y-position as well as the ionization time at z = 0 since the guiding fields are assumed to be uniform and the resulting profile is integrated along z). The initial momenta are sampled from double differential cross sections [22]. For each particle its time-of-flight (TOF) until leaving the bunch region, described by x(t) 2 +y(t) 2 ≤ R 2 and z(t) ≤ (t−4σ z )βc, was computed as the minimum between the TOF in transverse direction, considering Eq. (1) and Eq. (25), as well as in z-direction, using the bunch velocity and the electron velocity in z-direction via Eq. (2) and Eq. (4). We then use Eq. (3) and Eq. (4) in order to compute the final transverse velocity |v xz (t of )| = v x (t of ) 2 + v z (t of ) 2 from which we calculate the gyroradius given the magnetic guiding field. We then consider gyration around the central point indicated by Eq. (1) and a random position of detection within the gyromotion range with probabilities proportional to the corresponding point-spread function (PSF) [14]. In order to increase the statistics, for each particle 100 final positions were sampled according to the corresponding PSF, all corresponding to the same initial position. This results in a total of 100 000 data points for computing the standard deviation of the beam profile and the measured profile respectively. Each configuration was scored according to the relative deviation of measured standard deviation with respect to standard deviation of the beam profile λ = (σ m −σ b )σ −1 b , where λ denotes the score and σ m , σ b denote the measured and beam profile standard deviation respectively. A particular magnetic field strength is considered sufficient if λ ≤ 0.01. The magnetic field strength B is computed by finding the root of the function λ(B) − 0.01 using the bisection method [30] with a tolerance of 1 mT.

D. SCALING FACTORS FOR DIFFERENT BUNCH SHAPES
Different bunch shapes are compared to a Gaussian distribution and their free parameters are adjusted such that the resulting distribution has a minimal least square difference to the corresponding Gaussian distribution: arg min η +∞ −∞ (ρ(x|η) − N (0, σ)) p dx (29) where ρ(x|η) represents a particular bunch shape parametrized by η. Least squares difference (p = 2) is used for the Uniform bunch shape and p = 4 is used for the Parabolic Ellipsoid shape for numerical stability. The L-BFGS-B solver from the scipy.optimize package [37] is