CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/RecoLuminosity/LumiDB/python/dataDML.py

Go to the documentation of this file.
00001 import os,coral,fnmatch,time
00002 from RecoLuminosity.LumiDB import nameDealer,dbUtil,revisionDML,lumiTime,CommonUtil,lumiCorrections
00003 from datetime import datetime
00004 import array
00005 
00006 
00007 ########################################################################
00008 # LumiDB DML                           API                             #
00009 #                                                                      #
00010 # Author:      Zhen Xie                                                #
00011 ########################################################################
00012 
00013 #==============================
00014 # SELECT
00015 #==============================
00016 def guesscorrIdByName(schema,tagname=None):
00017     '''
00018     select data_id from lumicorrectionss [where entry_name=:tagname]
00019     result lumicorrectionsdataid
00020     
00021     '''
00022     lumicorrectionids=[]
00023     result=None
00024     qHandle=schema.newQuery()
00025     try:
00026         qHandle.addToTableList( nameDealer.lumicorrectionsTableName() )
00027         qHandle.addToOutputList('DATA_ID')
00028         if tagname:
00029             qConditionStr='ENTRY_NAME=:tagname '
00030             qCondition=coral.AttributeList()
00031             qCondition.extend('tagname','string')
00032             qCondition['tagname'].setData(tagname)
00033         qResult=coral.AttributeList()
00034         qResult.extend('DATA_ID','unsigned long long')
00035         qHandle.defineOutput(qResult)
00036         if tagname:
00037             qHandle.setCondition(qConditionStr,qCondition)
00038         cursor=qHandle.execute()
00039         while cursor.next():
00040             dataid=cursor.currentRow()['DATA_ID'].data()
00041             lumicorrectionids.append(dataid)
00042     except :
00043         del qHandle
00044         raise
00045     del qHandle
00046     if len(lumicorrectionids) !=0:return max(lumicorrectionids)
00047     return result
00048 
00049 def lumicorrById(schema,correctiondataid):
00050     '''
00051     select entry_name,a1,a2,drift from lumicorrections where DATA_ID=:dataid
00052     output: {tagname:(data_id(0),a1(1),a2(2),driftcoeff(3))}
00053     '''
00054     result=None
00055     qHandle=schema.newQuery()
00056     try:
00057         qHandle.addToTableList(nameDealer.lumicorrectionsTableName())
00058         qHandle.addToOutputList('ENTRY_NAME')
00059         qHandle.addToOutputList('A1')
00060         qHandle.addToOutputList('A2')
00061         qHandle.addToOutputList('DRIFT')
00062         qCondition=coral.AttributeList()
00063         qCondition.extend('dataid','unsigned long long')
00064         qCondition['dataid'].setData(correctiondataid)
00065         qResult=coral.AttributeList()
00066         qResult.extend('ENTRY_NAME','string')
00067         qResult.extend('A1','float')
00068         qResult.extend('A2','float')
00069         qResult.extend('DRIFT','float')
00070         qHandle.defineOutput(qResult)
00071         qHandle.setCondition('DATA_ID=:dataid',qCondition)
00072         cursor=qHandle.execute()
00073         while cursor.next():
00074             tagname=cursor.currentRow()['ENTRY_NAME'].data()
00075             a1=cursor.currentRow()['A1'].data()
00076             a2=0.0
00077             if cursor.currentRow()['A2'].data():
00078                 a2=cursor.currentRow()['A2'].data()
00079             drift=0.0
00080             if cursor.currentRow()['DRIFT'].data():
00081                 drift=cursor.currentRow()['DRIFT'].data()
00082             result={tagname:(correctiondataid,a1,a2,drift)}
00083     except :
00084         del qHandle
00085         raise
00086     del qHandle
00087     return result
00088 
00089 def fillInRange(schema,fillmin,fillmax,amodetag,startT,stopT):
00090     '''
00091     select fillnum,runnum,starttime from cmsrunsummary where [where fillnum>=:fillmin and fillnum<=:fillmax and amodetag=:amodetag]
00092     output: [fill]
00093     '''
00094     result=[]
00095     tmpresult={}
00096     qHandle=schema.newQuery()
00097     r=nameDealer.cmsrunsummaryTableName()
00098     lute=lumiTime.lumiTime()
00099     try:
00100         qHandle.addToTableList(r)
00101         qConditionPieces=[]
00102         qConditionStr=''
00103         qCondition=coral.AttributeList()
00104         if fillmin:
00105             qConditionPieces.append('FILLNUM>=:fillmin')
00106             qCondition.extend('fillmin','unsigned int')
00107             qCondition['fillmin'].setData(int(fillmin))
00108         if fillmax:
00109             qConditionPieces.append('FILLNUM<=:fillmax')
00110             qCondition.extend('fillmax','unsigned int')
00111             qCondition['fillmax'].setData(int(fillmax))
00112         if amodetag:
00113             qConditionPieces.append('AMODETAG=:amodetag')
00114             qCondition.extend('amodetag','string')
00115             qCondition['amodetag'].setData(amodetag)
00116         if len(qConditionPieces)!=0:
00117             qConditionStr=(' AND ').join(qConditionPieces)
00118         qResult=coral.AttributeList()
00119         qResult.extend('fillnum','unsigned int')
00120         qResult.extend('runnum','unsigned int')
00121         qResult.extend('starttime','string')
00122         qHandle.defineOutput(qResult)
00123         if len(qConditionStr)!=0:
00124             qHandle.setCondition(qConditionStr,qCondition)
00125         qHandle.addToOutputList('FILLNUM','fillnum')
00126         qHandle.addToOutputList('RUNNUM','runnum')
00127         qHandle.addToOutputList('TO_CHAR('+r+'.STARTTIME,\'MM/DD/YY HH24:MI:SS\')','starttime')
00128         cursor=qHandle.execute()
00129         while cursor.next():
00130             currentfill=cursor.currentRow()['fillnum'].data()
00131             runnum=cursor.currentRow()['runnum'].data()
00132             starttimeStr=cursor.currentRow()['starttime'].data()
00133             runTime=lute.StrToDatetime(starttimeStr,customfm='%m/%d/%y %H:%M:%S')
00134             minTime=None
00135             maxTime=None
00136             if startT and stopT:
00137                 minTime=lute.StrToDatetime(startT,customfm='%m/%d/%y %H:%M:%S')
00138                 maxTime=lute.StrToDatetime(stopT,customfm='%m/%d/%y %H:%M:%S')                
00139                 if runTime>=minTime and runTime<=maxTime:
00140                     tmpresult.setdefault(currentfill,[]).append(runnum)
00141             elif startT is not None:
00142                 minTime=lute.StrToDatetime(startT,customfm='%m/%d/%y %H:%M:%S')
00143                 if runTime>=minTime:
00144                     tmpresult.setdefault(currentfill,[]).append(runnum)
00145             elif stopT is not None:
00146                 maxTime=lute.StrToDatetime(stopT,customfm='%m/%d/%y %H:%M:%S')
00147                 if runTime<=maxTime:
00148                     tmpresult.setdefault(currentfill,[]).append(runnum)
00149             else:                
00150                 tmpresult.setdefault(currentfill,[]).append(runnum)
00151         #print tmpresult
00152         for f in sorted(tmpresult):
00153             if tmpresult[f]:
00154                 result.append(f)
00155     except :
00156         del qHandle
00157         raise
00158     del qHandle
00159     return result    
00160 def fillrunMap(schema,fillnum=None,runmin=None,runmax=None,startT=None,stopT=None,l1keyPattern=None,hltkeyPattern=None,amodetag=None):
00161     '''
00162     select fillnum,runnum,starttime from cmsrunsummary [where fillnum=:fillnum and runnum>=runmin and runnum<=runmax and amodetag=:amodetag ]
00163     output: {fill:[runnum,...]}
00164     '''
00165     result={}
00166     timelesslist=[]
00167     qHandle=schema.newQuery()
00168     r=nameDealer.cmsrunsummaryTableName()
00169     lute=lumiTime.lumiTime()
00170     try:
00171         qHandle.addToTableList(r)
00172         qConditionPieces=[]
00173         qConditionStr=''
00174         qCondition=coral.AttributeList()        
00175         if fillnum:
00176             qConditionPieces.append('FILLNUM=:fillnum')
00177             qCondition.extend('fillnum','unsigned int')
00178             qCondition['fillnum'].setData(int(fillnum))
00179         if runmin:
00180             qConditionPieces.append('RUNNUM>=:runmin')
00181             qCondition.extend('runmin','unsigned int')
00182             qCondition['runmin'].setData(runmin)
00183         if runmax:
00184             qConditionPieces.append('RUNNUM<=:runmax')
00185             qCondition.extend('runmax','unsigned int')
00186             qCondition['runmax'].setData(runmax)
00187         if amodetag:
00188             qConditionPieces.append('AMODETAG=:amodetag')
00189             qCondition.extend('amodetag','string')
00190             qCondition['amodetag'].setData(amodetag)
00191         if l1keyPattern:
00192             qConditionPieces.append('regexp_like(L1KEY,:l1keypattern)')
00193             qCondition.extend('l1keypattern','string')
00194             qCondition['l1keypattern'].setData(l1keyPattern)
00195         if hltkeyPattern:
00196             qConditionPieces.append('regexp_like(HLTKEY,:hltkeypattern)')
00197             qCondition.extend('hltkeypattern','string')
00198             qCondition['hltkeypattern'].setData(hltkeyPattern)
00199         if len(qConditionPieces)!=0:
00200             qConditionStr=(' AND ').join(qConditionPieces)        
00201         qResult=coral.AttributeList()
00202         qResult.extend('fillnum','unsigned int')
00203         qResult.extend('runnum','unsigned int')
00204         qResult.extend('starttime','string')
00205         qHandle.defineOutput(qResult)
00206         if len(qConditionStr) !=0:
00207             qHandle.setCondition(qConditionStr,qCondition)
00208         qHandle.addToOutputList('FILLNUM','fillnum')    
00209         qHandle.addToOutputList('RUNNUM','runnum')
00210         qHandle.addToOutputList('TO_CHAR('+r+'.STARTTIME,\'MM/DD/YY HH24:MI:SS\')','starttime')
00211         cursor=qHandle.execute()        
00212         while cursor.next():
00213             currentfill=cursor.currentRow()['fillnum'].data()
00214             starttimeStr=cursor.currentRow()['starttime'].data()
00215             runnum=cursor.currentRow()['runnum'].data()
00216             runTime=lute.StrToDatetime(starttimeStr,customfm='%m/%d/%y %H:%M:%S')
00217             minTime=None
00218             maxTime=None
00219             if startT and stopT:
00220                 minTime=lute.StrToDatetime(startT,customfm='%m/%d/%y %H:%M:%S')
00221                 maxTime=lute.StrToDatetime(stopT,customfm='%m/%d/%y %H:%M:%S')                
00222                 if runTime>=minTime and runTime<=maxTime:
00223                     result.setdefault(currentfill,[]).append(runnum)
00224             elif startT is not None:
00225                 minTime=lute.StrToDatetime(startT,customfm='%m/%d/%y %H:%M:%S')
00226                 if runTime>=minTime:
00227                     result.setdefault(currentfill,[]).append(runnum)
00228             elif stopT is not None:
00229                 maxTime=lute.StrToDatetime(stopT,customfm='%m/%d/%y %H:%M:%S')
00230                 if runTime<=maxTime:
00231                     result.setdefault(currentfill,[]).append(runnum)
00232             else:                
00233                 result.setdefault(currentfill,[]).append(runnum)
00234     except :
00235         del qHandle
00236         raise
00237     del qHandle
00238     return result
00239     
00240 def runList(schema,datatagid,runmin=None,runmax=None,fillmin=None,fillmax=None,startT=None,stopT=None,l1keyPattern=None,hltkeyPattern=None,amodetag=None,nominalEnergy=None,energyFlut=0.2,requiretrg=True,requirehlt=True,preselectedruns=None,lumitype=None):
00241     '''
00242     select r.runnum,l.starttime,l.stoptime,l.data_id,t.data_id,h.data_id from cmsrunsummary r,tagruns tag,lumidata l,trgdata t,hltdata h where l.runnum=tag.runnum and r.runnum=l.runnum and l.runnum=t.runnum and t.runnum=h.runnum and r.fillnum>=:fillmin and r.fillnum<=fillmax and r.runnum>:runmin and r.runnum<:runmax and r.amodetag=:amodetag and regexp_like(r.l1key,:l1keypattern) and regexp_like(hltkey,:hltkeypattern) and l.nominalEnergy>=:nominalEnergy*(1-energyFlut) and l.nominalEnergy<=:nominalEnergy*(1+energyFlut) and tag.tagid<=:tagid and l.starttime is not null and l.stoptime is not null
00243     output: {runnum:[lumiid,trgid,hltid]}
00244     '''
00245     #print datatagid,runmin,runmax,fillmin,fillmax,preselectedruns
00246     if lumitype not in ['HF','PIXEL']:
00247         raise ValueError('unknown lumitype '+lumitype)
00248     lumitableName=''
00249     tagrunstablename=''
00250     if lumitype=='HF':
00251         lumitableName=nameDealer.lumidataTableName()
00252         tagrunstablename=nameDealer.tagRunsTableName()
00253     elif lumitype == 'PIXEL':
00254         lumitableName = nameDealer.pixellumidataTableName()
00255         tagrunstablename=nameDealer.pixeltagRunsTableName()
00256     else:
00257         assert False, "ERROR Unknown lumitype '%s'" % lumitype
00258     
00259     result={}#{runnum,[[lumiid,trgid,hltid]]}
00260     qHandle=schema.newQuery()
00261     r=nameDealer.cmsrunsummaryTableName()
00262     l=lumitableName
00263     tag=tagrunstablename
00264     t=nameDealer.trgdataTableName()
00265     h=nameDealer.hltdataTableName()
00266     lute=lumiTime.lumiTime()
00267     try:
00268         qHandle.addToTableList(r)
00269         qHandle.addToTableList(l)
00270         qHandle.addToTableList(tag)
00271         qConditionStr=r+'.RUNNUM='+l+'.RUNNUM AND '+tag+'.RUNNUM='+l+'.RUNNUM AND '+tag+'.TAGID<=:tagid'
00272         qCondition=coral.AttributeList()
00273         qCondition.extend('tagid','unsigned long long')
00274         qCondition['tagid'].setData(datatagid)
00275         if requiretrg:
00276             qHandle.addToTableList(t)
00277             qConditionStr+=' and '+l+'.RUNNUM='+t+'.RUNNUM'
00278         if requirehlt:
00279             qHandle.addToTableList(h)
00280             qConditionStr+=' and '+l+'.RUNNUM='+h+'.RUNNUM'
00281         if runmin and runmax :
00282             if runmin==runmax:
00283                 qConditionStr+=' AND '+r+'.RUNNUM=:runmin'
00284                 qCondition.extend('runmin','unsigned int')
00285                 qCondition['runmin'].setData(int(runmin))
00286             elif runmax>runmin:
00287                 qConditionStr+=' AND '+r+'.RUNNUM>=:runmin AND '+r+'.RUNNUM<=:runmax'
00288                 qCondition.extend('runmin','unsigned int')
00289                 qCondition.extend('runmax','unsigned int')
00290                 qCondition['runmin'].setData(int(runmin))
00291                 qCondition['runmax'].setData(int(runmax))
00292             else:
00293                 raise 'runmin > runmax'
00294         elif runmin:
00295             qConditionStr+=' AND '+r+'.RUNNUM>=:runmin'
00296             qCondition.extend('runmin','unsigned int')
00297             qCondition['runmin'].setData(int(runmin))
00298         elif runmax:
00299             qConditionStr+=' AND '+r+'.RUNNUM<=:runmax'
00300             qCondition.extend('runmax','unsigned int')
00301             qCondition['runmax'].setData(int(runmax))
00302         else:
00303             pass
00304         if fillmin and fillmax:
00305             if fillmin==fillmax:
00306                 qConditionStr+=' AND '+r+'.FILLNUM=:fillnum'
00307                 qCondition.extend('fillnum','unsigned int')
00308                 qCondition['fillnum'].setData(int(fillmin))
00309             elif fillmax>fillmin:
00310                 qConditionStr+=' AND '+r+'.FILLNUM>=:fillmin AND '+r+'.FILLNUM<=:fillmax'
00311                 qCondition.extend('fillmin','unsigned int')
00312                 qCondition.extend('fillmax','unsigned int')
00313                 qCondition['fillmin'].setData(int(fillmin))
00314                 qCondition['fillmax'].setData(int(fillmax))
00315             else:
00316                 raise 'fillmin > fillmax'
00317         if amodetag:
00318             qConditionStr+=' AND '+r+'.AMODETAG=:amodetag'
00319             qCondition.extend('amodetag','string')
00320             qCondition['amodetag'].setData(amodetag)
00321         if l1keyPattern:
00322             qConditionStr+=' AND regexp_like('+r+'.L1KEY,:l1keypattern)'
00323             qCondition.extend('l1keypattern','string')
00324             qCondition['l1keypattern'].setData(l1keyPattern)
00325         if hltkeyPattern:
00326             qConditionStr+=' AND regexp_like('+r+'.HLTKEY,:hltkeypattern)'
00327             qCondition.extend('hltkeypattern','string')
00328             qCondition['hltkeypattern'].setData(hltkeyPattern)
00329         if nominalEnergy:
00330             emin=nominalEnergy*(1.0-energyFlut)
00331             emax=nominalEnergy*(1.0+energyFlut)
00332             qConditionStr+=' AND '+l+'.NOMINALEGEV>=:emin and '+l+'.NOMINALEGEV<=:emax'
00333             qCondition.extend('emin','float')
00334             qCondition.extend('emax','float')
00335             qCondition['emin'].setData(emin)
00336             qCondition['emax'].setData(emax)
00337         qResult=coral.AttributeList()
00338         qResult.extend('runnum','unsigned int')
00339         qResult.extend('starttime','string')
00340         qResult.extend('stoptime','string')
00341         qResult.extend('lumiid','unsigned long long')
00342         if requiretrg:
00343             qResult.extend('trgid','unsigned long long')
00344         if requirehlt:
00345             qResult.extend('hltid','unsigned long long')
00346         qHandle.setCondition(qConditionStr,qCondition)
00347         qHandle.addToOutputList(r+'.RUNNUM','runnum')
00348         qHandle.addToOutputList('TO_CHAR('+l+'.STARTTIME,\'MM/DD/YY HH24:MI:SS\')','starttime')
00349         qHandle.addToOutputList('TO_CHAR('+l+'.STOPTIME,\'MM/DD/YY HH24:MI:SS\')','stoptime')
00350         qHandle.addToOutputList(l+'.DATA_ID','lumiid')
00351         if requiretrg:
00352             qHandle.addToOutputList(t+'.DATA_ID','trgid')
00353         if requirehlt:
00354             qHandle.addToOutputList(h+'.DATA_ID','hltid')
00355         qHandle.defineOutput(qResult)
00356         cursor=qHandle.execute()
00357         lumiid=0
00358         trgid=0
00359         hltid=0
00360         while cursor.next():
00361             runnum=cursor.currentRow()['runnum'].data()
00362             if preselectedruns and runnum not in preselectedruns:
00363                 continue
00364             if cursor.currentRow()['starttime'].isNull():
00365                 continue
00366             if cursor.currentRow()['stoptime'].isNull():
00367                 continue
00368             starttimeStr=cursor.currentRow()['starttime'].data()
00369             stoptimeStr=cursor.currentRow()['stoptime'].data()
00370             runstartTime=lute.StrToDatetime(starttimeStr,customfm='%m/%d/%y %H:%M:%S')
00371             runstopTime=lute.StrToDatetime(stoptimeStr,customfm='%m/%d/%y %H:%M:%S')
00372             minTime=datetime(2010,1,1,0,0,0,0)
00373             maxTime=datetime.now()
00374             if startT and stopT:
00375                 minTime=lute.StrToDatetime(startT,customfm='%m/%d/%y %H:%M:%S')
00376                 maxTime=lute.StrToDatetime(stopT,customfm='%m/%d/%y %H:%M:%S')
00377                 if not (runstopTime>=minTime and runstartTime<=maxTime):
00378                     continue
00379             elif startT is not None:
00380                 minTime=lute.StrToDatetime(startT,customfm='%m/%d/%y %H:%M:%S')
00381                 if not (runstopTime>=minTime):
00382                         continue
00383             elif stopT is not None:
00384                 maxTime=lute.StrToDatetime(stopT,customfm='%m/%d/%y %H:%M:%S')
00385                 runTime=lute.StrToDatetime(starttimeStr,customfm='%m/%d/%y %H:%M:%S')
00386                 if not (runTime<=maxTime):
00387                     continue
00388             else:
00389                 pass
00390             if not cursor.currentRow()['lumiid'].isNull():
00391                 lumiid=cursor.currentRow()['lumiid'].data()
00392                 if result.has_key(runnum):
00393                     if lumiid>result[runnum][0]:
00394                         result[runnum][0]=lumiid
00395                 else:
00396                     result[runnum]=[lumiid,0,0]
00397             if requiretrg and not cursor.currentRow()['trgid'].isNull():
00398                 trgid=cursor.currentRow()['trgid'].data()
00399                 if result.has_key(runnum):
00400                     if trgid>result[runnum][1]:
00401                         result[runnum][1]=trgid
00402             if requirehlt and not cursor.currentRow()['hltid'].isNull():
00403                 hltid=cursor.currentRow()['hltid'].data()
00404                 if result.has_key(runnum):
00405                     if hltid>result[runnum][2]:
00406                         result[runnum][2]=hltid
00407     except :
00408         del qHandle
00409         raise
00410     del qHandle
00411     return result
00412 
00413 def runsummary(schema,runnum,sessionflavor=''):
00414     '''
00415     select l1key,amodetag,hltkey,fillnum,fillscheme from cmsrunsummary where runnum=:runnum
00416     output: [l1key(0),amodetag(1),hltkey(3),fillnum(4),fillscheme(5)]
00417     '''
00418     result=[]
00419     qHandle=schema.newQuery()
00420     t=lumiTime.lumiTime()
00421     try:
00422         qHandle.addToTableList(nameDealer.cmsrunsummaryTableName())
00423         qCondition=coral.AttributeList()
00424         qCondition.extend('runnum','unsigned int')
00425         qCondition['runnum'].setData(int(runnum))
00426         qHandle.addToOutputList('L1KEY','l1key')
00427         qHandle.addToOutputList('AMODETAG','amodetag')
00428         #qHandle.addToOutputList('EGEV','egev')
00429         qHandle.addToOutputList('HLTKEY','hltkey')
00430         qHandle.addToOutputList('FILLNUM','fillnum')
00431         qHandle.addToOutputList('FILLSCHEME','fillscheme')
00432         #if sessionflavor=='SQLite':
00433         #    qHandle.addToOutputList('STARTTIME','starttime')
00434         #    qHandle.addToOutputList('STOPTIME','stoptime')
00435         #else:
00436         #    qHandle.addToOutputList('to_char(STARTTIME,\''+t.coraltimefm+'\')','starttime')
00437         #    qHandle.addToOutputList('to_char(STOPTIME,\''+t.coraltimefm+'\')','stoptime')
00438         qHandle.setCondition('RUNNUM=:runnum',qCondition)
00439         qResult=coral.AttributeList()
00440         qResult.extend('l1key','string')
00441         qResult.extend('amodetag','string')
00442         #qResult.extend('egev','unsigned int')
00443         qResult.extend('hltkey','string')
00444         qResult.extend('fillnum','unsigned int')
00445         qResult.extend('fillscheme','string')
00446         #qResult.extend('starttime','string')
00447         #qResult.extend('stoptime','string')
00448         qHandle.defineOutput(qResult)
00449         cursor=qHandle.execute()
00450         while cursor.next():
00451             result.append(cursor.currentRow()['l1key'].data())
00452             result.append(cursor.currentRow()['amodetag'].data())
00453             #result.append(cursor.currentRow()['egev'].data())
00454             result.append(cursor.currentRow()['hltkey'].data())
00455             result.append(cursor.currentRow()['fillnum'].data())
00456             fillscheme=''
00457             if not cursor.currentRow()['fillscheme'].isNull():
00458                 fillscheme=cursor.currentRow()['fillscheme'].data()
00459             result.append(fillscheme)
00460             #result.append(cursor.currentRow()['starttime'].data())
00461             #result.append(cursor.currentRow()['stoptime'].data())
00462     except :
00463         del qHandle
00464         raise
00465     del qHandle
00466     return result
00467 
00468 #def mostRecentLuminorms(schema,branchfilter):
00469 #    '''
00470 #    this overview query should be only for norm
00471 #    select e.name,n.data_id,r.revision_id,n.amodetag,n.norm_1,n.egev_1,n.norm_occ2,n.norm_et,n.norm_pu,n.constfactor from luminorms_entries e,luminorms_rev r,luminorms n where n.entry_id=e.entry_id and n.data_id=r.data_id and r.revision_id>=min(branchfilter) and r.revision_id<=max(branchfilter);
00472 #    output {norm_name:(amodetag(0),norm_1(1),egev_1(2),norm_occ2(3),norm_et(4),norm_pu(5),constfactor(6))}
00473 #    '''
00474 #    #print branchfilter
00475 #    result={}
00476 #    entry2datamap={}
00477 #    branchmin=0
00478 #    branchmax=0
00479 #    if branchfilter and len(branchfilter)!=0:
00480 #        branchmin=min(branchfilter)
00481 #        branchmax=max(branchfilter)
00482 #    else:
00483 #        return result
00484 #    #print branchmin,branchmax
00485 #    qHandle=schema.newQuery()
00486 #    normdict={}
00487 #    try:
00488 #        qHandle.addToTableList(nameDealer.entryTableName(nameDealer.luminormTableName()),'e')
00489 #        qHandle.addToTableList(nameDealer.luminormTableName(),'n')
00490 #        qHandle.addToTableList(nameDealer.revmapTableName(nameDealer.luminormTableName()),'r')
00491 #        qHandle.addToOutputList('e.NAME','normname')
00492 #        qHandle.addToOutputList('r.DATA_ID','data_id')
00493 #        qHandle.addToOutputList('r.REVISION_ID','revision_id')
00494 #        qHandle.addToOutputList('n.AMODETAG','amodetag')
00495 #        qHandle.addToOutputList('n.NORM_1','norm_1')
00496 #        qHandle.addToOutputList('n.EGEV_1','energy_1')
00497 #        qHandle.addToOutputList('n.NORM_OCC2','norm_occ2')
00498 #        qHandle.addToOutputList('n.NORM_ET','norm_et')
00499 #        qHandle.addToOutputList('n.NORM_PU','norm_pu')
00500 #        qHandle.addToOutputList('n.CONSTFACTOR','constfactor')
00501 #        qCondition=coral.AttributeList()
00502 #        qCondition.extend('branchmin','unsigned long long')
00503 #        qCondition.extend('branchmax','unsigned long long')
00504 #        qCondition['branchmin'].setData(branchmin)
00505 #        qCondition['branchmax'].setData(branchmax)
00506 #        qResult=coral.AttributeList()
00507 #        qResult.extend('normname','string')
00508 #        qResult.extend('data_id','unsigned long long')
00509 #        qResult.extend('revision_id','unsigned long long')
00510 #        qResult.extend('amodetag','string')
00511 #        qResult.extend('norm_1','float')
00512 #        qResult.extend('energy_1','unsigned int')
00513 #        qResult.extend('norm_occ2','float')
00514 #        qResult.extend('norm_et','float')
00515 #        qResult.extend('norm_pu','float')
00516 #        qResult.extend('constfactor','float')
00517 #        qHandle.defineOutput(qResult)
00518 #        qHandle.setCondition('n.ENTRY_ID=e.ENTRY_ID and n.DATA_ID=r.DATA_ID AND n.DATA_ID=r.DATA_ID AND r.REVISION_ID>=:branchmin AND r.REVISION_ID<=:branchmax',qCondition)
00519 #        cursor=qHandle.execute()
00520 #        while cursor.next():
00521 #            data_id=cursor.currentRow()['data_id'].data()
00522 #            normname=cursor.currentRow()['normname'].data()
00523 #            if not normdict.has_key(normname):
00524 #                normdict[normname]=0
00525 #            if data_id>normdict[normname]:
00526 #                normdict[normname]=data_id
00527 #                amodetag=cursor.currentRow()['amodetag'].data()
00528 #                norm_1=cursor.currentRow()['norm_1'].data()
00529 #                energy_1=cursor.currentRow()['energy_1'].data()
00530 #                norm_occ2=1.0
00531 #                if not cursor.currentRow()['norm_occ2'].isNull():
00532 #                    norm_occ2=cursor.currentRow()['norm_occ2'].data()
00533 #                norm_et=1.0
00534 #                if not cursor.currentRow()['norm_et'].isNull():
00535 #                    norm_et=cursor.currentRow()['norm_et'].data()
00536 #                norm_pu=1.0
00537 #                if not cursor.currentRow()['norm_pu'].isNull():
00538 #                    norm_pu=cursor.currentRow()['norm_pu'].data()
00539 #                constfactor=1.0
00540 #                if not cursor.currentRow()['constfactor'].isNull():
00541 #                    constfactor=cursor.currentRow()['constfactor'].data()
00542 #                result[normname]=(amodetag,norm_1,energy_1,norm_occ2,norm_et,norm_pu,constfactor)
00543 #    except:
00544 #        raise
00545 #    return result
00546 def luminormById(schema,dataid):
00547     '''
00548     select entry_name,amodetag,norm_1,egev_1,norm_2,egev_2 from luminorms where DATA_ID=:dataid
00549     output: {norm_name:(amodetag(0),norm_1(1),egev_1(2),norm_occ2(3),norm_et(4),norm_pu(5),constfactor(6))}
00550     '''
00551     result=None
00552     qHandle=schema.newQuery()
00553     try:
00554         qHandle.addToTableList(nameDealer.luminormTableName())
00555         qHandle.addToOutputList('ENTRY_NAME','normname')
00556         qHandle.addToOutputList('AMODETAG','amodetag')
00557         qHandle.addToOutputList('NORM_1','norm_1')
00558         qHandle.addToOutputList('EGEV_1','energy_1')
00559         qHandle.addToOutputList('NORM_OCC2','norm_occ2')
00560         qHandle.addToOutputList('NORM_ET','norm_et')
00561         qHandle.addToOutputList('NORM_PU','norm_pu')
00562         qHandle.addToOutputList('CONSTFACTOR','constfactor')        
00563         qCondition=coral.AttributeList()
00564         qCondition.extend('dataid','unsigned long long')
00565         qCondition['dataid'].setData(dataid)
00566         qResult=coral.AttributeList()
00567         qResult.extend('normname','string')
00568         qResult.extend('amodetag','string')
00569         qResult.extend('norm_1','float')
00570         qResult.extend('energy_1','unsigned int')
00571         qResult.extend('norm_occ2','float')
00572         qResult.extend('norm_et','float')
00573         qResult.extend('norm_pu','float')
00574         qResult.extend('constfactor','float')
00575         qHandle.defineOutput(qResult)
00576         qHandle.setCondition('DATA_ID=:dataid',qCondition)
00577         cursor=qHandle.execute()
00578         while cursor.next():
00579             normname=cursor.currentRow()['normname'].data()
00580             amodetag=cursor.currentRow()['amodetag'].data()
00581             norm_1=cursor.currentRow()['norm_1'].data()
00582             energy_1=cursor.currentRow()['energy_1'].data()
00583             norm_occ2=1.0
00584             if cursor.currentRow()['norm_occ2'].data():
00585                 norm_occ2=cursor.currentRow()['norm_occ2'].data()
00586             norm_et=1.0
00587             if cursor.currentRow()['norm_et'].data():
00588                 norm_et=cursor.currentRow()['norm_et'].data()
00589             norm_pu=1.0
00590             if cursor.currentRow()['norm_pu'].data():
00591                 norm_pu=cursor.currentRow()['norm_pu'].data()
00592             constfactor=1.0
00593             if cursor.currentRow()['constfactor'].data():
00594                 constfactor=cursor.currentRow()['constfactor'].data()
00595             result={normname:(amodetag,norm_1,energy_1,norm_occ2,norm_et,norm_pu,constfactor)}
00596     except :
00597         del qHandle
00598         raise
00599     del qHandle
00600     return result
00601 
00602 #def mostRecentLumicorrs(schema,branchfilter):
00603 #    '''
00604 #    this overview query should be only for corr
00605 #    select e.name,n.data_id,r.revision_id , n.a1,n.a2,n.drift from lumicorrections_entries e,lumicorrections_rev r,lumicorrections n where n.entry_id=e.entry_id and n.data_id=r.data_id and r.revision_id>=min(branchfilter) and r.revision_id<=max(branchfilter) group by e.entry_name,r.revision_id,n.a1,n.a2,n.drift;
00606 #    output {corrname:(data_id,a1,a2,drift)}
00607 #    '''
00608 #    #print branchfilter
00609 #    result={}
00610 #    entry2datamap={}
00611 #    branchmin=0
00612 #    branchmax=0
00613 #    if branchfilter and len(branchfilter)!=0:
00614 #        branchmin=min(branchfilter)
00615 #        branchmax=max(branchfilter)
00616 #    else:
00617 #        return result
00618 #    qHandle=schema.newQuery()
00619 #    corrdict={}
00620 #    try:
00621 #        qHandle.addToTableList(nameDealer.entryTableName(nameDealer.lumicorrectionsTableName()),'e')
00622 #        qHandle.addToTableList(nameDealer.lumicorrectionsTableName(),'n')
00623 #        qHandle.addToTableList(nameDealer.revmapTableName(nameDealer.lumicorrectionsTableName()),'r')
00624 #        qHandle.addToOutputList('e.NAME','corrname')
00625 #        qHandle.addToOutputList('r.DATA_ID','data_id')
00626 #        qHandle.addToOutputList('r.REVISION_ID','revision_id')
00627 #        qHandle.addToOutputList('n.A1','a1')
00628 #        qHandle.addToOutputList('n.A2','a2')
00629 #        qHandle.addToOutputList('n.DRIFT','drift')
00630 #        qCondition=coral.AttributeList()
00631 #        qCondition.extend('branchmin','unsigned long long')
00632 #        qCondition.extend('branchmax','unsigned long long')
00633 #        qCondition['branchmin'].setData(branchmin)
00634 #        qCondition['branchmax'].setData(branchmax)
00635 #        qResult=coral.AttributeList()
00636 #        qResult.extend('corrname','string')
00637 #        qResult.extend('data_id','unsigned long long')
00638 #        qResult.extend('revision_id','unsigned long long')
00639 #        qResult.extend('a1','float')
00640 #        qResult.extend('a2','float')
00641 #        qResult.extend('drift','float')
00642 #        qHandle.defineOutput(qResult)
00643 #        qHandle.setCondition('n.ENTRY_ID=e.ENTRY_ID and n.DATA_ID=r.DATA_ID AND n.DATA_ID=r.DATA_ID AND r.REVISION_ID>=:branchmin AND r.REVISION_ID<=:branchmax',qCondition)
00644 #        cursor=qHandle.execute()
00645 #        while cursor.next():
00646 #            corrname=cursor.currentRow()['corrname'].data()
00647 #            data_id=cursor.currentRow()['data_id'].data()
00648 #            if not corrdict.has_key(corrname):
00649 #                corrdict[corrname]=0
00650 #            if data_id>corrdict[corrname]:
00651 #                corrdict[corrname]=data_id
00652 #                a1=cursor.currentRow()['a1'].data() #required
00653 #                a2=0.0
00654 #                if not cursor.currentRow()['a2'].isNull():
00655 #                    a2=cursor.currentRow()['a2'].data()
00656 #                drift=0.0
00657 #                if not cursor.currentRow()['drift'].isNull():
00658 #                    drift=cursor.currentRow()['drift'].data()
00659 #                result[corrname]=(data_id,a1,a2,drift)
00660 #    except:
00661 #        raise
00662 #    return result
00663 
00664 def luminormById(schema,dataid):
00665     '''
00666     select entry_name,amodetag,norm_1,egev_1,norm_2,egev_2 from luminorms where DATA_ID=:dataid
00667     result (normname(0),amodetag(1),egev(2),norm(3),norm_occ2(4),norm_et(5),norm_pu(6),constfactor(7))
00668     '''
00669     result=None
00670     qHandle=schema.newQuery()
00671     try:
00672         qHandle.addToTableList(nameDealer.luminormTableName())
00673         qHandle.addToOutputList('ENTRY_NAME','normname')
00674         qHandle.addToOutputList('AMODETAG','amodetag')
00675         qHandle.addToOutputList('NORM_1','norm_1')
00676         qHandle.addToOutputList('EGEV_1','energy_1')
00677         qHandle.addToOutputList('NORM_OCC2','norm_occ2')
00678         qHandle.addToOutputList('NORM_ET','norm_et')
00679         qHandle.addToOutputList('NORM_PU','norm_pu')
00680         qHandle.addToOutputList('CONSTFACTOR','constfactor')
00681         qCondition=coral.AttributeList()
00682         qCondition.extend('dataid','unsigned long long')
00683         qCondition['dataid'].setData(dataid)
00684         qResult=coral.AttributeList()
00685         qResult.extend('normname','string')
00686         qResult.extend('amodetag','string')
00687         qResult.extend('norm_1','float')
00688         qResult.extend('energy_1','unsigned int')
00689         qResult.extend('norm_occ2','float')
00690         qResult.extend('norm_et','float')
00691         qResult.extend('norm_pu','float')
00692         qResult.extend('constfactor','float')
00693         qHandle.defineOutput(qResult)
00694         qHandle.setCondition('DATA_ID=:dataid',qCondition)
00695         cursor=qHandle.execute()
00696         while cursor.next():
00697             normname=cursor.currentRow()['normname'].data()
00698             amodetag=cursor.currentRow()['amodetag'].data()
00699             norm_1=cursor.currentRow()['norm_1'].data()
00700             energy_1=cursor.currentRow()['energy_1'].data()
00701             norm_occ2=1.0
00702             if cursor.currentRow()['norm_occ2'].data():
00703                 norm_occ2=cursor.currentRow()['norm_occ2'].data()
00704             norm_et=1.0
00705             if cursor.currentRow()['norm_et'].data():
00706                 norm_et=cursor.currentRow()['norm_et'].data()
00707             norm_pu=1.0
00708             if cursor.currentRow()['norm_pu'].data():
00709                 norm_pu=cursor.currentRow()['norm_pu'].data()
00710             constfactor=1.0
00711             if cursor.currentRow()['constfactor'].data():
00712                 constfactor=cursor.currentRow()['constfactor'].data()
00713             result={normname:(amodetag,norm_1,energy_1,norm_occ2,norm_et,norm_pu,constfactor)}
00714     except :
00715         del qHandle
00716         raise
00717     del qHandle
00718     return result
00719 
00720 def trgRunById(schema,dataid,trgbitname=None,trgbitnamepattern=None):
00721     '''
00722     query: select RUNNUM,SOURCE,BITZERONAME,BITNAMECLOB,ALGOMASK_H,ALGOMASK_L,TECHMASK from trgdata where DATA_ID=:dataid
00723     
00724     output: [runnum(0),datasource(1),bitzeroname(2),bitnamedict(3),algomask_h(4),algomask_l(5),techmask(6)]
00725              -- runnumber
00726              -- original source database name
00727              -- deadtime norm bitname
00728              -- bitnamedict [(bitidx,bitname),...]
00729     '''
00730     result=[]
00731     qHandle=schema.newQuery()
00732     runnum=None
00733     datasource=None
00734     bitzeroname=None
00735     bitnamedict=[]
00736     try:
00737         qHandle.addToTableList(nameDealer.trgdataTableName())
00738         qHandle.addToOutputList('RUNNUM','runnum')
00739         qHandle.addToOutputList('SOURCE','source')
00740         qHandle.addToOutputList('BITZERONAME','bitzeroname')
00741         qHandle.addToOutputList('BITNAMECLOB','bitnameclob')
00742         qHandle.addToOutputList('ALGOMASK_H','algomask_h')
00743         qHandle.addToOutputList('ALGOMASK_L','algomask_l')
00744         qHandle.addToOutputList('TECHMASK','techmask')
00745         qCondition=coral.AttributeList()
00746         qCondition.extend('dataid','unsigned long long')
00747         qCondition['dataid'].setData(dataid)
00748         qResult=coral.AttributeList()
00749         qResult.extend('runnum','unsigned int')
00750         qResult.extend('source','string')
00751         qResult.extend('bitzeroname','string')
00752         qResult.extend('bitnameclob','string')
00753         qResult.extend('algomask_h','unsigned long long')
00754         qResult.extend('algomask_l','unsigned long long')
00755         qResult.extend('techmask','unsigned long long')
00756         qHandle.defineOutput(qResult)
00757         qHandle.setCondition('DATA_ID=:dataid',qCondition)        
00758         cursor=qHandle.execute()
00759         bitnameclob=None
00760         bitnames=[]
00761         while cursor.next():
00762             runnum=cursor.currentRow()['runnum'].data()
00763             source=cursor.currentRow()['source'].data()
00764             bitzeroname=cursor.currentRow()['bitzeroname'].data()
00765             bitnameclob=cursor.currentRow()['bitnameclob'].data()
00766             algomask_h=cursor.currentRow()['algomask_h'].data()
00767             algomask_l=cursor.currentRow()['algomask_l'].data()
00768             techmask=cursor.currentRow()['techmask'].data()
00769         if bitnameclob:
00770             bitnames=bitnameclob.split(',')
00771             for trgnameidx,trgname in enumerate(bitnames):
00772                 if trgbitname :
00773                     if trgname==trgbitname:
00774                         bitnamedict.append((trgnameidx,trgname))
00775                         break
00776                 elif trgbitnamepattern:
00777                     if fnmatch.fnmatch(trgname,trgbitnamepattern):
00778                         bitnamedict.append((trgnameidx,trgname))
00779                 else:
00780                     bitnamedict.append((trgnameidx,trgname))
00781         result=[runnum,source,bitzeroname,bitnamedict,algomask_h,algomask_l,techmask]
00782     except :
00783         del qHandle
00784         raise 
00785     del qHandle
00786     return result
00787 
00788 def trgLSById(schema,dataid,trgbitname=None,trgbitnamepattern=None,withL1Count=False,withPrescale=False):
00789     '''
00790     output: (runnum,{cmslsnum:[deadtimecount(0),bitzerocount(1),bitzeroprescale(2),deadfrac(3),[(bitname,trgcount,prescale,mask)](4)]})
00791     '''
00792     runnum=0
00793     result={}
00794     trgnamedict=[]
00795     if  trgbitname or trgbitnamepattern or withPrescale or withL1Count:
00796         trgrundata=trgRunById(schema,dataid,trgbitname=trgbitname,trgbitnamepattern=trgbitnamepattern)
00797         trgnamedict=trgrundata[3]#[runnum(0),datasource(1),bitzeroname(2),bitnamedict(3),algomask_h(4),algomask_l(5),techmask(6)]
00798         algomask_h=trgrundata[4]
00799         algomask_l=trgrundata[5]
00800         techmask=trgrundata[6]
00801     qHandle=schema.newQuery()
00802     try:
00803         qHandle.addToTableList(nameDealer.lstrgTableName())
00804         qHandle.addToOutputList('RUNNUM','runnum')
00805         qHandle.addToOutputList('CMSLSNUM','cmslsnum')
00806         qHandle.addToOutputList('DEADTIMECOUNT','deadtimecount')
00807         #qHandle.addToOutputList('BITZEROCOUNT','bitzerocount')
00808         #qHandle.addToOutputList('BITZEROPRESCALE','bitzeroprescale')
00809         qHandle.addToOutputList('DEADFRAC','deadfrac')
00810         if withPrescale:
00811             qHandle.addToOutputList('PRESCALEBLOB','prescalesblob')
00812         if withL1Count:
00813             qHandle.addToOutputList('TRGCOUNTBLOB','trgcountblob')
00814         qConditionStr='DATA_ID=:dataid'
00815         qCondition=coral.AttributeList()
00816         qCondition.extend('dataid','unsigned long long')
00817         qCondition['dataid'].setData(dataid)
00818         qResult=coral.AttributeList()
00819         qResult.extend('runnum','unsigned int')
00820         qResult.extend('cmslsnum','unsigned int')
00821         qResult.extend('deadtimecount','unsigned long long')
00822         #qResult.extend('bitzerocount','unsigned int')
00823         #qResult.extend('bitzeroprescale','unsigned int')
00824         qResult.extend('deadfrac','float')
00825         if withPrescale:
00826             qResult.extend('prescalesblob','blob')
00827         if withL1Count:
00828             qResult.extend('trgcountblob','blob')
00829         qHandle.defineOutput(qResult)
00830         qHandle.setCondition(qConditionStr,qCondition)
00831         cursor=qHandle.execute()
00832         while cursor.next():
00833             runnum=cursor.currentRow()['runnum'].data()
00834             cmslsnum=cursor.currentRow()['cmslsnum'].data()
00835             deadtimecount=cursor.currentRow()['deadtimecount'].data()
00836             #bitzerocount=cursor.currentRow()['bitzerocount'].data()
00837             #bitzeroprescale=cursor.currentRow()['bitzeroprescale'].data()
00838             bitzerocount=0
00839             bitzeroprescale=0
00840             deadfrac=cursor.currentRow()['deadfrac'].data()
00841             if not result.has_key(cmslsnum):
00842                 result[cmslsnum]=[]
00843             result[cmslsnum].append(deadtimecount)
00844             result[cmslsnum].append(bitzerocount)
00845             result[cmslsnum].append(bitzeroprescale)
00846             result[cmslsnum].append(deadfrac)
00847             prescalesblob=None
00848             trgcountblob=None
00849             if withPrescale:
00850                 prescalesblob=cursor.currentRow()['prescalesblob'].data()
00851             if withL1Count:
00852                 trgcountblob=cursor.currentRow()['trgcountblob'].data()
00853             prescales=[]
00854             trgcounts=[]
00855             if prescalesblob:
00856                 if runnum <150008: ###WORKAROUND PATCH!! because the 2010 blobs were packed as type l ###
00857                     prescales=CommonUtil.unpackBlobtoArray(prescalesblob,'l')
00858                 else:
00859                     prescales=CommonUtil.unpackBlobtoArray(prescalesblob,'I')
00860             if trgcountblob:
00861                 if runnum <150008: ###WORKAROUND PATCH!! because the 2010 blobs were packed as type l ###
00862                     trgcounts=CommonUtil.unpackBlobtoArray(trgcountblob,'l')
00863                 else:
00864                     trgcounts=CommonUtil.unpackBlobtoArray(trgcountblob,'I')
00865                     
00866             bitinfo=[]
00867             for (bitidx,thisbitname) in trgnamedict:
00868                 thispresc=None
00869                 thistrgcount=None
00870                 if prescales:
00871                     thispresc=prescales[bitidx]
00872                 if trgcounts:
00873                     thistrgcount=trgcounts[bitidx]
00874                 maskval=0
00875                 #[0-127] is algobit
00876                 if bitidx in range(0,128):
00877                     if bitidx<64 :#0-63 is in algo_l
00878                         maskval=algomask_l>>bitidx&1
00879                     else:#64-127 is in algo_h
00880                         maskval=algomask_h>>(bitidx-64)&1
00881                 else:
00882                 #[128-191] is techbit
00883                     maskval=techmask>>(bitidx-128)&1
00884                 thisbitinfo=(thisbitname,thistrgcount,thispresc,maskval)
00885                 bitinfo.append(thisbitinfo)
00886             result[cmslsnum].append(bitinfo)
00887     except:
00888         del qHandle
00889         raise 
00890     del qHandle
00891 #    t1=time.time()
00892 #    print 'trgLSById time ',t1-t0
00893     return (runnum,result)
00894 
00895 def lumiRunByIds(schema,dataidMap,lumitype='HF'):
00896     '''
00897     input dataidMap : {run:lumidataid}
00898     result {runnum: (datasource(0),nominalegev(1),ncollidingbunches(2),starttime(3),stoptime(4),nls(5)}
00899     '''
00900     result={}
00901     if not dataidMap:
00902         return result
00903     inputRange=dataidMap.keys()
00904     for r in inputRange:
00905         lumidataid=dataidMap[r][0]
00906         if lumidataid:
00907             perrundata=lumiRunById(schema,lumidataid,lumitype=lumitype)
00908             result[r]=(perrundata[1],perrundata[2],perrundata[3],perrundata[4],perrundata[5])
00909     return result
00910 
00911 def beamstatusByIds(schema,dataidMap):
00912     '''
00913     input dataidMap : {run:lumidataid}
00914     result {runnum:{cmslsnum:beamstatus}}
00915     '''
00916     result={}
00917     if not dataidMap:
00918         return result
00919     inputRange=dataidMap.keys()
00920     for r in inputRange:
00921         if not result.has_key(r):
00922             result[r]={}
00923         lumidataid=dataidMap[r][0]
00924         if lumidataid:
00925             qHandle=schema.newQuery()
00926             try:
00927                 qHandle.addToTableList(nameDealer.lumisummaryv2TableName())
00928                 qHandle.addToOutputList('CMSLSNUM')
00929                 qHandle.addToOutputList('BEAMSTATUS')
00930                 qConditionStr='DATA_ID=:dataid'
00931                 qCondition=coral.AttributeList()
00932                 qCondition.extend('dataid','unsigned long long')
00933                 qCondition['dataid'].setData(int(lumidataid))
00934                 qResult=coral.AttributeList()
00935                 qResult.extend('CMSLSNUM','unsigned int')
00936                 qResult.extend('BEAMSTATUS','string')
00937                 qHandle.defineOutput(qResult)
00938                 qHandle.setCondition(qConditionStr,qCondition)
00939                 cursor=qHandle.execute()
00940                 while cursor.next():
00941                     cmslsnum=cursor.currentRow()['CMSLSNUM'].data()
00942                     bs=cursor.currentRow()['BEAMSTATUS'].data()
00943                     if bs!='STABLE BEAMS':
00944                         result[r][cmslsnum]=bs
00945             except:
00946                 del qHandle
00947                 raise 
00948             del qHandle
00949     return result
00950 
00951 def lumiRunById(schema,lumidataid,lumitype='HF'):
00952     '''
00953     input: lumidataid
00954     output: (runnum(0),datasource(1),nominalegev(2),ncollidingbunches(3),starttimestr(4),stoptimestr(5),nls(6))
00955     '''
00956     result=None
00957     if lumitype not in ['HF','PIXEL']:
00958         raise ValueError('unknown lumitype '+lumitype)
00959     lumitableName=''
00960     if lumitype=='HF':
00961         lumitableName = nameDealer.lumidataTableName()
00962     else:
00963         lumitableName = nameDealer.pixellumidataTableName()
00964     qHandle=schema.newQuery()
00965     try:
00966         qHandle.addToTableList(lumitableName)
00967         qHandle.addToOutputList('RUNNUM')
00968         qHandle.addToOutputList('SOURCE')
00969         qHandle.addToOutputList('NOMINALEGEV')
00970         qHandle.addToOutputList('NCOLLIDINGBUNCHES')
00971         qHandle.addToOutputList('TO_CHAR('+lumitableName+'.STARTTIME,\'MM/DD/YY HH24:MI:SS\')','startT')
00972         qHandle.addToOutputList('TO_CHAR('+lumitableName+'.STOPTIME,\'MM/DD/YY HH24:MI:SS\')','stopT')
00973         qHandle.addToOutputList('NLS')
00974         qConditionStr='DATA_ID=:dataid'
00975         qCondition=coral.AttributeList()
00976         qCondition.extend('dataid','unsigned long long')
00977         qCondition['dataid'].setData(lumidataid)
00978         qResult=coral.AttributeList()
00979         qResult.extend('RUNNUM','unsigned int')
00980         qResult.extend('SOURCE','string')
00981         qResult.extend('NOMINALEGEV','float')
00982         qResult.extend('NCOLLIDINGBUNCHES','unsigned int')
00983         qResult.extend('startT','string')
00984         qResult.extend('stopT','string')
00985         qResult.extend('NLS','unsigned int')
00986         qHandle.defineOutput(qResult)
00987         qHandle.setCondition(qConditionStr,qCondition)
00988         cursor=qHandle.execute()
00989         lu=lumiTime.lumiTime()
00990         nls=0
00991         while cursor.next():
00992             runnum=cursor.currentRow()['RUNNUM'].data()
00993             datasource=cursor.currentRow()['SOURCE'].data()
00994             nominalegev=0
00995             if not cursor.currentRow()['NOMINALEGEV'].isNull():
00996                 nominalegev=cursor.currentRow()['NOMINALEGEV'].data()
00997             ncollidingbunches=0
00998             if not cursor.currentRow()['NCOLLIDINGBUNCHES'].isNull():
00999                 ncollidingbunches=cursor.currentRow()['NCOLLIDINGBUNCHES'].data()
01000             startTstr=cursor.currentRow()['startT'].data()
01001             stopTstr=cursor.currentRow()['stopT'].data()
01002             #startT=lu.StrToDatetime(startTstr)
01003             #stopT=lu.StrToDatetime(stopTstr)
01004             if not cursor.currentRow()['NLS'].isNull():
01005                 nls=cursor.currentRow()['NLS'].data()
01006             result=(runnum,datasource,nominalegev,ncollidingbunches,startTstr,stopTstr,nls)
01007     except :
01008         del qHandle
01009         raise
01010     del qHandle
01011     return result
01012 
01013 #def correctionByName(schema,correctiontagname=None):
01014 #    '''
01015 #    get correction coefficients by name
01016 #    input: correctiontagname if None,get current default
01017 #    output: [tagname,a1,a2,drift]
01018 #    if not correctiontagname
01019 #    select entry_name,data_id,a1,a2,drift from lumicorrections where 
01020 #    else:
01021 #    select entry_name,data_id,a1,a2,drift from lumicorrections where entry_name=:correctiontagname
01022 #    '''
01023 
01024     
01025 #def fillschemeByRun(schema,runnum):
01026 #    fillscheme=''
01027 #    ncollidingbunches=0
01028 #    r=nameDealer.cmsrunsummaryTableName()
01029 #    qHandle=schema.newQuery()
01030 #    try:
01031 #        qHandle.addToTableList(r)
01032 #        qHandle.addToOutputList('FILLSCHEME')
01033 #        qHandle.addToOutputList('NCOLLIDINGBUNCHES')
01034 #        qResult=coral.AttributeList()
01035 #        qResult.extend('FILLSCHEME','string')
01036 #        qResult.extend('NCOLLIDINGBUNCHES','unsigned int')
01037 #        qConditionStr='RUNNUM=:runnum'
01038 #        qCondition=coral.AttributeList()
01039 #        qCondition.extend('runnum','unsigned int')
01040 #        qCondition['runnum'].setData(int(runnum))
01041 #        qHandle.defineOutput(qResult)
01042 #        qHandle.setCondition(qConditionStr,qCondition)
01043 #        cursor=qHandle.execute()
01044 #        while cursor.next(): 
01045 #            if not cursor.currentRow()['NCOLLIDINGBUNCHES'].isNull():
01046 #                ncollidingbunches=cursor.currentRow()['NCOLLIDINGBUNCHES'].data()
01047 #            if not cursor.currentRow()['FILLSCHEME'].isNull():
01048 #                fillscheme=cursor.currentRow()['FILLSCHEME'].data()
01049 #    except :
01050 #        del qHandle
01051 #        raise
01052 #    del qHandle
01053 #    return (fillscheme,ncollidingbunches)
01054 def allfillschemes(schema):
01055     afterglows=[]
01056     s=nameDealer.fillschemeTableName()
01057     try:
01058         qHandle.addToTableList(s)
01059         qResult=coral.AttributeList()
01060         qResult.extend('FILLSCHEMEPATTERN','string')
01061         qResult.extend('CORRECTIONFACTOR','float')
01062         qHandle.defineOutput(qResult)
01063         qHandle.addToOutputList('FILLSCHEMEPATTERN')
01064         qHandle.addToOutputList('CORRECTIONFACTOR')
01065         cursor=qHandle.execute()
01066         while cursor.next():
01067             fillschemePattern=cursor.currentRow()['FILLSCHEMEPATTERN'].data()
01068             afterglowfac=cursor.currentRow()['CORRECTIONFACTOR'].data()
01069             afterglows.append((fillschemePattern,afterglowfac))
01070     except :
01071         del qHandle
01072         raise
01073     del qHandle
01074     return afterglows
01075     
01076 def lumiLSById(schema,dataid,beamstatus=None,withBXInfo=False,bxAlgo='OCC1',withBeamIntensity=False,tableName=None):
01077     '''
01078     input:
01079        beamstatus: filter on beam status flag
01080     output:
01081     result (runnum,{lumilsnum,[cmslsnum(0),instlumi(1),instlumierr(2),instlumiqlty(3),beamstatus(4),beamenergy(5),numorbit(6),startorbit(7),(bxvalueArray,bxerrArray)(8),(bxindexArray,beam1intensityArray,beam2intensityArray)(9)]})
01082     '''
01083     runnum=0
01084     result={}
01085     qHandle=schema.newQuery()
01086     if withBXInfo and bxAlgo not in ['OCC1','OCC2','ET']:
01087         raise ValueError('unknown lumi algo '+bxAlgo)
01088     if beamstatus and beamstatus not in ['STABLE BEAMS',]:
01089         raise ValueError('unknown beam status '+beamstatus)
01090     try:
01091         if tableName is None:
01092             lls=nameDealer.lumisummaryv2TableName()
01093         else:
01094             lls=tableName
01095         qHandle.addToTableList(lls)
01096         qHandle.addToOutputList('RUNNUM','runnum')
01097         qHandle.addToOutputList('LUMILSNUM','lumilsnum')
01098         qHandle.addToOutputList('CMSLSNUM','cmslsnum')
01099         qHandle.addToOutputList('INSTLUMI','instlumi')
01100         qHandle.addToOutputList('INSTLUMIERROR','instlumierr')
01101         qHandle.addToOutputList('INSTLUMIQUALITY','instlumiqlty')
01102         qHandle.addToOutputList('BEAMSTATUS','beamstatus')
01103         qHandle.addToOutputList('BEAMENERGY','beamenergy')
01104         qHandle.addToOutputList('NUMORBIT','numorbit')
01105         qHandle.addToOutputList('STARTORBIT','startorbit')
01106         if withBXInfo:
01107             qHandle.addToOutputList('BXLUMIVALUE_'+bxAlgo,'bxvalue')
01108             qHandle.addToOutputList('BXLUMIERROR_'+bxAlgo,'bxerror')
01109         if withBeamIntensity:
01110             qHandle.addToOutputList('CMSBXINDEXBLOB','bxindexblob')
01111             qHandle.addToOutputList('BEAMINTENSITYBLOB_1','beam1intensity')
01112             qHandle.addToOutputList('BEAMINTENSITYBLOB_2','beam2intensity')
01113         
01114         qConditionStr='DATA_ID=:dataid'
01115         qCondition=coral.AttributeList()
01116         qCondition.extend('dataid','unsigned long long')
01117         qCondition['dataid'].setData(int(dataid))
01118         if beamstatus:
01119             qConditionStr+=' and BEAMSTATUS=:beamstatus'
01120             qCondition.extend('beamstatus','string')
01121             qCondition['beamstatus'].setData(beamstatus)
01122         qResult=coral.AttributeList()
01123         qResult.extend('runnum','unsigned int')
01124         qResult.extend('lumilsnum','unsigned int')
01125         qResult.extend('cmslsnum','unsigned int')
01126         qResult.extend('instlumi','float')
01127         qResult.extend('instlumierr','float')
01128         qResult.extend('instlumiqlty','short')
01129         qResult.extend('beamstatus','string')
01130         qResult.extend('beamenergy','float')
01131         qResult.extend('numorbit','unsigned int')
01132         qResult.extend('startorbit','unsigned int')
01133         if withBXInfo:
01134             qResult.extend('bxvalue','blob')
01135             qResult.extend('bxerror','blob')          
01136         if withBeamIntensity:
01137             qResult.extend('bxindexblob','blob')
01138             qResult.extend('beam1intensity','blob')
01139             qResult.extend('beam2intensity','blob')
01140         qHandle.defineOutput(qResult)
01141         qHandle.setCondition(qConditionStr,qCondition)
01142         cursor=qHandle.execute()
01143         while cursor.next():
01144             runnum=cursor.currentRow()['runnum'].data()
01145             lumilsnum=cursor.currentRow()['lumilsnum'].data()
01146             cmslsnum=cursor.currentRow()['cmslsnum'].data()
01147             instlumi=cursor.currentRow()['instlumi'].data()
01148             instlumierr=cursor.currentRow()['instlumierr'].data()
01149             instlumiqlty=cursor.currentRow()['instlumiqlty'].data()
01150             bs=cursor.currentRow()['beamstatus'].data()
01151             begev=cursor.currentRow()['beamenergy'].data()
01152             numorbit=cursor.currentRow()['numorbit'].data()
01153             startorbit=cursor.currentRow()['startorbit'].data()
01154             bxinfo=None
01155             bxvalueblob=None
01156             bxerrblob=None
01157             if withBXInfo:
01158                 bxvalueblob=cursor.currentRow()['bxvalue'].data()
01159                 bxerrblob=cursor.currentRow()['bxerror'].data()
01160                 if bxvalueblob and bxerrblob:
01161                     bxvaluesArray=CommonUtil.unpackBlobtoArray(bxvalueblob,'f')
01162                     bxerrArray=CommonUtil.unpackBlobtoArray(bxerrblob,'f')
01163                     bxinfo=(bxvaluesArray,bxerrArray)
01164             bxindexblob=None
01165             beam1intensity=None
01166             beam2intensity=None
01167             beaminfo=None
01168             if withBeamIntensity:
01169                 bxindexblob=cursor.currentRow()['bxindexblob'].data()
01170                 beam1intensity=cursor.currentRow()['beam1intensity'].data()
01171                 beam2intensity=cursor.currentRow()['beam2intensity'].data()
01172                 if bxindexblob :
01173                     bxindexArray=CommonUtil.unpackBlobtoArray(bxindexblob,'h')
01174                     beam1intensityArray=CommonUtil.unpackBlobtoArray(beam1intensity,'f')
01175                     beam2intensityArray=CommonUtil.unpackBlobtoArray(beam2intensity,'f')
01176                     beaminfo=(bxindexArray,beam1intensityArray,beam2intensityArray)
01177             result[lumilsnum]=[cmslsnum,instlumi,instlumierr,instlumiqlty,bs,begev,numorbit,startorbit,bxinfo,beaminfo]
01178     except :
01179         del qHandle
01180         raise 
01181     del qHandle
01182     return (runnum,result)
01183 def beamInfoById(schema,dataid,withBeamIntensity=False,minIntensity=0.1):
01184     '''
01185     result (runnum,[(lumilsnum(0),cmslsnum(1),beamstatus(2),beamenergy(3),ncollidingbunches(4),beaminfolist(5),..])
01186          beaminfolist=[(bxidx,beam1intensity,beam2intensity)]
01187     '''
01188     runnum=0
01189     result=[]
01190     ncollidingbunches=0
01191     qHandle=schema.newQuery()
01192     try:
01193         qHandle.addToTableList(nameDealer.lumidataTableName())
01194         qHandle.addToOutputList('NCOLLIDINGBUNCHES')
01195         qConditionStr='DATA_ID=:dataid'
01196         qCondition=coral.AttributeList()
01197         qCondition.extend('dataid','unsigned long long')
01198         qCondition['dataid'].setData(dataid)
01199         qResult=coral.AttributeList()
01200         qResult.extend('NCOLLIDINGBUNCHES','unsigned int')
01201         qHandle.defineOutput(qResult)
01202         qHandle.setCondition(qConditionStr,qCondition)
01203         cursor=qHandle.execute()
01204         while cursor.next():
01205             ncollidingbunches=cursor.currentRow()['NCOLLIDINGBUNCHES'].data()
01206     except :
01207         del qHandle
01208         raise
01209     del qHandle
01210     qHandle=schema.newQuery()
01211     try:
01212        qHandle.addToTableList(nameDealer.lumisummaryv2TableName())
01213        qHandle.addToOutputList('RUNNUM')
01214        qHandle.addToOutputList('CMSLSNUM')
01215        qHandle.addToOutputList('LUMILSNUM')
01216        qHandle.addToOutputList('BEAMSTATUS')
01217        qHandle.addToOutputList('BEAMENERGY')
01218        if withBeamIntensity:
01219            qHandle.addToOutputList('CMSBXINDEXBLOB')
01220            qHandle.addToOutputList('BEAMINTENSITYBLOB_1')
01221            qHandle.addToOutputList('BEAMINTENSITYBLOB_2')
01222        qConditionStr='DATA_ID=:dataid'
01223        qCondition=coral.AttributeList()
01224        qCondition.extend('dataid','unsigned long long')
01225        qCondition['dataid'].setData(dataid)
01226        qResult=coral.AttributeList()
01227        qResult.extend('RUNNUM','unsigned int')
01228        qResult.extend('CMSLSNUM','unsigned int')
01229        qResult.extend('LUMILSNUM','unsigned int')
01230        qResult.extend('BEAMSTATUS','string')
01231        qResult.extend('BEAMENERGY','float')
01232        if withBeamIntensity:
01233            qResult.extend('BXINDEXBLOB','blob')
01234            qResult.extend('BEAM1INTENSITY','blob')
01235            qResult.extend('BEAM2INTENSITY','blob')
01236        qHandle.defineOutput(qResult)
01237        qHandle.setCondition(qConditionStr,qCondition)
01238        cursor=qHandle.execute()
01239        while cursor.next():
01240            runnum=cursor.currentRow()['RUNNUM'].data()
01241            cmslsnum=cursor.currentRow()['CMSLSNUM'].data()
01242            lumilsnum=cursor.currentRow()['LUMILSNUM'].data()
01243            beamstatus=cursor.currentRow()['BEAMSTATUS'].data()
01244            beamenergy=cursor.currentRow()['BEAMENERGY'].data()
01245            bxindexblob=None
01246            beaminfotupleList=[]
01247            if withBeamIntensity:
01248                bxindexblob=cursor.currentRow()['BXINDEXBLOB'].data()
01249                beam1intensityblob=cursor.currentRow()['BEAM1INTENSITY'].data()
01250                beam2intensityblob=cursor.currentRow()['BEAM2INTENSITY'].data()
01251                bxindexArray=None
01252                beam1intensityArray=None
01253                beam2intensityArray=None
01254                if bxindexblob:
01255                    bxindexArray=CommonUtil.unpackBlobtoArray(bxindexblob,'h')
01256                if beam1intensityblob:
01257                    beam1intensityArray=CommonUtil.unpackBlobtoArray(beam1intensityblob,'f')
01258                if beam2intensityblob:
01259                    beam2intensityArray=CommonUtil.unpackBlobtoArray(beam2intensityblob,'f')
01260                if bxindexArray and beam1intensityArray and beam2intensityArray:
01261                    for idx,bxindex in enumerate(bxindexArray):
01262                        if (beam1intensityArray[idx] and beam1intensityArray[idx]>minIntensity) or (beam2intensityArray[idx] and beam2intensityArray[idx]>minIntensity):
01263                            beaminfotuple=(bxindex,beam1intensityArray[idx],beam2intensityArray[idx])                   
01264                            beaminfotupleList.append(beaminfotuple)
01265                    del bxindexArray[:]
01266                    del beam1intensityArray[:]
01267                    del beam2intensityArray[:]           
01268            result.append((lumilsnum,cmslsnum,beamstatus,beamenergy,ncollidingbunches,beaminfotupleList))
01269     except:
01270        del qHandle
01271        raise
01272     del qHandle
01273     return (runnum,result)
01274 def lumiBXByAlgo(schema,dataid,algoname):
01275     '''
01276     result {lumilsnum:[cmslsnum,numorbit,startorbit,bxlumivalue,bxlumierr,bxlumiqlty]}
01277     '''
01278     result={}
01279     qHandle=schema.newQuery()
01280     try:
01281         qHandle.addToTableList(nameDealer.lumisummaryv2TableName())
01282         qHandle.addToOutputList('CMSLSNUM','cmslsnum')
01283         qHandle.addToOutputList('LUMILSNUM','lumilsnum')
01284         #qHandle.addToOutputList('ALGONAME','algoname')
01285         qHandle.addToOutputList('NUMORBIT','numorbit')
01286         qHandle.addToOutputList('STARTORBIT','startorbit')
01287         qHandle.addToOutputList('BXLUMIVALUE_'+algoname,'bxlumivalue')
01288         qHandle.addToOutputList('BXLUMIERROR_'+algoname,'bxlumierr')
01289         qHandle.addToOutputList('BXLUMIQUALITY_'+algoname,'bxlumiqlty')
01290         qConditionStr='DATA_ID=:dataid'
01291         qCondition=coral.AttributeList()
01292         qCondition.extend('dataid','unsigned long long')
01293         qCondition['dataid'].setData(dataid)
01294         qResult=coral.AttributeList()
01295         qResult.extend('cmslsnum','unsigned int')
01296         qResult.extend('lumilsnum','unsigned int')
01297         qResult.extend('numorbit','unsigned int')
01298         qResult.extend('startorbit','unsigned int')
01299         qResult.extend('bxlumivalue','blob')
01300         qResult.extend('bxlumierr','blob')
01301         qResult.extend('bxlumiqlty','blob')
01302         qHandle.defineOutput(qResult)
01303         qHandle.setCondition(qConditionStr,qCondition)
01304         cursor=qHandle.execute()
01305         while cursor.next():
01306             cmslsnum=cursor.currentRow()['cmslsnum'].data()
01307             lumilsnum=cursor.currentRow()['lumilsnum'].data()
01308             numorbit=cursor.currentRow()['numorbit'].data()
01309             startorbit=cursor.currentRow()['startorbit'].data()
01310             bxlumivalue=cursor.currentRow()['bxlumivalue'].data()
01311             bxlumierr=cursor.currentRow()['bxlumierr'].data()
01312             bxlumiqlty=cursor.currentRow()['bxlumiqlty'].data()
01313             if not result.has_key(algoname):
01314                 result[algoname]={}
01315             if not result[algoname].has_key(lumilsnum):
01316                 result[algoname][lumilsnum]=[]
01317             result[algoname][lumilsnum].extend([cmslsnum,numorbit,startorbit,bxlumivalue,bxlumierr,bxlumiqlty])
01318     except :
01319         del qHandle
01320         raise RuntimeError(' dataDML.lumiBXById: '+str(e)) 
01321     del qHandle
01322     return result
01323 
01324 def hltRunById(schema,dataid,hltpathname=None,hltpathpattern=None):
01325     '''
01326     result [runnum(0),datasource(1),npath(2),hltnamedict(3)]
01327     output :
01328          npath : total number of hltpath in DB
01329          hltnamedict : list of all selected paths [(hltpathidx,hltname),(hltpathidx,hltname)]
01330     '''
01331     result=[]    
01332     qHandle=schema.newQuery()
01333     runnum=None
01334     datasource=None
01335     npath=None
01336     hltnamedict=[]
01337     try:
01338         qHandle.addToTableList(nameDealer.hltdataTableName())
01339         qHandle.addToOutputList('RUNNUM','runnum')
01340         qHandle.addToOutputList('SOURCE','datasource')
01341         qHandle.addToOutputList('NPATH','npath')
01342         qHandle.addToOutputList('PATHNAMECLOB','pathnameclob')
01343         qConditionStr='DATA_ID=:dataid'
01344         qCondition=coral.AttributeList()
01345         qCondition.extend('dataid','unsigned long long')
01346         qCondition['dataid'].setData(dataid)
01347         qResult=coral.AttributeList()
01348         qResult.extend('runnum','unsigned int')
01349         qResult.extend('datasource','string')
01350         qResult.extend('npath','unsigned int')
01351         qResult.extend('pathnameclob','string')
01352         qHandle.defineOutput(qResult)
01353         qHandle.setCondition(qConditionStr,qCondition)
01354         cursor=qHandle.execute()
01355         pathnameclob=None
01356         pathnames=[]
01357         while cursor.next():
01358             runnum=cursor.currentRow()['runnum'].data()
01359             datasource=cursor.currentRow()['datasource'].data()
01360             npath=cursor.currentRow()['npath'].data()
01361             pathnameclob=cursor.currentRow()['pathnameclob'].data()
01362         if pathnameclob:
01363             pathnames=pathnameclob.split(',')
01364             for pathnameidx,hltname in enumerate(pathnames):
01365                 if hltpathname:
01366                     if hltpathname==hltname:
01367                         hltnamedict.append((pathnameidx,hltname))
01368                         break
01369                 elif hltpathpattern:
01370                     if fnmatch.fnmatch(hltname,hltpathpattern):
01371                         hltnamedict.append((pathnameidx,hltname))
01372                 #else:
01373                     #hltnamedict.append((pathnameidx,hltname))
01374         result=[runnum,datasource,npath,hltnamedict]
01375     except :
01376         del qHandle
01377         raise 
01378     del qHandle
01379     return result
01380 
01381 def hlttrgMappingByrun(schema,runnum,hltpathname=None,hltpathpattern=None):
01382     '''
01383     select m.hltpathname,m.l1seed from cmsrunsummary r,trghltmap m where r.runnum=:runnum and m.hltkey=r.hltkey and [m.hltpathname=:hltpathname] 
01384     output: {hltpath:l1seed}
01385     '''
01386     result={}
01387     queryHandle=schema.newQuery()
01388     r=nameDealer.cmsrunsummaryTableName()
01389     m=nameDealer.trghltMapTableName()
01390     if hltpathpattern and hltpathpattern in ['*','all','All','ALL']:
01391         hltpathpattern=None
01392     try:
01393         queryHandle.addToTableList(r)
01394         queryHandle.addToTableList(m)
01395         queryCondition=coral.AttributeList()
01396         queryCondition.extend('runnum','unsigned int')
01397         queryCondition['runnum'].setData(int(runnum))
01398         #queryHandle.addToOutputList(m+'.HLTKEY','hltkey')
01399         queryHandle.addToOutputList(m+'.HLTPATHNAME','hltpathname')
01400         queryHandle.addToOutputList(m+'.L1SEED','l1seed')
01401         conditionStr=r+'.RUNNUM=:runnum and '+m+'.HLTKEY='+r+'.HLTKEY'
01402         if hltpathname:
01403             hltpathpattern=None
01404             conditionStr+=' AND '+m+'.HLTPATHNAME=:hltpathname'
01405             queryCondition.extend('hltpathname','string')
01406             queryCondition['hltpathname'].setData(hltpathname)
01407         queryHandle.setCondition(conditionStr,queryCondition)
01408         queryResult=coral.AttributeList()
01409         queryResult.extend('pname','string')
01410         queryResult.extend('l1seed','string')
01411         queryHandle.defineOutput(queryResult)
01412         cursor=queryHandle.execute()
01413         while cursor.next():
01414             pname=cursor.currentRow()['pname'].data()
01415             l1seed=cursor.currentRow()['l1seed'].data()
01416             if not result.has_key(hltpathname):
01417                 if hltpathpattern:
01418                     if fnmatch.fnmatch(pname,hltpathpattern):
01419                         result[pname]=l1seed
01420                 else:
01421                     result[pname]=l1seed
01422     except :
01423         del queryHandle
01424         raise
01425     del queryHandle
01426     return result
01427 
01428 def hltLSById(schema,dataid,hltpathname=None,hltpathpattern=None,withL1Pass=False,withHLTAccept=False):
01429     '''
01430     result (runnum, {cmslsnum:[(pathname,prescale,1lpass,hltaccept)](0)]} 
01431     '''
01432     #print 'entering hltLSById '
01433     #t0=time.time()
01434     result={}
01435     hltrundata=hltRunById(schema,dataid,hltpathname=hltpathname,hltpathpattern=hltpathpattern)
01436     if not hltrundata:
01437         return result        
01438     hltnamedict=hltrundata[3]
01439     if not hltnamedict:
01440         return (hltrundata[0],{})
01441     #tt1=time.time()
01442     #print '\thltrunbyid time ',tt1-t0
01443     #tt0=time.time()
01444     qHandle=schema.newQuery()
01445     try:
01446         qHandle.addToTableList(nameDealer.lshltTableName())
01447         qHandle.addToOutputList('RUNNUM','runnum')
01448         qHandle.addToOutputList('CMSLSNUM','cmslsnum')
01449         if len(hltnamedict)!=0:
01450             qHandle.addToOutputList('PRESCALEBLOB','prescaleblob')
01451         if withL1Pass:
01452             qHandle.addToOutputList('HLTCOUNTBLOB','hltcountblob')
01453         if withHLTAccept:
01454             qHandle.addToOutputList('HLTACCEPTBLOB','hltacceptblob')
01455         qConditionStr='DATA_ID=:dataid'
01456         qCondition=coral.AttributeList()
01457         qCondition.extend('dataid','unsigned long long')
01458         qCondition['dataid'].setData(dataid)
01459         qResult=coral.AttributeList()
01460         qResult.extend('runnum','unsigned int')
01461         qResult.extend('cmslsnum','unsigned int')
01462         if len(hltnamedict)!=0:
01463             qResult.extend('prescaleblob','blob')
01464         if withL1Pass:
01465             qResult.extend('hltcountblob','blob')
01466         if withHLTAccept:
01467             qResult.extend('hltacceptblob','blob')
01468         qHandle.defineOutput(qResult)
01469         qHandle.setCondition(qConditionStr,qCondition)
01470         cursor=qHandle.execute()
01471         while cursor.next():
01472             runnum=cursor.currentRow()['runnum'].data()
01473             cmslsnum=cursor.currentRow()['cmslsnum'].data()
01474             prescaleblob=None
01475             hltcountblob=None
01476             hltacceptblob=None
01477             if len(hltnamedict)!=0:
01478                 prescaleblob=cursor.currentRow()['prescaleblob'].data()
01479             if withL1Pass:
01480                 hltcountblob=cursor.currentRow()['hltcountblob'].data()
01481             if withHLTAccept:
01482                 hltacceptblob=cursor.currentRow()['hltacceptblob'].data()
01483             if not result.has_key(cmslsnum):
01484                 result[cmslsnum]=[]
01485             pathinfo=[]
01486             prescales=None
01487             hltcounts=None
01488             hltaccepts=None
01489             if prescaleblob:
01490                 if runnum <150008: ###WORKAROUND PATCH!! because the 2010 blobs were packed as type l ###
01491                     prescales=CommonUtil.unpackBlobtoArray(prescaleblob,'l')
01492                 else:
01493                     prescales=CommonUtil.unpackBlobtoArray(prescaleblob,'I')
01494             if hltcountblob:
01495                 if runnum <150008: ###WORKAROUND PATCH!! because the 2010 blobs were packed as type l ###
01496                     hltcounts=CommonUtil.unpackBlobtoArray(hltcountblob,'l')
01497                 else:
01498                     hltcounts=CommonUtil.unpackBlobtoArray(hltcountblob,'I')
01499             if hltacceptblob:
01500                 if runnum <150008: ###WORKAROUND PATCH!! because the 2010 blobs were packed as type l ###
01501                     hltaccepts=CommonUtil.unpackBlobtoArray(hltacceptblob,'l')
01502                 else:
01503                     hltaccepts=CommonUtil.unpackBlobtoArray(hltacceptblob,'I')
01504             for (hltpathidx,thispathname) in hltnamedict:#loop over selected paths
01505                 thispresc=0
01506                 thishltcount=0
01507                 thisaccept=0
01508                 if prescales:
01509                     thispresc=prescales[hltpathidx]
01510                 if hltcounts:
01511                     thishltcount=hltcounts[hltpathidx]
01512                 if hltaccepts:
01513                     thisaccept=hltaccepts[hltpathidx]
01514                 thispathinfo=(thispathname,thispresc,thishltcount,thisaccept)
01515                 pathinfo.append(thispathinfo)
01516             result[cmslsnum]=pathinfo
01517     except :
01518         del qHandle
01519         raise
01520     del qHandle
01521     #tt1=time.time()
01522     #print '\tdb stuff time ',tt1-tt0
01523     #t1=time.time()
01524     #print 'tot hltLSById time ',t1-t0
01525     return (runnum,result)
01526 
01527 def intglumiForRange(schema,runlist):
01528     '''
01529     output: {run:intglumi_in_fb}
01530     '''
01531     result={}
01532     if not runlist:
01533         return result
01534     minrun=min(runlist)
01535     maxrun=max(runlist)
01536     qHandle=schema.newQuery()
01537     try:
01538         qHandle.addToTableList(nameDealer.intglumiv2TableName())
01539         qResult=coral.AttributeList()
01540         qResult.extend('RUNNUM','unsigned int')
01541         qResult.extend('INTGLUMI','float')
01542         qConditionStr='RUNNUM>=:minrun AND RUNNUM<=:maxrun'
01543         qCondition=coral.AttributeList()
01544         qCondition.extend('minrun','unsigned int')
01545         qCondition.extend('maxrun','unsigned int')
01546         qCondition['minrun'].setData(minrun)
01547         qCondition['maxrun'].setData(maxrun)
01548         qHandle.addToOutputList('RUNNUM')
01549         qHandle.addToOutputList('INTGLUMI')
01550         qHandle.setCondition(qConditionStr,qCondition)
01551         qHandle.defineOutput(qResult)
01552         cursor=qHandle.execute()
01553         while cursor.next():
01554             runnum=cursor.currentRow()['RUNNUM'].data()
01555             intglumi=cursor.currentRow()['INTGLUMI'].data()
01556             result[runnum]=intglumi
01557     except :
01558         del qHandle
01559         raise
01560     del qHandle
01561     return result
01562 
01563 def fillschemePatternMap(schema,lumitype):
01564     '''
01565     output:(patternStr:correctionFac)
01566     '''
01567     if lumitype not in ['PIXEL','HF']:
01568         raise ValueError('[ERROR] unsupported lumitype '+lumitype)
01569     correctorField='CORRECTIONFACTOR'
01570     if lumitype=='PIXEL':
01571         correctorField='PIXELCORRECTIONFACTOR'
01572     result={}
01573     qHandle=schema.newQuery()
01574     try:
01575         qHandle.addToTableList(nameDealer.fillschemeTableName())
01576         qResult=coral.AttributeList()
01577         qResult.extend('FILLSCHEMEPATTERN','string')
01578         qResult.extend('CORRECTIONFACTOR','float')
01579         qHandle.defineOutput(qResult)
01580         qHandle.addToOutputList('FILLSCHEMEPATTERN')
01581         qHandle.addToOutputList(correctorField)
01582         cursor=qHandle.execute()
01583         while cursor.next():
01584             fillschemePattern=cursor.currentRow()['FILLSCHEMEPATTERN'].data()
01585             afterglowfac=cursor.currentRow()['CORRECTIONFACTOR'].data()
01586             result[fillschemePattern]=afterglowfac
01587     except :
01588         del qHandle
01589         raise
01590     del qHandle
01591     return result
01592 
01593 def guessLumiDataIdByRunInBranch(schema,runnum,tablename,branchName):
01594     revlist=revisionDML.revisionsInBranchName(schema,branchName)
01595     lumientry_id=revisionDML.entryInBranch(schema,tablename,str(runnum),branchName)
01596     latestrevision=revisionDML.latestDataRevisionOfEntry(schema,tablename,lumientry_id,revlist)
01597     return latestrevision
01598         
01599 def guessTrgDataIdByRunInBranch(schema,runnum,tablename,branchName):    
01600     revlist=revisionDML.revisionsInBranchName(schema,branchName)
01601     trgentry_id=revisionDML.entryInBranch(schema,tablename,str(runnum),branchName)
01602     latestrevision=revisionDML.latestDataRevisionOfEntry(schema,tablename,trgentry_id,revlist)
01603     return latestrevision
01604 
01605 def guessHltDataIdByRunInBranch(schema,runnum,tablename,branchName):    
01606     revlist=revisionDML.revisionsInBranchName(schema,branchName)
01607     hltentry_id=revisionDML.entryInBranch(schema,tablename,str(runnum),branchName)
01608     latestrevision=revisionDML.latestDataRevisionOfEntry(schema,tablename,hltentry_id,revlist)
01609     return latestrevision
01610 
01611 def guessDataIdByRun(schema,runnum,tablename,revfilter=None):
01612     '''
01613     select max data_id of the given run. In current design, it's the most recent data of the run
01614     '''
01615     result=None
01616     ids=[]
01617     qHandle=schema.newQuery()
01618     try:
01619         qHandle.addToTableList(tablename)
01620         qHandle.addToOutputList('DATA_ID')
01621         qConditionStr='RUNNUM=:runnum '
01622         qCondition=coral.AttributeList()
01623         qCondition.extend('runnum','unsigned int')
01624         qCondition['runnum'].setData(runnum)
01625         qResult=coral.AttributeList()
01626         qResult.extend('DATA_ID','unsigned long long')
01627         qHandle.defineOutput(qResult)
01628         qHandle.setCondition(qConditionStr,qCondition)
01629         cursor=qHandle.execute()
01630         while cursor.next():
01631             dataid=cursor.currentRow()['DATA_ID'].data()
01632             ids.append(dataid)
01633     except :
01634         del qHandle
01635         raise 
01636     del qHandle
01637     if len(ids)>0 :
01638         return max(ids)
01639     else:
01640         return result
01641         
01642 def guessDataIdForRange(schema,inputRange,tablename):
01643     '''
01644     input: inputRange [run]
01645     output: {run:lumiid}
01646     select data_id,runnum from hltdata where runnum<=runmax and runnum>=:runmin 
01647     '''
01648     result={}
01649     if not inputRange : return result
01650     if len(inputRange)==1:
01651         trgid=guessDataIdByRun(schema,inputRange[0],tablename)
01652         result[inputRange[0]]=trgid
01653         return result
01654     rmin=min(inputRange)
01655     rmax=max(inputRange)
01656     result=dict.fromkeys(inputRange,None)
01657     qHandle=schema.newQuery()
01658     try:
01659         qHandle.addToTableList(tablename)
01660         qHandle.addToOutputList('DATA_ID')
01661         qHandle.addToOutputList('RUNNUM')
01662         qConditionStr='RUNNUM>=:rmin'
01663         qCondition=coral.AttributeList()
01664         qCondition.extend('rmin','unsigned int')
01665         qCondition['rmin'].setData(rmin)
01666         if rmin!=rmax:
01667             qConditionStr+=' AND RUNNUM<=:rmax'
01668             qCondition.extend('rmax','unsigned int')
01669             qCondition['rmax'].setData(rmax)
01670         qResult=coral.AttributeList()
01671         qResult.extend('DATA_ID','unsigned long long')
01672         qResult.extend('RUNNUM','unsigned int')
01673         qHandle.defineOutput(qResult)
01674         qHandle.setCondition(qConditionStr,qCondition)
01675         cursor=qHandle.execute()
01676         while cursor.next():
01677             dataid=cursor.currentRow()['DATA_ID'].data()
01678             runnum=cursor.currentRow()['RUNNUM'].data()
01679             if result.has_key(runnum):
01680                 if dataid>result[runnum]:
01681                     result[runnum]=dataid
01682     except :
01683         del qHandle
01684         raise 
01685     del qHandle
01686     return result
01687 #def guessAllDataIdByRun(schema,runnum):
01688 #    '''
01689 #    get dataids by runnumber, if there are duplicates, pick max(dataid).Bypass full version lookups
01690 #    result (lumidataid(0),trgdataid(1),hltdataid(2)) 
01691 #    '''
01692 #    lumiids=[]
01693 #    trgids=[]
01694 #    hltids=[]
01695 #    qHandle=schema.newQuery()
01696 #    try:
01697 #        qHandle.addToTableList(nameDealer.lumidataTableName(),'l')
01698 #        qHandle.addToTableList(nameDealer.trgdataTableName(),'t')
01699 #        qHandle.addToTableList(nameDealer.hltdataTableName(),'h')
01700 #        qHandle.addToOutputList('l.DATA_ID','lumidataid')
01701 #        qHandle.addToOutputList('t.DATA_ID','trgdataid')
01702 #        qHandle.addToOutputList('h.DATA_ID','hltdataid')
01703 #        qConditionStr='l.RUNNUM=t.RUNNUM and t.RUNNUM=h.RUNNUM and l.RUNNUM=:runnum '
01704 #        qCondition=coral.AttributeList()
01705 #        qCondition.extend('runnum','unsigned int')
01706 #        qCondition['runnum'].setData(runnum)
01707 #        qResult=coral.AttributeList()
01708 #        qResult.extend('lumidataid','unsigned long long')
01709 #        qResult.extend('trgdataid','unsigned long long')
01710 #        qResult.extend('hltdataid','unsigned long long')
01711 #        qHandle.defineOutput(qResult)
01712 #        qHandle.setCondition(qConditionStr,qCondition)
01713 #        cursor=qHandle.execute()
01714 #        while cursor.next():
01715 #            lumidataid=cursor.currentRow()['lumidataid'].data()
01716 #            trgdataid=cursor.currentRow()['trgdataid'].data()
01717 #            hltdataid=cursor.currentRow()['hltdataid'].data()
01718 #            lumiids.append(lumidataid)
01719 #            trgids.append(trgdataid)
01720 #            hltids.append(hltdataid)
01721 #    except :
01722 #        del qHandle
01723 #        raise 
01724 #    del qHandle
01725 #    if len(lumiids)>0 and len(trgids)>0 and len(hltids)>0:
01726 #        return (max(lumiids),max(trgids),max(hltids))
01727 #    else:
01728 #        return (None,None,None)
01729 
01730 def guessnormIdByContext(schema,amodetag,egev1):
01731     '''
01732     get norm dataids by amodetag, egev if there are duplicates, pick max(dataid).Bypass full version lookups
01733     select data_id from luminorm where amodetag=:amodetag and egev_1=:egev1   
01734     '''
01735     luminormids=[]
01736     qHandle=schema.newQuery()
01737     egevmin=egev1*0.95
01738     egevmax=egev1*1.05
01739     try:
01740         qHandle.addToTableList( nameDealer.luminormTableName() )
01741         qHandle.addToOutputList('DATA_ID','normdataid')
01742         qConditionStr='AMODETAG=:amodetag AND EGEV_1>=:egevmin AND  EGEV_1<=:egevmax'
01743         qCondition=coral.AttributeList()
01744         qCondition.extend('amodetag','string')
01745         qCondition.extend('egevmin','unsigned int')
01746         qCondition.extend('egevmax','unsigned int')
01747         qCondition['amodetag'].setData(amodetag)
01748         qCondition['egevmin'].setData(int(egevmin))
01749         qCondition['egevmax'].setData(int(egevmax))
01750         qResult=coral.AttributeList()
01751         qResult.extend('normdataid','unsigned long long')
01752         qHandle.defineOutput(qResult)
01753         qHandle.setCondition(qConditionStr,qCondition)
01754         cursor=qHandle.execute()
01755         while cursor.next():
01756             normdataid=cursor.currentRow()['normdataid'].data()
01757             luminormids.append(normdataid)
01758     except :
01759         del qHandle
01760         raise
01761     del qHandle
01762     if len(luminormids) !=0:return max(luminormids)
01763     return None
01764 
01765 def guessnormIdByName(schema,normname):
01766     '''
01767     get norm dataids by name, if there are duplicates, pick max(dataid).Bypass full version lookups
01768     select data_id from luminorms where entry_name=:normname
01769     result luminormdataid
01770     '''   
01771     luminormids=[]
01772     qHandle=schema.newQuery()
01773     try:
01774         qHandle.addToTableList( nameDealer.luminormTableName() )
01775         qHandle.addToOutputList('DATA_ID','normdataid')
01776         qConditionStr='ENTRY_NAME=:normname '
01777         qCondition=coral.AttributeList()
01778         qCondition.extend('normname','string')
01779         qCondition['normname'].setData(normname)
01780         qResult=coral.AttributeList()
01781         qResult.extend('normdataid','unsigned long long')
01782         qHandle.defineOutput(qResult)
01783         qHandle.setCondition(qConditionStr,qCondition)
01784         cursor=qHandle.execute()
01785         while cursor.next():
01786             normdataid=cursor.currentRow()['normdataid'].data()
01787             luminormids.append(normdataid)
01788     except :
01789         del qHandle
01790         raise
01791     del qHandle
01792     if len(luminormids) !=0:return max(luminormids)
01793     return None
01794 
01795 ########
01796 ########
01797 def dataentryIdByRun(schema,runnum,branchfilter):
01798     '''
01799     select el.entry_id,et.entry_id,eh.entry_id,el.revision_id,et.revision_id,eh.revision_id from lumidataentiries el,trgdataentries et,hltdataentries eh where el.name=et.name and et.name=eh.name and el.name=:entryname;
01800     check on entryrev
01801    
01802     return [lumientryid,trgentryid,hltentryid]
01803     '''
01804     result=[]
01805     qHandle=schema.newQuery()
01806     try:
01807         qHandle.addToTableList(nameDealer.entryTableName( lumidataTableName() ))
01808         qHandle.addToTableList(nameDealer.entryTableName( trgdataTableName() ))
01809         qHandle.addToTableList(nameDealer.entryTableName( hltdataTableName() ))
01810         qHandle.addToOutputList(lumidataTableName()+'.ENTRY_ID','lumientryid')
01811         qHandle.addToOutputList(trgdataTableName()+'.ENTRY_ID','trgentryid')
01812         qHandle.addToOutputList(hltdataTableName()+'.ENTRY_ID','hltentryid')
01813         qConditionStr=lumidataTableName()+'.NAME='+trgdataTableName()+'.NAME AND '+trgdataTableName()+'.NAME='+hltdataTableName()+'.NAME AND '+lumidataTableName()+'.NAME=:runnumstr'
01814         qCondition=coral.AttributeList()
01815         qCondition.extend('runnumstr','string')
01816         qCondition['runnumstr'].setData(str(runnum))
01817         qResult=coral.AttributeList()
01818         qResult.extend('lumientryid','unsigned long long')
01819         qResult.extend('trgentryid','unsigned long long')
01820         qResult.extend('hltentryid','unsigned long long')
01821         qHandle.defineOutput(qResult)
01822         qHandle.setCondition(qConditionStr,qCondition)
01823         cursor=qHandle.execute()
01824         while cursor.next():
01825             lumientryid=cursor.currentRow()['lumientryid'].data()
01826             trgentryid=cursor.currentRow()['trgentryid'].data()
01827             hltentryid=cursor.currentRow()['hltentryid'].data()
01828             if lumientryid in branchfilter and trgentryid in branchfilter and hltentryid in branchfilter:
01829                 result.extend([lumientryid,trgentryid,hltentryid])
01830     except:
01831         del qHandle
01832         raise 
01833     del qHandle
01834     return result
01835 
01836 def latestdataIdByEntry(schema,entryid,datatype,branchfilter):
01837     '''
01838     select l.data_id,rl.revision_id from lumidatatable l,lumirevisions rl where  l.data_id=rl.data_id and l.entry_id=:entryid
01839     check revision_id is in branch
01840     '''
01841     dataids=[]
01842     datatablename=''
01843     revmaptablename=''
01844     if datatype=='lumi':
01845         datatablename=nameDealer.lumidataTableName()
01846     elif datatype=='trg':
01847         datatablename=nameDealer.trgdataTableName()
01848     elif dataytpe=='hlt':
01849         tablename=nameDealer.hltdataTableName()
01850     else:
01851         raise RunTimeError('datatype '+datatype+' is not supported')
01852     revmaptablename=nameDealer.revmapTableName(datatablename)
01853     qHandle=schema.newQuery()
01854     try:
01855         qHandle.addToTableList(revmaptablename)
01856         qHandle.addToTableList(datatablename)
01857         qHandle.addToOutputList('l.DATA_ID','dataid')
01858         qHandle.addToOutputList(revmaptablename+'.REVISION_ID','revisionid')
01859         qConditionStr=datatablename+'.DATA_ID='+revmaptablename+'.DATA_ID AND '+datatablename+'.ENTRY_ID=:entryid'
01860         qCondition=coral.AttributeList()
01861         qCondition.extend('entryid','unsigned long long')
01862         qResult=coral.AttributeList()
01863         qResult.extend('dataid','unsigned long long')
01864         qResult.extend('revisionid','unsigned long long')
01865         qHandle.defineOutput(qResult)
01866         qHandle.setCondition(qConditionStr,qCondition)
01867         cursor=qHandle.execute()
01868         while cursor.next():
01869             dataid=cursor.currentRow()['dataid'].data()
01870             revisionid=cursor.currentRow()['revisionid'].data()
01871             if revisionid in branchfilter:
01872                 dataids.append(dataid)
01873     except:
01874         del qHandle
01875         raise
01876     del qHandle
01877     if len(dataids)!=0:return max(dataids)
01878     return None
01879 
01880 
01881 #=======================================================
01882 #   INSERT requires in update transaction
01883 #=======================================================
01884 def addNormToBranch(schema,normname,amodetag,norm1,egev1,optionalnormdata,branchinfo):
01885     '''
01886     input:
01887        branchinfo(normrevisionid,branchname)
01888        optionalnormdata {'norm_occ2':norm_occ2,'norm_et':norm_et,'norm_pu':norm_pu,'constfactor':constfactor}
01889     output:
01890        (revision_id,entry_id,data_id)
01891     '''
01892     #print 'branchinfo ',branchinfo
01893     norm_occ2=1.0
01894     if optionalnormdata.has_key('normOcc2'):
01895         norm_occ2=optionalnormdata['norm_occ2']
01896     norm_et=1.0
01897     if optionalnormdata.has_key('norm_et'):
01898         norm_et=optionalnormdata['norm_et']
01899     norm_pu=1.0
01900     if optionalnormdata.has_key('norm_pu'):
01901         norm_pu=optionalnormdata['norm_pu']
01902     constfactor=1.0
01903     if optionalnormdata.has_key('constfactor'):
01904         constfactor=optionalnormdata['constfactor']
01905     try:
01906         entry_id=revisionDML.entryInBranch(schema,nameDealer.luminormTableName(),normname,branchinfo[1])
01907         if entry_id is None:
01908             (revision_id,entry_id,data_id)=revisionDML.bookNewEntry(schema,nameDealer.luminormTableName())
01909             entryinfo=(revision_id,entry_id,normname,data_id)
01910             revisionDML.addEntry(schema,nameDealer.luminormTableName(),entryinfo,branchinfo)
01911         else:
01912             (revision_id,data_id)=revisionDML.bookNewRevision( schema,nameDealer.luminormTableName() )
01913             revisionDML.addRevision(schema,nameDealer.luminormTableName(),(revision_id,data_id),branchinfo)
01914         tabrowDefDict={'DATA_ID':'unsigned long long','ENTRY_ID':'unsigned long long','ENTRY_NAME':'string','AMODETAG':'string','NORM_1':'float','EGEV_1':'unsigned int','NORM_OCC2':'float','NORM_ET':'float','NORM_PU':'float','CONSTFACTOR':'float'}
01915         tabrowValueDict={'DATA_ID':data_id,'ENTRY_ID':entry_id,'ENTRY_NAME':normname,'AMODETAG':amodetag,'NORM_1':norm1,'EGEV_1':egev1,'NORM_OCC2':norm_occ2,'NORM_ET':norm_et,'NORM_PU':norm_pu,'CONSTFACTOR':constfactor}
01916         db=dbUtil.dbUtil(schema)
01917         db.insertOneRow(nameDealer.luminormTableName(),tabrowDefDict,tabrowValueDict)
01918         return (revision_id,entry_id,data_id)
01919     except :
01920         raise
01921     
01922 def addCorrToBranch(schema,corrname,a1,optionalcorrdata,branchinfo):
01923     '''
01924     input:
01925        branchinfo(corrrevisionid,branchname)
01926        optionalcorrdata {'a2':a2,'drift':drif}
01927     output:
01928        (revision_id,entry_id,data_id)
01929     '''
01930     a2=1.0
01931     if optionalcorrdata.has_key('a2'):
01932         a2=optionalcorrdata['a2']
01933     drift=1.0
01934     if optionalcorrdata.has_key('drift'):
01935         drift=optionalcorrdata['drift']
01936     try:
01937         entry_id=revisionDML.entryInBranch(schema,nameDealer.lumicorrectionsTableName(),corrname,branchinfo[1])
01938         if entry_id is None:
01939             (revision_id,entry_id,data_id)=revisionDML.bookNewEntry(schema,nameDealer.lumicorrectionsTableName())
01940             entryinfo=(revision_id,entry_id,corrname,data_id)
01941             revisionDML.addEntry(schema,nameDealer.lumicorrectionsTableName(),entryinfo,branchinfo)
01942         else:
01943             (revision_id,data_id)=revisionDML.bookNewRevision( schema,nameDealer.lumicorrectionsTableName() )
01944             revisionDML.addRevision(schema,nameDealer.lumicorrectionsTableName(),(revision_id,data_id),branchinfo)
01945         tabrowDefDict={'DATA_ID':'unsigned long long','ENTRY_ID':'unsigned long long','ENTRY_NAME':'string','A1':'float','A2':'float','DRIFT':'float'}
01946         tabrowValueDict={'DATA_ID':data_id,'ENTRY_ID':entry_id,'ENTRY_NAME':corrname,'A1':a1,'A2':a2,'DRIFT':drift}
01947         db=dbUtil.dbUtil(schema)
01948         db.insertOneRow(nameDealer.lumicorrectionsTableName(),tabrowDefDict,tabrowValueDict)
01949         return (revision_id,entry_id,data_id)
01950     except :
01951         raise
01952 
01953 def addLumiRunDataToBranch(schema,runnumber,lumirundata,branchinfo,tableName):
01954     '''
01955     input:
01956           lumirundata [datasource,nominalenergy,ncollidingbunches,starttime,stoptime,nls]
01957           branchinfo (branch_id,branch_name)
01958           tableName lumiruntablename
01959     output:
01960           (revision_id,entry_id,data_id)
01961     '''
01962     try:
01963         datasource=lumirundata[0]
01964         nominalegev=3500.0
01965         ncollidingbunches=0
01966         starttime=coral.TimeStamp()
01967         stoptime=coral.TimeStamp()
01968         nls=0
01969         if len(lumirundata)>1:
01970             nominalenergy=lumirundata[1]
01971             ncollidingbunches=lumirundata[2]
01972             starttime=lumirundata[3]
01973             stoptime=lumirundata[4]
01974             nls=lumirundata[5]
01975         entry_id=revisionDML.entryInBranch(schema,tableName,str(runnumber),branchinfo[1])
01976         if entry_id is None:
01977             (revision_id,entry_id,data_id)=revisionDML.bookNewEntry(schema,tableName)
01978             entryinfo=(revision_id,entry_id,str(runnumber),data_id)
01979             revisionDML.addEntry(schema,tableName,entryinfo,branchinfo)
01980         else:
01981             (revision_id,data_id)=revisionDML.bookNewRevision(schema,tableName)
01982             #print 'revision_id,data_id ',revision_id,data_id
01983             revisionDML.addRevision(schema,tableName,(revision_id,data_id),branchinfo)
01984         tabrowDefDict={'DATA_ID':'unsigned long long','ENTRY_ID':'unsigned long long','ENTRY_NAME':'string','RUNNUM':'unsigned int','SOURCE':'string','NOMINALEGEV':'float','NCOLLIDINGBUNCHES':'unsigned int','STARTTIME':'time stamp','STOPTIME':'time stamp','NLS':'unsigned int'}
01985         tabrowValueDict={'DATA_ID':data_id,'ENTRY_ID':entry_id,'ENTRY_NAME':str(runnumber),'RUNNUM':int(runnumber),'SOURCE':datasource,'NOMINALEGEV':nominalegev,'NCOLLIDINGBUNCHES':ncollidingbunches,'STARTTIME':starttime,'STOPTIME':stoptime,'NLS':nls}
01986         db=dbUtil.dbUtil(schema)
01987         db.insertOneRow(tableName,tabrowDefDict,tabrowValueDict)
01988         return (revision_id,entry_id,data_id)
01989     except :
01990         raise
01991 
01992 def addTrgRunDataToBranch(schema,runnumber,trgrundata,branchinfo):
01993     '''
01994     input:
01995        trgrundata [datasource(0),bitzeroname(1),bitnameclob(2)]
01996        bitnames clob, bitnames separated by ','
01997     output:
01998        (revision_id,entry_id,data_id)
01999     '''
02000     try:   #fixme: need to consider revision only case
02001         datasource=trgrundata[0]
02002         bitzeroname=trgrundata[1]
02003         bitnames=trgrundata[2]
02004         entry_id=revisionDML.entryInBranch(schema,nameDealer.trgdataTableName(),str(runnumber),branchinfo[1])
02005         if entry_id is None:
02006             (revision_id,entry_id,data_id)=revisionDML.bookNewEntry(schema,nameDealer.trgdataTableName())
02007             entryinfo=(revision_id,entry_id,str(runnumber),data_id)
02008             revisionDML.addEntry(schema,nameDealer.trgdataTableName(),entryinfo,branchinfo)
02009         else:
02010             (revision_id,data_id)=revisionDML.bookNewRevision( schema,nameDealer.trgdataTableName() )
02011             revisionDML.addRevision(schema,nameDealer.trgdataTableName(),(revision_id,data_id),branchinfo)
02012         tabrowDefDict={'DATA_ID':'unsigned long long','ENTRY_ID':'unsigned long long','ENTRY_NAME':'string','SOURCE':'string','RUNNUM':'unsigned int','BITZERONAME':'string','BITNAMECLOB':'string'}
02013         tabrowValueDict={'DATA_ID':data_id,'ENTRY_ID':entry_id,'ENTRY_NAME':str(runnumber),'SOURCE':datasource,'RUNNUM':int(runnumber),'BITZERONAME':bitzeroname,'BITNAMECLOB':bitnames}
02014         db=dbUtil.dbUtil(schema)
02015         db.insertOneRow(nameDealer.trgdataTableName(),tabrowDefDict,tabrowValueDict)
02016         return (revision_id,entry_id,data_id)
02017     except :
02018         raise
02019 def addHLTRunDataToBranch(schema,runnumber,hltrundata,branchinfo):
02020     '''
02021     input:
02022         hltrundata [pathnameclob(0),datasource(1)]
02023     output:
02024         (revision_id,entry_id,data_id)
02025     '''
02026     try:
02027          pathnames=hltrundata[0]
02028          datasource=hltrundata[1]
02029          npath=len(pathnames.split(','))
02030          entry_id=revisionDML.entryInBranch(schema,nameDealer.hltdataTableName(),str(runnumber),branchinfo[1])
02031          if entry_id is None:
02032              (revision_id,entry_id,data_id)=revisionDML.bookNewEntry(schema,nameDealer.hltdataTableName())
02033              entryinfo=(revision_id,entry_id,str(runnumber),data_id)
02034              revisionDML.addEntry(schema,nameDealer.hltdataTableName(),entryinfo,branchinfo)
02035          else:
02036              (revision_id,data_id)=revisionDML.bookNewRevision( schema,nameDealer.hltdataTableName() )
02037              revisionDML.addRevision(schema,nameDealer.hltdataTableName(),(revision_id,data_id),branchinfo)
02038          tabrowDefDict={'DATA_ID':'unsigned long long','ENTRY_ID':'unsigned long long','ENTRY_NAME':'string','RUNNUM':'unsigned int','SOURCE':'string','NPATH':'unsigned int','PATHNAMECLOB':'string'}
02039          tabrowValueDict={'DATA_ID':data_id,'ENTRY_ID':entry_id,'ENTRY_NAME':str(runnumber),'RUNNUM':int(runnumber),'SOURCE':datasource,'NPATH':npath,'PATHNAMECLOB':pathnames}
02040          db=dbUtil.dbUtil(schema)
02041          db.insertOneRow(nameDealer.hltdataTableName(),tabrowDefDict,tabrowValueDict)
02042          return (revision_id,entry_id,data_id)
02043     except :
02044         raise 
02045 
02046 def insertRunSummaryData(schema,runnumber,runsummarydata,complementalOnly=False):
02047     '''
02048     input:
02049         runsummarydata [l1key,amodetag,egev,sequence,hltkey,fillnum,starttime,stoptime]
02050     output:
02051     '''
02052     l1key=runsummarydata[0]
02053     amodetag=runsummarydata[1]
02054     egev=runsummarydata[2]
02055     hltkey=''
02056     fillnum=0
02057     sequence=''
02058     starttime=''
02059     stoptime=''
02060     if not complementalOnly:
02061         sequence=runsummarydata[3]
02062         hltkey=runsummarydata[4]
02063         fillnum=runsummarydata[5]
02064         starttime=runsummarydata[6]
02065         stoptime=runsummarydata[7]
02066     try:
02067         if not complementalOnly:
02068             tabrowDefDict={'RUNNUM':'unsigned int','L1KEY':'string','AMODETAG':'string','EGEV':'unsigned int','SEQUENCE':'string','HLTKEY':'string','FILLNUM':'unsigned int','STARTTIME':'time stamp','STOPTIME':'time stamp'}
02069             tabrowValueDict={'RUNNUM':int(runnumber),'L1KEY':l1key,'AMODETAG':amodetag,'EGEV':int(egev),'SEQUENCE':sequence,'HLTKEY':hltkey,'FILLNUM':int(fillnum),'STARTTIME':starttime,'STOPTIME':stoptime}
02070             db=dbUtil.dbUtil(schema)
02071             db.insertOneRow(nameDealer.cmsrunsummaryTableName(),tabrowDefDict,tabrowValueDict)
02072         else:
02073             setClause='L1KEY=:l1key,AMODETAG=:amodetag,EGEV=:egev'
02074             updateCondition='RUNNUM=:runnum'
02075             inputData=coral.AttributeList()
02076             inputData.extend('l1key','string')
02077             inputData.extend('amodetag','string')
02078             inputData.extend('egev','unsigned int')
02079             inputData.extend('runnum','unsigned int')
02080             inputData['l1key'].setData(l1key)
02081             inputData['amodetag'].setData(amodetag)
02082             inputData['egev'].setData(int(egev))
02083             inputData['runnum'].setData(int(runnumber))
02084             db=dbUtil.dbUtil(schema)
02085             db.singleUpdate(nameDealer.cmsrunsummaryTableName(),setClause,updateCondition,inputData)
02086     except :
02087         raise   
02088 def insertTrgHltMap(schema,hltkey,trghltmap):
02089     '''
02090     input:
02091         trghltmap {hltpath:l1seed}
02092     output:
02093     '''
02094     hltkeyExists=False
02095     nrows=0
02096     try:
02097         kQueryBindList=coral.AttributeList()
02098         kQueryBindList.extend('hltkey','string')
02099         kQuery=schema.newQuery()
02100         kQuery.addToTableList(nameDealer.trghltMapTableName())
02101         kQuery.setCondition('HLTKEY=:hltkey',kQueryBindList)
02102         kQueryBindList['hltkey'].setData(hltkey)
02103         kResult=kQuery.execute()
02104         while kResult.next():
02105             hltkeyExists=True
02106         if not hltkeyExists:
02107             bulkvalues=[]   
02108             trghltDefDict=[('HLTKEY','string'),('HLTPATHNAME','string'),('L1SEED','string')]
02109             for hltpath,l1seed in trghltmap.items():
02110                 bulkvalues.append([('HLTKEY',hltkey),('HLTPATHNAME',hltpath),('L1SEED',l1seed)])
02111             db=dbUtil.dbUtil(schema)
02112             db.bulkInsert(nameDealer.trghltMapTableName(),trghltDefDict,bulkvalues)
02113             nrows=len(bulkvalues)
02114         return nrows
02115     except :
02116         print 'error in insertTrgHltMap '
02117         raise
02118 def bulkInsertTrgLSData(session,runnumber,data_id,trglsdata,bulksize=500):
02119     '''
02120     insert trg per-LS data for given run and data_id, this operation can be split in transaction chuncks 
02121     input:
02122         trglsdata {cmslsnum:[deadtime,bitzerocount,bitzeroprescale,trgcountBlob,trgprescaleBlob]}
02123     result nrows inserted
02124     if nrows==0, then this insertion failed
02125     '''
02126     print 'total number of trg rows ',len(trglsdata)
02127     lstrgDefDict=[('DATA_ID','unsigned long long'),('RUNNUM','unsigned int'),('CMSLSNUM','unsigned int'),('DEADTIMECOUNT','unsigned long long'),('BITZEROCOUNT','unsigned int'),('BITZEROPRESCALE','unsigned int'),('PRESCALEBLOB','blob'),('TRGCOUNTBLOB','blob')]
02128     committedrows=0
02129     nrows=0
02130     bulkvalues=[]
02131     try:
02132         for cmslsnum,perlstrg in trglsdata.items():
02133             deadtimecount=perlstrg[0]           
02134             bitzerocount=perlstrg[1]
02135             bitzeroprescale=perlstrg[2]
02136             trgcountblob=perlstrg[3]
02137             trgprescaleblob=perlstrg[4]
02138             bulkvalues.append([('DATA_ID',data_id),('RUNNUM',runnumber),('CMSLSNUM',cmslsnum),('DEADTIMECOUNT',deadtimecount),('BITZEROCOUNT',bitzerocount),('BITZEROPRESCALE',bitzeroprescale),('PRESCALEBLOB',trgprescaleblob),('TRGCOUNTBLOB',trgcountblob)])
02139             nrows+=1
02140             committedrows+=1
02141             if nrows==bulksize:
02142                 print 'committing trg in LS chunck ',nrows
02143                 db=dbUtil.dbUtil(session.nominalSchema())
02144                 session.transaction().start(False)
02145                 db.bulkInsert(nameDealer.lstrgTableName(),lstrgDefDict,bulkvalues)
02146                 session.transaction().commit()
02147                 nrows=0
02148                 bulkvalues=[]
02149             elif committedrows==len(trglsdata):
02150                 print 'committing trg at the end '
02151                 db=dbUtil.dbUtil(session.nominalSchema())
02152                 session.transaction().start(False)
02153                 db.bulkInsert(nameDealer.lstrgTableName(),lstrgDefDict,bulkvalues)
02154                 session.transaction().commit()
02155     except :
02156         print 'error in bulkInsertTrgLSData'
02157         raise 
02158 def bulkInsertHltLSData(session,runnumber,data_id,hltlsdata,bulksize=500):
02159     '''
02160     input:
02161     hltlsdata {cmslsnum:[inputcountBlob,acceptcountBlob,prescaleBlob]}
02162     '''
02163     print 'total number of hlt rows ',len(hltlsdata)
02164     lshltDefDict=[('DATA_ID','unsigned long long'),('RUNNUM','unsigned int'),('CMSLSNUM','unsigned int'),('PRESCALEBLOB','blob'),('HLTCOUNTBLOB','blob'),('HLTACCEPTBLOB','blob')]
02165     committedrows=0
02166     nrows=0
02167     bulkvalues=[]   
02168     try:             
02169         for cmslsnum,perlshlt in hltlsdata.items():
02170             inputcountblob=perlshlt[0]
02171             acceptcountblob=perlshlt[1]
02172             prescaleblob=perlshlt[2]
02173             bulkvalues.append([('DATA_ID',data_id),('RUNNUM',runnumber),('CMSLSNUM',cmslsnum),('PRESCALEBLOB',prescaleblob),('HLTCOUNTBLOB',inputcountblob),('HLTACCEPTBLOB',acceptcountblob)])
02174             
02175             nrows+=1
02176             committedrows+=1
02177             if nrows==bulksize:
02178                 print 'committing hlt in LS chunck ',nrows
02179                 db=dbUtil.dbUtil(session.nominalSchema())
02180                 session.transaction().start(False)
02181                 db.bulkInsert(nameDealer.lshltTableName(),lshltDefDict,bulkvalues)
02182                 session.transaction().commit()
02183                 nrows=0
02184                 bulkvalues=[]
02185             elif committedrows==len(hltlsdata):
02186                 print 'committing hlt at the end '
02187                 db=dbUtil.dbUtil(session.nominalSchema())
02188                 session.transaction().start(False)
02189                 db.bulkInsert(nameDealer.lshltTableName(),lshltDefDict,bulkvalues)
02190                 session.transaction().commit()
02191     except  :
02192         print 'error in bulkInsertHltLSData'
02193         raise 
02194     
02195 def bulkInsertLumiLSSummary(session,runnumber,data_id,lumilsdata,tableName,bulksize=500,withDetails=True):
02196     '''
02197     input:
02198           lumilsdata {lumilsnum:[cmslsnum,instlumi,instlumierror,instlumiquality,beamstatus,beamenergy,numorbit,startorbit,cmsbxindexblob,beam1intensity,beam2intensity,bxlumivalue_occ1,bxlumierror_occ1,bxlumiquality_occ1,bxlumivalue_occ2,bxlumierror_occ2,bxlumiquality_occ2,bxlumivalue_et,bxlumierror_et,bxlumiquality_et]}
02199     '''
02200     lslumiDefDict=[]
02201     if withDetails:
02202         lslumiDefDict=[('DATA_ID','unsigned long long'),('RUNNUM','unsigned int'),('LUMILSNUM','unsigned int'),('CMSLSNUM','unsigned int'),('INSTLUMI','float'),('INSTLUMIERROR','float'),('INSTLUMIQUALITY','short'),('BEAMSTATUS','string'),('BEAMENERGY','float'),('NUMORBIT','unsigned int'),('STARTORBIT','unsigned int'),('CMSBXINDEXBLOB','blob'),('BEAMINTENSITYBLOB_1','blob'),('BEAMINTENSITYBLOB_2','blob'),('BXLUMIVALUE_OCC1','blob'),('BXLUMIERROR_OCC1','blob'),('BXLUMIQUALITY_OCC1','blob'),('BXLUMIVALUE_OCC2','blob'),('BXLUMIERROR_OCC2','blob'),('BXLUMIQUALITY_OCC2','blob'),('BXLUMIVALUE_ET','blob'),('BXLUMIERROR_ET','blob'),('BXLUMIQUALITY_ET','blob')]
02203     else:
02204         lslumiDefDict=[('DATA_ID','unsigned long long'),('RUNNUM','unsigned int'),('LUMILSNUM','unsigned int'),('CMSLSNUM','unsigned int'),('INSTLUMI','float'),('INSTLUMIERROR','float'),('INSTLUMIQUALITY','short'),('BEAMSTATUS','string'),('BEAMENERGY','float'),('NUMORBIT','unsigned int'),('STARTORBIT','unsigned int')]
02205     print 'total number of lumi rows ',len(lumilsdata)
02206     try:
02207         committedrows=0
02208         nrows=0
02209         bulkvalues=[]
02210         for lumilsnum,perlslumi in lumilsdata.items():
02211             cmslsnum=perlslumi[0]
02212             instlumi=perlslumi[1]
02213             instlumierror=perlslumi[2]
02214             instlumiquality=perlslumi[3]
02215             beamstatus=perlslumi[4]
02216             beamenergy=perlslumi[5]
02217             numorbit=perlslumi[6]
02218             startorbit=perlslumi[7]
02219             if withDetails:
02220                 cmsbxindexindexblob=perlslumi[8]
02221                 beam1intensity=perlslumi[9]
02222                 beam2intensity=perlslumi[10]
02223                 bxlumivalue_occ1=perlslumi[11]
02224                 bxlumierror_occ1=perlslumi[12]
02225                 bxlumiquality_occ1=perlslumi[13]
02226                 bxlumivalue_occ2=perlslumi[14]
02227                 bxlumierror_occ2=perlslumi[15]
02228                 bxlumiquality_occ2=perlslumi[16]
02229                 bxlumivalue_et=perlslumi[17]
02230                 bxlumierror_et=perlslumi[18]
02231                 bxlumiquality_et=perlslumi[19]
02232                 bulkvalues.append([('DATA_ID',data_id),('RUNNUM',runnumber),('LUMILSNUM',lumilsnum),('CMSLSNUM',cmslsnum),('INSTLUMI',instlumi),('INSTLUMIERROR',instlumierror),('INSTLUMIQUALITY',instlumiquality),('BEAMSTATUS',beamstatus),('BEAMENERGY',beamenergy),('NUMORBIT',numorbit),('STARTORBIT',startorbit),('CMSBXINDEXBLOB',cmsbxindexindexblob),('BEAMINTENSITYBLOB_1',beam1intensity),('BEAMINTENSITYBLOB_2',beam2intensity),('BXLUMIVALUE_OCC1',bxlumivalue_occ1),('BXLUMIERROR_OCC1',bxlumierror_occ1),('BXLUMIQUALITY_OCC1',bxlumiquality_occ1),('BXLUMIVALUE_OCC2',bxlumivalue_occ2),('BXLUMIERROR_OCC2',bxlumierror_occ2),('BXLUMIQUALITY_OCC2',bxlumiquality_occ2),('BXLUMIVALUE_ET',bxlumivalue_et),('BXLUMIERROR_ET',bxlumierror_et),('BXLUMIQUALITY_ET',bxlumiquality_et)])
02233             else:
02234                 bulkvalues.append([('DATA_ID',data_id),('RUNNUM',runnumber),('LUMILSNUM',lumilsnum),('CMSLSNUM',cmslsnum),('INSTLUMI',instlumi),('INSTLUMIERROR',instlumierror),('INSTLUMIQUALITY',instlumiquality),('BEAMSTATUS',beamstatus),('BEAMENERGY',beamenergy),('NUMORBIT',numorbit),('STARTORBIT',startorbit)])
02235             nrows+=1
02236             committedrows+=1
02237             if nrows==bulksize:
02238                 print 'committing lumi in LS chunck ',nrows
02239                 db=dbUtil.dbUtil(session.nominalSchema())
02240                 session.transaction().start(False)
02241                 db.bulkInsert(tableName,lslumiDefDict,bulkvalues)
02242                 session.transaction().commit()
02243                 nrows=0
02244                 bulkvalues=[]
02245             elif committedrows==len(lumilsdata):
02246                 print 'committing lumi at the end '
02247                 db=dbUtil.dbUtil(session.nominalSchema())
02248                 session.transaction().start(False)
02249                 db.bulkInsert(tableName,lslumiDefDict,bulkvalues)
02250                 session.transaction().commit()
02251     except :
02252         raise
02253 
02254 #def insertLumiLSDetail(schema,runnumber,data_id,lumibxdata):
02255 #    '''
02256 #    input:
02257 #          lumibxdata [(algoname,{lumilsnum:[cmslsnum,bxlumivalue,bxlumierror,bxlumiquality]}),(algoname,{lumilsnum:[cmslsnum,bxlumivalue,bxlumierror,bxlumiquality]}),(algoname,{lumilsnum:[cmslsnum,bxlumivalue,bxlumierror,bxlumiquality]})]
02258 #    output:
02259 #          nrows
02260 #    '''
02261 #    try:
02262 #        nrow=0
02263 #        bulkvalues=[]
02264 #        lslumiDefDict=[('DATA_ID','unsigned long long'),('RUNNUM','unsigned int'),('LUMILSNUM','unsigned int'),('CMSLSNUM','unsigned int'),('ALGONAME','string'),('BXLUMIVALUE','blob'),('BXLUMIERROR','blob'),('BXLUMIQUALITY','blob')]
02265 #        for (algoname,peralgobxdata) in lumibxdata:
02266 #            for lumilsnum,bxdata in peralgobxdata.items():
02267 #                cmslsnum=bxdata[0]
02268 #                bxlumivalue=bxdata[1]
02269 #                bxlumierror=bxdata[2]
02270 #                bxlumiquality=bxdata[3]
02271 #                bulkvalues.append([('DATA_ID',data_id),('RUNNUM',runnumber),('LUMILSNUM',lumilsnum),('CMSLSNUM',cmslsnum),('ALGONAME',algoname),('BXLUMIVALUE',bxlumivalue),('BXLUMIERROR',bxlumierror),('BXLUMIQUALITY',bxlumiquality)])
02272 #        db=dbUtil.dbUtil(schema)
02273 #        db.bulkInsert(nameDealer.lumidetailTableName(),lslumiDefDict,bulkvalues)
02274 #        return len(bulkvalues)
02275 #    except:
02276 #        raise 
02277     
02278 #def completeOldLumiData(schema,runnumber,lsdata,data_id):
02279 #    '''
02280 #    input:
02281 #    lsdata [[lumisummary_id,lumilsnum,cmslsnum]]
02282 #    '''
02283 #    try:
02284 #        #update in lumisummary table
02285 #        #print 'insert in lumisummary table'
02286 #        setClause='DATA_ID=:data_id'
02287 #        updateCondition='RUNNUM=:runnum AND DATA_ID is NULL'
02288 #        updateData=coral.AttributeList()
02289 #        updateData.extend('data_id','unsigned long long')
02290 #        updateData.extend('runnum','unsigned int')
02291 #        updateData['data_id'].setData(data_id)
02292 #        updateData['runnum'].setData(int(runnumber))
02293 #        db=dbUtil.dbUtil(schema)
02294 #        db.singleUpdate(nameDealer.lumisummaryTableName(),setClause,updateCondition,updateData)
02295 #        #updates in lumidetail table
02296 #        updateAction='DATA_ID=:data_id,RUNNUM=:runnum,CMSLSNUM=:cmslsnum,LUMILSNUM=:lumilsnum'
02297 #        updateCondition='LUMISUMMARY_ID=:lumisummary_id'
02298 #        bindvarDef=[]
02299 #        bindvarDef.append(('data_id','unsigned long long'))
02300 #        bindvarDef.append(('runnum','unsigned int'))
02301 #        bindvarDef.append(('cmslsnum','unsigned int'))
02302 #        bindvarDef.append(('lumilsnum','unsigned int'))        
02303 #        inputData=[]
02304 #        for [lumisummary_id,lumilsnum,cmslsnum] in lsdata:
02305 #            inputData.append([('data_id',data_id),('runnum',int(runnumber)),('cmslsnum',cmslsnum),('lumilsnum',lumilsnum)])
02306 #        db.updateRows(nameDealer.lumidetailTableName(),updateAction,updateCondition,bindvarDef,inputData)
02307 #    except:
02308 #        raise
02309     
02310 #=======================================================
02311 #   DELETE
02312 #=======================================================
02313 
02314 
02315 #=======================================================
02316 #   Unit Test
02317 #=======================================================
02318 if __name__ == "__main__":
02319     import sessionManager
02320     import lumidbDDL,revisionDML,generateDummyData
02321     #myconstr='sqlite_file:test2.db'
02322     myconstr='oracle://devdb10/cms_xiezhen_dev'
02323     svc=sessionManager.sessionManager(myconstr,authpath='/afs/cern.ch/user/x/xiezhen',debugON=False)
02324     session=svc.openSession(isReadOnly=False,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')])
02325     schema=session.nominalSchema()
02326     session.transaction().start(False)
02327     lumidbDDL.dropTables(schema,nameDealer.schemaV2Tables())
02328     lumidbDDL.dropTables(schema,nameDealer.commonTables())
02329     tables=lumidbDDL.createTables(schema)
02330     try:
02331     #    #lumidbDDL.createUniqueConstraints(schema)
02332         trunkinfo=revisionDML.createBranch(schema,'TRUNK',None,comment='main')
02333         print trunkinfo
02334         datainfo=revisionDML.createBranch(schema,'DATA','TRUNK',comment='hold data')
02335         print datainfo
02336         norminfo=revisionDML.createBranch(schema,'NORM','TRUNK',comment='hold normalization factor')
02337         print norminfo
02338     except:
02339         raise
02340         #print 'branch already exists, do nothing'
02341     (normbranchid,normbranchparent)=revisionDML.branchInfoByName(schema,'NORM')
02342     normbranchinfo=(normbranchid,'NORM')
02343     addNormToBranch(schema,'pp7TeV','PROTPHYS',6370.0,3500,{},normbranchinfo)
02344     addNormToBranch(schema,'hi7TeV','HIPHYS',2.38,3500,{},normbranchinfo)
02345     (branchid,branchparent)=revisionDML.branchInfoByName(schema,'DATA')
02346     branchinfo=(branchid,'DATA')
02347     for runnum in [1200,1211,1222,1233,1345]:
02348         runsummarydata=generateDummyData.runsummary(schema,'PROTPHYS',3500)
02349         insertRunSummaryData(schema,runnum,runsummarydata)
02350         hlttrgmap=generateDummyData.hlttrgmap(schema)
02351         insertTrgHltMap(schema,hlttrgmap[0],hlttrgmap[1])
02352         lumidummydata=generateDummyData.lumiSummary(schema,20)
02353         lumirundata=[lumidummydata[0]]
02354         lumilsdata=lumidummydata[1]
02355         (lumirevid,lumientryid,lumidataid)=addLumiRunDataToBranch(schema,runnum,lumirundata,branchinfo)
02356         insertLumiLSSummary(schema,runnum,lumidataid,lumilsdata)
02357         trgdata=generateDummyData.trg(schema,20)        
02358         trgrundata=[trgdata[0],trgdata[1],trgdata[2]]
02359         trglsdata=trgdata[3]
02360         (trgrevid,trgentryid,trgdataid)=addTrgRunDataToBranch(schema,runnum,trgrundata,branchinfo)
02361         insertTrgLSData(schema,runnum,trgdataid,trglsdata)        
02362         hltdata=generateDummyData.hlt(schema,20)
02363         hltrundata=[hltdata[0],hltdata[1]]
02364         hltlsdata=hltdata[2]
02365         (hltrevid,hltentryid,hltdataid)=addHLTRunDataToBranch(schema,runnum,hltrundata,branchinfo)
02366         insertHltLSData(schema,runnum,hltdataid,hltlsdata)
02367     session.transaction().commit()
02368     print 'test reading'
02369     session.transaction().start(True)
02370     print '===inspecting NORM by name==='
02371     normrevlist=revisionDML.revisionsInBranchName(schema,'NORM')
02372     luminormentry_id=revisionDML.entryInBranch(schema,nameDealer.luminormTableName(),'pp7TeV','NORM')
02373     latestNorms=revisionDML.latestDataRevisionOfEntry(schema,nameDealer.luminormTableName(),luminormentry_id,normrevlist)
02374     print 'latest norm data_id for pp7TeV ',latestNorms
02375     
02376     print '===inspecting DATA branch==='
02377     print revisionDML.branchType(schema,'DATA')
02378     revlist=revisionDML.revisionsInBranchName(schema,'DATA')
02379     print revlist
02380     lumientry_id=revisionDML.entryInBranch(schema,nameDealer.lumidataTableName(),'1211','DATA')
02381     latestrevision=revisionDML.latestDataRevisionOfEntry(schema,nameDealer.lumidataTableName(),lumientry_id,revlist)
02382     print 'latest lumi data_id for run 1211 ',latestrevision
02383     lumientry_id=revisionDML.entryInBranch(schema,nameDealer.lumidataTableName(),'1222','DATA')
02384     latestrevision=revisionDML.latestDataRevisionOfEntry(schema,nameDealer.lumidataTableName(),lumientry_id,revlist)
02385     print 'latest lumi data_id for run 1222 ',latestrevision
02386     trgentry_id=revisionDML.entryInBranch(schema,nameDealer.trgdataTableName(),'1222','DATA')
02387     latestrevision=revisionDML.latestDataRevisionOfEntry(schema,nameDealer.trgdataTableName(),trgentry_id,revlist)
02388     print 'latest trg data_id for run 1222 ',latestrevision
02389     session.transaction().commit()
02390     print 'tagging data so far as data_orig'
02391     session.transaction().start(False)
02392     (revisionid,parentid,parentname)=revisionDML.createBranch(schema,'data_orig','DATA',comment='tag of 2010data')
02393     session.transaction().commit()
02394     session.transaction().start(True)
02395     print revisionDML.branchType(schema,'data_orig')
02396     revlist=revisionDML.revisionsInTag(schema,revisionid,branchinfo[0])
02397     print revlist
02398     session.transaction().commit()
02399     session.transaction().start(False)
02400     for runnum in [1200,1222]:
02401         print 'revising lumidata for run ',runnum
02402         lumidummydata=generateDummyData.lumiSummary(schema,20)
02403         lumirundata=[lumidummydata[0]]
02404         lumilsdata=lumidummydata[1]
02405         (lumirevid,lumientryid,lumidataid)=addLumiRunDataToBranch(schema,runnum,lumirundata,branchinfo)
02406         insertLumiLSSummary(schema,runnum,lumidataid,lumilsdata)
02407     revlist=revisionDML.revisionsInTag(schema,revisionid,branchinfo[0])
02408     print 'revisions in branch DATA',revisionDML.revisionsInBranch(schema,branchinfo[0])
02409     session.transaction().commit()
02410     #print 'revisions in tag data_orig ',revlist
02411     
02412     print '===test reading==='
02413     session.transaction().start(True)
02414     #print 'guess norm by name'
02415     #normid1=guessnormIdByName(schema,'pp7TeV')
02416     #print 'normid1 ',normid1
02417     #normid2=guessnormIdByContext(schema,'PROTPHYS',3500)
02418     #print 'guess norm of PROTPHYS 3500'
02419     #print 'normid2 ',normid2
02420     #normid=normid2
02421     #(lumidataid,trgdataid,hltdataid)=guessDataIdByRun(schema,1200)
02422     #print 'normid,lumiid,trgid,hltid ',normid,lumidataid,trgdataid,hltdataid
02423     #print 'lumi norm'
02424     #print luminormById(schema,normid)
02425     #print 'runinfo '
02426     #print runsummary(schema,runnum,session.properties().flavorName())
02427     #print 'lumirun '
02428     #print lumiRunById(schema,lumidataid)
02429     #print 'lumisummary'
02430     #print lumiLSById(schema,lumidataid)
02431     #print 'beam info'
02432     #print beamInfoById(schema,lumidataid)
02433     #print 'lumibx by algo OCC1'
02434     #print lumiBXByAlgo(schema,lumidataid,'OCC1')
02435     print 'trg run, trgdataid ',trgdataid
02436     print trgRunById(schema,trgdataid,withblobdata=True)  
02437     #print 'trg ls'
02438     #print trgLSById(schema,trgdataid)
02439     #print 'hlt run'
02440     #print hltRunById(schema,hltdataid)
02441     #print 'hlt ls'
02442     #print hltLSById(schema,hltdataid)
02443     session.transaction().commit()
02444     del session