haniwers.v1.cli.threshold#

Threshold management commands.

This module provides commands for writing threshold values to OSECHI detector channels using a unified configuration interface.

Commands: write: Write threshold values to detector channels (single or multi-channel) fit: Placeholder for threshold fitting functionality optimize: Placeholder for optimal threshold optimization list: Placeholder for listing current threshold configurations

Dependencies: - ConfigLoader: Load configuration from TOML files - Device: Serial communication with detector - apply_thresholds: High-level batch threshold application with retry and logging - SensorConfig: Configuration model for detector channels - parse_thresholds: Parse and validate threshold configuration strings

Module Contents#

Functions#

_apply_overrides

Apply overrides to configuration object

count_hits_by_channel

Count events where each channel has value >0

get_all_thresholds

Get current threshold values for all channels.

write

Write threshold values to detector channels.

serial

Run serial threshold scanning for detector characterization.

parallel

Run parallel threshold scanning for detector characterization.

Data#

app

API#

haniwers.v1.cli.threshold.app#

‘Typer(…)’

haniwers.v1.cli.threshold._apply_overrides(config, overrides: dict) None#

Apply overrides to configuration object

Args: config: Configuration object to override (e.g. cfg.device) overrides: Dict of {attribute_name: value}

haniwers.v1.cli.threshold.count_hits_by_channel(events: list[haniwers.v1.daq.model.RawEvent]) dict[str, int]#

Count events where each channel has value >0

haniwers.v1.cli.threshold.get_all_thresholds(sensors: dict[str, haniwers.v1.config.model.SensorConfig]) dict[str, int | None]#

Get current threshold values for all channels.

haniwers.v1.cli.threshold.write(ctx: typer.Context, config: Optional[pathlib.Path] = ConfigOptions.config, port: Optional[str] = DeviceOptions.port, baudrate: Optional[int] = DeviceOptions.baudrate, timeout: Optional[float] = DeviceOptions.timeout, device_label: Optional[str] = DeviceOptions.device_label, thresholds: str = ThresholdOptions.thresholds, max_retry: int = ThresholdOptions.max_retry, history: pathlib.Path = ThresholdOptions.history, workspace: Optional[pathlib.Path] = OutputOptions.workspace, filename_prefix: Optional[str] = OutputOptions.filename_prefix, filename_suffix: Optional[str] = OutputOptions.filename_suffix, events_per_file: Optional[int] = SamplerOptions.events_per_file, number_of_files: Optional[int] = SamplerOptions.number_of_files, stream_mode: bool = SamplerOptions.stream_mode, mock: bool = TestingOptions.mock, verbose: bool = LoggerOptions.verbose, logfile: str = LoggerOptions.logfile) None#

Write threshold values to detector channels.

This command writes threshold values to the three detector layers (top, middle, bottom). The threshold determines the detector’s sensitivity to cosmic ray signals.

Supports single-channel or multi-channel configuration using the unified –thresholds option. Includes automatic retry logic on communication failures and audit trail logging to CSV file.

Args: thresholds: Threshold configuration as ‘channel:value;channel:value;…’ Examples: - Single channel: ‘1:280’ - All channels: ‘1:290;2:320;3:298’

Output: For each channel, displays: - Status icon: ✓ (success) or ✗ (failed) - Channel number and status - Threshold value written (vth) - Number of attempts (1 = immediate success, >1 = retried)

Examples: Write to single channel: $ haniwers-v1 threshold write --port /dev/tty.usbserial --thresholds 1:280

Write to multiple channels:
    $ haniwers-v1 threshold write --port /dev/tty.usbserial --thresholds '1:290;2:320;3:298'

With custom workspace and max retries:
    $ haniwers-v1 threshold write --port /dev/tty.usbserial --thresholds '1:280' \
        --workspace data --max-retry 5

Note: The device must be powered on and connected before running this command. Logs are automatically created in YYYYMMDD directory matching DAQ structure. Each threshold write is logged to CSV with timestamp for audit trail.

