DESY Hbb Analysis Framework
naf_mult_submit.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 import os
3 import glob
4 import re
5 import sys
6 from argparse import ArgumentParser
7 from shutil import copyfile, rmtree, move
8 from time import sleep
9 
10 # --- functions ---
11 def getConfigParameter( config, parameter ):
12  p = None
13  exist = False
14  with open(config) as f:
15  for line in f:
16  line = line.replace(" ","").strip()
17  if len(line) == 0:
18  continue
19  if line[0] != '#' and line.split("=")[0] == parameter:
20  par = line.split("=")[1]
21  exist = True
22  p = [parameter,par]
23  break
24 
25  return p
26 
27 def createConfigParameter( config, parameter ):
28  exist = False
29  with open(config,"r") as f:
30  lines = f.readlines()
31 
32  for line in lines:
33  line = line.replace(" ","").strip()
34  if len(line) == 0:
35  continue
36  if line[0] != '#' and line.split("=")[0] == parameter:
37  exist = True
38  break
39 
40  if not exist:
41  with open(config, "w") as f:
42  f.write(parameter+" = \n")
43  for line in lines:
44  f.write(line)
45 
46 
47 def replaceConfigParameter( config, parameter, newpar ):
48  par = None
49  with open(config, "r") as f:
50  lines = f.readlines()
51  with open(config, "w") as f:
52  for line in lines:
53  l = line.replace(" ","").strip()
54  if len(l) < 1:
55  f.write(line)
56  continue
57  if l[0] != '#' and l.split('=')[0] == parameter:
58  if l.split('=')[1] == "" :
59  par = "="
60  newpar = " = " + newpar
61  else:
62  par = l.split('=')[1]
63  f.write(re.sub(par, newpar, line))
64  else:
65  f.write(line)
66 
67 def removeConfigParameter( config, parameter ):
68  with open(config, "r") as f:
69  lines = f.readlines()
70  with open(config, "w") as f:
71  for line in lines:
72  l = line.replace(" ","").strip()
73  if len(l) < 1:
74  f.write(line)
75  continue
76  if l[0] != '#' and l.split('=')[0] == parameter:
77  continue;
78  f.write(line)
79 
80 
81 def basenameConfigParameter( config, name ):
82  with open(config, "r") as f:
83  lines = f.readlines()
84  with open(config, "w") as f:
85  for line in lines:
86  f.write(re.sub(name, os.path.basename(name), line))
87 
88 
89 
90 # --- main code ---
91 
92 # parsing arguments
93 parser = ArgumentParser()
94 parser.add_argument("-e", "--exe", dest="exe", help="Executable")
95 parser.add_argument("-n", "--ntuples", dest="ntuples", help="List of ntuples file")
96 parser.add_argument("-x", "--nfiles", dest="nfiles", type=int, default=1, help="Number of ntuple files per job")
97 parser.add_argument("-c", "--config", dest="config", help="Configuration file")
98 parser.add_argument("-j", "--json", dest="json", help="JSON file with certified data")
99 args = parser.parse_args()
100 if not args.exe:
101  print "nothing to be done"
102  quit()
103 
104 ntuples = args.ntuples
105 json = args.json
106 config = args.config
107 
108 configNtuples = None
109 # get parameter from configuration
110 if config:
111  if not os.path.isfile(config):
112  print "Configuration file does not exist"
113  quit()
114  configNtuples = getConfigParameter( config, "ntuplesList" )
115  if not ntuples:
116  if configNtuples:
117  ntuples = configNtuples[1]
118  if not ntuples:
119  print "*error* You must define the parameter ntuplesList in your configuration."
120  quit()
121  configJson = getConfigParameter( config, "json" )
122  if not json:
123  if configJson:
124  json = configJson[1]
125 
126 # checking if require files exist
127 if ntuples:
128  if not os.path.isfile(ntuples):
129  print "Ntuples list file does not exist"
130  quit()
131 if json:
132  if not os.path.isfile(json):
133  print "Json file does not exist"
134  quit()
135 
136 # directory where the jobs will be stored
137 maindir = "Condor_"+os.path.basename(args.exe)
138 if config:
139  maindir = maindir+"_"+ os.path.splitext(os.path.basename(config))[0]
140 cwd = os.getcwd()
141 if os.path.exists(cwd+"/"+maindir):
142  print maindir + "already exists. Rename or remove it and then resubmit"
143  quit()
144 
145 splitcwd = cwd.split('/')
146 newcwd = ''
147 if splitcwd[1] == 'afs':
148  newcwd = '/nfs/dust/cms/user'
149  for d in splitcwd[5:]:
150  newcwd += '/'+d
151  print('nfs directory:',newcwd)
152 
153 if newcwd != '':
154  maindir = newcwd+'/'+maindir
155  os.makedirs(maindir)
156  lncmd = 'ln -s ' + maindir
157  os.system(lncmd)
158  if not os.path.exists('histograms'):
159  ln2cmd = 'ln -s ' + newcwd + ' histograms'
160  os.system(ln2cmd)
161 else:
162  os.mkdir(maindir)
163 
164 # splitting the file list
165 if ntuples:
166  pid = os.getpid()
167  tmpdir = ".tmp_" + str(pid)
168  os.mkdir(tmpdir)
169  copyfile(ntuples, tmpdir+"/"+os.path.basename(ntuples))
170  if config:
171  copyfile(config, tmpdir+"/"+os.path.basename(config))
172  os.chdir(tmpdir)
173  if config:
174  # replace json and ntuples in the local exe config by their basenames
175  if json:
176  createConfigParameter(os.path.basename(config),'json')
177  replaceConfigParameter(os.path.basename(config), 'json', os.path.basename(json))
178  else:
179  removeConfigParameter(os.path.basename(config),'json')
180  # ntuples list
181  createConfigParameter(os.path.basename(config),'ntuplesList')
182  replaceConfigParameter(os.path.basename(config), 'ntuplesList', os.path.basename(ntuples))
183 
184  splitcmd = "split.csh" + " " + str(args.nfiles) + " " + os.path.basename(ntuples)
185  os.system(splitcmd)
186  os.remove(os.path.basename(ntuples)) # not needed anymore after the splitting
187  files = glob.glob('.*_x????.txt')
188  files.sort()
189  os.chdir(cwd)
190 
191  # loop over the splitted files, each will correspond to a job on the NAF
192  for f in files:
193  jobnum = os.path.splitext(f)[0][-4:]
194  jobid = "job_"+jobnum
195  exedir = maindir+"/"+jobid
196  os.mkdir(exedir)
197  # moving stuff to the proper directories
198  move(tmpdir+"/"+f,exedir+"/"+os.path.basename(ntuples))
199  if json:
200  copyfile(json, exedir+"/"+os.path.basename(json))
201  if config:
202  copyfile(tmpdir+"/"+os.path.basename(config),exedir+"/"+os.path.basename(config))
203 # condorcmd = "condor_mult_submit.csh" + " " + jobid + " " + args.exe + " " + os.path.basename(config)
204  condorcmd = "condor_job_prep.csh job " + args.exe + " " + os.path.basename(config)
205  else:
206 # condorcmd = "condor_mult_submit.csh" + " " + jobid + " " + args.exe
207  condorcmd = "condor_job_prep.csh job "+ args.exe
208  # make the submissions
209  os.chdir(exedir)
210  jobf = open('./seed.txt', 'w+')
211  print >> jobf, int(jobnum)+1
212  jobf.close()
213  print "Submitting ",jobid,"..."
214  os.system(condorcmd)
215  sleep(0.2)
216  # back to original directory
217  os.chdir(cwd)
218 
219 else:
220  exedir = maindir+"/job_0000"
221  os.mkdir(exedir)
222  if os.path.isfile(args.exe):
223  copyfile(args.exe, exedir+"/"+os.path.basename(args.exe))
224  os.chdir(exedir)
225  jobf = open('./seed.txt', 'w+')
226  print >> jobf, 1
227  jobf.close()
228  condorcmd = "condor_submit.csh job_0000" + " " + os.path.basename(args.exe)
229  os.system(condorcmd)
230  os.chdir(cwd)
231 
232 
233 ## NOW DO SUBMISSION
234 os.chdir(maindir)
235 if config:
236  condorcmd = "condor_mult_submit.csh job " + args.exe + " " + os.path.basename(config)
237 else:
238  condorcmd = "condor_mult_submit.csh job "+ args.exe
239 os.system(condorcmd)
240 os.chdir(cwd)
241 
242 # remove the temporary directory
243 os.chdir(cwd)
244 if ntuples:
245  os.remove(tmpdir+"/"+os.path.basename(config))
246  rmtree(tmpdir)
247 
248 
def basenameConfigParameter(config, name)
def replaceConfigParameter(config, parameter, newpar)
def getConfigParameter(config, parameter)
def createConfigParameter(config, parameter)
def removeConfigParameter(config, parameter)