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

In This Package:

DQDumpRpc.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #useage: nuwa.py -n 200 --no-history --repack-rpc="on" -m"RPCCalib.rpccalibTools" -m"DQDump.DQDumpRpc" inputdata
00003 
00004 # Load DybPython
00005 from DybPython.DybPythonAlg import DybPythonAlg
00006 from GaudiPython import SUCCESS, FAILURE
00007 from GaudiPython import gbl
00008 import GaudiKernel.SystemOfUnits as units
00009 DayaBay = gbl.DayaBay
00010 ServiceMode = gbl.ServiceMode
00011 
00012 from DybDbi import gDbi
00013 from DybDbi import GDataQualityRpc as DQRpc
00014 from DybDbi import ContextRange,Site,SimFlag,TimeStamp
00015 
00016 import os,sys
00017 import MySQLdb
00018 os.environ['DBCONF'] = 'tmp_ligs_offline_db'
00019 
00020 # Make your algorithm
00021 class DumpRpcToDbAlg(DybPythonAlg):
00022     "Template Python Algorithm"
00023     def __init__(self,name):
00024         DybPythonAlg.__init__(self,name)
00025         self.runNo = 0
00026         self.fileNo = 0
00027         return
00028 
00029     def getSeqNo(self, runNo, fileNo):
00030         """Get file seqno by runno and fileno"""
00031         try:
00032             con = MySQLdb.connect( host = "dayabaydb.lbl.gov",
00033                                    user = "dayabay",
00034                                    passwd = "3quarks",
00035                                    db = "offline_db" )
00036             cursor = con.cursor()
00037             cursor.execute("SELECT seqno FROM DaqRawDataFileInfo WHERE runNo = "
00038                             + str(runNo) + " AND " + "fileNo = " + str(fileNo))
00039             row = cursor.fetchone()
00040             if row:
00041                 print "get seqno successful"
00042         except MySQLdb.Error,e:
00043             print "Error %d: %s" % (e.args[0], e.args[1])
00044             sys.exit(1)
00045         cursor.close()
00046         con.close()
00047         return row[0]
00048 
00049     def getFileTimeInfo(self, seqNo):
00050         """Talk to offline DB and get the time range of the run"""
00051         try:
00052             con = MySQLdb.connect( host = "dayabaydb.lbl.gov",
00053                                    user = "dayabay",
00054                                    passwd = "3quarks",
00055                                    db = "offline_db" )
00056             cursor = con.cursor()
00057             cursor.execute("SELECT timestart,timeend FROM DaqRawDataFileInfoVld WHERE seqno = " + str(seqNo))
00058             row = cursor.fetchone()
00059             if row:
00060                 print "get file time info successfully"
00061         except MySQLdb.Error,e:
00062             print "Error %d: %s" % (e.args[0], e.args[1])
00063             sys.exit(1)
00064 
00065         cursor.close()
00066         con.close()
00067         return row
00068 
00069     def dumptodb(self):
00070         seqNo = self.getSeqNo(self.runNo, self.fileNo)
00071         fileTime = self.getFileTimeInfo(seqNo)
00072         import calendar
00073         starttime = calendar.timegm( fileTime[0].timetuple() )
00074         endtime = calendar.timegm( fileTime[1].timetuple() )
00075         
00076         wrt = DQRpc.Wrt()
00077         cr = ContextRange( Site.kAll, 
00078                            SimFlag.kData|SimFlag.kMC,
00079                            TimeStamp(starttime),
00080                            TimeStamp(endtime) )
00081         wrt.ctx(contextrange=cr, versiondate=TimeStamp(0,0))
00082 
00083         FILENAME='RawRPCCalib.txt'
00084         f = open(FILENAME,'r')
00085         rowCount = 0
00086         for line in f:
00087             if line[0] != '#':
00088                 line = line.strip()
00089                 para = line.split()
00090                 rpcLayerId = int( para[0] )
00091                 row = int( para[1] )
00092                 column = int( para[2] )
00093                 layer = int( para[3] )
00094                 eff = float( para[4] )
00095                 effErr = float( para[5] )
00096                 noiseRate = float( para[6] )
00097                 #noise rate error may be 'inf' calculated by RPCCalib,
00098                 #this is an ugly work-around
00099                 if para[7] == 'inf':
00100                     noiseRateErr = -1
00101                 else:
00102                     noiseRateErr = float( para[7] )
00103                 record = DQRpc.Create( RunNo = self.runNo,
00104                                        FileNo = self.fileNo,
00105                                        RpcSensorId = DayaBay.RpcSensor(rpcLayerId), 
00106                                        PanelRow = row,
00107                                        PanelColumn = column,
00108                                        Layer = layer,
00109                                        Efficiency = eff,
00110                                        EfficiencyErr = effErr,
00111                                        NoiseRate = noiseRate,
00112                                        NoiseRateErr = noiseRateErr )
00113                 wrt.Write(record)
00114         wrt.Close()
00115         return
00116     
00117     def initialize(self):
00118         status = DybPythonAlg.initialize(self)
00119         if status.isFailure(): return status
00120 
00121         self.daqDetailsSvc = self.svc('IDaqDetailsSvc','DetailsFromRecordSvc')
00122         if self.daqDetailsSvc == None:
00123             self.error("Failed to get IDaqDetailsSvc")
00124             return FAILURE
00125 
00126         self.info("initializing")
00127         return SUCCESS
00128 
00129     def execute(self):
00130         if((self.runNo == 0) and (self.fileNo == 0) ):
00131             evt = self.evtSvc()
00132             readoutHdr = evt["/Event/Readout/ReadoutHeader"]
00133             if readoutHdr == None:
00134                 self.error("Failed to get current readout header")
00135                 return FAILURE
00136             svcMode = ServiceMode(readoutHdr.context(), 0)
00137             self.runNo = self.daqDetailsSvc.runDetails(svcMode).runNumber()
00138             self.fileNo = self.daqDetailsSvc.fileDetails(svcMode).fileNumber()
00139         return SUCCESS
00140 
00141     def finalize(self):
00142         self.info("finalizing")
00143         self.dumptodb()
00144         os.system("mv RawRPCCalib.txt RawRPCCalib_finished.txt")
00145         status = DybPythonAlg.finalize(self)
00146         return status
00147 
00148 #####  Job Configuration for nuwa.py ########################################
00149 def configure():
00150     """ Configure """
00151     return
00152 
00153 def run(app):
00154     '''
00155     Configure and add the algorithm to job
00156     '''
00157     app.ExtSvc += ["DetailsFromRecordSvc"]
00158     # Add Python algorithm to job here
00159     myAlg = DumpRpcToDbAlg("MyDumpRpcAlg")
00160     # Add algorithm to job
00161     app.addAlgorithm(myAlg)
00162     pass
00163 
| 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