CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/src/RecoLuminosity/LumiDB/python/normDML.py

Go to the documentation of this file.
00001 import os,coral
00002 from RecoLuminosity.LumiDB import nameDealer,dbUtil,revisionDML,CommonUtil
00003 
00004 ########################################################################
00005 # Norm/Correction/version DML API                                      #
00006 #                                                                      #
00007 # Author:      Zhen Xie                                                #
00008 ########################################################################
00009     
00010 #==============================
00011 # SELECT
00012 #==============================
00013 def allNorms(schema):
00014     '''
00015     list all lumi norms
00016     select DATA_ID,ENTRY_NAME,LUMITYPE,ISTYPEDEFAULT,COMMENT,CTIME FROM LUMINORMSV2
00017     output:
00018     {normname:[data_id,lumitype,istypedefault,comment,creationtime]}
00019     '''
00020     result={}
00021     qHandle=schema.newQuery()
00022     try:
00023         qHandle.addToTableList( nameDealer.luminormv2TableName() )
00024         qHandle.addToOutputList('DATA_ID')
00025         qHandle.addToOutputList('ENTRY_NAME')
00026         qHandle.addToOutputList('LUMITYPE')
00027         qHandle.addToOutputList('ISTYPEDEFAULT')
00028         qHandle.addToOutputList('COMMENT')
00029         qHandle.addToOutputList('TO_CHAR(CTIME,\'MM/DD/YY HH24:MI\')','creationtime')
00030         qResult=coral.AttributeList()
00031         qResult.extend('DATA_ID','unsigned long long')
00032         qResult.extend('ENTRY_NAME','string')
00033         qResult.extend('LUMITYPE','string')
00034         qResult.extend('ISTYPEDEFAULT','unsigned int')
00035         qResult.extend('COMMENT','string')
00036         qResult.extend('creationtime','string')        
00037         qHandle.defineOutput(qResult)
00038         cursor=qHandle.execute()
00039         while cursor.next():
00040             normname=cursor.currentRow()['ENTRY_NAME'].data()
00041             if not result.has_key(normname):
00042                 result[normname]=[]
00043             dataid=cursor.currentRow()['DATA_ID'].data()
00044             lumitype=cursor.currentRow()['LUMITYPE'].data()
00045             istypedefault=cursor.currentRow()['ISTYPEDEFAULT'].data()
00046             comment=''
00047             if not cursor.currentRow()['COMMENT'].isNull():
00048                 comment=cursor.currentRow()['COMMENT'].data()
00049             creationtime=cursor.currentRow()['creationtime'].data()
00050             if len(result[normname])==0:
00051                 result[normname]=[dataid,lumitype,istypedefault,comment,creationtime]
00052             elif len(result[normname])!=0 and dataid>result[normname][0]:
00053                 result[normname]=[dataid,lumitype,istypedefault,comment,creationtime]
00054     except :
00055         del qHandle
00056         raise
00057     del qHandle
00058     return result
00059 
00060 def normIdByName(schema,normname):
00061     '''
00062     select max(DATA_ID) FROM LUMINORMSV2 WHERE ENTRY_NAME=:normname
00063     '''
00064     luminormids=[]
00065     result=None
00066     qHandle=schema.newQuery()
00067     try:
00068         qHandle.addToTableList( nameDealer.luminormv2TableName() )
00069         qHandle.addToOutputList('DATA_ID')
00070         if normname:
00071             qConditionStr='ENTRY_NAME=:normname '
00072             qCondition=coral.AttributeList()
00073             qCondition.extend('normname','string')
00074             qCondition['normname'].setData(normname)
00075         qResult=coral.AttributeList()
00076         qResult.extend('DATA_ID','unsigned long long')
00077         qHandle.defineOutput(qResult)
00078         if normname:
00079             qHandle.setCondition(qConditionStr,qCondition)
00080         cursor=qHandle.execute()
00081         while cursor.next():
00082             dataid=cursor.currentRow()['DATA_ID'].data()
00083             luminormids.append(dataid)
00084     except :
00085         del qHandle
00086         raise
00087     del qHandle
00088     if len(luminormids) !=0:
00089         return max(luminormids)    
00090     return result
00091 
00092 def normIdByType(schema,lumitype='HF',defaultonly=True):
00093     '''
00094     select max(DATA_ID) FROM LUMINORMSV2 WHERE LUMITYPE=:lumitype
00095     output:
00096         luminormidmap {normname:normid}
00097     '''
00098     luminormidmap={}
00099     qHandle=schema.newQuery()
00100     try:
00101         qHandle.addToTableList( nameDealer.luminormv2TableName() )
00102         qHandle.addToOutputList('DATA_ID')
00103         qHandle.addToOutputList('ENTRY_NAME')
00104         qConditionStr='LUMITYPE=:lumitype'
00105         qCondition=coral.AttributeList()
00106         qCondition.extend('lumitype','string')
00107         qCondition['lumitype'].setData(lumitype)
00108         if defaultonly:
00109             qConditionStr+=' AND ISTYPEDEFAULT=:istypedefault'
00110             qCondition.extend('istypedefault','unsigned int')
00111             qCondition['istypedefault'].setData(int(1))
00112         qResult=coral.AttributeList()
00113         qResult.extend('DATA_ID','unsigned long long')
00114         qResult.extend('ENTRY_NAME','string')
00115         qHandle.defineOutput(qResult)
00116         qHandle.setCondition(qConditionStr,qCondition)
00117         cursor=qHandle.execute()
00118         while cursor.next():
00119             if not cursor.currentRow()['DATA_ID'].isNull():
00120                 dataid=cursor.currentRow()['DATA_ID'].data()
00121                 normname=cursor.currentRow()['ENTRY_NAME'].data()
00122                 if not luminormidmap.has_key(normname):
00123                     luminormidmap[normname]=dataid
00124                 else:
00125                     if dataid>luminormidmap[normname]:
00126                         luminormidmap[normname]=dataid
00127     except :
00128         del qHandle
00129         raise
00130     del qHandle
00131     return luminormidmap
00132 
00133 def normInfoByName(schema,normname):
00134     '''
00135     select DATA_ID,LUMITYPE,ISTYPEDEFAULT,COMMENT,TO_CHAR(CTIME,\'MM/DD/YY HH24:MI\') FROM LUMINORMS WHERE ENTRY_NAME=:normname
00136     output:
00137         [data_id[0],lumitype[1],istypedefault[2],comment[3],creationtime[4]]
00138     '''
00139     result={}
00140     qHandle=schema.newQuery()
00141     try:
00142         qHandle.addToTableList( nameDealer.luminormv2TableName() )
00143         qHandle.addToOutputList('DATA_ID')
00144         qHandle.addToOutputList('LUMITYPE')
00145         qHandle.addToOutputList('ISTYPEDEFAULT')
00146         qHandle.addToOutputList('COMMENT')
00147         qHandle.addToOutputList('TO_CHAR(CTIME,\'MM/DD/YY HH24:MI\')','ctime')
00148         qConditionStr='ENTRY_NAME=:normname'
00149         qCondition=coral.AttributeList()
00150         qCondition.extend('normname','string')
00151         qCondition['normname'].setData(normname)
00152         qResult=coral.AttributeList()
00153         qResult.extend('DATA_ID','unsigned long long')
00154         qResult.extend('LUMITYPE','string')
00155         qResult.extend('ISTYPEDEFAULT','unsigned int')
00156         qResult.extend('COMMENT','string')
00157         qResult.extend('ctime','string')
00158         qHandle.defineOutput(qResult)
00159         qHandle.setCondition(qConditionStr,qCondition)
00160         cursor=qHandle.execute()
00161         while cursor.next():
00162             if not cursor.currentRow()['DATA_ID'].isNull():
00163                 dataid=cursor.currentRow()['DATA_ID'].data()
00164             else:
00165                 continue
00166             lumitype=cursor.currentRow()['LUMITYPE'].data()
00167             istypedefault=cursor.currentRow()['ISTYPEDEFAULT'].data()
00168             comment=''
00169             if not cursor.currentRow()['COMMENT'].isNull():
00170                 comment=cursor.currentRow()['COMMENT'].data()
00171             creationtime=cursor.currentRow()['ctime'].data()
00172             if not result.has_key(dataid):
00173                 result[dataid]=[dataid,lumitype,istypedefault,comment,creationtime]
00174     except :
00175         del qHandle
00176         raise
00177     if len(result)>0:
00178         maxdataid=max(result.keys())
00179         return result[maxdataid]
00180     return result
00181 def normValueById(schema,normid):
00182     '''
00183     select * from luminormsv2data where data_id=normid
00184     output:
00185         {since:[corrector(0),{paramname:paramvalue}(1),amodetag(2),egev(3),comment(4)]}
00186     '''
00187     result={}
00188     l=nameDealer.luminormv2dataTableName()
00189     paramdict={}
00190     qHandle=schema.newQuery()
00191     try:
00192         qHandle.addToTableList(l)
00193         qConditionStr='DATA_ID=:normid'
00194         qCondition=coral.AttributeList()
00195         qCondition.extend('normid','unsigned long long')
00196         qCondition['normid'].setData(normid)
00197         qResult=coral.AttributeList()
00198         qHandle.setCondition(qConditionStr,qCondition)
00199         cursor=qHandle.execute()
00200         while cursor.next():
00201             since=cursor.currentRow()['SINCE'].data()
00202             corrector=cursor.currentRow()['CORRECTOR'].data()
00203             amodetag=cursor.currentRow()['AMODETAG'].data()
00204             nominalegev=cursor.currentRow()['NOMINALEGEV'].data()
00205             comment=''
00206             if not cursor.currentRow()['COMMENT'].isNull():
00207                 comment=cursor.currentRow()['COMMENT'].data()
00208             (correctorfunc,params)=CommonUtil.parselumicorrector(corrector)
00209             paramdict={}
00210             for param in params:
00211                 paramvalue=0.0
00212                 if not cursor.currentRow()[param.upper()].isNull():
00213                     paramvalue=cursor.currentRow()[param.upper()].data()
00214                     paramdict[param]=paramvalue
00215             result[since]=[correctorfunc,paramdict,amodetag,nominalegev,comment]
00216     except:
00217         raise
00218     return result
00219 
00220 #=======================================================
00221 #   INSERT/UPDATE requires in update transaction
00222 #=======================================================
00223 def createNorm(schema,normname,lumitype,istypedefault,branchinfo,comment=''):
00224     '''
00225     branchinfo(normrevisionid,branchname)    
00226     '''
00227     try:
00228         entry_id=revisionDML.entryInBranch(schema,nameDealer.luminormv2TableName(),normname,branchinfo[1])
00229         if entry_id is None:
00230             (revision_id,entry_id,data_id)=revisionDML.bookNewEntry(schema,nameDealer.luminormv2TableName())
00231             entryinfo=(revision_id,entry_id,normname,data_id)
00232             revisionDML.addEntry(schema,nameDealer.luminormv2TableName(),entryinfo,branchinfo)
00233         else:
00234             (revision_id,data_id)=revisionDML.bookNewRevision( schema,nameDealer.luminormv2TableName() )
00235             revisionDML.addRevision(schema,nameDealer.luminormv2TableName(),(revision_id,data_id),branchinfo)
00236         tabrowDefDict={'DATA_ID':'unsigned long long','ENTRY_ID':'unsigned long long','ENTRY_NAME':'string','LUMITYPE':'string','ISTYPEDEFAULT':'unsigned int','COMMENT':'string','CTIME':'time stamp'}
00237         tabrowValueDict={'DATA_ID':data_id,'ENTRY_ID':entry_id,'ENTRY_NAME':normname,'LUMITYPE':lumitype,'ISTYPEDEFAULT':istypedefault,'COMMENT':comment,'CTIME':coral.TimeStamp()}
00238         db=dbUtil.dbUtil(schema)
00239         db.insertOneRow(nameDealer.luminormv2TableName(),tabrowDefDict,tabrowValueDict)
00240         return (revision_id,entry_id,data_id)
00241     except :
00242         raise
00243     
00244 def demoteNormFromTypeDefault(schema,normname,lumitype):
00245     '''
00246     demote norm from typedefault to non default
00247     '''
00248     try:
00249         thisnormid=normIdByName(schema,normname)
00250         if not thisnormid:
00251             raise ValueError(normname+' does not exist, nothing to update')
00252         setClause='ISTYPEDEFAULT=0'
00253         updateCondition='DATA_ID=:thisnormid AND LUMITYPE=:lumitype'
00254         inputData=coral.AttributeList()
00255         inputData.extend('thisnormid','unsigned long long')
00256         inputData.extend('LUMITYPE','string')
00257         inputData['thisnormid'].setData(thisnormid)
00258         inputData['LUMITYPE'].setData(lumitype)
00259         db=dbUtil.dbUtil(schema)
00260         db.singleUpdate(nameDealer.luminormv2TableName(),setClause,updateCondition,inputData)
00261     except :
00262         raise
00263     
00264 def promoteNormToTypeDefault(schema,normname,lumitype):
00265     '''
00266     set the named norm as default for a given type,reset the old default if any
00267     thisnormid=normIdByName(schema,normname)
00268     olddefaultid=normIdByType(schema,lumitype=lumitype,defaultonly=True)
00269     if thisnormid:
00270         update LUMINORMSV2 set ISTYPEDEFAULT=1 where DATA_ID=:thisnormid
00271     else:
00272         raise ValueError('normname does not exist, nothing to update')
00273     if olddefaultid and olddefaultid!=thisnormid:
00274         update LUMINORMSV2 set ISTYPEDEFAULT=0 where DATA_ID=:olddefaultid
00275     '''
00276     try:
00277         thisnormid=normIdByName(schema,normname)
00278         olddefaultid=normIdByType(schema,lumitype=lumitype,defaultonly=True)
00279         if not thisnormid:
00280             raise ValueError(normname+' does not exist, nothing to update')
00281         setClause='ISTYPEDEFAULT=1'
00282         updateCondition='DATA_ID=:thisnormid'
00283         inputData=coral.AttributeList()
00284         inputData.extend('thisnormid','unsigned long long')
00285         inputData['thisnormid'].setData(thisnormid)
00286         db=dbUtil.dbUtil(schema)
00287         db.singleUpdate(nameDealer.luminormTable(),setClause,updateCondition,inputData)
00288         if olddefaultid:
00289             setClause='ISTYPEDEFAULT=0'
00290             updateCondition='DATA_ID=:olddefaultid'
00291             inputData=coral.AttributeList()
00292             inputData.extend('olddefaultid','unsigned long long')
00293             inputData['olddefaultid'].setData(olddefaultid)
00294             db=dbUtil.dbUtil(schema)
00295             db.singleUpdate(nameDealer.luminormTable(),setClause,updateCondition,inputData)
00296     except :
00297         raise
00298 def insertValueToNormId(schema,normdataid,sincerun,corrector,amodetag,egev,parameters,comment=''):
00299     '''
00300     insert into LUMINORMSV2DATA(DATA_ID,SINCERUN,CORRECTOR,...) values(normdataid,)sincerun,corrector,...);
00301     require len(parameters)>=1.
00302     input:
00303       parameterDict {'NORM_OCC1':normocc1,'NORM_OCC2':normocc2,'NORM_ET':normet,'NORM_PU':normpu,'DRIFT':drift,'A1':a1,...}
00304     output:
00305     '''
00306     if len(parameters)==0:
00307         raise ValueError('appendValueToNormId: at least one value is required')
00308     try:
00309         db=dbUtil.dbUtil(schema)
00310         tabrowDefDict={}
00311         tabrowDefDict['DATA_ID']='unsigned long long'
00312         tabrowDefDict['CORRECTOR']='string'
00313         tabrowDefDict['SINCE']='unsigned int'
00314         tabrowDefDict['AMODETAG']='string'
00315         tabrowDefDict['NOMINALEGEV']='unsigned int'
00316         tabrowDefDict['COMMENT']='string'
00317         tabrowValueDict={}
00318         tabrowValueDict['DATA_ID']=normdataid
00319         tabrowValueDict['CORRECTOR']=corrector
00320         tabrowValueDict['SINCE']=sincerun
00321         tabrowValueDict['AMODETAG']=amodetag
00322         tabrowValueDict['NOMINALEGEV']=egev
00323         tabrowValueDict['COMMENT']=comment
00324         for paramname,paramvalue in parameters.items():
00325             try:
00326                 floatparam=float(paramvalue)
00327                 tabrowDefDict[paramname.upper()]='float'
00328                 tabrowValueDict[paramname.upper()]=float(paramvalue)
00329             except ValueError:
00330                 tabrowDefDict[paramname.upper()]='string'
00331                 tabrowValueDict[paramname.upper()]=paramvalue
00332         db.insertOneRow(nameDealer.luminormv2dataTableName(),tabrowDefDict,tabrowValueDict)
00333     except:
00334         raise
00335 ################################################
00336 # copy/export/import 
00337 ################################################
00338     
00339 def exportNormValue(schema,sourcenormname,destnormname,firstsince=None,lastsince=None):
00340     '''
00341     copy specified piece of source norm to dest
00342     input:
00343        time boundary [firstsince, lastsince]
00344        if None: open 
00345     '''
00346     copysince=0
00347     if firstsince:
00348         copysince=firstsince
00349     copylastsince=4294967295
00350     if lastsince:
00351         copylastsince=lastsince
00352     try:
00353         destnormid=normIdByName(schema,destnornmae)
00354         if not destnormid:
00355             raise RuntimeError('[ERROR] destnorm does not exist')
00356         sourcenormid=normIdByName(schema,sourcenorname)
00357         if not sourcenormid:
00358             raise RuntimeError('[ERROR] sourcenorm does not exist')
00359         normvalueDict=normValueById(schema,sourcenormid) #{since:[corrector,{paramname:paramvalue},amodetag,egev,comment]}
00360         for sincerun,normvalue in normvalueDict.items():
00361             if sincerun>copysince and sincerun<copylastsince:
00362                 corrector=normvalue[0]
00363                 parameters=normvalue[1]
00364                 amodetag=normvalue[2]
00365                 egev=normvalue[3]
00366                 comment=normvalue[4]
00367                 insertValueToNormId(schema,destnormid,sincerun,corrector,amodetag,egev,comment=comment)
00368     except:
00369         raise