CMS 3D CMS Logo

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