CMS 3D CMS Logo

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