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