Skip to content

Commit

Permalink
Rething the beam center calculation and remove messy DetectorParams
Browse files Browse the repository at this point in the history
  • Loading branch information
noemifrisina committed Jan 10, 2025
1 parent 1380f97 commit 4dc9243
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 82 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ dependencies = [
"ophyd == 1.9.0",
"ophyd-async >= 0.8a5",
"bluesky >= 1.13.0a4",
"dls-dodal @ git+https://github.com/DiamondLightSource/dodal.git@e6ad77ee5aa489d0035918e45a28a0d6f72b19ed",
"dls-dodal @ git+https://github.com/DiamondLightSource/dodal.git@076d23f5e2902b7775bda37d221a04557f09bd9d",
]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
log_on_entry,
)
from mx_bluesky.beamlines.i24.serial.parameters import ExtruderParameters
from mx_bluesky.beamlines.i24.serial.parameters.constants import BEAM_CENTER_LUT_FILES
from mx_bluesky.beamlines.i24.serial.setup_beamline import Pilatus, caget, caput, pv
from mx_bluesky.beamlines.i24.serial.setup_beamline import setup_beamline as sup
from mx_bluesky.beamlines.i24.serial.setup_beamline.setup_detector import (
Expand Down Expand Up @@ -206,10 +207,14 @@ def main_extruder_plan(
dcid: DCID,
start_time: datetime,
) -> MsgGenerator:
beam_center_pixels = sup.compute_beam_center_position_from_lut(
BEAM_CENTER_LUT_FILES[parameters.detector_name],
parameters.detector_distance_mm,
parameters.detector_size_constants,
)
yield from sup.set_detector_beam_center_plan(
beam_center_device,
parameters.detector_params,
parameters.detector_distance_mm,
beam_center_pixels,
)

# Setting up the beamline
Expand Down Expand Up @@ -283,7 +288,6 @@ def main_extruder_plan(
SSX_LOGGER.info("Using Eiger detector")

SSX_LOGGER.debug(f"Creating the directory for the collection in {filepath}.")
# NOTE Directory now created by the parameter model

caput(pv.eiger_seqID, int(caget(pv.eiger_seqID)) + 1)
SSX_LOGGER.info(f"Eiger quickshot setup: filepath {filepath}")
Expand Down Expand Up @@ -490,6 +494,8 @@ def run_extruder_plan(
parameters: ExtruderParameters = yield from read_parameters(
detector_stage, attenuator
)
# Create collection directory
parameters.collection_directory.mkdir(parents=True, exist_ok=True)

beam_center_device = sup.get_beam_center_device(parameters.detector_name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
)
from mx_bluesky.beamlines.i24.serial.log import SSX_LOGGER, log_on_entry
from mx_bluesky.beamlines.i24.serial.parameters import FixedTargetParameters
from mx_bluesky.beamlines.i24.serial.parameters.constants import BEAM_CENTER_LUT_FILES
from mx_bluesky.beamlines.i24.serial.setup_beamline import caget, cagetstring, caput, pv
from mx_bluesky.beamlines.i24.serial.setup_beamline import setup_beamline as sup
from mx_bluesky.beamlines.i24.serial.setup_beamline.setup_zebra_plans import (
Expand Down Expand Up @@ -410,7 +411,6 @@ def start_i24(
SSX_LOGGER.info("Using Eiger detector")

SSX_LOGGER.debug(f"Creating the directory for the collection in {filepath}.")
# NOTE Directory now created by the parameter model

SSX_LOGGER.info(f"Triggered Eiger setup: filepath {filepath}")
SSX_LOGGER.info(f"Triggered Eiger setup: filename {filename}")
Expand Down Expand Up @@ -547,10 +547,14 @@ def main_fixed_target_plan(
) -> MsgGenerator:
SSX_LOGGER.info("Running a chip collection on I24")

beam_center_pixels = sup.compute_beam_center_position_from_lut(
BEAM_CENTER_LUT_FILES[parameters.detector_name],
parameters.detector_distance_mm,
parameters.detector_size_constants,
)
yield from sup.set_detector_beam_center_plan(
beam_center_device,
parameters.detector_params,
parameters.detector_distance_mm,
beam_center_pixels,
)

SSX_LOGGER.info("Getting Program Dictionary")
Expand Down Expand Up @@ -699,6 +703,9 @@ def run_fixed_target_plan(
detector_stage, attenuator
)

# Create collection directory
parameters.collection_directory.mkdir(parents=True, exist_ok=True)

if parameters.chip_map:
upload_chip_map_to_geobrick(pmac, parameters.chip_map)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
from pathlib import Path

import numpy as np
from dodal.devices.detector import DetectorParams, TriggerMode
from dodal.devices.detector.det_dim_constants import EIGER2_X_9M_SIZE, PILATUS_6M_SIZE
from dodal.devices.detector.det_dim_constants import (
EIGER2_X_9M_SIZE,
PILATUS_6M_SIZE,
DetectorSizeConstants,
)
from pydantic import BaseModel, ConfigDict, computed_field, field_validator

from mx_bluesky.beamlines.i24.serial.fixed_target.ft_utils import (
Expand All @@ -13,7 +16,6 @@
PumpProbeSetting,
)
from mx_bluesky.beamlines.i24.serial.parameters.constants import (
BEAM_CENTER_LUT_FILES,
DetectorName,
SSXType,
)
Expand All @@ -40,9 +42,16 @@ def _parse_visit(cls, visit: str | Path):
@property
def collection_directory(self) -> Path:
directory = Path(self.visit) / self.directory
directory.mkdir(parents=True, exist_ok=True)
return directory

@property
def detector_size_constants(self) -> DetectorSizeConstants:
return (
EIGER2_X_9M_SIZE
if self.detector_name is DetectorName.EIGER
else PILATUS_6M_SIZE
)


class LaserExperiment(BaseModel):
"""Laser settings for pump probe serial collections."""
Expand All @@ -69,10 +78,6 @@ def nexgen_experiment_type(self) -> str:
def ispyb_experiment_type(self) -> SSXType:
pass

@property
@abstractmethod
def detector_params(self) -> DetectorParams: ...


class ExtruderParameters(SerialAndLaserExperiment):
"""Extruder parameter model."""
Expand All @@ -88,32 +93,6 @@ def nexgen_experiment_type(self) -> str:
def ispyb_experiment_type(self) -> SSXType:
return SSXType.EXTRUDER

@property
def detector_params(self):
det_dist_to_beam_lut = BEAM_CENTER_LUT_FILES[self.detector_name]
det_size_constants = (
EIGER2_X_9M_SIZE
if self.detector_name is DetectorName.EIGER
else PILATUS_6M_SIZE
)

self.collection_directory.mkdir(parents=True, exist_ok=True)

return DetectorParams(
detector_size_constants=det_size_constants,
exposure_time=self.exposure_time_s,
directory=self.collection_directory.as_posix(),
prefix=self.filename,
detector_distance=self.detector_distance_mm,
omega_start=0.0,
omega_increment=0.0,
num_images_per_trigger=1,
num_triggers=self.num_images,
det_dist_to_beam_converter_path=det_dist_to_beam_lut.as_posix(),
use_roi_mode=False, # Dasabled
trigger_mode=TriggerMode.SET_FRAMES, # For now...
)


class ChipDescription(BaseModel):
"""Parameters defining the chip in use for FT collection."""
Expand Down Expand Up @@ -169,32 +148,6 @@ def nexgen_experiment_type(self) -> str:
def ispyb_experiment_type(self) -> SSXType:
return SSXType.FIXED

@property
def detector_params(self):
det_dist_to_beam_lut = BEAM_CENTER_LUT_FILES[self.detector_name]
det_size_constants = (
EIGER2_X_9M_SIZE
if self.detector_name is DetectorName.EIGER
else PILATUS_6M_SIZE
)

self.collection_directory.mkdir(parents=True, exist_ok=True)

return DetectorParams(
detector_size_constants=det_size_constants,
exposure_time=self.exposure_time_s,
directory=self.collection_directory.as_posix(),
prefix=self.filename,
detector_distance=self.detector_distance_mm,
omega_start=0.0,
omega_increment=0.0,
num_images_per_trigger=self.num_exposures,
num_triggers=self.total_num_images,
det_dist_to_beam_converter_path=det_dist_to_beam_lut.as_posix(),
use_roi_mode=False, # Dasabled
trigger_mode=TriggerMode.SET_FRAMES, # For now...
)

@computed_field # type: ignore # Mypy doesn't like it
@property
def total_num_images(self) -> int:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from pathlib import Path
from time import sleep

import bluesky.plan_stubs as bps
from dodal.beamlines import i24
from dodal.devices.detector.detector import DetectorParams
from dodal.devices.detector.det_dim_constants import DetectorSizeConstants
from dodal.devices.i24.aperture import Aperture, AperturePositions
from dodal.devices.i24.beam_center import DetectorBeamCenter
from dodal.devices.i24.beamstop import Beamstop, BeamstopPositions
Expand All @@ -25,27 +26,35 @@ def get_beam_center_device(detector_in_use: str) -> DetectorBeamCenter:
return i24.pilatus_beam_center()


def compute_beam_center_position_mm(
lut_path: str, det_dist: float
def compute_beam_center_position_from_lut(
lut_path: Path,
detector_distance_mm: float,
det_size_constants: DetectorSizeConstants,
) -> tuple[float, float]:
"""Calculate the beam center position for the detector distance \
using the values in the lookup table for the conversion.
"""
lut_values = parse_lookup_table(lut_path)
lut_values = parse_lookup_table(lut_path.as_posix())

calc_x = linear_interpolation_lut(lut_values[0], lut_values[1])
beam_x = calc_x(det_dist)
beam_x_mm = calc_x(detector_distance_mm)
beam_x = (
beam_x_mm
* det_size_constants.det_size_pixels.width
/ det_size_constants.det_dimension.width
)

calc_y = linear_interpolation_lut(lut_values[0], lut_values[2])
beam_y = calc_y(det_dist)
beam_y_mm = calc_y(detector_distance_mm)
beam_y = (
beam_y_mm
* det_size_constants.det_size_pixels.height
/ det_size_constants.det_dimension.height
)

return beam_x, beam_y


# for now in mm, should probably be passed to det params like tyhis
# and the conversion to pixels there


def setup_beamline_for_collection_plan(
aperture: Aperture,
backlight: DualBacklight,
Expand Down Expand Up @@ -80,17 +89,14 @@ def move_detector_stage_to_position_plan(

def set_detector_beam_center_plan(
beam_center_device: DetectorBeamCenter,
detector_params: DetectorParams,
detector_distace: float,
beam_center_pixels: tuple[float, float],
group: str = "set_beamcenter",
wait: bool = True,
):
"""A small temporary plan to set up the beam center on the detector in use."""
# NOTE This will be removed once the detectors are using ophyd_async devices
# See https://github.com/DiamondLightSource/mx-bluesky/issues/62
beam_position_x, beam_position_y = detector_params.get_beam_position_pixels(
detector_distace
)
beam_position_x, beam_position_y = beam_center_pixels
SSX_LOGGER.info(f"Setting beam center to: {beam_position_x}, {beam_position_y}")
yield from bps.abs_set(beam_center_device.beam_x, beam_position_x, group=group)
yield from bps.abs_set(beam_center_device.beam_y, beam_position_y, group=group)
Expand Down

0 comments on commit 4dc9243

Please sign in to comment.