CMS 3D CMS Logo

createPayload.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 #____________________________________________________________
3 #
4 # createPayload
5 #
6 # A very simple way to create condition DB payloads
7 #
8 # Francisco Yumiceva
9 # yumiceva@fnal.gov
10 #
11 # Fermilab, 2009
12 #
13 #____________________________________________________________
14 
15 """
16  createPayload.py
17 
18  A very simple script to handle payload for beam spot results
19 
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.
32 
33  Francisco Yumiceva (yumiceva@fnal.gov)
34  Fermilab 2010
35 
36 """
37 from __future__ import print_function
38 
39 
40 from builtins import range
41 import sys,os
42 import subprocess, re, time
43 import datetime
44 from CommonMethods import *
45 
46 workflowdir = 'test/workflow/'
47 workflowdirLastPayloads = workflowdir + 'lastPayloads/'
48 workflowdirTmp = workflowdir + 'tmp/'
49 workflowdirArchive = workflowdir + 'archive/'
50 optionstring = ''
51 tagType = ''
52 
54  global workflowdirArchive
55  lsCommand = ''
56  cpCommand = ''
57  listoffiles = []
58  tmplistoffiles = []
59  if path.find('castor') != -1:
60  print("Getting files from castor ...")
61  lsCommand = 'ns'
62  cpCommand = 'rf'
63  elif not os.path.exists(path):
64  exit("ERROR: File or directory " + path + " doesn't exist")
65 
66  if path[len(path)-4:len(path)] != '.txt':
67  if path[len(path)-1] != '/':
68  path = path + '/'
69 
70  aCommand = lsCommand + 'ls '+ path + " | grep .txt"
71 
72  tmpstatus = subprocess.getstatusoutput( aCommand )
73  tmplistoffiles = tmpstatus[1].split('\n')
74  if len(tmplistoffiles) == 1:
75  if tmplistoffiles[0] == '':
76  exit('ERROR: No files found in directory ' + path)
77  if tmplistoffiles[0].find('No such file or directory') != -1:
78  exit("ERROR: File or directory " + path + " doesn't exist")
79 
80  else:
81  tmplistoffiles.append(path[path.rfind('/')+1:len(path)])
82  path = path[0:path.rfind('/')+1]
83 
84 
85  archiveName = path
86  if path == './':
87  archiveName = os.getcwd() + '/'
88  archiveName = archiveName[archiveName[:len(archiveName)-1].rfind('/')+1:len(archiveName)]
89  if path[:len(path)-1].rfind('/') != -1:
90  archiveName = path[path[:len(path)-1].rfind('/')+1:len(path)]
91 
92  workflowdirArchive = workflowdirArchive + archiveName
93  if tagType != '' :
94  workflowdirArchive = workflowdirArchive[:len(workflowdirArchive)-1] + '_' + tagType + '/'
95  if not os.path.isdir(workflowdirArchive):
96  os.mkdir(workflowdirArchive)
97  elif(option.newarchive):
98 # tmpTime = str(datetime.datetime.now())
99 # tmpTime = tmpTime.replace(' ','-')
100 # tmpTime = tmpTime.replace('.','-')
101 # workflowdirArchive = workflowdirArchive[:len(workflowdirArchive)-1] + '_' + tmpTime + '/'
102 # os.mkdir(workflowdirArchive)
103  for n in range(1,100000):
104  tryDir = workflowdirArchive[:len(workflowdirArchive)-1] + '_' + str(n) + '/'
105  if not os.path.isdir(tryDir):
106  workflowdirArchive = tryDir
107  os.mkdir(workflowdirArchive)
108  break
109  elif n == 100000-1:
110  exit('ERROR: Unbelievable! do you ever clean ' + workflowdir + '?. I think you have to remove some directories!')
111 
112  for ifile in tmplistoffiles:
113  if ifile.find('.txt') != -1:
114  if os.path.isfile(workflowdirArchive+"/"+ifile):
115  if option.overwrite:
116  print("File " + ifile + " already exists in destination. We will overwrite it.")
117  else:
118  print("File " + ifile + " already exists in destination. Keep original file.")
119  listoffiles.append( workflowdirArchive + ifile )
120  continue
121  listoffiles.append( workflowdirArchive + ifile )
122  # copy to local disk
123  aCommand = cpCommand + 'cp '+ path + ifile + " " + workflowdirArchive
124  print(" >> " + aCommand)
125  tmpstatus = subprocess.getstatusoutput( aCommand )
126  return listoffiles
127 
129  global workflowdir
130  global workflowdirLastPayloads
131  global workflowdirTmp
132  global workflowdirArchive
133  if not os.path.isdir(workflowdir):
134  print("Making " + workflowdir + " directory...")
135  os.mkdir(workflowdir)
136 
137  if not os.path.isdir(workflowdirLastPayloads):
138  os.mkdir(workflowdirLastPayloads)
139  else:
140  os.system("rm -f "+ workflowdirLastPayloads + "*")
141 
142  if not os.path.isdir(workflowdirTmp):
143  os.mkdir(workflowdirTmp)
144  else:
145  os.system("rm -f "+ workflowdirTmp + "*")
146 
147  if not os.path.isdir(workflowdirArchive):
148  os.mkdir(workflowdirArchive)
149 
150 
151 if __name__ == '__main__':
152  #if len(sys.argv) < 2:
153 # print "\n [usage] createPayload <beamspot file> <tag name> <IOV since> <IOV till=-1=inf> <IOV comment> <destDB=oracle://cms_orcon_prod/CMS_COND_31X_BEAMSPOT>"
154  #print " e.g. createPayload BeamFitResults_template.txt BeamSpotObjects_2009_v1_express 122745 \"\" \"beam spot for early collisions\" \"oracle://cms_orcon_prod/CMS_COND_31X_BEAMSPOT\"\n"
155  #sys.exit()
156 
157 
158  # COMMAND LINE OPTIONS
159 
160  option,args = parse(__doc__)
161  if not args and not option: exit()
162 
163  workflowdir = os.getenv("CMSSW_BASE") + "/src/RecoVertex/BeamSpotProducer/test/workflow/"
164  if option.Output:
165  workflowdir = option.Output
166  if workflowdir[len(workflowdir)-1] != '/':
167  workflowdir = workflowdir + '/'
168  workflowdirLastPayloads = workflowdir + "lastPayloads/"
169  workflowdirTmp = workflowdir + "tmp/"
170  workflowdirArchive = workflowdir + "archive/"
171 
172  if ( (option.data and option.tag) or (option.data and option.copy)):
173  mkWorkflowdir()
174 
175  if not option.data:
176  print("ERROR: You must provide the data file or the a directory with data files")
177  exit()
178 
179  if option.copy:
180  copyToWorkflowdir(option.data)
181  exit("Files copied in " + workflowdirArchive)
182 
183  tagname = ''
184  if option.tag:
185  tagname = option.tag
186  if tagname.find("offline") != -1:
187  tagType = "offline"
188  elif tagname.find("prompt") != -1:
189  tagType = "prompt"
190  elif tagname.find("express") != -1 :
191  tagType = "express"
192  elif tagname.find("hlt") != -1:
193  tagType = "hlt"
194  else:
195  print("I am assuming your tag is for the offline database...")
196  tagType = "offline"
197 
198  else:
199  print("ERROR: You must provide the database tag name")
200  exit()
201 
202  IOVbase = 'runbase'
203  timetype = 'runnumber'
204  if option.IOVbase:
205  if option.IOVbase != "runbase" and option.IOVbase != "lumibase" and option.IOVbase != "timebase":
206  print("\n\n unknown iov base option: "+ option.IOVbase +" \n\n\n")
207  exit()
208  IOVbase = option.IOVbase
209 
210  listoffiles = copyToWorkflowdir(option.data)
211  # sort list of data files in chronological order
212  sortedlist = {}
213 
214  for beam_file in listoffiles:
215 
216  if len(listoffiles)==1 and option.merged:
217  mergedfile = open(beam_file)
218  alllines = mergedfile.readlines()
219  npayloads = int(len(alllines)/23)
220  for i in range(0,npayloads):
221  block = alllines[i * 23: (i+1)*23]
222  arun = ''
223  atime = ''
224  alumi = ''
225  for line in block:
226  if line.find('Runnumber') != -1:
227  arun = line.split()[1]
228  if line.find("EndTimeOfFit") != -1:
229  atime = time.strptime(line.split()[1] + " " + line.split()[2] + " " + line.split()[3],"%Y.%m.%d %H:%M:%S %Z")
230  if line.find("LumiRange") != -1:
231  alumi = line.split()[3]
232  if line.find('Type') != -1 and line.split()[1] != '2':
233  continue
234  sortedlist[int(pack(int(arun), int(alumi)))] = block
235  break
236 
237  tmpfile = open(beam_file)
238  atime = ''
239  arun = ''
240  alumis = ''
241  skip = False
242  for line in tmpfile:
243  if line.find('Runnumber') != -1:
244  arun = line.split()[1]
245  if line.find("EndTimeOfFit") != -1:
246  atime = time.strptime(line.split()[1] + " " + line.split()[2] + " " + line.split()[3],"%Y.%m.%d %H:%M:%S %Z")
247  if line.find("LumiRange") != -1:
248  alumi = line.split()[3]
249  if line.find('Type') != -1 and line.split()[1] == '0':
250  skip = True
251  if skip:
252  print(" zero fit result, skip file " + beam_file + " with time stamp:")
253  print(" run " + arun + " lumis " + alumis)
254  else:
255  sortedlist[int(pack(int(arun), int(alumi)))] = beam_file
256 
257  tmpfile.close()
258 
259  keys = sorted(sortedlist.keys())
260 
261  # write combined data file
262  if not os.path.isdir(workflowdirArchive + "AllIOVs"):
263  os.mkdir(workflowdirArchive + "AllIOVs")
264  allbeam_file = workflowdirArchive + "AllIOVs/" + tagname + "_all_IOVs.txt"
265 # if os.path.isfile(allbeam_file):
266 
267  allfile = open( allbeam_file, 'a')
268  print(" merging all results into file: " + allbeam_file)
269 
270  # check if merged sqlite file exists
271  if os.path.exists(workflowdirArchive+"payloads/Combined.db"):
272  os.system("rm "+workflowdirArchive+"payloads/Combined.db")
273 
274 
275  nfile = 0
276  iov_since_first = '1'
277  total_files = len(keys)
278 
279  destDB = 'oracle://cms_orcon_prod/CMS_COND_31X_BEAMSPOT'
280  if option.Test:
281  destDB = 'oracle://cms_orcoff_prep/CMS_COND_BEAMSPOT'
282 
283  iov_comment = 'Beam spot position'
284  for key in keys:
285 
286  iov_since = '1'
287  iov_till = ''
288 
289  suffix = "_" + str(nfile)
290  writedb_template = os.getenv("CMSSW_BASE") + "/src/RecoVertex/BeamSpotProducer/test/write2DB_template.py"
291  readdb_template = os.getenv("CMSSW_BASE") + "/src/RecoVertex/BeamSpotProducer/test/readDB_template.py"
292  sqlite_file_name = tagname + suffix
293  sqlite_file = workflowdirTmp + sqlite_file_name + '.db'
294  metadata_file = workflowdirTmp + sqlite_file_name + '.txt'
295  nfile = nfile + 1
296 
297 
298 
299  beam_file = sortedlist[key]
300  tmp_datafilename = workflowdirTmp+"tmp_datafile.txt"
301  if option.merged:
302  tmpfile = open(tmp_datafilename,'w')
303  tmpfile.writelines(sortedlist[key])
304  tmpfile.close()
305  beam_file = tmp_datafilename
306 
307  print("read input beamspot file: " + beam_file)
308  tmpfile = open(beam_file)
309  beam_file_tmp = workflowdirTmp + beam_file[beam_file.rfind('/')+1:] + ".tmp"
310  newtmpfile = open(beam_file_tmp,"w")
311  tmp_run = ""
312  tmp_lumi_since = ""
313  tmp_lumi_till = ""
314  for line in tmpfile:
315  if line.find("Runnumber") != -1:
316  iov_since = line.split()[1]
317  iov_till = iov_since
318  tmp_run = line.split()[1]
319  elif line.find("LumiRange") != -1:
320  tmp_lumi_since = line.split()[1]
321  tmp_lumi_till = line.split()[3]
322  elif line.find("BeginTimeOfFit") == -1 and line.find("EndTimeOfFit") == -1 and line.find("LumiRange") == -1:
323  if line.find("sigmaZ0") != -1 and option.zlarge:
324  line = "sigmaZ0 10\n"
325  if line.find("Cov(3,j)") != -1 and option.zlarge:
326  line = "Cov(3,j) 0 0 0 2.5e-05 0 0 0\n"
327  newtmpfile.write(line)
328  allfile.write(line)
329 
330  # pack run number and lumi section
331  if IOVbase == "lumibase":
332  timetype = "lumiid"
333  iov_since = str( pack(int(tmp_run), int(tmp_lumi_since)) )
334  iov_till = str( pack(int(tmp_run), int(tmp_lumi_till)) )
335  # keep first iov for merged output metafile
336  if nfile == 1:
337  iov_since_first = iov_since
338 
339  tmpfile.close()
340  newtmpfile.close()
341  if option.copy:
342  continue
343 
344  beam_file = beam_file_tmp
345 
346  if not writeSqliteFile(sqlite_file,tagname,timetype,beam_file,writedb_template,workflowdirTmp):
347  print("An error occurred while writing the sqlite file: " + sqlite_file)
348 
349  subprocess.getstatusoutput('rm -f ' + beam_file)
350 
351  readSqliteFile(sqlite_file,tagname,readdb_template,workflowdirTmp)
352 
353 
354  if not os.path.isdir(workflowdirArchive + 'payloads'):
355  os.mkdir(workflowdirArchive + 'payloads')
356 
357  print(" merge sqlite file ...")
358  appendSqliteFile("Combined.db", sqlite_file, tagname, iov_since, iov_till ,workflowdirTmp)
359 
360  # keep last payload for express, and prompt tags
361  if nfile == total_files:
362  print(" this is the last IOV. You can use this payload for express and prompt conditions.")
363  os.system("cp "+sqlite_file+ " "+workflowdirArchive+"payloads/express.db")
364  print("a copy of this payload has been placed at:")
365  print(workflowdirArchive+"payloads/express.db")
366 
367  # clean up
368  os.system("rm "+ sqlite_file)
369  print(" clean up done.")
370 
371  os.system("mv " + workflowdirTmp + "Combined.db " + workflowdirArchive + "payloads/")
372  allfile.close()
373 
374 
375 
376  print(" create MERGED payload card for dropbox ...")
377 
378  sqlite_file = workflowdirArchive+'payloads/Combined.db'
379  metadata_file = workflowdirArchive+'payloads/Combined.txt'
380  dfile = open(metadata_file,'w')
381 
382  dfile.write('destDB '+ destDB +'\n')
383  dfile.write('tag '+ tagname +'\n')
384  dfile.write('inputtag' +'\n')
385  dfile.write('since ' + iov_since_first +'\n')
386  # dfile.write('till ' + iov_till +'\n')
387  if IOVbase == "runbase":
388  dfile.write('Timetype runnumber\n')
389  elif IOVbase == "lumibase":
390  dfile.write('Timetype lumiid\n')
391  checkType = tagType
392  if tagType == "express":
393  checkType = "hlt"
394  dfile.write('IOVCheck ' + checkType + '\n')
395  dfile.write('usertext ' + iov_comment +'\n')
396 
397  dfile.close()
398 
399  uuid = subprocess.getstatusoutput('uuidgen -t')[1]
400  final_sqlite_file_name = tagname + '@' + uuid
401 
402  if not os.path.isdir(workflowdirArchive + 'payloads'):
403  os.mkdir(workflowdirArchive + 'payloads')
404  subprocess.getstatusoutput('cp ' + sqlite_file + ' ' + workflowdirArchive + 'payloads/' + final_sqlite_file_name + '.db')
405  subprocess.getstatusoutput('cp ' + metadata_file + ' ' + workflowdirArchive + 'payloads/' + final_sqlite_file_name + '.txt')
406 
407  subprocess.getstatusoutput('mv ' + sqlite_file + ' ' + workflowdirLastPayloads + final_sqlite_file_name + '.db')
408  subprocess.getstatusoutput('mv ' + metadata_file + ' ' + workflowdirLastPayloads + final_sqlite_file_name + '.txt')
409 
410  print(workflowdirLastPayloads + final_sqlite_file_name + '.db')
411  print(workflowdirLastPayloads + final_sqlite_file_name + '.txt')
412 
413  if option.upload:
414  print(" scp files to offline Drop Box")
415  dropbox = "/DropBox"
416  if option.Test:
417  dropbox = "/DropBox_test"
418 
419  uploadSqliteFile(workflowdirLastPayloads,final_sqlite_file_name,dropbox)
def pack(high, low)
vector< string > parse(string line, const string &delimiter)
def copyToWorkflowdir(path)
def appendSqliteFile(combinedSqliteFileName, sqliteFileName, tagName, IOVSince, IOVTill, tmpDir="/tmp/")
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
def uploadSqliteFile(sqliteFileDirName, sqliteFileName, dropbox="/DropBox")
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def readSqliteFile(sqliteFileName, tagName, sqliteTemplateFile, tmpDir="/tmp/")
def writeSqliteFile(sqliteFileName, tagName, timeType, beamSpotFile, sqliteTemplateFile, tmpDir="/tmp/")
#define str(s)
def exit(msg="")