Belle II Software  release-05-01-25
calibration.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 """
5 PXD calibration APIs for CAF
6 Author: qingyuan.liu@desy.de
7 """
8 
9 from basf2 import register_module, create_path
10 from ROOT.Belle2 import PXDHotPixelMaskCalibrationAlgorithm, PXDAnalyticGainCalibrationAlgorithm
11 from ROOT.Belle2 import PXDValidationAlgorithm
12 from caf.framework import Calibration
13 from caf.strategies import SequentialRunByRun, SimpleRunByRun, SequentialBoundaries
14 
15 run_types = ['beam', 'physics', 'cosmic', 'all']
16 gain_methods = ['analytic', 'generic_mc', 'cluster_sim', '']
17 
18 
19 def hot_pixel_mask_calibration(
20  input_files,
21  cal_name="PXDHotPixelMaskCalibration",
22  run_type=None,
23  global_tags=None,
24  local_dbs=None,
25  **kwargs):
26  """
27  Parameters:
28  input_files (list): A list of input files to be assigned to calibration.input_files
29 
30  run_type ( str): The run type which will define different calibration parameters.
31  Should be in ['beam', 'physics', 'cosmic', 'all']
32 
33  global_tags (list): A list of global tags
34 
35  local_dbs (list): A list of local databases
36 
37  **kwargs: Additional configuration to support extentions without changing scripts in calibration folder.
38  Supported options are listed below:
39 
40  "activate_masking" is a boolean to activate existing masking in the payload.
41  PXDHotPixelMaskCollector will be used then instead of PXDRawHotPixelMaskCollector
42 
43  Return:
44  A caf.framework.Calibration obj.
45  """
46 
47  if global_tags is None:
48  global_tags = []
49  if local_dbs is None:
50  local_dbs = []
51  if run_type is None:
52  run_type = 'all'
53  if not isinstance(run_type, str) or run_type.lower() not in run_types:
54  raise ValueError("run_type not found in run_types : {}".format(run_type))
55  activate_masking = kwargs.get("activate_masking", False)
56  if not isinstance(activate_masking, bool):
57  raise ValueError("activate_masking is not a boolean!")
58 
59  # Create BASF2 path
60 
61  gearbox = register_module('Gearbox')
62  geometry = register_module('Geometry')
63  geometry.param('components', ['PXD'])
64  pxdunpacker = register_module('PXDUnpacker')
65  pxdunpacker.param('SuppressErrorMask', 0xFFFFFFFFFFFFFFFF)
66  checker = register_module('PXDPostErrorChecker')
67  checker.param("CriticalErrorMask", 0) # 0xC000000000000000)
68 
69  main = create_path()
70  main.add_module(gearbox)
71  main.add_module(geometry)
72  main.add_module(pxdunpacker)
73  main.add_module(checker)
74  if activate_masking:
75  main.add_module("ActivatePXDPixelMasker")
76  main.add_module("PXDRawHitSorter")
77 
78  # Collector setup
79  collector_name = "PXDRawHotPixelMaskCollector"
80  if activate_masking:
81  collector_name = "PXDHotPixelMaskCollector"
82  collector = register_module(collector_name)
83  collector.param("granularity", "run")
84 
85  # Algorithm setup
86 
87  algorithm = PXDHotPixelMaskCalibrationAlgorithm() # Getting a calibration algorithm instanced
88  algorithm.forceContinueMasking = False # Continue masking even when few/no events were collected
89  algorithm.minEvents = 30000 # Minimum number of collected events for masking
90  algorithm.minHits = 15 # Do dead pixel masking when median number of hits per pixel is higher
91  algorithm.pixelMultiplier = 7 # Occupancy threshold is median occupancy x multiplier
92  algorithm.maskDrains = True # Set True to allow masking of hot drain lines
93  algorithm.drainMultiplier = 7 # Occupancy threshold is median occupancy x multiplier
94  algorithm.maskRows = True # Set True to allow masking of hot rows
95  algorithm.rowMultiplier = 7 # Occupancy threshold is median occupancy x multiplier
96  algorithm.setPrefix(collector_name)
97  if run_type.lower() == 'cosmic':
98  algorithm.forceContinueMasking = True
99 
100  # Create the calibration instance
101 
102  cal = Calibration(
103  name=cal_name,
104  collector=collector,
105  algorithms=[algorithm],
106  input_files=input_files)
107  for global_tag in global_tags:
108  cal.use_central_database(global_tag)
109  for local_db in local_dbs:
110  cal.use_local_database(local_db)
111  cal.pre_collector_path = main
112  cal.strategies = SequentialRunByRun
113 
114  # Run type dependent configurations
115 
116  if run_type.lower() == 'cosmic':
117  cal.strategies = SimpleRunByRun
118 
119  return cal
120 
121 
122 def gain_calibration(input_files, cal_name="PXDGainCalibration",
123  boundaries=None, global_tags=None, local_dbs=None,
124  gain_method="Analytic", validation=True, **kwargs):
125  """
126  Parameters:
127  input_files (list): A list of input files to be assigned to calibration.input_files
128 
129  boundaries (c++ std::vector): boundaries for iov creation
130 
131  global_tags (list): A list of global tags
132 
133  local_dbs (list): A list of local databases
134 
135  gain_method (str): A string of gain algorithm in
136  ['analytic', 'generic_mc', 'cluster_sim', '']. Empty str means to skip gain
137  calibration and validation has to be True otherwise no algorithm will be used.
138  Caveat: Only the analytic method is now implemented.
139 
140  validation (bool): Adding validation algorithm if True (default)
141 
142  **kwargs: Additional configuration to support extentions without changing scripts in calibration folder.
143 
144  Return:
145  A caf.framework.Calibration obj.
146  """
147 
148  if global_tags is None:
149  global_tags = []
150  if local_dbs is None:
151  local_dbs = []
152  if gain_method is None:
153  gain_method = 'analytic'
154  if not isinstance(gain_method, str) or gain_method.lower() not in gain_methods:
155  raise ValueError("gain_method not found in gain_methods : {}".format(gain_method))
156 
157  # Create BASF2 path
158 
159  gearbox = register_module('Gearbox')
160  geometry = register_module('Geometry', useDB=True)
161  # pxdunpacker = register_module('PXDUnpacker')
162  # pxdunpacker.param('SuppressErrorMask', 0xFFFFFFFFFFFFFFFF)
163  # checker = register_module('PXDPostErrorChecker')
164  # checker.param("CriticalErrorMask", 0) # 0xC000000000000000)
165  genFitExtrapolation = register_module('SetupGenfitExtrapolation')
166  roiFinder = register_module('PXDROIFinder')
167  # roiFinder.param('tolerancePhi', 0.15) # default: 0.15
168  # roiFinder.param('toleranceZ', 0.5) # default 0.5
169 
170  main = create_path()
171  main.add_module(gearbox)
172  main.add_module(geometry)
173  # main.add_module(pxdunpacker)
174  # main.add_module(checker)
175  # main.add_module("PXDRawHitSorter")
176  main.add_module(genFitExtrapolation)
177  main.add_module(roiFinder) # for PXDIntercepts
178  main.add_module("PXDPerformance")
179 
180  # Collector setup
181 
182  collector = register_module("PXDPerformanceCollector")
183  collector.param("granularity", "run")
184  collector.param("minClusterCharge", 8)
185  collector.param("minClusterSize", 1)
186  collector.param("maxClusterSize", 10)
187  collector.param("nBinsU", 4)
188  collector.param("nBinsV", 6)
189  collector.param("fillEventTree", False)
190  collector.param("fillChargeRatioHistogram", True)
191 
192  # Algorithm setup
193  algorithms = []
194  if validation:
195  validation_alg = PXDValidationAlgorithm()
196  validation_alg.setPrefix("PXDPerformanceCollector")
197  validation_alg.minTrackPoints = 10 # Minimum number of track points
198  validation_alg.save2DHists = True # Flag to save 2D histogram for efficiency on layer 1 or 2 in Z-phi plane
199  algorithms.append(validation_alg)
200 
201  # validation_alg.setBoundaries(boundaries) # This takes boundaries from the expert_config
202  if (gain_method != ''):
203  algorithm = PXDAnalyticGainCalibrationAlgorithm() # Getting a calibration algorithm instanced
204  algorithm.minClusters = 200 # Minimum number of clusters in each region
205  algorithm.safetyFactor = 11 # Safety factor x minClusters/region -> <minClusters> for module
206  algorithm.forceContinue = False # Force continue the algorithm instead of return c_notEnoughData
207  algorithm.strategy = 0 # 0: medians, 1: landau fit
208  algorithm.correctForward = True # Correct default gains in forward regions due to low statistics
209  algorithm.useChargeRatioHistogram = True # Use Charge ratio histograms for the calibration
210  algorithm.setPrefix("PXDPerformanceCollector")
211  # algorithm.setBoundaries(boundaries) # This takes boundaries from the expert_config
212  algorithms.append(algorithm)
213 
214  # Create the calibration instance
215 
216  cal = Calibration(
217  name=cal_name,
218  collector=collector,
219  algorithms=algorithms,
220  input_files=input_files)
221  for global_tag in global_tags:
222  cal.use_central_database(global_tag)
223  for local_db in local_dbs:
224  cal.use_local_database(local_db)
225  cal.pre_collector_path = main
226  # cal.strategies = SequentialBoundaries
227  cal.strategies = SequentialRunByRun
228  # cal.strategies = SimpleRunByRun
229 
230  return cal
Calibration
Definition: Calibration.py:1