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