/search.css" rel="stylesheet" type="text/css"/> /search.js">
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

FillGoodFile.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 ############CUT VERSION##########
00004 # cutv1: HV off PMT number > 20, bad file
00005 # cutv2: HV off PMT number > 20 or abnormal PMT number > 10.
00006 #        abnormal PMT is defined by (Gain-3*GainErr>25 OR Gain+3*GainErr<15) AND Gainerr<2 AND Gainerr!=0 
00007 # cutv3: HV off PMT number > 20 or abnormal PMT number > 10.
00008 #        abnormal PMT is defined by (Gain-3*GainErr>35 OR Gain+3*GainErr<15) AND Gainerr<2 AND Gainerr!=0 
00009 #(To accommodate the earlier 2e7 gain run)
00010 #################################
00011 
00012 
00013 import ROOT
00014 TFile = ROOT.TFile
00015 import math
00016 
00017 from DybDbi import gDbi
00018 from DybDbi import ContextRange,Site,SimFlag,TimeStamp
00019 
00020 import MySQLdb
00021 import os,sys
00022 from time import strftime, localtime
00023 
00024 #os.environ['DBCONF'] = 'tmp_offline_db'
00025 #os.environ['DBCONF'] = 'neutrino2_test_db'
00026 
00027 from DybPython import DB
00028 dq_db = DB('dq_db')
00029 #dq_db = DB('neutrino2_test_db')
00030 offline_db = DB('offline_db')
00031 
00032 class GoodFileTable:
00033     """Get Rate from ROOT file"""
00034     def __init__(self, inputFilename,rev):
00035     #def __init__(self):
00036         """Constructor"""
00037         self.inputFilename = inputFilename
00038         #self.inputFilename = "realDataTag_run0006998_seq0001.root"
00039         self.runNo = inputFilename[-20:-13] #runNo is a string
00040         self.fileNo = inputFilename[-9:-5]
00041         self.jobVersion = inputFilename[-31:-24]
00042         self.jobVersion = inputFilename.split('/')[-1]
00043         self.jobVersion = self.jobVersion[:-24] 
00044         self.revision = rev
00045         self.cutVersion = 'cutv3'
00046         return
00047 
00048     def fillGoodFileTable(self):
00049         """fill good file table"""
00050         #neutrino flux table
00051 
00052 
00053         #pmt table
00054         # need to consider version here!!
00055         #r=dq_db('SELECT * FROM DqPmt WHERE RunNo=%s and FileNo=%s and Quality!=1' % (self.runNo,self.fileNo))
00056         r=dq_db('SELECT SEQNO FROM DqPmt WHERE RunNo=%s AND FileNo=%s GROUP BY SEQNO ORDER BY SEQNO DESC LIMIT 1' % (self.runNo,self.fileNo))
00057         #working pmt number
00058         if len(r)==0:# no pmt info yet
00059             #pmt info not yet dumped into DB
00060             status = 'undefined'
00061         #elif r[0]['COUNT(SEQNO)']>20:
00062         #    status = 'bad'
00063         #else:
00064         #    status = 'good'
00065         else:
00066             seqno = r[0]['SEQNO']
00067             r=dq_db('SELECT COUNT(SEQNO) FROM DqPmt WHERE SEQNO=%s AND Quality=0' % seqno)
00068             NHVoffPmt = r[0]['COUNT(SEQNO)']
00069             r=dq_db('SELECT COUNT(SEQNO) FROM DqPmt WHERE (Gain-3*GainErr>35 OR Gain+3*GainErr<15) AND Gainerr<2 AND Gainerr!=0 AND SEQNO=%s' % seqno)
00070             NAbnormalPmt = r[0]['COUNT(SEQNO)']
00071             if NHVoffPmt>20 or NAbnormalPmt>10:
00072                 status = 'bad'
00073             else:
00074                 status = 'good'
00075 
00076         #detector table
00077         #r=dq_db('SELECT * FROM DqDetector WHERE RunNo=%s and FileNo=%s' % (self.runNo,self.fileNo))
00078 
00079         #trigger type table
00080 
00081 
00082         #daq raw file table
00083         r=offline_db('SELECT SEQNO,streamType,stream FROM DaqRawDataFileInfo WHERE RunNo=%s and FileNo=%s' % (self.runNo,self.fileNo))
00084         ###  r is tuple whose element is dictionary, different from MySQLdb
00085         tagid = dq_db('SELECT id FROM tags WHERE tag="%s"' % r[0]['streamType'])
00086         tagid = tagid[0]['id']
00087         fileid = r[0]['SEQNO'] #fileid from file table or DaqRawFileInfo?
00088 
00089 
00090         userid = 2  # offline robot
00091         
00092         #obtain revisionid
00093         if self.revision==None:
00094             #nuwaVersion = os.getenv('SITEROOT')
00095             #nuwaVersion = nuwaVersion.split('/')
00096             #nuwaVersion = nuwaVersion[-1]
00097             nuwaVersion = os.getenv('NUWA_RELEASE')
00098             revision = self.jobVersion+'_'+nuwaVersion+'_'+self.cutVersion
00099         else:
00100             revision = self.revision
00101         r = dq_db('SELECT id FROM revisions WHERE comment="%s"' % revision)
00102         if len(r)==0:
00103             dq_db('INSERT INTO revisions(user_id, comment) VALUES(%s, "%s")' % (userid, revision))
00104         r = dq_db('SELECT id FROM revisions WHERE comment="%s"' % revision)
00105         revisionid = r[0]['id']
00106 
00107         
00108         #fill user_file_tag table
00109         lastNTags = dq_db('SELECT COUNT(file_id) AS NTags FROM user_file_tag WHERE file_id=%s GROUP BY tag_date ORDER BY tag_date DESC LIMIT 1' % fileid)
00110         if len(lastNTags)==1:#not the first time 
00111             NTags = lastNTags[0]['NTags'] 
00112             lastTags = dq_db('SELECT * FROM user_file_tag WHERE file_id=%s ORDER BY tag_date desc LIMIT %s' % (fileid, NTags))
00113             lastRevisionid = lastTags[0]['revision_id']
00114             lastUserid = lastTags[0]['user_id']
00115             r = dq_db('SELECT comment FROM revisions WHERE id=%s' % lastRevisionid)
00116             lastRevision = r[0]['comment']
00117             #if lastRevisionid != revisionid:#
00118             if lastRevision.find(revision)==-1:#
00119                 if lastRevision.startswith('mtagrev'):#auto update last time with manual tag version
00120                     tmp = lastRevision.split('_')
00121                     mRev = tmp.pop(0) #manual update rev number
00122                     #lastRevision = '-'.join(tmp)
00123                     #if lastRevision != revision:
00124                     #    revision = mRev+'_'+revision
00125                     #    dq_db('INSERT INTO revisions(user_id, comment) VALUES(%s, "%s")' % (userid, revision))
00126                     #    r = dq_db('SELECT id FROM revisions WHERE comment="%s"' % revision)
00127                     #    revisionid = r[0]['id']
00128                     revision = mRev+'_'+revision
00129                     r = dq_db('SELECT id FROM revisions WHERE comment="%s"' % revision)
00130                     if len(r)==0:
00131                         dq_db('INSERT INTO revisions(user_id, comment) VALUES(%s, "%s")' % (userid, revision))
00132                     r = dq_db('SELECT id FROM revisions WHERE comment="%s"' % revision)
00133                     revisionid = r[0]['id']
00134                     
00135                 elif lastUserid==1:# manual update last time
00136                     revision = ('mtagrev%s_'+revision) % lastRevisionid
00137                     r = dq_db('SELECT id FROM revisions WHERE comment="%s"' % revision)
00138                     if len(r)==0:
00139                         dq_db('INSERT INTO revisions(user_id, comment) VALUES(%s, "%s")' % (userid, revision))
00140                     r = dq_db('SELECT id FROM revisions WHERE comment="%s"' % revision)
00141                     revisionid = r[0]['id']
00142                     
00143 
00144                 #obtain current time
00145                 now = strftime("%Y-%m-%d %H:%M:%S",localtime())
00146                 for idx in range(NTags):
00147                     dq_db( 'INSERT INTO user_file_tag(user_id, file_id, tag_id, status, tag_date, revision_id) VALUES(%s, %s, %s, "%s", "%s", %s)' % (userid, fileid, lastTags[idx]['tag_id'], status, now, revisionid) )
00148                     #update most_recent_file_tag table
00149                     r = dq_db('SELECT * FROM most_recent_file_tag WHERE file_id=%s' % fileid)
00150                     if len(r)==1:
00151                         dq_db('UPDATE most_recent_file_tag SET last_status="%s", last_user_id=%s, last_date="%s", last_revision_id=%s WHERE file_id=%s' % (status, userid, now, revisionid, fileid))
00152                     else:
00153                         dq_db('INSERT INTO most_recent_file_tag(file_id, last_status, last_user_id, last_date, last_revision_id) VALUES(%s, "%s", %s, "%s", %s)' % (fileid, status, userid, now, revisionid))
00154                         
00155         else:
00156             now = strftime("%Y-%m-%d %H:%M:%S",localtime())
00157             dq_db('INSERT INTO user_file_tag(user_id, file_id, tag_id, status, tag_date, revision_id) VALUES(%s, %s, %s, "%s", "%s", %s)' % (userid, fileid, tagid, status, now, revisionid))
00158             #add corresponding record in most_recent_file_tag table
00159             dq_db('INSERT INTO most_recent_file_tag(file_id, last_status, last_user_id, last_date, last_revision_id) VALUES(%s, "%s", %s, "%s", %s)' % (fileid, status, userid, now, revisionid))
00160         offline_db.close()
00161         dq_db.close()
00162 
00163         
00164 if __name__=="__main__":
00165     """Process command line, Get rate"""
00166     import sys, getopt, os
00167     opts, args = getopt.getopt(sys.argv[1:],"r:")
00168     rev=None
00169     for opt,arg in opts:
00170         if opt=='-r':
00171             rev = arg
00172     # Make sure we have input file        
00173     if not len(args)==1:
00174         print "Usage:  % FillGoodFile.py [-r test_version] inputfile.root"
00175         sys.exit(1)
00176     #Check for existence of input files 
00177     inputFile = args[0]
00178     if not os.path.isfile(inputFile):       
00179         print "Error: '",inputFile,"' does not exist!"
00180         sys.exit(1)
00181     myalg = GoodFileTable(inputFile, rev)
00182     #print "initialized successfully"
00183     myalg.fillGoodFileTable()
00184     #print "done with dump to db"
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Fri May 16 2014 09:54:23 for DQDump by doxygen 1.7.4