Belle II Software  release-06-02-00
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  import basf2
101  from basf2 import register_module, create_path
102  import ROOT
103  from ROOT import Belle2
104  from ROOT.Belle2 import TestCalibrationAlgorithm
105  from caf.framework import Collection
106 
107 
109  root_input = register_module('RootInput')
110  rec_path_bhabha = create_path()
111  rec_path_bhabha.add_module(root_input)
112  if 'Gearbox' not in rec_path_bhabha:
113  rec_path_bhabha.add_module('Gearbox')
114  if 'Geometry' not in rec_path_bhabha:
115  rec_path_bhabha.add_module('Geometry', useDB=True)
116 
117  prepare_cdst_analysis(rec_path_bhabha) # for new 2020 cdst format
118 
119  # ====================================================
120  t0BiasCorrection = -0.9 # Correct for the CDC t0 bias
121  # ====================================================
122 
123  col_bhabha = register_module('ECLBhabhaTCollector')
124  col_bhabha.param('timeAbsMax', 250)
125  col_bhabha.param('minCrystal', 1)
126  col_bhabha.param('maxCrystal', 8736)
127  col_bhabha.param('saveTree', False)
128  col_bhabha.param('hadronEventT0_TO_bhabhaEventT0_correction', t0BiasCorrection)
129 
130  eclTCol = Collection(collector=col_bhabha,
131  input_files=input_files_physics,
132  pre_collector_path=rec_path_bhabha)
133 
134 
136 
137  eclTAlgCrystals = Belle2.ECL.eclBhabhaTAlgorithm()
138 
139  # Define the CAF algorithm arguments
140  # Set the crateIDLo to be larger than crateIDHi so that no crate
141  # calibrations will be performed.
142  eclTAlgCrystals.crateIDLo = 3
143  eclTAlgCrystals.crateIDHi = 2
144  eclTAlgCrystals.debugOutput = True
145  eclTAlgCrystals.meanCleanRebinFactor = 3
146  eclTAlgCrystals.meanCleanCutMinFactor = 0.3
147  eclTAlgCrystals.debugFilenameBase = "eclBhabhaTAlgorithm"
148 
149 
151 
152  from caf.framework import Calibration
153 
154  cal_crystals = Calibration("ECLcrystalTimeCalibration_physics")
155  cal_crystals.add_collection(name="bhabha", collection=eclTCol)
156  cal_crystals.algorithms = [eclTAlgCrystals]
157 
158  # Here we set the AlgorithmStrategy for our algorithm
159  from caf.strategies import SingleIOV
160 
161  # The default value is SingleIOV, you don't have to set this, it is done automatically.
162  # SingleIOV just takes all of the runs as one big IoV and executes the algorithm once on all of their data.
163  # You can use granularity='run' or granularity='all' for the collector when using this strategy.
164 
165  cal_crystals.strategies = SingleIOV
166 
167 
170 
171  # We use a dummy collector that barely outputs any data and we set the input files to a single file so
172  # we spawn only one very fast job.
173  # It doesn't matter which input file we choose as the output is never used.
174 
176  cal_ecl_merge = Calibration(name="ecl_t_merge", collector="DummyCollector", algorithms=[merging_alg],
177  input_files=input_files_physics[:1])
178 
179  # The important part is that we depend on all the calibrations we previously ran
180  cal_ecl_merge.depends_on(cal_crystals)
181 
182  # ..Uses cdst data so it requires prepare_cdst_analysis
183  ecl_merge_pre_path = basf2.create_path()
184  prepare_cdst_analysis(ecl_merge_pre_path, components=['ECL'])
185  ecl_merge_pre_path.pre_collector_path = ecl_merge_pre_path
186 
187  # --------------------------------------------------------------
188  # ..Force the output iovs to be open
189  intermediate_iov = IoV(0, 0, -1, -1)
190  requested_iov = kwargs.get("requested_iov", None)
191  output_iov = IoV(requested_iov.exp_low, requested_iov.run_low, -1, -1)
192  for algorithm in cal_crystals.algorithms:
193  algorithm.params = {"apply_iov": intermediate_iov}
194  for algorithm in cal_ecl_merge.algorithms:
195  algorithm.params = {"apply_iov": output_iov}
196 
197 
199  return [cal_crystals, cal_ecl_merge]
Calibrate ecl crystals using bhabha events.
Calibrate ecl crystals using previously created payloads.