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 from __future__ import print_function
6 import sys,os
7 from itertools import groupby
8 from operator import attrgetter
9 from math import pi as PI
10 
11 # settings temporary
12 global shiftPhi
13 shiftPhi = True
14 
15 
16 
17 class Entry:
18  """Represents a single input ROI (one line in the .dat file)
19  """
20  def __init__(self, subsystid, sectorid, roi, etamin, etamax, phimin, phimax, eta, phi, slot, connector, sectorname, mioct, sectoraddress, etacode, phicode):
21  self.hemisphere = "A" if subsystid=="0" else "C"
22  self.sectorid = int(sectorid)
23  self.roi = int(roi)
24  self.etamin = float(etamin)
25  self.etamax = float(etamax)
26  self.phimin = float(phimin)
27  self.phimax = float(phimax)
28  self.eta = float(eta)
29  self.phi = float(phi)
30  self.slot = int(slot)
31  self.connector = int(connector)
32  self.sectorname = sectorname
33  self.mioct = int(mioct)
34  self.etacode = etacode
35  self.phicode = phicode
36 
37  global shiftPhi
38  if shiftPhi:
39  # make sure to have everything between 0 and 2*PI
40  if max(self.phimin, self.phimax) < 0:
41  self.phimin += 2 * PI
42  self.phimax += 2 * PI
43  self.phi += 2 * PI
44 
45 
46  if self.phimin * self.phimax < -1:
47  print("WARNING %s" % str(self))
48 
49 
50  def __str__(self):
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)
52 
53  def __repr__(self):
54  return self.__str__()
55 
56 
57 
58 #def keyfunc(t):
59 # return (t.mioct, t.connector)
60 
61 
62 
63 def writeXML(g):
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()):
70 
71  allSlots = set()
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)
78  slot = allSlots.pop()
79  allSectorNames = sorted(list( allSectorNames ))
80 
81  print(' <MIOCT id="%i" slot="%s">' % ( mioct, slot ), file=xml)
82  print(' <!-- contains sectors %s -->' % ", ".join(allSectorNames), file=xml)
83  for connector in sorted(g[mioct].keys()):
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)
88  for e in sector:
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)
91 
92  allROIsInMIOCT = []
93  map(allROIsInMIOCT.extend, g[mioct].values()) # add all ROIs in this MIOCT
94 
95  print(' <Decode>', file=xml)
96 
97  for (eta_code,phi_code), rois in groupby(sorted(allROIsInMIOCT,key=attrgetter('etacode','phicode')), key=attrgetter('etacode','phicode')):
98 
99  (eta, phi, ieta, iphi, etamin, etamax, phimin, phimax) = getTopoCellPosition(rois)
100 
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)
104  # hardcoding the PT at the moment
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)
114  xml.close()
115  print("Wrote %s" % filename)
116 
117 
118 
119 
121 
122  rois = list(rois)
123 
124  # ETA
125 
126  # eta min and max are different in the RPC (barrel) and TGC (endcap and forward)
127  # in the RPC: abs(etamin)<abs(etamax)
128  # in the TGC: etamin<etamax
129  # we are going to use the TGC definition: etamin<etamax
130 
131  cellsEta = [(min(e.etamin,e.etamax), max(e.etamin,e.etamax)) for e in rois]
132  etamins, etamaxs = zip(*cellsEta) # transpose
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
136 
137 
138  # PHI CONVENTION
139 
140 
141  cellsPhi = [(e.phimin, e.phimax) for e in rois]
142 
143  # check if there are cells on both sides of the PI boundary
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
147 
148 
149  if splitTopoCell:
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: # shift down
154  phimin = minAtUpperEdge - 2 * PI
155  phimax = maxAtLowerEdge
156  else: # shift up
157  phimin = minAtUpperEdge
158  phimax = maxAtLowerEdge + 2 * PI
159  phi = (phimin+phimax)/2
160  #print "JOERG centerTopoCell = ", centerTopoCell, "=>", phi,"[", phimin, "-", phimax, "] (" , minAtUpperEdge," " , maxAtLowerEdge , ")"
161  else:
162 
163  phimins, phimaxs = zip(*cellsPhi) # transpose
164  phimin = min(phimins)
165  phimax = max(phimaxs)
166  phi = (phimin+phimax)/2
167 
168 
169  ieta = round(eta*10)
170  iphi = round(phi*10)
171 
172  #if (phimin * phimax) < -100:
173  # print "eta ", eta
174  # print "phimin ", phimin
175  # print "phimax ", phimax
176  # print "phi ", phi
177  # print "iphi ", iphi
178  # print ""
179 
180 
181  #test = (phimin * phimax) < 0
182  #if test:
183  # print '\n'.join([str(x) for x in cells])
184  # print "\n\n\n"
185 
186 
187  return (eta, phi, ieta, iphi, etamin, etamax, phimin, phimax)
188 
189 
190 
191 
192 
193 
194 
195 
196 def main():
197  if len(sys.argv)!=2:
198  print("Usage: %s <geomety.dat>" % os.path.basename(sys.argv[0]))
199  return
200 
201  # read in the data files containing the ROI information
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') )
205  data.close()
206 
207  groups = {}
208  for k, g in groupby(lines, key=attrgetter('mioct')):
209  groups[k] = {}
210  for k2, g2 in groupby(g, key = attrgetter('connector')):
211  groups[k][k2] = list(g2)
212 
213  writeXML(groups)
214 
215  return 0
216 
217 
218 
219 
220 if __name__=="__main__":
221  try:
222  sys.exit(main())
223  except Exception:
224  import traceback
225  traceback.print_exc()
226  sys.exit(1)
replace
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition: hcg.cxx:307
generateMioctEncodingFile.Entry.phicode
phicode
Definition: generateMioctEncodingFile.py:35
generateMioctEncodingFile.writeXML
def writeXML(g)
Definition: generateMioctEncodingFile.py:63
generateMioctEncodingFile.Entry.phi
phi
Definition: generateMioctEncodingFile.py:29
max
#define max(a, b)
Definition: cfImp.cxx:41
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
generateMioctEncodingFile.Entry.__repr__
def __repr__(self)
Definition: generateMioctEncodingFile.py:53
generateMioctEncodingFile.Entry.sectorid
sectorid
Definition: generateMioctEncodingFile.py:22
MuonGM::round
float round(const float toRound, const unsigned int decimals)
Definition: Mdt.cxx:27
generateMioctEncodingFile.Entry.roi
roi
Definition: generateMioctEncodingFile.py:23
generateMioctEncodingFile.Entry.slot
slot
Definition: generateMioctEncodingFile.py:30
python.Bindings.values
values
Definition: Control/AthenaPython/python/Bindings.py:797
generateMioctEncodingFile.Entry.eta
eta
Definition: generateMioctEncodingFile.py:28
generateMioctEncodingFile.Entry.connector
connector
Definition: generateMioctEncodingFile.py:31
generateMioctEncodingFile.Entry.mioct
mioct
Definition: generateMioctEncodingFile.py:33
generateMioctEncodingFile.Entry.hemisphere
hemisphere
Definition: generateMioctEncodingFile.py:21
generateMioctEncodingFile.main
def main()
Definition: generateMioctEncodingFile.py:196
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
generateMioctEncodingFile.Entry.etamin
etamin
Definition: generateMioctEncodingFile.py:24
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename T::value_type > sorted(T begin, T end)
Helper function to create a sorted vector from an unsorted one.
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:224
min
#define min(a, b)
Definition: cfImp.cxx:40
generateMioctEncodingFile.Entry.phimin
phimin
Definition: generateMioctEncodingFile.py:26
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:20
generateMioctEncodingFile.Entry
Definition: generateMioctEncodingFile.py:17
generateMioctEncodingFile.Entry.__str__
def __str__(self)
Definition: generateMioctEncodingFile.py:50
Trk::open
@ open
Definition: BinningType.h:40
generateMioctEncodingFile.Entry.etamax
etamax
Definition: generateMioctEncodingFile.py:25
generateMioctEncodingFile.getTopoCellPosition
def getTopoCellPosition(rois)
Definition: generateMioctEncodingFile.py:120
generateMioctEncodingFile.Entry.phimax
phimax
Definition: generateMioctEncodingFile.py:27
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28
str
Definition: BTagTrackIpAccessor.cxx:11
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:790
generateMioctEncodingFile.Entry.etacode
etacode
Definition: generateMioctEncodingFile.py:34
generateMioctEncodingFile.Entry.sectorname
sectorname
Definition: generateMioctEncodingFile.py:32
readCCLHist.float
float
Definition: readCCLHist.py:83
Trk::split
@ split
Definition: LayerMaterialProperties.h:38