test
CMS 3D CMS Logo

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