15from softwaretrigger
import constants, path_utils
16from geometry
import check_components
17from pxd
import add_roi_finder, add_roi_payload_assembler, add_pxd_percentframe
18from rawdata
import add_unpackers
19from reconstruction
import add_reconstruction, add_cosmics_reconstruction
22def setup_basf2_and_db(zmq=False):
24 Setup local database usage for HLT
26 parser = argparse.ArgumentParser(description='basf2 for online')
29 parser.add_argument(
"--input", required=
True, type=str, help=
"ZMQ Address of the distributor process")
30 parser.add_argument(
"--output", required=
True, type=str, help=
"ZMQ Address of the collector process")
31 parser.add_argument(
"--dqm", required=
True, type=str, help=
"ZMQ Address of the histoserver process")
33 parser.add_argument(
'input_buffer_name', type=str,
34 help=
'Input Ring Buffer names')
35 parser.add_argument(
'output_buffer_name', type=str,
36 help=
'Output Ring Buffer name')
37 parser.add_argument(
'histo_port', type=int,
38 help=
'Port of the HistoManager to connect to')
39 parser.add_argument(
'--input-file', type=str,
40 help=
"Input sroot file, if set no RingBuffer input will be used",
42 parser.add_argument(
'--output-file', type=str,
43 help=
"Filename for SeqRoot output, if set no RingBuffer output will be used",
45 parser.add_argument(
'--histo-output-file', type=str,
46 help=
"Filename for histogram output",
48 parser.add_argument(
'--no-output',
49 help=
"Don't write any output files",
50 action=
"store_true", default=
False)
52 parser.add_argument(
'--number-processes', type=int, default=multiprocessing.cpu_count() - 5,
53 help=
'Number of parallel processes to use')
54 parser.add_argument(
'--local-db-path', type=str,
55 help=
"set path to the local payload locations to use for the ConditionDB",
56 default=constants.DEFAULT_DB_FILE_LOCATION)
57 parser.add_argument(
'--local-db-tag', type=str, nargs=
"*",
58 help=
"Use the local db with a specific tag (can be applied multiple times, order is relevant)")
59 parser.add_argument(
'--central-db-tag', type=str, nargs=
"*",
60 help=
"Use the central db with a specific tag (can be applied multiple times, order is relevant)")
61 parser.add_argument(
'--udp-hostname', type=str,
62 help=
"set hostname for UDP logging connection", default=
None)
63 parser.add_argument(
'--udp-port', type=int,
64 help=
"set port number for UDP logging connection", default=
None)
66 args = parser.parse_args()
69 basf2.conditions.override_globaltags()
70 if args.central_db_tag:
71 for central_tag
in args.central_db_tag:
72 basf2.conditions.prepend_globaltag(central_tag)
75 for local_tag
in args.local_db_tag:
76 basf2.conditions.prepend_globaltag(local_tag)
78 basf2.conditions.globaltags = [
"online"]
79 basf2.conditions.metadata_providers = [
"file://" + basf2.find_file(args.local_db_path +
"/metadata.sqlite")]
80 basf2.conditions.payload_locations = [basf2.find_file(args.local_db_path)]
83 basf2.set_nprocesses(args.number_processes)
86 basf2.set_log_level(basf2.LogLevel.ERROR)
89 basf2.logging.enable_escape_newlines =
True
92 if (args.udp_hostname
is not None)
and (args.udp_port
is not None):
93 basf2.logging.add_udp(args.udp_hostname, args.udp_port)
96 basf2.set_realm(
"online")
101def start_path(args, location):
103 Create and return a path used
for HLT
and ExpressReco running
105 path = basf2.create_path()
107 input_buffer_module_name = ""
108 if location == constants.Location.expressreco:
109 input_buffer_module_name =
"Rbuf2Ds"
110 elif location == constants.Location.hlt:
111 input_buffer_module_name =
"Raw2Ds"
113 basf2.B2FATAL(f
"Does not know location {location}")
116 if not args.input_file:
117 path.add_module(input_buffer_module_name, RingBufferName=args.input_buffer_name)
119 if args.input_file.endswith(
".sroot"):
120 path.add_module(
'SeqRootInput', inputFileName=args.input_file)
122 path.add_module(
'RootInput', inputFileName=args.input_file)
125 if not args.histo_output_file:
126 path.add_module(
'DqmHistoManager', Port=args.histo_port, DumpInterval=1000, workDirName=tempfile.gettempdir()+
"/")
128 workdir = os.path.dirname(args.histo_output_file)
129 filename = os.path.basename(args.histo_output_file)
130 path.add_module(
'HistoManager', histoFileName=filename, workDirName=workdir)
135def start_zmq_path(args, location):
137 reco_path = basf2.Path()
139 if location == constants.Location.expressreco:
140 input_module = path.add_module(
"HLTZMQ2Ds", input=args.input, addExpressRecoObjects=
True)
141 elif location == constants.Location.hlt:
142 input_module = path.add_module(
"HLTZMQ2Ds", input=args.input)
144 basf2.B2FATAL(f
"Does not know location {location}")
146 input_module.if_value(
"==0", reco_path, basf2.AfterConditionPath.CONTINUE)
147 reco_path.add_module(
"HLTDQM2ZMQ", output=args.dqm, sendOutInterval=30)
149 return path, reco_path
152def add_hlt_processing(path,
153 run_type=constants.RunTypes.beam,
154 softwaretrigger_mode=constants.SoftwareTriggerModes.filter,
157 unpacker_components=None,
158 reco_components=None,
159 create_hlt_unit_histograms=True,
160 switch_off_slow_modules_for_online=True,
163 Add all modules for processing on HLT filter machines
167 if run_type == constants.RunTypes.cosmic:
168 basf2.declare_cosmics()
171 if run_type == constants.RunTypes.beam:
177 path.add_module(
'StatisticsSummary').set_name(
'Sum_Wait')
179 if unpacker_components
is None:
180 unpacker_components = constants.DEFAULT_HLT_COMPONENTS
181 if reco_components
is None:
182 reco_components = constants.DEFAULT_HLT_COMPONENTS
184 check_components(unpacker_components)
185 check_components(reco_components)
191 path.add_module(
"PruneDataStore", matchEntries=constants.HLT_INPUT_OBJECTS)
194 path_utils.add_geometry_if_not_present(path)
195 path.add_module(
'StatisticsSummary').set_name(
'Sum_Initialization')
198 add_unpackers(path, components=unpacker_components, writeKLMDigitRaws=
True)
199 path.add_module(
'StatisticsSummary').set_name(
'Sum_Unpackers')
202 accept_path = basf2.Path()
205 path_utils.add_pre_filter_reconstruction(
208 components=reco_components,
209 switch_off_slow_modules_for_online=switch_off_slow_modules_for_online,
214 path_utils.add_filter_software_trigger(path, store_array_debug_prescale=1)
217 path_utils.add_hlt_dqm(path, run_type=run_type, components=reco_components, dqm_mode=constants.DQMModes.before_filter,
218 create_hlt_unit_histograms=create_hlt_unit_histograms)
221 if softwaretrigger_mode == constants.SoftwareTriggerModes.filter:
223 hlt_filter_module = path_utils.add_filter_module(path)
227 path_utils.hlt_event_abort(hlt_filter_module,
"==0", ROOT.Belle2.EventMetaData.c_HLTDiscard)
229 hlt_filter_module.if_value(
"==1", accept_path, basf2.AfterConditionPath.CONTINUE)
230 elif softwaretrigger_mode == constants.SoftwareTriggerModes.monitor:
232 path.add_path(accept_path)
234 basf2.B2FATAL(f
"The software trigger mode {softwaretrigger_mode} is not supported.")
237 path_utils.add_post_filter_reconstruction(
240 components=reco_components,
241 switch_off_slow_modules_for_online=switch_off_slow_modules_for_online
245 add_roi_finder(accept_path)
246 accept_path.add_module(
'StatisticsSummary').set_name(
'Sum_ROI_Finder')
249 path_utils.add_hlt_dqm(
252 components=reco_components,
253 dqm_mode=constants.DQMModes.filtered,
254 create_hlt_unit_histograms=create_hlt_unit_histograms)
261 pxd_ignores_hlt_decision = (softwaretrigger_mode == constants.SoftwareTriggerModes.monitor)
262 add_roi_payload_assembler(path, ignore_hlt_decision=pxd_ignores_hlt_decision)
263 path.add_module(
'StatisticsSummary').set_name(
'Sum_ROI_Payload_Assembler')
266 path_utils.add_hlt_dqm(path, run_type=run_type, components=reco_components, dqm_mode=constants.DQMModes.all_events,
267 create_hlt_unit_histograms=create_hlt_unit_histograms)
271 path_utils.add_store_only_rawdata_path(path)
272 path.add_module(
'StatisticsSummary').set_name(
'Sum_Close_Event')
275def add_hlt_passthrough(path):
277 Add all modules for operating HLT machines
in passthrough mode.
279 add_pxd_percentframe(path, fraction=0.1, random_position=True)
280 add_roi_payload_assembler(path, ignore_hlt_decision=
True)
283def add_expressreco_processing(path,
284 run_type=constants.RunTypes.beam,
285 select_only_accepted_events=False,
288 unpacker_components=None,
289 reco_components=None,
290 do_reconstruction=True,
291 switch_off_slow_modules_for_online=True,
294 Add all modules for processing on the ExpressReco machines
298 if run_type == constants.RunTypes.cosmic:
299 basf2.declare_cosmics()
302 if run_type == constants.RunTypes.beam:
305 if unpacker_components
is None:
306 unpacker_components = constants.DEFAULT_EXPRESSRECO_COMPONENTS
307 if reco_components
is None:
308 reco_components = constants.DEFAULT_EXPRESSRECO_COMPONENTS
310 check_components(unpacker_components)
311 check_components(reco_components)
316 if select_only_accepted_events:
317 skim_module = path.add_module(
"TriggerSkim", triggerLines=[
"software_trigger_cut&all&total_result"], resultOnMissing=0)
318 skim_module.if_value(
"==0", basf2.Path(), basf2.AfterConditionPath.END)
324 path.add_module(
"PruneDataStore", matchEntries=constants.EXPRESSRECO_INPUT_OBJECTS)
326 path_utils.add_geometry_if_not_present(path)
327 add_unpackers(path, components=unpacker_components, writeKLMDigitRaws=
True)
330 basf2.set_module_parameters(path,
"PXDPostErrorChecker", CriticalErrorMask=0)
332 if do_reconstruction:
333 if run_type == constants.RunTypes.beam:
334 add_reconstruction(path,
335 components=reco_components,
337 skipGeometryAdding=
True,
338 add_trigger_calculation=
False,
339 switch_off_slow_modules_for_online=switch_off_slow_modules_for_online,
341 elif run_type == constants.RunTypes.cosmic:
342 add_cosmics_reconstruction(path, components=reco_components, pruneTracks=
False,
343 skipGeometryAdding=
True, **kwargs)
345 basf2.B2FATAL(f
"Run Type {run_type} not supported.")
347 basf2.set_module_parameters(path,
"SVDTimeGrouping", forceGroupingFromDB=
False,
348 isEnabledIn6Samples=
True, isEnabledIn3Samples=
True)
350 path_utils.add_expressreco_dqm(path, run_type, components=reco_components)
353 path.add_module(
"PruneDataStore", matchEntries=constants.ALWAYS_SAVE_OBJECTS + constants.RAWDATA_OBJECTS +
354 constants.PROCESSED_OBJECTS)
357def finalize_path(path, args, location, show_progress_bar=True):
359 Add the required output modules for expressreco/HLT
361 save_objects = constants.ALWAYS_SAVE_OBJECTS + constants.RAWDATA_OBJECTS
362 if location == constants.Location.expressreco:
363 save_objects += constants.PROCESSED_OBJECTS
365 if show_progress_bar:
366 path.add_module(
"Progress")
369 basf2.set_streamobjs(save_objects)
374 output_buffer_module_name =
""
375 if location == constants.Location.expressreco:
376 output_buffer_module_name =
"Ds2Sample"
377 elif location == constants.Location.hlt:
378 output_buffer_module_name =
"Ds2Rbuf"
380 basf2.B2FATAL(f
"Does not know location {location}")
382 if not args.output_file:
383 path.add_module(output_buffer_module_name, RingBufferName=args.output_buffer_name,
384 saveObjs=save_objects)
386 if args.output_file.endswith(
".sroot"):
387 path.add_module(
"SeqRootOutput", saveObjs=save_objects, outputFileName=args.output_file)
390 path.add_module(
"RootOutput", outputFileName=args.output_file)
393def finalize_zmq_path(path, args, location):
395 Add the required output modules for expressreco/HLT
397 save_objects = constants.ALWAYS_SAVE_OBJECTS + constants.RAWDATA_OBJECTS
398 if location == constants.Location.expressreco:
399 save_objects += constants.PROCESSED_OBJECTS
402 basf2.set_streamobjs(save_objects)
404 if location == constants.Location.expressreco:
405 path.add_module(
"HLTDs2ZMQ", output=args.output, raw=
False, outputConfirmation=
False)
406 elif location == constants.Location.hlt:
407 path.add_module(
"HLTDs2ZMQ", output=args.output, raw=
True, outputConfirmation=
True)
409 basf2.B2FATAL(f
"Does not know location {location}")