## 3 Code Functionality
For this reason, as well as the thought that there may be datasets in which the photopeak is wrongly pinpointed, the code was thus built in with a manual override, giving the user the option to select a photopeak time by hand when necessary due to the dataset limitations. This can prove useful in situations such as the above, in which the photopeak cannot be seen on the individual set, but its location can be inferred from other sets taken under the same conditions. To find the beginning of the data curve and thus the maximum proton energy, the gradient was calculated for every point using least squares regression in the range of r points to the left and right of that datapoint. This represents a series of points by the calculated best fit line derived from [19]:  
\[m=\frac{NS_{xy}-S_{x}S_{y}}{NS_{xx}-S_{x}^{2}} S_{x}\equiv\Sigma_{i}x_{i} \tag{2}\] \[S_{y}\equiv\Sigma_{i}y_{i}\]  
\[c=\frac{S_{x}S_{y}-S_{x}S_{y}}{NS_{xx}-S_{x}^{2}} S_{xy}\equiv\Sigma_{i}x_{i}y_{i} \tag{3}\] \[S_{xx}\equiv\Sigma_{i}x_{i}^{2}\]  
where \(x\) and \(y\) are the time and signal values, respectively, \(m\) is the gradient of the best fit line, \(c\) is the point of \(Y\)-axis intersection, all forms of S are self-defined above, and \(N\) is the total datapoint count \(2r+1\). The value \(r\), named the range value, was initially arbitrarily assigned, however it was later refined using basic linear regression methods to determine a simple formula for the calculation of an appropriate range value for a new dataset. This process will be covered later in this section. The process to calculate the local gradient about a point (using Equation (2)) was repeated for every point within the defined point range. From this, a graph of signal change against time can be plotted (Figure 4) which, for a sufficiently large range value, will have greatly reduced noise in comparison to the initial TOF graph (Figure 3). From this relatively noiseless graph, the rising edge can be accurately pinpointed algorithmically based on fluctuations of its own gradient, acting as an accurate prediction of the time-of-arrival of the maximum energy protons. A secondary method which calculated a moving average in place of a gradient calculation was also designed, however later testing (see Section 4) showed a consistently lower accuracy and larger deviation from the value that was gathered by eye for this method and was, therefore, cut from this paper for the sake of brevity. This raises a key point in the development of this code, however. A small range value will result in a gradient chart with a lesser reduced noise, whereas a large range value will result in over-smoothing of the data and a significant shifting of the curve's rising edge, thus losing accuracy. The system's ability to accurately determine an estimation for maximum proton energy is, therefore, dependent on a procedural range value which must differ for every dataset depending on dataset size and S/N ratio. While the optimal range value (i.e., the range value which results in the predicted maximum proton energy that is closest to the manually chosen reference value) is unique for each dataset and cannot be predicted specifically (again, due to dataset noise), we can use trends in pre-existing data and basic linear regression techniques to notice trends and patterns that will allow an accurate prediction. A separate, secondary code was developed which calculates an estimate for the beginning of the TOF curve for a given dataset for a series of different range values. For each range value, the calculated prediction for the time-of-arrival is found and compared against the reference answer, a value determined manually by eye which is estimated to be the point at which the curve begins. Throughcomparison to the reference value, the range value which gives the most accurate estimation of the minimum TOF for this dataset can be found.