CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/RecoLuminosity/LumiDB/python/lumiQueryAPI.py

Go to the documentation of this file.
00001 import os
00002 import coral,datetime
00003 from RecoLuminosity.LumiDB import nameDealer,lumiTime,CommonUtil,lumiCorrections
00004 import array
00005 from RecoLuminosity.LumiDB import argparse, nameDealer, selectionParser, hltTrgSeedMapper, \
00006      connectstrParser, cacheconfigParser, tablePrinter, csvReporter, csvSelectionParser
00007 from RecoLuminosity.LumiDB.wordWrappers import wrap_always, wrap_onspace, wrap_onspace_strict
00008 from pprint import pprint, pformat
00009 
00010 '''
00011 This module defines lowlevel SQL query API for lumiDB 
00012 We do not like range queries so far because of performance of range scan.Use only necessary.
00013 The principle is to query by runnumber and per each coral queryhandle
00014 Try reuse db session/transaction and just renew query handle each time to reduce metadata queries.
00015 Avoid unnecessary explicit order by
00016 Do not handle transaction in here.
00017 Do not do explicit del queryhandle in here.
00018 Note: all the returned dict format are not sorted by itself.Sort it outside if needed.
00019 '''
00020 ###==============temporarilly here======###
00021 
00022 class ParametersObject (object):
00023 
00024     def __init__ (self):
00025         self.norm            = 1.0
00026         self.lumiversion     = '0001'
00027         self.NBX             = 3564  # number beam crossings
00028         self.rotationRate    = 11245.613 # for 3.5 TeV Beam energy
00029         self.normFactor      = 6.37
00030         self.beammode        = '' #possible choices stable, quiet, either
00031         self.verbose         = False
00032         self.noWarnings      = False
00033         self.lumischema      = 'CMS_LUMI_PROD'
00034         #self.lumidb          = 'oracle://cms_orcoff_prod/cms_lumi_prod'
00035         self.lumisummaryname = 'LUMISUMMARY'
00036         self.lumidetailname  = 'LUMIDETAIL'
00037         self.lumiXing        = False
00038         self.xingMinLum      = 1.0e-4
00039         self.xingIndex       = 5
00040         self.minBiasXsec     = 71300 # unit: microbarn
00041         self.pileupHistName  = 'pileup'
00042         self.maxPileupBin    = 10
00043         self.calculateTimeParameters()
00044 
00045     def calculateTimeParameters (self):
00046         '''Given the rotation rate, calculate lumi section length and
00047         rotation time.  This should be called if rotationRate is
00048         updated.'''
00049         self.rotationTime    = 1 / self.rotationRate
00050         self.lumiSectionLen  = 2**18 * self.rotationTime
00051         
00052     def defaultfrontierConfigString (self):
00053         return '''<frontier-connect><proxy url = "https://cmst0frontier.cern.ch:3128"/><proxy url = "https://cmst0frontier.cern.ch:3128"/><proxy url = "https://cmst0frontier1.cern.ch:3128"/><proxy url = "https://cmst0frontier2.cern.ch:3128"/><server url = "https://cmsfrontier.cern.ch:8000/FrontierInt"/><server url = "https://cmsfrontier.cern.ch:8000/FrontierInt"/><server url = "https://cmsfrontier1.cern.ch:8000/FrontierInt"/><server url = "https://cmsfrontier2.cern.ch:8000/FrontierInt"/><server url = "https://cmsfrontier3.cern.ch:8000/FrontierInt"/><server url = "https://cmsfrontier4.cern.ch:8000/FrontierInt"/></frontier-connect>'''
00054 
00055 def lslengthsec (numorbit, numbx):
00056     #print numorbit, numbx
00057     l = numorbit * numbx * 25.0e-09
00058     return l
00059 
00060 def lsBylsLumi (deadtable):
00061     """
00062     input: {lsnum:[deadtime, instlumi, bit_0, norbits,prescale...]}
00063     output: {lsnum:[instlumi, recordedlumi...]}
00064     """
00065     result = {}
00066     for myls, deadArray in deadtable.items():
00067         lstime = lslengthsec (deadArray[3], 3564)
00068         instlumi = deadArray[1] * lstime
00069         if float( deadArray[2] ) ==  0.0:
00070             deadfrac = 1.0
00071         else:
00072             deadfrac = float (deadArray[0]) / (float (deadArray[2])*float(deadArray[4]))
00073         recordedLumi = instlumi * (1.0 - deadfrac)
00074         myLsList = [instlumi, recordedLumi]
00075         #print myls,instlumi,recordedLumi,lstime,deadfrac
00076         if len (deadArray) > 5:
00077             myLsList.extend (deadArray[5:])
00078         result[myls] = myLsList
00079     return result
00080 
00081 
00082 def deliveredLumiForRange (dbsession, parameters,inputRange,finecorrections=None):
00083     '''Takes either single run as a string or dictionary of run ranges'''
00084     lumidata = []
00085     runs=[]
00086     if isinstance(inputRange, str):
00087         runs.append(int(inputRange))
00088     else:
00089         runs=inputRange.runs()
00090     for r in sorted(runs):
00091         if parameters.verbose:
00092             print "run", run
00093         c=None
00094         if finecorrections:
00095             c=finecorrections[r]
00096         lumidata.append( deliveredLumiForRun (dbsession, parameters,r,finecorrections=c) )       
00097     return lumidata
00098 
00099 def recordedLumiForRange (dbsession, parameters, inputRange,finecorrections=None):
00100     '''Takes either single run as a string or dictionary of run ranges'''
00101     lumidata = []
00102     if isinstance(inputRange,str):
00103         run=int(inputRange)
00104         if finecorrections and finecorrections[run]:
00105             lumiDataPiece = recordedLumiForRun (dbsession, parameters,inputRange,lslist=None,finecorrections=finecorrections[run])
00106         else:
00107             lumiDataPiece = recordedLumiForRun(dbsession, parameters,run)
00108         if parameters.lumiXing:            
00109             # get the xing information for the run
00110             if  finecorrections and finecorrections[run]:
00111                 xingLumiDict = xingLuminosityForRun (dbsession, inputRange,
00112                                                      parameters,finecorrections=finecorrections[run])
00113             else:
00114                 xingLumiDict = xingLuminosityForRun (dbsession, inputRange,
00115                                                      parameters)
00116             mergeXingLumi (lumiDataPiece, xingLumiDict)
00117         lumidata.append (lumiDataPiece)
00118     else:
00119         # we want to collapse the lists so that every run is considered once.
00120         runLsDict = {}
00121         maxLumiSectionDict = {}
00122         for (run, lslist) in sorted (inputRange.runsandls().items() ):
00123             if(len(lslist)!=0):
00124                 maxLumiSectionDict[run] = max ( max (lslist),
00125                                            maxLumiSectionDict.get(run,0) )
00126             runLsDict.setdefault (run, []).append (lslist)
00127         for run, metaLsList in sorted (runLsDict.iteritems()):
00128             if parameters.verbose:
00129                 print "run", run
00130             runLumiData = []
00131             for lslist in metaLsList:
00132                 if finecorrections and finecorrections[run]:
00133                     runLumiData.append( recordedLumiForRun (dbsession, parameters,run,lslist=lslist,finecorrections=finecorrections[run]) )
00134                     
00135                 else:
00136                     runLumiData.append( recordedLumiForRun (dbsession, parameters,run,lslist=lslist) )
00137             if parameters.lumiXing:
00138                 # get the xing information once for the whole run
00139                 if finecorrections and finecorrections[run]:
00140                     xingLumiDict = xingLuminosityForRun (dbsession, run,
00141                                                          parameters,
00142                                                          maxLumiSection = \
00143                                                          maxLumiSectionDict[run],
00144                                                          finecorrections=finecorrections[run])
00145                 else:
00146                     xingLumiDict = xingLuminosityForRun (dbsession, run,
00147                                                          parameters,
00148                                                          maxLumiSection = \
00149                                                          maxLumiSectionDict[run])
00150                 # merge it with every piece of lumi data for this run
00151                 for lumiDataPiece in runLumiData:
00152                     mergeXingLumi (lumiDataPiece, xingLumiDict)
00153                     lumidata.append (lumiDataPiece)
00154             else:
00155                 lumidata.extend( runLumiData )
00156     return lumidata
00157 
00158 def deliveredLumiForRun (dbsession, parameters, runnum, finecorrections=None ):    
00159     """
00160     select sum (INSTLUMI), count (INSTLUMI) from lumisummary where runnum = 124025 and lumiversion = '0001';
00161     select INSTLUMI,NUMORBIT  from lumisummary where runnum = 124025 and lumiversion = '0001'
00162     query result unit E27cm^-2 (= 1 / mb)
00163 
00164     optional corrections=None/(constfactor,afterglowfactor,nonlinearfactor)
00165     """    
00166     #if parameters.verbose:
00167     #    print 'deliveredLumiForRun : norm : ', parameters.norm, ' : run : ', runnum
00168     #output ['run', 'totalls', 'delivered', 'beammode']
00169     delivered = 0.0
00170     totalls = 0
00171     try:
00172         conditionstring="RUNNUM = :runnum AND LUMIVERSION = :lumiversion"
00173         dbsession.transaction().start (True)
00174         schema = dbsession.nominalSchema()
00175         query = schema.tableHandle (nameDealer.lumisummaryTableName()).newQuery()
00176         query.addToOutputList("INSTLUMI",'instlumi')
00177         query.addToOutputList ("NUMORBIT", "norbits")
00178         queryBind = coral.AttributeList()
00179         queryBind.extend ("runnum", "unsigned int")
00180         queryBind.extend ("lumiversion", "string")
00181         queryBind["runnum"].setData (int (runnum))
00182         queryBind["lumiversion"].setData (parameters.lumiversion)
00183         #print parameters.beammode
00184         if len(parameters.beammode)!=0:
00185             conditionstring=conditionstring+' and BEAMSTATUS=:beamstatus'
00186             queryBind.extend('beamstatus','string')
00187             queryBind['beamstatus'].setData(parameters.beammode)
00188         result = coral.AttributeList()
00189         result.extend ("instlumi", "float")
00190         result.extend ("norbits", "unsigned int")
00191         query.defineOutput (result)
00192         query.setCondition (conditionstring,queryBind)
00193         cursor = query.execute()
00194         while cursor.next():
00195             instlumi = cursor.currentRow()['instlumi'].data()
00196             norbits = cursor.currentRow()['norbits'].data()
00197             if finecorrections is not None:
00198                 instlumi=lumiCorrections.applyfinecorrection(instlumi,finecorrections[0],finecorrections[1],finecorrections[2])
00199             if instlumi is not None and norbits is not None:
00200                 lstime = lslengthsec(norbits, parameters.NBX)
00201                 delivered=delivered+instlumi*parameters.norm*lstime
00202                 totalls+=1
00203         del query
00204         dbsession.transaction().commit()
00205         lumidata = []
00206         if delivered == 0.0:
00207             lumidata = [str (runnum), 'N/A', 'N/A', 'N/A']
00208         else:
00209             lumidata = [str (runnum), str (totalls), '%.3f'%delivered, parameters.beammode]
00210         return lumidata
00211     except Exception, e:
00212         print str (e)
00213         dbsession.transaction().rollback()
00214         del dbsession
00215 
00216 def recordedLumiForRun (dbsession, parameters, runnum, lslist=None,finecorrections=None):
00217     """
00218     lslist = [] means take none in the db
00219     lslist = None means to take all in the db
00220     output: ['runnumber', 'trgtable{}', 'deadtable{}']
00221     """
00222     recorded = 0.0
00223     lumidata = [] #[runnumber, trgtable, deadtable]
00224     trgtable = {} #{hltpath:[l1seed, hltprescale, l1prescale]}
00225     deadtable = {} #{lsnum:[deadtime, instlumi, bit_0, norbits,bitzero_prescale]}
00226     lumidata.append (runnum)
00227     lumidata.append (trgtable)
00228     lumidata.append (deadtable)
00229     collectedseeds = [] #[ (hltpath, l1seed)]
00230     conditionstring='trghltmap.HLTKEY = cmsrunsummary.HLTKEY AND cmsrunsummary.RUNNUM = :runnumber'
00231     try:
00232         dbsession.transaction().start (True)
00233         schema = dbsession.nominalSchema()
00234         query = schema.newQuery()
00235         query.addToTableList (nameDealer.cmsrunsummaryTableName(), 'cmsrunsummary')
00236         query.addToTableList (nameDealer.trghltMapTableName(), 'trghltmap')#small table first
00237         queryCondition = coral.AttributeList()
00238         queryCondition.extend ("runnumber", "unsigned int")
00239         queryCondition["runnumber"].setData (int (runnum))
00240         query.setCondition (conditionstring,queryCondition)
00241         query.addToOutputList ("trghltmap.HLTPATHNAME", "hltpathname")
00242         query.addToOutputList ("trghltmap.L1SEED", "l1seed")
00243         result = coral.AttributeList()
00244         result.extend ("hltpathname", "string")
00245         result.extend ("l1seed", "string")
00246         query.defineOutput (result)
00247         cursor = query.execute()
00248         while cursor.next():
00249             hltpathname = cursor.currentRow()["hltpathname"].data()
00250             l1seed = cursor.currentRow()["l1seed"].data()
00251             collectedseeds.append ( (hltpathname, l1seed))
00252         #print 'collectedseeds ', collectedseeds
00253         del query
00254         dbsession.transaction().commit()
00255         #loop over hltpath
00256         for (hname, sname) in collectedseeds:
00257             l1bitname = hltTrgSeedMapper.findUniqueSeed (hname, sname)
00258             #print 'found unque seed ', hname, l1bitname
00259             if l1bitname:
00260                 lumidata[1][hname] = []
00261                 lumidata[1][hname].append (l1bitname.replace ('\"', ''))
00262         dbsession.transaction().start (True)
00263         schema = dbsession.nominalSchema()
00264         hltprescQuery = schema.tableHandle (nameDealer.hltTableName()).newQuery()
00265         hltprescQuery.addToOutputList ("PATHNAME", "hltpath")
00266         hltprescQuery.addToOutputList ("PRESCALE", "hltprescale")
00267         hltprescCondition = coral.AttributeList()
00268         hltprescCondition.extend ('runnumber', 'unsigned int')
00269         hltprescCondition.extend ('cmslsnum', 'unsigned int')
00270         hltprescCondition.extend ('inf', 'unsigned int')
00271         hltprescResult = coral.AttributeList()
00272         hltprescResult.extend ('hltpath', 'string')
00273         hltprescResult.extend ('hltprescale', 'unsigned int')
00274         hltprescQuery.defineOutput (hltprescResult)
00275         hltprescCondition['runnumber'].setData (int (runnum))
00276         hltprescCondition['cmslsnum'].setData (1)
00277         hltprescCondition['inf'].setData (0)
00278         hltprescQuery.setCondition ("RUNNUM = :runnumber and CMSLSNUM = :cmslsnum and PRESCALE != :inf",hltprescCondition)
00279         cursor = hltprescQuery.execute()
00280         while cursor.next():
00281             hltpath = cursor.currentRow()['hltpath'].data()
00282             hltprescale = cursor.currentRow()['hltprescale'].data()
00283             if lumidata[1].has_key (hltpath):
00284                 lumidata[1][hltpath].append (hltprescale)
00285                 
00286         cursor.close()
00287         del hltprescQuery
00288         dbsession.transaction().commit()      
00289         dbsession.transaction().start (True)
00290         schema = dbsession.nominalSchema()
00291         query = schema.newQuery()
00292         query.addToTableList (nameDealer.trgTableName(), 'trg')
00293         query.addToTableList (nameDealer.lumisummaryTableName(), 'lumisummary')#small table first--right-most
00294         queryCondition = coral.AttributeList()
00295         queryCondition.extend ("runnumber", "unsigned int")
00296         queryCondition.extend ("lumiversion", "string")
00297         queryCondition["runnumber"].setData (int (runnum))
00298         queryCondition["lumiversion"].setData (parameters.lumiversion)
00299         conditionstring='lumisummary.RUNNUM =:runnumber and lumisummary.LUMIVERSION =:lumiversion AND lumisummary.CMSLSNUM=trg.CMSLSNUM and lumisummary.RUNNUM=trg.RUNNUM'
00300         if len(parameters.beammode)!=0:
00301             conditionstring=conditionstring+' and lumisummary.BEAMSTATUS=:beamstatus'
00302             queryCondition.extend('beamstatus','string')
00303             queryCondition['beamstatus'].setData(parameters.beammode)
00304         query.setCondition(conditionstring,queryCondition)
00305         query.addToOutputList ("lumisummary.CMSLSNUM", "cmsls")
00306         query.addToOutputList ("lumisummary.INSTLUMI", "instlumi")
00307         query.addToOutputList ("lumisummary.NUMORBIT", "norbits")
00308         query.addToOutputList ("trg.TRGCOUNT",         "trgcount")
00309         query.addToOutputList ("trg.BITNAME",          "bitname")
00310         query.addToOutputList ("trg.DEADTIME",         "trgdeadtime")
00311         query.addToOutputList ("trg.PRESCALE",         "trgprescale")
00312         query.addToOutputList ("trg.BITNUM",           "trgbitnum")
00313         
00314         result = coral.AttributeList()
00315         result.extend ("cmsls",       "unsigned int")
00316         result.extend ("instlumi",    "float")
00317         result.extend ("norbits",     "unsigned int")
00318         result.extend ("trgcount",    "unsigned int")
00319         result.extend ("bitname",     "string")
00320         result.extend ("trgdeadtime", "unsigned long long")
00321         result.extend ("trgprescale", "unsigned int")
00322         result.extend ("trgbitnum",   "unsigned int")
00323         trgprescalemap = {}
00324         query.defineOutput (result)
00325         cursor = query.execute()
00326         while cursor.next():
00327             cmsls       = cursor.currentRow()["cmsls"].data()
00328             instlumi    = cursor.currentRow()["instlumi"].data()*parameters.norm
00329             if finecorrections:
00330                 instlumi=lumiCorrections.applyfinecorrection(instlumi,finecorrections[0],finecorrections[1],finecorrections[2])
00331             norbits     = cursor.currentRow()["norbits"].data()
00332             trgcount    = cursor.currentRow()["trgcount"].data()
00333             trgbitname  = cursor.currentRow()["bitname"].data()
00334             trgdeadtime = cursor.currentRow()["trgdeadtime"].data()
00335             trgprescale = cursor.currentRow()["trgprescale"].data()
00336             trgbitnum   = cursor.currentRow()["trgbitnum"].data()
00337             if cmsls == 1:
00338                 if not trgprescalemap.has_key (trgbitname):
00339                     trgprescalemap[trgbitname] = trgprescale
00340             if trgbitnum == 0:
00341                 if not deadtable.has_key (cmsls):
00342                     deadtable[cmsls] = []
00343                     deadtable[cmsls].append (trgdeadtime)
00344                     deadtable[cmsls].append (instlumi)
00345                     deadtable[cmsls].append (trgcount)
00346                     deadtable[cmsls].append (norbits)
00347                     deadtable[cmsls].append (trgprescale)
00348         cursor.close()
00349         del query
00350         dbsession.transaction().commit()
00351         
00352         #
00353         #consolidate results
00354         #
00355         #trgtable
00356         #print 'trgprescalemap', trgprescalemap
00357         #print lumidata[1]
00358         for hpath, trgdataseq in lumidata[1].items():   
00359             bitn = trgdataseq[0]
00360             if trgprescalemap.has_key (bitn) and len (trgdataseq) == 2:
00361                 lumidata[1][hpath].append (trgprescalemap[bitn])                
00362         #filter selected cmsls
00363         lumidata[2] = filterDeadtable (deadtable, lslist)
00364         if not parameters.noWarnings:
00365             if len(lumidata[2])!=0:
00366                 for lumi, deaddata in lumidata[2].items():
00367                     if deaddata[1] == 0.0 and deaddata[2]!=0 and deaddata[0]!=0:
00368                         print '[Warning] : run %s :ls %d has 0 instlumi but trigger has data' % (runnum, lumi)
00369                     if (deaddata[2] == 0 or deaddata[0] == 0) and deaddata[1]!=0.0:
00370                         print '[Warning] : run %s :ls %d has 0 dead counts or 0 zerobias bit counts, but inst!=0' % (runnum, lumi)
00371         #print 'lumidata[2] ', lumidata[2]
00372     except Exception, e:
00373         print str (e)
00374         dbsession.transaction().rollback()
00375         del dbsession
00376     #print 'before return lumidata ', lumidata
00377     ## if parameters.lumiXing:
00378     ##     xingLumiDict =  xingLuminosityForRun (dbsession, runnum, parameters)
00379     ##     mergeXingLumi (lumidata, xingLumiDict)
00380     return lumidata
00381 
00382 
00383 def filterDeadtable (inTable, lslist):
00384     result = {}
00385     if lslist is None:
00386         return inTable
00387     if len (lslist) == 0: #if request no ls, then return nothing
00388         return result
00389     for existingLS in inTable.keys():
00390         if existingLS in lslist:
00391             result[existingLS] = inTable[existingLS]
00392     return result
00393 
00394 
00395 def printDeliveredLumi (lumidata, mode):
00396     labels = [ ('Run', 'Delivered LS', 'Delivered'+u' (/\u03bcb)'.encode ('utf-8'), 'Beam Mode')]
00397     print tablePrinter.indent (labels+lumidata, hasHeader = True, separateRows = False,
00398                                prefix = '| ', postfix = ' |', justify = 'right',
00399                                delim = ' | ', wrapfunc = lambda x: wrap_onspace (x, 20) )
00400 
00401 def dumpData (lumidata, filename):
00402     """
00403     input params: lumidata [{'fieldname':value}]
00404                   filename csvname
00405     """
00406     
00407     r = csvReporter.csvReporter(filename)
00408     r.writeRows(lumidata)
00409 
00410 def calculateTotalRecorded (deadtable):
00411     """
00412     input: {lsnum:[deadtime, instlumi, bit_0, norbits,prescale]}
00413     output: recordedLumi
00414     """
00415     recordedLumi = 0.0
00416     for myls, d in deadtable.items():
00417         instLumi = d[1]
00418         #deadfrac = float (d[0])/float (d[2]*3564)
00419         #print myls, float (d[2])
00420         if float (d[2]) == 0.0:
00421             deadfrac = 1.0
00422         else:
00423             deadfrac = float (d[0])/(float (d[2])*float (d[-1]))
00424         lstime = lslengthsec (d[3], 3564)
00425         recordedLumi += instLumi* (1.0-deadfrac)*lstime
00426     return recordedLumi
00427 
00428 
00429 def splitlistToRangeString (inPut):
00430     result = []
00431     first = inPut[0]
00432     last = inPut[0]
00433     result.append ([inPut[0]])
00434     counter = 0
00435     for i in inPut[1:]:
00436         if i == last+1:
00437             result[counter].append (i)
00438         else:
00439             counter += 1
00440             result.append ([i])
00441         last = i
00442     return ', '.join (['['+str (min (x))+'-'+str (max (x))+']' for x in result])
00443 
00444 
00445 def calculateEffective (trgtable, totalrecorded):
00446     """
00447     input: trgtable{hltpath:[l1seed, hltprescale, l1prescale]}, totalrecorded (float)
00448     output:{hltpath, recorded}
00449     """
00450     #print 'inputtrgtable', trgtable
00451     result = {}
00452     for hltpath, data in trgtable.items():
00453         if len (data) ==  3:
00454             result[hltpath] = totalrecorded/ (data[1]*data[2])
00455         else:
00456             result[hltpath] = 0.0
00457     return result
00458 
00459 
00460 def getDeadfractions (deadtable):
00461     """
00462     inputtable: {lsnum:[deadtime, instlumi, bit_0, norbits,bit_0_prescale]}
00463     output: {lsnum:deadfraction}
00464     """
00465     result = {}
00466     for myls, d in deadtable.items():
00467         #deadfrac = float (d[0])/ (float (d[2])*float (3564))
00468         if float (d[2]) == 0.0: ##no beam
00469             deadfrac = -1.0
00470         else:
00471             deadfrac = float (d[0])/ (float (d[2])*float(d[-1]))
00472         result[myls] = deadfrac
00473     return result
00474 
00475 def printPerLSLumi (lumidata, isVerbose = False):
00476     '''
00477     input lumidata  [['runnumber', 'trgtable{}', 'deadtable{}']]
00478     deadtable {lsnum:[deadtime, instlumi, bit_0, norbits,prescale]}
00479     '''
00480     datatoprint = []
00481     totalrow = []
00482     labels = [ ('Run', 'LS', 'Delivered', 'Recorded'+u' (/\u03bcb)'.encode ('utf-8'))]
00483     lastrowlabels = [ ('Selected LS', 'Delivered'+u' (/\u03bcb)'.encode ('utf-8'), 'Recorded'+u' (/\u03bcb)'.encode ('utf-8'))]
00484     totalDeliveredLS = 0
00485     totalSelectedLS = 0
00486     totalDelivered = 0.0
00487     totalRecorded = 0.0
00488     
00489     for perrundata in lumidata:
00490         runnumber = perrundata[0]
00491         deadtable = perrundata[2]
00492         lumiresult = lsBylsLumi (deadtable)
00493         totalSelectedLS = totalSelectedLS+len (deadtable)
00494         for lsnum, dataperls in lumiresult.items():
00495             rowdata = []
00496             if len (dataperls) == 0:
00497                 rowdata  +=  [str (runnumber), str (lsnum), 'N/A', 'N/A']
00498             else:
00499                 rowdata  +=  [str (runnumber), str (lsnum), '%.3f' % (dataperls[0]), '%.3f' % (dataperls[1])]
00500                 totalDelivered = totalDelivered+dataperls[0]
00501                 totalRecorded = totalRecorded+dataperls[1]
00502             datatoprint.append (rowdata)
00503     totalrow.append ([str (totalSelectedLS), '%.3f'% (totalDelivered), '%.3f'% (totalRecorded)])
00504     print ' ==  = '
00505     print tablePrinter.indent (labels+datatoprint, hasHeader = True, separateRows = False, prefix = '| ',
00506                                postfix = ' |', justify = 'right', delim = ' | ',
00507                                wrapfunc = lambda x: wrap_onspace_strict (x, 22))
00508     print ' ==  =  Total : '
00509     print tablePrinter.indent (lastrowlabels+totalrow, hasHeader = True, separateRows = False, prefix = '| ',
00510                                postfix = ' |', justify = 'right', delim = ' | ',
00511                                wrapfunc = lambda x: wrap_onspace (x, 20))    
00512 
00513     
00514 def dumpPerLSLumi (lumidata):
00515     datatodump = []
00516     for perrundata in lumidata:
00517         runnumber = perrundata[0]
00518         deadtable = perrundata[2]
00519         lumiresult = lsBylsLumi (deadtable)
00520         for lsnum, dataperls in lumiresult.items():
00521             rowdata = []
00522             if len (dataperls) == 0:
00523                 rowdata += [str (runnumber), str (lsnum), 'N/A', 'N/A']
00524             else:
00525                 rowdata += [str (runnumber), str (lsnum), dataperls[0], dataperls[1]]
00526             if len (dataperls) > 2:
00527                 rowdata.extend ( flatten (dataperls[2:]) )
00528             datatodump.append (rowdata)
00529     return datatodump
00530 
00531 
00532 def printRecordedLumi (lumidata, isVerbose = False, hltpath = ''):
00533     datatoprint = []
00534     totalrow = []
00535     labels = [ ('Run', 'HLT path', 'Recorded'+u' (/\u03bcb)'.encode ('utf-8'))]
00536     lastrowlabels = [ ('Selected LS', 'Recorded'+u' (/\u03bcb)'.encode ('utf-8'))]
00537     if len (hltpath) != 0 and hltpath != 'all':
00538         lastrowlabels = [ ('Selected LS', 'Recorded'+u' (/\u03bcb)'.encode ('utf-8'),
00539                            'Effective '+u' (/\u03bcb) '.encode ('utf-8')+hltpath)]
00540     if isVerbose:
00541         labels = [ ('Run', 'HLT-path', 'L1-bit', 'L1-presc', 'HLT-presc', 'Recorded'+u' (/\u03bcb)'.encode ('utf-8'))]
00542     totalSelectedLS = 0
00543     totalRecorded = 0.0
00544     totalRecordedInPath = 0.0
00545     
00546     for dataperRun in lumidata:
00547         runnum = dataperRun[0]
00548         if len (dataperRun[1]) == 0:
00549             rowdata = []
00550             rowdata += [str (runnum)]+2*['N/A']
00551             datatoprint.append (rowdata)
00552             continue
00553         perlsdata = dataperRun[2]
00554         totalSelectedLS = totalSelectedLS+len (perlsdata)
00555         recordedLumi = 0.0
00556         #norbits = perlsdata.values()[0][3]
00557         recordedLumi = calculateTotalRecorded (perlsdata)
00558         totalRecorded = totalRecorded+recordedLumi
00559         trgdict = dataperRun[1]
00560         effective = calculateEffective (trgdict, recordedLumi)
00561         if trgdict.has_key (hltpath) and effective.has_key (hltpath):
00562             rowdata = []
00563             l1bit = trgdict[hltpath][0]
00564             if len (trgdict[hltpath]) !=  3:
00565                 if not isVerbose:
00566                     rowdata += [str (runnum), hltpath, 'N/A']
00567                 else:
00568                     rowdata += [str (runnum), hltpath, l1bit, 'N/A', 'N/A', 'N/A']
00569             else:
00570                 if not isVerbose:
00571                     rowdata += [str (runnum), hltpath, '%.3f'% (effective[hltpath])]
00572                 else:
00573                     hltprescale = trgdict[hltpath][1]
00574                     l1prescale = trgdict[hltpath][2]
00575                     rowdata += [str (runnum), hltpath, l1bit, str (l1prescale), str (hltprescale),
00576                                 '%.3f'% (effective[hltpath])]
00577                 totalRecordedInPath = totalRecordedInPath+effective[hltpath]
00578             datatoprint.append (rowdata)
00579             continue
00580         
00581         for trg, trgdata in trgdict.items():
00582             #print trg, trgdata
00583             rowdata = []                    
00584             if trg == trgdict.keys()[0]:
00585                 rowdata += [str (runnum)]
00586             else:
00587                 rowdata += ['']
00588             l1bit = trgdata[0]
00589             if len (trgdata) == 3:
00590                 if not isVerbose:
00591                     rowdata += [trg, '%.3f'% (effective[trg])]
00592                 else:
00593                     hltprescale = trgdata[1]
00594                     l1prescale = trgdata[2]
00595                     rowdata += [trg, l1bit, str (l1prescale), str (hltprescale), '%.3f'% (effective[trg])]
00596             else:
00597                 if not isVerbose:
00598                     rowdata += [trg, 'N/A']
00599                 else:
00600                     rowdata += [trg, l1bit, 'N/A', 'N/A', '%.3f'% (effective[trg])]
00601             datatoprint.append (rowdata)
00602     #print datatoprint
00603     print ' ==  = '
00604     print tablePrinter.indent (labels+datatoprint, hasHeader = True, separateRows = False, prefix = '| ',
00605                                postfix = ' |', justify = 'right', delim = ' | ',
00606                                wrapfunc = lambda x: wrap_onspace_strict (x, 22))
00607 
00608     if len (hltpath) != 0 and hltpath != 'all':
00609         totalrow.append ([str (totalSelectedLS), '%.3f'% (totalRecorded), '%.3f'% (totalRecordedInPath)])
00610     else:
00611         totalrow.append ([str (totalSelectedLS), '%.3f'% (totalRecorded)])
00612     print ' ==  =  Total : '
00613     print tablePrinter.indent (lastrowlabels+totalrow, hasHeader = True, separateRows = False, prefix = '| ',
00614                                postfix = ' |', justify = 'right', delim = ' | ',
00615                                wrapfunc = lambda x: wrap_onspace (x, 20))    
00616     if isVerbose:
00617         deadtoprint = []
00618         deadtimelabels = [ ('Run', 'Lumi section : Dead fraction')]
00619 
00620         for dataperRun in lumidata:
00621             runnum = dataperRun[0]
00622             if len (dataperRun[1]) == 0:
00623                 deadtoprint.append ([str (runnum), 'N/A'])
00624                 continue
00625             perlsdata = dataperRun[2]
00626             #print 'perlsdata 2 : ', perlsdata
00627             deadT = getDeadfractions (perlsdata)
00628             t = ''
00629             for myls, de in deadT.items():
00630                 if de<0:
00631                     t += str (myls)+':nobeam '
00632                 else:
00633                     t += str (myls)+':'+'%.5f'% (de)+' '
00634             deadtoprint.append ([str (runnum), t])
00635         print ' ==  = '
00636         print tablePrinter.indent (deadtimelabels+deadtoprint, hasHeader = True, separateRows = True, prefix = '| ',
00637                                    postfix = ' |', justify = 'right', delim = ' | ',
00638                                    wrapfunc = lambda x: wrap_onspace (x, 80))
00639 
00640 
00641 def dumpRecordedLumi (lumidata, hltpath = ''):
00642     #labels = ['Run', 'HLT path', 'Recorded']
00643     datatodump = []
00644     for dataperRun in lumidata:
00645         runnum = dataperRun[0]
00646         if len (dataperRun[1]) == 0:
00647             rowdata = []
00648             rowdata += [str (runnum)]+2*['N/A']
00649             datatodump.append (rowdata)
00650             continue
00651         perlsdata = dataperRun[2]
00652         recordedLumi = 0.0
00653         #norbits = perlsdata.values()[0][3]
00654         recordedLumi = calculateTotalRecorded (perlsdata)
00655         trgdict = dataperRun[1]
00656         effective = calculateEffective (trgdict, recordedLumi)
00657         if trgdict.has_key (hltpath) and effective.has_key (hltpath):
00658             rowdata = []
00659             l1bit = trgdict[hltpath][0]
00660             if len (trgdict[hltpath]) !=  3:
00661                 rowdata += [str (runnum), hltpath, 'N/A']
00662             else:
00663                 hltprescale = trgdict[hltpath][1]
00664                 l1prescale = trgdict[hltpath][2]
00665                 rowdata += [str (runnum), hltpath, effective[hltpath]]
00666             datatodump.append (rowdata)
00667             continue
00668         
00669         for trg, trgdata in trgdict.items():
00670             #print trg, trgdata
00671             rowdata = []                    
00672             rowdata += [str (runnum)]
00673             l1bit = trgdata[0]
00674             if len (trgdata) == 3:
00675                 rowdata += [trg, effective[trg]]
00676             else:
00677                 rowdata += [trg, 'N/A']
00678             datatodump.append (rowdata)
00679     return datatodump
00680 
00681 
00682 def printOverviewData (delivered, recorded, hltpath = ''):
00683     if len (hltpath) == 0 or hltpath == 'all':
00684         toprowlabels = [ ('Run', 'Delivered LS', 'Delivered'+u' (/\u03bcb)'.encode ('utf-8'), 'Selected LS', 'Recorded'+u' (/\u03bcb)'.encode ('utf-8') )]
00685         lastrowlabels = [ ('Delivered LS', 'Delivered'+u' (/\u03bcb)'.encode ('utf-8'), 'Selected LS', 'Recorded'+u' (/\u03bcb)'.encode ('utf-8') ) ]
00686     else:
00687         toprowlabels = [ ('Run', 'Delivered LS', 'Delivered'+u' (/\u03bcb)'.encode ('utf-8'), 'Selected LS', 'Recorded'+u' (/\u03bcb)'.encode ('utf-8'), 'Effective'+u' (/\u03bcb) '.encode ('utf-8')+hltpath )]
00688         lastrowlabels = [ ('Delivered LS', 'Delivered'+u' (/\u03bcb)'.encode ('utf-8'), 'Selected LS', 'Recorded'+u' (/\u03bcb)'.encode ('utf-8'), 'Effective '+u' (/\u03bcb) '.encode ('utf-8')+hltpath)]
00689     datatable = []
00690     totaldata = []
00691     totalDeliveredLS = 0
00692     totalSelectedLS = 0
00693     totalDelivered = 0.0
00694     totalRecorded = 0.0
00695     totalRecordedInPath = 0.0
00696     totaltable = []
00697     for runidx, deliveredrowdata in enumerate (delivered):
00698         rowdata = []
00699         rowdata += [deliveredrowdata[0], deliveredrowdata[1], deliveredrowdata[2]]
00700         if deliveredrowdata[1] == 'N/A': #run does not exist
00701             if  hltpath != '' and hltpath != 'all':
00702                 rowdata += ['N/A', 'N/A', 'N/A']
00703             else:
00704                 rowdata += ['N/A', 'N/A']
00705             datatable.append (rowdata)
00706             continue
00707         totalDeliveredLS += int (deliveredrowdata[1])
00708         totalDelivered += float (deliveredrowdata[2])
00709         selectedls = recorded[runidx][2].keys()
00710         #print 'runidx ', runidx, deliveredrowdata
00711         #print 'selectedls ', selectedls
00712         if len (selectedls) == 0:
00713             selectedlsStr = '[]'
00714             recordedLumi = 0
00715             if  hltpath != '' and hltpath != 'all':
00716                 rowdata += [selectedlsStr, 'N/A', 'N/A']
00717             else:
00718                 rowdata += [selectedlsStr, 'N/A']
00719         else:
00720             selectedlsStr = splitlistToRangeString (selectedls)
00721             recordedLumi = calculateTotalRecorded (recorded[runidx][2])
00722             lumiinPaths = calculateEffective (recorded[runidx][1], recordedLumi)
00723             if hltpath != '' and hltpath != 'all':
00724                 if lumiinPaths.has_key (hltpath):
00725                     rowdata += [selectedlsStr, '%.3f'% (recordedLumi), '%.3f'% (lumiinPaths[hltpath])]
00726                     totalRecordedInPath += lumiinPaths[hltpath]
00727                 else:
00728                     rowdata += [selectedlsStr, '%.3f'% (recordedLumi), 'N/A']
00729             else:
00730                 #rowdata += [selectedlsStr, '%.3f'% (recordedLumi), '%.3f'% (recordedLumi)]
00731                 rowdata += [selectedlsStr, '%.3f'% (recordedLumi)]
00732         totalSelectedLS += len (selectedls)
00733         totalRecorded += recordedLumi
00734         datatable.append (rowdata)
00735 
00736     if hltpath != '' and hltpath != 'all':
00737         totaltable = [[str (totalDeliveredLS), '%.3f'% (totalDelivered), str (totalSelectedLS),
00738                        '%.3f'% (totalRecorded), '%.3f'% (totalRecordedInPath)]]
00739     else:
00740         totaltable = [[str (totalDeliveredLS), '%.3f'% (totalDelivered), str (totalSelectedLS),
00741                        '%.3f'% (totalRecorded)]]
00742     print tablePrinter.indent (toprowlabels+datatable, hasHeader = True, separateRows = False, prefix = '| ',
00743                                postfix = ' |', justify = 'right', delim = ' | ',
00744                                wrapfunc = lambda x: wrap_onspace (x, 20))
00745     print ' ==  =  Total : '
00746     print tablePrinter.indent (lastrowlabels+totaltable, hasHeader = True, separateRows = False, prefix = '| ',
00747                                postfix = ' |', justify = 'right', delim = ' | ',
00748                                wrapfunc = lambda x: wrap_onspace (x, 20))
00749 
00750 
00751 def dumpOverview (delivered, recorded, hltpath = ''):
00752     #toprowlabels = ['run', 'delivered', 'recorded', 'hltpath']
00753     datatable = []
00754     for runidx, deliveredrowdata in enumerate (delivered):
00755         rowdata = []
00756         rowdata += [deliveredrowdata[0], deliveredrowdata[2]]
00757         if deliveredrowdata[1] == 'N/A': #run does not exist
00758             rowdata += ['N/A', 'N/A']
00759             datatable.append (rowdata)
00760             continue
00761         recordedLumi = calculateTotalRecorded (recorded[runidx][2])
00762         lumiinPaths = calculateEffective (recorded[runidx][1], recordedLumi)
00763         if hltpath != '' and hltpath != 'all':
00764             if lumiinPaths.has_key (hltpath):
00765                 rowdata += [recordedLumi, lumiinPaths[hltpath]]
00766             else:
00767                 rowdata += [recordedLumi, 'N/A']
00768         else:
00769             rowdata += [recordedLumi, recordedLumi]
00770         datatable.append (rowdata)
00771     return datatable
00772 
00773 
00774 def xingLuminosityForRun (dbsession, runnum, parameters, lumiXingDict = {},
00775                           maxLumiSection = None, finecorrections=None):
00776     '''Given a run number and a minimum xing luminosity value,
00777     returns a dictionary (keyed by (run, lumi section)) where the
00778     value is a list of tuples of (xingID, xingLum).
00779 
00780     - For all xing luminosities, simply set minLumValue to 0.
00781 
00782     - If you want one dictionary for several runs, pass it in to
00783       "lumiXingDict"
00784 
00785     select 
00786     s.cmslsnum, d.bxlumivalue, d.bxlumierror, d.bxlumiquality, d.algoname from LUMIDETAIL d, LUMISUMMARY s where s.runnum = 133885 and d.algoname = 'OCC1' and s.lumisummary_id = d.lumisummary_id order by s.startorbit, s.cmslsnum
00787     '''
00788     try:
00789         runnum = int (runnum)
00790         dbsession.transaction().start (True)
00791         schema = dbsession.schema (parameters.lumischema)
00792         if not schema:
00793             raise 'cannot connect to schema ', parameters.lumischema
00794         detailOutput = coral.AttributeList()
00795         detailOutput.extend ('startorbit',    'unsigned int')
00796         detailOutput.extend ('cmslsnum',      'unsigned int')
00797         detailOutput.extend ('bxlumivalue',   'blob')
00798         detailOutput.extend ('bxlumierror',   'blob')
00799         detailOutput.extend ('bxlumiquality', 'blob')
00800         detailOutput.extend ('algoname',      'string')
00801         detailCondition = coral.AttributeList()
00802         detailCondition.extend ('runnum',   'unsigned int')
00803         detailCondition.extend ('algoname', 'string')
00804         detailCondition['runnum'].setData (runnum)
00805         detailCondition['algoname'].setData ('OCC1')
00806         query = schema.newQuery()
00807         query.addToTableList(nameDealer.lumisummaryTableName(), 's')
00808         query.addToTableList(nameDealer.lumidetailTableName(),  'd')
00809         query.addToOutputList ('s.STARTORBIT',    'startorbit')
00810         query.addToOutputList ('s.CMSLSNUM',      'cmslsnum')
00811         query.addToOutputList ('d.BXLUMIVALUE',   'bxlumivalue')
00812         query.addToOutputList ('d.BXLUMIERROR',   'bxlumierror')
00813         query.addToOutputList ('d.BXLUMIQUALITY', 'bxlumiquality')
00814         query.addToOutputList ('d.ALGONAME',      'algoname')
00815         query.setCondition ('s.RUNNUM =:runnum and d.ALGONAME =:algoname and s.LUMISUMMARY_ID=d.LUMISUMMARY_ID',detailCondition)
00816         query.addToOrderList ('s.CMSLSNUM')
00817         query.defineOutput (detailOutput)
00818         cursor = query.execute()
00819         count = 0
00820         while cursor.next():
00821             ## ## Note: If you are going to break out of this loop early,
00822             ## ## make sure you call cursor.close():
00823             ## 
00824             ## if count > 20 :
00825             ##     cursor.close()
00826             ##     break
00827             ## count  +=  1
00828             cmslsnum    = cursor.currentRow()['cmslsnum'].data()
00829             algoname    = cursor.currentRow()['algoname'].data()
00830             bxlumivalue = cursor.currentRow()['bxlumivalue'].data()
00831             startorbit  = cursor.currentRow()['startorbit'].data()
00832             
00833             if maxLumiSection and maxLumiSection < cmslsnum:
00834                 cursor.close()
00835                 break
00836             
00837             xingArray = array.array ('f')
00838             xingArray.fromstring( bxlumivalue.readline() )
00839             numPrinted = 0
00840             xingLum = []
00841             avginstlumi=0.0
00842             if len(xingArray)!=0:
00843                 avginstlumi=sum(xingArray)
00844             for index, lum in enumerate (xingArray):
00845                 mynorm=parameters.normFactor
00846                 if finecorrections:
00847                     lum=lumiCorrections.applyfinecorrectionBX(lum,avginstlumi*mynorm,mynorm,finecorrections[0],finecorrections[1],finecorrections[2])
00848                 else:
00849                     lum*=mynorm
00850                 if lum < parameters.xingMinLum:
00851                     continue
00852                 xingLum.append( (index, lum) )
00853             lumiXingDict[ (runnum, cmslsnum) ] = xingLum
00854         del query
00855         dbsession.transaction().commit()
00856         return lumiXingDict      
00857     except Exception, e:
00858         print str (e)
00859         print "whoops"
00860         dbsession.transaction().rollback()
00861         del dbsession
00862 
00863 
00864 def flatten (obj):
00865     '''Given nested lists or tuples, returns a single flattened list'''
00866     result = []
00867     for piece in obj:
00868         if hasattr (piece, '__iter__') and not isinstance (piece, basestring):
00869             result.extend( flatten (piece) )
00870         else:
00871             result.append (piece)
00872     return result    
00873 
00874 
00875 def mergeXingLumi (triplet, xingLumiDict):
00876     '''Given general xing information and a xingLumiDict, the xing
00877     luminosity information is merged with the general information'''
00878     runNumber = triplet[0]
00879     deadTable = triplet[2]
00880     for lumi, lumiList in deadTable.iteritems():
00881         key = ( int(runNumber), int(lumi) )
00882         xingLumiValues = xingLumiDict.get (key)
00883         if xingLumiValues:
00884             lumiList.append( flatten (xingLumiValues) )
00885 
00886 
00887 def setupSession (connectString, siteconfpath, parameters, debug = False):
00888     '''returns database session'''
00889     connectparser = connectstrParser.connectstrParser (connectString)
00890     connectparser.parse()
00891     usedefaultfrontierconfig = False
00892     cacheconfigpath = ''
00893     if connectparser.needsitelocalinfo():
00894         if not siteconfpath:
00895             cacheconfigpath = os.environ['CMS_PATH']
00896             if cacheconfigpath:
00897                 cacheconfigpath = os.path.join (cacheconfigpath, 'SITECONF', 'local', 'JobConfig', 'site-local-config.xml')
00898             else:
00899                 usedefaultfrontierconfig = True
00900         else:
00901             cacheconfigpath = siteconfpath
00902             cacheconfigpath = os.path.join (cacheconfigpath, 'site-local-config.xml')
00903         ccp = cacheconfigParser.cacheconfigParser()
00904         if usedefaultfrontierconfig:
00905             ccp.parseString (parameters.defaultfrontierConfigString)
00906         else:
00907             ccp.parse (cacheconfigpath)
00908         connectString = connectparser.fullfrontierStr (connectparser.schemaname(), ccp.parameterdict())
00909     svc = coral.ConnectionService()
00910     if debug :
00911         msg = coral.MessageStream ('')
00912         msg.setMsgVerbosity (coral.message_Level_Debug)
00913         parameters.verbose = True
00914     session = svc.connect (connectString, accessMode = coral.access_ReadOnly)
00915     session.typeConverter().setCppTypeForSqlType ("unsigned int", "NUMBER (10)")
00916     session.typeConverter().setCppTypeForSqlType ("unsigned long long", "NUMBER (20)")
00917     return session, svc
00918 
00919 
00920 
00921 ###==============real api=====###
00922 
00923 def allruns(schemaHandle,requireRunsummary=True,requireLumisummary=False,requireTrg=False,requireHlt=False):
00924     '''
00925     find all runs in the DB. By default requires cmsrunsummary table contain the run. The condition can be loosed in situation where db loading failed on certain data portions.
00926     '''
00927     if not requireRunsummary and not requireLumiummary and not requireTrg and not requireHlt:
00928         print 'must require at least one table'
00929         raise
00930     runresult=[]
00931     runlist=[]
00932     numdups=0
00933     if requireRunsummary:
00934         numdups=numdups+1
00935         queryHandle=schemaHandle.newQuery()
00936         queryHandle.addToTableList(nameDealer.cmsrunsummaryTableName())
00937         queryHandle.addToOutputList("RUNNUM","run")
00938         #queryBind=coral.AttributeList()
00939         result=coral.AttributeList()
00940         result.extend("run","unsigned int")
00941         queryHandle.defineOutput(result)
00942         cursor=queryHandle.execute()
00943         while cursor.next():
00944             r=cursor.currentRow()['run'].data()
00945             runlist.append(r)
00946         del queryHandle
00947     if requireLumisummary:
00948         numdups=numdups+1
00949         queryHandle=schemaHandle.newQuery()
00950         queryHandle.addToTableList(nameDealer.lumisummaryTableName())
00951         queryHandle.addToOutputList("distinct RUNNUM","run")
00952         #queryBind=coral.AttributeList()
00953         result=coral.AttributeList()
00954         result.extend("run","unsigned int")
00955         queryHandle.defineOutput(result)
00956         cursor=queryHandle.execute()
00957         while cursor.next():
00958             r=cursor.currentRow()['run'].data()
00959             runlist.append(r)
00960         del queryHandle
00961     if requireTrg:
00962         numdups=numdups+1
00963         queryHandle=schemaHandle.newQuery()
00964         queryHandle.addToTableList(nameDealer.trgTableName())
00965         queryHandle.addToOutputList("distinct RUNNUM","run")
00966         #queryBind=coral.AttributeList()
00967         result=coral.AttributeList()
00968         result.extend("run","unsigned int")
00969         queryHandle.defineOutput(result)
00970         cursor=queryHandle.execute()
00971         while cursor.next():
00972             r=cursor.currentRow()['run'].data()
00973             runlist.append(r)
00974         del queryHandle
00975     if requireHlt:
00976         numdups=numdups+1
00977         queryHandle=schemaHandle.newQuery()
00978         queryHandle.addToTableList(nameDealer.hltTableName())
00979         queryHandle.addToOutputList("distinct RUNNUM","run")
00980         #queryBind=coral.AttributeList()
00981         result=coral.AttributeList()
00982         result.extend("run","unsigned int")
00983         queryHandle.defineOutput(result)
00984         cursor=queryHandle.execute()
00985         while cursor.next():
00986             r=cursor.currentRow()['run'].data()
00987             runlist.append(r)
00988         del queryHandle
00989     dupresult=CommonUtil.count_dups(runlist)
00990     for dup in dupresult:
00991         if dup[1]==numdups:
00992             runresult.append(dup[0])
00993     runresult.sort()
00994     return runresult
00995 
00996 def validation(queryHandle,run=None,cmsls=None):
00997     '''retrieve validation data per run or all
00998     input: run. if not run, retrive all; if cmslsnum selection list pesent, filter out unselected result
00999     output: {run:[[cmslsnum,status,comment]]}
01000     '''
01001     result={}
01002     queryHandle.addToTableList(nameDealer.lumivalidationTableName())
01003     queryHandle.addToOutputList('RUNNUM','runnum')
01004     queryHandle.addToOutputList('CMSLSNUM','cmslsnum')
01005     queryHandle.addToOutputList('FLAG','flag')
01006     queryHandle.addToOutputList('COMMENT','comment')
01007     if run:
01008         queryCondition='RUNNUM=:runnum'
01009         queryBind=coral.AttributeList()
01010         queryBind.extend('runnum','unsigned int')
01011         queryBind['runnum'].setData(run)
01012         queryHandle.setCondition(queryCondition,queryBind)
01013     queryResult=coral.AttributeList()
01014     queryResult.extend('runnum','unsigned int')
01015     queryResult.extend('cmslsnum','unsigned int')
01016     queryResult.extend('flag','string')
01017     queryResult.extend('comment','string')
01018     queryHandle.defineOutput(queryResult)
01019     cursor=queryHandle.execute()
01020     while cursor.next():
01021         runnum=cursor.currentRow()['runnum'].data()
01022         if not result.has_key(runnum):
01023             result[runnum]=[]
01024         cmslsnum=cursor.currentRow()['cmslsnum'].data()
01025         flag=cursor.currentRow()['flag'].data()
01026         comment=cursor.currentRow()['comment'].data()
01027         result[runnum].append([cmslsnum,flag,comment])
01028     if run and cmsls and len(cmsls)!=0:
01029         selectedresult={}
01030         for runnum,perrundata in result.items():
01031             for lsdata in perrundata:
01032                 if lsdata[0] not in cmsls:
01033                     continue
01034                 if not selectedresult.has_key(runnum):
01035                     selectedresult[runnum]=[]
01036                 selectedresult[runnum].append(lsdata)
01037         return selectedresult
01038     else:
01039         return result
01040     
01041 def allfills(queryHandle,filtercrazy=True):
01042     '''select distinct fillnum from cmsrunsummary
01043     there are crazy fill numbers. we assume they are not valid runs
01044     '''
01045     result=[]
01046     queryHandle.addToTableList(nameDealer.cmsrunsummaryTableName())
01047     queryHandle.addToOutputList('distinct FILLNUM','fillnum')
01048     
01049     if filtercrazy:
01050         queryCondition='FILLNUM>:zero and FILLNUM<:crazybig'
01051         queryBind=coral.AttributeList()
01052         queryBind.extend('zero','unsigned int')
01053         queryBind.extend('crazybig','unsigned int')
01054         queryBind['zero'].setData(int(0))
01055         queryBind['crazybig'].setData(int(29701))
01056         queryHandle.setCondition(queryCondition,queryBind)
01057     queryResult=coral.AttributeList()
01058     queryResult.extend('fillnum','unsigned int')
01059     queryHandle.defineOutput(queryResult)
01060     cursor=queryHandle.execute()
01061     while cursor.next():
01062         result.append(cursor.currentRow()['fillnum'].data())
01063     result.sort()
01064     return result
01065 def runsummaryByrun(queryHandle,runnum):
01066     '''
01067     select fillnum,sequence,hltkey,to_char(starttime),to_char(stoptime) from cmsrunsummary where runnum=:runnum
01068     output: [fillnum,sequence,hltkey,starttime,stoptime]
01069     '''
01070     t=lumiTime.lumiTime()
01071     result=[]
01072     queryHandle.addToTableList(nameDealer.cmsrunsummaryTableName())
01073     queryCondition=coral.AttributeList()
01074     queryCondition.extend('runnum','unsigned int')
01075     queryCondition['runnum'].setData(int(runnum))
01076     queryHandle.addToOutputList('FILLNUM','fillnum')
01077     queryHandle.addToOutputList('SEQUENCE','sequence')
01078     queryHandle.addToOutputList('HLTKEY','hltkey')
01079     queryHandle.addToOutputList('to_char(STARTTIME,\''+t.coraltimefm+'\')','starttime')
01080     queryHandle.addToOutputList('to_char(STOPTIME,\''+t.coraltimefm+'\')','stoptime')
01081     queryHandle.setCondition('RUNNUM=:runnum',queryCondition)
01082     queryResult=coral.AttributeList()
01083     queryResult.extend('fillnum','unsigned int')
01084     queryResult.extend('sequence','string')
01085     queryResult.extend('hltkey','string')
01086     queryResult.extend('starttime','string')
01087     queryResult.extend('stoptime','string')
01088     queryHandle.defineOutput(queryResult)
01089     cursor=queryHandle.execute()
01090     while cursor.next():
01091         result.append(cursor.currentRow()['fillnum'].data())
01092         result.append(cursor.currentRow()['sequence'].data())
01093         result.append(cursor.currentRow()['hltkey'].data())
01094         result.append(cursor.currentRow()['starttime'].data())
01095         result.append(cursor.currentRow()['stoptime'].data())
01096     #if len(result)!=5:
01097     #    print 'wrong runsummary result'
01098     #    raise
01099     return result
01100 
01101 def lumisummaryByrun(queryHandle,runnum,lumiversion,beamstatus=None,beamenergy=None,beamenergyfluctuation=0.09,finecorrections=None):
01102     '''
01103     one can impose beamstatus, beamenergy selections at the SQL query level or process them later from the general result
01104     select cmslsnum,instlumi,numorbit,startorbit,beamstatus,beamenery from lumisummary where runnum=:runnum and lumiversion=:lumiversion order by startorbit;
01105     output: [[cmslsnum,instlumi,numorbit,startorbit,beamstatus,beamenergy,cmsalive]]
01106     Note: the non-cmsalive LS are included in the result
01107     '''
01108     result=[]
01109     queryHandle.addToTableList(nameDealer.lumisummaryTableName())
01110     queryCondition=coral.AttributeList()
01111     queryCondition.extend('runnum','unsigned int')
01112     queryCondition.extend('lumiversion','string')
01113     conditionstring='RUNNUM=:runnum and LUMIVERSION=:lumiversion'
01114     queryCondition['runnum'].setData(int(runnum))
01115     queryCondition['lumiversion'].setData(lumiversion)
01116     queryHandle.addToOutputList('CMSLSNUM','cmslsnum')
01117     queryHandle.addToOutputList('INSTLUMI','instlumi')
01118     queryHandle.addToOutputList('NUMORBIT','numorbit')
01119     queryHandle.addToOutputList('STARTORBIT','startorbit')
01120     queryHandle.addToOutputList('BEAMSTATUS','beamstatus')
01121     queryHandle.addToOutputList('BEAMENERGY','beamenergy')
01122     queryHandle.addToOutputList('CMSALIVE','cmsalive')
01123     if beamstatus and len(beamstatus)!=0:
01124         conditionstring=conditionstring+' and BEAMSTATUS=:beamstatus'
01125         queryCondition.extend('beamstatus','string')
01126         queryCondition['beamstatus'].setData(beamstatus)
01127     if beamenergy:
01128         minBeamenergy=float(beamenergy*(1.0-beamenergyfluctuation))
01129         maxBeamenergy=float(beamenergy*(1.0+beamenergyfluctuation))
01130         conditionstring=conditionstring+' and BEAMENERGY>:minBeamenergy and BEAMENERGY<:maxBeamenergy'
01131         queryCondition.extend('minBeamenergy','float')
01132         queryCondition.extend('maxBeamenergy','float')
01133         queryCondition['minBeamenergy'].setData(float(minBeamenergy))
01134         queryCondition['maxBeamenergy'].setData(float(maxBeamenergy))
01135     queryResult=coral.AttributeList()
01136     queryResult.extend('cmslsnum','unsigned int')
01137     queryResult.extend('instlumi','float')
01138     queryResult.extend('numorbit','unsigned int')
01139     queryResult.extend('startorbit','unsigned int')
01140     queryResult.extend('beamstatus','string')
01141     queryResult.extend('beamenergy','float')
01142     queryResult.extend('cmsalive','unsigned int')
01143     queryHandle.defineOutput(queryResult)
01144     queryHandle.setCondition(conditionstring,queryCondition)
01145     queryHandle.addToOrderList('startorbit')
01146     cursor=queryHandle.execute()
01147     while cursor.next():
01148         cmslsnum=cursor.currentRow()['cmslsnum'].data()
01149         instlumi=cursor.currentRow()['instlumi'].data()
01150         if finecorrections:
01151             instlumi=lumiCorrections.applyfinecorrection(instlumi,finecorrections[0],finecorrections[1],finecorrections[2])
01152         numorbit=cursor.currentRow()['numorbit'].data()
01153         startorbit=cursor.currentRow()['startorbit'].data()
01154         beamstatus=cursor.currentRow()['beamstatus'].data()
01155         beamenergy=cursor.currentRow()['beamenergy'].data()
01156         cmsalive=cursor.currentRow()['cmsalive'].data()
01157         result.append([cmslsnum,instlumi,numorbit,startorbit,beamstatus,beamenergy,cmsalive])
01158     return result
01159 
01160 def lumisumByrun(queryHandle,runnum,lumiversion,beamstatus=None,beamenergy=None,beamenergyfluctuation=0.09,finecorrections=None):
01161     '''
01162     beamenergy unit : GeV
01163     beamenergyfluctuation : fraction allowed to fluctuate around beamenergy value
01164     select instlumi from lumisummary where runnum=:runnum and lumiversion=:lumiversion
01165     output: float sum(instlumi)
01166     Note: the output is the raw result, need to apply LS length in time(sec)
01167     '''
01168     result=[]
01169     queryHandle.addToTableList(nameDealer.lumisummaryTableName())
01170     queryCondition=coral.AttributeList()
01171     queryCondition.extend('runnum','unsigned int')
01172     queryCondition.extend('lumiversion','string')
01173     
01174     queryCondition['runnum'].setData(int(runnum))
01175     queryCondition['lumiversion'].setData(lumiversion)
01176     queryHandle.addToOutputList('INSTLUMI','instlumi')
01177     conditionstring='RUNNUM=:runnum and LUMIVERSION=:lumiversion'
01178     if beamstatus and len(beamstatus)!=0:
01179         conditionstring=conditionstring+' and BEAMSTATUS=:beamstatus'
01180         queryCondition.extend('beamstatus','string')
01181         queryCondition['beamstatus'].setData(beamstatus)
01182     if beamenergy and beamenergy!=0.0:
01183         minBeamenergy=float(beamenergy*(1.0-beamenergyfluctuation))
01184         maxBeamenergy=float(beamenergy*(1.0+beamenergyfluctuation))
01185         conditionstring=conditionstring+' and BEAMENERGY>:minBeamenergy and BEAMENERGY<:maxBeamenergy'
01186         queryCondition.extend('minBeamenergy','float')
01187         queryCondition.extend('maxBeamenergy','float')
01188         queryCondition['minBeamenergy'].setData(float(minBeamenergy))
01189         queryCondition['maxBeamenergy'].setData(float(maxBeamenergy))
01190     queryHandle.setCondition(conditionstring,queryCondition)
01191     queryResult=coral.AttributeList()
01192     queryResult.extend('instlumi','float')
01193     queryHandle.defineOutput(queryResult)
01194     cursor=queryHandle.execute()
01195     while cursor.next():
01196         instlumi=cursor.currentRow()['instlumi'].data()
01197         if instlumi:
01198             if finecorrections:
01199                 instlumi=lumiCorrections.applyfinecorrection(instlumi,finecorrections[0],finecorrections[1],finecorrections[2])
01200             result.append(instlumi)
01201     if result:
01202         return sum(result)
01203     else:
01204         return 0.0
01205 
01206 def trgbitzeroByrun(queryHandle,runnum):
01207     '''
01208     select cmslsnum,trgcount,deadtime,bitname,prescale from trg where runnum=:runnum and bitnum=0;
01209     output: {cmslsnum:[trgcount,deadtime,bitname,prescale]}
01210     '''
01211     result={}
01212     queryHandle.addToTableList(nameDealer.trgTableName())
01213     queryCondition=coral.AttributeList()
01214     queryCondition.extend('runnum','unsigned int')
01215     queryCondition.extend('bitnum','unsigned int')
01216     queryCondition['runnum'].setData(int(runnum))
01217     queryCondition['bitnum'].setData(int(0))
01218     queryHandle.addToOutputList('CMSLSNUM','cmslsnum')
01219     queryHandle.addToOutputList('TRGCOUNT','trgcount')
01220     queryHandle.addToOutputList('DEADTIME','deadtime')
01221     queryHandle.addToOutputList('BITNAME','bitname')
01222     queryHandle.addToOutputList('PRESCALE','prescale')
01223     queryHandle.setCondition('RUNNUM=:runnum and BITNUM=:bitnum',queryCondition)
01224     queryResult=coral.AttributeList()
01225     queryResult.extend('cmslsnum','unsigned int')
01226     queryResult.extend('trgcount','unsigned int')
01227     queryResult.extend('deadtime','unsigned int')
01228     queryResult.extend('bitname','string')
01229     queryResult.extend('prescale','unsigned int')
01230     queryHandle.defineOutput(queryResult)
01231     cursor=queryHandle.execute()
01232     while cursor.next():
01233         cmslsnum=cursor.currentRow()['cmslsnum'].data()
01234         trgcount=cursor.currentRow()['trgcount'].data()
01235         deadtime=cursor.currentRow()['deadtime'].data()
01236         bitname=cursor.currentRow()['bitname'].data()
01237         prescale=cursor.currentRow()['prescale'].data()
01238         if not result.has_key(cmslsnum):
01239             result[cmslsnum]=[trgcount,deadtime,bitname,prescale]
01240     return result
01241 
01242 def lumisummarytrgbitzeroByrun(queryHandle,runnum,lumiversion,beamstatus=None,beamenergy=None,beamenergyfluctuation=0.09,finecorrections=None):
01243     '''
01244     select l.cmslsnum,l.instlumi,l.numorbit,l.startorbit,l.beamstatus,l.beamenery,t.trgcount,t.deadtime,t.bitname,t.prescale from trg t,lumisummary l where t.bitnum=:bitnum and l.runnum=:runnum and l.lumiversion=:lumiversion and l.runnum=t.runnum and t.cmslsnum=l.cmslsnum; 
01245     Everything you ever need to know about bitzero and avg luminosity. Since we do not know if joint query is better of sperate, support both.
01246     output: {cmslsnum:[instlumi,numorbit,startorbit,beamstatus,beamenergy,bitzerocount,deadtime,bitname,prescale]}
01247     Note: only cmsalive LS are included in the result. Therefore, this function cannot be used for calculating delivered!
01248     '''
01249     result={}
01250     queryHandle.addToTableList(nameDealer.trgTableName(),'t')
01251     queryHandle.addToTableList(nameDealer.lumisummaryTableName(),'l')
01252     queryCondition=coral.AttributeList()
01253     queryCondition.extend('bitnum','unsigned int')
01254     queryCondition.extend('runnum','unsigned int')
01255     queryCondition.extend('lumiversion','string')
01256     queryCondition['bitnum'].setData(int(0))        
01257     queryCondition['runnum'].setData(int(runnum))
01258     queryCondition['lumiversion'].setData(lumiversion)
01259     
01260     queryHandle.addToOutputList('l.CMSLSNUM','cmslsnum')
01261     queryHandle.addToOutputList('l.INSTLUMI','instlumi')
01262     queryHandle.addToOutputList('l.NUMORBIT','numorbit')
01263     queryHandle.addToOutputList('l.STARTORBIT','startorbit')
01264     queryHandle.addToOutputList('l.BEAMSTATUS','beamstatus')
01265     queryHandle.addToOutputList('l.BEAMENERGY','beamenergy')
01266     queryHandle.addToOutputList('t.TRGCOUNT','trgcount')
01267     queryHandle.addToOutputList('t.DEADTIME','deadtime')
01268     queryHandle.addToOutputList('t.BITNAME','bitname')
01269     queryHandle.addToOutputList('t.PRESCALE','prescale')
01270     conditionstring='t.BITNUM=:bitnum and l.RUNNUM=:runnum and l.LUMIVERSION=:lumiversion and l.RUNNUM=t.RUNNUM and t.CMSLSNUM=l.CMSLSNUM'
01271     if beamstatus and len(beamstatus)!=0:
01272         conditionstring=conditionstring+' and l.BEAMSTATUS=:beamstatus'
01273         queryCondition.extend('beamstatus','string')
01274         queryCondition['beamstatus'].setData(beamstatus)
01275     if beamenergy and beamenergy!=0.0:
01276         minBeamenergy=float(beamenergy*(1-beamenergyfluctuation))
01277         maxBeamenergy=float(beamenergy*(1+beamenergyfluctuation))
01278         conditionstring=conditionstring+' and l.BEAMENERGY>:minBeamenergy and l.BEAMENERGY<:maxBeamenergy'
01279         queryCondition.extend('minBeamenergy','float')
01280         queryCondition.extend('maxBeamenergy','float')
01281         queryCondition['minBeamenergy'].setData(float(minBeamenergy))
01282         queryCondition['maxBeamenergy'].setData(float(maxBeamenergy))
01283     queryHandle.setCondition(conditionstring,queryCondition)
01284     queryResult=coral.AttributeList()
01285     queryResult.extend('cmslsnum','unsigned int')
01286     queryResult.extend('instlumi','float')
01287     queryResult.extend('numorbit','unsigned int')
01288     queryResult.extend('startorbit','unsigned int')
01289     queryResult.extend('beamstatus','string')
01290     queryResult.extend('beamenergy','float')  
01291     queryResult.extend('trgcount','unsigned int')
01292     queryResult.extend('deadtime','unsigned int')
01293     queryResult.extend('bitname','string')
01294     queryResult.extend('prescale','unsigned int')
01295     queryHandle.defineOutput(queryResult)
01296     cursor=queryHandle.execute()
01297     while cursor.next():
01298         cmslsnum=cursor.currentRow()['cmslsnum'].data()
01299         instlumi=cursor.currentRow()['instlumi'].data()
01300         if finecorrections:
01301             instlumi=lumiCorrections.applyfinecorrection(instlumi,finecorrections[0],finecorrections[1],finecorrections[2])
01302         numorbit=cursor.currentRow()['numorbit'].data()
01303         startorbit=cursor.currentRow()['startorbit'].data()
01304         beamstatus=cursor.currentRow()['beamstatus'].data()
01305         beamenergy=cursor.currentRow()['beamenergy'].data()
01306         trgcount=cursor.currentRow()['trgcount'].data()
01307         deadtime=cursor.currentRow()['deadtime'].data()
01308         bitname=cursor.currentRow()['bitname'].data()
01309         prescale=cursor.currentRow()['prescale'].data()
01310         if not result.has_key(cmslsnum):
01311             result[cmslsnum]=[instlumi,numorbit,startorbit,beamstatus,beamenergy,trgcount,deadtime,bitname,prescale]
01312     return result
01313 
01314 def trgBybitnameByrun(queryHandle,runnum,bitname):
01315     '''
01316     select cmslsnum,trgcount,deadtime,bitnum,prescale from trg where runnum=:runnum and bitname=:bitname;
01317     output: {cmslsnum:[trgcount,deadtime,bitnum,prescale]}
01318     '''
01319     result={}
01320     queryHandle.addToTableList(nameDealer.trgTableName())
01321     queryCondition=coral.AttributeList()
01322     queryCondition.extend('runnum','unsigned int')
01323     queryCondition.extend('bitname','string')
01324     queryCondition['runnum'].setData(int(runnum))
01325     queryCondition['bitname'].setData(bitname)        
01326     queryHandle.addToOutputList('CMSLSNUM','cmslsnum')
01327     queryHandle.addToOutputList('TRGCOUNT','trgcount')
01328     queryHandle.addToOutputList('DEADTIME','deadtime')
01329     queryHandle.addToOutputList('BITNUM','bitnum')
01330     queryHandle.addToOutputList('PRESCALE','prescale')
01331     queryHandle.setCondition('RUNNUM=:runnum and BITNAME=:bitname',queryCondition)
01332     queryResult=coral.AttributeList()
01333     queryResult.extend('cmslsnum','unsigned int')
01334     queryResult.extend('trgcount','unsigned int')
01335     queryResult.extend('deadtime','unsigned long long')
01336     queryResult.extend('bitnum','unsigned int')
01337     queryResult.extend('prescale','unsigned int')
01338     queryHandle.defineOutput(queryResult)
01339     cursor=queryHandle.execute()
01340     while cursor.next():
01341         cmslsnum=cursor.currentRow()['cmslsnum'].data()
01342         trgcount=cursor.currentRow()['trgcount'].data()
01343         deadtime=cursor.currentRow()['deadtime'].data()
01344         bitnum=cursor.currentRow()['bitnum'].data()
01345         prescale=cursor.currentRow()['prescale'].data()
01346         if not result.has_key(cmslsnum):
01347             result[cmslsnum]=[trgcount,deadtime,bitnum,prescale]
01348     return result
01349 
01350 def trgAllbitsByrun(queryHandle,runnum):
01351     '''
01352     all you ever want to know about trigger
01353     select cmslsnum,trgcount,deadtime,bitnum,bitname,prescale from trg where runnum=:runnum order by  bitnum,cmslsnum
01354     this can be changed to blob query later
01355     output: {cmslsnum:{bitname:[bitnum,trgcount,deadtime,prescale]}}
01356     '''
01357     result={}
01358     queryHandle.addToTableList(nameDealer.trgTableName())
01359     queryCondition=coral.AttributeList()
01360     queryCondition.extend('runnum','unsigned int')
01361     queryCondition['runnum'].setData(int(runnum))
01362     queryHandle.addToOutputList('cmslsnum')
01363     queryHandle.addToOutputList('trgcount')
01364     queryHandle.addToOutputList('deadtime')
01365     queryHandle.addToOutputList('bitnum')
01366     queryHandle.addToOutputList('bitname')
01367     queryHandle.addToOutputList('prescale')
01368     queryHandle.setCondition('runnum=:runnum',queryCondition)
01369     queryResult=coral.AttributeList()
01370     queryResult.extend('cmslsnum','unsigned int')
01371     queryResult.extend('trgcount','unsigned int')
01372     queryResult.extend('deadtime','unsigned long long')
01373     queryResult.extend('bitnum','unsigned int')
01374     queryResult.extend('bitname','string')
01375     queryResult.extend('prescale','unsigned int')
01376     queryHandle.defineOutput(queryResult)
01377     queryHandle.addToOrderList('bitnum')
01378     queryHandle.addToOrderList('cmslsnum')
01379     cursor=queryHandle.execute()
01380     while cursor.next():
01381         cmslsnum=cursor.currentRow()['cmslsnum'].data()
01382         trgcount=cursor.currentRow()['trgcount'].data()
01383         deadtime=cursor.currentRow()['deadtime'].data()
01384         bitnum=cursor.currentRow()['bitnum'].data()
01385         bitname=cursor.currentRow()['bitname'].data()
01386         prescale=cursor.currentRow()['prescale'].data()
01387         if not result.has_key(cmslsnum):
01388             dataperLS={}
01389             dataperLS[bitname]=[bitnum,trgcount,deadtime,prescale]
01390             result[cmslsnum]=dataperLS
01391         else:
01392             result[cmslsnum][bitname]=[bitnum,trgcount,deadtime,prescale]
01393     return result
01394 
01395 
01396 def hltBypathByrun(queryHandle,runnum,hltpath):
01397     '''
01398     select cmslsnum,inputcount,acceptcount,prescale from hlt where runnum=:runnum and pathname=:pathname
01399     output: {cmslsnum:[inputcount,acceptcount,prescale]}
01400     '''
01401     result={}
01402     queryHandle.addToTableList(nameDealer.hltTableName())
01403     queryCondition=coral.AttributeList()
01404     queryCondition.extend('runnum','unsigned int')
01405     queryCondition.extend('pathname','string')
01406     queryCondition['runnum'].setData(int(runnum))
01407     queryCondition['pathname'].setData(hltpath)
01408     queryHandle.addToOutputList('CMSLSNUM','cmslsnum')
01409     queryHandle.addToOutputList('INPUTCOUNT','inputcount')
01410     queryHandle.addToOutputList('ACCEPTCOUNT','acceptcount')
01411     queryHandle.addToOutputList('PRESCALE','prescale')
01412     queryHandle.setCondition('RUNNUM=:runnum and PATHNAME=:pathname',queryCondition)
01413     queryResult=coral.AttributeList()
01414     queryResult.extend('cmslsnum','unsigned int')
01415     queryResult.extend('inputcount','unsigned int')
01416     queryResult.extend('acceptcount','unsigned int')
01417     queryResult.extend('prescale','unsigned int')
01418     queryHandle.defineOutput(queryResult)
01419     cursor=queryHandle.execute()
01420     while cursor.next():
01421         cmslsnum=cursor.currentRow()['cmslsnum'].data()
01422         inputcount=cursor.currentRow()['inputcount'].data()
01423         acceptcount=cursor.currentRow()['acceptcount'].data()
01424         prescale=cursor.currentRow()['prescale'].data()
01425         if not result.has_key(cmslsnum):
01426             result[cmslsnum]=[inputcount,acceptcount,prescale]
01427     return result
01428 
01429 def hltAllpathByrun(queryHandle,runnum):
01430     '''
01431     select cmslsnum,inputcount,acceptcount,prescale,pathname from hlt where runnum=:runnum
01432     this can be changed to blob query later
01433     output: {cmslsnum:{pathname:[inputcount,acceptcount,prescale]}}
01434     '''
01435     result={}
01436     queryHandle.addToTableList(nameDealer.hltTableName())
01437     queryCondition=coral.AttributeList()
01438     queryCondition.extend('runnum','unsigned int')
01439     queryCondition['runnum'].setData(int(runnum))
01440     queryHandle.addToOutputList('CMSLSNUM','cmslsnum')
01441     queryHandle.addToOutputList('INPUTCOUNT','inputcount')
01442     queryHandle.addToOutputList('ACCEPTCOUNT','acceptcount')
01443     queryHandle.addToOutputList('PRESCALE','prescale')
01444     queryHandle.addToOutputList('PATHNAME','pathname')
01445     queryHandle.setCondition('RUNNUM=:runnum',queryCondition)
01446     queryResult=coral.AttributeList()
01447     queryResult.extend('cmslsnum','unsigned int')
01448     queryResult.extend('inputcount','unsigned int')
01449     queryResult.extend('acceptcount','unsigned int')
01450     queryResult.extend('prescale','unsigned int')
01451     queryResult.extend('pathname','string')
01452     queryHandle.defineOutput(queryResult)
01453     cursor=queryHandle.execute()
01454     while cursor.next():
01455         cmslsnum=cursor.currentRow()['cmslsnum'].data()
01456         inputcount=cursor.currentRow()['inputcount'].data()
01457         acceptcount=cursor.currentRow()['acceptcount'].data()
01458         prescale=cursor.currentRow()['prescale'].data()
01459         pathname=cursor.currentRow()['pathname'].data()
01460         if not result.has_key(cmslsnum):
01461             dataperLS={}
01462             dataperLS[pathname]=[inputcount,acceptcount,prescale]
01463             result[cmslsnum]=dataperLS
01464         else:
01465             result[cmslsnum][pathname]=[inputcount,acceptcount,prescale]
01466     return result
01467 
01468 
01469 def beamIntensityForRun(query,parameters,runnum):
01470     '''
01471     select CMSBXINDEXBLOB,BEAMINTENSITYBLOB_1,BEAMINTENSITYBLOB_2 from LUMISUMMARY where runnum=146315 and LUMIVERSION='0001'
01472     
01473     output : result {startorbit: [(bxidx,beam1intensity,beam2intensity)]}
01474     '''
01475     result={} #{startorbit:[(bxidx,occlumi,occlumierr,beam1intensity,beam2intensity)]}
01476     
01477     lumisummaryOutput=coral.AttributeList()
01478     lumisummaryOutput.extend('cmslsnum','unsigned int')
01479     lumisummaryOutput.extend('startorbit','unsigned int')
01480     lumisummaryOutput.extend('bxindexblob','blob');
01481     lumisummaryOutput.extend('beamintensityblob1','blob');
01482     lumisummaryOutput.extend('beamintensityblob2','blob');
01483     condition=coral.AttributeList()
01484     condition.extend('runnum','unsigned int')
01485     condition.extend('lumiversion','string')
01486     condition['runnum'].setData(int(runnum))
01487     condition['lumiversion'].setData(parameters.lumiversion)
01488     
01489     query.addToTableList(parameters.lumisummaryname)
01490     query.addToOutputList('CMSLSNUM','cmslsnum')
01491     query.addToOutputList('STARTORBIT','startorbit')
01492     query.addToOutputList('CMSBXINDEXBLOB','bxindexblob')
01493     query.addToOutputList('BEAMINTENSITYBLOB_1','beamintensityblob1')
01494     query.addToOutputList('BEAMINTENSITYBLOB_2','beamintensityblob2')
01495     query.setCondition('RUNNUM=:runnum AND LUMIVERSION=:lumiversion',condition)
01496     query.defineOutput(lumisummaryOutput)
01497     cursor=query.execute()
01498     while cursor.next():
01499         #cmslsnum=cursor.currentRow()['cmslsnum'].data()
01500         startorbit=cursor.currentRow()['startorbit'].data()
01501         bxidx=[]
01502         bb1=[]
01503         bb2=[]
01504         bxindexblob=None
01505         beamintensityblob1=None
01506         beamintensityblob2=None
01507         if not cursor.currentRow()["bxindexblob"].isNull():
01508             bxindexblob=cursor.currentRow()['bxindexblob'].data()
01509             if bxindexblob and bxindexblob.readline()!=None:
01510                 bxidx=CommonUtil.unpackBlobtoArray(bxindexblob,'h')
01511                 if not cursor.currentRow()['beamintensityblob1'].isNull():
01512                     beamintensityblob1=cursor.currentRow()['beamintensityblob1'].data()
01513                     if beamintensityblob1 and beamintensityblob1.readline()!=None:
01514                         bb1=CommonUtil.unpackBlobtoArray(beamintensityblob1,'f')
01515                 if not cursor.currentRow()['beamintensityblob2'].isNull():
01516                     beamintensityblob2=cursor.currentRow()['beamintensityblob2'].data()
01517                     if beamintensityblob2 and beamintensityblob2.readline()!=None:
01518                         bb2=CommonUtil.unpackBlobtoArray(beamintensityblob2,'f')
01519         if not result.has_key(startorbit):
01520             result[startorbit]=[]
01521         for idx,bxidxvalue in enumerate(bxidx):
01522             try:
01523                 b1intensity=bb1[idx]
01524             except IndexError:
01525                 b1intensity=0.0
01526             try:
01527                 b2intensity=bb2[idx]
01528             except IndexError:
01529                 b2intensity=0.0
01530             result[startorbit].append((bxidxvalue,b1intensity,b2intensity))
01531         del bxidx[:]
01532         del bb1[:]
01533         del bb2[:]
01534     return result
01535     
01536 def calibratedDetailForRunLimitresult(query,parameters,runnum,algoname='OCC1',finecorrection=None):
01537     '''select 
01538     s.cmslsnum,d.bxlumivalue,d.bxlumierror,d.bxlumiquality,d.algoname from LUMIDETAIL d,LUMISUMMARY s where s.runnum=133885 and d.algoname='OCC1' and s.lumisummary_id=d.lumisummary_id order by s.startorbit,s.cmslsnum
01539     result={(startorbit,cmslsnum):[(index,lumivalue,lumierr),]}
01540     '''
01541     result={}
01542     detailOutput=coral.AttributeList()
01543     detailOutput.extend('cmslsnum','unsigned int')
01544     detailOutput.extend('startorbit','unsigned int')
01545     detailOutput.extend('bxlumivalue','blob')
01546     detailOutput.extend('bxlumierror','blob')
01547     detailCondition=coral.AttributeList()
01548     detailCondition.extend('runnum','unsigned int')
01549     detailCondition.extend('algoname','string')
01550     detailCondition['runnum'].setData(runnum)
01551     detailCondition['algoname'].setData(algoname)
01552 
01553     query.addToTableList(parameters.lumisummaryname,'s')
01554     query.addToTableList(parameters.lumidetailname,'d')
01555     query.addToOutputList('s.CMSLSNUM','cmslsnum')
01556     query.addToOutputList('s.STARTORBIT','startorbit')
01557     query.addToOutputList('d.BXLUMIVALUE','bxlumivalue')
01558     query.addToOutputList('d.BXLUMIERROR','bxlumierror')
01559     query.addToOutputList('d.BXLUMIQUALITY','bxlumiquality')
01560     query.setCondition('s.RUNNUM=:runnum and d.ALGONAME=:algoname and s.LUMISUMMARY_ID=d.LUMISUMMARY_ID',detailCondition)
01561     query.defineOutput(detailOutput)
01562     cursor=query.execute()
01563     while cursor.next():
01564         cmslsnum=cursor.currentRow()['cmslsnum'].data()
01565         bxlumivalue=cursor.currentRow()['bxlumivalue'].data()
01566         bxlumierror=cursor.currentRow()['bxlumierror'].data()
01567         startorbit=cursor.currentRow()['startorbit'].data()
01568         bxlumivalueArray=array.array('f')
01569         bxlumivalueArray.fromstring(bxlumivalue.readline())
01570         bxlumierrorArray=array.array('f')
01571         bxlumierrorArray.fromstring(bxlumierror.readline())
01572         xingLum=[]
01573         #apply selection criteria
01574         maxlumi=0.0
01575         if len(bxlumivalueArray)!=0:
01576             maxlumi=max(bxlumivalueArray)
01577         avginstlumi=0.0
01578         if len(bxlumivalueArray)!=0:
01579             avginstlumi=sum(bxlumivalueArray)
01580         for index,lum in enumerate(bxlumivalueArray):
01581             lumierror = bxlumierrorArray[index]
01582             if lum<max(parameters.xingMinLum,maxlumi*0.2): 
01583                 continue
01584             mynorm=parameters.normFactor
01585             if finecorrection:
01586                 lum=lumiCorrections.applyfinecorrectionBX(lum,avginstlumi*mynorm,mynorm,finecorrection[0],finecorrection[1],finecorrection[2])
01587                 lumierror=lumiCorrections.applyfinecorrectionBX(lumierror,avginstlumi*mynorm,mynorm,finecorrection[0],finecorrection[1],finecorrection[2])
01588             else:
01589                 lum*=mynorm
01590                 lumierror*=mynorm
01591             xingLum.append( (index,lum,lumierror) )
01592         if len(xingLum)!=0:
01593             result[(startorbit,cmslsnum)]=xingLum
01594     return result
01595    
01596 def lumidetailByrunByAlgo(queryHandle,runnum,algoname='OCC1'):
01597     '''
01598     select s.cmslsnum,d.bxlumivalue,d.bxlumierror,d.bxlumiquality,s.startorbit from LUMIDETAIL d,LUMISUMMARY s where s.runnum=:runnum and d.algoname=:algoname and s.lumisummary_id=d.lumisummary_id order by s.startorbit
01599     output: [[cmslsnum,bxlumivalue,bxlumierror,bxlumiquality,startorbit]]
01600     since the output is ordered by time, it has to be in seq list format
01601     '''
01602     result=[]
01603     queryHandle.addToTableList(nameDealer.lumidetailTableName(),'d')
01604     queryHandle.addToTableList(nameDealer.lumisummaryTableName(),'s')
01605     queryCondition=coral.AttributeList()
01606     queryCondition.extend('runnum','unsigned int')
01607     queryCondition.extend('algoname','string')
01608     queryCondition['runnum'].setData(int(runnum))
01609     queryCondition['algoname'].setData(algoname)
01610     queryHandle.addToOutputList('s.CMSLSNUM','cmslsnum')
01611     queryHandle.addToOutputList('d.BXLUMIVALUE','bxlumivalue')
01612     queryHandle.addToOutputList('d.BXLUMIERROR','bxlumierror')
01613     queryHandle.addToOutputList('d.BXLUMIQUALITY','bxlumiquality')
01614     queryHandle.addToOutputList('s.STARTORBIT','startorbit')
01615     queryHandle.setCondition('s.runnum=:runnum and d.algoname=:algoname and s.lumisummary_id=d.lumisummary_id',queryCondition)
01616     queryResult=coral.AttributeList()
01617     queryResult.extend('cmslsnum','unsigned int')
01618     queryResult.extend('bxlumivalue','blob')
01619     queryResult.extend('bxlumierror','blob')
01620     queryResult.extend('bxlumiquality','blob')
01621     queryResult.extend('startorbit','unsigned int')    
01622     queryHandle.addToOrderList('s.STARTORBIT')
01623     queryHandle.defineOutput(queryResult)
01624     cursor=queryHandle.execute()
01625     while cursor.next():
01626         cmslsnum=cursor.currentRow()['cmslsnum'].data()
01627         bxlumivalue=cursor.currentRow()['bxlumivalue'].data()
01628         bxlumierror=cursor.currentRow()['bxlumierror'].data()
01629         bxlumiquality=cursor.currentRow()['bxlumiquality'].data()
01630         startorbit=cursor.currentRow()['startorbit'].data()
01631         result.append([cmslsnum,bxlumivalue,bxlumierror,bxlumiquality,startorbit])
01632     return result
01633 
01634 def lumidetailAllalgosByrun(queryHandle,runnum):
01635     '''
01636     select s.cmslsnum,d.bxlumivalue,d.bxlumierror,d.bxlumiquality,d.algoname,s.startorbit from LUMIDETAIL d,LUMISUMMARY s where s.runnum=:runnumber and s.lumisummary_id=d.lumisummary_id order by s.startorbit,d.algoname
01637     output: {algoname:{cmslsnum:[bxlumivalue,bxlumierror,bxlumiquality,startorbit]}}
01638     '''
01639     result={}
01640     queryHandle.addToTableList(nameDealer.lumidetailTableName(),'d')
01641     queryHandle.addToTableList(nameDealer.lumisummaryTableName(),'s')
01642     queryCondition=coral.AttributeList()
01643     queryCondition.extend('runnum','unsigned int')
01644     queryCondition['runnum'].setData(int(runnum))
01645     queryHandle.addToOutputList('s.CMSLSNUM','cmslsnum')
01646     queryHandle.addToOutputList('d.BXLUMIVALUE','bxlumivalue')
01647     queryHandle.addToOutputList('d.BXLUMIERROR','bxlumierror')
01648     queryHandle.addToOutputList('d.BXLUMIQUALITY','bxlumiquality')
01649     queryHandle.addToOutputList('d.ALGONAME','algoname')
01650     queryHandle.addToOutputList('s.STARTORBIT','startorbit')
01651     queryHandle.setCondition('s.RUNNUM=:runnum and s.LUMISUMMARY_ID=d.LUMISUMMARY_ID',queryCondition)
01652     queryResult=coral.AttributeList()
01653     queryResult.extend('cmslsnum','unsigned int')
01654     queryResult.extend('bxlumivalue','blob')
01655     queryResult.extend('bxlumierror','blob')
01656     queryResult.extend('bxlumiquality','blob')
01657     queryResult.extend('algoname','string')
01658     queryResult.extend('startorbit','unsigned int')    
01659     queryHandle.addToOrderList('startorbit')
01660     queryHandle.addToOrderList('algoname')
01661     queryHandle.defineOutput(queryResult)
01662     cursor=queryHandle.execute()
01663     while cursor.next():
01664         cmslsnum=cursor.currentRow()['cmslsnum'].data()
01665         bxlumivalue=cursor.currentRow()['bxlumivalue'].data()
01666         bxlumierror=cursor.currentRow()['bxlumierror'].data()
01667         bxlumiquality=cursor.currentRow()['bxlumiquality'].data()
01668         algoname=cursor.currentRow()['algoname'].data()
01669         startorbit=cursor.currentRow()['startorbit'].data()
01670         if not result.has_key(algoname):
01671             dataPerAlgo={}
01672             dataPerAlgo[cmslsnum]=[bxlumivalue,bxlumierror,bxlumiquality,startorbit]
01673             result[algoname]=dataPerAlgo
01674         else:
01675             result[algoname][cmslsnum]=[bxlumivalue,bxlumierror,bxlumiquality,startorbit]           
01676     return result
01677 
01678 def hlttrgMappingByrun(queryHandle,runnum):
01679     '''
01680     select m.hltpathname,m.l1seed from cmsrunsummary r,trghltmap m where r.runnum=:runnum and m.hltkey=r.hltkey
01681     output: {hltpath:l1seed}
01682     '''
01683     result={}
01684     queryHandle.addToTableList(nameDealer.cmsrunsummaryTableName(),'r')
01685     queryHandle.addToTableList(nameDealer.trghltMapTableName(),'m')
01686     queryCondition=coral.AttributeList()
01687     queryCondition.extend('runnum','unsigned int')
01688     queryCondition['runnum'].setData(int(runnum))
01689     queryHandle.addToOutputList('m.HLTPATHNAME','hltpathname')
01690     queryHandle.addToOutputList('m.L1SEED','l1seed')
01691     queryHandle.setCondition('r.RUNNUM=:runnum and m.HLTKEY=r.HLTKEY',queryCondition)
01692     queryResult=coral.AttributeList()
01693     queryResult.extend('hltpathname','string')
01694     queryResult.extend('l1seed','string')
01695     queryHandle.defineOutput(queryResult)
01696     cursor=queryHandle.execute()
01697     while cursor.next():
01698         hltpathname=cursor.currentRow()['hltpathname'].data()
01699         l1seed=cursor.currentRow()['l1seed'].data()
01700         if not result.has_key(hltpathname):
01701             result[hltpathname]=l1seed
01702     return result
01703 
01704 def runsByfillrange(queryHandle,minFill,maxFill):
01705     '''
01706     find all runs in the fill range inclusive
01707     select runnum,fillnum from cmsrunsummary where fillnum>=:minFill and fillnum<=:maxFill
01708     output: fillDict={fillnum:[runlist]}
01709     '''
01710     result={}
01711     queryHandle.addToTableList(nameDealer.cmsrunsummaryTableName())
01712     queryCondition=coral.AttributeList()
01713     queryCondition.extend('minFill','unsigned int')
01714     queryCondition.extend('maxFill','unsigned int')
01715     queryCondition['minFill'].setData(int(minFill))
01716     queryCondition['maxFill'].setData(int(maxFill))
01717     queryHandle.addToOutputList('RUNNUM','runnum')
01718     queryHandle.addToOutputList('FILLNUM','fillnum')
01719     queryHandle.setCondition('FILLNUM>=:minFill and FILLNUM<=:maxFill',queryCondition)
01720     queryResult=coral.AttributeList()
01721     queryResult.extend('runnum','unsigned int')
01722     queryResult.extend('fillnum','unsigned int')
01723     queryHandle.defineOutput(queryResult)
01724     cursor=queryHandle.execute()
01725     while cursor.next():
01726         runnum=cursor.currentRow()['runnum'].data()
01727         fillnum=cursor.currentRow()['fillnum'].data()
01728         if not result.has_key(fillnum):
01729             result[fillnum]=[runnum]
01730         else:
01731             result[fillnum].append(runnum)
01732     return result
01733 
01734 def runsByTimerange(queryHandle,minTime,maxTime):
01735     '''
01736     find all runs in the time range inclusive
01737     the selected run must have started after minTime and finished by maxTime
01738     select runnum,to_char(startTime),to_char(stopTime) from cmsrunsummary where startTime>=timestamp(minTime) and stopTime<=timestamp(maxTime);
01739     input: minTime,maxTime in python obj datetime.datetime
01740     output: {runnum:[starttime,stoptime]} return in python obj datetime.datetime
01741     '''
01742     t=lumiTime.lumiTime()
01743     result={}
01744     coralminTime=coral.TimeStamp(minTime.year,minTime.month,minTime.day,minTime.hour,minTime.minute,minTime.second,0)
01745     coralmaxTime=coral.TimeStamp(maxTime.year,maxTime.month,maxTime.day,maxTime.hour,maxTime.minute,maxTime.second,0)
01746     queryHandle.addToTableList(nameDealer.cmsrunsummaryTableName())
01747     queryCondition=coral.AttributeList()
01748     queryCondition.extend('minTime','time stamp')
01749     queryCondition.extend('maxTime','time stamp')
01750     queryCondition['minTime'].setData(coralminTime)
01751     queryCondition['maxTime'].setData(coralmaxTime)
01752     queryHandle.addToOutputList('RUNNUM','runnum')
01753     queryHandle.addToOutputList('TO_CHAR(STARTTIME,\''+t.coraltimefm+'\')','starttime')
01754     queryHandle.addToOutputList('TO_CHAR(STOPTIME,\''+t.coraltimefm+'\')','stoptime')
01755     queryHandle.setCondition('STARTTIME>=:minTime and STOPTIME<=:maxTime',queryCondition)
01756     queryResult=coral.AttributeList()
01757     queryResult.extend('runnum','unsigned int')
01758     queryResult.extend('starttime','string')
01759     queryResult.extend('stoptime','string')
01760     queryHandle.defineOutput(queryResult)
01761     cursor=queryHandle.execute()
01762     while cursor.next():
01763         runnum=cursor.currentRow()['runnum'].data()
01764         starttimeStr=cursor.currentRow()['starttime'].data()
01765         stoptimeStr=cursor.currentRow()['stoptime'].data()
01766         if not result.has_key(runnum):
01767             result[runnum]=[t.StrToDatetime(starttimeStr),t.StrToDatetime(stoptimeStr)]
01768     return result
01769     
01770 if __name__=='__main__':
01771     msg=coral.MessageStream('')
01772     #msg.setMsgVerbosity(coral.message_Level_Debug)
01773     msg.setMsgVerbosity(coral.message_Level_Error)
01774     os.environ['CORAL_AUTH_PATH']='/afs/cern.ch/cms/DB/lumi'
01775     svc = coral.ConnectionService()
01776     connectstr='oracle://cms_orcoff_prod/cms_lumi_prod'
01777     session=svc.connect(connectstr,accessMode=coral.access_ReadOnly)
01778     session.typeConverter().setCppTypeForSqlType("unsigned int","NUMBER(10)")
01779     session.typeConverter().setCppTypeForSqlType("unsigned long long","NUMBER(20)")
01780     session.transaction().start(True)
01781     schema=session.nominalSchema()
01782     allruns=allruns(schema,requireLumisummary=True,requireTrg=True,requireHlt=True)
01783     print 'allruns in runsummary and lumisummary and trg and hlt ',len(allruns)
01784     #q=schema.newQuery()
01785     #runsummaryOut=runsummaryByrun(q,139400)
01786     #del q
01787     #q=schema.newQuery()
01788     #lumisummaryOut=lumisummaryByrun(q,139400,'0001')
01789     #del q
01790     #q=schema.newQuery()
01791     #lumisummaryOutStablebeam7TeV=lumisummaryByrun(q,139400,'0001',beamstatus='STABLE BEAMS',beamenergy=3.5E003,beamenergyfluctuation=0.09)
01792     #del q
01793     #q=schema.newQuery()
01794     #lumitotal=lumisumByrun(q,139400,'0001')
01795     #del q
01796     #q=schema.newQuery()
01797     #lumitotalStablebeam7TeV=lumisumByrun(q,139400,'0001',beamstatus='STABLE BEAMS',beamenergy=3.5E003,beamenergyfluctuation=0.09)
01798     #del q
01799     #q=schema.newQuery()
01800     #trgbitzero=trgbitzeroByrun(q,139400)
01801     #del q
01802     #q=schema.newQuery()
01803     #lumijointrg=lumisummarytrgbitzeroByrun(q,135525,'0001')
01804     #del q
01805     #q=schema.newQuery()
01806     #lumijointrgStablebeam7TeV=lumisummarytrgbitzeroByrun(q,135525,'0001',beamstatus='STABLE BEAMS',beamenergy=3.5E003,beamenergyfluctuation=0.09)
01807     #del q
01808     #q=schema.newQuery()
01809     #trgforbit=trgBybitnameByrun(q,139400,'L1_ZeroBias')
01810     #del q
01811     #q=schema.newQuery()
01812     #trgallbits=trgAllbitsByrun(q,139400)
01813     #del q
01814     #q=schema.newQuery()
01815     #hltbypath=hltBypathByrun(q,139400,'HLT_Mu5')
01816     #del q
01817     #q=schema.newQuery()
01818     #hltallpath=hltAllpathByrun(q,139400)
01819     #del q
01820     #q=schema.newQuery()
01821     #hlttrgmap=hlttrgMappingByrun(q,139400)
01822     #del q
01823     #q=schema.newQuery()
01824     #occ1detail=lumidetailByrunByAlgo(q,139400,'OCC1')
01825     #del q
01826     #q=schema.newQuery()
01827     #alldetail=lumidetailAllalgosByrun(q,139400)
01828     #del q
01829     #q=schema.newQuery()
01830     #runsbyfill=runsByfillrange(q,1150,1170)
01831     #del q
01832     #now=datetime.datetime.now()
01833     #aweek=datetime.timedelta(weeks=1)
01834     #lastweek=now-aweek
01835     #print lastweek
01836     #q=schema.newQuery()
01837     #runsinaweek=runsByTimerange(q,lastweek,now)
01838     #del q
01839     q=schema.newQuery()
01840     allfills=allfills(q)
01841     del q
01842     session.transaction().commit()  
01843     del session
01844     del svc
01845     #print 'runsummaryByrun : ',runsummaryOut
01846     #print
01847     #print 'lumisummaryByrun : ',lumisummaryOut
01848     #print '######'
01849     #print 'lumisummaryByrun stable beams 7TeV : ',lumisummaryOutStablebeam7TeV
01850     #print '######'
01851     #print 'totallumi : ',lumitotal
01852     #print
01853     #print
01854     #print 'totallumi stable beam and 7TeV: ',lumitotalStablebeam7TeV
01855     #print
01856     #print 'trgbitzero : ',trgbitzero
01857     #print 
01858     #print 'lumijointrg : ', lumijointrg
01859     #print 'total LS : ',len(lumijointrg)
01860     #print 'lumijointrg stable beams 7TeV :', lumijointrgStablebeam7TeV
01861     #print 'total LS : ',len(lumijointrgStablebeam7TeV)
01862     #print 'trgforbit L1_ZeroBias ',trgforbit
01863     #print
01864     #print 'trgallbits ',trgallbits[1] #big query. be aware of speed
01865     #print
01866     #print 'hltforpath HLT_Mu5',hltbypath
01867     #print
01868     #print 'hltallpath ',hltallpath
01869     #print
01870     #print 'hlttrgmap ',hlttrgmap
01871     #print
01872     #print 'lumidetail occ1 ',len(occ1detail)
01873     #print
01874     #print 'runsbyfill ',runsbyfill
01875     #print
01876     #print 'runsinaweek ',runsinaweek.keys()
01877     print 'all fills ',allfills