Belle II Software  release-05-01-25
caf_klm_strip_efficiency.py
1 # -*- coding: utf-8 -*-
2 
3 """A simple example calibration that takes one input data list from raw data and performs
4 a single calibration."""
5 
6 import basf2
7 from prompt import CalibrationSettings
8 
9 
16 
17 
18 settings = CalibrationSettings(name='KLM strip efficiency',
19  expert_username='depietro',
20  description=__doc__,
21  input_data_formats=['cdst'],
22  input_data_names=['hlt_mumu'],
23  depends_on=[])
24 
25 
26 
27 
36 
37 
38 def get_calibrations(input_data, **kwargs):
39  """
40  Parameters:
41  input_data (dict): Should contain every name from the 'input_data_names' variable as a key.
42  Each value is a dictionary with {"/path/to/file_e1_r5.root": IoV(1,5,1,5), ...}. Useful for
43  assigning to calibration.files_to_iov
44 
45  **kwargs: Configuration options to be sent in. Since this may change we use kwargs as a way to help prevent
46  backwards compatibility problems. But you could use the correct arguments in b2caf-prompt-run for this
47  release explicitly if you want to.
48 
49  Currently only kwargs["requested_iov"] is used. This is the output IoV range that your payloads should
50  correspond to. Generally your highest ExpRun payload should be open ended e.g. IoV(3,4,-1,-1)
51 
52  Returns:
53  list(caf.framework.Calibration): All of the calibration objects we want to assign to the CAF process
54  """
55  # Set up config options
56 
57  # In this script we want to use one sources of input data.
58  # Get the input files from the input_data variable
59  file_to_iov_cdst = input_data['hlt_mumu']
60 
61  # We might have requested an enormous amount of data across a run range.
62  # There's a LOT more files than runs!
63  # Lets set some limits because this calibration doesn't need that much to run.
64  max_files_per_run = 2
65 
66  # If you are using Raw data there's a chance that input files could have zero events.
67  # This causes a B2FATAL in basf2 RootInput so the collector job will fail.
68  # Currently we don't have a good way of filtering this on the automated side, so we can check here.
69  min_events_per_file = 1
70 
71  # We filter out any more than 2 files per run. The input data files are sorted alphabetically by b2caf-prompt-run
72  # already. This procedure respects that ordering
73 
74  # For testing
75  # reduced_file_to_iov_cdst = filter_by_max_files_per_run(file_to_iov_cdst, max_files_per_run, min_events_per_file)
76  # input_files_cdst = sorted(list(reduced_file_to_iov_cdst.keys()))
77  input_files_cdst = sorted(list(file_to_iov_cdst.keys()))
78  basf2.B2INFO(f'Total number of \'hlt_mumu\' files actually used as input = {len(input_files_cdst)}')
79 
80  if not input_files_cdst:
81  raise Exception('No valid input files found!')
82 
83  # Get the overall IoV we our process should cover. Includes the end values that we may want to ignore since our output
84  # IoV should be open ended. We could also use this as part of the input data selection in some way.
85  requested_iov = kwargs['requested_iov']
86 
87  from caf.utils import IoV
88  # The actual value our output IoV payload should have. Notice that we've set it open ended.
89  output_iov = IoV(requested_iov.exp_low, requested_iov.run_low, -1, -1)
90 
91 
93 
94  from ROOT.Belle2 import KLMStripEfficiencyAlgorithm
95 
96  alg = KLMStripEfficiencyAlgorithm()
97 
98 
100 
101  from caf.framework import Calibration, Collection
102 
103  cal_klm = Calibration('KLMStripEfficiency')
104 
105 
107 
108  from klm_calibration_utils import get_strip_efficiency_pre_collector_path
109 
110  if input_files_cdst:
111  coll_cdst = get_collector('hlt_mumu')
112  rec_path_cdst = get_strip_efficiency_pre_collector_path()
113 
114  collection_cdst = Collection(collector=coll_cdst,
115  input_files=input_files_cdst,
116  pre_collector_path=rec_path_cdst)
117 
118  cal_klm.add_collection(name='cdst', collection=collection_cdst)
119 
120 
122 
123  cal_klm.algorithms = [alg]
124 
125  from klm_strip_efficiency import KLMStripEfficiency
126 
127  for algorithm in cal_klm.algorithms:
128  algorithm.strategy = KLMStripEfficiency
129  algorithm.params = {'iov_coverage': output_iov}
130 
131  # You must return all calibrations you want to run in the prompt process, even if it's only one
132  return [cal_klm]
133 
134 
135 
136 
137 def get_collector(input_data_name):
138  """
139  Return the correct KLMStripEfficiencyCollector module setup for each data type.
140  Placed here so it can be different for prompt compared to standard.
141  """
142 
143  if input_data_name == 'hlt_mumu':
144  return basf2.register_module('KLMStripEfficiencyCollector',
145  MuonListName='mu+:klmStripEfficiency',
146  MinimalMatchingDigits=14,
147  MinimalMatchingDigitsOuterLayers=4,
148  MinimalMomentumNoOuterLayers=4.0)
149  raise Exception("Unknown input data name used when setting up collector")
Collection
Definition: Collection.py:1
Calibration
Definition: Calibration.py:1