CMS 3D CMS Logo

mergeAndRegister.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 from __future__ import print_function
4 import os,time,sys,shutil,glob
5 from sets import Set
6 from datetime import datetime
7 import smtplib
8 from email.MIMEText import MIMEText
9 from ROOT import TFile
10 import six
11 
12 def sendmail(EmailAddress,run):
13  s=smtplib.SMTP("localhost")
14  tolist=[EmailAddress]
15  body="File merge failed by unknown reason for run"+run
16  msg = MIMEText(body)
17  msg['Subject'] = "File merge failed."
18  msg['From'] = ServerMail
19  msg['To'] = EmailAddress
20  s.sendmail(ServerMail,tolist,msg.as_string())
21  s.quit()
22 
23 def filecheck(rootfile):
24  f = TFile(rootfile)
25  if (f.IsZombie()):
26  #print "File corrupted"
27  f.Close()
28  return 0
29  else:
30  hist = f.FindObjectAny("reportSummaryContents")
31  #(skip filecheck for HcalTiming files!!)
32  if (hist == None and rootfile.rfind('HcalTiming') == -1):
33  #print "File is incomplete"
34  f.Close()
35  return 0
36  else:
37  #print "File is OK"
38  f.Close()
39  return 1
40 
41 
42 
43 TempTag = TimeTag + '-tmp'
44 if not os.path.exists(TimeTag):
45  os.system('touch -t 01010000 '+ TimeTag)
46 
47 
48 #### search new files
49 NFOUND = 0
50 NEW = {}
51 TAG = os.stat(TimeTag).st_mtime
52 for dir, subdirs, files in os.walk(DIR):
53  paths = ["%s/%s" % (dir, x) for x in files]
54  for f in [x for x in paths if re.search(r'/DQM_.*_R[0-9]{9}\.root$', x)]:
55  if os.stat(f).st_mtime > TAG and f.index("DQM_Reference") < 0:
56  NEW.get(f[-14:-5], []).append(f)
57  NFOUND += 1
58 
59 if not NFOUND:
60  print('Have not found new files...')
61  os._exit(99)
62 
63 os.system("ls -l %s" % TimeTag)
64 os.system('touch '+ TempTag)
65 print(datetime.now())
66 print('Found %d new file(s).' % NFOUND)
67 
68 #### loop for runs
69 newFiles = []
70 allOldFiles = []
71 for (run, files) in six.iteritems(NEW):
72  runnr = "%09d" % long(run)
73  destdir = "%s/%s/%s/%s" % (FILEDIR, runnr[0:3], runnr[3:6], runnr[6:9])
74  oldfiles = []
75  version = 1
76  while True:
77  destfile = "%s/DQM_V%04d_R%s.root" % (destdir, version, runnr)
78  if not os.path.exists(destfile): break
79  oldfiles.append(destfile)
80  version += 1
81 
82  if not os.path.exists(destdir):
83  os.makedirs(destdir)
84 
85  logfile = "%s.log" % destfile[:-4]
86  tmpdestfile = "%s.tmp" % destfile
87 
88  print('Run %s is being merged...' % run)
89 
90  if os.path.exists(tmpdestfile):
91  os.remove(tmpdestfile)
92 
93  ntries = 0
94  while ntries < 30:
95  LOGFILE = file(logfile, 'a')
96  LOGFILE.write(os.popen('DQMMergeFile %s %s' % (tmpdestfile, " ".join(files))).read())
97  if not os.path.exists(tmpdestfile):
98  print('Failed merging files for run %s. Try again after two minutes' % run)
99  time.sleep(WAITTIME)
100  ntries += 1
101  else:
102  break
103 
104  os.rename(tmpdestfile, destfile)
105  sendmail(YourEmail,run)
106  allOldFiles.extend(oldfiles)
107  newFiles.append(destfile)
108 
109 
110 if os.path.exists(TMPDB):
111  os.remove(TMPDB)
112 
113 if os.path.exists(DB):
114  os.rename(DB, TMPDB)
115 else:
116  logfile.write('*** INITIALISE DATABASE ***\n')
117  logfile.write(os.popen('visDQMRegisterFile %s "/Global/Online/ALL" "Global run"' % TMPDB).read())
118 
119 logfile.write('*** UNREGISTER %d OLD FILES ***\n' % len(allOldFiles))
120 while len(allOldFiles) > 0:
121  (slice, rest) = (allOldFiles[0:50], allOldFiles[50:])
122  logfile.write(os.popen('visDQMUnregisterFile %s %s' % (tmpdb, " ".join(slice))).read())
123  allOldFiles = rest
124  for old in slice:
125  os.remove(old)
126 
127 for file in newFiles:
128  print('Registering %s' % file)
129  logfile.write('*** REGISTER FILE %s ***\n' % file)
130  logfile.write(os.popen('visDQMRegisterFile %s "/Global/Online/ALL" "Global run" %s' % (TMPDB, file)).read())
131  print('%s registered' % file)
132 
133 os.rename(TMPDB, DB)
134 os.remove(TimeTag)
135 os.rename(TempTag, TimeTag)
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def filecheck(rootfile)
def sendmail(EmailAddress, run)