Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
make_inp_lists.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file make_inp_lists.py
1 #!/cvmfs/sphenix.sdcc.bnl.gov/gcc-12.1.0/opt/sphenix/core/bin/python3
2 
3 '''
4 Input:
5  req: local directory full_lists/
6  usage:
7  make_inp_lists.py [num files total] [num files per list]
8  output:
9  new_dir: new directory named 'in_lists_[num files total]_by_[num files per list]'
10  input files lists: [new_dir]/dst_*_[num files per list]_[0,1,...]
11  new queue file: [queue_[num file total]_by_[num files per list].list]
12 '''
13 
14 from glob import glob
15 from sys import argv
16 import os
17 from os import path
18 
19 
20 def main():
21  lines_per_output = 1 # lines per output
22  try:
23  lines_per_output = argv[1]
24  except:
25  pass
26 
27  n_files = -1
28  try:
29  n_files = int(argv[2])
30  except:
31  pass
32 
33  inp_dir = 'full_lists'
34  try:
35  inp_dir = argv[3]
36  except:
37  pass
38 
39  # figure out the output directory tag
40  odir_tag = "jobs"
41  print (f"inp_dir: {inp_dir}")
42  if not inp_dir == "full_lists":
43  print (f"odir_tag: {odir_tag} 0")
44  if "_lists" in inp_dir:
45  odir_tag = inp_dir[:inp_dir.find("_lists")]
46  print (f"odir_tag: {odir_tag} 1")
47  else:
48  odir_tag = "jobs"
49  print (f"odir_tag: {odir_tag} 2")
50 
51 
52  if n_files != -1:
53  odir = f'{odir_tag}_{lines_per_output}x{n_files}'
54  else:
55  odir = f'{odir_tag}_{lines_per_output}xAll'
56 
57  print (f"odir: {odir}")
58 
59  if path.isdir(odir):
60  print(f'replacing contents of {odir}');
61  for f in glob(f'{odir}/*'):
62  os.remove(f)
63  else:
64  os.mkdir(odir)
65 
66  queue = open(f'{odir}/queue.list','w')
67 
68  # determine which input files will be divided
69  in_files = []
70  in_tags = []
71  n_file = -1
72  if (len(glob(f'{inp_dir}/dst_*.list')) == 0):
73  print(f"fatal error: no lists in {inp_dir}")
74  exit()
75 
76  for file in glob(f'{inp_dir}/dst_*.list'):
77  in_files .append(open(file,'r'))
78  in_tags .append(file.split('/')[-1][:-5])
79 
80  try:
81  while True:
82  n_file += 1
83  if n_files != -1 and n_file == n_files:
84  break
85  o_files = []
86  # queue.write(f'out_{n_file}.root, ')
87  first = True
88  for tag in in_tags:
89  name = f'{tag}_{n_file}.list'
90  if first:
91  queue.write(f'{name}')
92  first = False
93  else:
94  queue.write(f', {name}')
95  o_files.append(open(f'{odir}/{name}','w'))
96  queue.write('\n')
97  for n in range (int(lines_per_output)):
98  for i_file, o_file in zip(in_files, o_files):
99  line = i_file.readline()
100  if not line:
101  for file in o_files:
102  o_file.close()
103  raise StopIteration
104  o_file.write(line)
105  for file in o_files:
106  o_file.close()
107  except StopIteration:
108  pass
109  if n_files == -1:
110  with open(f'{odir}/last_list_is_{n_file}','w') as fout:
111  fout.write(f'There are {n_file+2} lists')
112 
113  queue.close()
114 
115 
116 
117 
118 if __name__ == '__main__':
119  main()