The problem with event detection is caused by the minimum value in the second derivative occuring before the event. This should be easily fixable - just limit the minimum swearch to values indexed after the first derivative.
The program can go through all force curves without crashing. There are not longer any force curves which don't think they hit thew surface. The problem now is early detection. It doesn't happen in many cases, but I might be able to do something similar. If the right limit is larger than the length of D2zdf2, then it's clearly an outlier. There must be something else it needs to be shorter than.