Belle II Software  release-08-02-06
caf_boostvector.py
1 
8 
9 """
10 Airflow script to perform BoostVector calibration.
11 """
12 
13 from prompt import CalibrationSettings, INPUT_DATA_FILTERS
14 from prompt.calibrations.caf_beamspot import settings as beamspot
15 from softwaretrigger.constants import ALWAYS_SAVE_OBJECTS, RAWDATA_OBJECTS
16 from basf2 import get_file_metadata, B2WARNING
17 import rawdata as rd
18 import reconstruction as re
19 import os
20 
21 
22 settings = CalibrationSettings(
23  name="BoostVector Calibrations",
24  expert_username="zlebcr",
25  description=__doc__,
26  input_data_formats=["cdst"],
27  input_data_names=["mumu_tight_or_highm_calib"],
28  input_data_filters={
29  "mumu_tight_or_highm_calib": [
30  INPUT_DATA_FILTERS["Data Tag"]["mumu_tight_or_highm_calib"],
31  INPUT_DATA_FILTERS["Run Type"]["physics"],
32  INPUT_DATA_FILTERS["Data Quality Tag"]["Good Or Recoverable"],
33  INPUT_DATA_FILTERS["Magnet"]["On"]]},
34  expert_config={
35  "outerLoss": "pow(rawTime - 8.0, 2) + 10 * pow(maxGap, 2)",
36  "innerLoss": "pow(rawTime - 8.0, 2) + 10 * pow(maxGap, 2)",
37  "minPXDhits": 0},
38  depends_on=[beamspot])
39 
40 
41 
42 
43 def is_cDST_file(fName):
44  """ Check if the file is cDST based on the metadata """
45 
46  metaData = get_file_metadata(fName)
47  description = metaData.getDataDescription()
48 
49  # if dataLevel is missing, determine from file name
50  if 'dataLevel' not in description:
51  B2WARNING('The cdst/mdst info is not stored in file metadata')
52  return ('cdst' in os.path.basename(fName))
53 
54  return (description['dataLevel'] == 'cdst')
55 
56 
57 def get_calibrations(input_data, **kwargs):
58  """
59  Parameters:
60  input_data (dict): Should contain every name from the 'input_data_names' variable as a key.
61  Each value is a dictionary with {"/path/to/file_e1_r5.root": IoV(1,5,1,5), ...}. Useful for
62  assigning to calibration.files_to_iov
63 
64  **kwargs: Configuration options to be sent in. Since this may change we use kwargs as a way to help prevent
65  backwards compatibility problems. But you could use the correct arguments in b2caf-prompt-run for this
66  release explicitly if you want to.
67 
68  Currently only kwargs["output_iov"] is used. This is the output IoV range that your payloads should
69  correspond to. Generally your highest ExpRun payload should be open ended e.g. IoV(3,4,-1,-1)
70 
71  Returns:
72  list(caf.framework.Calibration): All of the calibration objects we want to assign to the CAF process
73  """
74  import basf2
75  # Set up config options
76 
77  # In this script we want to use one sources of input data.
78  # Get the input files from the input_data variable
79  file_to_iov_physics = input_data["mumu_tight_or_highm_calib"]
80 
81  # We might have requested an enormous amount of data across a run range.
82  # There's a LOT more files than runs!
83  # Lets set some limits because this calibration doesn't need that much to run.
84  max_files_per_run = 1000000
85 
86  # We filter out any more than 100 files per run. The input data files are sorted alphabetically by b2caf-prompt-run
87  # already. This procedure respects that ordering
88  from prompt.utils import filter_by_max_files_per_run
89 
90  reduced_file_to_iov_physics = filter_by_max_files_per_run(file_to_iov_physics, max_files_per_run)
91  input_files_physics = list(reduced_file_to_iov_physics.keys())
92  basf2.B2INFO(f"Total number of files actually used as input = {len(input_files_physics)}")
93 
94  isCDST = is_cDST_file(input_files_physics[0]) if len(input_files_physics) > 0 else True
95 
96  # Get the overall IoV we our process should cover. Includes the end values that we may want to ignore since our output
97  # IoV should be open ended. We could also use this as part of the input data selection in some way.
98  requested_iov = kwargs.get("requested_iov", None)
99 
100  from caf.utils import IoV
101  # The actual value our output IoV payload should have. Notice that we've set it open ended.
102  output_iov = IoV(requested_iov.exp_low, requested_iov.run_low, -1, -1)
103 
104 
106 
107  from ROOT.Belle2 import BoostVectorAlgorithm
108  from basf2 import create_path, register_module
109  import modularAnalysis as ana
110  import vertex
111 
112 
114 
115  from caf.framework import Calibration
116  from caf.strategies import SingleIOV
117 
118  # module to be run prior the collector
119  rec_path_1 = create_path()
120  if isCDST:
121  rec_path_1.add_module("RootInput", branchNames=ALWAYS_SAVE_OBJECTS + RAWDATA_OBJECTS)
122  rd.add_unpackers(rec_path_1)
123  re.add_reconstruction(rec_path_1)
124 
125  minPXDhits = kwargs['expert_config']['minPXDhits']
126  muSelection = '[p>1.0]'
127  muSelection += ' and abs(dz)<2.0 and abs(dr)<0.5'
128  muSelection += f' and nPXDHits >= {minPXDhits} and nSVDHits >= 8 and nCDCHits >= 20'
129  ana.fillParticleList('mu+:BV', muSelection, path=rec_path_1)
130  ana.reconstructDecay('Upsilon(4S):BV -> mu+:BV mu-:BV', '9.5<M<11.5', path=rec_path_1)
131  vertex.treeFit('Upsilon(4S):BV', updateAllDaughters=True, ipConstraint=True, path=rec_path_1)
132 
133  collector_bv = register_module('BoostVectorCollector', Y4SPListName='Upsilon(4S):BV')
134  algorithm_bv = BoostVectorAlgorithm()
135  algorithm_bv.setOuterLoss(kwargs['expert_config']['outerLoss'])
136  algorithm_bv.setInnerLoss(kwargs['expert_config']['innerLoss'])
137 
138  calibration_bv = Calibration('BoostVector',
139  collector=collector_bv,
140  algorithms=algorithm_bv,
141  input_files=input_files_physics,
142  pre_collector_path=rec_path_1)
143 
144  calibration_bv.strategies = SingleIOV
145 
146  # Do this for the default AlgorithmStrategy to force the output payload IoV
147  # It may be different if you are using another strategy like SequentialRunByRun
148  for algorithm in calibration_bv.algorithms:
149  algorithm.params = {"iov_coverage": output_iov}
150 
151  # Most other options like database chain and backend args will be overwritten by b2caf-prompt-run.
152  # So we don't bother setting them.
153 
154  # You must return all calibrations you want to run in the prompt process, even if it's only one
155  return [calibration_bv]
156 
157 
def treeFit(list_name, conf_level=0.001, massConstraint=[], ipConstraint=False, updateAllDaughters=False, customOriginConstraint=False, customOriginVertex=[0.001, 0, 0.0116], customOriginCovariance=[0.0048, 0, 0, 0, 0.003567, 0, 0, 0, 0.0400], originDimension=3, treatAsInvisible='', ignoreFromVertexFit='', path=None)
Definition: vertex.py:239