You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »


Introduction

This page collects information about Python-based analysis framework for LCLS called pyana. This framework design borrows heavily from the various sources such as Online Analysis (a.k.a. myana), BaBar framework, etc. It's main principles are summarized here:

  • oriented on XTC processing, but could be extended to work with HDF5 data
  • should be easy to use and extend for end users
  • support re-use of the existing analysis code
  • allow parallel processing on multi-core systems
  • common simple configuration of user analysis code

Framework composition

The centerpiece of the framework is a regular Python application (pyana) which can load one or more user analysis modules which are also written in Python. The core application is responsible for the following tasks:

  • loading and initializing all user modules
  • reading XTC data from a list of input files
  • calling appropriate user methods based on the data being processed
  • providing data access to user modules
  • providing other services such as histogramming to user modules

User modules

User analysis module is a regular Python module (a Python file) which satisfies additional requirements:

  • it contains a class with the same name as the module name
  • the class defines constructor method with optional arguments and five regular methods: beginjob(), beginrun(), event(), endrun(), and endjob().

The application loads one or more user modules, the names of the modules to load are specified either in the job configuration file or on the command line. After loading the modules the application creates one or more instance objects of the class defined in the module. More than one instance may be useful if one wants to run the same analysis with different set of parameters in the same job. Number of instances and their parameters are determined by the job configuration file (see below).

Initialization

User analysis class can define zero or more parameters in its constructor (__init__() method). Parameters are initialized from the values defined in the job configuration file (see below). All parameters are passed to the Python code as strings, if the code expects a number or some other type then it's the code responsibility to convert the strings to appropriate type. If there are no default values defined for some parameters in constructor declaration then those parameters must be present in the configuration file.

For quick example suppose that we have this class defined in user module:

mypackage/src/myana.py
# user analysis class
class myana(object):
    def __init__(self, name, lower, uppper, bins=100)
        self.lower = float(lower)
        self.upper = float(upper)
        self.bins = int(bins)
    ...

and this job configuration file:

pyana.cfg
[pyana]
modules = mypackage.myana mypackage.myana:wide

[mypackage.myana]
lower = 0
upper = 100
name = default

[mypackage.myana:wide]
lower = 0
upper = 1000
bins = 1000
name = wide

With this the analysis job will instantiate two analysis objects with different parameters, equivalent to this pseudo-code:

# import class myana
from mypackage.myana import myana

# create instances
instances = [ myana(lower = "0", upper = "100", name = "default"),
    myana(lower = "0", upper = "1000", bins = "1000", name = "wide") ]

(the order of parameters in constructor and configuration file does not matter as all parameters are passed as keyword parameters.)

Data access

There are two types of data that framework passes to the user analysis modules – event data and environment data. Event data contains the data corresponding to current event that triggered the call to the user methods. In case of XTC input the event data contains complete datagram as read from DAQ. Event data in user module is represented with a special object of type pyana.event.Event which has an extended interface for extracting individual object from datagram. This interface is described in the reference guide.

Environment data include all kinds of data which are not part of the event data. Usually environment data either stay the same for the whole job or change at a slower rate than event data. Example of the environment data could be configuration data read from XTC at the beginning of the job, EPICS data which is not updated on every event, and few other things. Environment data is represented for user code through the object of type pyana.event.Env. Its interface is described in the reference guide.

Data Source Address

For some pieces of data one needs to specify data "address" which identifies (maybe partially) particular DAQ device which produced the data. This is needed because the instrument setup may include multiple devices producing the same data type. The DAQ defines a type which serves as a most specific device identification, the type is xtc.DetInfo in package pypdsdata. One can pass this DetInfo instance to a method which accepts device address to select that specific device. DetInfo object contains four essential pieces of information:

  • detector – one of the DetInfo.Detector.* values
  • detId – ID number selecting one of multiple detectors
  • device – one of the DetInfo.Device.* values
  • devId – ID number selecting one of multiple devices
    One can build DetInfo out of these four values, but DetInfo constructor (which mimics C++ behavior) needs also a nuisance processId parameter. In most cases using the address string described below is preferred to manually building DetInfo objects.

In many cases the DetInfo object is not known to the user or the code uses only partial information to identify one or many data sources. In such cases it is easier to use address string as an argument to methods accepting address parameter. The most generic format of address string is:

[detector][-[detId]]["|"[device][-[devId]]]

In words address string is an optional detector name followed by optional dash and detector ID separated by vertical bar from optional device name followed by optional dash and device ID. Detector name and device name are the names defined in DetInfo.Detector and DetInfo.Device enums respectively. Examples of detector names are AmoGasdet, AmoETof, Camp, etc. Examples of device names are Acqiris, pnCCD, Evr, etc. For complete list check the Reference Manual.

Here are few example address strings:

  • "AmoETof-0|Acqiris-0" – selects data produced by detector AmoETof, detId 0, device Acqiris, devId 0
  • "AmoETof|Acqiris" – selects data produced by detector AmoETof, any detId, device Acqiris, any devId
  • "AmoETof-0" – selects data produced by detector AmoETof, detId 0, any device, any devId
  • "|Acqiris-0" – selects data produced by any detector, any detId, device Acqiris, devId 0

Configuration

Multi-processing

The framework can be run in single-process or multi-process mode, by default everything runs in singe-process mode. In multi-process mode analysis job spawns a number of processes all running on the same host. In that case framework is also responsible for distributing individual events to a single or multiple processes and collecting and merging the results of the processing at the end of the job.

  • No labels