test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
normDML.py
Go to the documentation of this file.
1 import os,coral
2 from RecoLuminosity.LumiDB import nameDealer,dbUtil,revisionDML,CommonUtil
3 
4 ########################################################################
5 # Norm/Correction/version DML API #
6 # #
7 # Author: Zhen Xie #
8 ########################################################################
9 
10 #==============================
11 # SELECT
12 #==============================
13 def allNorms(schema):
14  '''
15  list all lumi norms
16  select DATA_ID,ENTRY_NAME,LUMITYPE,ISTYPEDEFAULT,COMMENT,CTIME FROM LUMINORMSV2
17  output:
18  {normname:[data_id,lumitype,istypedefault,comment,creationtime]}
19  '''
20  result={}
21  qHandle=schema.newQuery()
22  try:
23  qHandle.addToTableList( nameDealer.luminormv2TableName() )
24  qHandle.addToOutputList('DATA_ID')
25  qHandle.addToOutputList('ENTRY_NAME')
26  qHandle.addToOutputList('LUMITYPE')
27  qHandle.addToOutputList('ISTYPEDEFAULT')
28  qHandle.addToOutputList('COMMENT')
29  qHandle.addToOutputList('TO_CHAR(CTIME,\'MM/DD/YY HH24:MI\')','creationtime')
30  qResult=coral.AttributeList()
31  qResult.extend('DATA_ID','unsigned long long')
32  qResult.extend('ENTRY_NAME','string')
33  qResult.extend('LUMITYPE','string')
34  qResult.extend('ISTYPEDEFAULT','unsigned int')
35  qResult.extend('COMMENT','string')
36  qResult.extend('creationtime','string')
37  qHandle.defineOutput(qResult)
38  cursor=qHandle.execute()
39  while cursor.next():
40  normname=cursor.currentRow()['ENTRY_NAME'].data()
41  if not result.has_key(normname):
42  result[normname]=[]
43  dataid=cursor.currentRow()['DATA_ID'].data()
44  lumitype=cursor.currentRow()['LUMITYPE'].data()
45  istypedefault=cursor.currentRow()['ISTYPEDEFAULT'].data()
46  comment=''
47  if not cursor.currentRow()['COMMENT'].isNull():
48  comment=cursor.currentRow()['COMMENT'].data()
49  creationtime=cursor.currentRow()['creationtime'].data()
50  if len(result[normname])==0:
51  result[normname]=[dataid,lumitype,istypedefault,comment,creationtime]
52  elif len(result[normname])!=0 and dataid>result[normname][0]:
53  result[normname]=[dataid,lumitype,istypedefault,comment,creationtime]
54  except :
55  del qHandle
56  raise
57  del qHandle
58  return result
59 
60 def normIdByName(schema,normname):
61  '''
62  select max(DATA_ID) FROM LUMINORMSV2 WHERE ENTRY_NAME=:normname
63  '''
64  luminormids=[]
65  result=None
66  qHandle=schema.newQuery()
67  try:
68  qHandle.addToTableList( nameDealer.luminormv2TableName() )
69  qHandle.addToOutputList('DATA_ID')
70  if normname:
71  qConditionStr='ENTRY_NAME=:normname '
72  qCondition=coral.AttributeList()
73  qCondition.extend('normname','string')
74  qCondition['normname'].setData(normname)
75  qResult=coral.AttributeList()
76  qResult.extend('DATA_ID','unsigned long long')
77  qHandle.defineOutput(qResult)
78  if normname:
79  qHandle.setCondition(qConditionStr,qCondition)
80  cursor=qHandle.execute()
81  while cursor.next():
82  dataid=cursor.currentRow()['DATA_ID'].data()
83  luminormids.append(dataid)
84  except :
85  del qHandle
86  raise
87  del qHandle
88  if len(luminormids) !=0:
89  return max(luminormids)
90  return result
91 
92 def normIdByType(schema,lumitype='HF',defaultonly=True):
93  '''
94  select max(DATA_ID) FROM LUMINORMSV2 WHERE LUMITYPE=:lumitype
95  output:
96  luminormidmap {normname:normid}
97  '''
98  luminormidmap={}
99  qHandle=schema.newQuery()
100  try:
101  qHandle.addToTableList( nameDealer.luminormv2TableName() )
102  qHandle.addToOutputList('DATA_ID')
103  qHandle.addToOutputList('ENTRY_NAME')
104  qConditionStr='LUMITYPE=:lumitype'
105  qCondition=coral.AttributeList()
106  qCondition.extend('lumitype','string')
107  qCondition['lumitype'].setData(lumitype)
108  if defaultonly:
109  qConditionStr+=' AND ISTYPEDEFAULT=:istypedefault'
110  qCondition.extend('istypedefault','unsigned int')
111  qCondition['istypedefault'].setData(int(1))
112  qResult=coral.AttributeList()
113  qResult.extend('DATA_ID','unsigned long long')
114  qResult.extend('ENTRY_NAME','string')
115  qHandle.defineOutput(qResult)
116  qHandle.setCondition(qConditionStr,qCondition)
117  cursor=qHandle.execute()
118  while cursor.next():
119  if not cursor.currentRow()['DATA_ID'].isNull():
120  dataid=cursor.currentRow()['DATA_ID'].data()
121  normname=cursor.currentRow()['ENTRY_NAME'].data()
122  if not luminormidmap.has_key(normname):
123  luminormidmap[normname]=dataid
124  else:
125  if dataid>luminormidmap[normname]:
126  luminormidmap[normname]=dataid
127  except :
128  del qHandle
129  raise
130  del qHandle
131  return luminormidmap
132 
133 def normInfoByName(schema,normname):
134  '''
135  select DATA_ID,LUMITYPE,ISTYPEDEFAULT,COMMENT,TO_CHAR(CTIME,\'MM/DD/YY HH24:MI\') FROM LUMINORMS WHERE ENTRY_NAME=:normname
136  output:
137  [data_id[0],lumitype[1],istypedefault[2],comment[3],creationtime[4]]
138  '''
139  result={}
140  qHandle=schema.newQuery()
141  try:
142  qHandle.addToTableList( nameDealer.luminormv2TableName() )
143  qHandle.addToOutputList('DATA_ID')
144  qHandle.addToOutputList('LUMITYPE')
145  qHandle.addToOutputList('ISTYPEDEFAULT')
146  qHandle.addToOutputList('COMMENT')
147  qHandle.addToOutputList('TO_CHAR(CTIME,\'MM/DD/YY HH24:MI\')','ctime')
148  qConditionStr='ENTRY_NAME=:normname'
149  qCondition=coral.AttributeList()
150  qCondition.extend('normname','string')
151  qCondition['normname'].setData(normname)
152  qResult=coral.AttributeList()
153  qResult.extend('DATA_ID','unsigned long long')
154  qResult.extend('LUMITYPE','string')
155  qResult.extend('ISTYPEDEFAULT','unsigned int')
156  qResult.extend('COMMENT','string')
157  qResult.extend('ctime','string')
158  qHandle.defineOutput(qResult)
159  qHandle.setCondition(qConditionStr,qCondition)
160  cursor=qHandle.execute()
161  while cursor.next():
162  if not cursor.currentRow()['DATA_ID'].isNull():
163  dataid=cursor.currentRow()['DATA_ID'].data()
164  else:
165  continue
166  lumitype=cursor.currentRow()['LUMITYPE'].data()
167  istypedefault=cursor.currentRow()['ISTYPEDEFAULT'].data()
168  comment=''
169  if not cursor.currentRow()['COMMENT'].isNull():
170  comment=cursor.currentRow()['COMMENT'].data()
171  creationtime=cursor.currentRow()['ctime'].data()
172  if not result.has_key(dataid):
173  result[dataid]=[dataid,lumitype,istypedefault,comment,creationtime]
174  except :
175  del qHandle
176  raise
177  if len(result)>0:
178  maxdataid=max(result.keys())
179  return result[maxdataid]
180  return result
181 def normValueById(schema,normid):
182  '''
183  select * from luminormsv2data where data_id=normid
184  output:
185  {since:[corrector(0),{paramname:paramvalue}(1),amodetag(2),egev(3),comment(4)]}
186  '''
187  result={}
189  paramdict={}
190  qHandle=schema.newQuery()
191  try:
192  qHandle.addToTableList(l)
193  qConditionStr='DATA_ID=:normid'
194  qCondition=coral.AttributeList()
195  qCondition.extend('normid','unsigned long long')
196  qCondition['normid'].setData(normid)
197  qResult=coral.AttributeList()
198  qHandle.setCondition(qConditionStr,qCondition)
199  cursor=qHandle.execute()
200  while cursor.next():
201  since=cursor.currentRow()['SINCE'].data()
202  corrector=cursor.currentRow()['CORRECTOR'].data()
203  amodetag=cursor.currentRow()['AMODETAG'].data()
204  nominalegev=cursor.currentRow()['NOMINALEGEV'].data()
205  comment=''
206  if not cursor.currentRow()['COMMENT'].isNull():
207  comment=cursor.currentRow()['COMMENT'].data()
208  (correctorfunc,params)=CommonUtil.parselumicorrector(corrector)
209  paramdict={}
210  for param in params:
211  paramvalue=0.0
212  if not cursor.currentRow()[param.upper()].isNull():
213  paramvalue=cursor.currentRow()[param.upper()].data()
214  paramdict[param]=paramvalue
215  result[since]=[correctorfunc,paramdict,amodetag,nominalegev,comment]
216  except:
217  raise
218  return result
219 
220 #=======================================================
221 # INSERT/UPDATE requires in update transaction
222 #=======================================================
223 def createNorm(schema,normname,lumitype,istypedefault,branchinfo,comment=''):
224  '''
225  branchinfo(normrevisionid,branchname)
226  '''
227  try:
228  entry_id=revisionDML.entryInBranch(schema,nameDealer.luminormv2TableName(),normname,branchinfo[1])
229  if entry_id is None:
230  (revision_id,entry_id,data_id)=revisionDML.bookNewEntry(schema,nameDealer.luminormv2TableName())
231  entryinfo=(revision_id,entry_id,normname,data_id)
232  revisionDML.addEntry(schema,nameDealer.luminormv2TableName(),entryinfo,branchinfo)
233  else:
234  (revision_id,data_id)=revisionDML.bookNewRevision( schema,nameDealer.luminormv2TableName() )
235  revisionDML.addRevision(schema,nameDealer.luminormv2TableName(),(revision_id,data_id),branchinfo)
236  tabrowDefDict={'DATA_ID':'unsigned long long','ENTRY_ID':'unsigned long long','ENTRY_NAME':'string','LUMITYPE':'string','ISTYPEDEFAULT':'unsigned int','COMMENT':'string','CTIME':'time stamp'}
237  tabrowValueDict={'DATA_ID':data_id,'ENTRY_ID':entry_id,'ENTRY_NAME':normname,'LUMITYPE':lumitype,'ISTYPEDEFAULT':istypedefault,'COMMENT':comment,'CTIME':coral.TimeStamp()}
238  db=dbUtil.dbUtil(schema)
239  db.insertOneRow(nameDealer.luminormv2TableName(),tabrowDefDict,tabrowValueDict)
240  return (revision_id,entry_id,data_id)
241  except :
242  raise
243 
244 def demoteNormFromTypeDefault(schema,normname,lumitype):
245  '''
246  demote norm from typedefault to non default
247  '''
248  try:
249  thisnormid=normIdByName(schema,normname)
250  if not thisnormid:
251  raise ValueError(normname+' does not exist, nothing to update')
252  setClause='ISTYPEDEFAULT=0'
253  updateCondition='DATA_ID=:thisnormid AND LUMITYPE=:lumitype'
254  inputData=coral.AttributeList()
255  inputData.extend('thisnormid','unsigned long long')
256  inputData.extend('LUMITYPE','string')
257  inputData['thisnormid'].setData(thisnormid)
258  inputData['LUMITYPE'].setData(lumitype)
259  db=dbUtil.dbUtil(schema)
260  db.singleUpdate(nameDealer.luminormv2TableName(),setClause,updateCondition,inputData)
261  except :
262  raise
263 
264 def promoteNormToTypeDefault(schema,normname,lumitype):
265  '''
266  set the named norm as default for a given type,reset the old default if any
267  thisnormid=normIdByName(schema,normname)
268  olddefaultid=normIdByType(schema,lumitype=lumitype,defaultonly=True)
269  if thisnormid:
270  update LUMINORMSV2 set ISTYPEDEFAULT=1 where DATA_ID=:thisnormid
271  else:
272  raise ValueError('normname does not exist, nothing to update')
273  if olddefaultid and olddefaultid!=thisnormid:
274  update LUMINORMSV2 set ISTYPEDEFAULT=0 where DATA_ID=:olddefaultid
275  '''
276  try:
277  thisnormid=normIdByName(schema,normname)
278  olddefaultid=normIdByType(schema,lumitype=lumitype,defaultonly=True)
279  if not thisnormid:
280  raise ValueError(normname+' does not exist, nothing to update')
281  setClause='ISTYPEDEFAULT=1'
282  updateCondition='DATA_ID=:thisnormid'
283  inputData=coral.AttributeList()
284  inputData.extend('thisnormid','unsigned long long')
285  inputData['thisnormid'].setData(thisnormid)
286  db=dbUtil.dbUtil(schema)
287  db.singleUpdate(nameDealer.luminormTable(),setClause,updateCondition,inputData)
288  if olddefaultid:
289  setClause='ISTYPEDEFAULT=0'
290  updateCondition='DATA_ID=:olddefaultid'
291  inputData=coral.AttributeList()
292  inputData.extend('olddefaultid','unsigned long long')
293  inputData['olddefaultid'].setData(olddefaultid)
294  db=dbUtil.dbUtil(schema)
295  db.singleUpdate(nameDealer.luminormTable(),setClause,updateCondition,inputData)
296  except :
297  raise
298 def insertValueToNormId(schema,normdataid,sincerun,corrector,amodetag,egev,parameters,comment=''):
299  '''
300  insert into LUMINORMSV2DATA(DATA_ID,SINCERUN,CORRECTOR,...) values(normdataid,)sincerun,corrector,...);
301  require len(parameters)>=1.
302  input:
303  parameterDict {'NORM_OCC1':normocc1,'NORM_OCC2':normocc2,'NORM_ET':normet,'NORM_PU':normpu,'DRIFT':drift,'A1':a1,...}
304  output:
305  '''
306  if len(parameters)==0:
307  raise ValueError('appendValueToNormId: at least one value is required')
308  try:
309  db=dbUtil.dbUtil(schema)
310  tabrowDefDict={}
311  tabrowDefDict['DATA_ID']='unsigned long long'
312  tabrowDefDict['CORRECTOR']='string'
313  tabrowDefDict['SINCE']='unsigned int'
314  tabrowDefDict['AMODETAG']='string'
315  tabrowDefDict['NOMINALEGEV']='unsigned int'
316  tabrowDefDict['COMMENT']='string'
317  tabrowValueDict={}
318  tabrowValueDict['DATA_ID']=normdataid
319  tabrowValueDict['CORRECTOR']=corrector
320  tabrowValueDict['SINCE']=sincerun
321  tabrowValueDict['AMODETAG']=amodetag
322  tabrowValueDict['NOMINALEGEV']=egev
323  tabrowValueDict['COMMENT']=comment
324  for paramname,paramvalue in parameters.items():
325  try:
326  floatparam=float(paramvalue)
327  tabrowDefDict[paramname.upper()]='float'
328  tabrowValueDict[paramname.upper()]=float(paramvalue)
329  except ValueError:
330  tabrowDefDict[paramname.upper()]='string'
331  tabrowValueDict[paramname.upper()]=paramvalue
332  db.insertOneRow(nameDealer.luminormv2dataTableName(),tabrowDefDict,tabrowValueDict)
333  except:
334  raise
335 ################################################
336 # copy/export/import
337 ################################################
338 
339 def exportNormValue(schema,sourcenormname,destnormname,firstsince=None,lastsince=None):
340  '''
341  copy specified piece of source norm to dest
342  input:
343  time boundary [firstsince, lastsince]
344  if None: open
345  '''
346  copysince=0
347  if firstsince:
348  copysince=firstsince
349  copylastsince=4294967295
350  if lastsince:
351  copylastsince=lastsince
352  try:
353  destnormid=normIdByName(schema,destnornmae)
354  if not destnormid:
355  raise RuntimeError('[ERROR] destnorm does not exist')
356  sourcenormid=normIdByName(schema,sourcenorname)
357  if not sourcenormid:
358  raise RuntimeError('[ERROR] sourcenorm does not exist')
359  normvalueDict=normValueById(schema,sourcenormid) #{since:[corrector,{paramname:paramvalue},amodetag,egev,comment]}
360  for sincerun,normvalue in normvalueDict.items():
361  if sincerun>copysince and sincerun<copylastsince:
362  corrector=normvalue[0]
363  parameters=normvalue[1]
364  amodetag=normvalue[2]
365  egev=normvalue[3]
366  comment=normvalue[4]
367  insertValueToNormId(schema,destnormid,sincerun,corrector,amodetag,egev,comment=comment)
368  except:
369  raise
def exportNormValue
copy/export/import
Definition: normDML.py:339
def allNorms
Norm/Correction/version DML API # # Author: Zhen Xie #.
Definition: normDML.py:13
def parselumicorrector
Definition: CommonUtil.py:271
def promoteNormToTypeDefault
Definition: normDML.py:264
def demoteNormFromTypeDefault
Definition: normDML.py:244
def luminormv2dataTableName
Definition: nameDealer.py:43
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 bookNewRevision
Definition: revisionDML.py:330
def bookNewEntry
Definition: revisionDML.py:316
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
def normValueById
Definition: normDML.py:181
def entryInBranch
Definition: revisionDML.py:188
def luminormv2TableName
Definition: nameDealer.py:40
def normIdByName
Definition: normDML.py:60