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