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