Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: update vlen discussion for Evr::DataV4

...

Although much of the LCLS data fits into the simpler static model of arrays, or structs whose fields are arrays, some of the LCLS data can be quite complex. In particular data written as HDF5 variable length data (vlen data) will show up in cell arrays in Matlab. One example that users may want to work with is the Evr Data. The evr data includes a list of the event codes that fired during an event. As suggested above, contact your experiment POC about any Matlab code that is recommend for this kind of task. Below we provide a simple example for working with the evr data in Matlab. We'll unpack the cell array based data into a flat array. From the flat array, we'll make a logical index array of the events with a certain event code. Then we'll use this to average over cspad for those events. For recent data that uses the Evr::DataV4 type, one doesn't need to do this as the evr event codes will already be translated into such a flattened table called 'present'. For the purposes of demonstrating how to work with vlen data, we will forget this for the time being. One thing to note when using the 'present' table in the hdf5 is that it is a 0-up table, i.e, column 1 is for event code 0. Below we build a 1-up table (since event code 0 should not be used).

Code Block
% we'll read evr data from the first calib cycle of a tutorial file using Matlab's high level read function
evrData = h5read('/reg/d/psdm/xpp/xpptut13/hdf5/xpptut13-r0179.h5','/Configure:0000/Run:0000/CalibCycle:0000/EvrData::DataV3/NoDetector.0:Evr.0/data');

% evrData is a struct with one element - fifoEvents. fifoEvents is a cell array with one entry per event in the CalibCycle
fifoEvents = evrData.fifoEvents;
numberOfEvents = length(fifoEvents);
% numberOfEvents is 483

% let's identify the smallest and largest event code codes in this calib cycle
minEventCode=99999;
maxEventCode=0;
for eventIdx = 1:numberOfEvents; maxEventCode = max(maxEventCode, max(fifoEvents{eventIdx}.eventCode)); end;
for eventIdx = 1:numberOfEvents; minEventCode = min(minEventCode, min(fifoEvents{eventIdx}.eventCode)); end;
assert(minEventCode>0, 'unexpected - minimum event code should be greater than 0. matlab uses 1-up array indexing, 0 will not work with below code');

% lets fill out a flat Array, each row is an event, and column k is 1 only if eventCode k occurred in that event
eventCodesFlat = int8(zeros(numberOfEvents, maxEventCode));
for evtIdx=1:numberOfEvents; eventCode = fifoEvents{evtIdx}.eventCode; for ec=eventCode; eventCodesFlat(evtIdx,ec)=1; end; end;

% here is an example of how you might work with the flat array
numberOfEventsWithEventCode42 = sum(eventCodesFlat(:,42));
% there are 121 events with event code 42
% lets say we want to average over the cspad data for just those 121 events. 
% The below code is easy to write, although it uses quite a bit of memory:

cspad = h5read('/reg/d/psdm/xpp/xpptut13/hdf5/xpptut13-r0179.h5','/Configure:0000/Run:0000/CalibCycle:0000/CsPad::ElementV2/XppGon.0:Cspad.0/data');
% That reads in 2GB - 483 events * 2 bytes per element * 32 * 185 * 388.
% cspad's size is 388   185    32   483

eventsWith42 = eventCodesFlat(:,42)==1;
assert(length(eventsWith42)==length(cspad), 'number of events with cspad != number of events with evr data, even if equal, should do more and check that all entries in time datasets are the same');
 cspadEventCode42 = cspad(:,:,:,eventsWith42);
% size(cspadEventCode42) returns  388   185    32   121

cspadAverageEventCode42 = mean(cspadEventCode42,4);

...