\chapter{Simulation} The ratios of positrons, detected using NaI detectors in coincidence mode, to the electrons impinging on T1 at different energies in the experiment was substantially smaller than one would expect (10$^{-15}$ instead of 10$^{-3}$). To better understand this ratio and to study the processes of positron generation and transportation, simulations were performed by using G4beamline. ``G4beamline is a particle tracking and simulation program based on the GEANT4~\cite{geant4} toolkit that is specifically designed to easily simulate beamlines and other systems using single-particle tracking~\cite{muonsinc}.'' A sample G4beamline script for positron generation using HRRL beamline is given in the appendix F. The simulation predicts that at least one positron is created per 1000 incident 10~MeV electrons. A large amount of beam loss was observed during the initial simulation. As a result, the simulation was divided into three steps based on the locations along the beamline and beam loss. A new beam event generator was created based on the results of the previous step. The first step generated an electron beam with the energy distribution observed in the experiment as shown in Figure~\ref{fig:En-Scan}. The electrons generated were focused by three quadrupoles onto the target T1 (see Figure~\ref{fig:app-hrrl-line}). Electrons traversing T1 produced bremsstrahlung photons of sufficient energy to produce $e^+e^-$ pairs. The second step simulated the collection and transportation of positrons exiting T1 to the entrance of the first dipole D1. The last step transported positrons from the entrance of D1 all the way to the annihilation target T2, the interactions of positrons with T2, and the detection of the resulting 511~keV photon pairs. T1 was placed such that the upstream side of T1 was facing down with 45$^{\circ}$ angle (rotated 45$^{\circ}$ counter clockwise about x-axis). T2 was first placed with its upstream side facing down by 45$^{\circ}$ angle (rotated 45$^{\circ}$ clockwise about z-axis) and then was rotated by another 45$^{\circ}$ clockwise about y-axis. Result in upstream side T2 was facing the right NaI detector. \section{Step 1 - The Electron Beam Generation and Transportation to T1} In the first simulation step, an electron beam was generated with an energy distribution that was observed in the experiment. The emittance, the Twiss parameters, and the energy distribution of the electron beam were measured experimentally and used to generate the electron beam. The energy distribution of the electron beam is shown in Figure~\ref{fig:En-Scan}. The distribution was fit using two skewed Gaussian distributions. The fit parameters given in Table~\ref{tab:En-Scan_resluts} were used to generated electrons. A series of virtual detectors were placed along the beamline to sample the beam. As an example, three virtual circular detectors and T1 are shown in Figure~\ref{fig:T1_UpD_DwD2}. The electron beam was detected by a virtual detector DUPT1 (Detector UPstream of T1) placed 25.52~mm upstream of T1. Positrons, electrons, and photons generated during the interaction of the electron beam with T1 were observed by virtual detectors DT1 (Detector of T1) and DDNT1 (Detector DowNstream of T1) placed 25.52~mm downstream of T1. %In Figure~\ref{fig:SimS1_pos_En_DDNT1}, $13.8 \times 10^{10}$ electrons shot at T1 and generated positrons positrons shown in blue. \begin{figure}[htbp] \centering \includegraphics[scale=0.55]{3-Simulation/Figures/sim_setup_T1_UpD_DwD2.png} \caption{T1 is the positron production target. DUPT1 is a virtual detector located upstream of T1 to detect the incoming electron beam. DDNT1 is a virtual detector downstream of T1. DT1 is a virtual detector that is placed right after T1 and parallel to it.} \label{fig:T1_UpD_DwD2} \end{figure} \subsection{The Positron Beam on DDNT1} In the first step, $1.38 \times 10^{10}$ electrons were generated with the energy distribution shown by the dotted dashed line in Figure~\ref{fig:SimS1_T1UPDN}, transported to T1, and created the positrons represented by solid the line. The dashed line is the electron energy distribution observed by DDNT1. The simulation result of $1.38 \times 10^{7}$ electrons incident on T1 is drawn in Figure~\ref{fig:SimS1_T1UPDN}. The incident electrons were detected by the virtual detector DUPT1 and downstream positrons and electrons were detected by DDNT1. \begin{figure}[htbp] \centering \includegraphics[scale=0.75]{3-Simulation/Figures/s/s1/overlay8.eps} \caption{Energy distribution of incident electrons (dotted dashed line), electrons after T1 (dashed line) and created positrons (solid line). The incident electron distribution counts were weighted by 0.001.} \label{fig:SimS1_T1UPDN} \end{figure} \begin{figure}[htbp] \begin{tabular}{cc} {\scalebox{0.364} [0.364]{\includegraphics{3-Simulation/Figures/X_e+_DDNT1.eps}}} & {\scalebox{0.364} [0.364]{\includegraphics{3-Simulation/Figures/Y_e+_DDNT1.eps}}} \\ (a) $x$ projection. & (b) $y$ projection. \\ {\scalebox{0.364} [0.364]{\includegraphics{3-Simulation/Figures/XP_e+_DDNT1.eps}}} & {\scalebox{0.364} [0.364]{\includegraphics{3-Simulation/Figures/YP_e+_DDNT1.eps}}} \\ (c) $x$ projection of the divergence. & (d) $y$ projection of the divergence.\\ {\scalebox{0.364} [0.364]{\includegraphics{3-Simulation/Figures/XY_e+_DDNT1.png}}} & {\scalebox{0.364} [0.364]{\includegraphics{3-Simulation/Figures/XY_e+_DDNT1_zoom.png}}} \\ (e) The transverse beam profile. & (f) Zoom in of (e). \\ \end{tabular} \caption{The transverse beam projections and angular distributions of positrons detected.} \label{fig:DDNT1_results} \end{figure} The positron spatial and angular distribution detected by DDNT1 is shown in Figure~\ref{fig:DDNT1_results}. The $y$ $vs.$ $x$ spatial distribution of the beam is shown in Figure~\ref{fig:DDNT1_results}~(e) and Figure~\ref{fig:DDNT1_results} (f). As can be seen from Figure~\ref{fig:DDNT1_results} (b) and (d), the $y$ spatial distribution and divergence, defined in equation~\ref{eq:divergence}, of the positron beam have a sharp drop in counts in the region between $-25.8$~mm and $-27.2$~mm from the beam center. Figure~\ref{fig:sim-DDNT1-T1-geo} shows the geometry and location of T1 and DDNT1. If the size of T1 were to be increased, it would eventually intersect with DDNT1 at a distance between $25.8$~mm and $27.2$~mm from the beam center, $i.e.$ the edge of the T1 is facing this $1.4$~mm wide low count area. %This is the result of the target's thickness of 1.016 mm and the 45$^{\circ}$ angle of intersection ($1.016\sqrt{2}=1.44$). The edge of the target does not produce many positrons compared to the face of the target. \begin{figure}[htbp] \centering \includegraphics[scale=1]{3-Simulation/Figures/sharp_drop2.eps} \caption{The geometry of the target T1 and the virtual detector DDNT1.} \label{fig:sim-DDNT1-T1-geo} \end{figure} As shown in Figure~\ref{fig:DDNT1_YpY}, the $y$ distribution count decreases at $\theta$ = 45$^{\circ}$. Positrons were emitted from both the downstream and upstream side of T1. Positrons from the downstream side of T1 intersected the detector at angles below 45$^{\circ}$ while positrons from the upstream side of T1 begin to hit the detector at angles beyond 45$^{\circ}$. Neither positrons upstream nor downstream of T1 traveled to the 1.4~mm wide low count area. Only positrons created on the edge of T1 reached the low count area between 25.8~mm~$