Skip to content

psana.dgrammanager

DgramManager reads dgrams from xtc2 files, from a shared-memory server (["shmem"], through PyShmemClient and a background reader thread) or from the DRP (["drp"]). It keeps the Configure dgrams and builds the detector class tables used by run.Detector (_setup_det_class_table). The data sources create it; user code rarely needs it directly. The module also has a small command-line test (main) that prints the contents of an xtc2 file.

See XTC2 data format and Shared memory and live analysis.

psana.dgrammanager

Define DgramManager, which reads dgrams from xtc2 files, a shared-memory (shmem) client or the DRP shared-memory/message-queue interface, and keeps the current Configure dgrams.

Also contains dgram dump helpers and a small command-line test (main).

DgramManager

DgramManager(xtc_files, configs=[], fds=[], tag=None, max_retries=0, config_consumers=[])

Bases: object

Read dgrams from xtc2 files, shmem or the DRP, and hold the current Configure dgrams.

xtc_files can be one file name, a list or array of file names (opened read-only unless fds is given), ["shmem"] (connects a PyShmemClient with tag and starts a background reader thread) or ["drp"] (uses the shared memory and message queues described by tag). The Configure dgrams are read from the sources unless configs is given; set_configs then builds the detector class tables and per-detector config info and sets them on this object and on every object in config_consumers.

Parameters:

Name Type Description Default
xtc_files (str, list or ndarray)

Data sources, as described above.

required
configs list

Configure dgrams to use instead of reading them.

[]
fds list

Already-open file descriptors, one per file; they are not closed by close.

[]
tag optional

Shmem client tag, or for the DRP an object carrying the DRP connection details.

None
max_retries int

Passed to every dgram.Dgram file read.

0
config_consumers list

Objects that also receive the attributes made by set_configs.

[]

Opens xtc_files and stores configs. If file descriptors (fds) is given, reuse the given file descriptors.

set_configs

set_configs(dgrams)

Save and setup given dgrams class configs.

set_chunk_ids

set_chunk_ids()

Generates a list of chunk ids for all stream files

Chunk Id is extracted from data file name: New format: xpptut15-r0001-s000-c000[.smd].xtc2 Old format: xpptut15-r0001-s00-c00[.smd].xtc2

get_chunk_id

get_chunk_id(ind)

Return the chunk id of file index ind, or None if no chunk ids were found.

Chunk ids are parsed from the file names by set_chunk_ids.

set_chunk_id

set_chunk_id(ind, new_chunk_id)

Set the chunk id stored for file index ind to new_chunk_id.

close

close()

Close the file descriptors opened by this object.

Does nothing if the descriptors were passed in through fds.

close_reader

close_reader()

Close the reader thread (if present).

This is intended to allow breaking of the infinite reading loop. Currently it is used for the ability of tests to work on non-infinite sources of data. I.e. there is not a good reason to use this outside in production.

This method does nothing if you are not operating on shared memory, i.e., not using the DgramManager as part of a ShmemDataSource.

If needed, a more robust solution will be created in the future.

jumps

jumps(dgram_i, offset, size)

Read one dgram of size bytes at offset from file index dgram_i.

The read uses that file's descriptor and Configure dgram and is done by the C extension psana.dgram (dgram.Dgram).

Returns:

Type Description
Dgram or None

The dgram, or None if both offset and size are 0 or the read raises StopIteration.

jump

jump(offsets, sizes)

Jumps to the offset and reads out dgram on each xtc file. This is used in normal mode (multiple detectors with MPI).

get_timestamps

get_timestamps()

Return the timestamps of the dgram lists returned so far by iteration, as a numpy uint64 array.

dumpDict

dumpDict(dict, indent)

Print the keys of dict in sorted order, recursing into values that have a __dict__.

Each line is indented by indent spaces (each nested level adds 2); leaf values are printed after their key.

dumpDgram

dumpDgram(d)

Print the attribute tree of dgram d to stdout by calling dumpDict(d.__dict__, 0).

dgSize

dgSize(view)

Return the total size in bytes of the dgram at the start of buffer view.

Computed as 12 (the size of XtcData::TransitionBase, per the code comment) plus the uint32 extent field at uint32 index 5 of the buffer, so it depends on the XtcData::Dgram layout.

parse_command_line

parse_command_line()

Parse sys.argv for the command-line test main.

Accepts -h (calls usage_error, which exits), -f , and -v and -d , which are accepted but ignored.

Returns:

Type Description
tuple

(remaining positional arguments, xtc file name); the file name defaults to "data.xtc".

getMemUsage

getMemUsage()

Return the memory size of this process as reported by ps.

Runs /usr/bin/ps -q <pid> --no-headers -eo size with os.popen and returns its output as an int, in the units ps reports for size.

main

main()

Command-line test that prints the contents of an xtc2 file.

Opens the file given by -f (default "data.xtc") with DgramManager, prints the manager's attributes, then for each event prints the attribute names and types of every dgram. It also tries to write into dgram.xpphsd.raw.array0Pgp and prints whether that array is read-only, so it expects every dgram to have an xpphsd detector.

usage_error

usage_error()

Write the usage text to stdout and exit with status 1.