CMS 3D CMS Logo

lumiCalcAPI.py
Go to the documentation of this file.
1 import os,coral,datetime,fnmatch,time
2 from RecoLuminosity.LumiDB import nameDealer,revisionDML,dataDML,lumiTime,CommonUtil,selectionParser,hltTrgSeedMapper,normFunctors,lumiParameters
3 
4 ########################################################################
5 # Lumi data management and calculation API #
6 # #
7 # Author: Zhen Xie #
8 ########################################################################
9 
10 def runsummary(schema,irunlsdict):
11  '''
12  output [[run(0),l1key(1),amodetag(2),egev(3),hltkey(4),fillnum(5),fillscheme(6),starttime(7),stoptime(8)]]
13  '''
14  result=[]
15  for run in sorted(irunlsdict):
16  runinfo=dataDML.runsummary(schema,run)
17  runinfo.insert(0,run)
18  result.append(runinfo)
19  return result
20 
21 def runsummaryMap(schema,irunlsdict,dataidmap,lumitype='HF'):
22  '''
23  output: {run:[l1key(0),amodetag(1),egev(2),hltkey(3),fillnum(4),fillscheme(5),starttime(6),stoptime(7)]}
24  '''
25  result={}
26  seqresult=runsummary(schema,irunlsdict)
27  idresult=dataDML.lumiRunByIds(schema,dataidmap,lumitype)
28  for [run,l1key,amodetag,hltkey,fillnum,fillscheme] in seqresult:
29  egev=idresult[run][2]
30  startT=idresult[run][3]
31  stopT=idresult[run][4]
32  result[run]=[l1key,amodetag,egev,hltkey,fillnum,fillscheme,startT,stopT]
33  return result
34 
35 def fillInRange(schema,fillmin=1000,fillmax=9999,amodetag='PROTPHYS',startT=None,stopT=None):
36  '''
37  output [fill]
38  '''
39  fills=dataDML.fillInRange(schema,fillmin,fillmax,amodetag,startT,stopT)
40  return fills
41 def fillrunMap(schema,fillnum=None,runmin=None,runmax=None,startT=None,stopT=None,l1keyPattern=None,hltkeyPattern=None,amodetag=None):
42  '''
43  output: {fill:[runnum,...]}
44  '''
45  return dataDML.fillrunMap(schema,fillnum=fillnum,runmin=runmin,runmax=runmax,startT=startT,stopT=stopT,l1keyPattern=l1keyPattern,hltkeyPattern=hltkeyPattern,amodetag=amodetag)
46 
47 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='HF'):
48  '''
49  output: [runnumber,...]
50  '''
51  return dataDML.runList(schema,datatagid,runmin=runmin,runmax=runmax,fillmin=fillmin,fillmax=fillmax,startT=startT,stopT=stopT,l1keyPattern=l1keyPattern,hltkeyPattern=hltkeyPattern,amodetag=amodetag,nominalEnergy=nominalEnergy,energyFlut=energyFlut,requiretrg=requiretrg,requirehlt=requirehlt,lumitype=lumitype)
52 
53 def hltpathsForRange(schema,runlist,hltpathname=None,hltpathpattern=None):
54  '''
55  input: runlist [run], (required)
56  datatag: data version (optional)
57  output : {runnumber,[(hltpath,l1seedexpr,l1bitname)...]}
58  '''
59  result={}
60  for run in runlist:
61  hlttrgmap=dataDML.hlttrgMappingByrun(schema,run,hltpathname=hltpathname,hltpathpattern=hltpathpattern)
62  result[run]=[]
63  for hltpath in sorted(hlttrgmap):
64  l1seedexpr=hlttrgmap[hltpath]
65  (exptype,l1bits)=hltTrgSeedMapper.findUniqueSeed(hltpath,l1seedexpr)
66  l1bitname='n/a'
67  if l1bits:
68  if exptype:
69  l1bitname=l1seedexpr
70  else:
71  l1bitname=l1bits[0]
72  result[run].append((hltpath,l1seedexpr,l1bitname))
73  return result
74 
75 def beamForRange(schema,inputRange,withBeamIntensity=False,minIntensity=0.1,tableName=None,branchName=None):
76  '''
77  input:
78  inputRange: {run:[cmsls]} (required)
79  output : {runnumber:[(lumicmslnum,cmslsnum,beamenergy,beamstatus,[(ibx,b1,b2)])...](4)}
80  '''
81  if tableName is None:
83  if branchName is None:
84  branchName='DATA'
85  result={}
86  for run in inputRange.keys():
87  lslist=inputRange[run]
88  if lslist is not None and len(lslist)==0:
89  result[run]=[]#if no LS is selected for a run
90  continue
91  lumidataid=dataDML.guessLumiDataIdByRun(schema,run,tableName)
92  if lumidataid is None:
93  result[run]=None
94  continue #run non exist
95  lumidata=dataDML.beamInfoById(schema,lumidataid,withBeamIntensity=withBeamIntensity,minIntensity=minIntensity)
96  #(runnum,[(lumilsnum(0),cmslsnum(1),beamstatus(2),beamenergy(3),beaminfolist(4)),..])
97  result[run]=[]
98  perrundata=lumidata[1]
99  if not perrundata:
100  result[run]=[]
101  continue
102  for perlsdata in perrundata:
103  lumilsnum=perlsdata[0]
104  cmslsnum=perlsdata[1]
105  if lslist is not None and cmslsnum not in lslist:
106  continue
107  beamstatus=perlsdata[2]
108  beamenergy=perlsdata[3]
109  beamintInfolist=[]
110  if withBeamIntensity:
111  beamintInfolist=perlsdata[4]
112  result[run].append((lumilsnum,cmslsnum,beamstatus,beamenergy,beamintInfolist))
113  return result
114 
115 def beamForIds(schema,irunlsdict,dataidmap,withBeamIntensity=False,minIntensity=0.1):
116  '''
117  input:
118  inputRange: {run:[cmsls]} (required)
119  dataidmap: {run:(lumiid,trgid,hltid)}
120  output : {runnumber:[(lumicmslnum(0),cmslsnum(1),beamenergy(2),beamstatus(3),ncollidingbx(4),[(ibx,b1,b2)])...](5)}
121  '''
122  result={}
123  for run in irunlsdict.keys():
124  result[run]=[]
125  lslist=irunlsdict[run]
126  if lslist is not None and len(lslist)==0:
127  continue
128  if run not in dataidmap:
129  continue #run non exist
130  lumidataid=dataidmap[run][0]
131  if lumidataid is None:
132  result[run]=None
133  continue
134  lumidata=dataDML.beamInfoById(schema,lumidataid,withBeamIntensity=withBeamIntensity,minIntensity=minIntensity)
135  #(runnum,[(lumilsnum(0),cmslsnum(1),beamstatus(2),beamenergy(3),ncollidingbunches(4),beaminfolist(5),..])
136  if lumidata and lumidata[1]:
137  perrundata=lumidata[1]
138  for perlsdata in perrundata:
139  lumilsnum=perlsdata[0]
140  cmslsnum=perlsdata[1]
141  if lslist is not None and cmslsnum not in lslist:
142  continue
143  beamstatus=perlsdata[2]
144  beamenergy=perlsdata[3]
145  ncollidingbunches=perlsdata[4]
146  beamintInfolist=[]
147  if withBeamIntensity:
148  beamintInfolist=perlsdata[5]
149  result[run].append((lumilsnum,cmslsnum,beamstatus,beamenergy,ncollidingbunches,beamintInfolist))
150  return result
151 
152 def hltForIds(schema,irunlsdict,dataidmap,hltpathname=None,hltpathpattern=None,withL1Pass=False,withHLTAccept=False):
153  '''
154  input:
155  irunlsdict: {run:[cmsls]} (required)
156  dataidmap: {run:(lumiid,trgid,hltid)}
157  hltpathname: exact match hltpathname (optional)
158  hltpathpattern: regex match hltpathpattern (optional)
159  withL1Pass: with L1 pass count
160  withHLTAccept: with HLT accept
161  output: {runnumber:[(cmslsnum,[(hltpath,hltprescale,l1pass,hltaccept),...]),(cmslsnum,[])})}
162  '''
163  result={}
164  for run in irunlsdict.keys():
165  lslist=irunlsdict[run]
166  if lslist is not None and len(lslist)==0:
167  result[run]=[]#if no LS is selected for a run
168  continue
169  if run not in dataidmap:
170  continue
171  hltdataid=dataidmap[run][2]
172  if hltdataid is None:
173  result[run]=None
174  continue #run non exist
175  hltdata=dataDML.hltLSById(schema,hltdataid,hltpathname=hltpathname,hltpathpattern=hltpathpattern,withL1Pass=withL1Pass,withHLTAccept=withHLTAccept)
176  #(runnum,{cmslsnum:[(pathname,prescale,l1pass,hltaccept),...]})
177  result[run]=[]
178  if hltdata and hltdata[1]:
179  lsdict={}
180  for cmslsnum in sorted(hltdata[1]):
181  if lslist is not None and cmslsnum not in lslist:
182  continue
183  lsdata=[]
184  for perpathdata in hltdata[1][cmslsnum]:
185  pathname=perpathdata[0]
186  prescale=perpathdata[1]
187  l1pass=None
188  hltaccept=None
189  if withL1Pass:
190  l1pass=perpathdata[2]
191  if withHLTAccept:
192  hltaccept=perpathdata[3]
193  lsdata.append((pathname,prescale,l1pass,hltaccept))
194  result[run].append((cmslsnum,lsdata))
195  return result
196 
197 def trgForIds(schema,irunlsdict,dataidmap,trgbitname=None,trgbitnamepattern=None,withL1Count=False,withPrescale=False):
198  '''
199  input :
200  irunlsdict {run:[cmsls]} (required)
201  dataidmap: {run:(lumiid,trgid,hltid)}
202  trgbitname exact match trgbitname (optional)
203  trgbitnamepattern match trgbitname (optional)
204  output
205  result {run:[[cmslsnum(0),deadfrac(1),deadtimecount(2),bitzero_count(3),bitzero_prescale(4),[(bitname,prescale,counts,mask)](5)]]}
206  '''
207  result={}
208  for run in irunlsdict.keys():
209  result[run]=[]
210  lslist=irunlsdict[run]
211  if lslist is not None and len(lslist)==0:
212  #if no LS is selected for a run
213  continue
214  if run not in dataidmap:
215  continue
216  trgdataid=dataidmap[run][1]
217  if trgdataid is None:
218  result[run]=None
219  continue #if run non exist
220  if trgdataid==0:
221  continue
222  trgdata=dataDML.trgLSById(schema,trgdataid,trgbitname=trgbitname,trgbitnamepattern=trgbitnamepattern,withL1Count=withL1Count,withPrescale=withPrescale)
223 
224  #(runnum,{cmslsnum:[deadtimecount(0),bitzerocount(1),bitzeroprescale(2),deadfrac(3),[(bitname,trgcount,prescale)](4)]})
225  if trgdata and trgdata[1]:
226  lsdict={}
227  for cmslsnum in sorted(trgdata[1]):
228  if lslist is not None and cmslsnum not in lslist:
229  continue
230  lsdata=[]
231  #print trgdata[1][cmslsnum]
232  deadtimecount=trgdata[1][cmslsnum][0]
233  #bitzerocount=trgdata[1][cmslsnum][1]
234  #bitzeroprescale=trgdata[1][cmslsnum][2]
235  bitzerocount=0
236  bitzeroprescale=0
237  deadfrac=trgdata[1][cmslsnum][3]
238  if deadfrac<0 or deadfrac>1.0:
239  deadfrac=1.0
240  allbitsinfo=trgdata[1][cmslsnum][4]
241  lsdata.append(cmslsnum)
242  lsdata.append(deadfrac)
243  lsdata.append(deadtimecount)
244  lsdata.append(bitzerocount)
245  lsdata.append(bitzeroprescale)
246  lsdata.append(allbitsinfo)
247  result[run].append(lsdata)
248  return result
249 
250 def instLumiForIds(schema,irunlsdict,dataidmap,runsummaryMap,beamstatusfilter=None,timeFilter=None,withBXInfo=False,bxAlgo=None,xingMinLum=None,withBeamIntensity=False,lumitype='HF'):
251  '''
252  FROM ROOT FILE NO CORRECTION AT ALL
253  input:
254  irunlsdict: {run:[cmsls]}
255  dataidmap: {run:(lumiid,trgid,hltid)}
256  runsummaryMap: {run:[l1key(0),amodetag(1),egev(2),hltkey(3),fillnum(4),fillscheme(5),starttime(6),stoptime(7)]}
257  beamstatus: LS filter on beamstatus (optional)
258  timeFilter: (minLSBegTime,maxLSBegTime)
259  withBXInfo: get per bunch info (optional)
260  bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET']
261  xingMinLum: None means apply no cut
262  withBeamIntensity: get beam intensity info (optional)
263  lumitype: luminosity measurement source
264  output:
265  result {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),instlumi(5),instlumierr(6),startorbit(7),numorbit(8),(bxidx,bxvalues,bxerrs)(9),(bxidx,b1intensities,b2intensities)(10),fillnum(11)]}}
266 
267  special meanings:
268  {run:None} None means selected run not in lumiDB,
269  {run:[]} [] means no lumi data for this run in lumiDB
270  {run:cmslsnum(1)==0} means either not cmslsnum or iscms but not selected
271  instlumi unit in Hz/ub
272  '''
273  if lumitype not in ['HF','PIXEL']:
274  raise ValueError('unknown lumitype '+lumitype)
275  lumitableName=''
276  lumilstableName=''
277  if lumitype=='HF':
278  lumitableName=nameDealer.lumidataTableName()
279  lumilstableName=nameDealer.lumisummaryv2TableName()
280  else:
281  lumitableName=nameDealer.pixellumidataTableName()
283  result={}
284  for run in irunlsdict.keys():
285  #for run,(lumidataid,trgid,hltid ) in dataidmap.items():
286  lslist=irunlsdict[run]
287  if lslist is not None and len(lslist)==0:
288  result[run]=[]#no lumi data for this run in lumiDB
289  continue
290  fillnum=runsummaryMap[run][4]
291  runstarttimeStr=runsummaryMap[run][6]
292  if run not in dataidmap:
293  result[run]=[]#no lumi data for this run in lumiDB
294  continue
295  (lumidataid,trgid,hltid )=dataidmap[run]
296  if lumidataid is None: #selected run not in lumiDB
297  result[run]=None
298  continue
299  (lumirunnum,perlsresult)=dataDML.lumiLSById(schema,lumidataid,beamstatus=beamstatusfilter,withBXInfo=withBXInfo,bxAlgo=bxAlgo,withBeamIntensity=withBeamIntensity,tableName=lumilstableName)
300  lsresult=[]
302  for lumilsnum in perlsresult.keys():
303  perlsdata=perlsresult[lumilsnum]
304  cmslsnum=perlsdata[0]
305  if lslist is not None and cmslsnum not in lslist: #ls exists but not selected
306  cmslsnum=0
307  numorbit=perlsdata[6]
308  startorbit=perlsdata[7]
309  orbittime=c.OrbitToTime(runstarttimeStr,startorbit,begorbit=0,customfm='%m/%d/%y %H:%M:%S')
310  if timeFilter:
311  if timeFilter[0]:
312  if orbittime<timeFilter[0]: continue
313  if timeFilter[1]:
314  if orbittime>timeFilter[1]: continue
315  if lumitype=='HF':
316  instlumi=perlsdata[1]*1000.0 #HF db avg values are in Hz/mb,change it to Hz/ub
317  instlumierr=perlsdata[2]*1000.0
318  else:
319  instlumi=perlsdata[1] #PIXEL avg values are in Hz/ub, need no conversion
320  instlumierr=perlsdata[2]
321  beamstatus=perlsdata[4]
322  beamenergy=perlsdata[5]
323  bxidxlist=[]
324  bxvaluelist=[]
325  bxerrorlist=[]
326  bxdata=None
327  beamdata=None
328  if withBXInfo:
329  bxinfo=perlsdata[8]
330  bxvalueArray=None
331  bxerrArray=None
332  if bxinfo:
333  bxvalueArray=bxinfo[0]
334  bxerrArray=bxinfo[1]
335  if xingMinLum :
336  for idx,bxval in enumerate(bxvalueArray):
337  if bxval>xingMinLum:
338  bxidxlist.append(idx)
339  bxvaluelist.append(bxval)
340  bxerrorlist.append(bxerrArray[idx])
341  else:
342  bxidxlist=range(0,len(bxvalueArray))
343  bxvaluelist=bxvalueArray.tolist()
344  bxerrorlist=bxerrArray.tolist()
345  del bxvalueArray[:]
346  del bxerrArray[:]
347  bxdata=(bxidxlist,bxvaluelist,bxerrorlist)
348  if withBeamIntensity:
349  beaminfo=perlsdata[9]
350  bxindexlist=[]
351  b1intensitylist=[]
352  b2intensitylist=[]
353  if beaminfo[0] and beaminfo[1] and beaminfo[2]:
354  bxindexarray=beaminfo[0]
355  beam1intensityarray=beaminfo[1]
356  beam2intensityarray=beaminfo[2]
357  bxindexlist=bxindexarray.tolist()
358  b1intensitylist=beam1intensityarray.tolist()
359  b2intensitylist=beam2intensityarray.tolist()
360  del bxindexarray[:]
361  del beam1intensityarray[:]
362  del beam2intensityarray[:]
363  beamdata=(bxindexlist,b1intensitylist,b2intensitylist)
364  lsresult.append([lumilsnum,cmslsnum,orbittime,beamstatus,beamenergy,instlumi,instlumierr,startorbit,numorbit,bxdata,beamdata,fillnum])
365  del perlsdata[:]
366  result[run]=lsresult
367  return result
368 
369 def deliveredLumiForIds(schema,irunlsdict,dataidmap,runsummaryMap,beamstatusfilter=None,timeFilter=None,normmap=None,withBXInfo=False,bxAlgo=None,xingMinLum=None,withBeamIntensity=False,lumitype='HF',minbiasXsec=None):
370  '''
371  delivered lumi (including calibration,time integral)
372  input:
373  irunlsdict: {run:[lsnum]}, where [lsnum]==None means all ; [lsnum]==[] means selected ls
374  dataidmap : {run:(lumiid,trgid,hltid)}
375  runsummaryMap: {run:[l1key(0),amodetag(1),egev(2),hltkey(3),fillnum(4),fillscheme(5),starttime(6),stoptime(7)]}
376  beamstatus: LS filter on beamstatus
377  normmap: {since:[corrector(0),{paramname:paramvalue}(1),amodetag(2),egev(3),comment(4)]} if normmap empty, means without-correction , if notnormmap means without-correction
378  withBXInfo: get per bunch info (optional)
379  bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET']
380  xingMinLum: cut on bx lumi value (optional)
381  withBeamIntensity: get beam intensity info (optional)
382  lumitype: luminosity source
383  output:
384  result {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),deliveredlumi(5),calibratedlumierr(6),(bxidxlist,bxvalues,bxerrs)(7),(bxidx,b1intensities,b2intensities)(8),fillnum(9),pu(10)]}
385 
386  special meanings:
387  {run:None} None means no run in lumiDB,
388  {run:[]} [] means no lumi for this run in lumiDB
389  {run:cmslsnum(1)==0} means either not cmslsnum or iscms but not selected
390  lumi unit: /ub
391  '''
392  result = {}
394  lumirundata=dataDML.lumiRunByIds(schema,dataidmap,lumitype=lumitype)
395  instresult=instLumiForIds(schema,irunlsdict,dataidmap,runsummaryMap,beamstatusfilter=beamstatusfilter,timeFilter=timeFilter,withBXInfo=withBXInfo,bxAlgo=bxAlgo,withBeamIntensity=withBeamIntensity,lumitype=lumitype)
396 
397  intglumimap={}
398  if lumitype=='HF':
399  intglumimap=dataDML.intglumiForRange(schema,irunlsdict.keys())#some runs need drift correction
400  allsince=[]
401  if normmap:
402  allsince=sorted(normmap.keys())
403  correctorname='fPoly' #HF default
404  correctionparams={'a0':1.0}
405  runfillschemeMap={}
406  fillschemePatternMap={}
407  if lumitype=='PIXEL':
408  correctorname='fPolyScheme' #PIXEL default
409  fillschemePatternMap=dataDML.fillschemePatternMap(schema,'PIXEL')
410  for run,perrundata in instresult.items():
411  if perrundata is None:
412  result[run]=None
413  continue
414  intglumi=0.
415  if normmap and intglumimap and run in intglumimap and intglumimap[run]:
416  intglumi=intglumimap[run]
417  nBXs=0
418  if normmap and lumirundata and run in lumirundata and lumirundata[run][2]:
419  nBXs=lumirundata[run][2]
420  fillschemeStr=''
421  if normmap and runsummaryMap and run in runsummaryMap and runsummaryMap[run][5]:
422  fillschemeStr=runsummaryMap[run][5]
423  if allsince:
424  lastsince=allsince[0]
425  for since in allsince:
426  if run>=since:
427  lastsince=since
428  correctorname=normmap[lastsince][0]
429  correctionparams=normmap[lastsince][1]
430 
431  correctioninput=[0.,intglumi,nBXs,fillschemeStr,fillschemePatternMap]
432  result[run]=[]
433  for perlsdata in perrundata:#loop over ls
434  lumilsnum=perlsdata[0]
435  cmslsnum=perlsdata[1]
436  timestamp=perlsdata[2]
437  bs=perlsdata[3]
438  beamenergy=perlsdata[4]
439  instluminonorm=perlsdata[5]
440  correctioninput[0]=instluminonorm
441  totcorrectionFac=normFunctors.normFunctionCaller(correctorname,*correctioninput,**correctionparams)
442  fillnum=perlsdata[11]
443  instcorrectedlumi=totcorrectionFac*instluminonorm
444  numorbit=perlsdata[8]
445  numbx=lumip.NBX
446  lslen=lumip.lslengthsec()
447  deliveredlumi=instcorrectedlumi*lslen
448  calibratedbxdata=None
449  beamdata=None
450  pu=0.#avgPU
451  if nBXs and minbiasXsec:
452  pu=(instcorrectedlumi/nBXs)*minbiasXsec/lumip.rotationRate
453  if withBXInfo:
454  (bxidxData,bxvaluesData,bxerrsData)=perlsdata[9]
455  if lumitype=='HF':
456  if xingMinLum:
457  bxidxList=[]
458  bxvalueList=[]
459  bxerrList=[]
460  for idx,bxval in enumerate(bxvaluesData):
461  correctedbxintlumi=totcorrectionFac*bxval
462  correctedbxintlumierr=totcorrectionFac*bxerrsData[idx]
463  if correctedbxintlumi>xingMinLum:
464  bxidxList.append(bxidxData[idx])
465  bxvalueList.append(correctedbxintlumi)
466  bxerrList.append(correctedbxintlumierr)
467  calibratedbxdata=(bxidxList,bxvalueList,bxerrList)
468  else:
469  calibratedbxvalue=[totcorrectionFac*x for x in bxvaluesData]
470  calibratedlumierr=[totcorrectionFac*x for x in bxerrsData]
471  calibratedbxdata=(bxidxData,calibratedbxvalue,calibratedlumierr)
472  if withBeamIntensity:
473  beamdata=perlsdata[10]
474  calibratedlumierr=0.0
475  result[run].append([lumilsnum,cmslsnum,timestamp,bs,beamenergy,deliveredlumi,calibratedlumierr,calibratedbxdata,beamdata,fillnum,pu])
476  del perlsdata[:]
477  return result
478 
479 def lumiForIds(schema,irunlsdict,dataidmap,runsummaryMap,beamstatusfilter=None,timeFilter=None,normmap=None,withBXInfo=False,bxAlgo=None,xingMinLum=None,withBeamIntensity=False,lumitype='HF',minbiasXsec=None):
480  '''
481  delivered/recorded lumi (including calibration,time integral)
482  input:
483  irunlsdict: {run:[lsnum]}, where [lsnum]==None means all ; [lsnum]==[] means no selected ls
484  dataidmap : {run:(lumiid,trgid,hltid)}
485  runsummaryMap: {run:[l1key(0),amodetag(1),egev(2),hltkey(3),fillnum(4),fillscheme(5),starttime(6),stoptime(7)]}
486  beamstatus: LS filter on beamstatus
487  normmap:
488  withBXInfo: get per bunch info (optional)
489  bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET']
490  xingMinLum: cut on bx lumi value (optional)
491  withBeamIntensity: get beam intensity info (optional)
492  lumitype: luminosity source
493  output:
494  result {run:[[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),deliveredlumi(5),recordedlumi(6),calibratedlumierror(7),(bxidx,bxvalues,bxerrs)(8),(bxidx,b1intensities,b2intensities)(9),fillnum(10),ncollidingbunches(11)]...]}
495  special meanings:
496  {run:None} None means no run in lumiDB,
497  {run:[]} [] means no lumi for this run in lumiDB
498  {run:[....deliveredlumi(5),recordedlumi(6)None]} means no trigger in lumiDB
499  {run:cmslsnum(1)==0} means either not cmslsnum or is cms but not selected, therefore set recordedlumi=0,efflumi=0
500  lumi unit: 1/ub
501  '''
502  deliveredresult=deliveredLumiForIds(schema,irunlsdict,dataidmap,runsummaryMap,beamstatusfilter=beamstatusfilter,timeFilter=timeFilter,normmap=normmap,withBXInfo=withBXInfo,bxAlgo=bxAlgo,xingMinLum=xingMinLum,withBeamIntensity=withBeamIntensity,lumitype=lumitype,minbiasXsec=minbiasXsec)
503  trgresult=trgForIds(schema,irunlsdict,dataidmap)
504  for run in deliveredresult.keys():#loop over delivered,already selected
505  perrundata=deliveredresult[run]
506  if perrundata is None or len(perrundata)==0: #pass through
507  continue
508  alltrgls=[]
509  if run in trgresult and trgresult[run]:
510  alltrgls=[x[0] for x in trgresult[run]]
511  for perlsdata in perrundata:#loop over ls
512  if not perlsdata: continue #no lumi data for this ls
513  perlsdata.insert(6,None)
514  if not alltrgls: continue #no trg for this run,recorded=None
515  cmslsnum=perlsdata[1]
516  if cmslsnum==0:#if not a cmsls or not selected by cms list, set recordedlumi to 0
517  recordedlumi=0.0
518  else:
519  try:
520  trglsidx=alltrgls.index(cmslsnum)
521  deadfrac=trgresult[run][trglsidx][1]
522  if deadfrac<0 or deadfrac>1.0: deadfrac=1.0
523  deliveredlumi=perlsdata[5]
524  recordedlumi=(1.0-deadfrac)*deliveredlumi
525  except ValueError:
526  #print '[WARNING] no trigger for LS=',cmslsnum
527  recordedlumi=None
528  perlsdata[6]=recordedlumi
529  return deliveredresult
530 
531 def effectiveLumiForIds(schema,irunlsdict,dataidmap,runsummaryMap=None,beamstatusfilter=None,timeFilter=None,normmap=None,hltpathname=None,hltpathpattern=None,withBXInfo=False,bxAlgo=None,xingMinLum=None,withBeamIntensity=False,lumitype='HF',minbiasXsec=None):
532  '''
533  delivered/recorded/eff lumi in selected hlt path (including calibration,time integral)
534  input:
535  irunlsdict: {run:[lsnum]}, where [lsnum]==None means all ; [lsnum]==[] means selected ls
536  dataidmap : {run:(lumiid,trgid,hltid)}
537  runsummaryMap: {run:[l1key(0),amodetag(1),egev(2),hltkey(3),fillnum(4),fillscheme(5),starttime(6),stoptime(7)]}
538  beamstatusfilter: LS filter on beamstatus
539  normmap: {since:[corrector(0),{paramname:paramvalue}(1),amodetag(2),egev(3),comment(4)]} if normmap empty, means without-correction , if notnormmap means without-correction
540  hltpathname: selected hltpathname
541  hltpathpattern: regex select hltpaths
542  withBXInfo: get per bunch info (optional)
543  bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET']
544  xingMinLum: cut on bx lumi value (optional)
545  withBeamIntensity: get beam intensity info (optional)
546  lumitype: luminosity source
547  output:
548  result {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),deliveredlumi(5),recordedlumi(6),calibratedlumierror(7),{hltpath:[l1name,l1prescale,hltprescale,efflumi]}(8),bxdata(9),beamdata(10),fillnum(11),ncollidingbunches(12)]}
549  {run:None} None means no run in lumiDB,
550  {run:[]} [] means no lumi for this run in lumiDB
551  {run:[....deliveredlumi(5),recorded(6)==None,]} means no trigger in lumiDB
552  {run:[....deliveredlumi(5),recorded(6),calibratedlumierror(7)==None]} means no hlt in lumiDB
553 
554  lumi unit: 1/ub
555  '''
556  deliveredresult=deliveredLumiForIds(schema,irunlsdict,dataidmap,runsummaryMap,beamstatusfilter=beamstatusfilter,timeFilter=timeFilter,normmap=normmap,withBXInfo=withBXInfo,bxAlgo=bxAlgo,xingMinLum=xingMinLum,withBeamIntensity=withBeamIntensity,lumitype=lumitype,minbiasXsec=minbiasXsec)
557  trgresult=trgForIds(schema,irunlsdict,dataidmap,withPrescale=True) #{run:[cmslsnum,deadfrac,deadtimecount,bitzero_count,bitzero_prescale,[(bitname,prescale,counts,mask)]]}
558  hltresult=hltForIds(schema,irunlsdict,dataidmap,hltpathname=hltpathname,hltpathpattern=hltpathpattern,withL1Pass=False,withHLTAccept=False) #{runnumber:[(cmslsnum,[(hltpath,hltprescale,l1pass,hltaccept),...]),(cmslsnum,[])})}
559  for run in deliveredresult.keys(): #loop over delivered
560  perrundata=deliveredresult[run]
561  if perrundata is None or len(perrundata)==0:#pass through
562  continue
563  alltrgls=[]
564  if run in trgresult and trgresult[run]:
565  alltrgls=[x[0] for x in trgresult[run]]
566  allhltls=[]
567  if run in hltresult and hltresult[run]:
568  allhltls=[x[0] for x in hltresult[run]]
569  l1bitinfo=[]
570  hltpathinfo=[]
571  hlttrgmap=dataDML.hlttrgMappingByrun(schema,run,hltpathname=hltpathname,hltpathpattern=hltpathpattern)
572 
573  for perlsdata in perrundata: #loop over ls
574  if not perlsdata: continue #no lumi for this ls
575  perlsdata.insert(6,None)
576  perlsdata.insert(8,None)
577  if not alltrgls: continue #no trg for this run
578  cmslsnum=perlsdata[1]
579  recordedlumi=0.0
580  if cmslsnum==0:#if not a cmsls or not selected by cms list, set recordedlumi,efflumi to 0
581  continue
582  else:
583  try:
584  trglsidx=alltrgls.index(cmslsnum)
585  deadfrac=trgresult[run][trglsidx][1]
586  l1bitinfo=trgresult[run][trglsidx][5]
587  if deadfrac<0 or deadfrac>1.0:deadfrac=1.0
588  deliveredlumi=perlsdata[5]
589  recordedlumi=(1.0-deadfrac)*deliveredlumi
590  except ValueError:
591  #print '[WARNING] no trigger for LS=',cmslsnum
592  continue #do not go further
593  perlsdata[6]=recordedlumi
594  if not allhltls: continue #no hlt for this run
595  try:
596  hltlsidx=allhltls.index(cmslsnum)
597  except ValueError:
598  #print '[WARNING] no hlt for LS=',cmslsnum
599  continue #do not go further
600  trgprescalemap={} #{bitname:l1prescale} for this lumi section
601  if l1bitinfo:
602  for thisbitinfo in l1bitinfo:
603  thisbitname=thisbitinfo[0]
604  thisbitprescale=thisbitinfo[2]
605  trgprescalemap['"'+thisbitname+'"']=thisbitprescale
606  else:
607  continue
608  hltpathdata=hltresult[run][hltlsidx][1]
609  efflumidict={}#{pathname:[[l1bitname,l1prescale,hltprescale,efflumi]]}
610  for pathidx,thispathinfo in enumerate(hltpathdata):
611  thispathname=thispathinfo[0]
612  thisprescale=thispathinfo[1]
613  thisl1seed=None
614  l1bitname=None
615  l1prescale=None
616  try:
617  thisl1seed=hlttrgmap[thispathname]
618  except KeyError:
619  thisl1seed=None
620  if thisl1seed:
621  try:
622  (exptype,l1bits)=hltTrgSeedMapper.findUniqueSeed(thispathname,thisl1seed)
623  if l1bits:
624  if not exptype:
625  l1bitname=l1bits[0]
626  l1prescale=trgprescalemap[l1bits[0]]#need to match double quoted string!
627  else:
628  pmin=99999999
629  pmax=0
630  for bit in l1bits:
631  l1p=trgprescalemap[bit]
632  if exptype=='OR':
633  if l1p!=0 and l1p<pmin:
634  pmin=l1p
635  l1prescale=l1p
636  l1bitname=bit
637  if exptype=='AND':
638  if l1p!=0 and l1p>pmax:
639  pmax=l1p
640  l1prescale=l1p
641  l1bitname=bit
642  else:
643  l1prescale=None
644  except KeyError:
645  l1prescale=None
646 
647  efflumi=0.0
648  if l1prescale and thisprescale:#normal both prescaled
649  efflumi=recordedlumi/(float(l1prescale)*float(thisprescale))
650  efflumidict[thispathname]=[l1bitname,l1prescale,thisprescale,efflumi]
651  elif l1prescale and thisprescale==0: #hltpath in menu but masked
652  efflumidict[thispathname]=[l1bitname,l1prescale,thisprescale,efflumi]
653  else:
654  efflumidict[thispathname]=[None,0,thisprescale,efflumi]
655  perlsdata[8]=efflumidict
656  return deliveredresult
657 
def runList(schema, datatagid, runmin=None, runmax=None, fillmin=None, fillmax=None, startT=None, stopT=None, l1keyPattern=None, hltkeyPattern=None, amodetag=None, nominalEnergy=None, energyFlut=0.2, requiretrg=True, requirehlt=True, preselectedruns=None, lumitype=None)
Definition: dataDML.py:241
def effectiveLumiForIds(schema, irunlsdict, dataidmap, runsummaryMap=None, beamstatusfilter=None, timeFilter=None, normmap=None, hltpathname=None, hltpathpattern=None, withBXInfo=False, bxAlgo=None, xingMinLum=None, withBeamIntensity=False, lumitype='HF', minbiasXsec=None)
Definition: lumiCalcAPI.py:531
def lumisummaryv2TableName()
Definition: nameDealer.py:28
def hltpathsForRange(schema, runlist, hltpathname=None, hltpathpattern=None)
Definition: lumiCalcAPI.py:53
def beamForRange(schema, inputRange, withBeamIntensity=False, minIntensity=0.1, tableName=None, branchName=None)
Definition: lumiCalcAPI.py:75
def normFunctionCaller(funcName, args, kwds)
Definition: normFunctors.py:50
def runsummaryMap(schema, irunlsdict, dataidmap, lumitype='HF')
Definition: lumiCalcAPI.py:21
def lumiLSById(schema, dataid, beamstatus=None, withBXInfo=False, bxAlgo='OCC1', withBeamIntensity=False, tableName=None)
Definition: dataDML.py:1086
def lumiForIds(schema, irunlsdict, dataidmap, runsummaryMap, beamstatusfilter=None, timeFilter=None, normmap=None, withBXInfo=False, bxAlgo=None, xingMinLum=None, withBeamIntensity=False, lumitype='HF', minbiasXsec=None)
Definition: lumiCalcAPI.py:479
def hltForIds(schema, irunlsdict, dataidmap, hltpathname=None, hltpathpattern=None, withL1Pass=False, withHLTAccept=False)
Definition: lumiCalcAPI.py:152
def intglumiForRange(schema, runlist)
Definition: dataDML.py:1537
def beamForIds(schema, irunlsdict, dataidmap, withBeamIntensity=False, minIntensity=0.1)
Definition: lumiCalcAPI.py:115
def fillrunMap(schema, fillnum=None, runmin=None, runmax=None, startT=None, stopT=None, l1keyPattern=None, hltkeyPattern=None, amodetag=None)
Definition: lumiCalcAPI.py:41
def fillInRange(schema, fillmin=1000, fillmax=9999, amodetag='PROTPHYS', startT=None, stopT=None)
Definition: lumiCalcAPI.py:35
def pixellumisummaryv2TableName()
Definition: nameDealer.py:31
def fillrunMap(schema, fillnum=None, runmin=None, runmax=None, startT=None, stopT=None, l1keyPattern=None, hltkeyPattern=None, amodetag=None)
Definition: dataDML.py:161
def lumiRunByIds(schema, dataidMap, lumitype='HF')
Definition: dataDML.py:905
def hltLSById(schema, dataid, hltpathname=None, hltpathpattern=None, withL1Pass=False, withHLTAccept=False)
Definition: dataDML.py:1438
def trgLSById(schema, dataid, trgbitname=None, trgbitnamepattern=None, withL1Count=False, withPrescale=False)
Definition: dataDML.py:798
def instLumiForIds(schema, irunlsdict, dataidmap, runsummaryMap, beamstatusfilter=None, timeFilter=None, withBXInfo=False, bxAlgo=None, xingMinLum=None, withBeamIntensity=False, lumitype='HF')
Definition: lumiCalcAPI.py:250
def hlttrgMappingByrun(schema, runnum, hltpathname=None, hltpathpattern=None)
Definition: dataDML.py:1391
def trgForIds(schema, irunlsdict, dataidmap, trgbitname=None, trgbitnamepattern=None, withL1Count=False, withPrescale=False)
Definition: lumiCalcAPI.py:197
def runsummary(schema, runnum, sessionflavor='')
Definition: dataDML.py:423
def beamInfoById(schema, dataid, withBeamIntensity=False, minIntensity=0.1)
Definition: dataDML.py:1193
def fillInRange(schema, fillmin, fillmax, amodetag, startT, stopT)
Definition: dataDML.py:90
def pixellumidataTableName()
Definition: nameDealer.py:22
def findUniqueSeed(hltPath, ExprStr)
def runsummary(schema, irunlsdict)
Lumi data management and calculation API # # Author: Zhen Xie #.
Definition: lumiCalcAPI.py:10
def lumidataTableName()
Definition: nameDealer.py:19
def deliveredLumiForIds(schema, irunlsdict, dataidmap, runsummaryMap, beamstatusfilter=None, timeFilter=None, normmap=None, withBXInfo=False, bxAlgo=None, xingMinLum=None, withBeamIntensity=False, lumitype='HF', minbiasXsec=None)
Definition: lumiCalcAPI.py:369
def fillschemePatternMap(schema, lumitype)
Definition: dataDML.py:1573
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='HF')
Definition: lumiCalcAPI.py:47