Belle II Software  release-05-01-25
klm_strategies_common.py
1 # -*- coding: utf-8 -*-
2 
3 """Common functions for KLM calibration strategies."""
4 
5 import basf2
6 from caf.utils import ExpRun, IoV
7 
8 
9 def calibration_result_string(result):
10  """
11  Convert calibration result to text message.
12  Parameters:
13  result (int): Calibration result.
14  """
15  if (result == 0):
16  res = 'successful'
17  elif (result == 1):
18  res = 'iteration is necessary'
19  elif (result == 2):
20  res = 'not enough data'
21  elif (result == 3):
22  res = 'failure'
23  elif (result == 4):
24  res = 'undefined'
25  return res
26 
27 
28 def get_lowest_exprun(number_of_experiments, experiment_index, run_list,
29  iov_coverage):
30  """
31  Get lowest experiment and run numbers.
32  Parameters:
33  number_of_experiments (int): Number of experiments.
34  experiment_index (int): 1-based experiment index.
35  run_list (list[ExpRun]): List of runs for the selected experiment.
36  iov_coverage (IoV): IOV coverage.
37  """
38  if iov_coverage and experiment_index == 1:
39  lowest_exprun = ExpRun(iov_coverage.exp_low, iov_coverage.run_low)
40  if lowest_exprun > run_list[0]:
41  basf2.B2WARNING(f'The lowest run {run_list[0]} of input data is '
42  f'smaller than the lowest run {lowest_exprun} of '
43  'the requested IOV coverage. The IOV coverage is '
44  'extended.')
45  lowest_exprun = run_list[0]
46  else:
47  lowest_exprun = run_list[0]
48  # Start from the beginning of experiments except the first one.
49  if (experiment_index > 1):
50  lowest_exprun = ExpRun(lowest_exprun.exp, 0)
51  return lowest_exprun
52 
53 
54 def get_highest_exprun(number_of_experiments, experiment_index, run_list,
55  iov_coverage):
56  """
57  Get highest experiment and run numbers.
58  Parameters:
59  number_of_experiments (int): Number of experiments.
60  experiment_index (int): 1-based experiment index.
61  run_list (list[ExpRun]): List of runs for the selected experiment.
62  iov_coverage (IoV): IOV coverage.
63  """
64  if iov_coverage and experiment_index == number_of_experiments:
65  highest_exprun = ExpRun(iov_coverage.exp_high, iov_coverage.run_high)
66  if (highest_exprun < run_list[-1] and
67  not ((iov_coverage.exp_high == -1) or
68  (iov_coverage.exp_high == run_list[-1].exp and
69  iov_coverage.run_high == -1))):
70  basf2.B2WARNING(f'The highest run {run_list[-1]} of input data is '
71  f'larger than the highest run {highest_exprun} of '
72  'the requested IOV coverage. The IOV coverage is '
73  'extended.')
74  highest_exprun = run_list[-1]
75  else:
76  highest_exprun = run_list[-1]
77  # Extend the IOV to the end of experiments except the last one.
78  if (experiment_index < number_of_experiments):
79  highest_exprun = ExpRun(highest_exprun.exp, -1)
80  return highest_exprun