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