CMS 3D CMS Logo

dataDML.py
Go to the documentation of this file.
1 from __future__ import print_function
2 from __future__ import absolute_import
3 from builtins import range
4 import os,coral,fnmatch,time
5 from RecoLuminosity.LumiDB import nameDealer,dbUtil,revisionDML,lumiTime,CommonUtil,lumiCorrections
6 from datetime import datetime
7 import array
8 
9 
10 ########################################################################
11 # LumiDB DML API #
12 # #
13 # Author: Zhen Xie #
14 ########################################################################
15 
16 #==============================
17 # SELECT
18 #==============================
19 def guesscorrIdByName(schema,tagname=None):
20  '''
21  select data_id from lumicorrectionss [where entry_name=:tagname]
22  result lumicorrectionsdataid
23 
24  '''
25  lumicorrectionids=[]
26  result=None
27  qHandle=schema.newQuery()
28  try:
29  qHandle.addToTableList( nameDealer.lumicorrectionsTableName() )
30  qHandle.addToOutputList('DATA_ID')
31  if tagname:
32  qConditionStr='ENTRY_NAME=:tagname '
33  qCondition=coral.AttributeList()
34  qCondition.extend('tagname','string')
35  qCondition['tagname'].setData(tagname)
36  qResult=coral.AttributeList()
37  qResult.extend('DATA_ID','unsigned long long')
38  qHandle.defineOutput(qResult)
39  if tagname:
40  qHandle.setCondition(qConditionStr,qCondition)
41  cursor=qHandle.execute()
42  while next(cursor):
43  dataid=cursor.currentRow()['DATA_ID'].data()
44  lumicorrectionids.append(dataid)
45  except :
46  del qHandle
47  raise
48  del qHandle
49  if len(lumicorrectionids) !=0:return max(lumicorrectionids)
50  return result
51 
52 def lumicorrById(schema,correctiondataid):
53  '''
54  select entry_name,a1,a2,drift from lumicorrections where DATA_ID=:dataid
55  output: {tagname:(data_id(0),a1(1),a2(2),driftcoeff(3))}
56  '''
57  result=None
58  qHandle=schema.newQuery()
59  try:
60  qHandle.addToTableList(nameDealer.lumicorrectionsTableName())
61  qHandle.addToOutputList('ENTRY_NAME')
62  qHandle.addToOutputList('A1')
63  qHandle.addToOutputList('A2')
64  qHandle.addToOutputList('DRIFT')
65  qCondition=coral.AttributeList()
66  qCondition.extend('dataid','unsigned long long')
67  qCondition['dataid'].setData(correctiondataid)
68  qResult=coral.AttributeList()
69  qResult.extend('ENTRY_NAME','string')
70  qResult.extend('A1','float')
71  qResult.extend('A2','float')
72  qResult.extend('DRIFT','float')
73  qHandle.defineOutput(qResult)
74  qHandle.setCondition('DATA_ID=:dataid',qCondition)
75  cursor=qHandle.execute()
76  while next(cursor):
77  tagname=cursor.currentRow()['ENTRY_NAME'].data()
78  a1=cursor.currentRow()['A1'].data()
79  a2=0.0
80  if cursor.currentRow()['A2'].data():
81  a2=cursor.currentRow()['A2'].data()
82  drift=0.0
83  if cursor.currentRow()['DRIFT'].data():
84  drift=cursor.currentRow()['DRIFT'].data()
85  result={tagname:(correctiondataid,a1,a2,drift)}
86  except :
87  del qHandle
88  raise
89  del qHandle
90  return result
91 
92 def fillInRange(schema,fillmin,fillmax,amodetag,startT,stopT):
93  '''
94  select fillnum,runnum,starttime from cmsrunsummary where [where fillnum>=:fillmin and fillnum<=:fillmax and amodetag=:amodetag]
95  output: [fill]
96  '''
97  result=[]
98  tmpresult={}
99  qHandle=schema.newQuery()
101  lute=lumiTime.lumiTime()
102  try:
103  qHandle.addToTableList(r)
104  qConditionPieces=[]
105  qConditionStr=''
106  qCondition=coral.AttributeList()
107  if fillmin:
108  qConditionPieces.append('FILLNUM>=:fillmin')
109  qCondition.extend('fillmin','unsigned int')
110  qCondition['fillmin'].setData(int(fillmin))
111  if fillmax:
112  qConditionPieces.append('FILLNUM<=:fillmax')
113  qCondition.extend('fillmax','unsigned int')
114  qCondition['fillmax'].setData(int(fillmax))
115  if amodetag:
116  qConditionPieces.append('AMODETAG=:amodetag')
117  qCondition.extend('amodetag','string')
118  qCondition['amodetag'].setData(amodetag)
119  if len(qConditionPieces)!=0:
120  qConditionStr=(' AND ').join(qConditionPieces)
121  qResult=coral.AttributeList()
122  qResult.extend('fillnum','unsigned int')
123  qResult.extend('runnum','unsigned int')
124  qResult.extend('starttime','string')
125  qHandle.defineOutput(qResult)
126  if len(qConditionStr)!=0:
127  qHandle.setCondition(qConditionStr,qCondition)
128  qHandle.addToOutputList('FILLNUM','fillnum')
129  qHandle.addToOutputList('RUNNUM','runnum')
130  qHandle.addToOutputList('TO_CHAR('+r+'.STARTTIME,\'MM/DD/YY HH24:MI:SS\')','starttime')
131  cursor=qHandle.execute()
132  while next(cursor):
133  currentfill=cursor.currentRow()['fillnum'].data()
134  runnum=cursor.currentRow()['runnum'].data()
135  starttimeStr=cursor.currentRow()['starttime'].data()
136  runTime=lute.StrToDatetime(starttimeStr,customfm='%m/%d/%y %H:%M:%S')
137  minTime=None
138  maxTime=None
139  if startT and stopT:
140  minTime=lute.StrToDatetime(startT,customfm='%m/%d/%y %H:%M:%S')
141  maxTime=lute.StrToDatetime(stopT,customfm='%m/%d/%y %H:%M:%S')
142  if runTime>=minTime and runTime<=maxTime:
143  tmpresult.setdefault(currentfill,[]).append(runnum)
144  elif startT is not None:
145  minTime=lute.StrToDatetime(startT,customfm='%m/%d/%y %H:%M:%S')
146  if runTime>=minTime:
147  tmpresult.setdefault(currentfill,[]).append(runnum)
148  elif stopT is not None:
149  maxTime=lute.StrToDatetime(stopT,customfm='%m/%d/%y %H:%M:%S')
150  if runTime<=maxTime:
151  tmpresult.setdefault(currentfill,[]).append(runnum)
152  else:
153  tmpresult.setdefault(currentfill,[]).append(runnum)
154  #print tmpresult
155  for f in sorted(tmpresult):
156  if tmpresult[f]:
157  result.append(f)
158  except :
159  del qHandle
160  raise
161  del qHandle
162  return result
163 def fillrunMap(schema,fillnum=None,runmin=None,runmax=None,startT=None,stopT=None,l1keyPattern=None,hltkeyPattern=None,amodetag=None):
164  '''
165  select fillnum,runnum,starttime from cmsrunsummary [where fillnum=:fillnum and runnum>=runmin and runnum<=runmax and amodetag=:amodetag ]
166  output: {fill:[runnum,...]}
167  '''
168  result={}
169  timelesslist=[]
170  qHandle=schema.newQuery()
172  lute=lumiTime.lumiTime()
173  try:
174  qHandle.addToTableList(r)
175  qConditionPieces=[]
176  qConditionStr=''
177  qCondition=coral.AttributeList()
178  if fillnum:
179  qConditionPieces.append('FILLNUM=:fillnum')
180  qCondition.extend('fillnum','unsigned int')
181  qCondition['fillnum'].setData(int(fillnum))
182  if runmin:
183  qConditionPieces.append('RUNNUM>=:runmin')
184  qCondition.extend('runmin','unsigned int')
185  qCondition['runmin'].setData(runmin)
186  if runmax:
187  qConditionPieces.append('RUNNUM<=:runmax')
188  qCondition.extend('runmax','unsigned int')
189  qCondition['runmax'].setData(runmax)
190  if amodetag:
191  qConditionPieces.append('AMODETAG=:amodetag')
192  qCondition.extend('amodetag','string')
193  qCondition['amodetag'].setData(amodetag)
194  if l1keyPattern:
195  qConditionPieces.append('regexp_like(L1KEY,:l1keypattern)')
196  qCondition.extend('l1keypattern','string')
197  qCondition['l1keypattern'].setData(l1keyPattern)
198  if hltkeyPattern:
199  qConditionPieces.append('regexp_like(HLTKEY,:hltkeypattern)')
200  qCondition.extend('hltkeypattern','string')
201  qCondition['hltkeypattern'].setData(hltkeyPattern)
202  if len(qConditionPieces)!=0:
203  qConditionStr=(' AND ').join(qConditionPieces)
204  qResult=coral.AttributeList()
205  qResult.extend('fillnum','unsigned int')
206  qResult.extend('runnum','unsigned int')
207  qResult.extend('starttime','string')
208  qHandle.defineOutput(qResult)
209  if len(qConditionStr) !=0:
210  qHandle.setCondition(qConditionStr,qCondition)
211  qHandle.addToOutputList('FILLNUM','fillnum')
212  qHandle.addToOutputList('RUNNUM','runnum')
213  qHandle.addToOutputList('TO_CHAR('+r+'.STARTTIME,\'MM/DD/YY HH24:MI:SS\')','starttime')
214  cursor=qHandle.execute()
215  while next(cursor):
216  currentfill=cursor.currentRow()['fillnum'].data()
217  starttimeStr=cursor.currentRow()['starttime'].data()
218  runnum=cursor.currentRow()['runnum'].data()
219  runTime=lute.StrToDatetime(starttimeStr,customfm='%m/%d/%y %H:%M:%S')
220  minTime=None
221  maxTime=None
222  if startT and stopT:
223  minTime=lute.StrToDatetime(startT,customfm='%m/%d/%y %H:%M:%S')
224  maxTime=lute.StrToDatetime(stopT,customfm='%m/%d/%y %H:%M:%S')
225  if runTime>=minTime and runTime<=maxTime:
226  result.setdefault(currentfill,[]).append(runnum)
227  elif startT is not None:
228  minTime=lute.StrToDatetime(startT,customfm='%m/%d/%y %H:%M:%S')
229  if runTime>=minTime:
230  result.setdefault(currentfill,[]).append(runnum)
231  elif stopT is not None:
232  maxTime=lute.StrToDatetime(stopT,customfm='%m/%d/%y %H:%M:%S')
233  if runTime<=maxTime:
234  result.setdefault(currentfill,[]).append(runnum)
235  else:
236  result.setdefault(currentfill,[]).append(runnum)
237  except :
238  del qHandle
239  raise
240  del qHandle
241  return result
242 
243 def runList(schema,datatagid,runmin=None,runmax=None,fillmin=None,fillmax=None,startT=None,stopT=None,l1keyPattern=None,hltkeyPattern=None,amodetag=None,nominalEnergy=None,energyFlut=0.2,requiretrg=True,requirehlt=True,preselectedruns=None,lumitype=None):
244  '''
245  select r.runnum,l.starttime,l.stoptime,l.data_id,tag.trgdataid,tag.hltdataid from cmsrunsummary r,tagruns tag,lumidata l,trgdata t,hltdata h where l.runnum=tag.runnum and r.runnum=l.runnum (and l.runnum=t.runnum and t.runnum=h.runnum) and r.fillnum>=:fillmin and r.fillnum<=fillmax and r.runnum>:runmin and r.runnum<:runmax and r.amodetag=:amodetag (and regexp_like(r.l1key,:l1keypattern) and regexp_like(hltkey,:hltkeypattern) ) and l.nominalEnergy>=:nominalEnergy*(1-energyFlut) and l.nominalEnergy<=:nominalEnergy*(1+energyFlut) and tag.tagid<=:tagid and l.starttime is not null and l.stoptime is not null
246  output: {runnum:[lumiid,trgid,hltid]}
247  '''
248  #print datatagid,runmin,runmax,fillmin,fillmax,preselectedruns
249  if lumitype not in ['HF','PIXEL']:
250  raise ValueError('unknown lumitype '+lumitype)
251  lumitableName=''
252  tagrunstablename=''
253  if lumitype=='HF':
254  lumitableName=nameDealer.lumidataTableName()
255  tagrunstablename=nameDealer.tagRunsTableName()
256  elif lumitype == 'PIXEL':
257  lumitableName = nameDealer.pixellumidataTableName()
258  tagrunstablename=nameDealer.pixeltagRunsTableName()
259  else:
260  assert False, "ERROR Unknown lumitype '%s'" % lumitype
261 
262  result={}#{runnum,[[lumiid,trgid,hltid]]}
263  qHandle=schema.newQuery()
265  l=lumitableName
266  tag=tagrunstablename
269  lute=lumiTime.lumiTime()
270  try:
271  qHandle.addToTableList(r)
272  qHandle.addToTableList(l)
273  qHandle.addToTableList(tag)
274  qConditionStr=r+'.RUNNUM='+l+'.RUNNUM AND '+tag+'.RUNNUM='+l+'.RUNNUM AND '+tag+'.TAGID<=:tagid'
275  qCondition=coral.AttributeList()
276  qCondition.extend('tagid','unsigned long long')
277  qCondition['tagid'].setData(datatagid)
278  #if requiretrg:
279  # qHandle.addToTableList(t)
280  # qConditionStr+=' and '+l+'.RUNNUM='+t+'.RUNNUM'
281  #if requirehlt:
282  # qHandle.addToTableList(h)
283  # qConditionStr+=' and '+l+'.RUNNUM='+h+'.RUNNUM'
284  if runmin and runmax :
285  if runmin==runmax:
286  qConditionStr+=' AND '+r+'.RUNNUM=:runmin'
287  qCondition.extend('runmin','unsigned int')
288  qCondition['runmin'].setData(int(runmin))
289  elif runmax>runmin:
290  qConditionStr+=' AND '+r+'.RUNNUM>=:runmin AND '+r+'.RUNNUM<=:runmax'
291  qCondition.extend('runmin','unsigned int')
292  qCondition.extend('runmax','unsigned int')
293  qCondition['runmin'].setData(int(runmin))
294  qCondition['runmax'].setData(int(runmax))
295  else:
296  raise 'runmin > runmax'
297  elif runmin:
298  qConditionStr+=' AND '+r+'.RUNNUM>=:runmin'
299  qCondition.extend('runmin','unsigned int')
300  qCondition['runmin'].setData(int(runmin))
301  elif runmax:
302  qConditionStr+=' AND '+r+'.RUNNUM<=:runmax'
303  qCondition.extend('runmax','unsigned int')
304  qCondition['runmax'].setData(int(runmax))
305  else:
306  pass
307  if fillmin and fillmax:
308  if fillmin==fillmax:
309  qConditionStr+=' AND '+r+'.FILLNUM=:fillnum'
310  qCondition.extend('fillnum','unsigned int')
311  qCondition['fillnum'].setData(int(fillmin))
312  elif fillmax>fillmin:
313  qConditionStr+=' AND '+r+'.FILLNUM>=:fillmin AND '+r+'.FILLNUM<=:fillmax'
314  qCondition.extend('fillmin','unsigned int')
315  qCondition.extend('fillmax','unsigned int')
316  qCondition['fillmin'].setData(int(fillmin))
317  qCondition['fillmax'].setData(int(fillmax))
318  else:
319  raise 'fillmin > fillmax'
320  if amodetag:
321  qConditionStr+=' AND '+r+'.AMODETAG=:amodetag'
322  qCondition.extend('amodetag','string')
323  qCondition['amodetag'].setData(amodetag)
324  if l1keyPattern:
325  qHandle.addToTableList(t)
326  qConditionStr+=' AND regexp_like('+r+'.L1KEY,:l1keypattern )'+' AND '+l+'.RUNNUM='+t+'.RUNNUM'
327  qCondition.extend('l1keypattern','string')
328  qCondition['l1keypattern'].setData(l1keyPattern)
329  if hltkeyPattern:
330  qHandle.addToTableList(h)
331  qConditionStr+=' AND regexp_like('+r+'.HLTKEY,:hltkeypattern)'+' AND '+l+'.RUNNUM='+h+'.RUNNUM'
332  qCondition.extend('hltkeypattern','string')
333  qCondition['hltkeypattern'].setData(hltkeyPattern)
334  if nominalEnergy:
335  emin=nominalEnergy*(1.0-energyFlut)
336  emax=nominalEnergy*(1.0+energyFlut)
337  qConditionStr+=' AND '+l+'.NOMINALEGEV>=:emin and '+l+'.NOMINALEGEV<=:emax'
338  qCondition.extend('emin','float')
339  qCondition.extend('emax','float')
340  qCondition['emin'].setData(emin)
341  qCondition['emax'].setData(emax)
342  qResult=coral.AttributeList()
343  qResult.extend('runnum','unsigned int')
344  qResult.extend('starttime','string')
345  qResult.extend('stoptime','string')
346  qResult.extend('lumiid','unsigned long long')
347  if requiretrg:
348  qResult.extend('trgid','unsigned long long')
349  if requirehlt:
350  qResult.extend('hltid','unsigned long long')
351  qHandle.setCondition(qConditionStr,qCondition)
352  qHandle.addToOutputList(r+'.RUNNUM','runnum')
353  qHandle.addToOutputList('TO_CHAR('+l+'.STARTTIME,\'MM/DD/YY HH24:MI:SS\')','starttime')
354  qHandle.addToOutputList('TO_CHAR('+l+'.STOPTIME,\'MM/DD/YY HH24:MI:SS\')','stoptime')
355  qHandle.addToOutputList(l+'.DATA_ID','lumiid')
356  #if requiretrg:
357  # qHandle.addToOutputList(t+'.DATA_ID','trgid')
358  #if requirehlt:
359  # qHandle.addToOutputList(h+'.DATA_ID','hltid')
360  if requiretrg:
361  qHandle.addToOutputList(tag+'.TRGDATAID','trgid')
362  if requirehlt:
363  qHandle.addToOutputList(tag+'.HLTDATAID','hltid')
364  qHandle.defineOutput(qResult)
365  cursor=qHandle.execute()
366  lumiid=0
367  trgid=0
368  hltid=0
369  while next(cursor):
370  runnum=cursor.currentRow()['runnum'].data()
371  if preselectedruns and runnum not in preselectedruns:
372  continue
373  if cursor.currentRow()['starttime'].isNull():
374  continue
375  if cursor.currentRow()['stoptime'].isNull():
376  continue
377  starttimeStr=cursor.currentRow()['starttime'].data()
378  stoptimeStr=cursor.currentRow()['stoptime'].data()
379  runstartTime=lute.StrToDatetime(starttimeStr,customfm='%m/%d/%y %H:%M:%S')
380  runstopTime=lute.StrToDatetime(stoptimeStr,customfm='%m/%d/%y %H:%M:%S')
381  minTime=datetime(2010,1,1,0,0,0,0)
382  maxTime=datetime.now()
383  if startT and stopT:
384  minTime=lute.StrToDatetime(startT,customfm='%m/%d/%y %H:%M:%S')
385  maxTime=lute.StrToDatetime(stopT,customfm='%m/%d/%y %H:%M:%S')
386  if not (runstopTime>=minTime and runstartTime<=maxTime):
387  continue
388  elif startT is not None:
389  minTime=lute.StrToDatetime(startT,customfm='%m/%d/%y %H:%M:%S')
390  if not (runstopTime>=minTime):
391  continue
392  elif stopT is not None:
393  maxTime=lute.StrToDatetime(stopT,customfm='%m/%d/%y %H:%M:%S')
394  runTime=lute.StrToDatetime(starttimeStr,customfm='%m/%d/%y %H:%M:%S')
395  if not (runTime<=maxTime):
396  continue
397  else:
398  pass
399  if not cursor.currentRow()['lumiid'].isNull():
400  lumiid=cursor.currentRow()['lumiid'].data()
401  if runnum in result:
402  if lumiid>result[runnum][0]:
403  result[runnum][0]=lumiid
404  else:
405  result[runnum]=[lumiid,0,0]
406  if requiretrg :
407  if cursor.currentRow()['trgid'].isNull():
408  trgid=0
409  else:
410  trgid=cursor.currentRow()['trgid'].data()
411  if runnum in result:
412  if trgid>result[runnum][1]:
413  result[runnum][1]=trgid
414  if requirehlt and not cursor.currentRow()['hltid'].isNull():
415  hltid=cursor.currentRow()['hltid'].data()
416  if runnum in result:
417  if hltid>result[runnum][2]:
418  result[runnum][2]=hltid
419  except :
420  del qHandle
421  raise
422  del qHandle
423  return result
424 
425 def runsummary(schema,runnum,sessionflavor=''):
426  '''
427  select l1key,amodetag,hltkey,fillnum,fillscheme from cmsrunsummary where runnum=:runnum
428  output: [l1key(0),amodetag(1),hltkey(3),fillnum(4),fillscheme(5)]
429  '''
430  result=[]
431  qHandle=schema.newQuery()
433  try:
434  qHandle.addToTableList(nameDealer.cmsrunsummaryTableName())
435  qCondition=coral.AttributeList()
436  qCondition.extend('runnum','unsigned int')
437  qCondition['runnum'].setData(int(runnum))
438  qHandle.addToOutputList('L1KEY','l1key')
439  qHandle.addToOutputList('AMODETAG','amodetag')
440  #qHandle.addToOutputList('EGEV','egev')
441  qHandle.addToOutputList('HLTKEY','hltkey')
442  qHandle.addToOutputList('FILLNUM','fillnum')
443  qHandle.addToOutputList('FILLSCHEME','fillscheme')
444  #if sessionflavor=='SQLite':
445  # qHandle.addToOutputList('STARTTIME','starttime')
446  # qHandle.addToOutputList('STOPTIME','stoptime')
447  #else:
448  # qHandle.addToOutputList('to_char(STARTTIME,\''+t.coraltimefm+'\')','starttime')
449  # qHandle.addToOutputList('to_char(STOPTIME,\''+t.coraltimefm+'\')','stoptime')
450  qHandle.setCondition('RUNNUM=:runnum',qCondition)
451  qResult=coral.AttributeList()
452  qResult.extend('l1key','string')
453  qResult.extend('amodetag','string')
454  #qResult.extend('egev','unsigned int')
455  qResult.extend('hltkey','string')
456  qResult.extend('fillnum','unsigned int')
457  qResult.extend('fillscheme','string')
458  #qResult.extend('starttime','string')
459  #qResult.extend('stoptime','string')
460  qHandle.defineOutput(qResult)
461  cursor=qHandle.execute()
462  while next(cursor):
463  result.append(cursor.currentRow()['l1key'].data())
464  result.append(cursor.currentRow()['amodetag'].data())
465  #result.append(cursor.currentRow()['egev'].data())
466  result.append(cursor.currentRow()['hltkey'].data())
467  result.append(cursor.currentRow()['fillnum'].data())
468  fillscheme=''
469  if not cursor.currentRow()['fillscheme'].isNull():
470  fillscheme=cursor.currentRow()['fillscheme'].data()
471  result.append(fillscheme)
472  #result.append(cursor.currentRow()['starttime'].data())
473  #result.append(cursor.currentRow()['stoptime'].data())
474  except :
475  del qHandle
476  raise
477  del qHandle
478  return result
479 
480 #def mostRecentLuminorms(schema,branchfilter):
481 # '''
482 # this overview query should be only for norm
483 # select e.name,n.data_id,r.revision_id,n.amodetag,n.norm_1,n.egev_1,n.norm_occ2,n.norm_et,n.norm_pu,n.constfactor from luminorms_entries e,luminorms_rev r,luminorms n where n.entry_id=e.entry_id and n.data_id=r.data_id and r.revision_id>=min(branchfilter) and r.revision_id<=max(branchfilter);
484 # output {norm_name:(amodetag(0),norm_1(1),egev_1(2),norm_occ2(3),norm_et(4),norm_pu(5),constfactor(6))}
485 # '''
486 # #print branchfilter
487 # result={}
488 # entry2datamap={}
489 # branchmin=0
490 # branchmax=0
491 # if branchfilter and len(branchfilter)!=0:
492 # branchmin=min(branchfilter)
493 # branchmax=max(branchfilter)
494 # else:
495 # return result
496 # #print branchmin,branchmax
497 # qHandle=schema.newQuery()
498 # normdict={}
499 # try:
500 # qHandle.addToTableList(nameDealer.entryTableName(nameDealer.luminormTableName()),'e')
501 # qHandle.addToTableList(nameDealer.luminormTableName(),'n')
502 # qHandle.addToTableList(nameDealer.revmapTableName(nameDealer.luminormTableName()),'r')
503 # qHandle.addToOutputList('e.NAME','normname')
504 # qHandle.addToOutputList('r.DATA_ID','data_id')
505 # qHandle.addToOutputList('r.REVISION_ID','revision_id')
506 # qHandle.addToOutputList('n.AMODETAG','amodetag')
507 # qHandle.addToOutputList('n.NORM_1','norm_1')
508 # qHandle.addToOutputList('n.EGEV_1','energy_1')
509 # qHandle.addToOutputList('n.NORM_OCC2','norm_occ2')
510 # qHandle.addToOutputList('n.NORM_ET','norm_et')
511 # qHandle.addToOutputList('n.NORM_PU','norm_pu')
512 # qHandle.addToOutputList('n.CONSTFACTOR','constfactor')
513 # qCondition=coral.AttributeList()
514 # qCondition.extend('branchmin','unsigned long long')
515 # qCondition.extend('branchmax','unsigned long long')
516 # qCondition['branchmin'].setData(branchmin)
517 # qCondition['branchmax'].setData(branchmax)
518 # qResult=coral.AttributeList()
519 # qResult.extend('normname','string')
520 # qResult.extend('data_id','unsigned long long')
521 # qResult.extend('revision_id','unsigned long long')
522 # qResult.extend('amodetag','string')
523 # qResult.extend('norm_1','float')
524 # qResult.extend('energy_1','unsigned int')
525 # qResult.extend('norm_occ2','float')
526 # qResult.extend('norm_et','float')
527 # qResult.extend('norm_pu','float')
528 # qResult.extend('constfactor','float')
529 # qHandle.defineOutput(qResult)
530 # qHandle.setCondition('n.ENTRY_ID=e.ENTRY_ID and n.DATA_ID=r.DATA_ID AND n.DATA_ID=r.DATA_ID AND r.REVISION_ID>=:branchmin AND r.REVISION_ID<=:branchmax',qCondition)
531 # cursor=qHandle.execute()
532 # while cursor.next():
533 # data_id=cursor.currentRow()['data_id'].data()
534 # normname=cursor.currentRow()['normname'].data()
535 # if not normdict.has_key(normname):
536 # normdict[normname]=0
537 # if data_id>normdict[normname]:
538 # normdict[normname]=data_id
539 # amodetag=cursor.currentRow()['amodetag'].data()
540 # norm_1=cursor.currentRow()['norm_1'].data()
541 # energy_1=cursor.currentRow()['energy_1'].data()
542 # norm_occ2=1.0
543 # if not cursor.currentRow()['norm_occ2'].isNull():
544 # norm_occ2=cursor.currentRow()['norm_occ2'].data()
545 # norm_et=1.0
546 # if not cursor.currentRow()['norm_et'].isNull():
547 # norm_et=cursor.currentRow()['norm_et'].data()
548 # norm_pu=1.0
549 # if not cursor.currentRow()['norm_pu'].isNull():
550 # norm_pu=cursor.currentRow()['norm_pu'].data()
551 # constfactor=1.0
552 # if not cursor.currentRow()['constfactor'].isNull():
553 # constfactor=cursor.currentRow()['constfactor'].data()
554 # result[normname]=(amodetag,norm_1,energy_1,norm_occ2,norm_et,norm_pu,constfactor)
555 # except:
556 # raise
557 # return result
558 def luminormById(schema,dataid):
559  '''
560  select entry_name,amodetag,norm_1,egev_1,norm_2,egev_2 from luminorms where DATA_ID=:dataid
561  output: {norm_name:(amodetag(0),norm_1(1),egev_1(2),norm_occ2(3),norm_et(4),norm_pu(5),constfactor(6))}
562  '''
563  result=None
564  qHandle=schema.newQuery()
565  try:
566  qHandle.addToTableList(nameDealer.luminormTableName())
567  qHandle.addToOutputList('ENTRY_NAME','normname')
568  qHandle.addToOutputList('AMODETAG','amodetag')
569  qHandle.addToOutputList('NORM_1','norm_1')
570  qHandle.addToOutputList('EGEV_1','energy_1')
571  qHandle.addToOutputList('NORM_OCC2','norm_occ2')
572  qHandle.addToOutputList('NORM_ET','norm_et')
573  qHandle.addToOutputList('NORM_PU','norm_pu')
574  qHandle.addToOutputList('CONSTFACTOR','constfactor')
575  qCondition=coral.AttributeList()
576  qCondition.extend('dataid','unsigned long long')
577  qCondition['dataid'].setData(dataid)
578  qResult=coral.AttributeList()
579  qResult.extend('normname','string')
580  qResult.extend('amodetag','string')
581  qResult.extend('norm_1','float')
582  qResult.extend('energy_1','unsigned int')
583  qResult.extend('norm_occ2','float')
584  qResult.extend('norm_et','float')
585  qResult.extend('norm_pu','float')
586  qResult.extend('constfactor','float')
587  qHandle.defineOutput(qResult)
588  qHandle.setCondition('DATA_ID=:dataid',qCondition)
589  cursor=qHandle.execute()
590  while next(cursor):
591  normname=cursor.currentRow()['normname'].data()
592  amodetag=cursor.currentRow()['amodetag'].data()
593  norm_1=cursor.currentRow()['norm_1'].data()
594  energy_1=cursor.currentRow()['energy_1'].data()
595  norm_occ2=1.0
596  if cursor.currentRow()['norm_occ2'].data():
597  norm_occ2=cursor.currentRow()['norm_occ2'].data()
598  norm_et=1.0
599  if cursor.currentRow()['norm_et'].data():
600  norm_et=cursor.currentRow()['norm_et'].data()
601  norm_pu=1.0
602  if cursor.currentRow()['norm_pu'].data():
603  norm_pu=cursor.currentRow()['norm_pu'].data()
604  constfactor=1.0
605  if cursor.currentRow()['constfactor'].data():
606  constfactor=cursor.currentRow()['constfactor'].data()
607  result={normname:(amodetag,norm_1,energy_1,norm_occ2,norm_et,norm_pu,constfactor)}
608  except :
609  del qHandle
610  raise
611  del qHandle
612  return result
613 
614 #def mostRecentLumicorrs(schema,branchfilter):
615 # '''
616 # this overview query should be only for corr
617 # select e.name,n.data_id,r.revision_id , n.a1,n.a2,n.drift from lumicorrections_entries e,lumicorrections_rev r,lumicorrections n where n.entry_id=e.entry_id and n.data_id=r.data_id and r.revision_id>=min(branchfilter) and r.revision_id<=max(branchfilter) group by e.entry_name,r.revision_id,n.a1,n.a2,n.drift;
618 # output {corrname:(data_id,a1,a2,drift)}
619 # '''
620 # #print branchfilter
621 # result={}
622 # entry2datamap={}
623 # branchmin=0
624 # branchmax=0
625 # if branchfilter and len(branchfilter)!=0:
626 # branchmin=min(branchfilter)
627 # branchmax=max(branchfilter)
628 # else:
629 # return result
630 # qHandle=schema.newQuery()
631 # corrdict={}
632 # try:
633 # qHandle.addToTableList(nameDealer.entryTableName(nameDealer.lumicorrectionsTableName()),'e')
634 # qHandle.addToTableList(nameDealer.lumicorrectionsTableName(),'n')
635 # qHandle.addToTableList(nameDealer.revmapTableName(nameDealer.lumicorrectionsTableName()),'r')
636 # qHandle.addToOutputList('e.NAME','corrname')
637 # qHandle.addToOutputList('r.DATA_ID','data_id')
638 # qHandle.addToOutputList('r.REVISION_ID','revision_id')
639 # qHandle.addToOutputList('n.A1','a1')
640 # qHandle.addToOutputList('n.A2','a2')
641 # qHandle.addToOutputList('n.DRIFT','drift')
642 # qCondition=coral.AttributeList()
643 # qCondition.extend('branchmin','unsigned long long')
644 # qCondition.extend('branchmax','unsigned long long')
645 # qCondition['branchmin'].setData(branchmin)
646 # qCondition['branchmax'].setData(branchmax)
647 # qResult=coral.AttributeList()
648 # qResult.extend('corrname','string')
649 # qResult.extend('data_id','unsigned long long')
650 # qResult.extend('revision_id','unsigned long long')
651 # qResult.extend('a1','float')
652 # qResult.extend('a2','float')
653 # qResult.extend('drift','float')
654 # qHandle.defineOutput(qResult)
655 # qHandle.setCondition('n.ENTRY_ID=e.ENTRY_ID and n.DATA_ID=r.DATA_ID AND n.DATA_ID=r.DATA_ID AND r.REVISION_ID>=:branchmin AND r.REVISION_ID<=:branchmax',qCondition)
656 # cursor=qHandle.execute()
657 # while cursor.next():
658 # corrname=cursor.currentRow()['corrname'].data()
659 # data_id=cursor.currentRow()['data_id'].data()
660 # if not corrdict.has_key(corrname):
661 # corrdict[corrname]=0
662 # if data_id>corrdict[corrname]:
663 # corrdict[corrname]=data_id
664 # a1=cursor.currentRow()['a1'].data() #required
665 # a2=0.0
666 # if not cursor.currentRow()['a2'].isNull():
667 # a2=cursor.currentRow()['a2'].data()
668 # drift=0.0
669 # if not cursor.currentRow()['drift'].isNull():
670 # drift=cursor.currentRow()['drift'].data()
671 # result[corrname]=(data_id,a1,a2,drift)
672 # except:
673 # raise
674 # return result
675 
676 def luminormById(schema,dataid):
677  '''
678  select entry_name,amodetag,norm_1,egev_1,norm_2,egev_2 from luminorms where DATA_ID=:dataid
679  result (normname(0),amodetag(1),egev(2),norm(3),norm_occ2(4),norm_et(5),norm_pu(6),constfactor(7))
680  '''
681  result=None
682  qHandle=schema.newQuery()
683  try:
684  qHandle.addToTableList(nameDealer.luminormTableName())
685  qHandle.addToOutputList('ENTRY_NAME','normname')
686  qHandle.addToOutputList('AMODETAG','amodetag')
687  qHandle.addToOutputList('NORM_1','norm_1')
688  qHandle.addToOutputList('EGEV_1','energy_1')
689  qHandle.addToOutputList('NORM_OCC2','norm_occ2')
690  qHandle.addToOutputList('NORM_ET','norm_et')
691  qHandle.addToOutputList('NORM_PU','norm_pu')
692  qHandle.addToOutputList('CONSTFACTOR','constfactor')
693  qCondition=coral.AttributeList()
694  qCondition.extend('dataid','unsigned long long')
695  qCondition['dataid'].setData(dataid)
696  qResult=coral.AttributeList()
697  qResult.extend('normname','string')
698  qResult.extend('amodetag','string')
699  qResult.extend('norm_1','float')
700  qResult.extend('energy_1','unsigned int')
701  qResult.extend('norm_occ2','float')
702  qResult.extend('norm_et','float')
703  qResult.extend('norm_pu','float')
704  qResult.extend('constfactor','float')
705  qHandle.defineOutput(qResult)
706  qHandle.setCondition('DATA_ID=:dataid',qCondition)
707  cursor=qHandle.execute()
708  while next(cursor):
709  normname=cursor.currentRow()['normname'].data()
710  amodetag=cursor.currentRow()['amodetag'].data()
711  norm_1=cursor.currentRow()['norm_1'].data()
712  energy_1=cursor.currentRow()['energy_1'].data()
713  norm_occ2=1.0
714  if cursor.currentRow()['norm_occ2'].data():
715  norm_occ2=cursor.currentRow()['norm_occ2'].data()
716  norm_et=1.0
717  if cursor.currentRow()['norm_et'].data():
718  norm_et=cursor.currentRow()['norm_et'].data()
719  norm_pu=1.0
720  if cursor.currentRow()['norm_pu'].data():
721  norm_pu=cursor.currentRow()['norm_pu'].data()
722  constfactor=1.0
723  if cursor.currentRow()['constfactor'].data():
724  constfactor=cursor.currentRow()['constfactor'].data()
725  result={normname:(amodetag,norm_1,energy_1,norm_occ2,norm_et,norm_pu,constfactor)}
726  except :
727  del qHandle
728  raise
729  del qHandle
730  return result
731 
732 def trgRunById(schema,dataid,trgbitname=None,trgbitnamepattern=None):
733  '''
734  query: select RUNNUM,SOURCE,BITZERONAME,BITNAMECLOB,ALGOMASK_H,ALGOMASK_L,TECHMASK from trgdata where DATA_ID=:dataid
735 
736  output: [runnum(0),datasource(1),bitzeroname(2),bitnamedict(3),algomask_h(4),algomask_l(5),techmask(6)]
737  -- runnumber
738  -- original source database name
739  -- deadtime norm bitname
740  -- bitnamedict [(bitidx,bitname),...]
741  '''
742  result=[]
743  qHandle=schema.newQuery()
744  runnum=None
745  datasource=None
746  bitzeroname=None
747  bitnamedict=[]
748  try:
749  qHandle.addToTableList(nameDealer.trgdataTableName())
750  qHandle.addToOutputList('RUNNUM','runnum')
751  qHandle.addToOutputList('SOURCE','source')
752  qHandle.addToOutputList('BITZERONAME','bitzeroname')
753  qHandle.addToOutputList('BITNAMECLOB','bitnameclob')
754  qHandle.addToOutputList('ALGOMASK_H','algomask_h')
755  qHandle.addToOutputList('ALGOMASK_L','algomask_l')
756  qHandle.addToOutputList('TECHMASK','techmask')
757  qCondition=coral.AttributeList()
758  qCondition.extend('dataid','unsigned long long')
759  qCondition['dataid'].setData(dataid)
760  qResult=coral.AttributeList()
761  qResult.extend('runnum','unsigned int')
762  qResult.extend('source','string')
763  qResult.extend('bitzeroname','string')
764  qResult.extend('bitnameclob','string')
765  qResult.extend('algomask_h','unsigned long long')
766  qResult.extend('algomask_l','unsigned long long')
767  qResult.extend('techmask','unsigned long long')
768  qHandle.defineOutput(qResult)
769  qHandle.setCondition('DATA_ID=:dataid',qCondition)
770  cursor=qHandle.execute()
771  bitnameclob=None
772  bitnames=[]
773  while next(cursor):
774  runnum=cursor.currentRow()['runnum'].data()
775  source=cursor.currentRow()['source'].data()
776  bitzeroname=cursor.currentRow()['bitzeroname'].data()
777  bitnameclob=cursor.currentRow()['bitnameclob'].data()
778  algomask_h=cursor.currentRow()['algomask_h'].data()
779  algomask_l=cursor.currentRow()['algomask_l'].data()
780  techmask=cursor.currentRow()['techmask'].data()
781  if bitnameclob:
782  bitnames=bitnameclob.split(',')
783  for trgnameidx,trgname in enumerate(bitnames):
784  if trgbitname :
785  if trgname==trgbitname:
786  bitnamedict.append((trgnameidx,trgname))
787  break
788  elif trgbitnamepattern:
789  if fnmatch.fnmatch(trgname,trgbitnamepattern):
790  bitnamedict.append((trgnameidx,trgname))
791  else:
792  bitnamedict.append((trgnameidx,trgname))
793  result=[runnum,source,bitzeroname,bitnamedict,algomask_h,algomask_l,techmask]
794  except :
795  del qHandle
796  raise
797  del qHandle
798  return result
799 
800 def trgLSById(schema,dataid,trgbitname=None,trgbitnamepattern=None,withL1Count=False,withPrescale=False):
801  '''
802  output: (runnum,{cmslsnum:[deadtimecount(0),bitzerocount(1),bitzeroprescale(2),deadfrac(3),[(bitname,trgcount,prescale,mask)](4)]})
803  '''
804  runnum=0
805  result={}
806  trgnamedict=[]
807  if trgbitname or trgbitnamepattern or withPrescale or withL1Count:
808  trgrundata=trgRunById(schema,dataid,trgbitname=trgbitname,trgbitnamepattern=trgbitnamepattern)
809  trgnamedict=trgrundata[3]#[runnum(0),datasource(1),bitzeroname(2),bitnamedict(3),algomask_h(4),algomask_l(5),techmask(6)]
810  algomask_h=trgrundata[4]
811  algomask_l=trgrundata[5]
812  techmask=trgrundata[6]
813  qHandle=schema.newQuery()
814  try:
815  qHandle.addToTableList(nameDealer.lstrgTableName())
816  qHandle.addToOutputList('RUNNUM','runnum')
817  qHandle.addToOutputList('CMSLSNUM','cmslsnum')
818  qHandle.addToOutputList('DEADTIMECOUNT','deadtimecount')
819  #qHandle.addToOutputList('BITZEROCOUNT','bitzerocount')
820  #qHandle.addToOutputList('BITZEROPRESCALE','bitzeroprescale')
821  qHandle.addToOutputList('DEADFRAC','deadfrac')
822  if withPrescale:
823  qHandle.addToOutputList('PRESCALEBLOB','prescalesblob')
824  if withL1Count:
825  qHandle.addToOutputList('TRGCOUNTBLOB','trgcountblob')
826  qConditionStr='DATA_ID=:dataid'
827  qCondition=coral.AttributeList()
828  qCondition.extend('dataid','unsigned long long')
829  qCondition['dataid'].setData(dataid)
830  qResult=coral.AttributeList()
831  qResult.extend('runnum','unsigned int')
832  qResult.extend('cmslsnum','unsigned int')
833  qResult.extend('deadtimecount','unsigned long long')
834  #qResult.extend('bitzerocount','unsigned int')
835  #qResult.extend('bitzeroprescale','unsigned int')
836  qResult.extend('deadfrac','float')
837  if withPrescale:
838  qResult.extend('prescalesblob','blob')
839  if withL1Count:
840  qResult.extend('trgcountblob','blob')
841  qHandle.defineOutput(qResult)
842  qHandle.setCondition(qConditionStr,qCondition)
843  cursor=qHandle.execute()
844  while next(cursor):
845  runnum=cursor.currentRow()['runnum'].data()
846  cmslsnum=cursor.currentRow()['cmslsnum'].data()
847  deadtimecount=cursor.currentRow()['deadtimecount'].data()
848  #bitzerocount=cursor.currentRow()['bitzerocount'].data()
849  #bitzeroprescale=cursor.currentRow()['bitzeroprescale'].data()
850  bitzerocount=0
851  bitzeroprescale=0
852  deadfrac=cursor.currentRow()['deadfrac'].data()
853  if cmslsnum not in result:
854  result[cmslsnum]=[]
855  result[cmslsnum].append(deadtimecount)
856  result[cmslsnum].append(bitzerocount)
857  result[cmslsnum].append(bitzeroprescale)
858  result[cmslsnum].append(deadfrac)
859  prescalesblob=None
860  trgcountblob=None
861  if withPrescale:
862  prescalesblob=cursor.currentRow()['prescalesblob'].data()
863  if withL1Count:
864  trgcountblob=cursor.currentRow()['trgcountblob'].data()
865  prescales=[]
866  trgcounts=[]
867  if prescalesblob:
868  if runnum <150008: ###WORKAROUND PATCH!! because the 2010 blobs were packed as type l ###
869  prescales=CommonUtil.unpackBlobtoArray(prescalesblob,'l')
870  else:
871  prescales=CommonUtil.unpackBlobtoArray(prescalesblob,'I')
872  if trgcountblob:
873  if runnum <150008: ###WORKAROUND PATCH!! because the 2010 blobs were packed as type l ###
874  trgcounts=CommonUtil.unpackBlobtoArray(trgcountblob,'l')
875  else:
876  trgcounts=CommonUtil.unpackBlobtoArray(trgcountblob,'I')
877 
878  bitinfo=[]
879  for (bitidx,thisbitname) in trgnamedict:
880  thispresc=None
881  thistrgcount=None
882  if prescales:
883  thispresc=prescales[bitidx]
884  if trgcounts:
885  thistrgcount=trgcounts[bitidx]
886  maskval=0
887  #[0-127] is algobit
888  if bitidx in range(0,128):
889  if bitidx<64 :#0-63 is in algo_l
890  maskval=algomask_l>>bitidx&1
891  else:#64-127 is in algo_h
892  maskval=algomask_h>>(bitidx-64)&1
893  else:
894  #[128-191] is techbit
895  maskval=techmask>>(bitidx-128)&1
896  thisbitinfo=(thisbitname,thistrgcount,thispresc,maskval)
897  bitinfo.append(thisbitinfo)
898  result[cmslsnum].append(bitinfo)
899  except:
900  del qHandle
901  raise
902  del qHandle
903 # t1=time.time()
904 # print 'trgLSById time ',t1-t0
905  return (runnum,result)
906 
907 def lumiRunByIds(schema,dataidMap,lumitype='HF'):
908  '''
909  input dataidMap : {run:lumidataid}
910  result {runnum: (datasource(0),nominalegev(1),ncollidingbunches(2),starttime(3),stoptime(4),nls(5)}
911  '''
912  result={}
913  if not dataidMap:
914  return result
915  inputRange=dataidMap.keys()
916  for r in inputRange:
917  lumidataid=dataidMap[r][0]
918  if lumidataid:
919  perrundata=lumiRunById(schema,lumidataid,lumitype=lumitype)
920  result[r]=(perrundata[1],perrundata[2],perrundata[3],perrundata[4],perrundata[5])
921  return result
922 
923 def beamstatusByIds(schema,dataidMap):
924  '''
925  input dataidMap : {run:lumidataid}
926  result {runnum:{cmslsnum:beamstatus}}
927  '''
928  result={}
929  if not dataidMap:
930  return result
931  inputRange=dataidMap.keys()
932  for r in inputRange:
933  if r not in result:
934  result[r]={}
935  lumidataid=dataidMap[r][0]
936  if lumidataid:
937  qHandle=schema.newQuery()
938  try:
939  qHandle.addToTableList(nameDealer.lumisummaryv2TableName())
940  qHandle.addToOutputList('CMSLSNUM')
941  qHandle.addToOutputList('BEAMSTATUS')
942  qConditionStr='DATA_ID=:dataid'
943  qCondition=coral.AttributeList()
944  qCondition.extend('dataid','unsigned long long')
945  qCondition['dataid'].setData(int(lumidataid))
946  qResult=coral.AttributeList()
947  qResult.extend('CMSLSNUM','unsigned int')
948  qResult.extend('BEAMSTATUS','string')
949  qHandle.defineOutput(qResult)
950  qHandle.setCondition(qConditionStr,qCondition)
951  cursor=qHandle.execute()
952  while next(cursor):
953  cmslsnum=cursor.currentRow()['CMSLSNUM'].data()
954  bs=cursor.currentRow()['BEAMSTATUS'].data()
955  if bs!='STABLE BEAMS':
956  result[r][cmslsnum]=bs
957  except:
958  del qHandle
959  raise
960  del qHandle
961  return result
962 
963 def lumiRunById(schema,lumidataid,lumitype='HF'):
964  '''
965  input: lumidataid
966  output: (runnum(0),datasource(1),nominalegev(2),ncollidingbunches(3),starttimestr(4),stoptimestr(5),nls(6))
967  '''
968  result=None
969  if lumitype not in ['HF','PIXEL']:
970  raise ValueError('unknown lumitype '+lumitype)
971  lumitableName=''
972  if lumitype=='HF':
973  lumitableName = nameDealer.lumidataTableName()
974  else:
975  lumitableName = nameDealer.pixellumidataTableName()
976  qHandle=schema.newQuery()
977  try:
978  qHandle.addToTableList(lumitableName)
979  qHandle.addToOutputList('RUNNUM')
980  qHandle.addToOutputList('SOURCE')
981  qHandle.addToOutputList('NOMINALEGEV')
982  qHandle.addToOutputList('NCOLLIDINGBUNCHES')
983  qHandle.addToOutputList('TO_CHAR('+lumitableName+'.STARTTIME,\'MM/DD/YY HH24:MI:SS\')','startT')
984  qHandle.addToOutputList('TO_CHAR('+lumitableName+'.STOPTIME,\'MM/DD/YY HH24:MI:SS\')','stopT')
985  qHandle.addToOutputList('NLS')
986  qConditionStr='DATA_ID=:dataid'
987  qCondition=coral.AttributeList()
988  qCondition.extend('dataid','unsigned long long')
989  qCondition['dataid'].setData(lumidataid)
990  qResult=coral.AttributeList()
991  qResult.extend('RUNNUM','unsigned int')
992  qResult.extend('SOURCE','string')
993  qResult.extend('NOMINALEGEV','float')
994  qResult.extend('NCOLLIDINGBUNCHES','unsigned int')
995  qResult.extend('startT','string')
996  qResult.extend('stopT','string')
997  qResult.extend('NLS','unsigned int')
998  qHandle.defineOutput(qResult)
999  qHandle.setCondition(qConditionStr,qCondition)
1000  cursor=qHandle.execute()
1001  lu=lumiTime.lumiTime()
1002  nls=0
1003  while next(cursor):
1004  runnum=cursor.currentRow()['RUNNUM'].data()
1005  datasource=cursor.currentRow()['SOURCE'].data()
1006  nominalegev=0
1007  if not cursor.currentRow()['NOMINALEGEV'].isNull():
1008  nominalegev=cursor.currentRow()['NOMINALEGEV'].data()
1009  ncollidingbunches=0
1010  if not cursor.currentRow()['NCOLLIDINGBUNCHES'].isNull():
1011  ncollidingbunches=cursor.currentRow()['NCOLLIDINGBUNCHES'].data()
1012  startTstr=cursor.currentRow()['startT'].data()
1013  stopTstr=cursor.currentRow()['stopT'].data()
1014  #startT=lu.StrToDatetime(startTstr)
1015  #stopT=lu.StrToDatetime(stopTstr)
1016  if not cursor.currentRow()['NLS'].isNull():
1017  nls=cursor.currentRow()['NLS'].data()
1018  result=(runnum,datasource,nominalegev,ncollidingbunches,startTstr,stopTstr,nls)
1019  except :
1020  del qHandle
1021  raise
1022  del qHandle
1023  return result
1024 
1025 #def correctionByName(schema,correctiontagname=None):
1026 # '''
1027 # get correction coefficients by name
1028 # input: correctiontagname if None,get current default
1029 # output: [tagname,a1,a2,drift]
1030 # if not correctiontagname
1031 # select entry_name,data_id,a1,a2,drift from lumicorrections where
1032 # else:
1033 # select entry_name,data_id,a1,a2,drift from lumicorrections where entry_name=:correctiontagname
1034 # '''
1035 
1036 
1037 #def fillschemeByRun(schema,runnum):
1038 # fillscheme=''
1039 # ncollidingbunches=0
1040 # r=nameDealer.cmsrunsummaryTableName()
1041 # qHandle=schema.newQuery()
1042 # try:
1043 # qHandle.addToTableList(r)
1044 # qHandle.addToOutputList('FILLSCHEME')
1045 # qHandle.addToOutputList('NCOLLIDINGBUNCHES')
1046 # qResult=coral.AttributeList()
1047 # qResult.extend('FILLSCHEME','string')
1048 # qResult.extend('NCOLLIDINGBUNCHES','unsigned int')
1049 # qConditionStr='RUNNUM=:runnum'
1050 # qCondition=coral.AttributeList()
1051 # qCondition.extend('runnum','unsigned int')
1052 # qCondition['runnum'].setData(int(runnum))
1053 # qHandle.defineOutput(qResult)
1054 # qHandle.setCondition(qConditionStr,qCondition)
1055 # cursor=qHandle.execute()
1056 # while cursor.next():
1057 # if not cursor.currentRow()['NCOLLIDINGBUNCHES'].isNull():
1058 # ncollidingbunches=cursor.currentRow()['NCOLLIDINGBUNCHES'].data()
1059 # if not cursor.currentRow()['FILLSCHEME'].isNull():
1060 # fillscheme=cursor.currentRow()['FILLSCHEME'].data()
1061 # except :
1062 # del qHandle
1063 # raise
1064 # del qHandle
1065 # return (fillscheme,ncollidingbunches)
1066 def allfillschemes(schema):
1067  afterglows=[]
1069  try:
1070  qHandle.addToTableList(s)
1071  qResult=coral.AttributeList()
1072  qResult.extend('FILLSCHEMEPATTERN','string')
1073  qResult.extend('CORRECTIONFACTOR','float')
1074  qHandle.defineOutput(qResult)
1075  qHandle.addToOutputList('FILLSCHEMEPATTERN')
1076  qHandle.addToOutputList('CORRECTIONFACTOR')
1077  cursor=qHandle.execute()
1078  while next(cursor):
1079  fillschemePattern=cursor.currentRow()['FILLSCHEMEPATTERN'].data()
1080  afterglowfac=cursor.currentRow()['CORRECTIONFACTOR'].data()
1081  afterglows.append((fillschemePattern,afterglowfac))
1082  except :
1083  del qHandle
1084  raise
1085  del qHandle
1086  return afterglows
1087 
1088 def lumiLSById(schema,dataid,beamstatus=None,withBXInfo=False,bxAlgo='OCC1',withBeamIntensity=False,tableName=None):
1089  '''
1090  input:
1091  beamstatus: filter on beam status flag
1092  output:
1093  result (runnum,{lumilsnum,[cmslsnum(0),instlumi(1),instlumierr(2),instlumiqlty(3),beamstatus(4),beamenergy(5),numorbit(6),startorbit(7),(bxvalueArray,bxerrArray)(8),(bxindexArray,beam1intensityArray,beam2intensityArray)(9)]})
1094  '''
1095  runnum=0
1096  result={}
1097  qHandle=schema.newQuery()
1098  if withBXInfo and bxAlgo not in ['OCC1','OCC2','ET']:
1099  raise ValueError('unknown lumi algo '+bxAlgo)
1100  if beamstatus and beamstatus not in ['STABLE BEAMS',]:
1101  raise ValueError('unknown beam status '+beamstatus)
1102  try:
1103  if tableName is None:
1105  else:
1106  lls=tableName
1107  qHandle.addToTableList(lls)
1108  qHandle.addToOutputList('RUNNUM','runnum')
1109  qHandle.addToOutputList('LUMILSNUM','lumilsnum')
1110  qHandle.addToOutputList('CMSLSNUM','cmslsnum')
1111  qHandle.addToOutputList('INSTLUMI','instlumi')
1112  qHandle.addToOutputList('INSTLUMIERROR','instlumierr')
1113  qHandle.addToOutputList('INSTLUMIQUALITY','instlumiqlty')
1114  qHandle.addToOutputList('BEAMSTATUS','beamstatus')
1115  qHandle.addToOutputList('BEAMENERGY','beamenergy')
1116  qHandle.addToOutputList('NUMORBIT','numorbit')
1117  qHandle.addToOutputList('STARTORBIT','startorbit')
1118  if withBXInfo:
1119  qHandle.addToOutputList('BXLUMIVALUE_'+bxAlgo,'bxvalue')
1120  qHandle.addToOutputList('BXLUMIERROR_'+bxAlgo,'bxerror')
1121  if withBeamIntensity:
1122  qHandle.addToOutputList('CMSBXINDEXBLOB','bxindexblob')
1123  qHandle.addToOutputList('BEAMINTENSITYBLOB_1','beam1intensity')
1124  qHandle.addToOutputList('BEAMINTENSITYBLOB_2','beam2intensity')
1125 
1126  qConditionStr='DATA_ID=:dataid'
1127  qCondition=coral.AttributeList()
1128  qCondition.extend('dataid','unsigned long long')
1129  qCondition['dataid'].setData(int(dataid))
1130  if beamstatus:
1131  qConditionStr+=' and BEAMSTATUS=:beamstatus'
1132  qCondition.extend('beamstatus','string')
1133  qCondition['beamstatus'].setData(beamstatus)
1134  qResult=coral.AttributeList()
1135  qResult.extend('runnum','unsigned int')
1136  qResult.extend('lumilsnum','unsigned int')
1137  qResult.extend('cmslsnum','unsigned int')
1138  qResult.extend('instlumi','float')
1139  qResult.extend('instlumierr','float')
1140  qResult.extend('instlumiqlty','short')
1141  qResult.extend('beamstatus','string')
1142  qResult.extend('beamenergy','float')
1143  qResult.extend('numorbit','unsigned int')
1144  qResult.extend('startorbit','unsigned int')
1145  if withBXInfo:
1146  qResult.extend('bxvalue','blob')
1147  qResult.extend('bxerror','blob')
1148  if withBeamIntensity:
1149  qResult.extend('bxindexblob','blob')
1150  qResult.extend('beam1intensity','blob')
1151  qResult.extend('beam2intensity','blob')
1152  qHandle.defineOutput(qResult)
1153  qHandle.setCondition(qConditionStr,qCondition)
1154  cursor=qHandle.execute()
1155  while next(cursor):
1156  runnum=cursor.currentRow()['runnum'].data()
1157  lumilsnum=cursor.currentRow()['lumilsnum'].data()
1158  cmslsnum=cursor.currentRow()['cmslsnum'].data()
1159  instlumi=cursor.currentRow()['instlumi'].data()
1160  instlumierr=cursor.currentRow()['instlumierr'].data()
1161  instlumiqlty=cursor.currentRow()['instlumiqlty'].data()
1162  bs=cursor.currentRow()['beamstatus'].data()
1163  begev=cursor.currentRow()['beamenergy'].data()
1164  numorbit=cursor.currentRow()['numorbit'].data()
1165  startorbit=cursor.currentRow()['startorbit'].data()
1166  bxinfo=None
1167  bxvalueblob=None
1168  bxerrblob=None
1169  if withBXInfo:
1170  bxvalueblob=cursor.currentRow()['bxvalue'].data()
1171  bxerrblob=cursor.currentRow()['bxerror'].data()
1172  if bxvalueblob and bxerrblob:
1173  bxvaluesArray=CommonUtil.unpackBlobtoArray(bxvalueblob,'f')
1174  bxerrArray=CommonUtil.unpackBlobtoArray(bxerrblob,'f')
1175  bxinfo=(bxvaluesArray,bxerrArray)
1176  bxindexblob=None
1177  beam1intensity=None
1178  beam2intensity=None
1179  beaminfo=None
1180  if withBeamIntensity:
1181  bxindexblob=cursor.currentRow()['bxindexblob'].data()
1182  beam1intensity=cursor.currentRow()['beam1intensity'].data()
1183  beam2intensity=cursor.currentRow()['beam2intensity'].data()
1184  if bxindexblob :
1185  bxindexArray=CommonUtil.unpackBlobtoArray(bxindexblob,'h')
1186  beam1intensityArray=CommonUtil.unpackBlobtoArray(beam1intensity,'f')
1187  beam2intensityArray=CommonUtil.unpackBlobtoArray(beam2intensity,'f')
1188  beaminfo=(bxindexArray,beam1intensityArray,beam2intensityArray)
1189  result[lumilsnum]=[cmslsnum,instlumi,instlumierr,instlumiqlty,bs,begev,numorbit,startorbit,bxinfo,beaminfo]
1190  except :
1191  del qHandle
1192  raise
1193  del qHandle
1194  return (runnum,result)
1195 def beamInfoById(schema,dataid,withBeamIntensity=False,minIntensity=0.1):
1196  '''
1197  result (runnum,[(lumilsnum(0),cmslsnum(1),beamstatus(2),beamenergy(3),ncollidingbunches(4),beaminfolist(5),..])
1198  beaminfolist=[(bxidx,beam1intensity,beam2intensity)]
1199  '''
1200  runnum=0
1201  result=[]
1202  ncollidingbunches=0
1203  qHandle=schema.newQuery()
1204  try:
1205  qHandle.addToTableList(nameDealer.lumidataTableName())
1206  qHandle.addToOutputList('NCOLLIDINGBUNCHES')
1207  qConditionStr='DATA_ID=:dataid'
1208  qCondition=coral.AttributeList()
1209  qCondition.extend('dataid','unsigned long long')
1210  qCondition['dataid'].setData(dataid)
1211  qResult=coral.AttributeList()
1212  qResult.extend('NCOLLIDINGBUNCHES','unsigned int')
1213  qHandle.defineOutput(qResult)
1214  qHandle.setCondition(qConditionStr,qCondition)
1215  cursor=qHandle.execute()
1216  while next(cursor):
1217  ncollidingbunches=cursor.currentRow()['NCOLLIDINGBUNCHES'].data()
1218  except :
1219  del qHandle
1220  raise
1221  del qHandle
1222  qHandle=schema.newQuery()
1223  try:
1224  qHandle.addToTableList(nameDealer.lumisummaryv2TableName())
1225  qHandle.addToOutputList('RUNNUM')
1226  qHandle.addToOutputList('CMSLSNUM')
1227  qHandle.addToOutputList('LUMILSNUM')
1228  qHandle.addToOutputList('BEAMSTATUS')
1229  qHandle.addToOutputList('BEAMENERGY')
1230  if withBeamIntensity:
1231  qHandle.addToOutputList('CMSBXINDEXBLOB')
1232  qHandle.addToOutputList('BEAMINTENSITYBLOB_1')
1233  qHandle.addToOutputList('BEAMINTENSITYBLOB_2')
1234  qConditionStr='DATA_ID=:dataid'
1235  qCondition=coral.AttributeList()
1236  qCondition.extend('dataid','unsigned long long')
1237  qCondition['dataid'].setData(dataid)
1238  qResult=coral.AttributeList()
1239  qResult.extend('RUNNUM','unsigned int')
1240  qResult.extend('CMSLSNUM','unsigned int')
1241  qResult.extend('LUMILSNUM','unsigned int')
1242  qResult.extend('BEAMSTATUS','string')
1243  qResult.extend('BEAMENERGY','float')
1244  if withBeamIntensity:
1245  qResult.extend('BXINDEXBLOB','blob')
1246  qResult.extend('BEAM1INTENSITY','blob')
1247  qResult.extend('BEAM2INTENSITY','blob')
1248  qHandle.defineOutput(qResult)
1249  qHandle.setCondition(qConditionStr,qCondition)
1250  cursor=qHandle.execute()
1251  while next(cursor):
1252  runnum=cursor.currentRow()['RUNNUM'].data()
1253  cmslsnum=cursor.currentRow()['CMSLSNUM'].data()
1254  lumilsnum=cursor.currentRow()['LUMILSNUM'].data()
1255  beamstatus=cursor.currentRow()['BEAMSTATUS'].data()
1256  beamenergy=cursor.currentRow()['BEAMENERGY'].data()
1257  bxindexblob=None
1258  beaminfotupleList=[]
1259  if withBeamIntensity:
1260  bxindexblob=cursor.currentRow()['BXINDEXBLOB'].data()
1261  beam1intensityblob=cursor.currentRow()['BEAM1INTENSITY'].data()
1262  beam2intensityblob=cursor.currentRow()['BEAM2INTENSITY'].data()
1263  bxindexArray=None
1264  beam1intensityArray=None
1265  beam2intensityArray=None
1266  if bxindexblob:
1267  bxindexArray=CommonUtil.unpackBlobtoArray(bxindexblob,'h')
1268  if beam1intensityblob:
1269  beam1intensityArray=CommonUtil.unpackBlobtoArray(beam1intensityblob,'f')
1270  if beam2intensityblob:
1271  beam2intensityArray=CommonUtil.unpackBlobtoArray(beam2intensityblob,'f')
1272  if bxindexArray and beam1intensityArray and beam2intensityArray:
1273  for idx,bxindex in enumerate(bxindexArray):
1274  if (beam1intensityArray[idx] and beam1intensityArray[idx]>minIntensity) or (beam2intensityArray[idx] and beam2intensityArray[idx]>minIntensity):
1275  beaminfotuple=(bxindex,beam1intensityArray[idx],beam2intensityArray[idx])
1276  beaminfotupleList.append(beaminfotuple)
1277  del bxindexArray[:]
1278  del beam1intensityArray[:]
1279  del beam2intensityArray[:]
1280  result.append((lumilsnum,cmslsnum,beamstatus,beamenergy,ncollidingbunches,beaminfotupleList))
1281  except:
1282  del qHandle
1283  raise
1284  del qHandle
1285  return (runnum,result)
1286 def lumiBXByAlgo(schema,dataid,algoname):
1287  '''
1288  result {lumilsnum:[cmslsnum,numorbit,startorbit,bxlumivalue,bxlumierr,bxlumiqlty]}
1289  '''
1290  result={}
1291  qHandle=schema.newQuery()
1292  try:
1293  qHandle.addToTableList(nameDealer.lumisummaryv2TableName())
1294  qHandle.addToOutputList('CMSLSNUM','cmslsnum')
1295  qHandle.addToOutputList('LUMILSNUM','lumilsnum')
1296  #qHandle.addToOutputList('ALGONAME','algoname')
1297  qHandle.addToOutputList('NUMORBIT','numorbit')
1298  qHandle.addToOutputList('STARTORBIT','startorbit')
1299  qHandle.addToOutputList('BXLUMIVALUE_'+algoname,'bxlumivalue')
1300  qHandle.addToOutputList('BXLUMIERROR_'+algoname,'bxlumierr')
1301  qHandle.addToOutputList('BXLUMIQUALITY_'+algoname,'bxlumiqlty')
1302  qConditionStr='DATA_ID=:dataid'
1303  qCondition=coral.AttributeList()
1304  qCondition.extend('dataid','unsigned long long')
1305  qCondition['dataid'].setData(dataid)
1306  qResult=coral.AttributeList()
1307  qResult.extend('cmslsnum','unsigned int')
1308  qResult.extend('lumilsnum','unsigned int')
1309  qResult.extend('numorbit','unsigned int')
1310  qResult.extend('startorbit','unsigned int')
1311  qResult.extend('bxlumivalue','blob')
1312  qResult.extend('bxlumierr','blob')
1313  qResult.extend('bxlumiqlty','blob')
1314  qHandle.defineOutput(qResult)
1315  qHandle.setCondition(qConditionStr,qCondition)
1316  cursor=qHandle.execute()
1317  while next(cursor):
1318  cmslsnum=cursor.currentRow()['cmslsnum'].data()
1319  lumilsnum=cursor.currentRow()['lumilsnum'].data()
1320  numorbit=cursor.currentRow()['numorbit'].data()
1321  startorbit=cursor.currentRow()['startorbit'].data()
1322  bxlumivalue=cursor.currentRow()['bxlumivalue'].data()
1323  bxlumierr=cursor.currentRow()['bxlumierr'].data()
1324  bxlumiqlty=cursor.currentRow()['bxlumiqlty'].data()
1325  if algoname not in result:
1326  result[algoname]={}
1327  if lumilsnum not in result[algoname]:
1328  result[algoname][lumilsnum]=[]
1329  result[algoname][lumilsnum].extend([cmslsnum,numorbit,startorbit,bxlumivalue,bxlumierr,bxlumiqlty])
1330  except :
1331  del qHandle
1332  raise RuntimeError(' dataDML.lumiBXById: '+str(e))
1333  del qHandle
1334  return result
1335 
1336 def hltRunById(schema,dataid,hltpathname=None,hltpathpattern=None):
1337  '''
1338  result [runnum(0),datasource(1),npath(2),hltnamedict(3)]
1339  output :
1340  npath : total number of hltpath in DB
1341  hltnamedict : list of all selected paths [(hltpathidx,hltname),(hltpathidx,hltname)]
1342  '''
1343  result=[]
1344  qHandle=schema.newQuery()
1345  runnum=None
1346  datasource=None
1347  npath=None
1348  hltnamedict=[]
1349  try:
1350  qHandle.addToTableList(nameDealer.hltdataTableName())
1351  qHandle.addToOutputList('RUNNUM','runnum')
1352  qHandle.addToOutputList('SOURCE','datasource')
1353  qHandle.addToOutputList('NPATH','npath')
1354  qHandle.addToOutputList('PATHNAMECLOB','pathnameclob')
1355  qConditionStr='DATA_ID=:dataid'
1356  qCondition=coral.AttributeList()
1357  qCondition.extend('dataid','unsigned long long')
1358  qCondition['dataid'].setData(dataid)
1359  qResult=coral.AttributeList()
1360  qResult.extend('runnum','unsigned int')
1361  qResult.extend('datasource','string')
1362  qResult.extend('npath','unsigned int')
1363  qResult.extend('pathnameclob','string')
1364  qHandle.defineOutput(qResult)
1365  qHandle.setCondition(qConditionStr,qCondition)
1366  cursor=qHandle.execute()
1367  pathnameclob=None
1368  pathnames=[]
1369  while next(cursor):
1370  runnum=cursor.currentRow()['runnum'].data()
1371  datasource=cursor.currentRow()['datasource'].data()
1372  npath=cursor.currentRow()['npath'].data()
1373  pathnameclob=cursor.currentRow()['pathnameclob'].data()
1374  if pathnameclob:
1375  pathnames=pathnameclob.split(',')
1376  for pathnameidx,hltname in enumerate(pathnames):
1377  if hltpathname:
1378  if hltpathname==hltname:
1379  hltnamedict.append((pathnameidx,hltname))
1380  break
1381  elif hltpathpattern:
1382  if fnmatch.fnmatch(hltname,hltpathpattern):
1383  hltnamedict.append((pathnameidx,hltname))
1384  #else:
1385  #hltnamedict.append((pathnameidx,hltname))
1386  result=[runnum,datasource,npath,hltnamedict]
1387  except :
1388  del qHandle
1389  raise
1390  del qHandle
1391  return result
1392 
1393 def hlttrgMappingByrun(schema,runnum,hltpathname=None,hltpathpattern=None):
1394  '''
1395  select m.hltpathname,m.l1seed from cmsrunsummary r,trghltmap m where r.runnum=:runnum and m.hltkey=r.hltkey and [m.hltpathname=:hltpathname]
1396  output: {hltpath:l1seed}
1397  '''
1398  result={}
1399  queryHandle=schema.newQuery()
1402  if hltpathpattern and hltpathpattern in ['*','all','All','ALL']:
1403  hltpathpattern=None
1404  try:
1405  queryHandle.addToTableList(r)
1406  queryHandle.addToTableList(m)
1407  queryCondition=coral.AttributeList()
1408  queryCondition.extend('runnum','unsigned int')
1409  queryCondition['runnum'].setData(int(runnum))
1410  #queryHandle.addToOutputList(m+'.HLTKEY','hltkey')
1411  queryHandle.addToOutputList(m+'.HLTPATHNAME','hltpathname')
1412  queryHandle.addToOutputList(m+'.L1SEED','l1seed')
1413  conditionStr=r+'.RUNNUM=:runnum and '+m+'.HLTKEY='+r+'.HLTKEY'
1414  if hltpathname:
1415  hltpathpattern=None
1416  conditionStr+=' AND '+m+'.HLTPATHNAME=:hltpathname'
1417  queryCondition.extend('hltpathname','string')
1418  queryCondition['hltpathname'].setData(hltpathname)
1419  queryHandle.setCondition(conditionStr,queryCondition)
1420  queryResult=coral.AttributeList()
1421  queryResult.extend('pname','string')
1422  queryResult.extend('l1seed','string')
1423  queryHandle.defineOutput(queryResult)
1424  cursor=queryHandle.execute()
1425  while next(cursor):
1426  pname=cursor.currentRow()['pname'].data()
1427  l1seed=cursor.currentRow()['l1seed'].data()
1428  if hltpathname not in result:
1429  if hltpathpattern:
1430  if fnmatch.fnmatch(pname,hltpathpattern):
1431  result[pname]=l1seed
1432  else:
1433  result[pname]=l1seed
1434  except :
1435  del queryHandle
1436  raise
1437  del queryHandle
1438  return result
1439 
1440 def hltLSById(schema,dataid,hltpathname=None,hltpathpattern=None,withL1Pass=False,withHLTAccept=False):
1441  '''
1442  result (runnum, {cmslsnum:[(pathname,prescale,1lpass,hltaccept)](0)]}
1443  '''
1444  #print 'entering hltLSById '
1445  #t0=time.time()
1446  result={}
1447  hltrundata=hltRunById(schema,dataid,hltpathname=hltpathname,hltpathpattern=hltpathpattern)
1448  if not hltrundata:
1449  return result
1450  hltnamedict=hltrundata[3]
1451  if not hltnamedict:
1452  return (hltrundata[0],{})
1453  #tt1=time.time()
1454  #print '\thltrunbyid time ',tt1-t0
1455  #tt0=time.time()
1456  qHandle=schema.newQuery()
1457  try:
1458  qHandle.addToTableList(nameDealer.lshltTableName())
1459  qHandle.addToOutputList('RUNNUM','runnum')
1460  qHandle.addToOutputList('CMSLSNUM','cmslsnum')
1461  if len(hltnamedict)!=0:
1462  qHandle.addToOutputList('PRESCALEBLOB','prescaleblob')
1463  if withL1Pass:
1464  qHandle.addToOutputList('HLTCOUNTBLOB','hltcountblob')
1465  if withHLTAccept:
1466  qHandle.addToOutputList('HLTACCEPTBLOB','hltacceptblob')
1467  qConditionStr='DATA_ID=:dataid'
1468  qCondition=coral.AttributeList()
1469  qCondition.extend('dataid','unsigned long long')
1470  qCondition['dataid'].setData(dataid)
1471  qResult=coral.AttributeList()
1472  qResult.extend('runnum','unsigned int')
1473  qResult.extend('cmslsnum','unsigned int')
1474  if len(hltnamedict)!=0:
1475  qResult.extend('prescaleblob','blob')
1476  if withL1Pass:
1477  qResult.extend('hltcountblob','blob')
1478  if withHLTAccept:
1479  qResult.extend('hltacceptblob','blob')
1480  qHandle.defineOutput(qResult)
1481  qHandle.setCondition(qConditionStr,qCondition)
1482  cursor=qHandle.execute()
1483  while next(cursor):
1484  runnum=cursor.currentRow()['runnum'].data()
1485  cmslsnum=cursor.currentRow()['cmslsnum'].data()
1486  prescaleblob=None
1487  hltcountblob=None
1488  hltacceptblob=None
1489  if len(hltnamedict)!=0:
1490  prescaleblob=cursor.currentRow()['prescaleblob'].data()
1491  if withL1Pass:
1492  hltcountblob=cursor.currentRow()['hltcountblob'].data()
1493  if withHLTAccept:
1494  hltacceptblob=cursor.currentRow()['hltacceptblob'].data()
1495  if cmslsnum not in result:
1496  result[cmslsnum]=[]
1497  pathinfo=[]
1498  prescales=None
1499  hltcounts=None
1500  hltaccepts=None
1501  if prescaleblob:
1502  if runnum <150008: ###WORKAROUND PATCH!! because the 2010 blobs were packed as type l ###
1503  prescales=CommonUtil.unpackBlobtoArray(prescaleblob,'l')
1504  else:
1505  prescales=CommonUtil.unpackBlobtoArray(prescaleblob,'I')
1506  if hltcountblob:
1507  if runnum <150008: ###WORKAROUND PATCH!! because the 2010 blobs were packed as type l ###
1508  hltcounts=CommonUtil.unpackBlobtoArray(hltcountblob,'l')
1509  else:
1510  hltcounts=CommonUtil.unpackBlobtoArray(hltcountblob,'I')
1511  if hltacceptblob:
1512  if runnum <150008: ###WORKAROUND PATCH!! because the 2010 blobs were packed as type l ###
1513  hltaccepts=CommonUtil.unpackBlobtoArray(hltacceptblob,'l')
1514  else:
1515  hltaccepts=CommonUtil.unpackBlobtoArray(hltacceptblob,'I')
1516  for (hltpathidx,thispathname) in hltnamedict:#loop over selected paths
1517  thispresc=0
1518  thishltcount=0
1519  thisaccept=0
1520  if prescales:
1521  thispresc=prescales[hltpathidx]
1522  if hltcounts:
1523  thishltcount=hltcounts[hltpathidx]
1524  if hltaccepts:
1525  thisaccept=hltaccepts[hltpathidx]
1526  thispathinfo=(thispathname,thispresc,thishltcount,thisaccept)
1527  pathinfo.append(thispathinfo)
1528  result[cmslsnum]=pathinfo
1529  except :
1530  del qHandle
1531  raise
1532  del qHandle
1533  #tt1=time.time()
1534  #print '\tdb stuff time ',tt1-tt0
1535  #t1=time.time()
1536  #print 'tot hltLSById time ',t1-t0
1537  return (runnum,result)
1538 
1539 def intglumiForRange(schema,runlist):
1540  '''
1541  output: {run:intglumi_in_fb}
1542  '''
1543  result={}
1544  if not runlist:
1545  return result
1546  minrun=min(runlist)
1547  maxrun=max(runlist)
1548  qHandle=schema.newQuery()
1549  try:
1550  qHandle.addToTableList(nameDealer.intglumiv2TableName())
1551  qResult=coral.AttributeList()
1552  qResult.extend('RUNNUM','unsigned int')
1553  qResult.extend('INTGLUMI','float')
1554  qConditionStr='RUNNUM>=:minrun AND RUNNUM<=:maxrun'
1555  qCondition=coral.AttributeList()
1556  qCondition.extend('minrun','unsigned int')
1557  qCondition.extend('maxrun','unsigned int')
1558  qCondition['minrun'].setData(minrun)
1559  qCondition['maxrun'].setData(maxrun)
1560  qHandle.addToOutputList('RUNNUM')
1561  qHandle.addToOutputList('INTGLUMI')
1562  qHandle.setCondition(qConditionStr,qCondition)
1563  qHandle.defineOutput(qResult)
1564  cursor=qHandle.execute()
1565  while next(cursor):
1566  runnum=cursor.currentRow()['RUNNUM'].data()
1567  intglumi=cursor.currentRow()['INTGLUMI'].data()
1568  result[runnum]=intglumi
1569  except :
1570  del qHandle
1571  raise
1572  del qHandle
1573  return result
1574 
1575 def fillschemePatternMap(schema,lumitype):
1576  '''
1577  output:(patternStr:correctionFac)
1578  '''
1579  if lumitype not in ['PIXEL','HF']:
1580  raise ValueError('[ERROR] unsupported lumitype '+lumitype)
1581  correctorField='CORRECTIONFACTOR'
1582  if lumitype=='PIXEL':
1583  correctorField='PIXELCORRECTIONFACTOR'
1584  result={}
1585  qHandle=schema.newQuery()
1586  try:
1587  qHandle.addToTableList(nameDealer.fillschemeTableName())
1588  qResult=coral.AttributeList()
1589  qResult.extend('FILLSCHEMEPATTERN','string')
1590  qResult.extend('CORRECTIONFACTOR','float')
1591  qHandle.defineOutput(qResult)
1592  qHandle.addToOutputList('FILLSCHEMEPATTERN')
1593  qHandle.addToOutputList(correctorField)
1594  cursor=qHandle.execute()
1595  while next(cursor):
1596  fillschemePattern=cursor.currentRow()['FILLSCHEMEPATTERN'].data()
1597  afterglowfac=cursor.currentRow()['CORRECTIONFACTOR'].data()
1598  result[fillschemePattern]=afterglowfac
1599  except :
1600  del qHandle
1601  raise
1602  del qHandle
1603  return result
1604 
1605 def guessLumiDataIdByRunInBranch(schema,runnum,tablename,branchName):
1606  revlist=revisionDML.revisionsInBranchName(schema,branchName)
1607  lumientry_id=revisionDML.entryInBranch(schema,tablename,str(runnum),branchName)
1608  latestrevision=revisionDML.latestDataRevisionOfEntry(schema,tablename,lumientry_id,revlist)
1609  return latestrevision
1610 
1611 def guessTrgDataIdByRunInBranch(schema,runnum,tablename,branchName):
1612  revlist=revisionDML.revisionsInBranchName(schema,branchName)
1613  trgentry_id=revisionDML.entryInBranch(schema,tablename,str(runnum),branchName)
1614  latestrevision=revisionDML.latestDataRevisionOfEntry(schema,tablename,trgentry_id,revlist)
1615  return latestrevision
1616 
1617 def guessHltDataIdByRunInBranch(schema,runnum,tablename,branchName):
1618  revlist=revisionDML.revisionsInBranchName(schema,branchName)
1619  hltentry_id=revisionDML.entryInBranch(schema,tablename,str(runnum),branchName)
1620  latestrevision=revisionDML.latestDataRevisionOfEntry(schema,tablename,hltentry_id,revlist)
1621  return latestrevision
1622 
1623 def guessDataIdByRun(schema,runnum,tablename,revfilter=None):
1624  '''
1625  select max data_id of the given run. In current design, it's the most recent data of the run
1626  '''
1627  result=None
1628  ids=[]
1629  qHandle=schema.newQuery()
1630  try:
1631  qHandle.addToTableList(tablename)
1632  qHandle.addToOutputList('DATA_ID')
1633  qConditionStr='RUNNUM=:runnum '
1634  qCondition=coral.AttributeList()
1635  qCondition.extend('runnum','unsigned int')
1636  qCondition['runnum'].setData(runnum)
1637  qResult=coral.AttributeList()
1638  qResult.extend('DATA_ID','unsigned long long')
1639  qHandle.defineOutput(qResult)
1640  qHandle.setCondition(qConditionStr,qCondition)
1641  cursor=qHandle.execute()
1642  while next(cursor):
1643  dataid=cursor.currentRow()['DATA_ID'].data()
1644  ids.append(dataid)
1645  except :
1646  del qHandle
1647  raise
1648  del qHandle
1649  if len(ids)>0 :
1650  return max(ids)
1651  else:
1652  return result
1653 
1654 def guessDataIdForRange(schema,inputRange,tablename):
1655  '''
1656  input: inputRange [run]
1657  output: {run:lumiid}
1658  select data_id,runnum from hltdata where runnum<=runmax and runnum>=:runmin
1659  '''
1660  result={}
1661  if not inputRange : return result
1662  if len(inputRange)==1:
1663  trgid=guessDataIdByRun(schema,inputRange[0],tablename)
1664  result[inputRange[0]]=trgid
1665  return result
1666  rmin=min(inputRange)
1667  rmax=max(inputRange)
1668  result=dict.fromkeys(inputRange,None)
1669  qHandle=schema.newQuery()
1670  try:
1671  qHandle.addToTableList(tablename)
1672  qHandle.addToOutputList('DATA_ID')
1673  qHandle.addToOutputList('RUNNUM')
1674  qConditionStr='RUNNUM>=:rmin'
1675  qCondition=coral.AttributeList()
1676  qCondition.extend('rmin','unsigned int')
1677  qCondition['rmin'].setData(rmin)
1678  if rmin!=rmax:
1679  qConditionStr+=' AND RUNNUM<=:rmax'
1680  qCondition.extend('rmax','unsigned int')
1681  qCondition['rmax'].setData(rmax)
1682  qResult=coral.AttributeList()
1683  qResult.extend('DATA_ID','unsigned long long')
1684  qResult.extend('RUNNUM','unsigned int')
1685  qHandle.defineOutput(qResult)
1686  qHandle.setCondition(qConditionStr,qCondition)
1687  cursor=qHandle.execute()
1688  while next(cursor):
1689  dataid=cursor.currentRow()['DATA_ID'].data()
1690  runnum=cursor.currentRow()['RUNNUM'].data()
1691  if runnum in result:
1692  if dataid>result[runnum]:
1693  result[runnum]=dataid
1694  except :
1695  del qHandle
1696  raise
1697  del qHandle
1698  return result
1699 #def guessAllDataIdByRun(schema,runnum):
1700 # '''
1701 # get dataids by runnumber, if there are duplicates, pick max(dataid).Bypass full version lookups
1702 # result (lumidataid(0),trgdataid(1),hltdataid(2))
1703 # '''
1704 # lumiids=[]
1705 # trgids=[]
1706 # hltids=[]
1707 # qHandle=schema.newQuery()
1708 # try:
1709 # qHandle.addToTableList(nameDealer.lumidataTableName(),'l')
1710 # qHandle.addToTableList(nameDealer.trgdataTableName(),'t')
1711 # qHandle.addToTableList(nameDealer.hltdataTableName(),'h')
1712 # qHandle.addToOutputList('l.DATA_ID','lumidataid')
1713 # qHandle.addToOutputList('t.DATA_ID','trgdataid')
1714 # qHandle.addToOutputList('h.DATA_ID','hltdataid')
1715 # qConditionStr='l.RUNNUM=t.RUNNUM and t.RUNNUM=h.RUNNUM and l.RUNNUM=:runnum '
1716 # qCondition=coral.AttributeList()
1717 # qCondition.extend('runnum','unsigned int')
1718 # qCondition['runnum'].setData(runnum)
1719 # qResult=coral.AttributeList()
1720 # qResult.extend('lumidataid','unsigned long long')
1721 # qResult.extend('trgdataid','unsigned long long')
1722 # qResult.extend('hltdataid','unsigned long long')
1723 # qHandle.defineOutput(qResult)
1724 # qHandle.setCondition(qConditionStr,qCondition)
1725 # cursor=qHandle.execute()
1726 # while cursor.next():
1727 # lumidataid=cursor.currentRow()['lumidataid'].data()
1728 # trgdataid=cursor.currentRow()['trgdataid'].data()
1729 # hltdataid=cursor.currentRow()['hltdataid'].data()
1730 # lumiids.append(lumidataid)
1731 # trgids.append(trgdataid)
1732 # hltids.append(hltdataid)
1733 # except :
1734 # del qHandle
1735 # raise
1736 # del qHandle
1737 # if len(lumiids)>0 and len(trgids)>0 and len(hltids)>0:
1738 # return (max(lumiids),max(trgids),max(hltids))
1739 # else:
1740 # return (None,None,None)
1741 
1742 def guessnormIdByContext(schema,amodetag,egev1):
1743  '''
1744  get norm dataids by amodetag, egev if there are duplicates, pick max(dataid).Bypass full version lookups
1745  select data_id from luminorm where amodetag=:amodetag and egev_1=:egev1
1746  '''
1747  luminormids=[]
1748  qHandle=schema.newQuery()
1749  egevmin=egev1*0.95
1750  egevmax=egev1*1.05
1751  try:
1752  qHandle.addToTableList( nameDealer.luminormTableName() )
1753  qHandle.addToOutputList('DATA_ID','normdataid')
1754  qConditionStr='AMODETAG=:amodetag AND EGEV_1>=:egevmin AND EGEV_1<=:egevmax'
1755  qCondition=coral.AttributeList()
1756  qCondition.extend('amodetag','string')
1757  qCondition.extend('egevmin','unsigned int')
1758  qCondition.extend('egevmax','unsigned int')
1759  qCondition['amodetag'].setData(amodetag)
1760  qCondition['egevmin'].setData(int(egevmin))
1761  qCondition['egevmax'].setData(int(egevmax))
1762  qResult=coral.AttributeList()
1763  qResult.extend('normdataid','unsigned long long')
1764  qHandle.defineOutput(qResult)
1765  qHandle.setCondition(qConditionStr,qCondition)
1766  cursor=qHandle.execute()
1767  while next(cursor):
1768  normdataid=cursor.currentRow()['normdataid'].data()
1769  luminormids.append(normdataid)
1770  except :
1771  del qHandle
1772  raise
1773  del qHandle
1774  if len(luminormids) !=0:return max(luminormids)
1775  return None
1776 
1777 def guessnormIdByName(schema,normname):
1778  '''
1779  get norm dataids by name, if there are duplicates, pick max(dataid).Bypass full version lookups
1780  select data_id from luminorms where entry_name=:normname
1781  result luminormdataid
1782  '''
1783  luminormids=[]
1784  qHandle=schema.newQuery()
1785  try:
1786  qHandle.addToTableList( nameDealer.luminormTableName() )
1787  qHandle.addToOutputList('DATA_ID','normdataid')
1788  qConditionStr='ENTRY_NAME=:normname '
1789  qCondition=coral.AttributeList()
1790  qCondition.extend('normname','string')
1791  qCondition['normname'].setData(normname)
1792  qResult=coral.AttributeList()
1793  qResult.extend('normdataid','unsigned long long')
1794  qHandle.defineOutput(qResult)
1795  qHandle.setCondition(qConditionStr,qCondition)
1796  cursor=qHandle.execute()
1797  while next(cursor):
1798  normdataid=cursor.currentRow()['normdataid'].data()
1799  luminormids.append(normdataid)
1800  except :
1801  del qHandle
1802  raise
1803  del qHandle
1804  if len(luminormids) !=0:return max(luminormids)
1805  return None
1806 
1807 ########
1808 ########
1809 def dataentryIdByRun(schema,runnum,branchfilter):
1810  '''
1811  select el.entry_id,et.entry_id,eh.entry_id,el.revision_id,et.revision_id,eh.revision_id from lumidataentiries el,trgdataentries et,hltdataentries eh where el.name=et.name and et.name=eh.name and el.name=:entryname;
1812  check on entryrev
1813 
1814  return [lumientryid,trgentryid,hltentryid]
1815  '''
1816  result=[]
1817  qHandle=schema.newQuery()
1818  try:
1819  qHandle.addToTableList(nameDealer.entryTableName( lumidataTableName() ))
1820  qHandle.addToTableList(nameDealer.entryTableName( trgdataTableName() ))
1821  qHandle.addToTableList(nameDealer.entryTableName( hltdataTableName() ))
1822  qHandle.addToOutputList(lumidataTableName()+'.ENTRY_ID','lumientryid')
1823  qHandle.addToOutputList(trgdataTableName()+'.ENTRY_ID','trgentryid')
1824  qHandle.addToOutputList(hltdataTableName()+'.ENTRY_ID','hltentryid')
1825  qConditionStr=lumidataTableName()+'.NAME='+trgdataTableName()+'.NAME AND '+trgdataTableName()+'.NAME='+hltdataTableName()+'.NAME AND '+lumidataTableName()+'.NAME=:runnumstr'
1826  qCondition=coral.AttributeList()
1827  qCondition.extend('runnumstr','string')
1828  qCondition['runnumstr'].setData(str(runnum))
1829  qResult=coral.AttributeList()
1830  qResult.extend('lumientryid','unsigned long long')
1831  qResult.extend('trgentryid','unsigned long long')
1832  qResult.extend('hltentryid','unsigned long long')
1833  qHandle.defineOutput(qResult)
1834  qHandle.setCondition(qConditionStr,qCondition)
1835  cursor=qHandle.execute()
1836  while next(cursor):
1837  lumientryid=cursor.currentRow()['lumientryid'].data()
1838  trgentryid=cursor.currentRow()['trgentryid'].data()
1839  hltentryid=cursor.currentRow()['hltentryid'].data()
1840  if lumientryid in branchfilter and trgentryid in branchfilter and hltentryid in branchfilter:
1841  result.extend([lumientryid,trgentryid,hltentryid])
1842  except:
1843  del qHandle
1844  raise
1845  del qHandle
1846  return result
1847 
1848 def latestdataIdByEntry(schema,entryid,datatype,branchfilter):
1849  '''
1850  select l.data_id,rl.revision_id from lumidatatable l,lumirevisions rl where l.data_id=rl.data_id and l.entry_id=:entryid
1851  check revision_id is in branch
1852  '''
1853  dataids=[]
1854  datatablename=''
1855  revmaptablename=''
1856  if datatype=='lumi':
1857  datatablename=nameDealer.lumidataTableName()
1858  elif datatype=='trg':
1859  datatablename=nameDealer.trgdataTableName()
1860  elif dataytpe=='hlt':
1861  tablename=nameDealer.hltdataTableName()
1862  else:
1863  raise RunTimeError('datatype '+datatype+' is not supported')
1864  revmaptablename=nameDealer.revmapTableName(datatablename)
1865  qHandle=schema.newQuery()
1866  try:
1867  qHandle.addToTableList(revmaptablename)
1868  qHandle.addToTableList(datatablename)
1869  qHandle.addToOutputList('l.DATA_ID','dataid')
1870  qHandle.addToOutputList(revmaptablename+'.REVISION_ID','revisionid')
1871  qConditionStr=datatablename+'.DATA_ID='+revmaptablename+'.DATA_ID AND '+datatablename+'.ENTRY_ID=:entryid'
1872  qCondition=coral.AttributeList()
1873  qCondition.extend('entryid','unsigned long long')
1874  qResult=coral.AttributeList()
1875  qResult.extend('dataid','unsigned long long')
1876  qResult.extend('revisionid','unsigned long long')
1877  qHandle.defineOutput(qResult)
1878  qHandle.setCondition(qConditionStr,qCondition)
1879  cursor=qHandle.execute()
1880  while next(cursor):
1881  dataid=cursor.currentRow()['dataid'].data()
1882  revisionid=cursor.currentRow()['revisionid'].data()
1883  if revisionid in branchfilter:
1884  dataids.append(dataid)
1885  except:
1886  del qHandle
1887  raise
1888  del qHandle
1889  if len(dataids)!=0:return max(dataids)
1890  return None
1891 
1892 
1893 #=======================================================
1894 # INSERT requires in update transaction
1895 #=======================================================
1896 def addNormToBranch(schema,normname,amodetag,norm1,egev1,optionalnormdata,branchinfo):
1897  '''
1898  input:
1899  branchinfo(normrevisionid,branchname)
1900  optionalnormdata {'norm_occ2':norm_occ2,'norm_et':norm_et,'norm_pu':norm_pu,'constfactor':constfactor}
1901  output:
1902  (revision_id,entry_id,data_id)
1903  '''
1904  #print 'branchinfo ',branchinfo
1905  norm_occ2=1.0
1906  if 'normOcc2' in optionalnormdata:
1907  norm_occ2=optionalnormdata['norm_occ2']
1908  norm_et=1.0
1909  if 'norm_et' in optionalnormdata:
1910  norm_et=optionalnormdata['norm_et']
1911  norm_pu=1.0
1912  if 'norm_pu' in optionalnormdata:
1913  norm_pu=optionalnormdata['norm_pu']
1914  constfactor=1.0
1915  if 'constfactor' in optionalnormdata:
1916  constfactor=optionalnormdata['constfactor']
1917  try:
1918  entry_id=revisionDML.entryInBranch(schema,nameDealer.luminormTableName(),normname,branchinfo[1])
1919  if entry_id is None:
1920  (revision_id,entry_id,data_id)=revisionDML.bookNewEntry(schema,nameDealer.luminormTableName())
1921  entryinfo=(revision_id,entry_id,normname,data_id)
1922  revisionDML.addEntry(schema,nameDealer.luminormTableName(),entryinfo,branchinfo)
1923  else:
1924  (revision_id,data_id)=revisionDML.bookNewRevision( schema,nameDealer.luminormTableName() )
1925  revisionDML.addRevision(schema,nameDealer.luminormTableName(),(revision_id,data_id),branchinfo)
1926  tabrowDefDict={'DATA_ID':'unsigned long long','ENTRY_ID':'unsigned long long','ENTRY_NAME':'string','AMODETAG':'string','NORM_1':'float','EGEV_1':'unsigned int','NORM_OCC2':'float','NORM_ET':'float','NORM_PU':'float','CONSTFACTOR':'float'}
1927  tabrowValueDict={'DATA_ID':data_id,'ENTRY_ID':entry_id,'ENTRY_NAME':normname,'AMODETAG':amodetag,'NORM_1':norm1,'EGEV_1':egev1,'NORM_OCC2':norm_occ2,'NORM_ET':norm_et,'NORM_PU':norm_pu,'CONSTFACTOR':constfactor}
1928  db=dbUtil.dbUtil(schema)
1929  db.insertOneRow(nameDealer.luminormTableName(),tabrowDefDict,tabrowValueDict)
1930  return (revision_id,entry_id,data_id)
1931  except :
1932  raise
1933 
1934 def addCorrToBranch(schema,corrname,a1,optionalcorrdata,branchinfo):
1935  '''
1936  input:
1937  branchinfo(corrrevisionid,branchname)
1938  optionalcorrdata {'a2':a2,'drift':drif}
1939  output:
1940  (revision_id,entry_id,data_id)
1941  '''
1942  a2=1.0
1943  if 'a2' in optionalcorrdata:
1944  a2=optionalcorrdata['a2']
1945  drift=1.0
1946  if 'drift' in optionalcorrdata:
1947  drift=optionalcorrdata['drift']
1948  try:
1949  entry_id=revisionDML.entryInBranch(schema,nameDealer.lumicorrectionsTableName(),corrname,branchinfo[1])
1950  if entry_id is None:
1951  (revision_id,entry_id,data_id)=revisionDML.bookNewEntry(schema,nameDealer.lumicorrectionsTableName())
1952  entryinfo=(revision_id,entry_id,corrname,data_id)
1953  revisionDML.addEntry(schema,nameDealer.lumicorrectionsTableName(),entryinfo,branchinfo)
1954  else:
1955  (revision_id,data_id)=revisionDML.bookNewRevision( schema,nameDealer.lumicorrectionsTableName() )
1956  revisionDML.addRevision(schema,nameDealer.lumicorrectionsTableName(),(revision_id,data_id),branchinfo)
1957  tabrowDefDict={'DATA_ID':'unsigned long long','ENTRY_ID':'unsigned long long','ENTRY_NAME':'string','A1':'float','A2':'float','DRIFT':'float'}
1958  tabrowValueDict={'DATA_ID':data_id,'ENTRY_ID':entry_id,'ENTRY_NAME':corrname,'A1':a1,'A2':a2,'DRIFT':drift}
1959  db=dbUtil.dbUtil(schema)
1960  db.insertOneRow(nameDealer.lumicorrectionsTableName(),tabrowDefDict,tabrowValueDict)
1961  return (revision_id,entry_id,data_id)
1962  except :
1963  raise
1964 
1965 def addLumiRunDataToBranch(schema,runnumber,lumirundata,branchinfo,tableName):
1966  '''
1967  input:
1968  lumirundata [datasource,nominalenergy,ncollidingbunches,starttime,stoptime,nls]
1969  branchinfo (branch_id,branch_name)
1970  tableName lumiruntablename
1971  output:
1972  (revision_id,entry_id,data_id)
1973  '''
1974  try:
1975  datasource=lumirundata[0]
1976  nominalegev=3500.0
1977  ncollidingbunches=0
1978  starttime=coral.TimeStamp()
1979  stoptime=coral.TimeStamp()
1980  nls=0
1981  if len(lumirundata)>1:
1982  nominalenergy=lumirundata[1]
1983  ncollidingbunches=lumirundata[2]
1984  starttime=lumirundata[3]
1985  stoptime=lumirundata[4]
1986  nls=lumirundata[5]
1987  entry_id=revisionDML.entryInBranch(schema,tableName,str(runnumber),branchinfo[1])
1988  if entry_id is None:
1989  (revision_id,entry_id,data_id)=revisionDML.bookNewEntry(schema,tableName)
1990  entryinfo=(revision_id,entry_id,str(runnumber),data_id)
1991  revisionDML.addEntry(schema,tableName,entryinfo,branchinfo)
1992  else:
1993  (revision_id,data_id)=revisionDML.bookNewRevision(schema,tableName)
1994  #print 'revision_id,data_id ',revision_id,data_id
1995  revisionDML.addRevision(schema,tableName,(revision_id,data_id),branchinfo)
1996  tabrowDefDict={'DATA_ID':'unsigned long long','ENTRY_ID':'unsigned long long','ENTRY_NAME':'string','RUNNUM':'unsigned int','SOURCE':'string','NOMINALEGEV':'float','NCOLLIDINGBUNCHES':'unsigned int','STARTTIME':'time stamp','STOPTIME':'time stamp','NLS':'unsigned int'}
1997  tabrowValueDict={'DATA_ID':data_id,'ENTRY_ID':entry_id,'ENTRY_NAME':str(runnumber),'RUNNUM':int(runnumber),'SOURCE':datasource,'NOMINALEGEV':nominalegev,'NCOLLIDINGBUNCHES':ncollidingbunches,'STARTTIME':starttime,'STOPTIME':stoptime,'NLS':nls}
1998  db=dbUtil.dbUtil(schema)
1999  db.insertOneRow(tableName,tabrowDefDict,tabrowValueDict)
2000  return (revision_id,entry_id,data_id)
2001  except :
2002  raise
2003 
2004 def addTrgRunDataToBranch(schema,runnumber,trgrundata,branchinfo):
2005  '''
2006  input:
2007  trgrundata [datasource(0),bitzeroname(1),bitnameclob(2)]
2008  bitnames clob, bitnames separated by ','
2009  output:
2010  (revision_id,entry_id,data_id)
2011  '''
2012  try: #fixme: need to consider revision only case
2013  datasource=trgrundata[0]
2014  bitzeroname=trgrundata[1]
2015  bitnames=trgrundata[2]
2016  entry_id=revisionDML.entryInBranch(schema,nameDealer.trgdataTableName(),str(runnumber),branchinfo[1])
2017  if entry_id is None:
2018  (revision_id,entry_id,data_id)=revisionDML.bookNewEntry(schema,nameDealer.trgdataTableName())
2019  entryinfo=(revision_id,entry_id,str(runnumber),data_id)
2020  revisionDML.addEntry(schema,nameDealer.trgdataTableName(),entryinfo,branchinfo)
2021  else:
2022  (revision_id,data_id)=revisionDML.bookNewRevision( schema,nameDealer.trgdataTableName() )
2023  revisionDML.addRevision(schema,nameDealer.trgdataTableName(),(revision_id,data_id),branchinfo)
2024  tabrowDefDict={'DATA_ID':'unsigned long long','ENTRY_ID':'unsigned long long','ENTRY_NAME':'string','SOURCE':'string','RUNNUM':'unsigned int','BITZERONAME':'string','BITNAMECLOB':'string'}
2025  tabrowValueDict={'DATA_ID':data_id,'ENTRY_ID':entry_id,'ENTRY_NAME':str(runnumber),'SOURCE':datasource,'RUNNUM':int(runnumber),'BITZERONAME':bitzeroname,'BITNAMECLOB':bitnames}
2026  db=dbUtil.dbUtil(schema)
2027  db.insertOneRow(nameDealer.trgdataTableName(),tabrowDefDict,tabrowValueDict)
2028  return (revision_id,entry_id,data_id)
2029  except :
2030  raise
2031 def addHLTRunDataToBranch(schema,runnumber,hltrundata,branchinfo):
2032  '''
2033  input:
2034  hltrundata [pathnameclob(0),datasource(1)]
2035  output:
2036  (revision_id,entry_id,data_id)
2037  '''
2038  try:
2039  pathnames=hltrundata[0]
2040  datasource=hltrundata[1]
2041  npath=len(pathnames.split(','))
2042  entry_id=revisionDML.entryInBranch(schema,nameDealer.hltdataTableName(),str(runnumber),branchinfo[1])
2043  if entry_id is None:
2044  (revision_id,entry_id,data_id)=revisionDML.bookNewEntry(schema,nameDealer.hltdataTableName())
2045  entryinfo=(revision_id,entry_id,str(runnumber),data_id)
2046  revisionDML.addEntry(schema,nameDealer.hltdataTableName(),entryinfo,branchinfo)
2047  else:
2048  (revision_id,data_id)=revisionDML.bookNewRevision( schema,nameDealer.hltdataTableName() )
2049  revisionDML.addRevision(schema,nameDealer.hltdataTableName(),(revision_id,data_id),branchinfo)
2050  tabrowDefDict={'DATA_ID':'unsigned long long','ENTRY_ID':'unsigned long long','ENTRY_NAME':'string','RUNNUM':'unsigned int','SOURCE':'string','NPATH':'unsigned int','PATHNAMECLOB':'string'}
2051  tabrowValueDict={'DATA_ID':data_id,'ENTRY_ID':entry_id,'ENTRY_NAME':str(runnumber),'RUNNUM':int(runnumber),'SOURCE':datasource,'NPATH':npath,'PATHNAMECLOB':pathnames}
2052  db=dbUtil.dbUtil(schema)
2053  db.insertOneRow(nameDealer.hltdataTableName(),tabrowDefDict,tabrowValueDict)
2054  return (revision_id,entry_id,data_id)
2055  except :
2056  raise
2057 
2058 def insertRunSummaryData(schema,runnumber,runsummarydata,complementalOnly=False):
2059  '''
2060  input:
2061  runsummarydata [l1key,amodetag,egev,sequence,hltkey,fillnum,starttime,stoptime]
2062  output:
2063  '''
2064  l1key=runsummarydata[0]
2065  amodetag=runsummarydata[1]
2066  egev=runsummarydata[2]
2067  hltkey=''
2068  fillnum=0
2069  sequence=''
2070  starttime=''
2071  stoptime=''
2072  if not complementalOnly:
2073  sequence=runsummarydata[3]
2074  hltkey=runsummarydata[4]
2075  fillnum=runsummarydata[5]
2076  starttime=runsummarydata[6]
2077  stoptime=runsummarydata[7]
2078  try:
2079  if not complementalOnly:
2080  tabrowDefDict={'RUNNUM':'unsigned int','L1KEY':'string','AMODETAG':'string','EGEV':'unsigned int','SEQUENCE':'string','HLTKEY':'string','FILLNUM':'unsigned int','STARTTIME':'time stamp','STOPTIME':'time stamp'}
2081  tabrowValueDict={'RUNNUM':int(runnumber),'L1KEY':l1key,'AMODETAG':amodetag,'EGEV':int(egev),'SEQUENCE':sequence,'HLTKEY':hltkey,'FILLNUM':int(fillnum),'STARTTIME':starttime,'STOPTIME':stoptime}
2082  db=dbUtil.dbUtil(schema)
2083  db.insertOneRow(nameDealer.cmsrunsummaryTableName(),tabrowDefDict,tabrowValueDict)
2084  else:
2085  setClause='L1KEY=:l1key,AMODETAG=:amodetag,EGEV=:egev'
2086  updateCondition='RUNNUM=:runnum'
2087  inputData=coral.AttributeList()
2088  inputData.extend('l1key','string')
2089  inputData.extend('amodetag','string')
2090  inputData.extend('egev','unsigned int')
2091  inputData.extend('runnum','unsigned int')
2092  inputData['l1key'].setData(l1key)
2093  inputData['amodetag'].setData(amodetag)
2094  inputData['egev'].setData(int(egev))
2095  inputData['runnum'].setData(int(runnumber))
2096  db=dbUtil.dbUtil(schema)
2097  db.singleUpdate(nameDealer.cmsrunsummaryTableName(),setClause,updateCondition,inputData)
2098  except :
2099  raise
2100 def insertTrgHltMap(schema,hltkey,trghltmap):
2101  '''
2102  input:
2103  trghltmap {hltpath:l1seed}
2104  output:
2105  '''
2106  hltkeyExists=False
2107  nrows=0
2108  try:
2109  kQueryBindList=coral.AttributeList()
2110  kQueryBindList.extend('hltkey','string')
2111  kQuery=schema.newQuery()
2112  kQuery.addToTableList(nameDealer.trghltMapTableName())
2113  kQuery.setCondition('HLTKEY=:hltkey',kQueryBindList)
2114  kQueryBindList['hltkey'].setData(hltkey)
2115  kResult=kQuery.execute()
2116  while next(kResult):
2117  hltkeyExists=True
2118  if not hltkeyExists:
2119  bulkvalues=[]
2120  trghltDefDict=[('HLTKEY','string'),('HLTPATHNAME','string'),('L1SEED','string')]
2121  for hltpath,l1seed in trghltmap.items():
2122  bulkvalues.append([('HLTKEY',hltkey),('HLTPATHNAME',hltpath),('L1SEED',l1seed)])
2123  db=dbUtil.dbUtil(schema)
2124  db.bulkInsert(nameDealer.trghltMapTableName(),trghltDefDict,bulkvalues)
2125  nrows=len(bulkvalues)
2126  return nrows
2127  except :
2128  print('error in insertTrgHltMap ')
2129  raise
2130 def bulkInsertTrgLSData(session,runnumber,data_id,trglsdata,bulksize=500):
2131  '''
2132  insert trg per-LS data for given run and data_id, this operation can be split in transaction chuncks
2133  input:
2134  trglsdata {cmslsnum:[deadtime,bitzerocount,bitzeroprescale,trgcountBlob,trgprescaleBlob]}
2135  result nrows inserted
2136  if nrows==0, then this insertion failed
2137  '''
2138  print('total number of trg rows ',len(trglsdata))
2139  lstrgDefDict=[('DATA_ID','unsigned long long'),('RUNNUM','unsigned int'),('CMSLSNUM','unsigned int'),('DEADTIMECOUNT','unsigned long long'),('BITZEROCOUNT','unsigned int'),('BITZEROPRESCALE','unsigned int'),('PRESCALEBLOB','blob'),('TRGCOUNTBLOB','blob')]
2140  committedrows=0
2141  nrows=0
2142  bulkvalues=[]
2143  try:
2144  for cmslsnum,perlstrg in trglsdata.items():
2145  deadtimecount=perlstrg[0]
2146  bitzerocount=perlstrg[1]
2147  bitzeroprescale=perlstrg[2]
2148  trgcountblob=perlstrg[3]
2149  trgprescaleblob=perlstrg[4]
2150  bulkvalues.append([('DATA_ID',data_id),('RUNNUM',runnumber),('CMSLSNUM',cmslsnum),('DEADTIMECOUNT',deadtimecount),('BITZEROCOUNT',bitzerocount),('BITZEROPRESCALE',bitzeroprescale),('PRESCALEBLOB',trgprescaleblob),('TRGCOUNTBLOB',trgcountblob)])
2151  nrows+=1
2152  committedrows+=1
2153  if nrows==bulksize:
2154  print('committing trg in LS chunck ',nrows)
2155  db=dbUtil.dbUtil(session.nominalSchema())
2156  session.transaction().start(False)
2157  db.bulkInsert(nameDealer.lstrgTableName(),lstrgDefDict,bulkvalues)
2158  session.transaction().commit()
2159  nrows=0
2160  bulkvalues=[]
2161  elif committedrows==len(trglsdata):
2162  print('committing trg at the end ')
2163  db=dbUtil.dbUtil(session.nominalSchema())
2164  session.transaction().start(False)
2165  db.bulkInsert(nameDealer.lstrgTableName(),lstrgDefDict,bulkvalues)
2166  session.transaction().commit()
2167  except :
2168  print('error in bulkInsertTrgLSData')
2169  raise
2170 def bulkInsertHltLSData(session,runnumber,data_id,hltlsdata,bulksize=500):
2171  '''
2172  input:
2173  hltlsdata {cmslsnum:[inputcountBlob,acceptcountBlob,prescaleBlob]}
2174  '''
2175  print('total number of hlt rows ',len(hltlsdata))
2176  lshltDefDict=[('DATA_ID','unsigned long long'),('RUNNUM','unsigned int'),('CMSLSNUM','unsigned int'),('PRESCALEBLOB','blob'),('HLTCOUNTBLOB','blob'),('HLTACCEPTBLOB','blob')]
2177  committedrows=0
2178  nrows=0
2179  bulkvalues=[]
2180  try:
2181  for cmslsnum,perlshlt in hltlsdata.items():
2182  inputcountblob=perlshlt[0]
2183  acceptcountblob=perlshlt[1]
2184  prescaleblob=perlshlt[2]
2185  bulkvalues.append([('DATA_ID',data_id),('RUNNUM',runnumber),('CMSLSNUM',cmslsnum),('PRESCALEBLOB',prescaleblob),('HLTCOUNTBLOB',inputcountblob),('HLTACCEPTBLOB',acceptcountblob)])
2186 
2187  nrows+=1
2188  committedrows+=1
2189  if nrows==bulksize:
2190  print('committing hlt in LS chunck ',nrows)
2191  db=dbUtil.dbUtil(session.nominalSchema())
2192  session.transaction().start(False)
2193  db.bulkInsert(nameDealer.lshltTableName(),lshltDefDict,bulkvalues)
2194  session.transaction().commit()
2195  nrows=0
2196  bulkvalues=[]
2197  elif committedrows==len(hltlsdata):
2198  print('committing hlt at the end ')
2199  db=dbUtil.dbUtil(session.nominalSchema())
2200  session.transaction().start(False)
2201  db.bulkInsert(nameDealer.lshltTableName(),lshltDefDict,bulkvalues)
2202  session.transaction().commit()
2203  except :
2204  print('error in bulkInsertHltLSData')
2205  raise
2206 
2207 def bulkInsertLumiLSSummary(session,runnumber,data_id,lumilsdata,tableName,bulksize=500,withDetails=True):
2208  '''
2209  input:
2210  lumilsdata {lumilsnum:[cmslsnum,instlumi,instlumierror,instlumiquality,beamstatus,beamenergy,numorbit,startorbit,cmsbxindexblob,beam1intensity,beam2intensity,bxlumivalue_occ1,bxlumierror_occ1,bxlumiquality_occ1,bxlumivalue_occ2,bxlumierror_occ2,bxlumiquality_occ2,bxlumivalue_et,bxlumierror_et,bxlumiquality_et]}
2211  '''
2212  lslumiDefDict=[]
2213  if withDetails:
2214  lslumiDefDict=[('DATA_ID','unsigned long long'),('RUNNUM','unsigned int'),('LUMILSNUM','unsigned int'),('CMSLSNUM','unsigned int'),('INSTLUMI','float'),('INSTLUMIERROR','float'),('INSTLUMIQUALITY','short'),('BEAMSTATUS','string'),('BEAMENERGY','float'),('NUMORBIT','unsigned int'),('STARTORBIT','unsigned int'),('CMSBXINDEXBLOB','blob'),('BEAMINTENSITYBLOB_1','blob'),('BEAMINTENSITYBLOB_2','blob'),('BXLUMIVALUE_OCC1','blob'),('BXLUMIERROR_OCC1','blob'),('BXLUMIQUALITY_OCC1','blob'),('BXLUMIVALUE_OCC2','blob'),('BXLUMIERROR_OCC2','blob'),('BXLUMIQUALITY_OCC2','blob'),('BXLUMIVALUE_ET','blob'),('BXLUMIERROR_ET','blob'),('BXLUMIQUALITY_ET','blob')]
2215  else:
2216  lslumiDefDict=[('DATA_ID','unsigned long long'),('RUNNUM','unsigned int'),('LUMILSNUM','unsigned int'),('CMSLSNUM','unsigned int'),('INSTLUMI','float'),('INSTLUMIERROR','float'),('INSTLUMIQUALITY','short'),('BEAMSTATUS','string'),('BEAMENERGY','float'),('NUMORBIT','unsigned int'),('STARTORBIT','unsigned int')]
2217  print('total number of lumi rows ',len(lumilsdata))
2218  try:
2219  committedrows=0
2220  nrows=0
2221  bulkvalues=[]
2222  for lumilsnum,perlslumi in lumilsdata.items():
2223  cmslsnum=perlslumi[0]
2224  instlumi=perlslumi[1]
2225  instlumierror=perlslumi[2]
2226  instlumiquality=perlslumi[3]
2227  beamstatus=perlslumi[4]
2228  beamenergy=perlslumi[5]
2229  numorbit=perlslumi[6]
2230  startorbit=perlslumi[7]
2231  if withDetails:
2232  cmsbxindexindexblob=perlslumi[8]
2233  beam1intensity=perlslumi[9]
2234  beam2intensity=perlslumi[10]
2235  bxlumivalue_occ1=perlslumi[11]
2236  bxlumierror_occ1=perlslumi[12]
2237  bxlumiquality_occ1=perlslumi[13]
2238  bxlumivalue_occ2=perlslumi[14]
2239  bxlumierror_occ2=perlslumi[15]
2240  bxlumiquality_occ2=perlslumi[16]
2241  bxlumivalue_et=perlslumi[17]
2242  bxlumierror_et=perlslumi[18]
2243  bxlumiquality_et=perlslumi[19]
2244  bulkvalues.append([('DATA_ID',data_id),('RUNNUM',runnumber),('LUMILSNUM',lumilsnum),('CMSLSNUM',cmslsnum),('INSTLUMI',instlumi),('INSTLUMIERROR',instlumierror),('INSTLUMIQUALITY',instlumiquality),('BEAMSTATUS',beamstatus),('BEAMENERGY',beamenergy),('NUMORBIT',numorbit),('STARTORBIT',startorbit),('CMSBXINDEXBLOB',cmsbxindexindexblob),('BEAMINTENSITYBLOB_1',beam1intensity),('BEAMINTENSITYBLOB_2',beam2intensity),('BXLUMIVALUE_OCC1',bxlumivalue_occ1),('BXLUMIERROR_OCC1',bxlumierror_occ1),('BXLUMIQUALITY_OCC1',bxlumiquality_occ1),('BXLUMIVALUE_OCC2',bxlumivalue_occ2),('BXLUMIERROR_OCC2',bxlumierror_occ2),('BXLUMIQUALITY_OCC2',bxlumiquality_occ2),('BXLUMIVALUE_ET',bxlumivalue_et),('BXLUMIERROR_ET',bxlumierror_et),('BXLUMIQUALITY_ET',bxlumiquality_et)])
2245  else:
2246  bulkvalues.append([('DATA_ID',data_id),('RUNNUM',runnumber),('LUMILSNUM',lumilsnum),('CMSLSNUM',cmslsnum),('INSTLUMI',instlumi),('INSTLUMIERROR',instlumierror),('INSTLUMIQUALITY',instlumiquality),('BEAMSTATUS',beamstatus),('BEAMENERGY',beamenergy),('NUMORBIT',numorbit),('STARTORBIT',startorbit)])
2247  nrows+=1
2248  committedrows+=1
2249  if nrows==bulksize:
2250  print('committing lumi in LS chunck ',nrows)
2251  db=dbUtil.dbUtil(session.nominalSchema())
2252  session.transaction().start(False)
2253  db.bulkInsert(tableName,lslumiDefDict,bulkvalues)
2254  session.transaction().commit()
2255  nrows=0
2256  bulkvalues=[]
2257  elif committedrows==len(lumilsdata):
2258  print('committing lumi at the end ')
2259  db=dbUtil.dbUtil(session.nominalSchema())
2260  session.transaction().start(False)
2261  db.bulkInsert(tableName,lslumiDefDict,bulkvalues)
2262  session.transaction().commit()
2263  except :
2264  raise
2265 
2266 #def insertLumiLSDetail(schema,runnumber,data_id,lumibxdata):
2267 # '''
2268 # input:
2269 # lumibxdata [(algoname,{lumilsnum:[cmslsnum,bxlumivalue,bxlumierror,bxlumiquality]}),(algoname,{lumilsnum:[cmslsnum,bxlumivalue,bxlumierror,bxlumiquality]}),(algoname,{lumilsnum:[cmslsnum,bxlumivalue,bxlumierror,bxlumiquality]})]
2270 # output:
2271 # nrows
2272 # '''
2273 # try:
2274 # nrow=0
2275 # bulkvalues=[]
2276 # lslumiDefDict=[('DATA_ID','unsigned long long'),('RUNNUM','unsigned int'),('LUMILSNUM','unsigned int'),('CMSLSNUM','unsigned int'),('ALGONAME','string'),('BXLUMIVALUE','blob'),('BXLUMIERROR','blob'),('BXLUMIQUALITY','blob')]
2277 # for (algoname,peralgobxdata) in lumibxdata:
2278 # for lumilsnum,bxdata in peralgobxdata.items():
2279 # cmslsnum=bxdata[0]
2280 # bxlumivalue=bxdata[1]
2281 # bxlumierror=bxdata[2]
2282 # bxlumiquality=bxdata[3]
2283 # bulkvalues.append([('DATA_ID',data_id),('RUNNUM',runnumber),('LUMILSNUM',lumilsnum),('CMSLSNUM',cmslsnum),('ALGONAME',algoname),('BXLUMIVALUE',bxlumivalue),('BXLUMIERROR',bxlumierror),('BXLUMIQUALITY',bxlumiquality)])
2284 # db=dbUtil.dbUtil(schema)
2285 # db.bulkInsert(nameDealer.lumidetailTableName(),lslumiDefDict,bulkvalues)
2286 # return len(bulkvalues)
2287 # except:
2288 # raise
2289 
2290 #def completeOldLumiData(schema,runnumber,lsdata,data_id):
2291 # '''
2292 # input:
2293 # lsdata [[lumisummary_id,lumilsnum,cmslsnum]]
2294 # '''
2295 # try:
2296 # #update in lumisummary table
2297 # #print 'insert in lumisummary table'
2298 # setClause='DATA_ID=:data_id'
2299 # updateCondition='RUNNUM=:runnum AND DATA_ID is NULL'
2300 # updateData=coral.AttributeList()
2301 # updateData.extend('data_id','unsigned long long')
2302 # updateData.extend('runnum','unsigned int')
2303 # updateData['data_id'].setData(data_id)
2304 # updateData['runnum'].setData(int(runnumber))
2305 # db=dbUtil.dbUtil(schema)
2306 # db.singleUpdate(nameDealer.lumisummaryTableName(),setClause,updateCondition,updateData)
2307 # #updates in lumidetail table
2308 # updateAction='DATA_ID=:data_id,RUNNUM=:runnum,CMSLSNUM=:cmslsnum,LUMILSNUM=:lumilsnum'
2309 # updateCondition='LUMISUMMARY_ID=:lumisummary_id'
2310 # bindvarDef=[]
2311 # bindvarDef.append(('data_id','unsigned long long'))
2312 # bindvarDef.append(('runnum','unsigned int'))
2313 # bindvarDef.append(('cmslsnum','unsigned int'))
2314 # bindvarDef.append(('lumilsnum','unsigned int'))
2315 # inputData=[]
2316 # for [lumisummary_id,lumilsnum,cmslsnum] in lsdata:
2317 # inputData.append([('data_id',data_id),('runnum',int(runnumber)),('cmslsnum',cmslsnum),('lumilsnum',lumilsnum)])
2318 # db.updateRows(nameDealer.lumidetailTableName(),updateAction,updateCondition,bindvarDef,inputData)
2319 # except:
2320 # raise
2321 
2322 #=======================================================
2323 # DELETE
2324 #=======================================================
2325 
2326 
2327 #=======================================================
2328 # Unit Test
2329 #=======================================================
2330 if __name__ == "__main__":
2331  from . import sessionManager
2332  from . import lumidbDDL,revisionDML,generateDummyData
2333  #myconstr='sqlite_file:test2.db'
2334  myconstr='oracle://devdb10/cms_xiezhen_dev'
2335  svc=sessionManager.sessionManager(myconstr,authpath='/afs/cern.ch/user/x/xiezhen',debugON=False)
2336  session=svc.openSession(isReadOnly=False,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')])
2337  schema=session.nominalSchema()
2338  session.transaction().start(False)
2341  tables=lumidbDDL.createTables(schema)
2342  try:
2343  # #lumidbDDL.createUniqueConstraints(schema)
2344  trunkinfo=revisionDML.createBranch(schema,'TRUNK',None,comment='main')
2345  print(trunkinfo)
2346  datainfo=revisionDML.createBranch(schema,'DATA','TRUNK',comment='hold data')
2347  print(datainfo)
2348  norminfo=revisionDML.createBranch(schema,'NORM','TRUNK',comment='hold normalization factor')
2349  print(norminfo)
2350  except:
2351  raise
2352  #print 'branch already exists, do nothing'
2353  (normbranchid,normbranchparent)=revisionDML.branchInfoByName(schema,'NORM')
2354  normbranchinfo=(normbranchid,'NORM')
2355  addNormToBranch(schema,'pp7TeV','PROTPHYS',6370.0,3500,{},normbranchinfo)
2356  addNormToBranch(schema,'hi7TeV','HIPHYS',2.38,3500,{},normbranchinfo)
2357  (branchid,branchparent)=revisionDML.branchInfoByName(schema,'DATA')
2358  branchinfo=(branchid,'DATA')
2359  for runnum in [1200,1211,1222,1233,1345]:
2360  runsummarydata=generateDummyData.runsummary(schema,'PROTPHYS',3500)
2361  insertRunSummaryData(schema,runnum,runsummarydata)
2363  insertTrgHltMap(schema,hlttrgmap[0],hlttrgmap[1])
2364  lumidummydata=generateDummyData.lumiSummary(schema,20)
2365  lumirundata=[lumidummydata[0]]
2366  lumilsdata=lumidummydata[1]
2367  (lumirevid,lumientryid,lumidataid)=addLumiRunDataToBranch(schema,runnum,lumirundata,branchinfo)
2368  insertLumiLSSummary(schema,runnum,lumidataid,lumilsdata)
2369  trgdata=generateDummyData.trg(schema,20)
2370  trgrundata=[trgdata[0],trgdata[1],trgdata[2]]
2371  trglsdata=trgdata[3]
2372  (trgrevid,trgentryid,trgdataid)=addTrgRunDataToBranch(schema,runnum,trgrundata,branchinfo)
2373  insertTrgLSData(schema,runnum,trgdataid,trglsdata)
2374  hltdata=generateDummyData.hlt(schema,20)
2375  hltrundata=[hltdata[0],hltdata[1]]
2376  hltlsdata=hltdata[2]
2377  (hltrevid,hltentryid,hltdataid)=addHLTRunDataToBranch(schema,runnum,hltrundata,branchinfo)
2378  insertHltLSData(schema,runnum,hltdataid,hltlsdata)
2379  session.transaction().commit()
2380  print('test reading')
2381  session.transaction().start(True)
2382  print('===inspecting NORM by name===')
2383  normrevlist=revisionDML.revisionsInBranchName(schema,'NORM')
2384  luminormentry_id=revisionDML.entryInBranch(schema,nameDealer.luminormTableName(),'pp7TeV','NORM')
2385  latestNorms=revisionDML.latestDataRevisionOfEntry(schema,nameDealer.luminormTableName(),luminormentry_id,normrevlist)
2386  print('latest norm data_id for pp7TeV ',latestNorms)
2387 
2388  print('===inspecting DATA branch===')
2389  print(revisionDML.branchType(schema,'DATA'))
2390  revlist=revisionDML.revisionsInBranchName(schema,'DATA')
2391  print(revlist)
2392  lumientry_id=revisionDML.entryInBranch(schema,nameDealer.lumidataTableName(),'1211','DATA')
2393  latestrevision=revisionDML.latestDataRevisionOfEntry(schema,nameDealer.lumidataTableName(),lumientry_id,revlist)
2394  print('latest lumi data_id for run 1211 ',latestrevision)
2395  lumientry_id=revisionDML.entryInBranch(schema,nameDealer.lumidataTableName(),'1222','DATA')
2396  latestrevision=revisionDML.latestDataRevisionOfEntry(schema,nameDealer.lumidataTableName(),lumientry_id,revlist)
2397  print('latest lumi data_id for run 1222 ',latestrevision)
2398  trgentry_id=revisionDML.entryInBranch(schema,nameDealer.trgdataTableName(),'1222','DATA')
2399  latestrevision=revisionDML.latestDataRevisionOfEntry(schema,nameDealer.trgdataTableName(),trgentry_id,revlist)
2400  print('latest trg data_id for run 1222 ',latestrevision)
2401  session.transaction().commit()
2402  print('tagging data so far as data_orig')
2403  session.transaction().start(False)
2404  (revisionid,parentid,parentname)=revisionDML.createBranch(schema,'data_orig','DATA',comment='tag of 2010data')
2405  session.transaction().commit()
2406  session.transaction().start(True)
2407  print(revisionDML.branchType(schema,'data_orig'))
2408  revlist=revisionDML.revisionsInTag(schema,revisionid,branchinfo[0])
2409  print(revlist)
2410  session.transaction().commit()
2411  session.transaction().start(False)
2412  for runnum in [1200,1222]:
2413  print('revising lumidata for run ',runnum)
2414  lumidummydata=generateDummyData.lumiSummary(schema,20)
2415  lumirundata=[lumidummydata[0]]
2416  lumilsdata=lumidummydata[1]
2417  (lumirevid,lumientryid,lumidataid)=addLumiRunDataToBranch(schema,runnum,lumirundata,branchinfo)
2418  insertLumiLSSummary(schema,runnum,lumidataid,lumilsdata)
2419  revlist=revisionDML.revisionsInTag(schema,revisionid,branchinfo[0])
2420  print('revisions in branch DATA',revisionDML.revisionsInBranch(schema,branchinfo[0]))
2421  session.transaction().commit()
2422  #print 'revisions in tag data_orig ',revlist
2423 
2424  print('===test reading===')
2425  session.transaction().start(True)
2426  #print 'guess norm by name'
2427  #normid1=guessnormIdByName(schema,'pp7TeV')
2428  #print 'normid1 ',normid1
2429  #normid2=guessnormIdByContext(schema,'PROTPHYS',3500)
2430  #print 'guess norm of PROTPHYS 3500'
2431  #print 'normid2 ',normid2
2432  #normid=normid2
2433  #(lumidataid,trgdataid,hltdataid)=guessDataIdByRun(schema,1200)
2434  #print 'normid,lumiid,trgid,hltid ',normid,lumidataid,trgdataid,hltdataid
2435  #print 'lumi norm'
2436  #print luminormById(schema,normid)
2437  #print 'runinfo '
2438  #print runsummary(schema,runnum,session.properties().flavorName())
2439  #print 'lumirun '
2440  #print lumiRunById(schema,lumidataid)
2441  #print 'lumisummary'
2442  #print lumiLSById(schema,lumidataid)
2443  #print 'beam info'
2444  #print beamInfoById(schema,lumidataid)
2445  #print 'lumibx by algo OCC1'
2446  #print lumiBXByAlgo(schema,lumidataid,'OCC1')
2447  print('trg run, trgdataid ',trgdataid)
2448  print(trgRunById(schema,trgdataid,withblobdata=True))
2449  #print 'trg ls'
2450  #print trgLSById(schema,trgdataid)
2451  #print 'hlt run'
2452  #print hltRunById(schema,hltdataid)
2453  #print 'hlt ls'
2454  #print hltLSById(schema,hltdataid)
2455  session.transaction().commit()
2456  del session
Definition: start.py:1
def revisionsInBranchName(schema, branchname)
Definition: revisionDML.py:179
def dropTables(schema, tablelist)
Definition: lumidbDDL.py:225
def guessnormIdByContext(schema, amodetag, egev1)
Definition: dataDML.py:1742
def guessDataIdForRange(schema, inputRange, tablename)
Definition: dataDML.py:1654
def createTables(schema)
Definition: lumidbDDL.py:11
def runList(schema, datatagid, runmin=None, runmax=None, fillmin=None, fillmax=None, startT=None, stopT=None, l1keyPattern=None, hltkeyPattern=None, amodetag=None, nominalEnergy=None, energyFlut=0.2, requiretrg=True, requirehlt=True, preselectedruns=None, lumitype=None)
Definition: dataDML.py:243
def luminormById(schema, dataid)
Definition: dataDML.py:558
def lumicorrById(schema, correctiondataid)
Definition: dataDML.py:52
def insertRunSummaryData(schema, runnumber, runsummarydata, complementalOnly=False)
Definition: dataDML.py:2058
def lumiRunById(schema, lumidataid, lumitype='HF')
Definition: dataDML.py:963
def insertTrgHltMap(schema, hltkey, trghltmap)
Definition: dataDML.py:2100
def beamstatusByIds(schema, dataidMap)
Definition: dataDML.py:923
def lumisummaryv2TableName()
Definition: nameDealer.py:28
def addTrgRunDataToBranch(schema, runnumber, trgrundata, branchinfo)
Definition: dataDML.py:2004
def latestdataIdByEntry(schema, entryid, datatype, branchfilter)
Definition: dataDML.py:1848
def trgRunById(schema, dataid, trgbitname=None, trgbitnamepattern=None)
Definition: dataDML.py:732
def revisionsInBranch(schema, branchid)
Definition: revisionDML.py:63
def runsummary(schema, amodetag, egev)
def revisionsInTag(schema, tagrevisionid, branchid)
Definition: revisionDML.py:14
def unpackBlobtoArray(iblob, itemtypecode)
Definition: CommonUtil.py:231
def hltRunById(schema, dataid, hltpathname=None, hltpathpattern=None)
Definition: dataDML.py:1336
def pixeltagRunsTableName()
Definition: nameDealer.py:70
def guessDataIdByRun(schema, runnum, tablename, revfilter=None)
Definition: dataDML.py:1623
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def commonTables()
Definition: nameDealer.py:9
def bulkInsertTrgLSData(session, runnumber, data_id, trglsdata, bulksize=500)
Definition: dataDML.py:2130
def lumiLSById(schema, dataid, beamstatus=None, withBXInfo=False, bxAlgo='OCC1', withBeamIntensity=False, tableName=None)
Definition: dataDML.py:1088
def addEntry(schema, datatableName, entryinfo, branchinfo)
Definition: revisionDML.py:344
def entryTableName(dataTableName)
Definition: nameDealer.py:103
def luminormTableName()
Definition: nameDealer.py:37
def intglumiForRange(schema, runlist)
Definition: dataDML.py:1539
def addNormToBranch(schema, normname, amodetag, norm1, egev1, optionalnormdata, branchinfo)
Definition: dataDML.py:1896
def entryInBranch(schema, datatableName, entryname, branch)
Definition: revisionDML.py:190
def trg(schema, nls)
def guessLumiDataIdByRunInBranch(schema, runnum, tablename, branchName)
Definition: dataDML.py:1605
def addLumiRunDataToBranch(schema, runnumber, lumirundata, branchinfo, tableName)
Definition: dataDML.py:1965
def lshltTableName()
Definition: nameDealer.py:61
def lumiSummary(schema, nlumils)
def hlt(schema, nls)
def bulkInsertHltLSData(session, runnumber, data_id, hltlsdata, bulksize=500)
Definition: dataDML.py:2170
def bookNewRevision(schema, datatableName)
Definition: revisionDML.py:332
def bulkInsertLumiLSSummary(session, runnumber, data_id, lumilsdata, tableName, bulksize=500, withDetails=True)
Definition: dataDML.py:2207
T min(T a, T b)
Definition: MathUtil.h:58
def revmapTableName(dataTableName)
Definition: nameDealer.py:100
def bookNewEntry(schema, datatableName)
Definition: revisionDML.py:318
def lumiBXByAlgo(schema, dataid, algoname)
Definition: dataDML.py:1286
def addRevision(schema, datatableName, revisioninfo, branchinfo)
Definition: revisionDML.py:396
def fillrunMap(schema, fillnum=None, runmin=None, runmax=None, startT=None, stopT=None, l1keyPattern=None, hltkeyPattern=None, amodetag=None)
Definition: dataDML.py:163
def lumiRunByIds(schema, dataidMap, lumitype='HF')
Definition: dataDML.py:907
def addCorrToBranch(schema, corrname, a1, optionalcorrdata, branchinfo)
Definition: dataDML.py:1934
def hltLSById(schema, dataid, hltpathname=None, hltpathpattern=None, withL1Pass=False, withHLTAccept=False)
Definition: dataDML.py:1440
def lstrgTableName()
Definition: nameDealer.py:49
def trgdataTableName()
Definition: nameDealer.py:46
def guesscorrIdByName(schema, tagname=None)
LumiDB DML API # # Author: Zhen Xie #.
Definition: dataDML.py:19
def trgLSById(schema, dataid, trgbitname=None, trgbitnamepattern=None, withL1Count=False, withPrescale=False)
Definition: dataDML.py:800
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def branchInfoByName(schema, branchName)
Definition: revisionDML.py:284
def hlttrgMappingByrun(schema, runnum, hltpathname=None, hltpathpattern=None)
Definition: dataDML.py:1393
def intglumiv2TableName()
Definition: nameDealer.py:91
def runsummary(schema, runnum, sessionflavor='')
Definition: dataDML.py:425
def allfillschemes(schema)
Definition: dataDML.py:1066
def dataentryIdByRun(schema, runnum, branchfilter)
Definition: dataDML.py:1809
def fillschemeTableName()
Definition: nameDealer.py:13
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
def beamInfoById(schema, dataid, withBeamIntensity=False, minIntensity=0.1)
Definition: dataDML.py:1195
def hltdataTableName()
Definition: nameDealer.py:58
def fillInRange(schema, fillmin, fillmax, amodetag, startT, stopT)
Definition: dataDML.py:92
def cmsrunsummaryTableName()
Definition: nameDealer.py:16
def pixellumidataTableName()
Definition: nameDealer.py:22
def guessnormIdByName(schema, normname)
Definition: dataDML.py:1777
def guessHltDataIdByRunInBranch(schema, runnum, tablename, branchName)
Definition: dataDML.py:1617
def trghltMapTableName()
Definition: nameDealer.py:76
def createBranch(schema, name, parentname, comment='')
Definition: revisionDML.py:434
#define str(s)
def tagRunsTableName()
Definition: nameDealer.py:64
def addHLTRunDataToBranch(schema, runnumber, hltrundata, branchinfo)
Definition: dataDML.py:2031
def branchType(schema, name)
Definition: revisionDML.py:114
def lumidataTableName()
Definition: nameDealer.py:19
def fillschemePatternMap(schema, lumitype)
Definition: dataDML.py:1575
def guessTrgDataIdByRunInBranch(schema, runnum, tablename, branchName)
Definition: dataDML.py:1611
def schemaV2Tables()
Definition: nameDealer.py:5
def latestDataRevisionOfEntry(schema, datatableName, entry, revrange)
Definition: revisionDML.py:276