ATLAS Offline Software
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 """
7 Create 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
15 from optparse import OptionParser
16 parser = OptionParser(usage=__usage__, version=__version__)
17 parser.add_option('-p', '--plot', dest='plot', default='', help='what to plot')
18 parser.add_option('-c', '--comment', dest='comment', default=None, help='additional text (use semicolon to indicate line breaks)')
19 parser.add_option('-m', '--more', dest='more', default='', help='more info (displayed beneath statistics box)')
20 parser.add_option('-o', '--output', dest='output', default='.gif', help='comma-separated list of output files or formats (default: .gif')
21 parser.add_option('', '--name', dest='name', default=None, help='base name for plots (default: ntuple name)')
22 parser.add_option('-n', '--ntracks', dest='ntracks', type='int', default=5, help='Number of tracks/vtx (default: 4)')
23 parser.add_option('', '--lbmin', dest='lbmin', type='int', default=-1, help='Minimum LB to consider')
24 parser.add_option('', '--lbmax', dest='lbmax', type='int', default=9999999, help='Maximum LB to consider')
25 parser.add_option('', '--xmin', dest='xmin', type='float', default=None, help='x axis minimum')
26 parser.add_option('', '--xmax', dest='xmax', type='float', default=None, help='x axis maximum')
27 parser.add_option('', '--ymin', dest='ymin', type='float', default=None, help='y axis minimum')
28 parser.add_option('', '--ymax', dest='ymax', type='float', default=None, help='y axis maximum')
29 parser.add_option('', '--passed', dest='passed', action='store_true', default=False, help='vertex must have passed athena selection')
30 parser.add_option('', '--cuts', dest='cuts', default='', help='additional cuts (in addition to vertex type and what can be set with other options)')
31 parser.add_option('', '--bins', dest='nbins', type='int', default=200, help='number of bins')
32 parser.add_option('', '--fit', dest='fit', default='', help='fit histogram with function (e.g. gaus) (default: no fit)')
33 parser.add_option('', '--logy', dest='logy', action='store_true', default=False, help='log scale')
34 parser.add_option('', '--optstat', dest='optstat', default='emruo', help='default OptStat value (Default: emruo)')
35 parser.add_option('', '--energy', dest='energy', action='store_true', default=False, help='add energy label')
36 parser.add_option('', '--public', dest='public', action='store_true', default=False, help='use labelling for public plots')
37 parser.add_option('', '--prelim', dest='prelim', action='store_true', default=False, help='Add ATLAS Preliminary to figure')
38 parser.add_option('', '--approval', dest='approval', action='store_true', default=False, help='Label figure ATLAS for approval')
39 parser.add_option('', '--published', dest='published', action='store_true', default=False, help='add ATLAS to figure')
40 parser.add_option('', '--canvas', dest='canvas', default='default', help='canvas size: default, page, wide, extrawide or square')
41 parser.add_option('', '--atlasx', dest='atlasx', type='float', default=None, help='x position for drawing ATLAS label')
42 parser.add_option('', '--atlasy', dest='atlasy', type='float', default=None, help='y position for drawing ATLAS label')
43 parser.add_option('', '--atlasdx', dest='atlasdx', type='float', default=None, help='x position offset for drawing Preliminary label')
44 parser.add_option('-i', '--interactive', dest='interactive', action='store_true', default=False, help='interactive')
45 parser.add_option('-b', '--batch', dest='batch', action='store_true', default=False, help='run in batch mode')
46 (options,args) = parser.parse_args()
47 if len(args) != 1:
48  parser.error('wrong number of command line arguments')
49 ntFile = args[0]
50 ntName = ntFile.split('/')[-1][:-5] if not options.name else options.name
51 
52 
53 # Definition of generic plots
54 plotDef = {
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 }
67 def getPlotDef(what,property,default=''):
68  try:
69  return plotDef[what][property]
70  except:
71  return default
72 
73 # Plot defaults
74 xmin = options.xmin if options.xmin else getPlotDef(options.plot,'xmin',-500)
75 xmax = options.xmax if options.xmax else getPlotDef(options.plot,'xmax',+500)
76 ymin = options.ymin if options.ymin else getPlotDef(options.plot,'ymin',-500)
77 ymax = options.ymax if options.ymax else getPlotDef(options.plot,'ymax',+500)
78 nbins = getPlotDef(options.plot,'nbins',options.nbins)
79 
80 # Graphics defaults
81 if options.atlasx==None:
82  if options.published:
83  options.atlasx = 0.2
84  else:
85  options.atlasx = 0.2
86 if options.atlasy==None:
87  options.atlasy = 0.86
88 if options.atlasdx==None:
89  options.atlasdx = 0.115
90 
91 # Set cuts
92 cuts = 'vType==1 && lb>=%i && lb<%i' % (options.lbmin,options.lbmax)
93 if options.ntracks:
94  cuts += ' && nTracks>=%i' % options.ntracks
95 if options.passed:
96  cuts += ' && passed==1'
97 if options.cuts:
98  cuts += ' && %s' % options.cuts
99 print()
100 print ('Using cuts: ',cuts)
101 print()
102 
103 
104 # Reset DISPLAY if in batch
105 import os
106 if options.batch:
107  os.unsetenv('DISPLAY')
108 
109 
110 # Import ROOT (do this only now to avoid conlicts w/OptionParser)
111 import ROOT
112 from InDetBeamSpotExample import ROOTUtils
113 from InDetBeamSpotExample.Utils import getRunFromName
114 # TODO: Switch to using COOLQuery from COOLUtils for LHC info
115 from InDetBeamSpotExample.LHCInfoUtils import lhcFillData
116 from InDetBeamSpotExample.LHCInfoUtils import lhcEnergyData
119 ROOT.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 
135 def 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
264 run = getRunFromName(ntFile)
265 if options.public:
266  runFillInfo = 'Fill %s' % (lhcFillData.get(run,'') if lhcFillData.get(run,'') else '')
267 else:
268  runFillInfo = 'Run %s (LHC Fill %s)' % (run,lhcFillData.get(run,'') if lhcFillData.get(run,'') else '')
269 
270 if options.energy:
271  lhcEnergyInfo = '#sqrt{s} = %s;' % lhcEnergyData.get(run,'') if lhcEnergyData.get(run,'') else ''
272 else:
273  lhcEnergyInfo = ''
274 
275 f = ROOT.TFile(ntFile)
276 if f.Get('Vertices'):
277  nt = f.Get('Vertices')
278 elif f.Get('Beamspot/Vertices'):
279  nt = f.Get('Beamspot/Vertices')
280 #nt = f.Get('Beamspot/Vertices')
281 
282 ROOTUtils.MyCanvas.saveAsList = options.output.split(',')
283 ROOTUtils.MyCanvas.autoName = '%s-%%s%%s' % ntName
284 
285 plots = Plots('Primary Vertex Plots',['hist','histErr'])
286 plots.plot(getPlotDef(options.plot,'code',options.plot))
287 
288 if options.interactive:
289  os.environ['PYTHONINSPECT'] = '1'
ROOTUtils.drawText
def drawText(x=0.74, y=0.87, dy=0.06, text='', font=62, color=1, align=11, linesep=';')
Definition: roofit/ROOTUtils.py:243
BeamSpotData
plotBeamSpotVert.Plots.xz
def xz(self)
Definition: plotBeamSpotVert.py:221
plotBeamSpotVert.drawInfo
def drawInfo(c, h, more, x=0.6, size=0.04)
Definition: plotBeamSpotVert.py:135
plotBeamSpotVert.Plots.histErr
def histErr(self)
Definition: plotBeamSpotVert.py:167
ROOTUtils.MyCanvas
Definition: roofit/ROOTUtils.py:40
ROOTUtils.setStyle
def setStyle(style=None)
Definition: roofit/ROOTUtils.py:413
ROOTUtils.protect
def protect(obj)
Definition: roofit/ROOTUtils.py:17
python.Utils.getRunFromName
def getRunFromName(name, default='', asInt=False)
Definition: InnerDetector/InDetExample/InDetBeamSpotExample/python/Utils.py:13
ROOTUtils.atlasLabel
def atlasLabel(x, y, isPreliminary=False, color=1, offset=0.115, isForApproval=False, energy=8, customstring="", size=0.05)
Definition: roofit/ROOTUtils.py:303
plotBeamSpotVert.Plots.histErrComp
def histErrComp(self)
Definition: plotBeamSpotVert.py:185
plotBeamSpotVert.Plots
Definition: plotBeamSpotVert.py:145
plotBeamSpotVert.getPlotDef
def getPlotDef(what, property, default='')
Definition: plotBeamSpotVert.py:67
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
plotBeamSpotVert.Plots.hist
def hist(self)
Definition: plotBeamSpotVert.py:147
ROOTUtils.PlotLibrary
Definition: roofit/ROOTUtils.py:77
plotBeamSpotVert.Plots.yx
def yx(self)
Definition: plotBeamSpotVert.py:249
plotBeamSpotVert.drawLabels
def drawLabels()
Definition: plotBeamSpotVert.py:122
plotBeamSpotVert.Plots.yz
def yz(self)
Definition: plotBeamSpotVert.py:236