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