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(event_distribution_mode=constants.EventDistributionModes.ringbuffer):
24 Setup local database usage for HLT
26 parser = argparse.ArgumentParser(description='basf2 for online')
28 if event_distribution_mode == constants.EventDistributionModes.ringbuffer:
29 parser.add_argument(
'input_buffer_name', type=str,
30 help=
'Input Ring Buffer names')
31 parser.add_argument(
'output_buffer_name', type=str,
32 help=
'Output Ring Buffer name')
33 parser.add_argument(
'histo_port', type=int,
34 help=
'Port of the HistoManager to connect to')
35 parser.add_argument(
'--input-file', type=str,
36 help=
"Input sroot file, if set no RingBuffer input will be used",
38 parser.add_argument(
'--output-file', type=str,
39 help=
"Filename for SeqRoot output, if set no RingBuffer output will be used",
41 parser.add_argument(
'--histo-output-file', type=str,
42 help=
"Filename for histogram output",
44 parser.add_argument(
'--no-output',
45 help=
"Don't write any output files",
46 action=
"store_true", default=
False)
48 parser.add_argument(
"--input", required=
True, type=str, help=
"ZMQ Address of the distributor process")
49 parser.add_argument(
"--output", required=
True, type=str, help=
"ZMQ Address of the collector process")
50 parser.add_argument(
"--dqm", required=
True, type=str, help=
"ZMQ Address of the histoserver process")
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, event_distribution_mode):
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}")
147 if event_distribution_mode == constants.EventDistributionModes.zmq:
148 input_module.if_value(
"==0", reco_path, basf2.AfterConditionPath.CONTINUE)
149 reco_path.add_module(
"HLTDQM2ZMQ", output=args.dqm, sendOutInterval=30)
151 elif event_distribution_mode == constants.EventDistributionModes.zmqbasf2:
152 path.add_module(
"HLTDQM2ZMQ", output=args.dqm, sendOutInterval=30)
154 basf2.B2FATAL(f
"Does not know event_distribution_mode {event_distribution_mode}")
156 return path, reco_path
159def add_hlt_processing(path,
160 run_type=constants.RunTypes.beam,
161 softwaretrigger_mode=constants.SoftwareTriggerModes.filter,
164 unpacker_components=None,
165 reco_components=None,
166 create_hlt_unit_histograms=True,
167 switch_off_slow_modules_for_online=True,
168 hlt_prefilter_mode=constants.HLTPrefilterModes.monitor,
172 Add all modules for processing on HLT filter machines
176 if run_type == constants.RunTypes.cosmic:
177 basf2.declare_cosmics()
180 if run_type == constants.RunTypes.beam:
183 if dqm_run_type
is None:
184 dqm_run_type = run_type
189 path.add_module(
'StatisticsSummary').set_name(
'Sum_Wait')
191 if unpacker_components
is None:
192 unpacker_components = constants.DEFAULT_HLT_COMPONENTS
193 if reco_components
is None:
194 reco_components = constants.DEFAULT_HLT_COMPONENTS
196 check_components(unpacker_components)
197 check_components(reco_components)
203 path.add_module(
"PruneDataStore", matchEntries=constants.HLT_INPUT_OBJECTS)
206 path_utils.add_geometry_if_not_present(path)
207 path.add_module(
'StatisticsSummary').set_name(
'Sum_Initialization')
210 add_unpackers(path, components=unpacker_components, writeKLMDigitRaws=
True)
211 path.add_module(
'StatisticsSummary').set_name(
'Sum_Unpackers')
214 path_utils.add_prefilter_module(path, mode=hlt_prefilter_mode)
217 accept_path = basf2.Path()
220 path_utils.add_pre_filter_reconstruction(
223 components=reco_components,
224 switch_off_slow_modules_for_online=switch_off_slow_modules_for_online,
229 path_utils.add_filter_software_trigger(path, store_array_debug_prescale=1)
232 path_utils.add_hlt_dqm(path, run_type=dqm_run_type, components=reco_components, dqm_mode=constants.DQMModes.before_filter,
233 create_hlt_unit_histograms=create_hlt_unit_histograms)
236 if softwaretrigger_mode == constants.SoftwareTriggerModes.filter:
238 hlt_filter_module = path_utils.add_filter_module(path)
242 path_utils.hlt_event_abort(hlt_filter_module,
"==0", ROOT.Belle2.EventMetaData.c_HLTDiscard)
244 hlt_filter_module.if_value(
"==1", accept_path, basf2.AfterConditionPath.CONTINUE)
245 elif softwaretrigger_mode == constants.SoftwareTriggerModes.monitor:
247 path.add_path(accept_path)
249 basf2.B2FATAL(f
"The software trigger mode {softwaretrigger_mode} is not supported.")
252 path_utils.add_post_filter_reconstruction(
255 components=reco_components,
256 switch_off_slow_modules_for_online=switch_off_slow_modules_for_online
260 add_roi_finder(accept_path)
261 accept_path.add_module(
'StatisticsSummary').set_name(
'Sum_ROI_Finder')
264 path_utils.add_hlt_dqm(
266 run_type=dqm_run_type,
267 components=reco_components,
268 dqm_mode=constants.DQMModes.filtered,
269 create_hlt_unit_histograms=create_hlt_unit_histograms)
276 pxd_ignores_hlt_decision = (softwaretrigger_mode == constants.SoftwareTriggerModes.monitor)
277 add_roi_payload_assembler(path, ignore_hlt_decision=pxd_ignores_hlt_decision)
278 path.add_module(
'StatisticsSummary').set_name(
'Sum_ROI_Payload_Assembler')
281 path_utils.add_hlt_dqm(path, run_type=dqm_run_type, components=reco_components, dqm_mode=constants.DQMModes.all_events,
282 create_hlt_unit_histograms=create_hlt_unit_histograms)
286 path_utils.add_store_only_rawdata_path(path)
287 path.add_module(
'StatisticsSummary').set_name(
'Sum_Close_Event')
290def add_hlt_passthrough(path):
292 Add all modules for operating HLT machines
in passthrough mode.
294 add_pxd_percentframe(path, fraction=0.1, random_position=True)
295 add_roi_payload_assembler(path, ignore_hlt_decision=
True)
298def add_expressreco_processing(path,
299 run_type=constants.RunTypes.beam,
300 select_only_accepted_events=False,
303 unpacker_components=None,
304 reco_components=None,
305 do_reconstruction=True,
306 switch_off_slow_modules_for_online=True,
310 Add all modules for processing on the ExpressReco machines
314 if run_type == constants.RunTypes.cosmic:
315 basf2.declare_cosmics()
318 if run_type == constants.RunTypes.beam:
321 if dqm_run_type
is None:
322 dqm_run_type = run_type
324 if unpacker_components
is None:
325 unpacker_components = constants.DEFAULT_EXPRESSRECO_COMPONENTS
326 if reco_components
is None:
327 reco_components = constants.DEFAULT_EXPRESSRECO_COMPONENTS
329 check_components(unpacker_components)
330 check_components(reco_components)
335 if select_only_accepted_events:
336 skim_module = path.add_module(
"TriggerSkim", triggerLines=[
"software_trigger_cut&all&total_result"], resultOnMissing=0)
337 skim_module.if_value(
"==0", basf2.Path(), basf2.AfterConditionPath.END)
343 path.add_module(
"PruneDataStore", matchEntries=constants.EXPRESSRECO_INPUT_OBJECTS)
345 path_utils.add_geometry_if_not_present(path)
346 add_unpackers(path, components=unpacker_components, writeKLMDigitRaws=
True)
349 basf2.set_module_parameters(path,
"PXDPostErrorChecker", CriticalErrorMask=0)
351 if do_reconstruction:
352 if run_type == constants.RunTypes.beam:
353 add_reconstruction(path,
354 components=reco_components,
356 skipGeometryAdding=
True,
357 add_trigger_calculation=
False,
358 switch_off_slow_modules_for_online=switch_off_slow_modules_for_online,
360 elif run_type == constants.RunTypes.cosmic:
361 add_cosmics_reconstruction(path, components=reco_components, pruneTracks=
False,
362 skipGeometryAdding=
True, **kwargs)
364 basf2.B2FATAL(f
"Run Type {run_type} not supported.")
366 basf2.set_module_parameters(path,
"SVDTimeGrouping", forceGroupingFromDB=
False,
367 isEnabledIn6Samples=
True, isEnabledIn3Samples=
True)
369 path_utils.add_expressreco_dqm(path, dqm_run_type, components=reco_components)
372 path.add_module(
"PruneDataStore", matchEntries=constants.ALWAYS_SAVE_OBJECTS + constants.RAWDATA_OBJECTS +
373 constants.PROCESSED_OBJECTS)
376def finalize_path(path, args, location, show_progress_bar=True):
378 Add the required output modules for expressreco/HLT
380 save_objects = constants.ALWAYS_SAVE_OBJECTS + constants.RAWDATA_OBJECTS
381 if location == constants.Location.expressreco:
382 save_objects += constants.PROCESSED_OBJECTS
384 if show_progress_bar:
385 path.add_module(
"Progress")
388 basf2.set_streamobjs(save_objects)
393 output_buffer_module_name =
""
394 if location == constants.Location.expressreco:
395 output_buffer_module_name =
"Ds2Sample"
396 elif location == constants.Location.hlt:
397 output_buffer_module_name =
"Ds2Rbuf"
399 basf2.B2FATAL(f
"Does not know location {location}")
401 if not args.output_file:
402 path.add_module(output_buffer_module_name, RingBufferName=args.output_buffer_name,
403 saveObjs=save_objects)
405 if args.output_file.endswith(
".sroot"):
406 path.add_module(
"SeqRootOutput", saveObjs=save_objects, outputFileName=args.output_file)
409 path.add_module(
"RootOutput", outputFileName=args.output_file)
412def finalize_zmq_path(path, args, location):
414 Add the required output modules for expressreco/HLT
416 save_objects = constants.ALWAYS_SAVE_OBJECTS + constants.RAWDATA_OBJECTS
417 if location == constants.Location.expressreco:
418 save_objects += constants.PROCESSED_OBJECTS
421 basf2.set_streamobjs(save_objects)
423 if location == constants.Location.expressreco:
424 path.add_module(
"HLTDs2ZMQ", output=args.output, raw=
False, outputConfirmation=
False)
425 elif location == constants.Location.hlt:
426 path.add_module(
"HLTDs2ZMQ", output=args.output, raw=
True, outputConfirmation=
True)
428 basf2.B2FATAL(f
"Does not know location {location}")