CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/src/RecoLuminosity/LumiDB/scripts/lumiNorm.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 #########################################################################
00004 # Command to manage func/version/validity of lumi corrections in lumiDB #
00005 #                                                                       #
00006 # Author:      Zhen Xie                                                 #
00007 #########################################################################
00008 
00009 import os,sys
00010 from RecoLuminosity.LumiDB import normDML,revisionDML,argparse,sessionManager,lumiReport,normFileParser,CommonUtil
00011 
00012 ##############################
00013 ## ######################## ##
00014 ## ## ################## ## ##
00015 ## ## ## Main Program ## ## ##
00016 ## ## ################## ## ##
00017 ## ######################## ##
00018 ##############################
00019 
00020 if __name__ == '__main__':
00021     parser=argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description="Luminosity normalization/correction management tool",formatter_class=argparse.ArgumentDefaultsHelpFormatter)
00022     allowedActions=['list','create','insert','setdefault','unsetdefault']
00023     parser.add_argument('action',choices=allowedActions,help='command actions')
00024     parser.add_argument('-c',
00025                         dest='connect',
00026                         action='store',
00027                         required=False,
00028                         help='connect string to lumiDB,optional',default='frontier://LumiCalc/CMS_LUMI_PROD')
00029     parser.add_argument('-P',
00030                         dest='authpath',
00031                         action='store',
00032                         help='path to authentication file,optional')
00033     parser.add_argument('--name',
00034                         dest='normname',
00035                         action='store',
00036                         help='norm name')
00037     parser.add_argument('--lumitype',
00038                         dest='lumitype',
00039                         action='store',
00040                         help='lumitype')
00041     parser.add_argument('-f',
00042                         dest='normfile',
00043                         action='store',
00044                         help='norm definition file. Required for all update actions')
00045     parser.add_argument('--siteconfpath',
00046                         dest='siteconfpath',
00047                         action='store',
00048                         help='specific path to site-local-config.xml file, optional. If path undefined, fallback to cern proxy&server')
00049     parser.add_argument('--firstsince',
00050                         dest='firstsince',
00051                         action='store',
00052                         default=None,
00053                         help='pick only the pieces with since>=firstsince to insert')
00054     parser.add_argument('--debug',
00055                         dest='debug',
00056                         action='store_true',
00057                         help='debug')
00058     options=parser.parse_args()
00059     if options.authpath:
00060         os.environ['CORAL_AUTH_PATH']=options.authpath
00061     #############################################################
00062     #pre-check option compatibility
00063     #############################################################
00064     if options.action in ['create','insert','setdefault','unsetdefault']:
00065         if not options.connect:
00066             raise RuntimeError('argument -c connect is required for create/insert/updatedefault action')
00067         if not options.authpath:
00068             raise RuntimeError('argument -P authpath is required for create/insert/updatedefault action')
00069         if options.action in ['create','insert']:
00070             if not options.normfile:
00071                 raise RuntimeError('argument -f normfile is required for insert action')
00072     if options.action in ['setdefault','unsetdefault']:
00073         if not options.lumitype:
00074             raise RuntimeError('argument --lumitype lumitype is required for setdefault/unsetdefault action')
00075         if not options.normname:
00076             raise RuntimeError('argument --name normname is required for setdefault/unsetdefault action')
00077     svc=sessionManager.sessionManager(options.connect,authpath=options.authpath,siteconfpath=options.siteconfpath)
00078     ############################################################
00079     #  create,insert
00080     ############################################################
00081     if options.action in ['create','insert'] :
00082         dbsession=svc.openSession(isReadOnly=False,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')])
00083         normfileparser=normFileParser.normFileParser(options.normfile)
00084         normdata=normfileparser.parse()
00085         normdefinitionDict=normdata[0]#{defoption:value}
00086         normvalues=normdata[1]        #[{dataoption:value}]         
00087         dbsession.transaction().start(False)        
00088         normname=''
00089         if options.normname:#commandline has priorty
00090             normname=options.normname
00091         else:
00092             if normdefinitionDict.has_key('name') and normdefinitionDict['name']:
00093                 normname=normdefinitionDict['name']
00094         if not normname:
00095             raise RuntimeError('[ERROR] normname undefined')
00096         lumitype='HF'
00097         if options.lumitype:
00098             lumitype=options.lumitype
00099         else:
00100             if normdefinitionDict.has_key('lumitype') and normdefinitionDict['lumitype']:
00101                 lumitype=normdefinitionDict['lumitype']
00102         istypedefault=0
00103         if normdefinitionDict.has_key('istypedefault') and normdefinitionDict['istypedefault']:
00104             istypedefault=int(normdefinitionDict['istypedefault'])
00105         commentStr=''
00106         if normdefinitionDict.has_key('comment'):
00107             commentStr=normdefinitionDict['comment']
00108             
00109         if options.action=='create':
00110             (revision_id,branch_id)=revisionDML.branchInfoByName(dbsession.nominalSchema(),'NORM')
00111             branchinfo=(revision_id,'NORM')
00112             (normrev_id,normentry_id,normdata_id)=normDML.createNorm(dbsession.nominalSchema(),normname,lumitype,istypedefault,branchinfo,comment=commentStr)
00113         else:
00114             normdata_id=normDML.normIdByName(dbsession.nominalSchema(),normname)
00115         for normvalueDict in normvalues:
00116             if not normvalueDict.has_key('corrector') or not normvalueDict['corrector']:
00117                 raise RuntimeError('parameter corrector is required for create/insert action')
00118             if not normvalueDict.has_key('since') or not normvalueDict['since']:
00119                 raise RuntimeError('parameter since is required for create/insert action')
00120             correctorStr=normvalueDict['corrector']
00121             sincerun=int(normvalueDict['since'])
00122             if options.firstsince:
00123                 if sincerun<int(options.firstsince):
00124                     continue
00125             amodetag=normvalueDict['amodetag']
00126             egev=int(normvalueDict['egev'])
00127             detailcomment=normvalueDict['comment']
00128             (correctorname,parameterlist)=CommonUtil.parselumicorrector(correctorStr)
00129             parameterDict={}
00130             for param in parameterlist:
00131                 parameterDict[param]=normvalueDict[param]
00132             normDML.insertValueToNormId(dbsession.nominalSchema(),normdata_id,sincerun,correctorStr,amodetag,egev,parameterDict,comment=detailcomment)
00133         dbsession.transaction().commit()
00134         
00135     ##############################
00136     #  setdefault/unsetdefault
00137     ##############################
00138     if options.action in ['setdefault','unsetdefault']:
00139         dbsession=svc.openSession(isReadOnly=False,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')])
00140         dbsession.transaction().start(False)      
00141         if options.action=='setdefault':
00142             normDML.promoteNormToTypeDefault(dbsession.nominalSchema(),options.normname,options.lumitype)
00143         if options.action=='unsetdefault':
00144             normDML.demoteNormFromTypeDefault(dbsession.nominalSchema(),options.normname,options.lumitype)
00145         dbsession.transaction().commit()        
00146      ##############################
00147      #  list
00148      ##############################
00149     if options.action=='list':
00150         dbsession=svc.openSession(isReadOnly=True,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')])      
00151         dbsession.transaction().start(True)
00152         if options.normname:
00153             norminfo=normDML.normInfoByName(dbsession.nominalSchema(),options.normname)
00154             normdataid=norminfo[0]
00155             normvalues=normDML.normValueById(dbsession.nominalSchema(),normdataid)
00156             lumiReport.toScreenNormDetail(options.normname,norminfo,normvalues)
00157         elif options.lumitype:
00158             luminormidmap=normDML.normIdByType(dbsession.nominalSchema(),lumitype=options.lumitype,defaultonly=False)
00159             for normname,normid in luminormidmap.items():
00160                 norminfo=normDML.normInfoByName(dbsession.nominalSchema(),normname)
00161                 normvalues=normDML.normValueById(dbsession.nominalSchema(),normid)
00162                 lumiReport.toScreenNormDetail(normname,norminfo,normvalues)
00163         else:
00164             allnorms=normDML.allNorms(dbsession.nominalSchema())
00165             lumiReport.toScreenNormSummary(allnorms)
00166         dbsession.transaction().commit()
00167     del dbsession
00168     del svc