Skip to content

psana.psexp.run

The run classes yielded by ds.runs(). Run is the base class with the methods most user code calls: events() (one Event per L1Accept), steps() (one Step per BeginStep), Detector(name, accept_missing=False, **kwargs), and the properties detnames, epicsinfo, scaninfo, xtcinfo. Which subclass you get depends on the data source:

Class Data source
RunSerial SerialDataSource (exp=, one process)
RunSingleFile SingleFileDataSource (files=)
RunShmem ShmemDataSource (shmem=)
RunDrp DrpDataSource (drp=)
RunSmallData created internally (eventbuilder_manager.py, smdreader_manager.py) and passed to smd_callback
RunParallel MPIDataSource; defined in psana.psexp.mpi_ds

Run.__init__ also stores expt, runnum and timestamp (the experiment name, run number and BeginRun timestamp that the data source passes in; the data sources read them from the BeginRun dgrams). They have no docstrings, so they are not listed below. run.run() returns runnum. RunSmallData does not call Run.__init__ and does not set these three attributes.

See DataSource, Run and the event loop and Detector interface.

psana.psexp.run

Run classes returned by the psana data sources.

Run is the base class; RunShmem, RunDrp, RunSingleFile, RunSerial and RunSmallData are the variants for shared memory, the DRP, single files, serial reading and smalldata callbacks. Also defines StepEvent and CallbackRunState.

StepEvent

StepEvent(env_store)

Bases: object

Use by AMI to access

dgrams

dgrams(evt)

Return, for each stream, the last step dgram at or before the timestamp of evt (None if there is none).

Delegates to EnvStore.get_step_dgrams_of_event of the env store given to the constructor.

docstring

docstring(evt) -> str

Return the value of the "step_docstring" env variable for evt, or None if it is not found.

Uses EnvStore.values([evt], "step_docstring"), which looks back up to PS_N_STEP_SEARCH_STEPS (default 10) stored dgrams.

value

value(evt) -> float

Return the value of the "step_value" env variable for evt, or None if it is not found.

Uses EnvStore.values([evt], "step_value"), which looks back up to PS_N_STEP_SEARCH_STEPS (default 10) stored dgrams.

CallbackRunState

CallbackRunState()

Holds callback-side run state that must persist across SMD chunks.

reset_envstore

reset_envstore(configs)

Create a new EnvStoreManager for configs, clear the step state and return the new manager.

Sets in_step to False and current_step_evt to None.

Run

Run(expt, runnum, timestamp, dsparms, dm, smdr_man, begingrun_dgrams)

Bases: object

Base class for a psana run.

The constructor stores the experiment, run number, BeginRun timestamp, dsparms, the DgramManager and the smalldata reader manager, creates a RunCtx and an Event from the BeginRun dgrams, fills detinfo with build_detinfo_dict, and registers the run with RunHelper. Subclasses set configs, the env store manager esm and the iterator _evt_iter that events() and steps() read from.

scannames property

scannames

Set of scan variable names (first element of each key of scaninfo).

detnames property

detnames

Set of detector names in the "normal" detector class table (detectors with a matching detector class).

epicsinfo property

epicsinfo

Dict describing the epics variables, from get_info() of the "epics" env store.

Keys are (variable name, epics name) and values are the epics name ("" if unknown).

scaninfo property

scaninfo

Dict describing the scan variables, from get_info() of the "scan" env store.

Keys are (variable name, algorithm name) and values are the algorithm name.

xtcinfo property

xtcinfo

List of (detector name, detector type, data type name, version) tuples from the Configure dgrams (dsparms.xtc_info).

The version is the version numbers joined with "_".

run

run()

Returns integer representaion of run no. default: (when no run is given) is set to -1

events

events()

Yield the L1Accept events of the run as Event objects.

Other transitions are not yielded but update the env store. Iteration stops at EndRun or when the underlying iterator ends.

steps

steps()

Yield a Step for each BeginStep transition.

Every non-L1Accept transition updates the env store and L1Accepts seen here are skipped; iteration stops at EndRun. Each Step reads its events from this run's shared iterator.

terminate

terminate()

Sets terminate flag

