# Contents¶

This notebook covers the basics of creating an event list object and carrying out various operations such as simulating time and energies, joining, storing and retrieving event lists.

# Setup¶

Import some useful libraries.

:

import numpy as np

from matplotlib import pyplot as plt
%matplotlib inline


Import some relevant stingray libraries.

:

from stingray.events import EventList
from stingray.lightcurve import Lightcurve


## Creating EventList from Photon Arrival Times¶

Given photon arrival times, an eventlist object can be created.

:

times = [0.5, 1.5, 2.5, 3.5]


Create event list object by passing arrival times as argument.

:

ev = EventList(times)
ev.time

:

array([ 0.5,  1.5,  2.5,  3.5], dtype=float128)


Event lists can be loaded from light curve, where the standard followed is as follows: as many events are created as the counts in the lightcurve at the time specified by time bins.

To demonstrate this, define a light curve.

:

times = np.arange(3)
counts = np.floor(np.random.rand(3)*5)
lc = Lightcurve(times, counts)

:

lc.time, lc.counts

:

(array([0, 1, 2]), array([ 0.,  2.,  4.]))


Now, eventlist can be loaded by calling static from_lc() method.

:

ev = EventList.from_lc(lc)
ev.time

:

array([ 1.0,  1.0,  2.0,  2.0,  2.0,  2.0], dtype=float128)


## Simulating EventList from Lightcurve¶

Alternatively, event list can be simulated from light curve using acceptance rejection method.

:

ev = EventList()
ev.simulate_times(lc)
ev.time

:

array([ 0.88000835,  1.062921,  1.5550138,  1.6221689,  1.7120616,
1.7332546,  1.9129681,  2.3186986,  2.4757699], dtype=float128)


:

ev = EventList()
ev.time[1:10]

:

array([ 80000353.0,  80000056.0,  80000067.0,  80000637.0,  80000778.0,
80000159.0,  80000631.0,  80000563.0,  80000817.0], dtype=float128)


## Recovering Lightcurve from EventList¶

After simulating event list, the original light curve can be recovered. Let’s demonstrate by creating a light curve.

:

times = np.arange(50)
counts = np.floor(np.random.rand(50)*50000)
lc = Lightcurve(times, counts)


Simulate an event list.

:

ev = EventList()
ev.simulate_times(lc)


Recover original light curve curve using to_lc() method. Here, dt defines time resolution, tstart the starting time, and tseg the total time duration.

:

lc_new = ev.to_lc(dt=1, tstart=-0.5, tseg=50)

:

plt.plot(lc.counts,'r-', lc_new.counts, 'g-')
plt.xlabel('Times')
plt.ylabel('Counts')

:

<matplotlib.text.Text at 0x11522c950> As can be seen from the figure above, the recovered light curve is aligned with the original light curve.

## Simulating Energies¶

In order to simulate energies, a spectral distribution needs to be passed.

:

spectrum = [[1, 2, 3, 4, 5, 6],[1000, 2040, 1000, 3000, 4020, 2070]]

:

ev = EventList(ncounts=10)
ev.simulate_energies(spectrum)


## Joining EventLists¶

Two event lists can also be joined together.

:

ev1 = EventList(time=[1,2,3])

:

ev2 = EventList(time=[4,5])

:

ev = ev1.join(ev2)
ev.time

:

array([ 1.0,  2.0,  3.0,  4.0,  5.0], dtype=float128)


## IO¶

IO in all major formats including pickle, hdf5, fits and ascii is supported.

Below is an example of retrieving event list from a fits file.

:

ev = EventList()
ev.time[1:10]

:

array([ 80000353.0,  80000056.0,  80000067.0,  80000637.0,  80000778.0,
80000159.0,  80000631.0,  80000563.0,  80000817.0], dtype=float128)


Alternatively, we can used hdf5 to store and later retrieve data.

:

ev.write('ev.hdf5', 'hdf5')

:

array([ 80000353.0,  80000056.0,  80000067.0,  80000637.0,  80000778.0,