5 from __future__
import print_function
8 Dumps the beam spot parameters to a text (ascii) file
10 __author__ =
'Peter Loscutoff'
12 __usage__ =
'plotBeamSpotVxVal.py [options] bs.nt.root split.nt.root'
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')
27 (options,args) = parser.parse_args()
29 parser.error(
'wrong number of command line arguments')
33 os.unsetenv(
'DISPLAY')
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
41 from array
import array
43 canvas = TCanvas(
"PlotBsVxVal",
"PlotBsVxVal",1000,500)
44 canvas.SetFillColor(0)
47 bsfile = TFile(args[0])
48 splitfile = TFile(args[1])
60 plotrange =
int(options.plotrange) + 1
62 plotSV = TH1D(
"p(s)SV",
"Pull vs # Tracks",plotrange*2,-.25,plotrange-.25)
64 for i
in range(0,plotrange*2):
65 nameSV =
"plotSV%s" % i
66 subplotSVs[nameSV]=TH1D(nameSV,nameSV,fitbins,-1*outlierCut,outlierCut)
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) )
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())
84 plotSV.SetBinContent(bin,fitsigma)
85 plotSV.SetBinError(bin,fserror)
88 gPad.SetLeftMargin(.15)
89 gPad.SetBottomMargin(.15)
91 plotSV.SetXTitle(
"Average # Tracks in Split Vertices")
92 plotSV.SetYTitle(
"Vertex Error Scale Factor")
95 plotSV.SetMarkerStyle(1)
98 legend = TLegend(.60,.75,.92,.92)
99 legend.AddEntry(plotSV,
"K Factor from Splitting")
100 legend.SetFillColor(0)
101 legend.SetBorderSize(0)
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)
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):
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)
144 plotX = TH1D(
"p(s)X",
"k vs Lumi Block",lumibins,lumiedge)
145 plotBS = TH1D(
"p(s)bs",
"k vs Lumi Block",lumibins,lumiedge)
147 for i
in range(0,21):
148 splitWeight.append(0.0)
149 fullWeight.append(0.0)
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
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
172 for i
in range(0,len(splitWeight)):
173 if splitWeight[i] != 0
and fullWeight[i] != 0:
174 splitWeight[i] = fullWeight[i]/splitWeight[i]
177 sptree = gDirectory.Get(
"splitVertex")
178 entries = sptree.GetEntriesFast()
179 for entry
in range(entries):
180 noprint = sptree.GetEntry(entry)
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"]
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 ):
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)
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:
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)
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:
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])
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])
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:
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
261 if gDirectory.Get(
'BeamSpotNt'):
262 bstree = gDirecotry.Get(
'BeamSpotNt')
263 elif gDirectory.Get(
'Beamspot/Beamspots'):
264 bstree = gDirectory.Get(
'Beamspot/Beamspots')
266 entries = bstree.GetEntriesFast()
267 for i
in range(entries):
268 noprint = bstree.GetEntry(i)
270 fserror = sqrt(bstree.kk)
271 if fitsigma > 0
and fserror > 0:
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
285 print (
"Splitting: ",vsavg,
" +- ",vserr)
286 print (
"Beam spot: ",bsavg,
" +- ",bserr)
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)
301 plotBS.SetMarkerStyle(21)
302 plotBS.SetLineWidth(3)
303 plotBS.Draw(
"same,e")
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)
314 for o
in options.output.split(
','):
316 canvas.SaveAs(basename+what+o)
321 os.environ[
'PYTHONINSPECT'] =
'1'