6 from RecoLuminosity.LumiDB
import csvSelectionParser, selectionParser
7 import RecoLuminosity.LumiDB.lumiQueryAPI
as LumiQueryAPI
11 from pprint
import pprint
13 def CalcPileup (deadTable, parameters, luminometer, 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()):
22 if luminometer ==
"HFOC":
27 numerator = float (deadArray[1])
28 denominator = float (deadArray[0])
29 instLumiArray = deadArray[2]
34 livetime = numerator / denominator
41 print "no csv input! Doh!"
51 for xing, xingInstLumi, xingDelvLumi
in instLumiArray:
53 xingIntLumi = xingInstLumi * livetime
54 mean = xingInstLumi * parameters.rotationTime / parameters.lumiSectionLen
57 print "mean number of pileup events > 100 for run %d, lum %d : m %f l %f" % \
58 (runNumber, lumiSection, mean, xingInstLumi)
60 print "mean number of pileup events > 100 for lum %d: m %f l %f" % \
61 (lumiSection, mean, xingInstLumi)
64 if xingInstLumi > threshold:
66 TotalLumi = TotalLumi+xingIntLumi
67 TotalInt+= mean*xingIntLumi
68 FilledXings = FilledXings+1
74 MeanInt = TotalInt/TotalLumi
75 for xing, xingInstLumi, xingDelvlumi
in instLumiArray:
76 if xingInstLumi > threshold:
77 xingIntLumi = xingInstLumi * livetime
78 mean = xingInstLumi * parameters.rotationTime / parameters.lumiSectionLen
79 TotalInt2+= xingIntLumi*(mean-MeanInt)*(mean-MeanInt)
80 TotalWeight+= xingIntLumi
81 TotalWeight2+= xingIntLumi*xingIntLumi
85 if ((lumiSection > 0)):
88 AveLumi = TotalLumi/FilledXings
92 Denom = TotalWeight*TotalWeight-TotalWeight2
93 if TotalLumi > 0
and Denom > 0:
94 RMSLumi =
sqrt(TotalWeight/(TotalWeight*TotalWeight-TotalWeight2)*TotalInt2)
95 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (lumiSection, TotalLumi, RMSLumi, MeanInt)
96 LumiArray.append(lumiSection)
97 LumiArray.append(TotalLumi)
98 LumiArray.append(RMSLumi)
99 LumiArray.append(MeanInt)
100 lumiX=MeanInt*parameters.lumiSectionLen*FilledXings*(1./parameters.rotationTime)
129 if __name__ ==
'__main__':
130 parameters = LumiQueryAPI.ParametersObject()
131 parser = optparse.OptionParser (
"Usage: %prog [--options] output.root",
132 description =
"Script to estimate average instantaneous bunch crossing luminosity using xing instantaneous luminosity information. Output is JSON format file with one entry per LumiSection")
133 inputGroup = optparse.OptionGroup (parser,
"Input Options")
134 pileupGroup = optparse.OptionGroup (parser,
"Pileup Options")
135 inputGroup.add_option (
'--csvInput', dest =
'csvInput', type=
'string', default=
'',
136 help =
'Use CSV file from lumiCalc.py instead of lumiDB')
137 parser.add_option_group (inputGroup)
138 parser.add_option_group (pileupGroup)
140 (options, args) = parser.parse_args()
147 raise RuntimeError(
"Please specify an output file as your last argument")
151 if not options.csvInput:
152 raise "you must specify an input CSV file with (--csvInput)"
159 sepRE = re.compile (
r'[\]\[\s,;:]+')
160 events = open (options.csvInput,
'r')
174 if re.search(
'^#', line):
177 pieces = sepRE.split (line.strip())
181 if len (pieces) < 15:
184 run, lumi = int ( pieces[0] ), int ( pieces[2] )
185 delivered, recorded = float( pieces[11] ), float( pieces[12] )
187 if pieces[0] !=
'run':
188 print " cannot parse csv file "
191 GapDict[lumi] = [delivered, recorded]
197 run, lumi = int ( pieces[0] ), int ( pieces[2] )
198 delivered, recorded = float( pieces[11] ), float( pieces[12] )
199 luminometer = str( pieces[14] )
200 xingInstLumiArray = [( int(orbit), float(lum), float(lumdelv) ) \
201 for orbit, lum, lumdelv
in zip( pieces[15::3],
205 print " Bad Parsing: Check if the input format has changed"
206 print pieces[0],pieces[1],pieces[2],pieces[3],pieces[4],pieces[5],pieces[6],pieces[7],pieces[8],pieces[9]
209 csvDict.setdefault (run, {})[lumi] = \
210 ( delivered, recorded, xingInstLumiArray )
216 for lumiS, lumiInfo
in sorted ( GapDict.iteritems() ):
221 peakratio = lumiInfo[0]/LastDelivered
222 pileup = LastValidLumi[3]*peakratio
225 aveLumi = LastValidLumi[1]*peakratio*lumiInfo[1]/lumiInfo[0]
226 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (lumiS, aveLumi, LastValidLumi[2],pileup)
227 OUTPUTLINE += LumiString
229 LumiString =
"[%d,0.0,0.0,0.0]," % (lumiS)
230 OUTPUTLINE+=LumiString
232 LumiString =
"[%d,0.0,0.0,0.0]," % (lastLumiS+1)
233 OUTPUTLINE+=LumiString
239 LumiString =
"[%d,0.0,0.0,0.0]," % (LastValidLumi[0]+1)
240 OUTPUTLINE+=LumiString
242 lastindex=len(OUTPUTLINE)-1
243 trunc = OUTPUTLINE[0:lastindex]
247 OUTPUTLINE+= (
'"%d":' % run )
251 OUTPUTLINE+=
'[1,0.0,0.0,0.0],'
253 for runNumber, lumiDict
in sorted( csvDict.iteritems() ):
255 LumiArray = CalcPileup (lumiDict, parameters, luminometer,
258 LastValidLumi = LumiArray
259 LastDelivered = lumiDict[LumiArray[0]][0]
262 for lumiS, lumiInfo
in sorted ( GapDict.iteritems() ):
263 peakratio = lumiInfo[0]/LastDelivered
264 pileup = LumiArray[3]*peakratio
267 aveLumi = LumiArray[1]*peakratio*lumiInfo[1]/lumiInfo[0]
268 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (lumiS, aveLumi, LumiArray[2],pileup)
269 OUTPUTLINE += LumiString
271 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (LumiArray[0], LumiArray[1], LumiArray[2], LumiArray[3])
272 OUTPUTLINE += LumiString
275 lastindex=len(OUTPUTLINE)-1
276 trunc = OUTPUTLINE[0:lastindex]
281 outputfile = open(output,
'w')
283 raise RuntimeError(
"Could not open '%s' as an output JSON file" % output)
285 outputfile.write(OUTPUTLINE)
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)