CMS 3D CMS Logo

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