10 import multiprocessing
15 from softwaretrigger
import constants
16 from pxd
import add_roi_payload_assembler, add_roi_finder
18 from reconstruction
import add_reconstruction, add_cosmics_reconstruction
19 from softwaretrigger
import path_utils
20 from geometry
import check_components
21 from rawdata
import add_unpackers
24 def setup_basf2_and_db(zmq=False):
26 Setup local database usage for HLT
28 parser = argparse.ArgumentParser(description=
'basf2 for online')
31 parser.add_argument(
"--input", required=
True, type=str, help=
"ZMQ Address of the distributor process")
32 parser.add_argument(
"--output", required=
True, type=str, help=
"ZMQ Address of the collector process")
33 parser.add_argument(
"--dqm", required=
True, type=str, help=
"ZMQ Address of the histoserver process")
35 parser.add_argument(
'input_buffer_name', type=str,
36 help=
'Input Ring Buffer names')
37 parser.add_argument(
'output_buffer_name', type=str,
38 help=
'Output Ring Buffer name')
39 parser.add_argument(
'histo_port', type=int,
40 help=
'Port of the HistoManager to connect to')
41 parser.add_argument(
'--input-file', type=str,
42 help=
"Input sroot file, if set no RingBuffer input will be used",
44 parser.add_argument(
'--output-file', type=str,
45 help=
"Filename for SeqRoot output, if set no RingBuffer output will be used",
47 parser.add_argument(
'--histo-output-file', type=str,
48 help=
"Filename for histogram output",
50 parser.add_argument(
'--no-output',
51 help=
"Don't write any output files",
52 action=
"store_true", default=
False)
54 parser.add_argument(
'--number-processes', type=int, default=multiprocessing.cpu_count() - 5,
55 help=
'Number of parallel processes to use')
56 parser.add_argument(
'--local-db-path', type=str,
57 help=
"set path to the local payload locations to use for the ConditionDB",
58 default=constants.DEFAULT_DB_FILE_LOCATION)
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)")
62 args = parser.parse_args()
65 basf2.conditions.override_globaltags()
66 if args.central_db_tag:
67 for central_tag
in args.central_db_tag:
68 basf2.conditions.prepend_globaltag(central_tag)
70 basf2.conditions.globaltags = [
"online"]
71 basf2.conditions.metadata_providers = [
"file://" + basf2.find_file(args.local_db_path +
"/metadata.sqlite")]
72 basf2.conditions.payload_locations = [basf2.find_file(args.local_db_path)]
75 basf2.set_nprocesses(args.number_processes)
78 basf2.set_log_level(basf2.LogLevel.ERROR)
81 basf2.logging.enable_escape_newlines =
True
84 basf2.set_realm(
"online")
89 def start_path(args, location):
91 Create and return a path used for HLT and ExpressReco running
93 path = basf2.create_path()
95 input_buffer_module_name =
""
96 if location == constants.Location.expressreco:
97 input_buffer_module_name =
"Rbuf2Ds"
98 elif location == constants.Location.hlt:
99 input_buffer_module_name =
"Raw2Ds"
101 basf2.B2FATAL(f
"Does not know location {location}")
104 if not args.input_file:
105 path.add_module(input_buffer_module_name, RingBufferName=args.input_buffer_name)
107 if args.input_file.endswith(
".sroot"):
108 path.add_module(
'SeqRootInput', inputFileName=args.input_file)
110 path.add_module(
'RootInput', inputFileName=args.input_file)
113 if not args.histo_output_file:
114 path.add_module(
'DqmHistoManager', Port=args.histo_port, DumpInterval=1000, workDirName=
"/tmp/")
116 workdir = os.path.dirname(args.histo_output_file)
117 filename = os.path.basename(args.histo_output_file)
118 path.add_module(
'HistoManager', histoFileName=filename, workDirName=workdir)
123 def start_zmq_path(args, location):
125 reco_path = basf2.Path()
127 if location == constants.Location.expressreco:
128 input_module = path.add_module(
"HLTZMQ2Ds", input=args.input, addExpressRecoObjects=
True)
129 elif location == constants.Location.hlt:
130 input_module = path.add_module(
"HLTZMQ2Ds", input=args.input)
132 basf2.B2FATAL(f
"Does not know location {location}")
134 input_module.if_value(
"==0", reco_path, basf2.AfterConditionPath.CONTINUE)
135 reco_path.add_module(
"HLTDQM2ZMQ", output=args.dqm, sendOutInterval=30)
137 return path, reco_path
140 def add_hlt_processing(path,
141 run_type=constants.RunTypes.beam,
142 softwaretrigger_mode=constants.SoftwareTriggerModes.filter,
145 unpacker_components=None,
146 reco_components=None,
147 create_hlt_unit_histograms=True,
150 Add all modules for processing on HLT filter machines
152 path.add_module(
'StatisticsSummary').set_name(
'Sum_Wait')
154 if unpacker_components
is None:
155 unpacker_components = constants.DEFAULT_HLT_COMPONENTS
156 if reco_components
is None:
157 reco_components = constants.DEFAULT_HLT_COMPONENTS
159 check_components(unpacker_components)
160 check_components(reco_components)
166 path.add_module(
"PruneDataStore", matchEntries=constants.HLT_INPUT_OBJECTS)
169 path_utils.add_geometry_if_not_present(path)
170 path.add_module(
'StatisticsSummary').set_name(
'Sum_Initialization')
173 add_unpackers(path, components=unpacker_components, writeKLMDigitRaws=
True)
174 path.add_module(
'StatisticsSummary').set_name(
'Sum_Unpackers')
177 accept_path = basf2.Path()
180 path_utils.add_pre_filter_reconstruction(path, run_type=run_type, components=reco_components, **kwargs)
183 path_utils.add_filter_software_trigger(path, store_array_debug_prescale=1)
186 path_utils.add_hlt_dqm(path, run_type=run_type, components=reco_components, dqm_mode=constants.DQMModes.before_filter,
187 create_hlt_unit_histograms=create_hlt_unit_histograms)
190 if softwaretrigger_mode == constants.SoftwareTriggerModes.filter:
192 hlt_filter_module = path_utils.add_filter_module(path)
196 path_utils.hlt_event_abort(hlt_filter_module,
"==0", ROOT.Belle2.EventMetaData.c_HLTDiscard)
198 hlt_filter_module.if_value(
"==1", accept_path, basf2.AfterConditionPath.CONTINUE)
199 elif softwaretrigger_mode == constants.SoftwareTriggerModes.monitor:
201 path.add_path(accept_path)
203 basf2.B2FATAL(f
"The software trigger mode {softwaretrigger_mode} is not supported.")
206 path_utils.add_post_filter_reconstruction(accept_path, run_type=run_type, components=reco_components)
209 add_roi_finder(accept_path)
210 accept_path.add_module(
'StatisticsSummary').set_name(
'Sum_ROI_Finder')
213 path_utils.add_hlt_dqm(
216 components=reco_components,
217 dqm_mode=constants.DQMModes.filtered,
218 create_hlt_unit_histograms=create_hlt_unit_histograms)
225 pxd_ignores_hlt_decision = (softwaretrigger_mode == constants.SoftwareTriggerModes.monitor)
226 add_roi_payload_assembler(path, ignore_hlt_decision=pxd_ignores_hlt_decision)
227 path.add_module(
'StatisticsSummary').set_name(
'Sum_ROI_Payload_Assembler')
230 path_utils.add_hlt_dqm(path, run_type=run_type, components=reco_components, dqm_mode=constants.DQMModes.all_events,
231 create_hlt_unit_histograms=create_hlt_unit_histograms)
235 path_utils.add_store_only_rawdata_path(path)
236 path.add_module(
'StatisticsSummary').set_name(
'Sum_Close_Event')
239 def add_expressreco_processing(path,
240 run_type=constants.RunTypes.beam,
241 select_only_accepted_events=False,
244 unpacker_components=None,
245 reco_components=None,
246 do_reconstruction=True,
249 Add all modules for processing on the ExpressReco machines
251 if unpacker_components
is None:
252 unpacker_components = constants.DEFAULT_EXPRESSRECO_COMPONENTS
253 if reco_components
is None:
254 reco_components = constants.DEFAULT_EXPRESSRECO_COMPONENTS
256 check_components(unpacker_components)
257 check_components(reco_components)
262 if select_only_accepted_events:
263 skim_module = path.add_module(
"TriggerSkim", triggerLines=[
"software_trigger_cut&all&total_result"], resultOnMissing=0)
264 skim_module.if_value(
"==0", basf2.Path(), basf2.AfterConditionPath.END)
270 path.add_module(
"PruneDataStore", matchEntries=constants.EXPRESSRECO_INPUT_OBJECTS)
272 path_utils.add_geometry_if_not_present(path)
273 add_unpackers(path, components=unpacker_components, writeKLMDigitRaws=
True)
276 basf2.set_module_parameters(path,
"PXDPostErrorChecker", CriticalErrorMask=0)
278 if do_reconstruction:
279 if run_type == constants.RunTypes.beam:
280 add_reconstruction(path, components=reco_components, pruneTracks=
False,
281 skipGeometryAdding=
True, add_trigger_calculation=
False, **kwargs)
282 elif run_type == constants.RunTypes.cosmic:
283 add_cosmics_reconstruction(path, components=reco_components, pruneTracks=
False,
284 skipGeometryAdding=
True, **kwargs)
286 basf2.B2FATAL(
"Run Type {} not supported.".format(run_type))
288 path_utils.add_expressreco_dqm(path, run_type, components=reco_components)
291 path.add_module(
"PruneDataStore", matchEntries=constants.ALWAYS_SAVE_OBJECTS + constants.RAWDATA_OBJECTS +
292 constants.PROCESSED_OBJECTS)
295 def finalize_path(path, args, location, show_progress_bar=True):
297 Add the required output modules for expressreco/HLT
299 save_objects = constants.ALWAYS_SAVE_OBJECTS + constants.RAWDATA_OBJECTS
300 if location == constants.Location.expressreco:
301 save_objects += constants.PROCESSED_OBJECTS
303 if show_progress_bar:
304 path.add_module(
"Progress")
307 basf2.set_streamobjs(save_objects)
312 output_buffer_module_name =
""
313 if location == constants.Location.expressreco:
314 output_buffer_module_name =
"Ds2Sample"
315 elif location == constants.Location.hlt:
316 output_buffer_module_name =
"Ds2Rbuf"
318 basf2.B2FATAL(f
"Does not know location {location}")
320 if not args.output_file:
321 path.add_module(output_buffer_module_name, RingBufferName=args.output_buffer_name,
322 saveObjs=save_objects)
324 if args.output_file.endswith(
".sroot"):
325 path.add_module(
"SeqRootOutput", saveObjs=save_objects, outputFileName=args.output_file)
328 path.add_module(
"RootOutput", outputFileName=args.output_file)
331 def finalize_zmq_path(path, args, location):
333 Add the required output modules for expressreco/HLT
335 save_objects = constants.ALWAYS_SAVE_OBJECTS + constants.RAWDATA_OBJECTS
336 if location == constants.Location.expressreco:
337 save_objects += constants.PROCESSED_OBJECTS
340 basf2.set_streamobjs(save_objects)
342 if location == constants.Location.expressreco:
343 path.add_module(
"HLTDs2ZMQ", output=args.output, raw=
False)
344 elif location == constants.Location.hlt:
345 path.add_module(
"HLTDs2ZMQ", output=args.output, raw=
True)
347 basf2.B2FATAL(f
"Does not know location {location}")