test
CMS 3D CMS Logo

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