CMS 3D CMS Logo

applyCalibration.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # update lumisummary set instlumi=instlumi*:norm where runnum>=:startrun and runnum<=:endrun
4 #
5 #
6 # update lumisummary set instlumi=instlumi*:norm where runnum=:run
7 #
8 import os,sys
9 import coral
10 from RecoLuminosity.LumiDB import argparse
11 def calibrateRange(dbsession,normfactor,startrun,endrun):
12  '''
13  update lumisummary set instlumi=instlumi*:norm where runnum>=:startrun and runnum<=:endrun
14  '''
15  try:
16  dbsession.transaction().start(False)
17  schema=dbsession.nominalSchema()
18  if not schema:
19  raise 'cannot connect to schema'
20  if not schema.existsTable('LUMISUMMARY'):
21  raise 'non-existing table LUMISUMMARY'
22  inputData=coral.AttributeList()
23  inputData.extend('normfactor','float')
24  inputData['normfactor'].setData(float(normfactor))
25  inputData.extend('startrun','unsigned int')
26  inputData['startrun'].setData(int(startrun))
27  inputData.extend('endrun','unsigned int')
28  inputData['endrun'].setData(int(endrun))
29  nchanged=schema.tableHandle('LUMISUMMARY').dataEditor().updateRows('INSTLUMI=INSTLUMI*:normfactor','RUNNUM>=:startrun AND RUNNUM<=:endrun',inputData)
30  dbsession.transaction().commit()
31  return nchanged
32  except Exception as e:
33  print str(e)
34  dbsession.transaction().rollback()
35  del dbsession
36 def calibrateRun(dbsession,normfactor,runnum):
37  '''
38  update lumisummary set instlumi=instlumi*:norm where runnum=:run
39  '''
40  try:
41  dbsession.transaction().start(False)
42  schema=dbsession.nominalSchema()
43  if not schema:
44  raise 'cannot connect to schema'
45  if not schema.existsTable('LUMISUMMARY'):
46  raise 'non-existing table LUMISUMMARY'
47  inputData=coral.AttributeList()
48  inputData.extend('normfactor','float')
49  inputData['normfactor'].setData(float(normfactor))
50  inputData.extend('runnumber','unsigned int')
51  inputData['runnumber'].setData(int(runnum))
52  nchanged=schema.tableHandle('LUMISUMMARY').dataEditor().updateRows('INSTLUMI=INSTLUMI*:normfactor','RUNNUM=:runnumber',inputData)
53  dbsession.transaction().commit()
54  return nchanged
55  except Exception as e:
56  print str(e)
57  dbsession.transaction().rollback()
58  del dbsession
59 
60 def main():
61  parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description="apply normalization factor to inst lumi")
62  parser.add_argument('-c',dest='connectstr',action='store',required=True,help='connectstr')
63  parser.add_argument('-norm',dest='normfactor',action='store',required=True,help='normalization factor to apply')
64  parser.add_argument('-P',dest='authpath',action='store',required=True,help='path to authentication file')
65  parser.add_argument('-r',dest='runnumber',action='store',required=False,help='run number')
66  parser.add_argument('-startrun',dest='startrun',action='store',required=False,help='start run for range action')
67  parser.add_argument('-endrun',dest='endrun',action='store',required=False,help='end run for range action')
68  parser.add_argument('action',choices=['run','range'],help='calibrate run')
69 
70  parser.add_argument('--debug',dest='debug',action='store_true',help='debug')
71  args=parser.parse_args()
72  connectstr=args.connectstr
73  normfactor=args.normfactor
74  if len(args.authpath)!=0:
75  os.environ['CORAL_AUTH_PATH']=args.authpath
76  else:
77  raise '-P authpath argument is required'
78  svc=coral.ConnectionService()
79  session=svc.connect(connectstr,accessMode=coral.access_Update)
80  if args.debug:
81  msg=coral.MessageStream('')
82  msg.setMsgVerbosity(coral.message_Level_Debug)
83  n=0
84  if args.action == 'run':
85  runnumber=0
86  if args.runnumber:
87  runnumber=args.runnumber
88  else:
89  raise 'argument -r is required for action run'
90  if args.debug:
91  print 'connectstr : ',connectstr
92  print 'normfactor : ',normfactor
93  print 'authpath : ',os.environ['CORAL_AUTH_PATH']
94  print 'runnumber : ',runnumber
95  n=calibrateRun(session,normfactor,runnumber)
96  if args.action == 'range':
97  startrun=0
98  endrun=0
99  if args.startrun:
100  startrun=args.startrun
101  else:
102  raise 'argument -startrun is required for action range'
103  if args.endrun:
104  endrun=args.endrun
105  else:
106  raise 'argument -endrun is required for action range'
107  if args.debug:
108  print 'connectstr : ',connectstr
109  print 'normfactor : ',normfactor
110  print 'authpath : ',os.environ['CORAL_AUTH_PATH']
111  print 'startrun : ',startrun
112  print 'endrun : ',endrun
113  n=calibrateRange(session,normfactor,startrun,endrun)
114  print 'number of rows changed: ',n
115  del session
116  del svc
117 
118 if __name__=='__main__':
119  main()
120 
Definition: start.py:1
def calibrateRun(dbsession, normfactor, runnum)
Definition: main.py:1
def calibrateRange(dbsession, normfactor, startrun, endrun)