Belle II Software  release-08-02-06
caf_cdc_fudgefactor.py
1 
8 
9 """CDC fudge factor calibration."""
10 from prompt import CalibrationSettings, INPUT_DATA_FILTERS
11 from prompt.calibrations.caf_cdc import settings as cdc_tracking_calibration
12 from prompt.calibrations.caf_vxdcdc_alignment import settings as full_alignment
13 from prompt.utils import ExpRun
14 import basf2
15 from ROOT import Belle2
16 from caf.framework import Calibration
17 from caf import strategies
18 from modularAnalysis import fillParticleList, cutAndCopyList, reconstructDecay, applyCuts
19 from vertex import treeFit
20 
21 
22 settings = CalibrationSettings(name="CDC Sigma fudge factor",
23  expert_username="dvthanh",
24  description=__doc__,
25  input_data_formats=["raw"],
26  input_data_names=["mumu_tight_or_highm_calib"],
27  input_data_filters={"mumu_tight_or_highm_calib":
28  [INPUT_DATA_FILTERS["Data Tag"]["mumu_tight_or_highm_calib"],
29  INPUT_DATA_FILTERS["Data Quality Tag"]["Good"],
30  INPUT_DATA_FILTERS["Magnet"]["On"]]},
31  depends_on=[cdc_tracking_calibration, full_alignment],
32  expert_config={
33  "fileFormat": "RAW",
34  "min_events_per_file": 500,
35  "max_events_per_file": 30000,
36  "components": ["CDC", "ECL", "KLM"],
37  "vertex_fit": 0,
38  "payload_boundaries": [],
39  "backend_args": {"request_memory": "4 GB"}
40  })
41 
42 
43 
46 
47 def get_calibrations(input_data, **kwargs):
48  # Set up config options
49  # read expert_config values
50  expert_config = kwargs.get("expert_config")
51  min_events_per_file = expert_config["min_events_per_file"]
52  max_events_per_file = expert_config["max_events_per_file"]
53 # file_format = expert_config["file_format"]
54  components = expert_config["components"]
55  vertex_fit = expert_config["vertex_fit"]
56  fileFormat = expert_config["fileFormat"]
57  # In this script we want to use one sources of input data.
58  # Get the input files from the input_data variable
59  file_to_iov_mumu = input_data["mumu_tight_or_highm_calib"]
60 
61  from prompt.utils import filter_by_max_files_per_run
62  reduced_file_to_iov_mumu = filter_by_max_files_per_run(file_to_iov_mumu, 100, min_events_per_file)
63  input_files_mumu = list(reduced_file_to_iov_mumu.keys())
64  basf2.B2INFO("Complete input data selection.")
65  basf2.B2INFO(f"Total number of files actually used as input = {len(input_files_mumu)}")
66 
67  payload_boundaries = []
68  payload_boundaries.extend([ExpRun(*boundary) for boundary in expert_config["payload_boundaries"]])
69  basf2.B2INFO(f"Payload boundaries from expert_config: {payload_boundaries}")
70 
71  from caf.utils import IoV
72  # The actual value our output IoV payload should have. Notice that we've set it open ended.
73  requested_iov = kwargs.get("requested_iov", None)
74  output_iov = IoV(requested_iov.exp_low, requested_iov.run_low, -1, -1)
75 
76  # for SingleIOV stratrgy, it's better to set the granularity to 'all' so that the collector jobs will run faster
77  collector_granularity = 'all'
78  if payload_boundaries:
79  basf2.B2INFO('Found payload_boundaries: set collector granularity to run')
80  collector_granularity = 'run'
81 
82 
84  col = basf2.register_module("CDCFudgeFactorCalibrationCollector",
85  granularity=collector_granularity)
86  # call algorighm
88  algo.setHistFileName("histo_fudge_factor.root")
89 
91  fudge_calib = Calibration("CDC_FudgeFactor",
92  collector=col,
93  algorithms=algo,
94  input_files=input_files_mumu,
95  pre_collector_path=pre_collector(max_events_per_file,
96  components=components,
97  fileFormat=fileFormat,
98  vertex_fit=vertex_fit))
99 # backend_args=expert_config["backend_args"])
100  # collector_granularity=collector_granularity)
101  # Do this for the default AlgorithmStrategy to force the output payload IoV
102  # It may be different if you are using another strategy like SequentialRunByRun
103  if payload_boundaries:
104  basf2.B2INFO("Found payload_boundaries: calibration strategies set to SequentialBoundaries.")
105  fudge_calib.strategies = strategies.SequentialBoundaries
106  for alg in fudge_calib.algorithms:
107  alg.params = {"iov_coverage": output_iov, "payload_boundaries": payload_boundaries}
108  else:
109  for alg in fudge_calib.algorithms:
110  alg.params = {"apply_iov": output_iov}
111 
112  return [fudge_calib]
113 
114 
115 
116 def pre_collector(max_events=None, components=["CDC", "ECL", "KLM"], fileFormat="RAW", vertex_fit=0):
117  """
118  Define pre collection (reconstruction in our purpose).
119  Probably, we need only CDC and ECL data.
120  Parameters:
121  max_events [int] : number of events to be processed.
122  All events by Default.
123  Returns:
124  path : path for pre collection
125  """
126  from basf2 import create_path, register_module
127  from softwaretrigger.constants import HLT_INPUT_OBJECTS
128  reco_path = create_path()
129  if fileFormat == "RAW":
130  if max_events is None:
131  root_input = register_module('RootInput', branchNames=HLT_INPUT_OBJECTS)
132  else:
133  root_input = register_module('RootInput', branchNames=HLT_INPUT_OBJECTS,
134  entrySequences='0:{}'.format(max_events))
135  reco_path.add_module(root_input)
136  # unpack
137  from rawdata import add_unpackers
138  from reconstruction import add_reconstruction
139  add_unpackers(reco_path, components=components)
140  # reconstruction
141  add_reconstruction(reco_path,
142  components=components,
143  append_full_grid_cdc_eventt0=True)
144  if fileFormat == "mdst":
145  from modularAnalysis import inputMdst
146  inputMdst(filename="", path=reco_path, environmentType='default', skipNEvents=0, entrySequence=['0:{}'.format(max_events)])
147 
148  reco_path.add_module('Progress')
149  fillParticleList('gamma:HLT', 'E>0.1', path=reco_path)
150  goodTrack = 'abs(d0) < 2.0 and abs(z0) < 4.0 and pt > 2.0 and useCMSFrame(p) > 0.5'
151  fillParticleList('mu+:HLT', goodTrack, path=reco_path)
152  cutAndCopyList('mu+:sel', 'mu+:HLT', goodTrack, path=reco_path)
153  reconstructDecay('vpho:mumu -> mu+:sel mu-:sel', '', path=reco_path) # apply event cuts
154  applyCuts('vpho:mumu', '[nCleanedTracks('+goodTrack+') == 2]', path=reco_path)
155  if vertex_fit == 1:
156  treeFit('vpho:mumu', ipConstraint=False, updateAllDaughters=False, path=reco_path)
157 
158  return reco_path