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
16 def CalcPileup (deadTable, parameters, luminometer, mode='deadtable'):
17 '''Given a deadtable, will calculate parameters of pileup distribution. Return formatted 18 string with LumiSection, LS integrated lumi, RMS of bunch to bunch lumi and pileup.''' 23 for lumiSection, deadArray
in sorted (six.iteritems(deadTable)):
25 if luminometer ==
"HFOC":
30 numerator = float (deadArray[1])
31 denominator = float (deadArray[0])
32 instLumiArray = deadArray[2]
37 livetime = numerator / denominator
44 print(
"no csv input! Doh!")
54 for xing, xingInstLumi, xingDelvLumi
in instLumiArray:
55 if selBXs
and xing
not in selBXs:
57 xingIntLumi = xingInstLumi * livetime
58 mean = xingInstLumi * parameters.rotationTime / parameters.lumiSectionLen
61 print(
"mean number of pileup events > 100 for run %d, lum %d : m %f l %f" % \
62 (runNumber, lumiSection, mean, xingInstLumi))
64 print(
"mean number of pileup events > 100 for lum %d: m %f l %f" % \
65 (lumiSection, mean, xingInstLumi))
68 if xingInstLumi > threshold:
70 TotalLumi = TotalLumi+xingIntLumi
71 TotalInt+= mean*xingIntLumi
72 FilledXings = FilledXings+1
78 MeanInt = TotalInt/TotalLumi
79 for xing, xingInstLumi, xingDelvlumi
in instLumiArray:
80 if selBXs
and xing
not in selBXs:
82 if xingInstLumi > threshold:
83 xingIntLumi = xingInstLumi * livetime
84 mean = xingInstLumi * parameters.rotationTime / parameters.lumiSectionLen
85 TotalInt2+= xingIntLumi*(mean-MeanInt)*(mean-MeanInt)
86 TotalWeight+= xingIntLumi
87 TotalWeight2+= xingIntLumi*xingIntLumi
91 if ((lumiSection > 0)):
94 AveLumi = TotalLumi/FilledXings
98 Denom = TotalWeight*TotalWeight-TotalWeight2
99 if TotalLumi > 0
and Denom > 0:
100 RMSLumi =
sqrt(TotalWeight/(TotalWeight*TotalWeight-TotalWeight2)*TotalInt2)
101 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (lumiSection, TotalLumi, RMSLumi, MeanInt)
102 LumiArray.append(lumiSection)
103 LumiArray.append(TotalLumi)
104 LumiArray.append(RMSLumi)
105 LumiArray.append(MeanInt)
106 lumiX=MeanInt*parameters.lumiSectionLen*FilledXings*(1./parameters.rotationTime)
135 if __name__ ==
'__main__':
136 parameters = LumiQueryAPI.ParametersObject()
137 parser = optparse.OptionParser (
"Usage: %prog [--options] output.root",
138 description =
"Script to estimate average instantaneous bunch crossing luminosity using xing instantaneous luminosity information. Output is JSON format file with one entry per LumiSection")
139 inputGroup = optparse.OptionGroup (parser,
"Input Options")
140 pileupGroup = optparse.OptionGroup (parser,
"Pileup Options")
141 inputGroup.add_option (
'--csvInput', dest =
'csvInput', type=
'string', default=
'',
142 help =
'Use CSV file from lumiCalc.py instead of lumiDB')
143 inputGroup.add_option (
'--selBXs', dest =
'selBXs', type=
'string', default=
'',
144 help =
'CSV of BXs to use; if empty, select all')
145 parser.add_option_group (inputGroup)
146 parser.add_option_group (pileupGroup)
148 (options, args) = parser.parse_args()
155 raise RuntimeError(
"Please specify an output file as your last argument")
159 if not options.csvInput:
160 raise "you must specify an input CSV file with (--csvInput)" 162 if options.selBXs !=
"":
163 for iBX
in options.selBXs.split(
","):
169 print(iBX,
"is not an int")
171 print(
"selBXs",selBXs)
178 sepRE = re.compile (
r'[\]\[\s,;:]+')
179 events = open (options.csvInput,
'r') 193 if re.search(
'^#', line):
196 pieces = sepRE.split (line.strip())
200 if len (pieces) < 15:
203 run, lumi = int ( pieces[0] ), int ( pieces[2] )
204 delivered, recorded =
float( pieces[11] ),
float( pieces[12] )
206 if pieces[0] !=
'run':
207 print(
" cannot parse csv file ")
210 GapDict[lumi] = [delivered, recorded]
216 run, lumi = int ( pieces[0] ), int ( pieces[2] )
217 delivered, recorded =
float( pieces[11] ),
float( pieces[12] )
218 luminometer =
str( pieces[14] )
220 for orbit, lum, lumdelv
in zip( pieces[15::3],
224 print(
" Bad Parsing: Check if the input format has changed")
225 print(pieces[0],pieces[1],pieces[2],pieces[3],pieces[4],pieces[5],pieces[6],pieces[7],pieces[8],pieces[9])
228 csvDict.setdefault (run, {})[lumi] = \
229 ( delivered, recorded, xingInstLumiArray )
235 for lumiS, lumiInfo
in sorted ( six.iteritems(GapDict) ):
240 peakratio = lumiInfo[0]/LastDelivered
241 pileup = LastValidLumi[3]*peakratio
244 aveLumi = LastValidLumi[1]*peakratio*lumiInfo[1]/lumiInfo[0]
245 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (lumiS, aveLumi, LastValidLumi[2],pileup)
246 OUTPUTLINE += LumiString
248 LumiString =
"[%d,0.0,0.0,0.0]," % (lumiS)
249 OUTPUTLINE+=LumiString
251 LumiString =
"[%d,0.0,0.0,0.0]," % (lastLumiS+1)
252 OUTPUTLINE+=LumiString
258 LumiString =
"[%d,0.0,0.0,0.0]," % (LastValidLumi[0]+1)
259 OUTPUTLINE+=LumiString
261 lastindex=len(OUTPUTLINE)-1
262 trunc = OUTPUTLINE[0:lastindex]
266 OUTPUTLINE+= (
'"%d":' % run )
270 OUTPUTLINE+=
'[1,0.0,0.0,0.0],' 272 for runNumber, lumiDict
in sorted( six.iteritems(csvDict) ):
274 LumiArray = CalcPileup (lumiDict, parameters, luminometer,
277 LastValidLumi = LumiArray
278 LastDelivered = lumiDict[LumiArray[0]][0]
281 for lumiS, lumiInfo
in sorted ( six.iteritems(GapDict) ):
282 peakratio = lumiInfo[0]/LastDelivered
283 pileup = LumiArray[3]*peakratio
286 aveLumi = LumiArray[1]*peakratio*lumiInfo[1]/lumiInfo[0]
287 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (lumiS, aveLumi, LumiArray[2],pileup)
288 OUTPUTLINE += LumiString
290 LumiString =
"[%d,%2.4e,%2.4e,%2.4e]," % (LumiArray[0], LumiArray[1], LumiArray[2], LumiArray[3])
291 OUTPUTLINE += LumiString
294 lastindex=len(OUTPUTLINE)-1
295 trunc = OUTPUTLINE[0:lastindex]
300 outputfile = open(output,
'w')
302 raise RuntimeError(
"Could not open '%s' as an output JSON file" % output)
304 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, luminometer, mode='deadtable')