5 from __future__
import print_function
8 Plot beamspot properties vs mu
10 __author__ =
'Anthony Morley'
12 __usage__ =
'''%prog [options] [database1] [database2]
14 Database can be either connection string or db file:
16 e.g COOLOFL_INDET/CONDBR2 (default)
21 PlotVsMu --rl 341123 --ru 341123
25 from DQUtils
import fetch_iovs, process_iovs
26 from DQUtils.sugar
import IOVSet,RANGEIOV_VAL,RunLumiType
27 from array
import array
30 from InDetBeamSpotExample
import BeamSpotData
31 BeamSpotData.varDefs = getattr(BeamSpotData,
'varDefsGen')
33 from CoolLumiUtilities.CoolDataReader
import CoolDataReader
36 ROOT.gROOT.SetBatch(1)
38 lumiTag_default =
'OflPrefLumi-RUN3-UPD4-02'
39 indetbsTag_default =
'IndetBeampos-RUN3-ES1-UPD2-02'
41 from optparse
import OptionParser
42 parser = OptionParser(usage=__usage__, version=__version__)
43 parser.add_option(
'',
'--folderBS', dest=
'folderBS', default=
'/Indet/Beampos', help=
'Folder name (default: /Indet/Beampos)')
44 parser.add_option(
'',
'--folderLumi', dest=
'folderLumi', default=
'/TRIGGER/OFLLUMI/OflPrefLumi', help=
'Folder name (default: /TRIGGER/OFLLUMI/OflPrefLumi')
45 parser.add_option(
'',
'--tagBS', dest=
'tagBS', default=indetbsTag_default, help=
'Tag to compare (default: '+indetbsTag_default+
')')
46 parser.add_option(
'',
'--tagLumi', dest=
'tagLumi', default=lumiTag_default, help=
'Tag to compare to (default: '+lumiTag_default+
')')
47 parser.add_option(
'',
'--rl', dest=
'runMin', type=
'int', default=
None, help=
'Start run number (default: None)')
48 parser.add_option(
'',
'--ru', dest=
'runMax', type=
'int', default=
None, help=
'Max run number (default: None)')
49 parser.add_option(
'-p',
'--plot', dest=
'plot', default=
'', help=
'quantity to plot, only make one plot')
50 parser.add_option(
'',
'--plotGraph' , dest=
'plotGraph', action=
"store_true", default=
False, help=
'plot a graph instead of an histogram')
51 parser.add_option(
'',
'--noPlot', dest=
'plotSomething', action=
"store_false", default=
True, help=
'plot something')
52 (options,args) = parser.parse_args()
54 db1 = args[0]
if len(args)==1
else 'COOLOFL_INDET/CONDBR2'
55 db2 = args[1]
if len(args)==2
else 'COOLOFL_TRIGGER/CONDBR2'
62 varColl = [
'posX',
'posY',
'posZ',
'sigmaX',
'sigmaY',
'sigmaZ']
63 varPlot.append(options.plot)
65 varColl = [
'posX',
'posY',
'posZ',
'sigmaX',
'sigmaY',
'sigmaZ']
66 varPlot = [
'posX',
'posY',
'posZ',
'sigmaX',
'sigmaY',
'sigmaZ']
69 f1 =
"%s::%s" % (db1, options.folderBS)
70 f2 =
"%s::%s" % (db2, options.folderLumi)
73 print(
"Comparing: " )
74 print(
" * ", f1, options.tagBS )
75 print(
" * ", f2, options.tagLumi )
78 requiredForNtuple = [
'posX',
'posY',
'posZ',
'sigmaX',
'sigmaY',
'sigmaZ']
79 checkNtupleProd =
all(item
in varColl
for item
in requiredForNtuple)
80 if not checkNtupleProd:
81 print(
'Ntuple will not be filled missing vars' )
85 from PyCool
import cool
86 from CoolConvUtilities
import AtlCoolLib
87 cooldbBS = AtlCoolLib.indirectOpen(db1,
True,
False)
88 cooldbLumi = AtlCoolLib.indirectOpen(db2,
True,
False)
90 folderBS = cooldbBS.getFolder(options.folderBS)
91 folderLumi = cooldbLumi.getFolder(options.folderLumi)
94 coolQuery = COOLQuery()
96 if options.runMin
is not None:
97 iov1 = options.runMin << 32
98 if options.runMax
is not None:
99 iov2 = (options.runMax +1) << 32
101 iov2 = (options.runMin +1) << 32
102 print(
'Plotting iovs %i to %i ' % (iov1,iov2) )
104 print(
'No run selected -- ERROR' )
107 if (iov2>cool.ValidityKeyMax):
108 iov2=cool.ValidityKeyMax
110 print(
"Reading data from database" )
111 itrBS = folderBS.browseObjects(iov1, iov2, cool.ChannelSelection.all(), options.tagBS)
112 print(
"...finished getting BS data" )
116 startRLB =0x7FFFFFFFFFFFFFFF
119 outfile = ROOT.TFile(
"BeamspotLumi_%i.root" % (options.runMin),
"recreate")
120 ntuple = ROOT.TNtupleD(
'BeamspotLumi',
'BeamSpotLumi',
"x:y:z:sigma_x:sigma_y:sigma_z:run:mu:lumi:year:month:day:hour:minute:epoch" )
125 while itrBS.goToNext():
127 obj = itrBS.currentRef()
131 runBegin = since >> 32
132 lumiBegin = since & 0xFFFFFFFF
135 runUntil = until >> 32
136 lumiUntil = until & 0xFFFFFFFF
138 status =
int(obj.payloadValue(
'status'))
142 if runs.count(runBegin) < 1:
143 runs.append(runBegin)
153 values[var] =
float(obj.payloadValue(var))
154 values[var+
'Err'] =
float(obj.payloadValue(var+
'Err'))
155 values[
'until'] = until
156 lbDict[since] = values
159 print(
'Runs: ',runs )
172 sqtrt2pi = math.sqrt(2*math.pi)
174 lblb = CoolDataReader(
'COOLONL_TRIGGER/CONDBR2',
'/TRIGGER/LUMI/LBLB')
175 from DQUtils.sugar
import RANGEIOV_VAL, RunLumi
176 from DQUtils
import IOVSet
180 iov2 = (run + 1) << 32
182 itrLumi = folderLumi.browseObjects(iov1, iov2, cool.ChannelSelection(0), options.tagLumi)
183 print(
"...finished getting Lumi data for run %i" % run )
185 lblb.setIOVRangeFromRun(run)
187 if len(lblb.data) < 1:
188 print(
'No LBLB data found!' )
192 for obj
in lblb.data:
193 lblbMap[obj.since()] = (obj.payload()[
'StartTime'], obj.payload()[
'EndTime'])
196 while itrLumi.goToNext():
197 obj = itrLumi.currentRef()
200 runBegin = since >> 32
201 lumiBegin = since & 0xFFFFFFFF
204 runUntil = until >> 32
205 lumiUntil = until & 0xFFFFFFFF
208 mu =
float(obj.payloadValue(
'LBAvEvtsPerBX'))
209 instlumi =
float(obj.payloadValue(
'LBAvInstLumi'))
212 if lbDict[ since ][
'sigmaX'] > 0.1 :
214 startTime = lblbMap.get(obj.since(), (0., 0.))[0]
215 endTime = lblbMap.get(lbDict[ since ][
'until'], (0., 0.))[0]
216 mylumi = (endTime - startTime)/1e9 * instlumi/1e9
217 thisTime = time.gmtime( startTime /1.e9 )
224 lumi.append( mylumi );
228 if options.plotSomething:
230 if not var
in ydDict:
231 ydDict[var] =
array(
'd')
232 ydDict2[var] =
array(
'd')
233 eydDict[var] =
array(
'd')
235 ydDict2[var].
append( mu/(lbDict[ since ][var] * sqtrt2pi ))
236 ydDict[var].
append( lbDict[ since ][var] )
237 eydDict[var].
append( lbDict[ since ][var+
'Err'] )
239 timeSt = coolQuery.lbTime(
int(since >> 32),
int(since & 0xFFFFFFFF) )[0]
240 betaStar = coolQuery.getLHCInfo(timeSt).
get(
'BetaStar',0)
241 fillSt = coolQuery.getLHCInfo(timeSt).
get(
'FillNumber',0)
242 bstar.append(betaStar)
244 fillsLB.append(fillSt)
246 if checkNtupleProd
and lbDict[ since ][
'sigmaZErr'] < 5 :
247 ntuple.Fill( lbDict[ since ][
'posX'], lbDict[ since ][
'posY'], lbDict[ since ][
'posZ'], lbDict[ since ][
'sigmaX'], lbDict[ since ][
'sigmaY'], lbDict[ since ][
'sigmaZ'],runBegin, mu, mylumi, year, month, day,hour, mins, startTime /1.e9 )
249 runStart = startRLB >> 32
250 runEnd = endRLB >> 32
251 fillStart = fillEnd = 0
252 timeStart = timeEnd = 0
255 timeStart = coolQuery.lbTime(
int(startRLB >> 32),
int(startRLB & 0xFFFFFFFF) )[0]
259 timeEnd = coolQuery.lbTime(
int(endRLB >> 32),
int(endRLB & 0xFFFFFFFF)-1 )[1]
263 fillStart = coolQuery.getLHCInfo(timeStart).
get(
'FillNumber',0)
267 fillEnd = coolQuery.getLHCInfo(timeEnd).
get(
'FillNumber',0)
271 beamEnergy = coolQuery.getLHCInfo(timeStart).
get(
'BeamEnergyGeV',0)
278 if not options.plotSomething:
281 from InDetBeamSpotExample
import ROOTUtils
283 canvas = ROOT.TCanvas(
'BeamSpotComparison',
'BeamSpotComparison', 1600, 1200)
286 ROOT.gPad.SetTopMargin(0.05)
287 ROOT.gPad.SetLeftMargin(0.15)
288 ROOT.gPad.SetRightMargin(0.05)
290 if not options.plotGraph:
291 ROOT.gPad.SetRightMargin(0.15)
298 for runLB, betaStar
in zip(fillsLB,bstar):
301 pos = fillsFilled.index(runLB)
303 fillsFilled.append(
int(runLB))
305 print(
"Fills with beta* = 30 cm",fillsFilled)
306 length = len(fillsFilled)
307 print(
'Number of fills with beta* = 30 cm: ',length)
310 fillsFilled120 =
list()
311 for runLB, betaStar
in zip(fillsLB,bstar):
312 if betaStar > 119.
and betaStar < 121 :
314 pos = fillsFilled120.index(runLB)
316 fillsFilled120.append(
int(runLB))
317 fillsFilled120.sort()
318 print(
"Fills with beta* = 120 cm",fillsFilled120)
319 length120 = len(fillsFilled120)
320 print(
'Number of fills with beta* = 120 cm: ',length120)
324 if var
not in ydDict:
325 print(
'Missing yd: ',var )
326 if var
not in eydDict:
327 print(
'Missing eyd: ',var )
330 gr = ROOT.TGraphErrors(len(xd), xd, ydDict[var], exd, eydDict[var])
333 ymin =
min(ydDict[var])
334 ymax =
max(ydDict[var])
338 ymaxSmall = ymax + 0.25*h
341 ymin2 =
min(ydDict2[var])
342 ymax2 =
max(ydDict2[var])
353 histo = ROOT.TH2D(
'hd'+var,
'hd'+var, 100, xmin, xmax, 100, ymin, ymax)
354 histo.GetYaxis().SetTitle(
varDef(var,
'atit',var))
355 histo.GetXaxis().SetTitle(
'Average interactions per bunch crossing')
356 histo.GetZaxis().SetTitle(
'Entries')
358 histo2 = ROOT.TH2D(
'hd2'+var,
'hd2'+var, 100, xmin, xmax, 100, ymin2, ymax2)
359 histo2.GetYaxis().SetTitle(
"<Interaction density> @ z=0 [interactions/mm]")
360 histo2.GetXaxis().SetTitle(
'Average interactions per bunch crossing')
361 histo2.GetZaxis().SetTitle(
'Entries')
363 histo2W = ROOT.TH2D(
'hd3'+var,
'hd3'+var, 100, xmin, xmax, 100, ymin2, ymax2)
364 histo2W.GetYaxis().SetTitle(
"<Interaction density> @ z=0 [interactions/mm]")
365 histo2W.GetXaxis().SetTitle(
'Average interactions per bunch crossing')
366 histo2W.GetZaxis().SetTitle(
'Integrated Luminosity (fb^{-1}/bin)')
368 histoW = ROOT.TH2D(
'hdW'+var,
'hdW'+var, 100, xmin, xmax, 100, ymin, ymax)
369 histoW.GetYaxis().SetTitle(
varDef(var,
'atit',var))
370 histoW.GetXaxis().SetTitle(
'Average interactions per bunch crossing')
371 histoW.GetZaxis().SetTitle(
'Integrated Luminosity (fb^{-1}/bin)')
373 histoW1D = ROOT.TH1D(
'hd1D'+var,
'hd1D'+var, 100, ymin, ymaxSmall)
374 histoW1D.GetXaxis().SetTitle(
varDef(var,
'atit',var))
375 histoW1D.GetYaxis().SetTitle(
'Integrated Luminosity (fb^{-1}/bin)')
386 elif var ==
'sigmaX':
391 elif var ==
'sigmaY':
407 histoP = ROOT.TProfile(
'hdP'+var,
'hdP'+var, length,0.,length)
408 histoP.GetYaxis().SetTitle(
varDef(var,
'atit',var))
409 histoP.GetXaxis().SetTitle(
'Fill Number')
410 histoP.SetMinimum(yminP)
411 histoP.SetMaximum(ymaxP)
413 histoB = ROOT.TH2D(
'hdB'+var,
'hdB'+var, 100, 20., 150., 100, yminB, ymaxB)
414 histoB.GetYaxis().SetTitle(
varDef(var,
'atit',var))
415 histoB.GetXaxis().SetTitle(
'#beta*')
416 histoB.GetZaxis().SetTitle(
'Entries')
420 irate =
int(length/10)+1
421 for run
in fillsFilled:
423 if int((i-1)/irate)*irate == i-1:
424 histoP.GetXaxis().SetBinLabel(i,
str(run))
426 histoP.GetXaxis().SetBinLabel(i,
'')
429 irate =
int(length120/10)+1
431 if options.plotGraph:
434 for mu, x, l
in zip(xd, ydDict[var], lumi):
436 histoW.Fill( mu, x , l)
438 for mu, x, l
in zip(xd, ydDict2[var], lumi):
440 histo2W.Fill( mu,x, l)
441 for runLB, betaStar, x
in zip(fillsLB,bstar,ydDict[var]):
443 if runLB < 435229
or runLB > 435777 :
444 histoB.Fill( betaStar, x )
446 pos = fillsFilled.index(runLB)
447 histoP.Fill(pos+0.5,x)
448 if betaStar > 119.
and betaStar < 121 :
449 pos = fillsFilled120.index(runLB)
464 ROOTUtils.atlasLabel( 0.53,0.87,
False,offset=0.12,isForApproval=
False,customstring=
"Internal",energy=
'%2.1f' % beamEnergy,size=0.055)
470 comments.append(
'Run %i' % runStart)
472 comments.append(
'Runs %i - %i' % (runStart,runEnd))
474 if fillStart==fillEnd:
475 comments.append(
'Fill %i' % fillStart)
477 comments.append(
'Fills %i - %i' % (fillStart,fillEnd))
479 t1 = time.strftime(
'%d %b %Y',time.localtime(timeStart))
480 t2 = time.strftime(
'%d %b %Y',time.localtime(timeEnd))
484 comments.append(
'%s - %s' % (t1,t2))
488 canvas.Print(
"Run_%d_%sVsMu.png" % ( options.runMin,var ) )
490 if not options.plotGraph:
491 canvas.SetLogz(
False)
493 canvas.SetLogy(
False)
496 ROOTUtils.atlasLabel( 0.53,0.87,
False,offset=0.12,isForApproval=
False,customstring=
"Internal",energy=
'%2.1f' % beamEnergy,size=0.055)
500 canvas.Print(
"Run_%d_%sVsFill.png" % ( options.runMin,var ) )
501 canvas.SetLogy(
False)
504 ROOTUtils.atlasLabel( 0.53,0.87,
False,offset=0.12,isForApproval=
False,customstring=
"Internal",energy=
'%2.1f' % beamEnergy,size=0.055)
507 canvas.Print(
"Run_%d_%sVsBetaStar.png" % ( options.runMin,var ) )
508 canvas.SetLogy(
False)
510 if __name__ ==
"__main__":