haniwers.v1.cli.threshold.serial(ctx: typer.Context, config: Optional[pathlib.Path] = ConfigOptions.config, port: Optional[str] = DeviceOptions.port, baudrate: Optional[int] = DeviceOptions.baudrate, timeout: Optional[float] = DeviceOptions.timeout, device_label: Optional[str] = DeviceOptions.device_label, thresholds: str = ThresholdOptions.thresholds, suppress_threshold: int = ThresholdOptions.suppress_threshold, max_retry: int = ThresholdOptions.max_retry, history: pathlib.Path = ThresholdOptions.history, nsteps: int = ScanOptions.nsteps, step: int = ScanOptions.step, duration: float = ScanOptions.duration, workspace: Optional[pathlib.Path] = OutputOptions.workspace, filename_prefix: Optional[str] = OutputOptions.filename_prefix, filename_suffix: Optional[str] = OutputOptions.filename_suffix, events_per_file: Optional[int] = SamplerOptions.events_per_file, number_of_files: Optional[int] = SamplerOptions.number_of_files, stream_mode: bool = SamplerOptions.stream_mode, mock: bool = TestingOptions.mock, verbose: bool = LoggerOptions.verbose, logfile: str = LoggerOptions.logfile) None#

Run serial threshold scanning for detector characterization.

Scans one or more detector channels across a range of threshold values, collecting cosmic ray event data at each threshold level. Results are saved to CSV files for analysis.

This command is useful for:

  • Measuring detector sensitivity curves

  • Comparing channel responses

  • Characterizing detector behavior

Example commands:

Single-channel scan with mock device:
$ haniwers-v1 threshold serial --thresholds "1:250" \
    --nsteps 5 --step 10 --duration 2 --mock

Multi-channel scan with TOML config:
$ haniwers-v1 threshold serial --config config.toml

Override TOML parameters with CLI:
$ haniwers-v1 threshold serial --config config.toml \
    --thresholds "1:300;2:320" --duration 3

Output files (saved in workspace/YYYYMMDD/):

  • scan_results_ch1.csv, scan_results_ch2.csv, …: Threshold scan data

  • threshold_operations.csv: Audit log of all threshold changes

Note: Requires a TOML configuration file with sensor definitions, unless all parameters are provided via CLI options.

haniwers.v1.cli.threshold.parallel(ctx: typer.Context, config: Optional[pathlib.Path] = ConfigOptions.config, port: Optional[str] = DeviceOptions.port, baudrate: Optional[int] = DeviceOptions.baudrate, timeout: Optional[float] = DeviceOptions.timeout, device_label: Optional[str] = DeviceOptions.device_label, thresholds: str = ThresholdOptions.thresholds, max_retry: int = ThresholdOptions.max_retry, history: pathlib.Path = ThresholdOptions.history, nsteps: int = ScanOptions.nsteps, step: int = ScanOptions.step, duration: float = ScanOptions.duration, workspace: Optional[pathlib.Path] = OutputOptions.workspace, filename_prefix: Optional[str] = OutputOptions.filename_prefix, filename_suffix: Optional[str] = OutputOptions.filename_suffix, events_per_file: Optional[int] = SamplerOptions.events_per_file, number_of_files: Optional[int] = SamplerOptions.number_of_files, stream_mode: bool = SamplerOptions.stream_mode, mock: bool = TestingOptions.mock, verbose: bool = LoggerOptions.verbose, logfile: str = LoggerOptions.logfile) None#

Run parallel threshold scanning for detector characterization.

Scans all detector channels across a range of threshold values in parallel. All channels step together at each measurement point, with all active during data collection (unlike serial mode). This provides 3x speedup vs serial scan.

Results are saved to CSV files for analysis, one per channel.

This command is useful for:

  • Fast threshold characterization of all channels simultaneously

  • Collecting synchronized measurements from multiple channels

  • Comparing detector responses in parallel

Example commands:

Parallel scan with mock device (all channels step together):
$ haniwers-v1 threshold parallel --nsteps 5 --step 10 --duration 2 --mock

Multi-channel parallel scan with TOML config:
$ haniwers-v1 threshold parallel --config config.toml

Override TOML parameters with CLI:
$ haniwers-v1 threshold parallel --config config.toml \
    --thresholds "1:300;2:320;3:298" --duration 3

Output files (saved in workspace/YYYYMMDD/):

  • scan_results_ch1.csv, scan_results_ch2.csv, …: Threshold scan data

  • threshold_operations.csv: Audit log of all threshold changes

Note: Requires a TOML configuration file with sensor definitions, unless all parameters are provided via CLI options. All channels must have matching nsteps and step_size values.