submit_ppfxjobs.py
Go to the documentation of this file.
1 import os,random,shutil,tarfile,sys
2 import subprocess,string
3 from optparse import OptionParser
4 import random,time,platform
5 
6 
7 ##################AUTHOR AMIT BASHYAL (amitbashyal@gmail.com) ##################
8 #USAGE
9 #THIS SCRIPT IS TO GENERATE THE PPFX FILES FROM ALREADY EXISTING DK2NU FILES
10 # ls path_to_dk2nufiles/*.root >file_list.txt
11 # python submit_ppfxjobs.py -t file_list.txt -o /path/to/outputfile/ -n detector_index
12 #END USAGE#####################################################
13 
14 
15 
16 def make_tarfile(output_filename, source_dir):
17  tar = tarfile.open(output_filename, "w:gz")
18  for i in os.listdir(source_dir):
19  tar.add(i)
20  tar.close()
21 
22 
23 
24 CACHE_PNFS_AREA = "/pnfs/dune/scratch/users/{USER}/grid_cache/".format(EXPERIMENT = os.getenv("EXPERIMENT"),
25  USER = os.getenv("USER"))
26 PWD = os.getenv("PWD")
27 tarfilename = "local_install.tar.gz"
28 #start by creating a scratch area to send file
29 cache_folder = CACHE_PNFS_AREA + str(random.randint(10000,99999)) + "/"
30 os.makedirs(cache_folder)
31 
32 #write a wrapper file on the fly for the job submission.....
33 ppfx_dir = os.getcwd()
34 ppfx_executable = os.path.join(ppfx_dir,"bin/doReweight_dk2nu_numix")
35 
36 if not os.path.exists(ppfx_executable):
37  print "ERROR: I cannot find a ppfx executable at "+ppfx_executable
38  sys.exit()
39 
40 
41 MODE="OPT" #since no one uses "REF"
42 ppfx_setup = "setup_for_dune_cvmfs.sh "+MODE
43 ppfx_xml = ppfx_xml =ppfx_dir+"/scripts/inputs_default.xml"
44 
45 if not os.path.exists(ppfx_xml):
46  print "ERROR: I cannot find the ppfx option file at "+ppfx_xml
47  sys.exit()
48  if not os.path.exists(ppfx_setup):
49  print "ERROR: I cannot find ppfx setup script at "+ppfx_setup
50  sys.exit()
51 
52 
53 usage = "usage:%prog [options]"
54 user = os.getenv("USER")
55 parser = OptionParser(usage=usage)
56 
57 parser.add_option("-o","--output_dir",dest="output_dir",
58  help="Output Directory ",default="/pnfs/dune/scratch/users/"+user+"/ppfxfiles/reweights")
59 
60 parser.add_option("-t","--file_list",dest="file_list",
61  help = "Text file with a list of dk2nu input root files Should be in Same directory as teh job submission script (Only the name of the files) ",default="BLA")
62 
63 parser.add_option("-n","--detector_index",dest="detector_index",
64  help="Detector Index for Detector Location ",default = "1")
65 
66 (options, args) = parser.parse_args()
67 print "Output files will be written in ",options.output_dir
68 
69 if not os.path.exists(options.output_dir):
70  os.makedirs(options.output_dir)
71 
72 
73 #first thing look at the list of inputfiles
74 #read the text file line by line
75 text_file =str(options.file_list)
76 if text_file not in os.listdir(os.getcwd()):
77  print text_file, "not in ",os.getcwd()
78  sys.exit()
79 
80 xrootd_input = "root://fndca1.fnal.gov:1094/"
81 xrootd_text_file = "xroot_"+text_file
82 xrootd_file = open(xrootd_text_file,'w')
83 
84 with open(text_file,'r') as f:
85  for line in f:
86  if "fndca1.fnal.gov" not in line:
87  __line = xrootd_input+line.replace("/pnfs/","/pnfs/fnal.gov/usr/")
88  _line = __line.replace("//pnfs","/pnfs")
89  #print _line
90  xrootd_file.write(_line)
91 
92 xrootd_file.close()
93 #sys.exit()
94 _list = []
95 _textfile = open(xrootd_text_file,'r')
96 for line in _textfile:
97  _list.append(line)
98 
99 #_list = os.listdir(options.input_dir)
100 _pre = []
101 index=[]
102 for i in range(0,len(_list)):
103  if ".root" not in _list[i]:
104  continue
105  if "dk2nu.root" in _list[i]:
106  _temp = _list[i].replace(".dk2nu.root","")
107  else:
108  _temp = _list[i].replace(".root","")
109 
110  temp = _temp.split("_")
111  if len(_pre)!=0:
112  #print temp,_pre
113  for i in range(0,len(_pre)):
114  if temp[i]!=_pre[i]:
115  index.append(_pre[i])
116  _pre = temp
117  #print _pre
118 
119 #sys.exit()
120 __prefix = os.path.basename(_list[0])
121 _prefix = __prefix.replace("\n","")
122 #print "PREFIX ",_prefix
123 #sys.exit()
124 if len(index)!=0:
125  _temp_prefix = os.path.basename(_list[0])
126  temp_prefix = _temp_prefix.replace(index[0],"")
127  _prefix = temp_prefix.replace("\n","")
128 prefix=""
129 if ".dk2nu.root" in _prefix:
130  prefix = _prefix.replace(".dk2nu.root","")
131 else:
132  prefix = _prefix.replace(".root","")
133 
134 if prefix=="":
135  print "ROOT FILE DOESNT END WITH .dk2nu.root OR .root"
136  sys.exit()
137 
138 #print prefix,_list[0]
139 #sys.exit()
140 ntot = len(_list)
141 #print prefix,_list,len(_list)
142 #sys.exit()
143 #now stuff to make the wrapper
144 wrapname = "wrapper_doReweight_dk2nu_"+str(int(time.time()))+".sh"
145 wrapdir = CACHE_PNFS_AREA+"wrapdir/"
146 wrapfile = wrapdir+wrapname
147 temp_text = wrapname.replace(".sh",".txt")
148 #sys.exit()
149 if os.path.exists(wrapfile):
150  os.remove(wrapfile)
151 if not os.path.exists(wrapdir):
152  os.makedirs(wrapdir)
153 
154 os.chmod(wrapdir,0777)
155 os.chmod(options.output_dir,0777)
156 os.chmod(cache_folder,0777)
157 print "\Tarring up the local area...."
158 make_tarfile(tarfilename,".")
159 shutil.move(tarfilename, cache_folder) # temp file -> remove it from pwd
160 executable_file = cache_folder+tarfilename
161 
162 temp_dir = "$_CONDOR_SCRATCH_DIR/"
163 local_tar_path = "$_CONDOR_SCRATCH_DIR/"+os.path.basename(executable_file)
164 print "name of executable file "+wrapfile
165 wrapper = open(wrapfile,'w')
166 wrapper.write("source /cvmfs/fermilab.opensciencegrid.org/products/common/etc/setups \n\n")
167 wrapper.write("setup ifdhc \n\n")
168 wrapper.write("ifdh cp "+executable_file+" "+local_tar_path+"\n\n")
169 wrapper.write("tar -xzvf "+local_tar_path+" \n\n")
170 wrapper.write("source "+ppfx_setup+"\n\n")
171 #okay now we create
172 #wrapper.write("touch "+temp_text+"\n\n")
173 wrapper.write("export run_number=$((${PROCESS}+1)) \n\n")
174 wrapper.write('sed "${run_number}q;d" '+xrootd_text_file+' >'+temp_text+'\n\n')
175 wrapper.write('file="$(cat '+temp_text+')"'+'\n\n')
176 #wrapper.write("ifdh cp ${file} "+temp_dir+"\n\n")
177 #wrapper.write("ls "+temp_dir+"*.root > file_list.txt \n\n")
178 output_filename = prefix+"_${run_number}.root"
179 #print output_filename
180 command = "./bin/doReweight_dk2nu_numix "+temp_text+" "+temp_dir+output_filename+" scripts/inputs_default.xml "+str(options.detector_index)+"\n\n"
181 print command
182 #sys.exit()
183 wrapper.write(command)
184 
185 logfilename = output_filename.replace(".root","_\$PROCESS.log")
186 logfile = os.path.join(str(options.output_dir),logfilename)
187 
188 #now copy the output file
189 wrapper.write("if [ -f "+temp_dir+output_filename+" ];\n")
190 wrapper.write("then\n")
191 wrapper.write(" ifdh cp -D "+ temp_dir+output_filename+" "+options.output_dir+"\n")
192 wrapper.write("fi \n")
193 #now copy the log file
194 #wrapper.write("if [ -f "+temp_dir+logfile+" ];\n")
195 #wrapper.write("then\n")
196 #wrapper.write(" ifdh cp -D "+temp_dir+logfile+" "+options.output_dir+"\n")
197 #wrapper.write("fi \n")
198 wrapper.write("echo 'DONE!!!'\n")
199 wrapper.close()
200 os.chmod(wrapfile,0777)
201 
202 osver="SL%i"%int(float(platform.linux_distribution()[1]))
203 
204 jobsub_command = "jobsub_submit.py -N "+str(ntot)+" --resource-provides=usage_model=OPPORTUNISTIC --expected-lifetime 4h --OS="+osver+" --group=dune -L "+logfile+" file://"+wrapfile;
205 print jobsub_command
206 os.system(jobsub_command)
207 print "Submitted ",ntot," jobs in the grid "
208 print "*******************************************************************************************"
209 print "END OF JOBSUB"
int open(const char *, int)
Opens a file descriptor.
static bool format(QChar::Decomposition tag, QString &str, int index, int len)
Definition: qstring.cpp:11496
def make_tarfile(output_filename, source_dir)
AUTHOR AMIT BASHYAL (amitbashyal@gmail.com) ################## USAGE THIS SCRIPT IS TO GENERATE THE P...
static QCString str