The particles detected in a near Earth orbit could be primary cosmic rays,
trapped radiation, or albedo particles produced in the atmosphere. To under
stand the origin of these particles, we have developed a set of programs to
trace charged particles in the geomagnetic field. The algorithm and the mo
del of the geomagnetic field adopted are described in this article. The num
erical errors are studied in two ways, as a function of either the tracing
time or the gyration step size. An optimal step size of 0.04 rad is selecte
d for further analyses. Using this program, we studied the data obtained by
the Alpha Magnetic Spectrometer and were able to differentiate cosmic rays
from atmospheric secondary particles.