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