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