Belle II Software  release-08-01-10
caf_ecl_time_crystal.py
1 # -*- coding: utf-8 -*-
2 
3 
10 
11 """ECL timing calibration that performs the crystal calibrations, one for the whole set of runs."""
12 
13 from prompt import CalibrationSettings
14 from reconstruction import prepare_cdst_analysis
15 from caf.utils import IoV
16 
17 
18 
25 
26 
29 settings = CalibrationSettings(
30  name="ECL crystal time calibrations",
31  expert_username="ehill",
32  description=__doc__,
33  input_data_formats=["cdst"],
34  input_data_names=["bhabha_all_calib"],
35  input_data_filters={
36  "bhabha_all_calib": [
37  "bhabha_all_calib",
38  "4S",
39  "Continuum",
40  "Scan",
41  "Good",
42  "physics",
43  "On"]},
44  depends_on=[])
45 
46 
47 
48 
57 
58 
59 def get_calibrations(input_data, **kwargs):
60  """
61  Parameters:
62  input_data (dict): Should contain every name from the 'input_data_names' variable as a key.
63  Each value is a dictionary with {"/path/to/file_e1_r5.root": IoV(1,5,1,5), ...}. Useful for
64  assigning to calibration.files_to_iov
65 
66  **kwargs: Configuration options to be sent in. Since this may change we use kwargs as a way to help prevent
67  backwards compatibility problems. But you could use the correct arguments in b2caf-prompt-run for this
68  release explicitly if you want to.
69 
70  Currently only kwargs["output_iov"] is used. This is the output IoV range that your payloads should
71  correspond to. Generally your highest ExpRun payload should be open ended e.g. IoV(3,4,-1,-1)
72 
73  Returns:
74  list(caf.framework.Calibration): All of the calibration objects we want to assign to the CAF process
75  """
76  import basf2
77  # Set up config options
78 
79  # In this script we want to use one sources of input data.
80  # Get the input files from the input_data variable
81  # The input data should be the bhabha skim
82  file_to_iov_physics = input_data["bhabha_all_calib"]
83 
84  # Could remove this limit on the number of files per run but will just
85  # set to a large number in case we want to introduce it later.
86  # Also, keeping it allows the crystal calibrations code to look like the
87  # crates calibration code.
88  max_files_per_run = 2600
89 
90  # We filter addition files if there are more than [max_files_per_run] files per run.
91  # The input data files are sorted alphabetically by b2caf-prompt-run
92  # already. This procedure respects that ordering
93  from prompt.utils import filter_by_max_files_per_run
94 
95  reduced_file_to_iov_physics = filter_by_max_files_per_run(file_to_iov_physics, max_files_per_run)
96  input_files_physics = list(reduced_file_to_iov_physics.keys())
97  basf2.B2INFO(f"Total number of files actually used as input = {len(input_files_physics)}")
98 
99 
100  from basf2 import register_module, create_path
101  from ROOT import Belle2
102  from caf.framework import Collection
103 
104 
106  root_input = register_module('RootInput')
107  rec_path_bhabha = create_path()
108  rec_path_bhabha.add_module(root_input)
109  if 'Gearbox' not in rec_path_bhabha:
110  rec_path_bhabha.add_module('Gearbox')
111  if 'Geometry' not in rec_path_bhabha:
112  rec_path_bhabha.add_module('Geometry', useDB=True)
113 
114  prepare_cdst_analysis(rec_path_bhabha) # for new 2020 cdst format
115 
116  # ====================================================
117  t0BiasCorrection = -0.9 # Correct for the CDC t0 bias
118  # ====================================================
119 
120  col_bhabha = register_module('ECLBhabhaTCollector')
121  col_bhabha.param('timeAbsMax', 250)
122  col_bhabha.param('minCrystal', 1)
123  col_bhabha.param('maxCrystal', 8736)
124  col_bhabha.param('saveTree', False)
125  col_bhabha.param('hadronEventT0_TO_bhabhaEventT0_correction', t0BiasCorrection)
126 
127  eclTCol = Collection(collector=col_bhabha,
128  input_files=input_files_physics,
129  pre_collector_path=rec_path_bhabha)
130 
131 
133 
134  eclTAlgCrystals = Belle2.ECL.eclBhabhaTAlgorithm()
135 
136  # Define the CAF algorithm arguments
137  # Set the crateIDLo to be larger than crateIDHi so that no crate
138  # calibrations will be performed.
139  eclTAlgCrystals.crateIDLo = 3
140  eclTAlgCrystals.crateIDHi = 2
141  eclTAlgCrystals.debugOutput = True
142  eclTAlgCrystals.meanCleanRebinFactor = 3
143  eclTAlgCrystals.meanCleanCutMinFactor = 0.3
144  eclTAlgCrystals.debugFilenameBase = "eclBhabhaTAlgorithm"
145 
146 
148 
149  from caf.framework import Calibration
150 
151  cal_crystals = Calibration("ECLcrystalTimeCalibration_physics")
152  cal_crystals.add_collection(name="bhabha", collection=eclTCol)
153  cal_crystals.algorithms = [eclTAlgCrystals]
154 
155  # Here we set the AlgorithmStrategy for our algorithm
156  from caf.strategies import SingleIOV
157 
158  # The default value is SingleIOV, you don't have to set this, it is done automatically.
159  # SingleIOV just takes all of the runs as one big IoV and executes the algorithm once on all of their data.
160  # You can use granularity='run' or granularity='all' for the collector when using this strategy.
161 
162  cal_crystals.strategies = SingleIOV
163 
164 
167 
168  # We use a dummy collector that barely outputs any data and we set the input files to a single file so
169  # we spawn only one very fast job.
170  # It doesn't matter which input file we choose as the output is never used.
171 
173  cal_ecl_merge = Calibration(name="ecl_t_merge", collector="DummyCollector", algorithms=[merging_alg],
174  input_files=input_files_physics[:1])
175 
176  # The important part is that we depend on all the calibrations we previously ran
177  cal_ecl_merge.depends_on(cal_crystals)
178 
179  # ..Uses cdst data so it requires prepare_cdst_analysis
180  ecl_merge_pre_path = basf2.create_path()
181  prepare_cdst_analysis(ecl_merge_pre_path, components=['ECL'])
182  ecl_merge_pre_path.pre_collector_path = ecl_merge_pre_path
183 
184  # --------------------------------------------------------------
185  # ..Force the output iovs to be open
186  intermediate_iov = IoV(0, 0, -1, -1)
187  requested_iov = kwargs.get("requested_iov", None)
188  output_iov = IoV(requested_iov.exp_low, requested_iov.run_low, -1, -1)
189  for algorithm in cal_crystals.algorithms:
190  algorithm.params = {"apply_iov": intermediate_iov}
191  for algorithm in cal_ecl_merge.algorithms:
192  algorithm.params = {"apply_iov": output_iov}
193 
194 
196  return [cal_crystals, cal_ecl_merge]
Calibrate ecl crystals using bhabha events.
Calibrate ecl crystals using previously created payloads.