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