CMS 3D CMS Logo

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