Belle II Software  release-06-01-15
reco_cdst.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 '''
13 Test more or less everything.
14 '''
15 
16 import basf2
17 import b2test_utils as b2u
18 import argparse
19 import glob
20 import os
21 import subprocess as sp
22 import multiprocessing as mp
23 import concurrent.futures as cf
24 import functools as ft
25 
26 
27 def arg_parser():
28  '''
29  A very simple argument parser.
30  '''
31  parser = argparse.ArgumentParser(description=__doc__)
32  parser.add_argument('-j', '--jobs',
33  default=None,
34  type=int,
35  help='Number of workers to be used by ProcessPoolExecutor.',
36  metavar='JOBS')
37  return parser
38 
39 
40 def steer_this(steering_file, rawdata_file):
41  '''
42  Execute the steering file over the given input rawdata file.
43  '''
44  if 'cosmic' in os.path.basename(rawdata_file):
45  basf2.B2INFO(f'Running the test using {rawdata_file} as input file.')
46  return sp.call(['basf2', steering_file, rawdata_file, 'cosmic'])
47  elif 'physics' in os.path.basename(rawdata_file):
48  basf2.B2INFO(f'Running the test using {rawdata_file} as input file.')
49  return sp.call(['basf2', steering_file, rawdata_file, 'physics'])
50  else:
51  basf2.B2INFO('Not a "cosmic" neither a "physics" run, skipping it.')
52  return 0
53 
54 
55 if __name__ == '__main__':
56  # Skip this test if needed.
57  if 'BELLE2_VALIDATION_DATA_DIR' not in os.environ:
58  b2u.skip_test('BELLE2_VALIDATION_DATA_DIR environment variable not set, skipping the test.')
59  # First, let's check in a smart way how many cores we can use (or override it).
60  args = arg_parser().parse_args()
61  num_workers = None
62  if args.jobs is None:
63  num_workers = max(int(mp.cpu_count() / (1. + os.getloadavg()[1])), 1)
64  else: # Ok, we override the number of workers...
65  num_workers = args.jobs
66  basf2.B2INFO(f'The test will be executed using {num_workers} workers.')
67  num_workers = max(int(mp.cpu_count() / (1. + os.getloadavg()[1])), 1)
68  # Then, let's run the test!
69  steering_file = basf2.find_file('reconstruction/tests/reco_cdst.py_noexec')
70  rawdata_files = glob.glob(os.environ['BELLE2_VALIDATION_DATA_DIR'] + '/rawdata/*HLT?.*.root')
71  with cf.ProcessPoolExecutor(max_workers=num_workers) as pool:
72  for result in pool.map(ft.partial(steer_this, steering_file), rawdata_files):
73  assert(result == 0)