Skip to content

calib

Tools for mapping sources and groups to a detector type.

The current implementation tries to mimic the PSCalib source code from CalibFileFinder.py (https://github.com/lcls-psana/PSCalib/blob/master/src/CalibFileFinder.py).

Functions:

Name Description
group_from_det_type

str) -> str: Retrieve the group string

source_from_det_info

str, hutch: str) -> str: Retrieve the source string

Exceptions:

group_from_det_type(det_type)

Retrieve the group string from the detector type.

Source code in lute/io/calib.py
187
188
189
190
191
192
193
194
def group_from_det_type(det_type: str) -> Optional[str]:
    """Retrieve the group string from the detector type."""
    det_type_lower = det_type.lower()
    det_name = psana_to_calib_det_name.get(det_type_lower, "UNDEFINED")
    if det_name == "UNDEFINED":
        raise ValueError(f"Unknown detector type: {det_type}")
    group = det_to_group.get(det_name)
    return group

select_calib_file(calib_dir, run)

Select the calibration file from the calibration directory and run number.

Source code in lute/io/calib.py
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
def select_calib_file(calib_dir: str, run: int) -> Optional[str]:
    """Select the calibration file from the calibration directory and run number."""
    fnames = os.listdir(calib_dir)
    files = [os.path.join(calib_dir, fname) for fname in fnames]

    run_max = 9999
    run_files = []
    for file in files:
        f = os.path.basename(file)
        if f == "HISTORY":
            continue
        if os.path.splitext(f)[1] != ".data":
            continue
        basename = os.path.splitext(f)[0]
        fields = basename.split("-")
        begin, end = fields

        if begin.isdigit():
            begin_int = int(begin)
            if begin_int >= run_max:
                raise ValueError(
                    f"Begin run number {run} is too high for calibration directory {calib_dir}"
                )

        if end.isdigit():
            end_int = int(end)
            if end_int >= run_max:
                raise ValueError(
                    f"End run number {run} is too high for calibration directory {calib_dir}"
                )
        elif end == "end":
            end_int = run_max

        run_files.append((begin_int, end_int, file))
    run_files.sort(key=lambda x: int(x[0]))

    for run_file in run_files[::-1]:
        if run_file[0] <= run <= run_file[1]:
            return run_file[2]

    return ""

source_from_det_info(det_type, hutch)

Retrieve the source string from the detector type and hutch.

Source code in lute/io/calib.py
197
198
199
200
201
202
203
204
205
206
207
208
209
def source_from_det_info(det_type: str, hutch: str) -> Optional[str]:
    """Retrieve the source string from the detector type and hutch."""
    hutch_upper = hutch.upper()
    station = hutch_to_station.get(hutch_upper, "UNDEFINED")
    if station == "UNDEFINED":
        raise ValueError(f"Unknown hutch: {hutch}")
    det_type_lower = det_type.lower()
    det_name = psana_to_calib_det_name.get(det_type_lower, "UNDEFINED")
    if det_name == "UNDEFINED":
        raise ValueError(f"Unknown detector type: {det_type}")
    if "Epix10kaQuad" in det_name:
        return f"{station}:{det_name}"
    return f"{station}:{det_name}.0"