18 A very simple script to handle payload for beam spot results 20 usage: %prog -d <data file/directory> -t <tag name> 21 -c, --copy : Only copy files from input directory to test/workflow/files/ 22 -d, --data = DATA: Data file, or directory with data files. 23 -I, --IOVbase = IOVBASE: options: runbase(default), lumibase, timebase 24 -o, --overwrite : Overwrite results files when copying. 25 -O, --Output = OUTPUT: Output directory for data files (workflow directory) 26 -m, --merged : Use when data file contains combined results. 27 -n, --newarchive : Create a new archive directory when copying. 28 -t, --tag = TAG: Database tag name. 29 -T, --Test : Upload files to Test dropbox for data validation. 30 -u, --upload : Upload files to offline drop box via scp. 31 -z, --zlarge : Enlarge sigmaZ to 10 +/- 0.005 cm. 33 Francisco Yumiceva (yumiceva@fnal.gov) 37 from __future__
import print_function
41 import commands, re, time
43 from CommonMethods
import *
45 workflowdir =
'test/workflow/' 46 workflowdirLastPayloads = workflowdir +
'lastPayloads/' 47 workflowdirTmp = workflowdir +
'tmp/' 48 workflowdirArchive = workflowdir +
'archive/' 53 global workflowdirArchive
58 if path.find(
'castor') != -1:
59 print(
"Getting files from castor ...")
62 elif not os.path.exists(path):
63 exit(
"ERROR: File or directory " + path +
" doesn't exist")
65 if path[len(path)-4:len(path)] !=
'.txt':
66 if path[len(path)-1] !=
'/':
69 aCommand = lsCommand +
'ls '+ path +
" | grep .txt" 71 tmpstatus = commands.getstatusoutput( aCommand )
72 tmplistoffiles = tmpstatus[1].
split(
'\n')
73 if len(tmplistoffiles) == 1:
74 if tmplistoffiles[0] ==
'':
75 exit(
'ERROR: No files found in directory ' + path)
76 if tmplistoffiles[0].
find(
'No such file or directory') != -1:
77 exit(
"ERROR: File or directory " + path +
" doesn't exist")
80 tmplistoffiles.append(path[path.rfind(
'/')+1:len(path)])
81 path = path[0:path.rfind(
'/')+1]
86 archiveName = os.getcwd() +
'/' 87 archiveName = archiveName[archiveName[:len(archiveName)-1].rfind(
'/')+1:len(archiveName)]
88 if path[:len(path)-1].rfind(
'/') != -1:
89 archiveName = path[path[:len(path)-1].rfind(
'/')+1:len(path)]
91 workflowdirArchive = workflowdirArchive + archiveName
93 workflowdirArchive = workflowdirArchive[:len(workflowdirArchive)-1] +
'_' + tagType +
'/' 94 if not os.path.isdir(workflowdirArchive):
95 os.mkdir(workflowdirArchive)
96 elif(option.newarchive):
102 for n
in range(1,100000):
103 tryDir = workflowdirArchive[:len(workflowdirArchive)-1] +
'_' +
str(n) +
'/' 104 if not os.path.isdir(tryDir):
105 workflowdirArchive = tryDir
106 os.mkdir(workflowdirArchive)
109 exit(
'ERROR: Unbelievable! do you ever clean ' + workflowdir +
'?. I think you have to remove some directories!')
111 for ifile
in tmplistoffiles:
112 if ifile.find(
'.txt') != -1:
113 if os.path.isfile(workflowdirArchive+
"/"+ifile):
115 print(
"File " + ifile +
" already exists in destination. We will overwrite it.")
117 print(
"File " + ifile +
" already exists in destination. Keep original file.")
118 listoffiles.append( workflowdirArchive + ifile )
120 listoffiles.append( workflowdirArchive + ifile )
122 aCommand = cpCommand +
'cp '+ path + ifile +
" " + workflowdirArchive
123 print(
" >> " + aCommand)
124 tmpstatus = commands.getstatusoutput( aCommand )
129 global workflowdirLastPayloads
130 global workflowdirTmp
131 global workflowdirArchive
132 if not os.path.isdir(workflowdir):
133 print(
"Making " + workflowdir +
" directory...")
134 os.mkdir(workflowdir)
136 if not os.path.isdir(workflowdirLastPayloads):
137 os.mkdir(workflowdirLastPayloads)
139 os.system(
"rm -f "+ workflowdirLastPayloads +
"*")
141 if not os.path.isdir(workflowdirTmp):
142 os.mkdir(workflowdirTmp)
144 os.system(
"rm -f "+ workflowdirTmp +
"*")
146 if not os.path.isdir(workflowdirArchive):
147 os.mkdir(workflowdirArchive)
150 if __name__ ==
'__main__':
160 if not args
and not option:
exit()
162 workflowdir = os.getenv(
"CMSSW_BASE") +
"/src/RecoVertex/BeamSpotProducer/test/workflow/" 164 workflowdir = option.Output
165 if workflowdir[len(workflowdir)-1] !=
'/':
166 workflowdir = workflowdir +
'/' 167 workflowdirLastPayloads = workflowdir +
"lastPayloads/" 168 workflowdirTmp = workflowdir +
"tmp/" 169 workflowdirArchive = workflowdir +
"archive/" 171 if ( (option.data
and option.tag)
or (option.data
and option.copy)):
175 print(
"ERROR: You must provide the data file or the a directory with data files")
180 exit(
"Files copied in " + workflowdirArchive)
185 if tagname.find(
"offline") != -1:
187 elif tagname.find(
"prompt") != -1:
189 elif tagname.find(
"express") != -1 :
191 elif tagname.find(
"hlt") != -1:
194 print(
"I am assuming your tag is for the offline database...")
198 print(
"ERROR: You must provide the database tag name")
202 timetype =
'runnumber' 204 if option.IOVbase !=
"runbase" and option.IOVbase !=
"lumibase" and option.IOVbase !=
"timebase":
205 print(
"\n\n unknown iov base option: "+ option.IOVbase +
" \n\n\n")
207 IOVbase = option.IOVbase
213 for beam_file
in listoffiles:
215 if len(listoffiles)==1
and option.merged:
216 mergedfile = open(beam_file)
217 alllines = mergedfile.readlines()
218 npayloads = len(alllines)/23
219 for i
in range(0,npayloads):
220 block = alllines[i * 23: (i+1)*23]
224 atime = line.split()[1]
225 sortedlist[atime] = block
228 tmpfile = open(beam_file)
234 if line.find(
'Runnumber') != -1:
235 arun = line.split()[1]
236 if line.find(
"EndTimeOfFit") != -1:
237 atime = time.strptime(line.split()[1] +
" " + line.split()[2] +
" " + line.split()[3],
"%Y.%m.%d %H:%M:%S %Z")
238 if line.find(
"LumiRange") != -1:
239 alumi = line.split()[3]
240 if line.find(
'Type') != -1
and line.split()[1] ==
'0':
243 print(
" zero fit result, skip file " + beam_file +
" with time stamp:")
244 print(
" run " + arun +
" lumis " + alumis)
250 keys = sorted(sortedlist.keys())
253 if not os.path.isdir(workflowdirArchive +
"AllIOVs"):
254 os.mkdir(workflowdirArchive +
"AllIOVs")
255 allbeam_file = workflowdirArchive +
"AllIOVs/" + tagname +
"_all_IOVs.txt" 258 allfile = open( allbeam_file,
'a')
259 print(
" merging all results into file: " + allbeam_file)
262 if os.path.exists(workflowdirArchive+
"payloads/Combined.db"):
263 os.system(
"rm "+workflowdirArchive+
"payloads/Combined.db")
267 iov_since_first =
'1' 268 total_files = len(keys)
270 destDB =
'oracle://cms_orcon_prod/CMS_COND_31X_BEAMSPOT' 272 destDB =
'oracle://cms_orcoff_prep/CMS_COND_BEAMSPOT' 274 iov_comment =
'Beam spot position' 281 writedb_template = os.getenv(
"CMSSW_BASE") +
"/src/RecoVertex/BeamSpotProducer/test/write2DB_template.py" 282 readdb_template = os.getenv(
"CMSSW_BASE") +
"/src/RecoVertex/BeamSpotProducer/test/readDB_template.py" 283 sqlite_file_name = tagname + suffix
284 sqlite_file = workflowdirTmp + sqlite_file_name +
'.db' 285 metadata_file = workflowdirTmp + sqlite_file_name +
'.txt' 290 beam_file = sortedlist[key]
291 tmp_datafilename = workflowdirTmp+
"tmp_datafile.txt" 293 tmpfile =
file(tmp_datafilename,
'w')
294 tmpfile.writelines(sortedlist[key])
296 beam_file = tmp_datafilename
298 print(
"read input beamspot file: " + beam_file)
299 tmpfile = open(beam_file)
300 beam_file_tmp = workflowdirTmp + beam_file[beam_file.rfind(
'/')+1:] +
".tmp" 301 newtmpfile = open(beam_file_tmp,
"w")
306 if line.find(
"Runnumber") != -1:
307 iov_since = line.split()[1]
309 tmp_run = line.split()[1]
310 elif line.find(
"LumiRange") != -1:
311 tmp_lumi_since = line.split()[1]
312 tmp_lumi_till = line.split()[3]
313 elif line.find(
"BeginTimeOfFit") == -1
and line.find(
"EndTimeOfFit") == -1
and line.find(
"LumiRange") == -1:
314 if line.find(
"sigmaZ0") != -1
and option.zlarge:
315 line =
"sigmaZ0 10\n" 316 if line.find(
"Cov(3,j)") != -1
and option.zlarge:
317 line =
"Cov(3,j) 0 0 0 2.5e-05 0 0 0\n" 318 newtmpfile.write(line)
322 if IOVbase ==
"lumibase":
328 iov_since_first = iov_since
335 beam_file = beam_file_tmp
337 if not writeSqliteFile(sqlite_file,tagname,timetype,beam_file,writedb_template,workflowdirTmp):
338 print(
"An error occurred while writing the sqlite file: " + sqlite_file)
340 commands.getstatusoutput(
'rm -f ' + beam_file)
342 readSqliteFile(sqlite_file,tagname,readdb_template,workflowdirTmp)
345 if not os.path.isdir(workflowdirArchive +
'payloads'):
346 os.mkdir(workflowdirArchive +
'payloads')
348 print(
" merge sqlite file ...")
349 appendSqliteFile(
"Combined.db", sqlite_file, tagname, iov_since, iov_till ,workflowdirTmp)
352 if nfile == total_files:
353 print(
" this is the last IOV. You can use this payload for express and prompt conditions.")
354 os.system(
"cp "+sqlite_file+
" "+workflowdirArchive+
"payloads/express.db")
355 print(
"a copy of this payload has been placed at:")
356 print(workflowdirArchive+
"payloads/express.db")
359 os.system(
"rm "+ sqlite_file)
360 print(
" clean up done.")
362 os.system(
"mv " + workflowdirTmp +
"Combined.db " + workflowdirArchive +
"payloads/")
367 print(
" create MERGED payload card for dropbox ...")
369 sqlite_file = workflowdirArchive+
'payloads/Combined.db' 370 metadata_file = workflowdirArchive+
'payloads/Combined.txt' 371 dfile = open(metadata_file,
'w')
373 dfile.write(
'destDB '+ destDB +
'\n')
374 dfile.write(
'tag '+ tagname +
'\n')
375 dfile.write(
'inputtag' +
'\n')
376 dfile.write(
'since ' + iov_since_first +
'\n')
378 if IOVbase ==
"runbase":
379 dfile.write(
'Timetype runnumber\n')
380 elif IOVbase ==
"lumibase":
381 dfile.write(
'Timetype lumiid\n')
383 if tagType ==
"express":
385 dfile.write(
'IOVCheck ' + checkType +
'\n')
386 dfile.write(
'usertext ' + iov_comment +
'\n')
390 uuid = commands.getstatusoutput(
'uuidgen -t')[1]
391 final_sqlite_file_name = tagname +
'@' + uuid
393 if not os.path.isdir(workflowdirArchive +
'payloads'):
394 os.mkdir(workflowdirArchive +
'payloads')
395 commands.getstatusoutput(
'cp ' + sqlite_file +
' ' + workflowdirArchive +
'payloads/' + final_sqlite_file_name +
'.db')
396 commands.getstatusoutput(
'cp ' + metadata_file +
' ' + workflowdirArchive +
'payloads/' + final_sqlite_file_name +
'.txt')
398 commands.getstatusoutput(
'mv ' + sqlite_file +
' ' + workflowdirLastPayloads + final_sqlite_file_name +
'.db')
399 commands.getstatusoutput(
'mv ' + metadata_file +
' ' + workflowdirLastPayloads + final_sqlite_file_name +
'.txt')
401 print(workflowdirLastPayloads + final_sqlite_file_name +
'.db')
402 print(workflowdirLastPayloads + final_sqlite_file_name +
'.txt')
405 print(
" scp files to offline Drop Box")
408 dropbox =
"/DropBox_test"
def copyToWorkflowdir(path)
def appendSqliteFile(combinedSqliteFileName, sqliteFileName, tagName, IOVSince, IOVTill, tmpDir="/tmp/")
S & print(S &os, JobReport::InputFile const &f)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
def uploadSqliteFile(sqliteFileDirName, sqliteFileName, dropbox="/DropBox")
def readSqliteFile(sqliteFileName, tagName, sqliteTemplateFile, tmpDir="/tmp/")
def writeSqliteFile(sqliteFileName, tagName, timeType, beamSpotFile, sqliteTemplateFile, tmpDir="/tmp/")