Belle II Software  release-06-01-15
submit_create_mcruns.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 # Script using CAF backends to submit jobs for generating of mc files with PXDSimHits for
13 # PXD gain calibration.
14 #
15 # basf2 submit_create_mcruns.py -- --backend='local' --outputdir='pxd_mc_phase2' --runLow=4000 --runHigh=6522 --expNo=3
16 
17 import sys
18 import os
19 import pickle
20 from caf import backends
21 from pathlib import Path
22 from caf.utils import IoV
23 
24 
25 _tempdir_name = 'tmp_create_mc'
26 
27 
28 _number_of_sets = 10
29 
30 
31 _tag = 'Calibration_Offline_Development'
32 
33 
34 _bg = '/group/belle2/BGFile/OfficialBKG/15thCampaign/phase2'
35 
36 
37 def run_job_submission(backend, mc_iov_list):
38 
39  # List of jobs to be submitted
40  jobs = []
41 
42  for mc_iov in mc_iov_list:
43  for setNo in range(0, _number_of_sets):
44  expNo = mc_iov.exp_low
45  runNo = mc_iov.run_low
46  job = backends.Job("Create_Exp_{}_Run_{}_Set_{}".format(expNo, runNo, setNo))
47  job.output_dir = str(Path(_tempdir_name, job.name).absolute())
48  job.working_dir = str(Path(_tempdir_name, job.name).absolute())
49  job.cmd = [
50  'basf2',
51  'create_mcrun.py',
52  '--',
53  '--tag={}'.format(_tag),
54  '--bg={}'.format(_bg),
55  '--expNo={}'.format(expNo),
56  '--runNo={}'.format(runNo),
57  '--setNo={}'.format(setNo)]
58 
59  # Any files appended to this list will be place in the Job's working directory
60  job.input_sandbox_files.append("create_mcrun.py")
61 
62  # Sometimes you need to configure how the backend will treat your Job but in a backend specific way
63  # Any arguments to deal with this should be created in the backend_args dictionary
64  # Here we set the batch queue used to submit the job
65  if isinstance(backend, backends.LSF):
66  job.backend_args['queue'] = 's'
67  elif isinstance(backend, backends.PBS):
68  job.backend_args['queue'] = 'short'
69 
70  # Append to job to jobs
71  jobs.append(job)
72 
73  # Submit alls jobs at once
74  backend.submit(jobs)
75 
76  from basf2 import B2INFO
77  import time
78 
79  # Jobs provide a job.ready() function which returns True once the Job (or all its SubJobs) have finished their running
80  ready = False
81  while not ready:
82  ready = all(job.ready() for job in jobs)
83  B2INFO("Not finished all jobs yet")
84  time.sleep(5)
85  B2INFO("Finished")
86 
87 
88 def main(backend_choice, mc_iov_list, output_dir):
89  # Create a backend of our choosing
90  if backend_choice == "qsub":
91  backend = backends.PBS()
92  elif backend_choice == "bsub":
93  backend = backends.LSF()
94  elif backend_choice == "local":
95  backend = backends.Local(max_processes=20)
96  else:
97  print("How did I get here?")
98  sys.exit(1)
99  run_job_submission(backend, mc_iov_list)
100  if backend_choice == 'local':
101  # Should really be closing down the processing pool once we're done submitting.
102  backend.join()
103 
104  # Find all output files
105  import glob
106  output_files = glob.glob(str(Path(_tempdir_name).absolute()) + '/**/beam*.root')
107 
108  # Copy all outputs to output_dir
109  import shutil
110  for file_name in output_files:
111  shutil.move(file_name, output_dir)
112 
113 
114 if __name__ == "__main__":
115 
116  import argparse
117  parser = argparse.ArgumentParser(description="Submit jobs for creattion of SimHits for a run with user specified ExpRuns")
118  parser.add_argument('--outputdir', default='pxd_mc_phase2', type=str,
119  help='Add all simulated files to this folder. Create it if it does not exist.')
120  parser.add_argument('--backend', default='local', type=str, help='Where <backend> is one of: local, qsub, bsub.')
121  parser.add_argument('--runLow', default=0, type=int, help='Compute mask for specific IoV')
122  parser.add_argument('--runHigh', default=-1, type=int, help='Compute mask for specific IoV')
123  parser.add_argument('--expNo', default=3, type=int, help='Compute mask for specific IoV')
124  args = parser.parse_args()
125 
126  # Directory for collecting generated files for later usage in PXD gain calibration
127  output_dir = str(Path(args.outputdir).absolute())
128 
129  # Make sure output_dir exists
130  if not os.path.exists(output_dir):
131  os.makedirs(output_dir)
132 
133  # Set the IoV range for mc calibration
134  iov_to_calibrate = IoV(exp_low=args.expNo, run_low=args.runLow, exp_high=args.expNo, run_high=args.runHigh)
135 
136  # Access files_to_iovs for beam runs
137  with open("file_iov_map.pkl", 'br') as map_file:
138  files_to_iovs = pickle.load(map_file)
139 
140  # Set of all IoVs for beam runs (data). Potentially very, very large.
141  data_iov_set = set(files_to_iovs.values())
142 
143  # List of IoV for mc runs to be submitted to backends
144  mc_iov_list = []
145  for data_iov in data_iov_set:
146  if iov_to_calibrate.contains(data_iov):
147  mc_iov_list.append(data_iov)
148 
149  print('Number selected iovs for mc generation : {}'.format(len(mc_iov_list)))
150 
151  main(args.backend, mc_iov_list, output_dir)
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:75