test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
lumiNorm.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #########################################################################
4 # Command to manage func/version/validity of lumi corrections in lumiDB #
5 # #
6 # Author: Zhen Xie #
7 #########################################################################
8 
9 import os,sys
10 from RecoLuminosity.LumiDB import normDML,revisionDML,argparse,sessionManager,lumiReport,normFileParser,CommonUtil
11 
12 ##############################
13 ## ######################## ##
14 ## ## ################## ## ##
15 ## ## ## Main Program ## ## ##
16 ## ## ################## ## ##
17 ## ######################## ##
18 ##############################
19 
20 if __name__ == '__main__':
21  parser=argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description="Luminosity normalization/correction management tool",formatter_class=argparse.ArgumentDefaultsHelpFormatter)
22  allowedActions=['list','create','insert','setdefault','unsetdefault']
23  parser.add_argument('action',choices=allowedActions,help='command actions')
24  parser.add_argument('-c',
25  dest='connect',
26  action='store',
27  required=False,
28  help='connect string to lumiDB,optional',default='frontier://LumiCalc/CMS_LUMI_PROD')
29  parser.add_argument('-P',
30  dest='authpath',
31  action='store',
32  help='path to authentication file,optional')
33  parser.add_argument('--name',
34  dest='normname',
35  action='store',
36  help='norm name')
37  parser.add_argument('--lumitype',
38  dest='lumitype',
39  action='store',
40  help='lumitype')
41  parser.add_argument('-f',
42  dest='normfile',
43  action='store',
44  help='norm definition file. Required for all update actions')
45  parser.add_argument('--siteconfpath',
46  dest='siteconfpath',
47  action='store',
48  help='specific path to site-local-config.xml file, optional. If path undefined, fallback to cern proxy&server')
49  parser.add_argument('--firstsince',
50  dest='firstsince',
51  action='store',
52  default=None,
53  help='pick only the pieces with since>=firstsince to insert')
54  parser.add_argument('--debug',
55  dest='debug',
56  action='store_true',
57  help='debug')
58  options=parser.parse_args()
59  if options.authpath:
60  os.environ['CORAL_AUTH_PATH']=options.authpath
61  #############################################################
62  #pre-check option compatibility
63  #############################################################
64  if options.action in ['create','insert','setdefault','unsetdefault']:
65  if not options.connect:
66  raise RuntimeError('argument -c connect is required for create/insert/updatedefault action')
67  if not options.authpath:
68  raise RuntimeError('argument -P authpath is required for create/insert/updatedefault action')
69  if options.action in ['create','insert']:
70  if not options.normfile:
71  raise RuntimeError('argument -f normfile is required for insert action')
72  if options.action in ['setdefault','unsetdefault']:
73  if not options.lumitype:
74  raise RuntimeError('argument --lumitype lumitype is required for setdefault/unsetdefault action')
75  if not options.normname:
76  raise RuntimeError('argument --name normname is required for setdefault/unsetdefault action')
77  svc=sessionManager.sessionManager(options.connect,authpath=options.authpath,siteconfpath=options.siteconfpath)
78  ############################################################
79  # create,insert
80  ############################################################
81  if options.action in ['create','insert'] :
82  dbsession=svc.openSession(isReadOnly=False,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')])
83  normfileparser=normFileParser.normFileParser(options.normfile)
84  normdata=normfileparser.parse()
85  normdefinitionDict=normdata[0]#{defoption:value}
86  normvalues=normdata[1] #[{dataoption:value}]
87  dbsession.transaction().start(False)
88  normname=''
89  if options.normname:#commandline has priorty
90  normname=options.normname
91  else:
92  if 'name' in normdefinitionDict and normdefinitionDict['name']:
93  normname=normdefinitionDict['name']
94  if not normname:
95  raise RuntimeError('[ERROR] normname undefined')
96  lumitype='HF'
97  if options.lumitype:
98  lumitype=options.lumitype
99  else:
100  if 'lumitype' in normdefinitionDict and normdefinitionDict['lumitype']:
101  lumitype=normdefinitionDict['lumitype']
102  istypedefault=0
103  if 'istypedefault' in normdefinitionDict and normdefinitionDict['istypedefault']:
104  istypedefault=int(normdefinitionDict['istypedefault'])
105  commentStr=''
106  if 'comment' in normdefinitionDict:
107  commentStr=normdefinitionDict['comment']
108 
109  if options.action=='create':
110  (revision_id,branch_id)=revisionDML.branchInfoByName(dbsession.nominalSchema(),'NORM')
111  branchinfo=(revision_id,'NORM')
112  (normrev_id,normentry_id,normdata_id)=normDML.createNorm(dbsession.nominalSchema(),normname,lumitype,istypedefault,branchinfo,comment=commentStr)
113  else:
114  normdata_id=normDML.normIdByName(dbsession.nominalSchema(),normname)
115  for normvalueDict in normvalues:
116  if 'corrector' not in normvalueDict or not normvalueDict['corrector']:
117  raise RuntimeError('parameter corrector is required for create/insert action')
118  if 'since' not in normvalueDict or not normvalueDict['since']:
119  raise RuntimeError('parameter since is required for create/insert action')
120  correctorStr=normvalueDict['corrector']
121  sincerun=int(normvalueDict['since'])
122  if options.firstsince:
123  if sincerun<int(options.firstsince):
124  continue
125  amodetag=normvalueDict['amodetag']
126  egev=int(normvalueDict['egev'])
127  detailcomment=normvalueDict['comment']
128  (correctorname,parameterlist)=CommonUtil.parselumicorrector(correctorStr)
129  parameterDict={}
130  for param in parameterlist:
131  parameterDict[param]=normvalueDict[param]
132  normDML.insertValueToNormId(dbsession.nominalSchema(),normdata_id,sincerun,correctorStr,amodetag,egev,parameterDict,comment=detailcomment)
133  dbsession.transaction().commit()
134 
135  ##############################
136  # setdefault/unsetdefault
137  ##############################
138  if options.action in ['setdefault','unsetdefault']:
139  dbsession=svc.openSession(isReadOnly=False,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')])
140  dbsession.transaction().start(False)
141  if options.action=='setdefault':
142  normDML.promoteNormToTypeDefault(dbsession.nominalSchema(),options.normname,options.lumitype)
143  if options.action=='unsetdefault':
144  normDML.demoteNormFromTypeDefault(dbsession.nominalSchema(),options.normname,options.lumitype)
145  dbsession.transaction().commit()
146  ##############################
147  # list
148  ##############################
149  if options.action=='list':
150  dbsession=svc.openSession(isReadOnly=True,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')])
151  dbsession.transaction().start(True)
152  if options.normname:
153  norminfo=normDML.normInfoByName(dbsession.nominalSchema(),options.normname)
154  normdataid=norminfo[0]
155  normvalues=normDML.normValueById(dbsession.nominalSchema(),normdataid)
156  lumiReport.toScreenNormDetail(options.normname,norminfo,normvalues)
157  elif options.lumitype:
158  luminormidmap=normDML.normIdByType(dbsession.nominalSchema(),lumitype=options.lumitype,defaultonly=False)
159  for normname,normid in luminormidmap.items():
160  norminfo=normDML.normInfoByName(dbsession.nominalSchema(),normname)
161  normvalues=normDML.normValueById(dbsession.nominalSchema(),normid)
162  lumiReport.toScreenNormDetail(normname,norminfo,normvalues)
163  else:
164  allnorms=normDML.allNorms(dbsession.nominalSchema())
166  dbsession.transaction().commit()
167  del dbsession
168  del svc
Definition: start.py:1
def branchInfoByName
Definition: revisionDML.py:282
def allNorms
Norm/Correction/version DML API # # Author: Zhen Xie #.
Definition: normDML.py:13
def toScreenNormSummary
Definition: lumiReport.py:59
def parselumicorrector
Definition: CommonUtil.py:271
def promoteNormToTypeDefault
Definition: normDML.py:264
def demoteNormFromTypeDefault
Definition: normDML.py:244
def toScreenNormDetail
Definition: lumiReport.py:76
def normIdByType
Definition: normDML.py:92
def normInfoByName
Definition: normDML.py:133
def createNorm
Definition: normDML.py:223
def insertValueToNormId
Definition: normDML.py:298
def normValueById
Definition: normDML.py:181
norm file format spec lines beginning with a semicolon &#39;;&#39; a pound sign &#39;#&#39; or the letters &#39;REM&#39; (upp...
def normIdByName
Definition: normDML.py:60