DataSource, Run and the event loop¶
What it is¶
Every psana2 analysis follows the same pattern: create a DataSource, loop
over its runs, get detector objects from the run, and loop over the events of
the run (optionally grouped in steps of a scan):
DataSource(...) -> data source object (serial, single file, MPI, shmem, ...)
.runs() -> Run objects, one per run
run.Detector(name) -> detector interface (see "Detector interface")
run.events() -> Event objects (L1Accept only)
run.steps() -> Step objects; step.events() -> Event objects
The same user code runs serially, under MPI, or on shared memory; the choice is
made by DataSource from its keyword arguments and the environment.
Key concepts¶
DataSource(**kwargs)¶
A factory function in psana/psana/datasource.py (exported as
psana.DataSource). It checks the keywords shmem, exp, files, drp in
that order and returns the matching class (details on the
psana.datasource API page):
| You pass | You get | Typical use |
|---|---|---|
exp=, run= (optionally dir=) |
SerialDataSource, or under MPI MPIDataSource (see MPI parallel analysis) |
offline analysis of a run |
files= |
SingleFileDataSource |
one xtc2 file, no small data needed |
shmem= |
ShmemDataSource (see Shared memory and live analysis) |
monitoring a running DAQ |
drp= |
DrpDataSource |
Python code running inside the DAQ's DRP |
All of them derive from DataSourceBase (psana/psana/psexp/ds_base.py),
which reads the keyword arguments. Commonly used ones, with the defaults read
in DataSourceBase.__init__:
| Keyword | Default | Meaning (from the code) |
|---|---|---|
exp |
None |
Experiment name. |
run |
None |
Run number or list of run numbers; None means every run found in the xtc directory. |
dir |
None |
xtc directory. If not given: $SIT_PSDM_DATA/<exp[:3]>/<exp>/xtc (default SIT_PSDM_DATA is /reg/d/psdm). |
files |
None |
xtc2 file name (or list) for SingleFileDataSource. |
max_events |
0 |
Stop after this many events (0 = no limit). |
batch_size |
1000 |
Number of events per batch passed between MPI ranks. |
detectors / xdetectors |
[] |
Keep / drop the stream files of these detectors. |
small_xtc |
[] |
Detectors whose data are read from the big-data file instead of the small-data file. |
timestamps |
empty array | Only process these event timestamps (array or .npy path). |
live |
False |
Wait for files that are still being written (see Shared memory and live analysis). |
skip_calib_load |
[] |
Detector names (or "all") whose calibration constants are not fetched. |
log_level |
logging.INFO |
Logging level (a level name string is accepted). |
Unknown keywords are not an error: DataSourceBase logs
"Unrecognized kwarg" and ignores them. Some tests still pass a filter=
keyword, which is not in the list of known keywords and therefore has no
effect.
For exp= the data source expects the usual LCLS layout: big-data files
<exp>-r<run>-s<stream>-c<chunk>.xtc2 in the xtc directory and small-data
files smalldata/<exp>-r<run>-s<stream>-c<chunk>.smd.xtc2 next to them (see
XTC2 data format).
Run¶
psana/psana/psexp/run.py. The base class Run has the subclasses
RunSerial, RunSingleFile, RunShmem, RunDrp, RunSmallData, and
RunParallel (in psana/psana/psexp/mpi_ds.py). What user code typically uses:
run.expt,run.runnum,run.timestamp: taken from the BeginRun transition in the data (so they are also set forfiles=, if the file has that information).run.events(): yields anEventfor each L1Accept. Other transitions (SlowUpdate, BeginStep, ...) are consumed internally and update the EPICS and scan values; the loop ends at EndRun.run.steps(): yields aStepfor each BeginStep;step.events()yields the L1Accepts until the matching EndStep.run.Detector(name, accept_missing=False, **kwargs): see Detector interface.run.detnames,run.epicsinfo,run.scaninfo,run.xtcinfo: what is in the run.- Random access by timestamp:
RunSerialandRunParallelhave abuild_table()context manager andevent(ts); seepsana/psana/tests/test_run_build_table.py.
Event and Step¶
psana/psana/event.py and psana/psana/psexp/step.py.
evt.timestamp: 64-bit integer; upper 32 bits are seconds, lower 32 bits nanoseconds.evt.datetime(): the timestamp as a naivedatetime(the epoch used by the code is 1990-01-01).evt.run(): returns a smallRunCtxobject (psana/psana/psexp/run_ctx.py) with onlyexpt,runnum,timestamp,intg_det, not the fullRun.evt.service(): the transition id of the event.step.evt: the BeginStep event; scan variables can be read from it with a scan detector, for examplerun.Detector('motor2').
Minimal example¶
Adapted from psana/psana/tests/user_loops.py. xpptut15 run 14 is a
synthetic data set that the tests generate with the xtcwriter and smdwriter
tools; Synthetic test data shows
how to write it into .tmp with psana/psana/tests/setup_input_files.py.
With real LCLS data you would use your experiment name and run number and
usually omit dir.
from psana import DataSource
xtc_dir = ".tmp" # written by setup_input_files.py (see Getting started)
ds = DataSource(exp="xpptut15", run=14, dir=xtc_dir)
for run in ds.runs():
print(run.expt, run.runnum)
det = run.Detector("xppcspad") # area detector
edet = run.Detector("HX2:DVD:GCC:01:PMON") # EPICS variable
sdet = run.Detector("motor2") # scan variable
for step in run.steps():
for evt in step.events():
calib = det.raw.calib(evt)
print(evt.timestamp, sdet(evt), edet(evt), calib.shape)
Without steps, iterate run.events() directly. Reading one file:
DataSource(files=".tmp/xpptut15-r0014-s000-c000.xtc2") works the same way but
only sees the detectors stored in that file.
Where in the code¶
psana/psana/datasource.py:DataSource,InvalidDataSource.psana/psana/psexp/ds_base.py:DataSourceBase(keyword arguments, file discovery,smalldata()).psana/psana/psexp/serial_ds.py,singlefile_ds.py,shmem_ds.py,drp_ds.py,null_ds.py,mpi_ds.py: the data source classes.psana/psana/psexp/run.py:Runand its subclasses.psana/psana/psexp/step.py,psana/psana/event.py,psana/psana/psexp/run_ctx.py:Step,Event,RunCtx.psana/psana/dgrammanager.py:DgramManager(reads dgrams and Configure data).psana/psana/psexp/envstore.py,envstore_manager.py: EPICS and scan values.- Examples:
psana/psana/tests/user_loops.py,psana/psana/tests/ds.py.
API pages: psana.datasource, psana.psexp.ds_base, psana.psexp.run, psana.event, psana.dgrammanager.