1254 parser = argparse.ArgumentParser(prog='athenaEF.py', formatter_class=
1255 lambda prog : argparse.ArgumentDefaultsHelpFormatter(prog, max_help_position=32, width=100),
1256 usage = '%(prog)s [OPTION]... -f FILE jobOptions',
1257 add_help=False)
1258 parser.expert_groups = []
1259
1260
1261 g = parser.add_argument_group('Options')
1262 g.add_argument('jobOptions', nargs='?', help='job options: CA module (package.module:function) or JSON file (.json)')
1263 g.add_argument('--threads', metavar='N', type=int, default=1, help='number of threads')
1264 g.add_argument('--concurrent-events', metavar='N', type=int, help='number of concurrent events if different from --threads')
1265 g.add_argument('--log-level', '-l', metavar='LVL', default='INFO', help='OutputLevel of athena')
1266 g.add_argument('--precommand', '-c', metavar='CMD', action='append', default=[],
1267 help='Python commands executed before job options')
1268 g.add_argument('--postcommand', '-C', metavar='CMD', action='append', default=[],
1269 help='Python commands executed after job options')
1270 g.add_argument('--interactive', '-i', action='store_true', help='interactive mode')
1271 g.add_argument('--help', '-h', nargs='?', choices=['all'], action=MyHelp, help='show help')
1272
1273 g = parser.add_argument_group('Input/Output')
1274 g.add_argument('--file', '--filesInput', '-f', action='append', help='input RAW file')
1275 g.add_argument('--save-output', '-o', metavar='FILE', help='output file name')
1276 g.add_argument('--number-of-events', '--evtMax', '-n', metavar='N', type=int, default=None,
1277 help='processes N events (default: from DB/config, -1 means all)')
1278 g.add_argument('--skip-events', '--skipEvents', '-k', metavar='N', type=int, default=None,
1279 help='skip N first events')
1280 g.add_argument('--loop-files', action=argparse.BooleanOptionalAction, default=None,
1281 help='loop over input files if no more events')
1282 g.add_argument('--efdf-interface-library', metavar='LIB', default=None,
1283 help='name of the EFDF interface shared library to load (default: TrigDFEmulator)')
1284
1285
1286 g = parser.add_argument_group('Performance and debugging')
1287 g.add_argument('--perfmon', action='store_true', help='enable PerfMon')
1288 g.add_argument('--tcmalloc', action='store_true', default=True, help='use tcmalloc')
1289 g.add_argument('--stdcmalloc', action='store_true', help='use stdcmalloc')
1290 g.add_argument('--stdcmath', action='store_true', help='use stdcmath library')
1291 g.add_argument('--imf', action='store_true', default=True, help='use Intel math library')
1292 g.add_argument('--timeout', metavar='MSEC', type=int, default=None,
1293 help='event processing timeout (HardTimeout) in milliseconds. '
1294 f'NB: only the soft timeout ({SOFT_TIMEOUT_FRACTION*100:.0f}%% of it) is enforced')
1295
1296
1297 g = parser.add_argument_group('Conditions')
1298 g.add_argument('--run-number', '-R', metavar='RUN', type=int,
1299 help='run number (if None, read from first event)')
1300 g.add_argument('--lb-number', '-L', metavar='LBN', type=int,
1301 help='lumiblock number (if None, read from first event)')
1302 g.add_argument('--conditions-run', metavar='RUN', type=int, default=None,
1303 help='reference run number for conditions lookup (use when IS run number has no COOL data)')
1304 g.add_argument('--sor-time', type=arg_sor_time,
1305 help='The Start Of Run time. Three formats are accepted: '
1306 '1) the string "now", for current time; '
1307 '2) the number of nanoseconds since epoch (e.g. 1386355338658000000 or int(time.time() * 1e9)); '
1308 '3) human-readable "20/11/18 17:40:42.3043". If not specified the sor-time is read from the conditions DB')
1309 g.add_argument('--detector-mask', metavar='MASK', type=arg_detector_mask,
1310 help='detector mask (if None, read from the conditions DB), use string "all" to enable all detectors')
1311
1312
1313 g = parser.add_argument_group('Database')
1314 g.add_argument('--use-database', '-b', action='store_true',
1315 help='configure from trigger database using SMK')
1316 g.add_argument('--db-server', metavar='DB', default='TRIGGERDB_RUN3', help='DB server name (alias)')
1317 g.add_argument('--smk', type=int, default=None, help='Super Master Key')
1318 g.add_argument('--l1psk', type=int, default=None, help='L1 prescale key')
1319 g.add_argument('--hltpsk', type=int, default=None, help='HLT prescale key')
1320 g.add_argument('--use-crest', action='store_true', default=False,
1321 help='Use CREST for trigger configuration')
1322 g.add_argument('--crest-server', metavar='URL', default=None,
1323 help='CREST server URL (default: $CREST_SERVER or crest.cern.ch)')
1324 g.add_argument('--dump-config', action='store_true', help='Dump joboptions JSON file')
1325 g.add_argument('--dump-config-exit', action='store_true', help='Dump joboptions JSON file and exit')
1326
1327
1328 g = parser.add_argument_group('Magnets')
1329 g.add_argument('--solenoid-current', type=float, default=None,
1330 help='Solenoid current in Amperes (default: nominal current for offline running, required from IS online)')
1331 g.add_argument('--toroids-current', type=float, default=None,
1332 help='Toroids current in Amperes (default: nominal current for offline running, required from IS online)')
1333
1334
1335 g = parser.add_argument_group('Online')
1336 g.add_argument('--online-environment', action='store_true',
1337 help='Enable online environment: read run parameters from IS and trigger '
1338 'configuration keys (SMK, L1PSK, HLTPSK) from OKS via WEBDAQ REST API')
1339 g.add_argument('--partition', metavar='NAME', default=None,
1340 help='TDAQ partition name (defaults to TDAQ_PARTITION environment variable)')
1341 g.add_argument('--webdaq-base', metavar='URL', default=None,
1342 help='WEBDAQ base URL (defaults to TDAQ_WEBDAQ_BASE environment variable)')
1343
1344
1345 g = parser.add_argument_group('Online Histogramming')
1346 g.add_argument('--oh-monitoring', '-M', action='store_true', default=False,
1347 help='enable online histogram publishing via WebdaqHistSvc')
1348
1349
1350 g = parser.add_argument_group('Expert')
1351 parser.expert_groups.append(g)
1352 (args, unparsed_args) = parser.parse_known_args()
1353 check_args(parser, args)
1354
1355
1356 from PyUtils.Helpers import ROOTSetup
1357 ROOTSetup(batch=True)
1358
1359
1360 import ROOT
1361 ROOT.ROOT.EnableThreadSafety()
1362
1363
1364 import AthenaCommon.Logging
1365 AthenaCommon.Logging.log.setLevel(getattr(logging, args.log_level))
1366 AthenaCommon.Logging.log.setFormat("%(asctime)s Py:%(name)-31s %(levelname)7s %(message)s")
1367
1368
1369 if not args.concurrent_events:
1370 args.concurrent_events = args.threads
1371
1372
1373 is_database = args.use_database
1374 is_json = bool(args.jobOptions) and not is_database and args.jobOptions.endswith('.json')
1375 is_ca = not (is_database or is_json)
1376
1377
1378 log.info("Using CREST for trigger configuration: %s", args.use_crest)
1379 if args.use_crest and args.crest_server is None:
1380 from IOVDbSvc.IOVDbAutoCfgFlags import getCrestConnection
1381 args.crest_server = getCrestConnection()
1382 log.info("Using default CREST server: %s", args.crest_server)
1383
1384 update_run_params(args)
1385
1386
1387
1388
1389
1390 force_psk = args.use_database and ((args.hltpsk is not None) or args.online_environment)
1391
1392 if args.use_database:
1393
1394 update_trigconf_keys(args)
1395
1396
1397 if is_ca:
1398 configure_from_ca(args, unparsed_args)
1399
1400
1404 config_source = "the trigger database" if is_database else "a JSON file"
1405
1406 if unparsed_args:
1407 log.warning("Ignoring flag(s) given on the command line, the configuration is read from %s: %s",
1408 config_source, ' '.join(unparsed_args))
1409
1410
1411
1412
1413 overrides = RuntimeOverrides()
1414
1415 overrides.set('AvalancheSchedulerSvc.ThreadPoolSize', args.threads)
1416 overrides.set('EventDataSvc.NSlots', args.concurrent_events)
1417
1418 ef_files = args.file if args.file else []
1419 if ef_files:
1420 overrides.set('EFInterfaceSvc.Files', ef_files)
1421 overrides.set('EFInterfaceSvc.T0ProjectTag', args.T0_project_tag)
1422 overrides.set('EFInterfaceSvc.BeamType', args.beam_type)
1423 overrides.set('EFInterfaceSvc.BeamEnergy', args.beam_energy)
1424 overrides.set('EFInterfaceSvc.TriggerType', args.trigger_type)
1425 overrides.set('EFInterfaceSvc.Stream', args.stream)
1426 overrides.set('EFInterfaceSvc.Lumiblock', args.lumiblock)
1427 overrides.set('EFInterfaceSvc.DetMask', args.file_detector_mask)
1428 if args.run_number is not None:
1429 overrides.set('EFInterfaceSvc.RunNumber', args.run_number)
1430 if args.save_output is not None:
1431 overrides.set('EFInterfaceSvc.OutputFileName', args.save_output)
1432 if args.loop_files is not None:
1433 overrides.set('EFInterfaceSvc.LoopOverFiles', args.loop_files)
1434 if args.number_of_events is not None:
1435 overrides.set('EFInterfaceSvc.NumEvents', args.number_of_events)
1436 if args.skip_events is not None:
1437 overrides.set('EFInterfaceSvc.SkipEvents', args.skip_events)
1438 if args.efdf_interface_library is not None:
1439 overrides.set('EFInterfaceSvc.EFDFInterfaceLibraryName', args.efdf_interface_library)
1440
1441 if args.timeout is not None:
1442 overrides.set('HltEventLoopMgr.HardTimeout', float(args.timeout))
1443 overrides.set('HltEventLoopMgr.SoftTimeoutFraction', SOFT_TIMEOUT_FRACTION)
1444 if args.conditions_run is not None:
1445
1446 overrides.set('HltEventLoopMgr.forceRunNumber', args.conditions_run)
1447
1448 if force_psk:
1449 overrides.set('HLTPrescaleCondAlg.Source', 'DB')
1450
1451
1452
1453
1454 if not args.online_environment:
1455 if args.oh_monitoring:
1456 overrides.declare_type('THistSvc', 'WebdaqHistSvc')
1457 overrides.create_service('WebdaqInfoSvc')
1458 else:
1459 overrides.declare_type('THistSvc', 'THistSvc')
1460 overrides.drop_service('WebdaqInfoSvc')
1461
1462
1463 for cmd in args.postcommand:
1464 overrides.add_command(cmd)
1465
1466
1467 if args.precommand:
1468 log.info("Executing precommand(s)")
1469 for cmd in args.precommand:
1470 log.info(" %s", cmd)
1471 exec(cmd, globals(), {})
1472
1473 if is_database:
1474
1475
1476 if args.use_crest:
1477 from TrigConfStorage.TriggerCrestUtil import TriggerCrestUtil
1478 crestconn = TriggerCrestUtil.getCrestConnection(args.db_server)
1479 db_alias = f"{args.crest_server}/{crestconn}"
1480 log.info("Loading configuration via CREST from %s with SMK %d", db_alias, args.smk)
1481 else:
1482 db_alias = args.db_server
1483 log.info("Loading configuration from database %s with SMK %d", db_alias, args.smk)
1484
1485
1486 run_params = get_run_params(args).to_dict()
1487 acc = load_from_database(db_alias, args.smk, args.l1psk, args.hltpsk, run_params, overrides=overrides)
1488 log.info("Configuration loaded from database")
1489
1490 else:
1491
1492 log.info("Loading configuration from JSON file: %s", args.jobOptions)
1493
1494 run_params = get_run_params(args).to_dict()
1495 acc = load_from_json(args.jobOptions, run_params, overrides=overrides)
1496 log.info("Configuration loaded from JSON")
1497
1498
1499 if args.dump_config or args.dump_config_exit:
1500 fname = "HLTJobOptions"
1501
1502 if is_database:
1503
1504 from TrigConfIO.HLTTriggerConfigAccess import HLTJobOptionsAccess
1505 log.info("Fetching configuration from database for dump...")
1506 jo_access = HLTJobOptionsAccess(dbalias=acc.db_server, smkey=acc.smk)
1507 props = jo_access.algorithms()
1508
1509 log.info("Dumping configuration to %s.json", fname)
1510 hlt_json = {'filetype': 'joboptions', 'properties': props}
1511 with open(f"{fname}.json", "w") as f:
1512 json.dump(hlt_json, f, indent=4, sort_keys=True, ensure_ascii=True)
1513
1514 elif is_json:
1515
1516 props = acc.properties
1517 if props:
1518 log.info("Dumping configuration to %s.json", fname)
1519 hlt_json = {'filetype': 'joboptions', 'properties': props}
1520 with open(f"{fname}.json", "w") as f:
1521 json.dump(hlt_json, f, indent=4, sort_keys=True, ensure_ascii=True)
1522 else:
1523 log.warning("No properties available to dump")
1524
1525 if args.dump_config_exit:
1526 log.info("Configuration dumped. Exiting...")
1527 sys.exit(0)
1528
1529
1530 log.info("Starting Athena execution...")
1531
1532
1533
1534
1535 worker_dir = os.path.join(os.getcwd(), "athenaHLT_workers", "athenaHLT-01")
1536 if not os.path.exists(worker_dir):
1537 log.info("Creating worker directory: %s", worker_dir)
1538 os.makedirs(worker_dir, exist_ok=True)
1539
1540
1541 oh_infra = start_oh_infrastructure(args) if args.oh_monitoring else None
1542
1543 if args.interactive:
1544 log.info("Interactive mode - call acc.run() to execute")
1545 import code
1546 code.interact(local={'acc': acc})
1547 else:
1548
1549 from AthenaCommon import ExitCodes
1550 exitcode = 0
1551 try:
1552
1553 sc = acc.run(args.number_of_events)
1554 if sc.isFailure():
1555 exitcode = ExitCodes.EXE_ALG_FAILURE
1556 except SystemExit as e:
1557 exitcode = ExitCodes.EXE_ALG_FAILURE if e.code == 1 else e.code
1558 except Exception:
1559 traceback.print_exc()
1560 exitcode = ExitCodes.UNKNOWN_EXCEPTION
1561 finally:
1562 stop_oh_infrastructure(oh_infra)
1563
1564 log.info('Leaving with code %d: "%s"', exitcode, ExitCodes.what(exitcode))
1565 sys.exit(exitcode)
1566
1567