6 from RecoLuminosity.LumiDB
import csvSelectionParser, selectionParser
7 import RecoLuminosity.LumiDB.lumiQueryAPI
as LumiQueryAPI
11 from pprint
import pprint
13 def CalcPileup (deadTable, parameters, mode='deadtable'):
14 '''Given a deadtable, will calculate parameters of pileup distribution. Return formatted
15 string with LumiSection, LS integrated lumi, RMS of bunch to bunch lumi and pileup.'''
20 for lumiSection, deadArray
in sorted (deadTable.iteritems()):
23 numerator = float (deadArray[1])
24 denominator = float (deadArray[0])
25 instLumiArray = deadArray[2]
30 livetime = numerator / denominator
33 print "no csv input! Doh!"
43 for xing, xingInstLumi
in instLumiArray:
44 xingIntLumi = xingInstLumi * parameters.lumiSectionLen * livetime
45 mean = xingInstLumi * parameters.rotationTime
48 print "mean number of pileup events > 100 for run %d, lum %d : m %f l %f" % \
49 (runNumber, lumiSection, mean, xingInstLumi)
51 print "mean number of pileup events > 100 for lum %d: m %f l %f" % \
52 (lumiSection, mean, xingInstLumi)
55 if xingInstLumi > 0.1:
57 TotalLumi = TotalLumi+xingIntLumi
58 TotalInt+= mean*xingIntLumi
59 FilledXings = FilledXings+1
65 MeanInt = TotalInt/TotalLumi
66 for xing, xingInstLumi
in instLumiArray:
67 if xingInstLumi > 0.1:
68 xingIntLumi = xingInstLumi * parameters.lumiSectionLen * livetime
69 mean = xingInstLumi * parameters.rotationTime
70 TotalInt2+= xingIntLumi*(mean-MeanInt)*(mean-MeanInt)
71 TotalWeight+= xingIntLumi
72 TotalWeight2+= xingIntLumi*xingIntLumi
76 if ((lumiSection > 0)):
79 AveLumi = TotalLumi/FilledXings
83 Denom = TotalWeight*TotalWeight-TotalWeight2
84 if TotalLumi > 0
and Denom > 0:
85 RMSLumi =
sqrt(TotalWeight/(TotalWeight*TotalWeight-TotalWeight2)*TotalInt2)
86 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (lumiSection, TotalLumi, RMSLumi, MeanInt)
87 LumiArray.append(lumiSection)
88 LumiArray.append(TotalLumi)
89 LumiArray.append(RMSLumi)
90 LumiArray.append(MeanInt)
109 if __name__ ==
'__main__':
110 parameters = LumiQueryAPI.ParametersObject()
111 parser = optparse.OptionParser (
"Usage: %prog [--options] output.root",
112 description =
"Script to estimate average instantaneous bunch crossing luminosity using xing instantaneous luminosity information. Output is JSON format file with one entry per LumiSection")
113 inputGroup = optparse.OptionGroup (parser,
"Input Options")
114 pileupGroup = optparse.OptionGroup (parser,
"Pileup Options")
115 inputGroup.add_option (
'--csvInput', dest =
'csvInput', type=
'string', default=
'',
116 help =
'Use CSV file from lumiCalc.py instead of lumiDB')
117 parser.add_option_group (inputGroup)
118 parser.add_option_group (pileupGroup)
120 (options, args) = parser.parse_args()
127 raise RuntimeError,
"Please specify an output file as your last argument"
131 if not options.csvInput:
132 raise "you must specify an input CSV file with (--csvInput)"
139 sepRE = re.compile (
r'[\s,;:]+')
140 events = open (options.csvInput,
'r')
153 pieces = sepRE.split (line.strip())
157 run, lumi = int ( pieces[0] ), int ( pieces[2] )
158 delivered, recorded = float( pieces[7] ), float( pieces[8] )
160 if pieces[0] !=
'run':
161 print " cannot parse csv file "
164 GapDict[lumi] = [delivered, recorded]
170 run, lumi = int ( pieces[0] ), int ( pieces[2] )
171 delivered, recorded = float( pieces[7] ), float( pieces[8] )
172 xingInstLumiArray = [( int(orbit), float(lum) ) \
173 for orbit, lum
in zip( pieces[9::2],
176 print " Bad Parsing: Check if the input format has changed"
177 print pieces[0],pieces[1],pieces[2],pieces[3],pieces[4],pieces[5],pieces[6]
180 csvDict.setdefault (run, {})[lumi] = \
181 ( delivered, recorded, xingInstLumiArray )
187 for lumiS, lumiInfo
in sorted ( GapDict.iteritems() ):
192 peakratio = lumiInfo[0]/LastDelivered
193 pileup = LastValidLumi[3]*peakratio
196 aveLumi = LastValidLumi[1]*peakratio*lumiInfo[1]/lumiInfo[0]
197 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (lumiS, aveLumi, LastValidLumi[2],pileup)
198 OUTPUTLINE += LumiString
200 LumiString =
"[%d,0.0,0.0,0.0]," % (lumiS)
201 OUTPUTLINE+=LumiString
203 LumiString =
"[%d,0.0,0.0,0.0]," % (lastLumiS+1)
204 OUTPUTLINE+=LumiString
210 LumiString =
"[%d,0.0,0.0,0.0]," % (LastValidLumi[0]+1)
211 OUTPUTLINE+=LumiString
213 lastindex=len(OUTPUTLINE)-1
214 trunc = OUTPUTLINE[0:lastindex]
218 OUTPUTLINE+= (
'"%d":' % run )
222 OUTPUTLINE+=
'[1,0.0,0.0,0.0],'
224 for runNumber, lumiDict
in sorted( csvDict.iteritems() ):
226 LumiArray = CalcPileup (lumiDict, parameters,
229 LastValidLumi = LumiArray
230 LastDelivered = lumiDict[LumiArray[0]][0]
233 for lumiS, lumiInfo
in sorted ( GapDict.iteritems() ):
234 peakratio = lumiInfo[0]/LastDelivered
235 pileup = LumiArray[3]*peakratio
238 aveLumi = LumiArray[1]*peakratio*lumiInfo[1]/lumiInfo[0]
239 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (lumiS, aveLumi, LumiArray[2],pileup)
240 OUTPUTLINE += LumiString
242 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (LumiArray[0], LumiArray[1], LumiArray[2], LumiArray[3])
243 OUTPUTLINE += LumiString
246 lastindex=len(OUTPUTLINE)-1
247 trunc = OUTPUTLINE[0:lastindex]
252 outputfile = open(output,
'w')
254 raise RuntimeError, \
255 "Could not open '%s' as an output JSON file" % output
257 outputfile.write(OUTPUTLINE)