6 from RecoLuminosity.LumiDB
import csvSelectionParser, selectionParser
7 import RecoLuminosity.LumiDB.lumiQueryAPI
as LumiQueryAPI
11 from pprint
import pprint
14 def CalcPileup (deadTable, parameters, mode='deadtable'):
15 '''Given a deadtable, will calculate parameters of pileup distribution. Return formatted 16 string with LumiSection, LS integrated lumi, RMS of bunch to bunch lumi and pileup.''' 21 for lumiSection, deadArray
in sorted (six.iteritems(deadTable)):
24 numerator = float (deadArray[1])
25 denominator = float (deadArray[0])
26 instLumiArray = deadArray[2]
31 livetime = numerator / denominator
34 print "no csv input! Doh!" 44 for xing, xingInstLumi
in instLumiArray:
45 xingIntLumi = xingInstLumi * parameters.lumiSectionLen * livetime
46 mean = xingInstLumi * parameters.rotationTime
49 print "mean number of pileup events > 100 for run %d, lum %d : m %f l %f" % \
50 (runNumber, lumiSection, mean, xingInstLumi)
52 print "mean number of pileup events > 100 for lum %d: m %f l %f" % \
53 (lumiSection, mean, xingInstLumi)
56 if xingInstLumi > 0.1:
58 TotalLumi = TotalLumi+xingIntLumi
59 TotalInt+= mean*xingIntLumi
60 FilledXings = FilledXings+1
66 MeanInt = TotalInt/TotalLumi
67 for xing, xingInstLumi
in instLumiArray:
68 if xingInstLumi > 0.1:
69 xingIntLumi = xingInstLumi * parameters.lumiSectionLen * livetime
70 mean = xingInstLumi * parameters.rotationTime
71 TotalInt2+= xingIntLumi*(mean-MeanInt)*(mean-MeanInt)
72 TotalWeight+= xingIntLumi
73 TotalWeight2+= xingIntLumi*xingIntLumi
77 if ((lumiSection > 0)):
80 AveLumi = TotalLumi/FilledXings
84 Denom = TotalWeight*TotalWeight-TotalWeight2
85 if TotalLumi > 0
and Denom > 0:
86 RMSLumi =
sqrt(TotalWeight/(TotalWeight*TotalWeight-TotalWeight2)*TotalInt2)
87 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (lumiSection, TotalLumi, RMSLumi, MeanInt)
88 LumiArray.append(lumiSection)
89 LumiArray.append(TotalLumi)
90 LumiArray.append(RMSLumi)
91 LumiArray.append(MeanInt)
110 if __name__ ==
'__main__':
111 parameters = LumiQueryAPI.ParametersObject()
112 parser = optparse.OptionParser (
"Usage: %prog [--options] output.root",
113 description =
"Script to estimate average instantaneous bunch crossing luminosity using xing instantaneous luminosity information. Output is JSON format file with one entry per LumiSection")
114 inputGroup = optparse.OptionGroup (parser,
"Input Options")
115 pileupGroup = optparse.OptionGroup (parser,
"Pileup Options")
116 inputGroup.add_option (
'--csvInput', dest =
'csvInput', type=
'string', default=
'',
117 help =
'Use CSV file from lumiCalc.py instead of lumiDB')
118 parser.add_option_group (inputGroup)
119 parser.add_option_group (pileupGroup)
121 (options, args) = parser.parse_args()
128 raise RuntimeError(
"Please specify an output file as your last argument")
132 if not options.csvInput:
133 raise "you must specify an input CSV file with (--csvInput)" 140 sepRE = re.compile (
r'[\s,;:]+')
141 events = open (options.csvInput,
'r') 154 pieces = sepRE.split (line.strip())
158 run, lumi = int ( pieces[0] ), int ( pieces[2] )
161 if pieces[0] !=
'run':
162 print " cannot parse csv file " 165 GapDict[lumi] = [delivered, recorded]
171 run, lumi = int ( pieces[0] ), int ( pieces[2] )
172 delivered, recorded =
float( pieces[8] ),
float( pieces[9] )
174 for orbit, lum
in zip( pieces[10::2],
177 print " Bad Parsing: Check if the input format has changed" 178 print pieces[0],pieces[1],pieces[2],pieces[3],pieces[4],pieces[5],pieces[6]
181 csvDict.setdefault (run, {})[lumi] = \
182 ( delivered, recorded, xingInstLumiArray )
188 for lumiS, lumiInfo
in sorted ( six.iteritems(GapDict) ):
193 peakratio = lumiInfo[0]/LastDelivered
194 pileup = LastValidLumi[3]*peakratio
197 aveLumi = LastValidLumi[1]*peakratio*lumiInfo[1]/lumiInfo[0]
198 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (lumiS, aveLumi, LastValidLumi[2],pileup)
199 OUTPUTLINE += LumiString
201 LumiString =
"[%d,0.0,0.0,0.0]," % (lumiS)
202 OUTPUTLINE+=LumiString
204 LumiString =
"[%d,0.0,0.0,0.0]," % (lastLumiS+1)
205 OUTPUTLINE+=LumiString
211 LumiString =
"[%d,0.0,0.0,0.0]," % (LastValidLumi[0]+1)
212 OUTPUTLINE+=LumiString
214 lastindex=len(OUTPUTLINE)-1
215 trunc = OUTPUTLINE[0:lastindex]
219 OUTPUTLINE+= (
'"%d":' % run )
223 OUTPUTLINE+=
'[1,0.0,0.0,0.0],' 225 for runNumber, lumiDict
in sorted( six.iteritems(csvDict) ):
227 LumiArray = CalcPileup (lumiDict, parameters,
230 LastValidLumi = LumiArray
231 LastDelivered = lumiDict[LumiArray[0]][0]
234 for lumiS, lumiInfo
in sorted ( six.iteritems(GapDict) ):
235 peakratio = lumiInfo[0]/LastDelivered
236 pileup = LumiArray[3]*peakratio
239 aveLumi = LumiArray[1]*peakratio*lumiInfo[1]/lumiInfo[0]
240 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (lumiS, aveLumi, LumiArray[2],pileup)
241 OUTPUTLINE += LumiString
243 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (LumiArray[0], LumiArray[1], LumiArray[2], LumiArray[3])
244 OUTPUTLINE += LumiString
247 lastindex=len(OUTPUTLINE)-1
248 trunc = OUTPUTLINE[0:lastindex]
253 outputfile = open(output,
'w')
255 raise RuntimeError(
"Could not open '%s' as an output JSON file" % output)
257 outputfile.write(OUTPUTLINE)
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
def CalcPileup(deadTable, parameters, mode='deadtable')