1 import os,random,shutil,tarfile,sys
2 import subprocess,string
3 from optparse
import OptionParser
4 import random,time,platform
17 tar = tarfile.open(output_filename,
"w:gz")
18 for i
in os.listdir(source_dir):
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" 29 cache_folder = CACHE_PNFS_AREA +
str(random.randint(10000,99999)) +
"/" 30 os.makedirs(cache_folder)
33 ppfx_dir = os.getcwd()
34 ppfx_executable = os.path.join(ppfx_dir,
"bin/doReweight_dk2nu_numix")
36 if not os.path.exists(ppfx_executable):
37 print "ERROR: I cannot find a ppfx executable at "+ppfx_executable
42 ppfx_setup =
"setup_for_dune_cvmfs.sh "+MODE
43 ppfx_xml = ppfx_xml =ppfx_dir+
"/scripts/inputs_default.xml" 45 if not os.path.exists(ppfx_xml):
46 print "ERROR: I cannot find the ppfx option file at "+ppfx_xml
48 if not os.path.exists(ppfx_setup):
49 print "ERROR: I cannot find ppfx setup script at "+ppfx_setup
53 usage =
"usage:%prog [options]" 54 user = os.getenv(
"USER")
55 parser = OptionParser(usage=usage)
57 parser.add_option(
"-o",
"--output_dir",dest=
"output_dir",
58 help=
"Output Directory ",default=
"/pnfs/dune/scratch/users/"+user+
"/ppfxfiles/reweights")
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")
63 parser.add_option(
"-n",
"--detector_index",dest=
"detector_index",
64 help=
"Detector Index for Detector Location ",default =
"1")
66 (options, args) = parser.parse_args()
67 print "Output files will be written in ",options.output_dir
69 if not os.path.exists(options.output_dir):
70 os.makedirs(options.output_dir)
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()
80 xrootd_input =
"root://fndca1.fnal.gov:1094/" 81 xrootd_text_file =
"xroot_"+text_file
82 xrootd_file =
open(xrootd_text_file,
'w')
84 with
open(text_file,
'r') as 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")
90 xrootd_file.write(_line)
95 _textfile =
open(xrootd_text_file,
'r') 96 for line
in _textfile:
102 for i
in range(0,len(_list)):
103 if ".root" not in _list[i]:
105 if "dk2nu.root" in _list[i]:
106 _temp = _list[i].replace(
".dk2nu.root",
"")
108 _temp = _list[i].replace(
".root",
"")
110 temp = _temp.split(
"_")
113 for i
in range(0,len(_pre)):
115 index.append(_pre[i])
120 __prefix = os.path.basename(_list[0])
121 _prefix = __prefix.replace(
"\n",
"")
125 _temp_prefix = os.path.basename(_list[0])
126 temp_prefix = _temp_prefix.replace(index[0],
"")
127 _prefix = temp_prefix.replace(
"\n",
"")
129 if ".dk2nu.root" in _prefix:
130 prefix = _prefix.replace(
".dk2nu.root",
"")
132 prefix = _prefix.replace(
".root",
"")
135 print "ROOT FILE DOESNT END WITH .dk2nu.root OR .root" 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")
149 if os.path.exists(wrapfile):
151 if not os.path.exists(wrapdir):
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...." 159 shutil.move(tarfilename, cache_folder)
160 executable_file = cache_folder+tarfilename
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
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")
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')
178 output_filename = prefix+
"_${run_number}.root" 180 command =
"./bin/doReweight_dk2nu_numix "+temp_text+
" "+temp_dir+output_filename+
" scripts/inputs_default.xml "+
str(options.detector_index)+
"\n\n" 183 wrapper.write(command)
185 logfilename = output_filename.replace(
".root",
"_\$PROCESS.log")
186 logfile = os.path.join(
str(options.output_dir),logfilename)
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")
198 wrapper.write(
"echo 'DONE!!!'\n")
200 os.chmod(wrapfile,0777)
202 osver=
"SL%i"%
int(
float(platform.linux_distribution()[1]))
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;
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)
def make_tarfile(output_filename, source_dir)
AUTHOR AMIT BASHYAL (amitbashyal@gmail.com) ################## USAGE THIS SCRIPT IS TO GENERATE THE P...