The Events iterators of all Run types check this flag to see if they need to stop (raise StopIterator for RunSerial, RunShmem, & RunSinglefile or skip events for RunParallel (see BigDataNode implementation).

Note that mpi_ds implements this differently by adding Isend prior to setting this flag.

close_shared_memory

close_shared_memory()

Hook for cleaning up shared-memory resources (no-op by default).

Detector

Detector(name, accept_missing=False, **kwargs)

Return the detector interface object for name.

If name is a detector in the "normal" detector class table, the result is a container with one attribute per data type (for example raw), each an instance of the matching detector class created with the detector's config info, calibration constants (an empty dict if none) and **kwargs; the container also gets calibconst, a step attribute when the detector has its own env store, and cached calibration attributes when use_calib_cache is set and the detector is in cached_detectors. Otherwise, if name is an epics or scan variable name, or an epics name that maps to one, the result is a single detector object for that variable built with the env store.

Parameters:

Name Type Description Default
name str

Detector name or env variable name.

required
accept_missing bool

If True, return MissingDet() instead of raising when name is unknown.

False
**kwargs

Passed to the detector class constructors (detector case only).

{}

Raises:

Type Description
KeyError

If name is neither a detector nor an env variable and accept_missing is False.

get_filtered_detinfo

get_filtered_detinfo()

Returns detector info filtered by detector names present in detinfo.

Returns: dict[str, str]: A dictionary mapping detector names to unique IDs, filtered to only include detectors present in self.detinfo.

build_detinfo_dict

build_detinfo_dict()

Creates a dictionary of detector interfaces and their enumerated attributes, using minimal instantiation with config=None, calib=None, env=None.

Returns: dict[tuple[str, str], Any]: Mapping of (detname, xface_name) -> attribute summary

analyze

analyze(event_fn=None, det=None)

Loop over events() and call event_fn(event, det) for each event.

If event_fn is None the events are still read but nothing is called. Returns None.

step

step(evt)

Return an Event made of the step dgrams in effect for evt.

For each stream it takes the last dgram in the "scan" env store with a timestamp at or before evt.timestamp (None if there is none).

RunShmem

RunShmem(expt, runnum, timestamp, dsparms, dm, smdr_man, configs, begingrun_dgrams, **kwargs)

Bases: Run

Yields list of events from a shared memory client (no event building routine).

RunDrp

RunDrp(expt, runnum, timestamp, dsparms, dm, smdr_man, configs, begingrun_dgrams, **kwargs)

Bases: Run

Yields list of events from drp python (no event building routine).

add_detector

add_detector(detdef, algdef, datadef, nodeId=None, namesId=None, segment=None)

Add a detector definition to the dgram being edited and return the result.

Calls curr_dgramedit.Detector(detdef, algdef, datadef, nodeId, namesId, segment); the work is done by the psana.dgramedit extension (not visible here). Only allowed before the run is primed by events() or steps().

Raises:

Type Description
RuntimeError

If event iteration has already started.

add_data

add_data(data)

Add data to the dgram being edited by calling curr_dgramedit.adddata(data), and return its result.

The work is done by the psana.dgramedit extension; not visible here.

remove_data

remove_data(det_name, alg)

Remove the data of det_name / alg from the dgram being edited and return the result.

Calls curr_dgramedit.removedata(det_name, alg) (work done by the psana.dgramedit extension).

Raises:

Type Description
RuntimeError

If event iteration has not started yet.

events

events()

Yield the L1Accept events of the DRP run, saving every dgram to the DRP result shared memory.

On first use the Configure and BeginRun dgrams are saved and configuration edits are frozen. For every dgram a new DgramEdit becomes curr_dgramedit; transitions update the env store and are saved at once (iteration stops at EndRun), while each L1Accept is yielded first, so it can be edited, and saved to dm.shm_res_mv when the caller resumes the iteration.

steps

steps()

Yield a Step for each BeginStep transition of the DRP run, saving transitions to the DRP result shared memory.

Primes the run on first use as events() does. L1Accepts seen here are skipped; every other transition updates the env store and gets a new DgramEdit, a BeginStep is saved after the caller resumes the iteration, other transitions are saved at once, and iteration stops at EndRun.

Notes

Each Step is created with run=self, so its events are saved too.

RunSingleFile

RunSingleFile(expt, runnum, timestamp, dsparms, dm, smdr_man, configs, begingrun_dgrams)

Bases: Run

Yields list of events from a single bigdata file.

RunSerial

RunSerial(expt, runnum, timestamp, dsparms, dm, smdr_man, configs, begingrun_dgrams)

Bases: Run

Yields list of events from multiple smd/bigdata files using single core.

build_table

build_table()

Build timestamp→(fd offset, size) table by scanning SMD. Updates EnvStore for any non-L1 transitions encountered during the scan. Yields True if any L1 timestamps were captured.

event

event(ts)

Read and return the event with timestamp ts, using the table built by build_table.

Each stream's dgram is read with os.pread at the offset and size recorded for ts and wrapped in dgram.Dgram; streams without an entry are None.

Raises:

Type Description
RuntimeError

If build_table has not been used yet.

ValueError

If ts is not in the table.

RunSmallData

RunSmallData(eb, configs, dsparms, callback_run_state=None)

Bases: Run

Yields list of smalldata events

This class is created by SmdReaderManager and used exclusively by EventBuilder. There's no DataSource class associated with it a "bigdata" run is passed in so that we can propagate some values (w/o the ds). This class makes step and event generator available to user in smalldata callback.

steps

steps()

Yield a Step for each BeginStep transition in the event builder output.

If callback_run_state says a step is already in progress (from an earlier chunk), a Step for it is yielded first. Non-L1Accept transitions update the env store (a BeginRun resets it first) and have their proxy event appended to proxy_events; a BeginStep is recorded in callback_run_state, and iteration stops at EndRun.

events

events()

Yield the L1Accept events from the event builder as Event objects with their proxy events.

Transitions are not yielded: they update the env store (a BeginRun resets it first) and their proxy events are appended to proxy_events; iteration stops at EndRun. The events come from a new _iter_events() generator, which reads eb.events() while eb.has_more() is true.