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