CMS 3D CMS Logo

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