2 from __future__
import print_function
7 from RecoLuminosity.LumiDB
import csvSelectionParser, selectionParser
8 import RecoLuminosity.LumiDB.lumiQueryAPI
as LumiQueryAPI
12 from pprint
import pprint
15 def CalcPileup (deadTable, parameters, mode='deadtable'):
16 '''Given a deadtable, will calculate parameters of pileup distribution. Return formatted 17 string with LumiSection, LS integrated lumi, RMS of bunch to bunch lumi and pileup.''' 22 for lumiSection, deadArray
in sorted (six.iteritems(deadTable)):
25 numerator = float (deadArray[1])
26 denominator = float (deadArray[0])
27 instLumiArray = deadArray[2]
32 livetime = numerator / denominator
35 print(
"no csv input! Doh!")
45 for xing, xingInstLumi
in instLumiArray:
46 xingIntLumi = xingInstLumi * parameters.lumiSectionLen * livetime
47 mean = xingInstLumi * parameters.rotationTime
50 print(
"mean number of pileup events > 100 for run %d, lum %d : m %f l %f" % \
51 (runNumber, lumiSection, mean, xingInstLumi))
53 print(
"mean number of pileup events > 100 for lum %d: m %f l %f" % \
54 (lumiSection, mean, xingInstLumi))
57 if xingInstLumi > 0.1:
59 TotalLumi = TotalLumi+xingIntLumi
60 TotalInt+= mean*xingIntLumi
61 FilledXings = FilledXings+1
67 MeanInt = TotalInt/TotalLumi
68 for xing, xingInstLumi
in instLumiArray:
69 if xingInstLumi > 0.1:
70 xingIntLumi = xingInstLumi * parameters.lumiSectionLen * livetime
71 mean = xingInstLumi * parameters.rotationTime
72 TotalInt2+= xingIntLumi*(mean-MeanInt)*(mean-MeanInt)
73 TotalWeight+= xingIntLumi
74 TotalWeight2+= xingIntLumi*xingIntLumi
78 if ((lumiSection > 0)):
81 AveLumi = TotalLumi/FilledXings
85 Denom = TotalWeight*TotalWeight-TotalWeight2
86 if TotalLumi > 0
and Denom > 0:
87 RMSLumi =
sqrt(TotalWeight/(TotalWeight*TotalWeight-TotalWeight2)*TotalInt2)
88 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (lumiSection, TotalLumi, RMSLumi, MeanInt)
89 LumiArray.append(lumiSection)
90 LumiArray.append(TotalLumi)
91 LumiArray.append(RMSLumi)
92 LumiArray.append(MeanInt)
111 if __name__ ==
'__main__':
112 parameters = LumiQueryAPI.ParametersObject()
113 parser = optparse.OptionParser (
"Usage: %prog [--options] output.root",
114 description =
"Script to estimate average instantaneous bunch crossing luminosity using xing instantaneous luminosity information. Output is JSON format file with one entry per LumiSection")
115 inputGroup = optparse.OptionGroup (parser,
"Input Options")
116 pileupGroup = optparse.OptionGroup (parser,
"Pileup Options")
117 inputGroup.add_option (
'--csvInput', dest =
'csvInput', type=
'string', default=
'',
118 help =
'Use CSV file from lumiCalc.py instead of lumiDB')
119 parser.add_option_group (inputGroup)
120 parser.add_option_group (pileupGroup)
122 (options, args) = parser.parse_args()
129 raise RuntimeError(
"Please specify an output file as your last argument")
133 if not options.csvInput:
134 raise "you must specify an input CSV file with (--csvInput)" 141 sepRE = re.compile (
r'[\s,;:]+')
142 events = open (options.csvInput,
'r') 155 pieces = sepRE.split (line.strip())
159 run, lumi = int ( pieces[0] ), int ( pieces[2] )
162 if pieces[0] !=
'run':
163 print(
" cannot parse csv file ")
166 GapDict[lumi] = [delivered, recorded]
172 run, lumi = int ( pieces[0] ), int ( pieces[2] )
173 delivered, recorded =
float( pieces[8] ),
float( pieces[9] )
175 for orbit, lum
in zip( pieces[10::2],
178 print(
" Bad Parsing: Check if the input format has changed")
179 print(pieces[0],pieces[1],pieces[2],pieces[3],pieces[4],pieces[5],pieces[6])
182 csvDict.setdefault (run, {})[lumi] = \
183 ( delivered, recorded, xingInstLumiArray )
189 for lumiS, lumiInfo
in sorted ( six.iteritems(GapDict) ):
194 peakratio = lumiInfo[0]/LastDelivered
195 pileup = LastValidLumi[3]*peakratio
198 aveLumi = LastValidLumi[1]*peakratio*lumiInfo[1]/lumiInfo[0]
199 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (lumiS, aveLumi, LastValidLumi[2],pileup)
200 OUTPUTLINE += LumiString
202 LumiString =
"[%d,0.0,0.0,0.0]," % (lumiS)
203 OUTPUTLINE+=LumiString
205 LumiString =
"[%d,0.0,0.0,0.0]," % (lastLumiS+1)
206 OUTPUTLINE+=LumiString
212 LumiString =
"[%d,0.0,0.0,0.0]," % (LastValidLumi[0]+1)
213 OUTPUTLINE+=LumiString
215 lastindex=len(OUTPUTLINE)-1
216 trunc = OUTPUTLINE[0:lastindex]
220 OUTPUTLINE+= (
'"%d":' % run )
224 OUTPUTLINE+=
'[1,0.0,0.0,0.0],' 226 for runNumber, lumiDict
in sorted( six.iteritems(csvDict) ):
228 LumiArray = CalcPileup (lumiDict, parameters,
231 LastValidLumi = LumiArray
232 LastDelivered = lumiDict[LumiArray[0]][0]
235 for lumiS, lumiInfo
in sorted ( six.iteritems(GapDict) ):
236 peakratio = lumiInfo[0]/LastDelivered
237 pileup = LumiArray[3]*peakratio
240 aveLumi = LumiArray[1]*peakratio*lumiInfo[1]/lumiInfo[0]
241 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (lumiS, aveLumi, LumiArray[2],pileup)
242 OUTPUTLINE += LumiString
244 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (LumiArray[0], LumiArray[1], LumiArray[2], LumiArray[3])
245 OUTPUTLINE += LumiString
248 lastindex=len(OUTPUTLINE)-1
249 trunc = OUTPUTLINE[0:lastindex]
254 outputfile = open(output,
'w')
256 raise RuntimeError(
"Could not open '%s' as an output JSON file" % output)
258 outputfile.write(OUTPUTLINE)
S & print(S &os, JobReport::InputFile const &f)
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
def CalcPileup(deadTable, parameters, mode='deadtable')