Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hit indices #198

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/dxtbx/format/FormatXTC.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import functools
import sys
import time
from itertools import groupby

import numpy as np
import serialtbx.detector.xtc
Expand Down Expand Up @@ -32,11 +33,15 @@
psana = None

locator_str = """
hits_file = None
.type = str
.help = path to a file where each line is 2 numbers separated by a space, a run index, and an event index in the XTC stream
experiment = None
.type = str
.help = Experiment identifier, e.g. mfxo1916
run = None
.type = ints
.type = int
.multiple = True
.help = Run number or a list of runs to process
mode = idx
.type = str
Expand Down Expand Up @@ -147,6 +152,7 @@ def __init__(self, image_file, **kwargs):

self._ds = FormatXTC._get_datasource(image_file, self.params)
self._evr = None
self._load_hit_indices()
self.populate_events()

self._cached_psana_detectors = {}
Expand All @@ -162,6 +168,17 @@ def __init__(self, image_file, **kwargs):
else:
self._spectrum_pedestal = None

def _load_hit_indices(self):
self._hit_inds = None
if self.params.hits_file is not None:
assert self.params.mode == "idx"
hits = np.loadtxt(self.params.hits_file, int)
hits = list(map(tuple, hits))
key = lambda x: x[0]
gb = groupby(sorted(hits, key=key), key=key)
# dictionary where key is run number, and vals are indices of hits
self._hit_inds = {r:[ind for _,ind in group] for r,group in gb}

@staticmethod
def understand(image_file):
"""Extracts the datasource and detector_address from the image_file and then feeds it to PSANA
Expand Down Expand Up @@ -229,18 +246,27 @@ def populate_events(self):
self.run_mapping = {}

if self.params.mode == "idx":
for run in self._psana_runs.values():
for run_num, run in self._psana_runs.items():
times = run.times()
if self._hit_inds is not None and run_num not in self._hit_inds:
continue
if self._hit_inds is not None and run_num in self._hit_inds:
temp = []
for i_hit in self._hit_inds[run_num]:
temp.append( times[i_hit] )
times = tuple(temp)
if (
self.params.filter.required_present_codes
or self.params.filter.required_absent_codes
) and self.params.filter.pre_filter:
times = [t for t in times if self.filter_event(run.event(t))]
self.run_mapping[run.run()] = (

self.run_mapping[run_num] = (
len(self.times),
len(self.times) + len(times),
run,
)

self.times.extend(times)
self.n_images = len(self.times)

Expand Down
Loading