ATLAS Offline Software
generateMioctEncodingFile.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4 
5 import sys,os
6 from itertools import groupby
7 from operator import attrgetter
8 from math import pi as PI
9 
10 
11 class Entry:
12  """Represents a single input ROI (one line in the .dat file)
13  """
14  def __init__(self, subsystid, sectorid, roi, etamin, etamax, phimin, phimax, eta, phi, slot, connector, sectorname, mioct, sectoraddress, etacode, phicode):
15  self.hemisphere = "A" if subsystid=="0" else "C"
16  self.sectorid = int(sectorid)
17  self.roi = int(roi)
18  self.etamin = float(etamin)
19  self.etamax = float(etamax)
20  self.phimin = float(phimin)
21  self.phimax = float(phimax)
22  self.eta = float(eta)
23  self.phi = float(phi)
24  self.slot = int(slot)
25  self.connector = int(connector)
26  self.sectorname = sectorname
27  self.mioct = int(mioct)
28  self.etacode = etacode
29  self.phicode = phicode
30 
31  # make sure to have everything between 0 and 2*PI
32  if max(self.phimin, self.phimax) < 0:
33  self.phimin += 2 * PI
34  self.phimax += 2 * PI
35  self.phi += 2 * PI
36 
37 
38  if self.phimin * self.phimax < -1:
39  print("WARNING %s" % str(self))
40 
41 
42  def __str__(self):
43  return "mioct=%i (slot %i), sector=%i, roi=%i, eta=%f, phi=%f, conn=%i, %s" % (self.mioct, self.slot, self.sectorid, self.roi, self.eta, self.phi, self.connector, self.sectorname)
44 
45  def __repr__(self):
46  return self.__str__()
47 
48 
49 
50 #def keyfunc(t):
51 # return (t.mioct, t.connector)
52 
53 
54 
55 def writeXML(g):
56  filename = os.path.basename(sys.argv[1]).replace('.dat','.xml')
57  xml = open(filename,"w")
58  print('<?xml version="1.0" ?>\n', file=xml)
59  print('<!DOCTYPE MuCTPiGeometry SYSTEM "MUCTPIGeometry.dtd">\n', file=xml)
60  print('<MuCTPiGeometry>', file=xml)
61  for mioct in sorted(g.keys()):
62 
63  allSlots = set()
64  allSectorNames = set()
65  for sector in g[mioct].values():
66  allSlots.update( [e.slot for e in sector] )
67  allSectorNames.update( [e.sectorname for e in sector] )
68  if len(allSlots) != 1:
69  raise RuntimeError("More than one slot defined for MIOCT %i" % mioct)
70  slot = allSlots.pop()
71  allSectorNames = sorted(list( allSectorNames ))
72 
73  print(' <MIOCT id="%i" slot="%s">' % ( mioct, slot ), file=xml)
74  print(' <!-- contains sectors %s -->' % ", ".join(allSectorNames), file=xml)
75  for connector in sorted(g[mioct].keys()):
76  sector = sorted(g[mioct][connector], key=attrgetter('eta','phi','etacode','phicode'))
77  print(' <Sector connector="%s" name="%s">' % (connector, ", ".join(set([str(e.sectorname) for e in sector])) ), file=xml)
78  print(' <!-- contains %i ROIs -->' % len(sector), file=xml)
79  print(' <!-- mapping from ROI to coding scheme -->', file=xml)
80  for e in sector:
81  print(' <ROI eta="%f" phi="%f" etacode="%s" phicode="%s" etamin="%f" etamax="%f" phimin="%f" phimax="%f" roiid="%i"/>' % (e.eta, e.phi, e.etacode, e.phicode, e.etamin, e.etamax, e.phimin, e.phimax, e.roi), file=xml)
82  print(' </Sector>', file=xml)
83 
84  allROIsInMIOCT = []
85  map(allROIsInMIOCT.extend, g[mioct].values()) # add all ROIs in this MIOCT
86 
87  print(' <Decode>', file=xml)
88 
89  for (eta_code,phi_code), rois in groupby(sorted(allROIsInMIOCT,key=attrgetter('etacode','phicode')), key=attrgetter('etacode','phicode')):
90 
91  (eta, phi, ieta, iphi, etamin, etamax, phimin, phimax) = getTopoCellPosition(rois)
92 
93  print(' <TopoCell etacode="%s" phicode="%s" eta="%f" phi="%f" ieta="%i" iphi="%i" etamin="%f" etamax="%f" phimin="%f" phimax="%f"/>' % (eta_code, phi_code, eta, phi, ieta, iphi, etamin, etamax, phimin, phimax), file=xml)
94  print(' </Decode>', file=xml)
95  print(' </MIOCT>', file=xml)
96  # hardcoding the PT at the moment
97  print(' <PtEncoding>', file=xml)
98  print(' <PtCodeElement pt="1" code="0" value="4"/>', file=xml)
99  print(' <PtCodeElement pt="2" code="1" value="6"/>', file=xml)
100  print(' <PtCodeElement pt="3" code="2" value="10"/>', file=xml)
101  print(' <PtCodeElement pt="4" code="2" value="11"/>', file=xml)
102  print(' <PtCodeElement pt="5" code="2" value="15"/>', file=xml)
103  print(' <PtCodeElement pt="6" code="2" value="20"/>', file=xml)
104  print(' </PtEncoding>', file=xml)
105  print('</MuCTPiGeometry>', file=xml)
106  xml.close()
107  print("Wrote %s" % filename)
108 
109 
110 
111 
113 
114  rois = list(rois)
115 
116  # ETA
117 
118  # eta min and max are different in the RPC (barrel) and TGC (endcap and forward)
119  # in the RPC: abs(etamin)<abs(etamax)
120  # in the TGC: etamin<etamax
121  # we are going to use the TGC definition: etamin<etamax
122 
123  cellsEta = [(min(e.etamin,e.etamax), max(e.etamin,e.etamax)) for e in rois]
124  etamins, etamaxs = zip(*cellsEta) # transpose
125  etamin = min( [ min(e.etamin,e.etamax) for e in rois] )
126  etamax = max( [ max(e.etamin,e.etamax) for e in rois] )
127  eta = (etamin+etamax)/2
128 
129 
130  # PHI CONVENTION
131 
132 
133  cellsPhi = [(e.phimin, e.phimax) for e in rois]
134 
135  # check if there are cells on both sides of the PI boundary
136  upperedge = any([e.phi>6.2 for e in rois])
137  loweredge = any([e.phi<0.2 for e in rois])
138  splitTopoCell = upperedge and loweredge
139 
140 
141  if splitTopoCell:
142  maxAtLowerEdge = max([e.phimax for e in rois if e.phi<1])
143  minAtUpperEdge = min([e.phimin for e in rois if e.phi>5])
144  centerTopoCell = minAtUpperEdge + maxAtLowerEdge
145  if centerTopoCell>=2*PI: # shift down
146  phimin = minAtUpperEdge - 2 * PI
147  phimax = maxAtLowerEdge
148  else: # shift up
149  phimin = minAtUpperEdge
150  phimax = maxAtLowerEdge + 2 * PI
151  phi = (phimin+phimax)/2
152  #print "JOERG centerTopoCell = ", centerTopoCell, "=>", phi,"[", phimin, "-", phimax, "] (" , minAtUpperEdge," " , maxAtLowerEdge , ")"
153  else:
154 
155  phimins, phimaxs = zip(*cellsPhi) # transpose
156  phimin = min(phimins)
157  phimax = max(phimaxs)
158  phi = (phimin+phimax)/2
159 
160 
161  ieta = round(eta*10)
162  iphi = round(phi*10)
163 
164  #if (phimin * phimax) < -100:
165  # print "eta ", eta
166  # print "phimin ", phimin
167  # print "phimax ", phimax
168  # print "phi ", phi
169  # print "iphi ", iphi
170  # print ""
171 
172 
173  #test = (phimin * phimax) < 0
174  #if test:
175  # print '\n'.join([str(x) for x in cells])
176  # print "\n\n\n"
177 
178 
179  return (eta, phi, ieta, iphi, etamin, etamax, phimin, phimax)
180 
181 
182 
183 
184 
185 
186 
187 
188 def main():
189  if len(sys.argv)!=2:
190  print("Usage: %s <geomety.dat>" % os.path.basename(sys.argv[0]))
191  return
192 
193  # read in the data files containing the ROI information
194  data = open(sys.argv[1])
195  lines = [Entry(*l.rstrip().split()) for l in data.readlines() if not l.startswith('#')]
196  lines = sorted(lines, key = attrgetter('mioct','connector') )
197  data.close()
198 
199  groups = {}
200  for k, g in groupby(lines, key=attrgetter('mioct')):
201  groups[k] = {}
202  for k2, g2 in groupby(g, key = attrgetter('connector')):
203  groups[k][k2] = list(g2)
204 
205  writeXML(groups)
206 
207  return 0
208 
209 
210 
211 
212 if __name__=="__main__":
213  try:
214  sys.exit(main())
215  except Exception:
216  import traceback
217  traceback.print_exc()
218  sys.exit(1)
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename R::value_type > sorted(const R &r, PROJ proj={})
Helper function to create a sorted vector from an unsorted range.
replace
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition: hcg.cxx:310
generateMioctEncodingFile.Entry.phicode
phicode
Definition: generateMioctEncodingFile.py:29
generateMioctEncodingFile.writeXML
def writeXML(g)
Definition: generateMioctEncodingFile.py:55
generateMioctEncodingFile.Entry.phi
phi
Definition: generateMioctEncodingFile.py:23
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
generateMioctEncodingFile.Entry.__repr__
def __repr__(self)
Definition: generateMioctEncodingFile.py:45
generateMioctEncodingFile.Entry.sectorid
sectorid
Definition: generateMioctEncodingFile.py:16
MuonGM::round
float round(const float toRound, const unsigned int decimals)
Definition: Mdt.cxx:27
generateMioctEncodingFile.Entry.roi
roi
Definition: generateMioctEncodingFile.py:17
generateMioctEncodingFile.Entry.slot
slot
Definition: generateMioctEncodingFile.py:24
python.Bindings.values
values
Definition: Control/AthenaPython/python/Bindings.py:808
generateMioctEncodingFile.Entry.eta
eta
Definition: generateMioctEncodingFile.py:22
generateMioctEncodingFile.Entry.connector
connector
Definition: generateMioctEncodingFile.py:25
generateMioctEncodingFile.Entry.mioct
mioct
Definition: generateMioctEncodingFile.py:27
generateMioctEncodingFile.Entry.hemisphere
hemisphere
Definition: generateMioctEncodingFile.py:15
generateMioctEncodingFile.main
def main()
Definition: generateMioctEncodingFile.py:188
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
generateMioctEncodingFile.Entry.etamin
etamin
Definition: generateMioctEncodingFile.py:18
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:232
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:26
generateMioctEncodingFile.Entry.phimin
phimin
Definition: generateMioctEncodingFile.py:20
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
generateMioctEncodingFile.Entry.__init__
def __init__(self, subsystid, sectorid, roi, etamin, etamax, phimin, phimax, eta, phi, slot, connector, sectorname, mioct, sectoraddress, etacode, phicode)
Definition: generateMioctEncodingFile.py:14
generateMioctEncodingFile.Entry
Definition: generateMioctEncodingFile.py:11
generateMioctEncodingFile.Entry.__str__
def __str__(self)
Definition: generateMioctEncodingFile.py:42
Trk::open
@ open
Definition: BinningType.h:40
generateMioctEncodingFile.Entry.etamax
etamax
Definition: generateMioctEncodingFile.py:19
generateMioctEncodingFile.getTopoCellPosition
def getTopoCellPosition(rois)
Definition: generateMioctEncodingFile.py:112
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
generateMioctEncodingFile.Entry.phimax
phimax
Definition: generateMioctEncodingFile.py:21
str
Definition: BTagTrackIpAccessor.cxx:11
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:801
generateMioctEncodingFile.Entry.etacode
etacode
Definition: generateMioctEncodingFile.py:28
generateMioctEncodingFile.Entry.sectorname
sectorname
Definition: generateMioctEncodingFile.py:26
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
python.LArMinBiasAlgConfig.float
float
Definition: LArMinBiasAlgConfig.py:65