ATLAS Offline Software
plotBeamSpotVxVal.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4 
5 from __future__ import print_function
6 
7 """
8 Dumps the beam spot parameters to a text (ascii) file
9 """
10 __author__ = 'Peter Loscutoff'
11 __version__ = '$Id $'
12 __usage__ = 'plotBeamSpotVxVal.py [options] bs.nt.root split.nt.root'
13 
14 import sys, os
15 import time
16 from math import sqrt
17 
18 # Argument parsing
19 from optparse import OptionParser
20 parser = OptionParser(usage=__usage__, version=__version__)
21 parser.add_option('-b', '--batch', dest='batch', action='store_true', default=False, help='run in batch mode')
22 parser.add_option('-o', '--output', dest='output', default='.gif', help='comma-separated list of output files or formats')
23 parser.add_option('-H', '--test', dest='test', default=False, help='a test variable (NYI)')
24 parser.add_option('', '--time', dest='time', default=False, help='use time instead of lumiblocks as an axis (NYI)')
25 parser.add_option('-t', '--tracks', dest='plotrange', default=100, help='maximum number of tracks/vertex to plot')
26 
27 (options,args) = parser.parse_args()
28 if len(args) < 2:
29  parser.error('wrong number of command line arguments')
30 
31 # Setup ROOT
32 if options.batch:
33  os.unsetenv('DISPLAY')
34 
35 import math
36 from math import sqrt,sin,cos,floor,fabs
37 from ROOT import TFile,TH1D,gDirectory,TTree,TCanvas,TLegend,gStyle,gROOT,TMatrixDSym,gPad
38 from InDetBeamSpotExample import ROOTUtils
40 import time, calendar
41 from array import array
42 
43 canvas = TCanvas("PlotBsVxVal","PlotBsVxVal",1000,500)
44 canvas.SetFillColor(0)
45 canvas.Divide(2,1)
46 
47 bsfile = TFile(args[0])
48 splitfile = TFile(args[1])
49 
50 # Define our tools to get the time and fill number:
51 #from COOLUtils import RunTimeQuery
52 #c = RunTimeQuery()
53 #from CoolConvUtilities import AtlCoolTool
54 #tool = AtlCoolTool.AtlCoolTool("COOLOFL_DCS/COMP200")
55 
56 subplotSVs = {}
58 
59 fitbins = 3001
60 plotrange = int(options.plotrange) + 1
61 outlierCut = 20
62 plotSV = TH1D("p(s)SV","Pull vs # Tracks",plotrange*2,-.25,plotrange-.25)
63 
64 for i in range(0,plotrange*2):
65  nameSV = "plotSV%s" % i
66  subplotSVs[nameSV]=TH1D(nameSV,nameSV,fitbins,-1*outlierCut,outlierCut)
67 
68 splitfile.cd()
69 tree = gDirectory.Get("splitVertex")
70 entries = tree.GetEntriesFast()
71 for entry in range(entries):
72  noprint = tree.GetEntry(entry)
73  binSV = (tree.tracks_odd+tree.tracks_even)
74  nameSV = "plotSV%i" % binSV
75  if (binSV < (plotrange-1)*2 and tree.tracks_odd >= 4 and tree.tracks_even >= 4):
76  subplotSVs[nameSV].Fill( (tree.x_odd - tree.x_even)/sqrt(tree.c00_odd+tree.c00_even) )
77 
78 for i in range(0,plotrange*2):
79  nameSV = "plotSV%s" % i
80  if subplotSVs[nameSV].Integral() > 2:
81  fitsigma = subplotSVs[nameSV].GetRMS()
82  fserror = (subplotSVs[nameSV].GetRMS())/sqrt(subplotSVs[nameSV].Integral())
83  bin = i+1
84  plotSV.SetBinContent(bin,fitsigma)
85  plotSV.SetBinError(bin,fserror)
86 
87 canvas.cd(1)
88 gPad.SetLeftMargin(.15)
89 gPad.SetBottomMargin(.15)
90 
91 plotSV.SetXTitle("Average # Tracks in Split Vertices")
92 plotSV.SetYTitle("Vertex Error Scale Factor")
93 #plotSV.SetMarkerColor(4)
94 #plotSV.SetMarkerStyle(26)
95 plotSV.SetMarkerStyle(1)
96 plotSV.Draw("e")
97 
98 legend = TLegend(.60,.75,.92,.92)
99 legend.AddEntry(plotSV,"K Factor from Splitting")
100 legend.SetFillColor(0)
101 legend.SetBorderSize(0)
102 
103 legend.Draw()
104 
105 subplots = {}
106 
107 bstable = {}
108 
109 splitWeight = []
110 fullWeight = []
111 
112 lumiedge = array('d')
113 lumibins = 0
114 lumiend = 0
115 
116 bsfile.cd()
117 if gDirectory.Get("BeamSpotNt"):
118  bstree = gDirectory.Get("BeamSpotNt")
119 elif gDirectory.Get("Beamspot/Beamspots"):
120  bstree = gDirectory.Get("Beamspot/Beamspots")
121 entries = bstree.GetEntriesFast()
122 for i in range(entries):
123  noprint = bstree.GetEntry(i)
124  if bstree.k != 0:
125  lumibegin = bstree.lbStart
126  lumiedge.append(lumibegin)
127  lumiend = bstree.lbEnd
128  name = str(lumibegin) + "to" + str(lumiend)
129  lumibins = lumibins + 1
130  subplots[name]=TH1D(name,name,fitbins,-100,100)
131  for lb in range(lumibegin,lumiend+1):
132  bstable[lb] = {}
133  bstable[lb]["x0"] = bstree.x0
134  bstable[lb]["y0"] = bstree.y0
135  bstable[lb]["ax"] = bstree.ax
136  bstable[lb]["ay"] = bstree.ay
137  bstable[lb]["z"] = bstree.z
138  bstable[lb]["sx"] = bstree.sx
139  bstable[lb]["sy"] = bstree.sy
140  bstable[lb]["sz"] = bstree.sz
141  bstable[lb]["rhoxy"] = bstree.rhoxy
142 lumiedge.append(lumiend)
143 
144 plotX = TH1D("p(s)X","k vs Lumi Block",lumibins,lumiedge)
145 plotBS = TH1D("p(s)bs","k vs Lumi Block",lumibins,lumiedge)
146 
147 for i in range(0,21):
148  splitWeight.append(0.0)
149  fullWeight.append(0.0)
150 
151 splitfile.cd()
152 sptree = gDirectory.Get("splitVertex")
153 entries = sptree.GetEntriesFast()
154 for entry in range(entries):
155  noprint = sptree.GetEntry(entry)
156  splitBin = int(.5*(sptree.tracks_odd + sptree.tracks_even) )
157  if splitBin < 11: splitWeight[splitBin] = splitWeight[splitBin] + 1.0
158  else: splitWeight[11] = splitWeight[11] + 1.0
159 
160 bsfile.cd()
161 if gDirectory.Get('Vertices'):
162  vxtree = gDirectory.Get('Vertices')
163 elif gDirectory.Get('Beamspot/Vertices'):
164  vxtree = gDirectory.Get('Beamspot/Vertices')
165 entries = vxtree.GetEntriesFast()
166 for entry in range(entries):
167  noprint = vxtree.GetEntry(entry)
168  fullBin = vxtree.nTracks
169  if fullBin < 11: fullWeight[fullBin] = fullWeight[fullBin] + 1.0
170  else: fullWeight[11] = fullWeight[11] + 1.0
171 
172 for i in range(0,len(splitWeight)):
173  if splitWeight[i] != 0 and fullWeight[i] != 0:
174  splitWeight[i] = fullWeight[i]/splitWeight[i]
175 
176 splitfile.cd()
177 sptree = gDirectory.Get("splitVertex")
178 entries = sptree.GetEntriesFast()
179 for entry in range(entries):
180  noprint = sptree.GetEntry(entry)
181  lumi = sptree.lumi
182  if not lumi in bstable.keys(): continue
183  bs_x0 = bstable[lumi]["x0"]
184  bs_y0 = bstable[lumi]["y0"]
185  bs_a = bstable[lumi]["ax"]
186  bs_b = bstable[lumi]["ay"]
187  bs_z = bstable[lumi]["z"]
188  bs_sx = bstable[lumi]["sx"]
189  bs_sy = bstable[lumi]["sy"]
190  bs_sz = bstable[lumi]["sz"]
191  bs_rho = bstable[lumi]["rhoxy"]
192 
193  name = ""
194  for k in subplots.keys():
195  range = k.split("to")
196  if lumi >= int(range[0]) and lumi <= int(range[1]): name = k
197  if name == "": continue
198  splitBin = int(.5*(sptree.tracks_odd + sptree.tracks_even) )
199  if (sptree.tracks_odd >= 4 and sptree.tracks_even >= 4 ):
200 
201  cov = array('d')
202  cov.append(bs_sx*bs_sx + sptree.c00_odd)
203  cov.append(bs_sx*bs_sy*bs_rho + sptree.c01_odd)
204  cov.append(bs_sx*bs_sy*bs_rho + sptree.c01_odd)
205  cov.append(bs_sy*bs_sy + sptree.c11_odd)
206  covmat = TMatrixDSym(2)
207  covmat.Use(2,cov)
208  covmat.Invert()
209  if (covmat(0,0)*(sptree.x_odd - (bs_x0 + sptree.z_odd*bs_a))**2 +
210  covmat(1,1)*(sptree.y_odd - (bs_y0 + sptree.z_odd*bs_b))**2 +
211  2*covmat(0,1)*(sptree.x_odd - (bs_x0 + sptree.z_odd*bs_a))*(sptree.y_odd - (bs_y0 + sptree.z_odd*bs_b))) > outlierCut:
212  continue
213  cov = array('d')
214  cov.append(bs_sx*bs_sx + sptree.c00_even)
215  cov.append(bs_sx*bs_sy*bs_rho + sptree.c01_even)
216  cov.append(bs_sx*bs_sy*bs_rho + sptree.c01_even)
217  cov.append(bs_sy*bs_sy + sptree.c11_even)
218  covmat = TMatrixDSym(2)
219  covmat.Use(2,cov)
220  covmat.Invert()
221  if (covmat(0,0)*(sptree.x_even - (bs_x0 + sptree.z_even*bs_a))**2 +
222  covmat(1,1)*(sptree.y_even - (bs_y0 + sptree.z_even*bs_b))**2 +
223  2*covmat(0,1)*(sptree.x_even - (bs_x0 + sptree.z_even*bs_a))*(sptree.y_even - (bs_y0 + sptree.z_even*bs_b))) > outlierCut:
224  continue
225 
226  if splitBin < 11:
227  subplots[name].Fill( (sptree.x_odd - sptree.x_even)/sqrt(sptree.c00_odd+sptree.c00_even) , splitWeight[splitBin])
228  subplots[name].Fill( (sptree.y_odd - sptree.y_even)/sqrt(sptree.c11_odd+sptree.c11_even) , splitWeight[splitBin])
229  else:
230  subplots[name].Fill( (sptree.x_odd - sptree.x_even)/sqrt(sptree.c00_odd+sptree.c00_even) , splitWeight[11])
231  subplots[name].Fill( (sptree.y_odd - sptree.y_even)/sqrt(sptree.c11_odd+sptree.c11_even) , splitWeight[11])
232 
233 min = .8
234 max = 1.2
235 
236 sumw=0
237 sumwx=0
238 
239 for name in subplots.keys():
240  if subplots[name].Integral() > 100:
241  midpoint = int(name.split("to")[0])+.5
242  fitsigma = subplots[name].GetRMS()
243  fserror = (subplots[name].GetRMS())/sqrt(subplots[name].Integral())
244  if fitsigma > 0 and fserror > 0:
245  sumw += 1/fserror
246  sumwx += fitsigma/fserror
247  globalbin = plotBS.FindBin(midpoint)
248  bin = plotBS.GetBin(globalbin)
249  plotX.SetBinContent(bin,fitsigma)
250  plotX.SetBinError(bin,fserror)
251  if fitsigma*1.3 > max: max = fitsigma*1.3
252  if fitsigma*.8 < min and fitsigma != 0: min = fitsigma*.8
253 
254 vsavg = sumwx/sumw
255 vserr = 1.0/sumw
256 
257 sumw=0
258 sumwx=0
259 
260 bsfile.cd()
261 if gDirectory.Get('BeamSpotNt'):
262  bstree = gDirecotry.Get('BeamSpotNt')
263 elif gDirectory.Get('Beamspot/Beamspots'):
264  bstree = gDirectory.Get('Beamspot/Beamspots')
265 #bstree = gDirectory.Get("BeamSpotNt")
266 entries = bstree.GetEntriesFast()
267 for i in range(entries):
268  noprint = bstree.GetEntry(i)
269  fitsigma = bstree.k
270  fserror = sqrt(bstree.kk)
271  if fitsigma > 0 and fserror > 0:
272  sumw += 1/fserror
273  sumwx += fitsigma/fserror
274  midpoint = bstree.lbStart+.5
275  globalbin = plotBS.FindBin(midpoint)
276  bin = plotBS.GetBin(globalbin)
277  plotBS.SetBinContent(bin,fitsigma)
278  plotBS.SetBinError(bin,fserror)
279  if fitsigma*1.3 > max: max = fitsigma*1.3
280  if fitsigma*.8 < min and fitsigma != 0: min = fitsigma*.8
281 
282 bsavg = sumwx/sumw
283 bserr = 1.0/sumw
284 
285 print ("Splitting: ",vsavg," +- ",vserr)
286 print ("Beam spot: ",bsavg," +- ",bserr)
287 
288 canvas.cd(2)
289 
290 gPad.SetLeftMargin(.15)
291 gPad.SetBottomMargin(.15)
292 plotX.SetXTitle("Lumi Block")
293 plotX.SetYTitle("Vertex Error Scale Factor")
294 plotX.SetMaximum(max)
295 plotX.SetMinimum(min)
296 plotX.SetLineColor(33)
297 plotX.SetMarkerColor(33)
298 plotX.SetMarkerStyle(1)
299 plotX.SetFillColor(33)
300 plotX.Draw("e2")
301 plotBS.SetMarkerStyle(21)
302 plotBS.SetLineWidth(3)
303 plotBS.Draw("same,e")
304 
305 legend = TLegend(.50,.70,.80,.85)
306 legend.AddEntry(plotBS,"k from beamspot")
307 legend.AddEntry(plotX,"k from vx split")
308 legend.SetFillColor(0)
309 legend.SetBorderSize(0)
310 
311 legend.Draw()
312 
313 if options.output:
314  for o in options.output.split(','):
315  if o[0]=='.':
316  canvas.SaveAs(basename+what+o)
317  else:
318  canvas.SaveAs(o)
319 
320 import os
321 os.environ['PYTHONINSPECT'] = '1'
322 
323 
BeamSpotData
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
ROOTUtils.setStyle
def setStyle(style=None)
Definition: roofit/ROOTUtils.py:413
plotBeamSpotVxVal.covmat
covmat
Definition: plotBeamSpotVxVal.py:206
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
array
str
Definition: BTagTrackIpAccessor.cxx:11