private double[] TrimGates(double startTime, double endTime)
{
// check for swapped gates
if (startTime > endTime) return null;
// is the gate region null, or entirely outside the TOF?
int gateEndTime = gateStartTime + (Length - 1) * clockPeriod;
if (startTime == endTime) return null;
if (startTime > gateEndTime) return null;
if (endTime < gateStartTime) return null;
// trim the gates
if (endTime > gateEndTime) endTime = gateEndTime;
if (startTime < gateStartTime) startTime = gateStartTime;
// it's now possible for the trimmed gate region to be null (!) so check again
if (startTime == endTime) return null;
return new double[] { startTime, endTime };
}