ATLAS Offline Software
Loading...
Searching...
No Matches
plotBeamSpotVert.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
6"""
7Create beam spot primary vertex plots (e.g. for approved plots).
8"""
9__author__ = 'Juerg Beringer'
10__version__ = '$Id:$'
11__usage__ = '%prog [options] nt.root'
12
13
14# Argument parsing
15from optparse import OptionParser
16parser = OptionParser(usage=__usage__, version=__version__)
17parser.add_option('-p', '--plot', dest='plot', default='', help='what to plot')
18parser.add_option('-c', '--comment', dest='comment', default=None, help='additional text (use semicolon to indicate line breaks)')
19parser.add_option('-m', '--more', dest='more', default='', help='more info (displayed beneath statistics box)')
20parser.add_option('-o', '--output', dest='output', default='.gif', help='comma-separated list of output files or formats (default: .gif')
21parser.add_option('', '--name', dest='name', default=None, help='base name for plots (default: ntuple name)')
22parser.add_option('-n', '--ntracks', dest='ntracks', type='int', default=5, help='Number of tracks/vtx (default: 4)')
23parser.add_option('', '--lbmin', dest='lbmin', type='int', default=-1, help='Minimum LB to consider')
24parser.add_option('', '--lbmax', dest='lbmax', type='int', default=9999999, help='Maximum LB to consider')
25parser.add_option('', '--xmin', dest='xmin', type='float', default=None, help='x axis minimum')
26parser.add_option('', '--xmax', dest='xmax', type='float', default=None, help='x axis maximum')
27parser.add_option('', '--ymin', dest='ymin', type='float', default=None, help='y axis minimum')
28parser.add_option('', '--ymax', dest='ymax', type='float', default=None, help='y axis maximum')
29parser.add_option('', '--passed', dest='passed', action='store_true', default=False, help='vertex must have passed athena selection')
30parser.add_option('', '--cuts', dest='cuts', default='', help='additional cuts (in addition to vertex type and what can be set with other options)')
31parser.add_option('', '--bins', dest='nbins', type='int', default=200, help='number of bins')
32parser.add_option('', '--fit', dest='fit', default='', help='fit histogram with function (e.g. gaus) (default: no fit)')
33parser.add_option('', '--logy', dest='logy', action='store_true', default=False, help='log scale')
34parser.add_option('', '--optstat', dest='optstat', default='emruo', help='default OptStat value (Default: emruo)')
35parser.add_option('', '--energy', dest='energy', action='store_true', default=False, help='add energy label')
36parser.add_option('', '--public', dest='public', action='store_true', default=False, help='use labelling for public plots')
37parser.add_option('', '--prelim', dest='prelim', action='store_true', default=False, help='Add ATLAS Preliminary to figure')
38parser.add_option('', '--approval', dest='approval', action='store_true', default=False, help='Label figure ATLAS for approval')
39parser.add_option('', '--published', dest='published', action='store_true', default=False, help='add ATLAS to figure')
40parser.add_option('', '--canvas', dest='canvas', default='default', help='canvas size: default, page, wide, extrawide or square')
41parser.add_option('', '--atlasx', dest='atlasx', type='float', default=None, help='x position for drawing ATLAS label')
42parser.add_option('', '--atlasy', dest='atlasy', type='float', default=None, help='y position for drawing ATLAS label')
43parser.add_option('', '--atlasdx', dest='atlasdx', type='float', default=None, help='x position offset for drawing Preliminary label')
44parser.add_option('-i', '--interactive', dest='interactive', action='store_true', default=False, help='interactive')
45parser.add_option('-b', '--batch', dest='batch', action='store_true', default=False, help='run in batch mode')
46(options,args) = parser.parse_args()
47if len(args) != 1:
48 parser.error('wrong number of command line arguments')
49ntFile = args[0]
50ntName = ntFile.split('/')[-1][:-5] if not options.name else options.name
51
52
53# Definition of generic plots
54plotDef = {
55 'x': {'code': 'hist', 'var': 'x', 'hname': 'pvX', 'xmin': -2, 'xmax': 2, 'units': '[mm]'},
56 'y': {'code': 'hist', 'var': 'y', 'hname': 'pvY', 'xmin': -2, 'xmax': 2, 'units': '[mm]'},
57 'z': {'code': 'hist', 'var': 'z', 'hname': 'pvZ', 'xmin': -500, 'xmax': 500, 'units': '[mm]'},
58 'errX': {'code': 'histErr', 'var': 'vxx', 'hname': 'errX', 'xmin': 0, 'xmax': 500, 'units': '[mm]'},
59 'errXComp': {'code': 'histErrComp', 'var': 'vxx', 'hname': 'errX', 'xmin': 0, 'xmax': 100, 'units': '[mm]'},
60 'errY': {'code': 'histErr', 'var': 'vyy', 'hname': 'errY', 'xmin': 0, 'xmax': 500, 'units': '[mm]'},
61 'errZ': {'code': 'histErr', 'var': 'vzz', 'hname': 'errZ', 'xmin': 0, 'xmax': 500, 'units': '[mm]'},
62 'ntracks': {'code': 'hist', 'var': 'nTracks', 'hname': 'pvNTracks', 'xmin': 0, 'xmax': 200, 'nbins': 200},
63 'xz': {'xmin': -250, 'xmax': 250, 'ymin': -1.5, 'ymax': 2.5},
64 'yz': {'xmin': -250, 'xmax': 250, 'ymin': -0.5, 'ymax': 3.5},
65 'yx': {'xmin': -1.4, 'xmax': 0.6, 'ymin': 0, 'ymax': 2}
66}
67def getPlotDef(what,property,default=''):
68 try:
69 return plotDef[what][property]
70 except:
71 return default
72
73# Plot defaults
74xmin = options.xmin if options.xmin else getPlotDef(options.plot,'xmin',-500)
75xmax = options.xmax if options.xmax else getPlotDef(options.plot,'xmax',+500)
76ymin = options.ymin if options.ymin else getPlotDef(options.plot,'ymin',-500)
77ymax = options.ymax if options.ymax else getPlotDef(options.plot,'ymax',+500)
78nbins = getPlotDef(options.plot,'nbins',options.nbins)
79
80# Graphics defaults
81if options.atlasx==None:
82 if options.published:
83 options.atlasx = 0.2
84 else:
85 options.atlasx = 0.2
86if options.atlasy==None:
87 options.atlasy = 0.86
88if options.atlasdx==None:
89 options.atlasdx = 0.115
90
91# Set cuts
92cuts = 'vType==1 && lb>=%i && lb<%i' % (options.lbmin,options.lbmax)
93if options.ntracks:
94 cuts += ' && nTracks>=%i' % options.ntracks
95if options.passed:
96 cuts += ' && passed==1'
97if options.cuts:
98 cuts += ' && %s' % options.cuts
99print()
100print ('Using cuts: ',cuts)
101print()
102
103
104# Reset DISPLAY if in batch
105import os
106if options.batch:
107 os.unsetenv('DISPLAY')
108
109
110# Import ROOT (do this only now to avoid conlicts w/OptionParser)
111import ROOT
112from InDetBeamSpotExample import ROOTUtils
113from InDetBeamSpotExample.Utils import getRunFromName
114# TODO: Switch to using COOLQuery from COOLUtils for LHC info
115from InDetBeamSpotExample.LHCInfoUtils import lhcFillData
116from InDetBeamSpotExample.LHCInfoUtils import lhcEnergyData
119ROOT.gStyle.SetPalette(1) # Better color scheme than default
120
121
123 if options.prelim:
124 ROOTUtils.atlasLabel(options.atlasx,options.atlasy,True,offset=options.atlasdx,energy=None)
125 if options.approval:
126 ROOTUtils.atlasLabel(options.atlasx,options.atlasy,False,offset=options.atlasdx,isForApproval=True,energy=None)
127 if options.published:
128 ROOTUtils.atlasLabel(options.atlasx,options.atlasy,False,offset=options.atlasdx,energy=None)
129
130 ROOTUtils.drawText(options.atlasx,0.77,0.06,runFillInfo,font=42)
131 comment = options.comment if options.comment is not None else '#geq %i tracks/vertex' % (options.ntracks)
132 ROOTUtils.drawText(options.atlasx,0.71,0.06,lhcEnergyInfo+comment,font=42)
133
134
135def drawInfo(c,h,more,x=0.6,size=0.04):
136 what = c.GetName()
137 info = 'Entries %5i' % h.GetEntries()
138 info += ';RMS %s %6.3g mm' % (what[0],h.GetRMS(2))
139 info += ';RMS %s %6.3g mm' % (what[1],h.GetRMS(1))
140 ROOTUtils.drawText(x,0.88,size,info,font=102)
141 if more:
142 ROOTUtils.drawText(x,0.73,size,more,font=102)
143
144
146
147 def hist(self):
148 hname = getPlotDef(options.plot,'hname','h')
149 var = getPlotDef(options.plot,'var')
150 units = getPlotDef(options.plot,'units','')
151 ROOT.gStyle.SetOptStat(options.optstat)
152 if options.fit:
153 ROOT.gStyle.SetOptFit(1111)
154 c = ROOTUtils.protect(ROOTUtils.MyCanvas(var,options.canvas))
155 c.SetRightMargin(0.14)
156 h = ROOTUtils.protect(ROOT.TH1F(hname,'Primary vertex: %s;Primary vertex %s %s' % (var,var,units),nbins,xmin,xmax))
157 nt.Draw('%s >> %s' % (var,hname),cuts)
158 if options.fit:
159 h.Fit(options.fit)
160 ROOTUtils.drawText(0.2,0.77,0.06,runFillInfo,font=42)
161 comment = options.comment if options.comment is not None else '#geq %i tracks/vertex;%s' % (options.ntracks,options.cuts)
162 ROOTUtils.drawText(0.2,0.71,0.06,lhcEnergyInfo+comment,font=42)
163 ROOT.gPad.SetLogy(options.logy)
164 ROOT.gPad.Update()
165 c.save()
166
167 def histErr(self):
168 hname = getPlotDef(options.plot,'hname','h')
169 var = getPlotDef(options.plot,'var')
170 vName = var[1:] # Ugly - should take from plotDef
171 ROOT.gStyle.SetOptStat(options.optstat)
172 ROOT.gStyle.SetOptFit(0)
173 c = ROOTUtils.protect(ROOTUtils.MyCanvas(var,options.canvas))
174 c.SetRightMargin(0.14)
175 h = ROOTUtils.protect(ROOT.TH1F(hname,'Primary vertex error: %s;Primary vertex error #sqrt{V_{%s}} (#mum);Number of vertices' % (hname,vName),
176 nbins,xmin,xmax))
177 nt.Draw('1e3*sqrt(%s) >> %s' % (var,hname),cuts)
178 ROOTUtils.drawText(0.45,0.86,0.06,'Primary Vertex Error #sqrt{V_{%s}}' % vName)
179 comment = options.comment if options.comment is not None else '#geq %i tracks/vertex;%s' % (options.ntracks,options.cuts)
180 ROOTUtils.drawText(0.45,0.8,0.06,lhcEnergyInfo+comment,font=42)
181 ROOT.gPad.SetLogy(options.logy)
182 ROOT.gPad.Update()
183 c.save()
184
185 def histErrComp(self):
186 hname = getPlotDef(options.plot,'hname','h')
187 var = getPlotDef(options.plot,'var')
188 vName = var[1:] # Ugly - should take from plotDef
189 ROOT.gStyle.SetOptStat(0)
190 ROOT.gStyle.SetOptFit(0)
191 c = ROOTUtils.protect(ROOTUtils.MyCanvas(var,options.canvas))
192 c.SetRightMargin(0.14)
193 h = ROOTUtils.protect(ROOT.TH1F(hname,'Vertex Error: %s;Vertex error #sqrt{V_{%s}} [#mum];Fraction of entries' % (hname,vName),
194 nbins,xmin,xmax))
195 nt.Draw('1e3*sqrt(%s) >> %s' % (var,hname),cuts)
196 norm = h.Integral()
197 h.Scale(1./norm)
198 pname = hname+'_pileup'
199 p = ROOTUtils.protect(ROOT.TH1F(pname,'Vertex Error: %s;Vertex error #sqrt{V_{%s}} [#mum];Fraction of entries' % (hname,vName),
200 nbins,xmin,xmax))
201 pcuts = cuts.replace('vType==1','vType==3')
202 print (pcuts)
203 nt.Draw('1e3*sqrt(%s) >> %s' % (var,pname),pcuts,"SAME")
204 pnorm = p.Integral()
205 p.Scale(1./pnorm)
206 p.SetLineColor(4)
207 p.SetLineStyle(2)
208 ROOTUtils.drawText(0.51,0.86,0.06,'Vertex error #sqrt{V_{%s}}' % vName)
209 comment = options.comment if options.comment is not None else '#geq %i tracks/vertex;%s' % (options.ntracks,options.cuts)
210 ROOTUtils.drawText(0.51,0.8,0.06,lhcEnergyInfo+comment,font=42)
211 legend = ROOT.TLegend(.50,.6,.85,.72)
212 legend.AddEntry(h,'Primary vertices','L')
213 legend.AddEntry(p,'Pileup vertices','L')
214 legend.SetFillColor(0)
215 legend.SetBorderSize(0)
216 legend.Draw()
217 ROOT.gPad.SetLogy(options.logy)
218 ROOT.gPad.Update()
219 c.save()
220
221 def xz(self):
222 #ROOT.gStyle.SetOptStat(1010)
223 ROOT.gStyle.SetOptStat(0)
224 c = ROOTUtils.protect(ROOTUtils.MyCanvas('xz',options.canvas))
225 c.SetRightMargin(0.14)
226 h = ROOTUtils.protect(ROOT.TH2F('pvXZ','Primary vertex: x vs z;Primary vertex z [mm];Primary vertex x [mm]',nbins,xmin,xmax,nbins,ymin,ymax))
227 nt.Draw('x:z >> pvXZ',cuts)
228 h.Draw('COLZ')
229 h.GetYaxis().SetTitleOffset(1.0)
230 drawLabels()
231 #ROOTUtils.moveStats(h,-0.16,-0.08)
232 drawInfo(c,h,options.more)
233 ROOT.gPad.Update()
234 c.save()
235
236 def yz(self):
237 ROOT.gStyle.SetOptStat(0)
238 c = ROOTUtils.protect(ROOTUtils.MyCanvas('yz',options.canvas))
239 c.SetRightMargin(0.14)
240 h = ROOTUtils.protect(ROOT.TH2F('pvYZ','Primary vertex: y vs z;Primary vertex z [mm];Primary vertex y [mm]',nbins,xmin,xmax,nbins,ymin,ymax))
241 nt.Draw('y:z >> pvYZ',cuts)
242 h.Draw('COLZ')
243 h.GetYaxis().SetTitleOffset(1.0)
244 drawLabels()
245 drawInfo(c,h,options.more)
246 ROOT.gPad.Update()
247 c.save()
248
249 def yx(self):
250 ROOT.gStyle.SetOptStat(0)
251 c = ROOTUtils.protect(ROOTUtils.MyCanvas('yx',options.canvas))
252 c.SetRightMargin(0.14)
253 h = ROOTUtils.protect(ROOT.TH2F('pvYX','Primary vertex: y vs x;Primary vertex x [mm];Primary vertex y [mm]',nbins,xmin,xmax,nbins,ymin,ymax))
254 nt.Draw('y:x >> pvYX',cuts)
255 h.Draw('COLZ')
256 h.GetYaxis().SetTitleOffset(1.0)
257 drawLabels()
258 drawInfo(c,h,options.more)
259 ROOT.gPad.Update()
260 c.save()
261
262
263# Start processing
264run = getRunFromName(ntFile)
265if options.public:
266 runFillInfo = 'Fill %s' % (lhcFillData.get(run,'') if lhcFillData.get(run,'') else '')
267else:
268 runFillInfo = 'Run %s (LHC Fill %s)' % (run,lhcFillData.get(run,'') if lhcFillData.get(run,'') else '')
269
270if options.energy:
271 lhcEnergyInfo = '#sqrt{s} = %s;' % lhcEnergyData.get(run,'') if lhcEnergyData.get(run,'') else ''
272else:
273 lhcEnergyInfo = ''
274
275f = ROOT.TFile(ntFile)
276if f.Get('Vertices'):
277 nt = f.Get('Vertices')
278elif f.Get('Beamspot/Vertices'):
279 nt = f.Get('Beamspot/Vertices')
280#nt = f.Get('Beamspot/Vertices')
281
282ROOTUtils.MyCanvas.saveAsList = options.output.split(',')
283ROOTUtils.MyCanvas.autoName = '%s-%%s%%s' % ntName
284
285plots = Plots('Primary Vertex Plots',['hist','histErr'])
286plots.plot(getPlotDef(options.plot,'code',options.plot))
287
288if options.interactive:
289 os.environ['PYTHONINSPECT'] = '1'
void print(char *figname, TCanvas *c1)
atlasLabel(x, y, isPreliminary=False, color=1, offset=0.115, isForApproval=False, energy=8, customstring="", size=0.05)
setStyle(style=None)
drawText(x=0.74, y=0.87, dy=0.06, text='', font=62, color=1, align=11, linesep=';')
getPlotDef(what, property, default='')
drawInfo(c, h, more, x=0.6, size=0.04)