5 from __future__
import print_function
7 from itertools
import groupby
8 from operator
import attrgetter
9 from math
import pi
as PI
18 """Represents a single input ROI (one line in the .dat file)
20 def __init__(self, subsystid, sectorid, roi, etamin, etamax, phimin, phimax, eta, phi, slot, connector, sectorname, mioct, sectoraddress, etacode, phicode):
51 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)
64 filename = os.path.basename(sys.argv[1]).
replace(
'.dat',
'.xml')
65 xml =
open(filename,
"w")
66 print(
'<?xml version="1.0" ?>\n', file=xml)
67 print(
'<!DOCTYPE MuCTPiGeometry SYSTEM "MUCTPIGeometry.dtd">\n', file=xml)
68 print(
'<MuCTPiGeometry>', file=xml)
69 for mioct
in sorted(g.keys()):
72 allSectorNames =
set()
73 for sector
in g[mioct].
values():
74 allSlots.update( [e.slot
for e
in sector] )
75 allSectorNames.update( [e.sectorname
for e
in sector] )
76 if len(allSlots) != 1:
77 raise RuntimeError(
"More than one slot defined for MIOCT %i" % mioct)
79 allSectorNames =
sorted(
list( allSectorNames ))
81 print(
' <MIOCT id="%i" slot="%s">' % ( mioct, slot ), file=xml)
82 print(
' <!-- contains sectors %s -->' %
", ".
join(allSectorNames), file=xml)
84 sector =
sorted(g[mioct][connector], key=attrgetter(
'eta',
'phi',
'etacode',
'phicode'))
85 print(
' <Sector connector="%s" name="%s">' % (connector,
", ".
join(
set([
str(e.sectorname)
for e
in sector])) ), file=xml)
86 print(
' <!-- contains %i ROIs -->' % len(sector), file=xml)
87 print(
' <!-- mapping from ROI to coding scheme -->', file=xml)
89 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)
90 print(
' </Sector>', file=xml)
93 map(allROIsInMIOCT.extend, g[mioct].
values())
95 print(
' <Decode>', file=xml)
97 for (eta_code,phi_code), rois
in groupby(
sorted(allROIsInMIOCT,key=attrgetter(
'etacode',
'phicode')), key=attrgetter(
'etacode',
'phicode')):
101 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)
102 print(
' </Decode>', file=xml)
103 print(
' </MIOCT>', file=xml)
105 print(
' <PtEncoding>', file=xml)
106 print(
' <PtCodeElement pt="1" code="0" value="4"/>', file=xml)
107 print(
' <PtCodeElement pt="2" code="1" value="6"/>', file=xml)
108 print(
' <PtCodeElement pt="3" code="2" value="10"/>', file=xml)
109 print(
' <PtCodeElement pt="4" code="2" value="11"/>', file=xml)
110 print(
' <PtCodeElement pt="5" code="2" value="15"/>', file=xml)
111 print(
' <PtCodeElement pt="6" code="2" value="20"/>', file=xml)
112 print(
' </PtEncoding>', file=xml)
113 print(
'</MuCTPiGeometry>', file=xml)
115 print(
"Wrote %s" % filename)
131 cellsEta = [(
min(e.etamin,e.etamax),
max(e.etamin,e.etamax))
for e
in rois]
132 etamins, etamaxs = zip(*cellsEta)
133 etamin =
min( [
min(e.etamin,e.etamax)
for e
in rois] )
134 etamax =
max( [
max(e.etamin,e.etamax)
for e
in rois] )
135 eta = (etamin+etamax)/2
141 cellsPhi = [(e.phimin, e.phimax)
for e
in rois]
144 upperedge = any([e.phi>6.2
for e
in rois])
145 loweredge = any([e.phi<0.2
for e
in rois])
146 splitTopoCell = upperedge
and loweredge
150 maxAtLowerEdge =
max([e.phimax
for e
in rois
if e.phi<1])
151 minAtUpperEdge =
min([e.phimin
for e
in rois
if e.phi>5])
152 centerTopoCell = minAtUpperEdge + maxAtLowerEdge
153 if centerTopoCell>=2*PI:
154 phimin = minAtUpperEdge - 2 * PI
155 phimax = maxAtLowerEdge
157 phimin = minAtUpperEdge
158 phimax = maxAtLowerEdge + 2 * PI
159 phi = (phimin+phimax)/2
163 phimins, phimaxs = zip(*cellsPhi)
164 phimin =
min(phimins)
165 phimax =
max(phimaxs)
166 phi = (phimin+phimax)/2
187 return (eta, phi, ieta, iphi, etamin, etamax, phimin, phimax)
198 print(
"Usage: %s <geomety.dat>" % os.path.basename(sys.argv[0]))
202 data =
open(sys.argv[1])
203 lines = [
Entry(*l.rstrip().
split())
for l
in data.readlines()
if not l.startswith(
'#')]
204 lines =
sorted(lines, key = attrgetter(
'mioct',
'connector') )
208 for k, g
in groupby(lines, key=attrgetter(
'mioct')):
210 for k2, g2
in groupby(g, key = attrgetter(
'connector')):
211 groups[k][k2] =
list(g2)
220 if __name__==
"__main__":
225 traceback.print_exc()