ATLAS Offline Software
visualize.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
4 
5 import argparse
6 from TrigConfMuctpi.XMLReader import MioctGeometryXMLReader
7 import math
8 
9 from ROOT import gROOT, gStyle, TH2F, TCanvas, TBox, TLegend, TArc, TLatex
10 
11 global box, c, h, leg
12 
13 colorMap = [ 1, 13,
14  8, 3,
15  5,801,
16  9, 4,
17  50, 2,
18  11, 7,
19  30, 51,
20  6, 38 ]
21 
22 colorMap2 = [
23  1, 8, 801, 9, 50, 4,
24  2, 11, 7, 30, 51, 6, 38 ]
25 fillStyleMap2 = [ 3004, 3012, 3005, 3010 ]
26 
27 
28 drawOrder = [ 0, 8,
29  7, 15,
30  6, 14,
31  5, 13,
32  4, 12,
33  3, 11,
34  2, 10,
35  1, 9 ]
36 
37 
38 def readXML(filename):
39  geom = MioctGeometryXMLReader(filename)
40  #print [str(m) for m in geom.getMIOCTs()]
41  return geom
42 
43 
44 def drawROIGeometry(geometry, is2016):
45 
46  outfn = "ROILayout%s.pdf" % ("2016" if is2016 else "2015")
47 
48  global box, c, h, leg
49 
50  gROOT.Reset()
51  gStyle.SetOptStat(0)
52 
53  c = TCanvas('c',"MuCTPi Geometry %s" % "2016" if is2016 else "2015",1400,950)
54  c.Draw()
55 
56  #h = TH2F("h","Muon Geometry",10,-2.6,2.6,10,-6.4,6.4)
57  h = TH2F("h","Muon Geometry %s" % "2016" if is2016 else "2015",10,-2.6,2.6,10,-0.15,6.4)
58  h.SetXTitle("#eta")
59  h.SetYTitle("#phi")
60  h.Draw()
61 
62  box = TBox()
63  box.SetFillStyle(0)
64  box.SetLineColor(3)
65  box.SetLineColor(3)
66 
67  text = TLatex()
68  text.SetTextSize(0.005)
69  text.SetTextFont(42)
70  text.SetTextAlign(22)
71 
72  secLabel = TLatex()
73  secLabel.SetTextSize(0.008)
74  secLabel.SetTextFont(42)
75  secLabel.SetTextAlign(22)
76 
77  leg = TLegend(0.7,0.1,0.9,0.4)
78  leg.SetEntrySeparation(0.05)
79  leg.SetNColumns(2)
80 
81  #for MIOCT in geometry.getMIOCTs():
82  for colorIndex,MioctID in enumerate(drawOrder):
83  MIOCT = geometry.getMIOCT(MioctID)
84  firstBox = True
85  color = colorMap[colorIndex % len(colorMap)]
86  #print "Using color ",color
87  box.SetLineColor(color)
88  box.SetLineWidth(1)
89 
90  for Sector in MIOCT.Sectors:
91  ymin = 10
92  ymax = -10
93  for ROI in Sector.ROIs:
94  c1_x = float(ROI["etamin"])
95  c1_y = float(ROI["phimin"])
96  c2_x = float(ROI["etamax"])
97  c2_y = float(ROI["phimax"])
98  ymin = min(ymin,c1_y)
99  ymax = max(ymax,c2_y)
100  #print "eta [%f - %f], phi [%f - %f]" % (c1_x,c2_x,c1_y,c2_y)
101  b = box.DrawBox(c1_x,c1_y,c2_x,c2_y)
102  text.DrawText( (c1_x + c2_x)/2, (c1_y + c2_y)/2 ,ROI["roiid"])
103  if firstBox:
104  firstBox = False
105  leg.AddEntry(b,"Slot %s" % MIOCT["slot"],"l")
106  if Sector["name"].startswith("B"):
107  if int(Sector["name"][1:])<32:
108  xpos = -0.02
109  ypos = (ymin+ymax)/2 - 0.05
110  else:
111  xpos = 0.02
112  ypos = (ymin+ymax)/2 + 0.03
113  secLabel.DrawText(xpos,ypos,Sector["name"])
114 
115  leg.Draw()
116 
117  c.Update()
118  c.SaveAs(outfn)
119 
120 
121 
122 def drawTopoGeometry(geometry, is2016):
123 
124  outfn = "TopoLayout%s.pdf" % ("2016" if is2016 else "2015")
125 
126  global box, c, h
127 
128  gROOT.Reset()
129  gStyle.SetOptStat(0)
130 
131  c = TCanvas('c',"MuCTPi to Topo Geometry",1400,950)
132  c.Draw()
133 
134  h = TH2F("h","Muon Topo Geometry %s" % "2016" if is2016 else "2015",10,-2.6,2.6,10,-0.15,6.4)
135  h.SetXTitle("#eta")
136  h.SetYTitle("#phi")
137  h.Draw()
138 
139  box = TBox()
140  box.SetFillStyle(0)
141  box.SetLineColor(3)
142 
143  circle = TArc()
144 
145  for colorIndex,MioctID in enumerate(drawOrder):
146  MIOCT = geometry.getMIOCT(MioctID)
147  color = colorMap[colorIndex % len(colorMap)]
148  box.SetLineColor(color)
149  box.SetFillColor(color)
150 
151  circle.SetLineColor(color)
152  circle.SetFillColor(color)
153 
154  fillStyle = 3004
155  for cellIdx,TopoCell in enumerate(MIOCT.Decode.TopoCells):
156  # corner 1
157  c1_x = float(TopoCell["etamin"])
158  c1_y = float(TopoCell["phimin"])
159  # corner 2
160  c2_x = float(TopoCell["etamax"])
161  c2_y = float(TopoCell["phimax"])
162  # center rounded
163  c_x = float(TopoCell["ieta"])
164  c_y = float(TopoCell["iphi"])
165 
166  #print "cell %i : eta [%f - %f], phi [%f - %f]" % (cellIdx, c1_x, c2_x, c1_y, c2_y)
167 
168  if fillStyle==3004:
169  fillStyle = 3012
170  else:
171  fillStyle = 3004
172  box.SetFillStyle(fillStyle)
173  box.DrawBox(c1_x,c1_y,c2_x,c2_y)
174  box.SetFillStyle(0)
175  box.DrawBox(c1_x,c1_y,c2_x,c2_y)
176 
177  circle.DrawArc(c_x/10.,c_y/10.,0.02)
178 
179  c.Update()
180  c.SaveAs(outfn)
181 
182 
183 
184 ETACODE=0
185 PHICODE=1
186 IETA=2
187 IPHI=3
188 
189 def drawTopoGeometryEtaPhi(geometry, colorBy, is2016):
190 
191  if colorBy<ETACODE or colorBy>IPHI:
192  return
193 
194  global box, c, h, leg
195 
196  gROOT.Reset()
197  gStyle.SetOptStat(0)
198 
199  c = TCanvas('c',"MuCTPi to Topo Geometry",1400,950)
200  c.Draw()
201 
202  h = TH2F("h","Muon Topo Geometry %i" % (2016 if is2016 else 2015),10,-2.6,2.6,10,-0.15,6.4)
203  h.SetXTitle("#eta")
204  h.SetYTitle("#phi")
205  h.Draw()
206 
207  box = TBox()
208  box.SetFillStyle(0)
209  box.SetLineColor(3)
210 
211  circle = TArc()
212 
213  if colorBy==IPHI:
214  leg = TLegend(0.9,0.1,0.98,0.9)
215  else:
216  leg = TLegend(0.8,0.1,0.9,0.35)
217  #leg.SetEntrySeparation(0.05)
218  #leg.SetNColumns(2)
219 
220 
221  codeInLegend = []
222  for MioctID in drawOrder:
223  #for MioctID in [3,4,5,6,7]:
224  MIOCT = geometry.getMIOCT(MioctID)
225 
226  fillStyle = 3004
227  for cellIdx,TopoCell in enumerate(MIOCT.Decode.TopoCells):
228 
229  if colorBy==ETACODE:
230  code = int(TopoCell["etacode"],16)
231  elif colorBy==PHICODE:
232  code = int(TopoCell["phicode"],16)
233  elif colorBy==IETA:
234  code = abs(int(TopoCell["ieta"]))
235  elif colorBy==IPHI:
236  code = int(TopoCell["iphi"])
237  else:
238  raise RuntimeError("Don't know how to color the eta-phi map (%r)" % colorBy)
239  color = colorMap2[code % len(colorMap2)]
240  fillStyle = fillStyleMap2[code % 4]
241  box.SetLineColor(color)
242  box.SetFillColor(color)
243 
244  circle.SetLineColor(color)
245  circle.SetFillColor(color)
246 
247  # corner 1
248  c1_x = float(TopoCell["etamin"])
249  c1_y = float(TopoCell["phimin"])
250  # corner 2
251  c2_x = float(TopoCell["etamax"])
252  c2_y = float(TopoCell["phimax"])
253  # center
254  c_x = float(TopoCell["ieta"])
255  c_y = float(TopoCell["iphi"])
256 
257  #if code>63:
258  # continue
259  #print "cell %i : eta [%f - %f], phi [%f - %f]" % (cellIdx, c1_x, c2_x, c1_y, c2_y)
260 
261  box.SetFillStyle(fillStyle)
262  b = box.DrawBox(c1_x,c1_y,c2_x,c2_y)
263  box.SetFillStyle(0)
264  box.DrawBox(c1_x,c1_y,c2_x,c2_y)
265 
266  circle.DrawArc(c_x/10.,c_y/10.,0.02)
267 
268 
269  if code not in codeInLegend:
270  codeInLegend += [code]
271  if colorBy==ETACODE:
272  leg.AddEntry(b,"etacode %i" % code,"lf")
273  elif colorBy==PHICODE:
274  leg.AddEntry(b,"phicode %i" % code,"f")
275  elif colorBy==IETA:
276  leg.AddEntry(b,"|ieta| %i" % code,"f")
277  elif colorBy==IPHI:
278  leg.AddEntry(b,"iphi %i" % code,"f")
279 
280  leg.Draw()
281 
282  c.Update()
283  if colorBy==ETACODE:
284  ext = "EtaCode"
285  elif colorBy==PHICODE:
286  ext = "PhiCode"
287  elif colorBy==IETA:
288  ext = "Eta"
289  elif colorBy==IPHI:
290  ext = "Phi"
291 
292 
293  c.SaveAs("TopoLayout%s%s.pdf" % ("2016" if is2016 else "2015", ext))
294 
295 
296 
297 def drawCodeValues(geometry, is2016):
298 
299  global h,c
300 
301  gROOT.Reset()
302  gStyle.SetOptStat(0)
303 
304  c = TCanvas('c',"Topo encoding",1400,950)
305  c.Draw()
306 
307  h = TH2F("h","Muon Topo encoding %i" % (2016 if is2016 else 2015),8,0,8,8,0,8)
308  h.SetXTitle("#eta")
309  h.SetYTitle("#phi")
310  h.Draw()
311 
312  for MioctID in drawOrder:
313  #for MioctID in [3,4,5,6,7]:
314  MIOCT = geometry.getMIOCT(MioctID)
315  for tc in MIOCT.Decode.TopoCells:
316  h.Fill(int(tc['etacode'],16),int(tc['phicode'],16))
317 
318  h.Draw("colz")
319  c.Update()
320  c.SaveAs("TopoCodes%s.pdf" % "2016" if is2016 else "2015")
321 
322 
323 def drawIValues(geometry, is2016):
324 
325  global h,c
326 
327  gROOT.Reset()
328  gStyle.SetOptStat(0)
329 
330  c = TCanvas('c',"Topo encoding",1400,950)
331  c.Draw()
332 
333  h = TH2F("h","Muon Topo encoding %i" % (2016 if is2016 else 2015),48,-24,24,64,0,64)
334  h.SetXTitle("#eta_{index}")
335  h.SetYTitle("#phi_{index}")
336  h.Draw()
337 
338  for MioctID in drawOrder:
339  MIOCT = geometry.getMIOCT(MioctID)
340  for tc in MIOCT.Decode.TopoCells:
341  h.Fill(int(tc['ieta'])+0.5,int(tc['iphi'])+0.5)
342 
343  h.Draw("colz")
344  c.Update()
345  c.SaveAs("TopoCellCenters%s.pdf" % "2016" if is2016 else "2015")
346 
347 
348 
349 def drawRoiDeltaR(geometry, is2016):
350 
351  def getTopoCell(mioct, etacode, phicode):
352  global mioctTCmap
353  mioctTCmap = {}
354  mioctid = mioct['id']
355  if mioctid not in mioctTCmap:
356  d = {}
357  for tc in mioct.Decode.TopoCells:
358  key = ( int(tc['etacode'],16), int(tc['phicode'],16))
359  d[key] = tc
360  mioctTCmap[mioctid] = d
361  return mioctTCmap[mioctid][(etacode,phicode)]
362 
363  global c,hB,hEC,hFW
364 
365  gROOT.Reset()
366  gStyle.SetOptStat(0)
367 
368  c = TCanvas('c',"Topo encoding",1400,950)
369  c.Divide(2,2)
370  c.Draw()
371 
372  hB = TH2F("hb","Delta R between ROI and TopoCell %i in Barrel" % (2016 if is2016 else 2015),64,0,64,32,0,32)
373  hB.SetXTitle("SL")
374  hB.SetYTitle("ROI ID")
375 
376  hEC = TH2F("hec","Delta R between ROI and TopoCell %i in Endcap" % (2016 if is2016 else 2015),96,0,96,148,0,148)
377  hEC.SetXTitle("SL")
378  hEC.SetYTitle("ROI ID")
379 
380  hFW = TH2F("hfw","Delta R between ROI and TopoCell %i in Forward" % (2016 if is2016 else 2015),48,0,48,64,0,64)
381  hFW.SetXTitle("SL")
382  hFW.SetYTitle("ROI ID")
383 
384  for MioctID in drawOrder:
385  mioct = geometry.getMIOCT(MioctID)
386 
387  for sector in mioct.Sectors:
388  for roi in sector.ROIs:
389  tc = getTopoCell(mioct, int(roi['etacode'],16), int(roi['phicode'],16))
390  deltaEta = float(roi['eta'])-float(tc['ieta'])/10.
391  deltaPhi = float(roi['phi'])-float(tc['iphi'])/10.
392  if deltaPhi>math.pi:
393  deltaPhi -= 2*math.pi # some topocells are made up from ROIs that are on both sides of 0
394  deltaR = math.sqrt(deltaEta*deltaEta+deltaPhi*deltaPhi)
395  sectorid = int(sector['name'].lstrip('ABCDEF'))
396  roiid = int(roi['roiid'])
397  if sector['name'].startswith('B'):
398  hB.Fill(sectorid,roiid,deltaR)
399  elif sector['name'].startswith('E'):
400  if sector['name'].startswith('EA'):
401  sectorid += 48
402  hEC.Fill(sectorid,roiid,deltaR)
403  elif sector['name'].startswith('F'):
404  if sector['name'].startswith('FA'):
405  sectorid += 24
406  hFW.Fill(sectorid,roiid,deltaR)
407 
408 
409  c.cd(1)
410  hB.Draw("colz")
411  c.cd(2)
412  hEC.Draw("colz")
413  c.cd(3)
414  hFW.Draw("colz")
415  c.Update()
416  c.SaveAs("ROIDeltaR%s.pdf" % "2016" if is2016 else "2015")
417 
418 
419 
420 def main():
421 
422  parser = argparse.ArgumentParser( description=__doc__,
423  formatter_class = argparse.RawTextHelpFormatter)
424 
425  parser.add_argument( '-i', dest='filename', default="TrigConfMuctpi/TestMioctGeometry2016.xml", type=str,
426  help='name of muon geometry xml file')
427 
428  args = parser.parse_args()
429 
430  is2016 = '2016' in args.filename
431 
432  print("Using input %s" % args.filename)
433 
434  geometry = readXML(args.filename)
435 
436  drawROIGeometry(geometry, is2016 = is2016)
437 
438  drawTopoGeometry(geometry, is2016 = is2016)
439 
440  drawTopoGeometryEtaPhi(geometry, colorBy=ETACODE, is2016 = is2016)
441 
442  drawTopoGeometryEtaPhi(geometry, colorBy=PHICODE, is2016 = is2016)
443 
444  drawTopoGeometryEtaPhi(geometry, colorBy=IPHI, is2016 = is2016)
445 
446  drawTopoGeometryEtaPhi(geometry, colorBy=IETA, is2016 = is2016)
447 
448  drawCodeValues(geometry, is2016 = is2016)
449 
450  drawIValues(geometry, is2016 = is2016)
451 
452  drawRoiDeltaR(geometry, is2016 = is2016)
453 
454 
455  input("Press Enter to continue...")
456 
457 
458 if __name__=="__main__":
459  main()
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
python.TrigEgammaMonitorHelper.TH2F
def TH2F(name, title, nxbins, bins_par2, bins_par3, bins_par4, bins_par5=None, bins_par6=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:45
visualize.drawIValues
def drawIValues(geometry, is2016)
Definition: visualize.py:323
visualize.main
def main()
Definition: visualize.py:420
visualize.drawCodeValues
def drawCodeValues(geometry, is2016)
Definition: visualize.py:297
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:26
visualize.drawROIGeometry
def drawROIGeometry(geometry, is2016)
Definition: visualize.py:44
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
visualize.drawTopoGeometryEtaPhi
def drawTopoGeometryEtaPhi(geometry, colorBy, is2016)
Definition: visualize.py:189
visualize.drawTopoGeometry
def drawTopoGeometry(geometry, is2016)
Definition: visualize.py:122
visualize.drawRoiDeltaR
def drawRoiDeltaR(geometry, is2016)
Definition: visualize.py:349
visualize.readXML
def readXML(filename)
Definition: visualize.py:38
python.LArMinBiasAlgConfig.float
float
Definition: LArMinBiasAlgConfig.py:65