Belle II Software development
submit_sampler_kekcc.py
1import glob
2import argparse
3import os
4
5if __name__ == "__main__":
6 parser = argparse.ArgumentParser(description='Sample TFlaT training data')
7 parser.add_argument(
8 '--output_dir',
9 dest='output_dir',
10 type=str,
11 help='Path to directory where sampled root files will be saved to'
12 )
13 parser.add_argument(
14 '--input_dir',
15 dest='input_dir',
16 type=str,
17 help='Path to directory where .mdst (.root) files are stored'
18 )
19 parser.add_argument(
20 '--uniqueIdentifier',
21 dest='uniqueIdentifier',
22 type=str,
23 default="TFlaT_Belle_light_2601_hyperion",
24 help='Name of both the config .yaml to be used and the produced weightfile'
25 )
26 parser.add_argument(
27 '--is_belle',
28 dest='is_belle',
29 type=bool,
30 default=False,
31 help='If True, sample .mdst files with Belle MC'
32 )
33 parser.add_argument(
34 '--channel',
35 metavar='channel',
36 dest='channel',
37 type=str,
38 default='nunu',
39 help='Sampler channel: nunu or jpsiks'
40 )
41
42 args, _ = parser.parse_known_args()
43 output_dir = args.output_dir
44 input_dir = args.input_dir
45 uniqueIdentifier = args.uniqueIdentifier
46 is_belle = args.is_belle
47 channel = args.channel
48
49 # Make a list of input files
50 if is_belle:
51 files = sorted(glob.glob(os.path.join(input_dir, "*.mdst")))
52 else:
53 files = sorted(glob.glob(os.path.join(input_dir, "*.root")))
54
55 print(f"Found {len(files)} files for sampling")
56
57 log_dir = os.path.join(output_dir, 'logs')
58 # Make sure output directory exists
59 os.makedirs(log_dir, exist_ok=True)
60
61 njobs = 0
62 for sampler_id, file in enumerate(files):
63
64 # If the output file exists and the log file indicates success, skip the job.
65 output_file_name = os.path.join(output_dir, uniqueIdentifier + f'_training_data{sampler_id}.root')
66 log_file_name = f'{log_dir}/{uniqueIdentifier}_{sampler_id}.log'
67 if os.path.isfile(output_file_name) and os.path.isfile(log_file_name):
68 with open(log_file_name, 'r') as log_file:
69 content = log_file.read()
70 if '\nSuccessfully completed.\n' in content:
71 continue
72 # remove old log file before submission to avoid later interference with checking for success
73 if os.path.isfile(log_file_name):
74 os.remove(log_file_name)
75
76 # remove old .root file before submission to avoid interference with flavorTagger
77 if os.path.isfile(output_file_name):
78 os.remove(output_file_name)
79
80 # Submit job to create output file
81 os.system(
82 f'bsub -q s -o {log_file_name} python3 sampler.py --uniqueIdentifier {uniqueIdentifier}'
83 f' --inputfile {file} --working_dir {output_dir} --BELLE {str(is_belle)} --sampler_id {sampler_id}'
84 f' --channel {channel}')
85
86 njobs += 1
87
88 if njobs == len(files):
89 print(f"Submitted {njobs} jobs to queue to create {njobs} output files")
90 else:
91 print(f"Submitted {njobs} jobs to queue to create {njobs} missing output files."
92 f" {len(files)-njobs} existing files skipped.")