New method for measuring longitudinal fluctuations and directed flow in ultrarelativistic heavy ion reactions