5 from __future__
import print_function
8 Create beam spot primary vertex plots (e.g. for approved plots).
10 __author__ =
'Juerg Beringer'
12 __usage__ =
'%prog [options] nt.root'
16 from optparse
import OptionParser
17 parser = OptionParser(usage=__usage__, version=__version__)
18 parser.add_option(
'-p',
'--plot', dest=
'plot', default=
'', help=
'what to plot')
19 parser.add_option(
'-c',
'--comment', dest=
'comment', default=
None, help=
'additional text (use semicolon to indicate line breaks)')
20 parser.add_option(
'-m',
'--more', dest=
'more', default=
'', help=
'more info (displayed beneath statistics box)')
21 parser.add_option(
'-o',
'--output', dest=
'output', default=
'.gif', help=
'comma-separated list of output files or formats (default: .gif')
22 parser.add_option(
'',
'--name', dest=
'name', default=
None, help=
'base name for plots (default: ntuple name)')
23 parser.add_option(
'-n',
'--ntracks', dest=
'ntracks', type=
'int', default=5, help=
'Number of tracks/vtx (default: 4)')
24 parser.add_option(
'',
'--lbmin', dest=
'lbmin', type=
'int', default=-1, help=
'Minimum LB to consider')
25 parser.add_option(
'',
'--lbmax', dest=
'lbmax', type=
'int', default=9999999, help=
'Maximum LB to consider')
26 parser.add_option(
'',
'--xmin', dest=
'xmin', type=
'float', default=
None, help=
'x axis minimum')
27 parser.add_option(
'',
'--xmax', dest=
'xmax', type=
'float', default=
None, help=
'x axis maximum')
28 parser.add_option(
'',
'--ymin', dest=
'ymin', type=
'float', default=
None, help=
'y axis minimum')
29 parser.add_option(
'',
'--ymax', dest=
'ymax', type=
'float', default=
None, help=
'y axis maximum')
30 parser.add_option(
'',
'--passed', dest=
'passed', action=
'store_true', default=
False, help=
'vertex must have passed athena selection')
31 parser.add_option(
'',
'--cuts', dest=
'cuts', default=
'', help=
'additional cuts (in addition to vertex type and what can be set with other options)')
32 parser.add_option(
'',
'--bins', dest=
'nbins', type=
'int', default=200, help=
'number of bins')
33 parser.add_option(
'',
'--fit', dest=
'fit', default=
'', help=
'fit histogram with function (e.g. gaus) (default: no fit)')
34 parser.add_option(
'',
'--logy', dest=
'logy', action=
'store_true', default=
False, help=
'log scale')
35 parser.add_option(
'',
'--optstat', dest=
'optstat', default=
'emruo', help=
'default OptStat value (Default: emruo)')
36 parser.add_option(
'',
'--energy', dest=
'energy', action=
'store_true', default=
False, help=
'add energy label')
37 parser.add_option(
'',
'--public', dest=
'public', action=
'store_true', default=
False, help=
'use labelling for public plots')
38 parser.add_option(
'',
'--prelim', dest=
'prelim', action=
'store_true', default=
False, help=
'Add ATLAS Preliminary to figure')
39 parser.add_option(
'',
'--approval', dest=
'approval', action=
'store_true', default=
False, help=
'Label figure ATLAS for approval')
40 parser.add_option(
'',
'--published', dest=
'published', action=
'store_true', default=
False, help=
'add ATLAS to figure')
41 parser.add_option(
'',
'--canvas', dest=
'canvas', default=
'default', help=
'canvas size: default, page, wide, extrawide or square')
42 parser.add_option(
'',
'--atlasx', dest=
'atlasx', type=
'float', default=
None, help=
'x position for drawing ATLAS label')
43 parser.add_option(
'',
'--atlasy', dest=
'atlasy', type=
'float', default=
None, help=
'y position for drawing ATLAS label')
44 parser.add_option(
'',
'--atlasdx', dest=
'atlasdx', type=
'float', default=
None, help=
'x position offset for drawing Preliminary label')
45 parser.add_option(
'-i',
'--interactive', dest=
'interactive', action=
'store_true', default=
False, help=
'interactive')
46 parser.add_option(
'-b',
'--batch', dest=
'batch', action=
'store_true', default=
False, help=
'run in batch mode')
47 (options,args) = parser.parse_args()
49 parser.error(
'wrong number of command line arguments')
51 ntName = ntFile.split(
'/')[-1][:-5]
if not options.name
else options.name
56 'x': {
'code':
'hist',
'var':
'x',
'hname':
'pvX',
'xmin': -2,
'xmax': 2,
'units':
'[mm]'},
57 'y': {
'code':
'hist',
'var':
'y',
'hname':
'pvY',
'xmin': -2,
'xmax': 2,
'units':
'[mm]'},
58 'z': {
'code':
'hist',
'var':
'z',
'hname':
'pvZ',
'xmin': -500,
'xmax': 500,
'units':
'[mm]'},
59 'errX': {
'code':
'histErr',
'var':
'vxx',
'hname':
'errX',
'xmin': 0,
'xmax': 500,
'units':
'[mm]'},
60 'errXComp': {
'code':
'histErrComp',
'var':
'vxx',
'hname':
'errX',
'xmin': 0,
'xmax': 100,
'units':
'[mm]'},
61 'errY': {
'code':
'histErr',
'var':
'vyy',
'hname':
'errY',
'xmin': 0,
'xmax': 500,
'units':
'[mm]'},
62 'errZ': {
'code':
'histErr',
'var':
'vzz',
'hname':
'errZ',
'xmin': 0,
'xmax': 500,
'units':
'[mm]'},
63 'ntracks': {
'code':
'hist',
'var':
'nTracks',
'hname':
'pvNTracks',
'xmin': 0,
'xmax': 200,
'nbins': 200},
64 'xz': {
'xmin': -250,
'xmax': 250,
'ymin': -1.5,
'ymax': 2.5},
65 'yz': {
'xmin': -250,
'xmax': 250,
'ymin': -0.5,
'ymax': 3.5},
66 'yx': {
'xmin': -1.4,
'xmax': 0.6,
'ymin': 0,
'ymax': 2}
70 return plotDef[what][property]
75 xmin = options.xmin
if options.xmin
else getPlotDef(options.plot,
'xmin',-500)
76 xmax = options.xmax
if options.xmax
else getPlotDef(options.plot,
'xmax',+500)
77 ymin = options.ymin
if options.ymin
else getPlotDef(options.plot,
'ymin',-500)
78 ymax = options.ymax
if options.ymax
else getPlotDef(options.plot,
'ymax',+500)
82 if options.atlasx==
None:
87 if options.atlasy==
None:
89 if options.atlasdx==
None:
90 options.atlasdx = 0.115
93 cuts =
'vType==1 && lb>=%i && lb<%i' % (options.lbmin,options.lbmax)
95 cuts +=
' && nTracks>=%i' % options.ntracks
97 cuts +=
' && passed==1'
99 cuts +=
' && %s' % options.cuts
101 print (
'Using cuts: ',cuts)
108 os.unsetenv(
'DISPLAY')
113 from InDetBeamSpotExample
import ROOTUtils
114 from InDetBeamSpotExample.Utils
import getRunFromName
116 from InDetBeamSpotExample.LHCInfoUtils
import lhcFillData
117 from InDetBeamSpotExample.LHCInfoUtils
import lhcEnergyData
120 ROOT.gStyle.SetPalette(1)
127 ROOTUtils.atlasLabel(options.atlasx,options.atlasy,
False,offset=options.atlasdx,isForApproval=
True,energy=
None)
128 if options.published:
132 comment = options.comment
if options.comment
is not None else '#geq %i tracks/vertex' % (options.ntracks)
138 info =
'Entries %5i' % h.GetEntries()
139 info +=
';RMS %s %6.3g mm' % (what[0],h.GetRMS(2))
140 info +=
';RMS %s %6.3g mm' % (what[1],h.GetRMS(1))
152 ROOT.gStyle.SetOptStat(options.optstat)
154 ROOT.gStyle.SetOptFit(1111)
156 c.SetRightMargin(0.14)
157 h =
ROOTUtils.protect(ROOT.TH1F(hname,
'Primary vertex: %s;Primary vertex %s %s' % (var,var,units),nbins,xmin,xmax))
158 nt.Draw(
'%s >> %s' % (var,hname),cuts)
162 comment = options.comment
if options.comment
is not None else '#geq %i tracks/vertex;%s' % (options.ntracks,options.cuts)
164 ROOT.gPad.SetLogy(options.logy)
172 ROOT.gStyle.SetOptStat(options.optstat)
173 ROOT.gStyle.SetOptFit(0)
175 c.SetRightMargin(0.14)
176 h =
ROOTUtils.protect(ROOT.TH1F(hname,
'Primary vertex error: %s;Primary vertex error #sqrt{V_{%s}} (#mum);Number of vertices' % (hname,vName),
178 nt.Draw(
'1e3*sqrt(%s) >> %s' % (var,hname),cuts)
180 comment = options.comment
if options.comment
is not None else '#geq %i tracks/vertex;%s' % (options.ntracks,options.cuts)
182 ROOT.gPad.SetLogy(options.logy)
190 ROOT.gStyle.SetOptStat(0)
191 ROOT.gStyle.SetOptFit(0)
193 c.SetRightMargin(0.14)
194 h =
ROOTUtils.protect(ROOT.TH1F(hname,
'Vertex Error: %s;Vertex error #sqrt{V_{%s}} [#mum];Fraction of entries' % (hname,vName),
196 nt.Draw(
'1e3*sqrt(%s) >> %s' % (var,hname),cuts)
199 pname = hname+
'_pileup'
200 p =
ROOTUtils.protect(ROOT.TH1F(pname,
'Vertex Error: %s;Vertex error #sqrt{V_{%s}} [#mum];Fraction of entries' % (hname,vName),
202 pcuts = cuts.replace(
'vType==1',
'vType==3')
204 nt.Draw(
'1e3*sqrt(%s) >> %s' % (var,pname),pcuts,
"SAME")
210 comment = options.comment
if options.comment
is not None else '#geq %i tracks/vertex;%s' % (options.ntracks,options.cuts)
212 legend = ROOT.TLegend(.50,.6,.85,.72)
213 legend.AddEntry(h,
'Primary vertices',
'L')
214 legend.AddEntry(p,
'Pileup vertices',
'L')
215 legend.SetFillColor(0)
216 legend.SetBorderSize(0)
218 ROOT.gPad.SetLogy(options.logy)
224 ROOT.gStyle.SetOptStat(0)
226 c.SetRightMargin(0.14)
227 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))
228 nt.Draw(
'x:z >> pvXZ',cuts)
230 h.GetYaxis().SetTitleOffset(1.0)
238 ROOT.gStyle.SetOptStat(0)
240 c.SetRightMargin(0.14)
241 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))
242 nt.Draw(
'y:z >> pvYZ',cuts)
244 h.GetYaxis().SetTitleOffset(1.0)
251 ROOT.gStyle.SetOptStat(0)
253 c.SetRightMargin(0.14)
254 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))
255 nt.Draw(
'y:x >> pvYX',cuts)
257 h.GetYaxis().SetTitleOffset(1.0)
267 runFillInfo =
'Fill %s' % (lhcFillData.get(run,
'')
if lhcFillData.get(run,
'')
else '')
269 runFillInfo =
'Run %s (LHC Fill %s)' % (run,lhcFillData.get(run,
'')
if lhcFillData.get(run,
'')
else '')
272 lhcEnergyInfo =
'#sqrt{s} = %s;' % lhcEnergyData.get(run,
'')
if lhcEnergyData.get(run,
'')
else ''
276 f = ROOT.TFile(ntFile)
277 if f.Get(
'Vertices'):
278 nt = f.Get(
'Vertices')
279 elif f.Get(
'Beamspot/Vertices'):
280 nt = f.Get(
'Beamspot/Vertices')
283 ROOTUtils.MyCanvas.saveAsList = options.output.split(
',')
284 ROOTUtils.MyCanvas.autoName =
'%s-%%s%%s' % ntName
286 plots =
Plots(
'Primary Vertex Plots',[
'hist',
'histErr'])
287 plots.plot(
getPlotDef(options.plot,
'code',options.plot))
289 if options.interactive:
290 os.environ[
'PYTHONINSPECT'] =
'1'