Belle II Software  release-05-01-25
run_local_test.py
1 import os
2 from time import sleep
3 
4 from hlt.clean_execution import CleanBasf2Execution
5 from zmq_daq.example_support import get_sockets
6 from zmq_daq.utils import get_monitor_table, show_monitoring, write_monitoring
7 
8 import yaml
9 import argparse
10 
11 
12 def yaml_settings(yaml_file):
13  with open(yaml_file, "r") as f:
14  return yaml.load(f)
15 
16 
17 def command_load(args):
18  execution = CleanBasf2Execution()
19 
20  for app in args.settings_file["programs"]:
21  execution.start(app)
22 
23  try:
24  execution.wait()
25  except KeyboardInterrupt:
26  pass
27 
28 
29 def command_monitor(args):
30  # Create and connect all needed sockets
31  sockets = get_sockets(args.settings_file)
32 
33  # When no additional things are requested, just show the table once and exit
34  if not args.watch and not args.dat:
35  df = get_monitor_table(sockets, show_detail=args.show_detail)
36  show_monitoring(df)
37  exit()
38 
39  # Else we go into a main loop
40  if args.dat:
41  f = open(args.dat, "wb")
42  try:
43  while True:
44  if args.watch:
45  os.system("clear")
46 
47  df = get_monitor_table(sockets, show_detail=args.show_detail)
48  if args.watch:
49  show_monitoring(df)
50 
51  if args.dat:
52  write_monitoring(df, f)
53 
54  sleep(0.5)
55  finally:
56  if args.dat:
57  f.close()
58 
59 
60 def command_start(args):
61  sockets = get_sockets(args.settings_file)
62  for socket in sockets.values():
63  socket.send_multipart([b"n", b"", b""])
64 
65 
66 def command_input_file(args):
67  execution = CleanBasf2Execution()
68 
69  input_port = args.settings_file["input"]
70  remaining_args = []
71  if args.raw:
72  remaining_args += ["--raw"]
73  if args.repeat:
74  remaining_args += ["--repeat"]
75  execution.start(["b2hlt_file2socket", args.data_file, str(input_port)] + remaining_args)
76 
77  try:
78  execution.wait()
79  except KeyboardInterrupt:
80  pass
81 
82 
83 def command_stop(args):
84  sockets = get_sockets(args.settings_file)
85  sockets["distributor"].send_multipart([b"l", b"", b""])
86 
87  if args.all:
88  for socket in sockets.keys():
89  if socket == "distributor":
90  continue
91 
92  sockets[socket].send_multipart([b"l", b"", b""])
93 
94 
95 def command_terminate(args):
96  sockets = get_sockets(args.settings_file)
97  sockets["distributor"].send_multipart([b"x", b"", b""])
98 
99  if args.all:
100  for socket in sockets.keys():
101  if socket == "distributor":
102  continue
103 
104  sockets[socket].send_multipart([b"x", b"", b""])
105 
106 
107 if __name__ == '__main__':
108  parser = argparse.ArgumentParser(description="""Try out different modes for the ZMQ HLT.
109  By choosing the settings_file, the different modes can be chosen.
110  Together with the settings file, a command needs to be chosen.
111  Typically, this will be "load" in one console and "input_file" in a different console.
112  You can then monitor the process either with the "monitor" command
113  or by using "b2hlt_monitor.py".
114  """)
115 
116  parser.add_argument("settings_file", type=yaml_settings,
117  help="Which config file to load for the test")
118 
119  subparsers = parser.add_subparsers(help="Choose the command", dest="command")
120  subparsers.required = True
121 
122  subparser = subparsers.add_parser("load")
123  subparser.set_defaults(func=command_load)
124 
125  subparser = subparsers.add_parser("monitor")
126  subparser.add_argument(
127  "--show-detail",
128  action="store_true",
129  help="By default, some information are omitted. With this flag, all details are shown (can be very long output).")
130  subparser.add_argument("--watch", action="store_true", help="Enter watch mode, where the script is called every 1 second.")
131  subparser.add_argument("--dat", help="Write out the results of the monitoring periodically into a dat file. " +
132  "If not combined with --watch, the output will not be shown.")
133  subparser.set_defaults(func=command_monitor)
134 
135  subparser = subparsers.add_parser("send_stop")
136  subparser.add_argument("--all", action="store_true", help="Send the signal to all processes")
137  subparser.set_defaults(func=command_stop)
138 
139  subparser = subparsers.add_parser("send_terminate")
140  subparser.add_argument("--all", action="store_true", help="Send the signal to all processes")
141  subparser.set_defaults(func=command_terminate)
142 
143  subparser = subparsers.add_parser("send_start")
144  subparser.set_defaults(func=command_start)
145 
146  subparser = subparsers.add_parser("input_file")
147  subparser.add_argument("data_file", help="Which data file to send")
148  subparser.add_argument("--raw", help="send and receive raw data instead of event buffers", action="store_true")
149  subparser.add_argument("--repeat", help="repeat after the file is finished", action="store_true")
150  subparser.set_defaults(func=command_input_file)
151 
152  args = parser.parse_args()
153  args.func(args)
hlt.clean_execution
Definition: clean_execution.py:1
zmq_daq.utils
Definition: utils.py:1
zmq_daq.example_support
Definition: example_support.py:1