CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/RecoLuminosity/LumiDB/scripts/applyCalibration.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # update lumisummary set instlumi=instlumi*:norm where runnum>=:startrun and runnum<=:endrun
00004 #
00005 #
00006 # update lumisummary set instlumi=instlumi*:norm where runnum=:run 
00007 #
00008 import os,sys
00009 import coral
00010 from RecoLuminosity.LumiDB import argparse
00011 def calibrateRange(dbsession,normfactor,startrun,endrun):
00012     '''
00013     update lumisummary set instlumi=instlumi*:norm where runnum>=:startrun and runnum<=:endrun
00014     '''
00015     try:
00016         dbsession.transaction().start(False)
00017         schema=dbsession.nominalSchema()
00018         if not schema:
00019             raise 'cannot connect to schema'
00020         if not schema.existsTable('LUMISUMMARY'):
00021             raise 'non-existing table LUMISUMMARY'
00022         inputData=coral.AttributeList()
00023         inputData.extend('normfactor','float')
00024         inputData['normfactor'].setData(float(normfactor))
00025         inputData.extend('startrun','unsigned int')
00026         inputData['startrun'].setData(int(startrun))
00027         inputData.extend('endrun','unsigned int')
00028         inputData['endrun'].setData(int(endrun))
00029         nchanged=schema.tableHandle('LUMISUMMARY').dataEditor().updateRows('INSTLUMI=INSTLUMI*:normfactor','RUNNUM>=:startrun AND RUNNUM<=:endrun',inputData)
00030         dbsession.transaction().commit()
00031         return nchanged
00032     except Exception,e:
00033         print str(e)
00034         dbsession.transaction().rollback()
00035         del dbsession
00036 def calibrateRun(dbsession,normfactor,runnum):
00037     '''
00038     update lumisummary set instlumi=instlumi*:norm where runnum=:run 
00039     '''
00040     try:
00041         dbsession.transaction().start(False)
00042         schema=dbsession.nominalSchema()
00043         if not schema:
00044             raise 'cannot connect to schema'
00045         if not schema.existsTable('LUMISUMMARY'):
00046             raise 'non-existing table LUMISUMMARY'
00047         inputData=coral.AttributeList()
00048         inputData.extend('normfactor','float')
00049         inputData['normfactor'].setData(float(normfactor))
00050         inputData.extend('runnumber','unsigned int')
00051         inputData['runnumber'].setData(int(runnum))
00052         nchanged=schema.tableHandle('LUMISUMMARY').dataEditor().updateRows('INSTLUMI=INSTLUMI*:normfactor','RUNNUM=:runnumber',inputData)
00053         dbsession.transaction().commit()
00054         return nchanged
00055     except Exception,e:
00056         print str(e)
00057         dbsession.transaction().rollback()
00058         del dbsession
00059         
00060 def main():
00061     parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description="apply normalization factor to inst lumi")
00062     parser.add_argument('-c',dest='connectstr',action='store',required=True,help='connectstr')
00063     parser.add_argument('-norm',dest='normfactor',action='store',required=True,help='normalization factor to apply')
00064     parser.add_argument('-P',dest='authpath',action='store',required=True,help='path to authentication file')
00065     parser.add_argument('-r',dest='runnumber',action='store',required=False,help='run number')
00066     parser.add_argument('-startrun',dest='startrun',action='store',required=False,help='start run for range action')
00067     parser.add_argument('-endrun',dest='endrun',action='store',required=False,help='end run for range action')
00068     parser.add_argument('action',choices=['run','range'],help='calibrate run')
00069     
00070     parser.add_argument('--debug',dest='debug',action='store_true',help='debug')
00071     args=parser.parse_args()
00072     connectstr=args.connectstr
00073     normfactor=args.normfactor
00074     if len(args.authpath)!=0:
00075         os.environ['CORAL_AUTH_PATH']=args.authpath
00076     else:
00077         raise '-P authpath argument is required'
00078     svc=coral.ConnectionService()
00079     session=svc.connect(connectstr,accessMode=coral.access_Update)
00080     if args.debug:
00081         msg=coral.MessageStream('')
00082         msg.setMsgVerbosity(coral.message_Level_Debug)
00083     n=0
00084     if args.action == 'run':
00085         runnumber=0
00086         if args.runnumber:
00087             runnumber=args.runnumber
00088         else:
00089             raise 'argument -r is required for action run'
00090         if args.debug:
00091             print 'connectstr : ',connectstr
00092             print 'normfactor : ',normfactor
00093             print 'authpath : ',os.environ['CORAL_AUTH_PATH']
00094             print 'runnumber : ',runnumber
00095         n=calibrateRun(session,normfactor,runnumber)
00096     if args.action == 'range':
00097         startrun=0
00098         endrun=0
00099         if args.startrun:
00100             startrun=args.startrun
00101         else:
00102             raise 'argument -startrun is required for action range'
00103         if args.endrun:
00104             endrun=args.endrun
00105         else:
00106             raise 'argument -endrun is required for action range'
00107         if args.debug:
00108             print 'connectstr : ',connectstr
00109             print 'normfactor : ',normfactor
00110             print 'authpath : ',os.environ['CORAL_AUTH_PATH']
00111             print 'startrun : ',startrun
00112             print 'endrun : ',endrun
00113         n=calibrateRange(session,normfactor,startrun,endrun)
00114     print 'number of rows changed: ',n
00115     del session
00116     del svc
00117         
00118 if __name__=='__main__':
00119     main()
00120