ATLAS Offline Software
keylayer_zslicemap.py
Go to the documentation of this file.
1 #--------------------------------------------------------------------------------
2 # Config
3 #--------------------------------------------------------------------------------
4 
5 # Create an rmap file and populate it with eta\phi regions sliced by Z in a key layer
6 # run: python keylayer_zslicemap.py path/to/Extrapolation/file.root
7 
8 # Extrapolation Engine files are stored on eos:
9 # /eos/atlas/atlascerngroupdisk/det-htt/HTTsim/user/abkahn/ExtrapolationEngine/
10 
11 # For information on map configuration files see the README in FPGATrackSimMaps
12 
13 import argparse
14 import numpy as np
15 import ROOT
16 import time
17 import sys
18 import glob
19 
20 from PyJobTransforms.trfLogger import msg
21 
22 parser = argparse.ArgumentParser(description='Flattens Wrapper Files')
23 parser.add_argument('inname', metavar='inputfile', type=str,
24  help='input file name (should be FPGATrackSim Wrapper File)')
25 args = parser.parse_args()
26 
27 # initialize empty sets and lists
28 phi_pb, phi_pe, phi_sb, phi_se = set(),set(),set(),set()
29 eta_pb, eta_pe, eta_sb, eta_se = set(),set(),set(),set()
30 printPB, printPE, printSB, printSE = [[] for i in range(7)],[[] for i in range(7)],[[] for i in range(7)],[[] for i in range(7)] # 7 slices
31 
32 # create rmap file
33 rmap = open('alleta_allphi_kl_strip2_ATLAS-P2-ITK-23-00-01.rmap', 'w')
34 
35 '''
36 # get list of files
37 fileList = glob.glob("%s/*.root" % args.inname)
38 # make TChain
39 Data = ROOT.TChain("ExtrapolationEngineTest")
40 # add files to TChain
41 for file in fileList:
42  msg.info("Adding file " + file)
43  Data.AddFile(file)
44 '''
45 
46 # get single file
47 f = ROOT.TFile.Open(args.inname)
48 Data = f.Get("ExtrapolationEngineTest")
49 
50 sys.stdout.write("start\n")
51 sys.stdout.flush()
52 
53 #--------------------------------------------------------------------------------
54 # parse geometry
55 #--------------------------------------------------------------------------------
56 sys.stdout.write("reading track input\n")
57 sys.stdout.flush()
58 t0 = time.clock()
59 
60 hits = {} # hit dictionary
61 num_entries = Data.GetEntries()
62 counter, events, perc = 0,0,0
63 for trk in Data:
64  events = events + 1
65  hit_idx = 0
66  for dic_idx in np.arange(counter, counter + len(trk.SensitivePosX)):
67  hits[dic_idx] = {}
68  hits[dic_idx]["geom"] = trk.SensitiveIsPixel[hit_idx]
69  hits[dic_idx]["ec"] = trk.SensitiveBarrelEndcap[hit_idx]
70  if trk.SensitiveIsPixel[hit_idx] == 0: #strip
71  layer = 2*trk.SensitiveLayerDisc[hit_idx]+trk.SensitiveSide[hit_idx]
72  else: #pixel
73  layer = trk.SensitiveLayerDisc[hit_idx]
74  '''
75  # If you want to use the layer scheme in the pixel endcap where each disk is a unique layer:
76  if trk.SensitiveIsPixel[hit_idx] == 0: # strip
77  layer = 2*trk.SensitiveLayerDisc[hit_idx]+trk.SensitiveSide[hit_idx]
78  else:
79  if trk.SensitiveBarrelEndcap[hit_idx] == 0: # pixel barrel
80  layer = trk.SensitiveLayerDisc[hit_idx]
81  else: # pixel endcap
82  # number of disks per layer in 23-00-01: [15, 29, 6, 11, 8, 8, 9, 9] --> [0, 15, 44, 50, 61, 69, 77, 86]
83  # number of disks per layer in 22-02-00: [17, 30, 11, 8, 9] --> [0,17,47,58,66]
84  etas_per_layer = [0, 15, 44, 50, 61, 69, 77, 86]
85  layer = etas_per_layer[trk.SensitiveLayerDisc[hit_idx]] + trk.SensitiveEtaModule[hit_idx]
86  '''
87  hits[dic_idx]["layer"] = layer
88  hits[dic_idx]["z"] = trk.SensitivePosZ[hit_idx]
89  hit_idx = hit_idx + 1
90  # print status
91  counter = counter + len(trk.SensitivePosX)
92  perc = 100 * float(events)/float(num_entries)
93  if events % 5000 == 0:
94  sys.stdout.write("%d %% complete\n" % perc)
95  sys.stdout.flush()
96 
97 t1 = time.clock()
98 sys.stdout.write("parse geom took %d seconds\n" % (t1-t0))
99 sys.stdout.flush()
100 
101 #--------------------------------------------------------------------------------
102 # fill slices
103 #--------------------------------------------------------------------------------
104 sys.stdout.write("\nfilling slices\n")
105 sys.stdout.flush()
106 t2 = time.clock()
107 
108 # make key layer dictionary - strip barrel layer 2
109 kl = {k:v for (k,v) in hits.items() if hits[k]['geom'] == 0 if hits[k]['ec'] == 0 if hits[k]['layer'] == 2}
110 max_z = max(int(l['z']) for l in kl.values())
111 min_z = min(int(l['z']) for l in kl.values())
112 slices = np.linspace(min_z,max_z, 7)
113 
114 # array of slice dictionaries, each one hold the hits in that slice
115 sliceDicts = [dict() for x in range(6)]
116 
117 for i in range(6): # loop over slices
118  sys.stdout.write("\nstarting slice %d \n" % i)
119  sys.stdout.write("%.1f mm < z < %.1f mm \n" % (slices[i],slices[i+1]))
120  sys.stdout.flush()
121  s = {}
122  dic_idx = 0 # index that counts the number of hits stored in each slice dictionary
123  for trk in Data:
124  fillHits = False
125 
126  for hit in range(len(trk.SensitivePosX)): # look for keyhit, then find appropriate slice
127  isPix = trk.SensitiveIsPixel[hit]
128  bEC = trk.SensitiveBarrelEndcap[hit]
129  if isPix == 0: # strip hit
130  layer = 2*trk.SensitiveLayerDisc[hit]+trk.SensitiveSide[hit]
131  else: # pixel hit
132  layer = trk.SensitiveLayerDisc[hit]
133 
134  z = trk.SensitivePosZ[hit]
135  # if hit is in key layer, add all hits in track to appropriate slice dictionary
136  if isPix == 0 and bEC == 0 and layer == 2 and z >= slices[i] and z <= slices[i+1]:
137  fillHits = True
138 
139  if fillHits == True:
140  for hit in range(len(trk.SensitivePosX)):
141  s[dic_idx] = {}
142  s[dic_idx]["geom"] = trk.SensitiveIsPixel[hit]
143  s[dic_idx]["ec"] = trk.SensitiveBarrelEndcap[hit]
144  if trk.SensitiveIsPixel[hit] == 0:
145  layer = 2*trk.SensitiveLayerDisc[hit]+trk.SensitiveSide[hit]
146  else:
147  layer = trk.SensitiveLayerDisc[hit]
148  s[dic_idx]["layer"] = layer
149  s[dic_idx]["phi"] = trk.SensitivePhiModule[hit]
150  s[dic_idx]["eta"] = trk.SensitiveEtaModule[hit]
151  strip = trk.SensitivePhiIndex[hit]
152  # calculate row from strip
153  if trk.SensitiveIsPixel[hit] == 0 and layer <= 3: #First four sct FPGATrackSim layers have 4 rows per module
154  if strip >= 0 and strip < 1280:
155  row = 0
156  elif strip >= 1280 and strip < 2560:
157  row = 1
158  elif strip >= 2560 and strip < 3840:
159  row = 2
160  elif strip >= 3840 and strip <= 5120:
161  row = 3
162  else:
163  row = 999 # so it's obvious if something went wrong
164  s[dic_idx]["eta"] = 4*s[dic_idx]["eta"] + row
165  elif trk.SensitiveIsPixel[hit] == 0 and layer >1: # Outer two ITK sct layers have 2 rows per module
166  if strip >= 0 and strip < 1280:
167  row = 0
168  elif strip >= 1280 and strip <= 2560:
169  row = 1
170  else:
171  row = -999
172  s[dic_idx]["eta"] = 2*s[dic_idx]["eta"] + row
173  dic_idx = dic_idx + 1
174  sliceDicts[i] = s
175 
176 t3 = time.clock()
177 sys.stdout.write("Find key hits and fill slices took %d seconds\n" % (t3-t2))
178 sys.stdout.flush()
179 
180 #--------------------------------------------------------------------------------
181 # print map
182 #--------------------------------------------------------------------------------
183 for i in range(0,6):
184 
185  s = sliceDicts[i]
186 
187  #make pixel barrel dict
188  pb = {k:v for (k,v) in s.items() if s[k]['geom'] == 1 if s[k]['ec'] == 0}
189  #make pixel endcap dict
190  pe = {k:v for (k,v) in s.items() if s[k]['geom'] == 1 if s[k]['ec'] != 0}
191  #make strip barrel dict
192  sb = {k:v for (k,v) in s.items() if s[k]['geom'] == 0 if s[k]['ec'] == 0}
193  #make strip endcap dict
194  se = {k:v for (k,v) in s.items() if s[k]['geom'] == 0 if s[k]['ec'] != 0}
195 
196  try:
197  pbmax = max(int(l['layer']) for l in pb.values()) + 1
198  except ValueError:
199  msg.warning("pixel barrel had no hits in slice %d" % i)
200  pbmax = -1
201  try:
202  pemax = max(int(l['layer']) for l in pe.values()) + 1
203  except ValueError:
204  msg.warning("pixel endcap had no hits in slice %d" % i)
205  pemax = -1
206  try:
207  sbmax = max(int(l['layer']) for l in sb.values()) + 1
208  except ValueError:
209  msg.warning("strip barrel had no hits in slice %d" % i)
210  sbmax = -1
211  try:
212  semax = max(int(l['layer']) for l in se.values()) + 1
213  except ValueError:
214  msg.warning("strip endcap had no hits in slice %d" % i)
215  semax = -1
216 
217  for lyr in range(max(int(l['layer']+1) for l in s.values())):
218  phi_pb.clear()
219  phi_pe.clear()
220  phi_sb.clear()
221  phi_se.clear()
222 
223  eta_pb.clear()
224  eta_pe.clear()
225  eta_sb.clear()
226  eta_se.clear()
227 
228  # Pixel Barrel
229  if lyr < pbmax:
230  for key in pb:
231  if pb[key]['layer'] == (lyr):
232  phi_pb.add(pb[key]["phi"])
233  eta_pb.add(pb[key]["eta"])
234  printPB[i].append("1 0 %d %d %d %d %d %d %d" % (lyr,min(phi_pb),max(phi_pb),len(phi_pb),min(eta_pb),max(eta_pb),len(eta_pb)))
235 
236  # Pixel Endcap
237  if lyr < pemax:
238  for key in pe:
239  if pe[key]['layer'] == (lyr):
240  phi_pe.add(pe[key]["phi"])
241  eta_pe.add(pe[key]["eta"])
242  printPE[i].append("1 1 %d %d %d %d %d %d %d" % (lyr,min(phi_pe),max(phi_pe),len(phi_pe),min(eta_pe),max(eta_pe),len(eta_pe)))
243 
244  # Strip Barrel
245  if lyr < sbmax:
246  for key in sb:
247  if sb[key]['layer'] == (lyr):
248  phi_sb.add(sb[key]["phi"])
249  eta_sb.add(sb[key]["eta"])
250  printSB[i].append("0 0 %d %d %d %d %d %d %d" % (lyr,min(phi_sb),max(phi_sb),len(phi_sb),min(eta_sb),max(eta_sb),len(eta_sb)))
251 
252  #Strip Endcap
253  if lyr < semax:
254  for key in se:
255  if se[key]['layer'] == (lyr):
256  phi_se.add(se[key]["phi"])
257  eta_se.add(se[key]["eta"])
258  printSE[i].append("0 1 %d %d %d %d %d %d %d" % (lyr,min(phi_se),max(phi_se),len(phi_se),min(eta_se),max(eta_se),len(eta_se)))
259 
260 
261  # Print empty pe layers
262  if pemax == -1:
263  for j in range(4): # hardcoded
264  printPE[i].append("0 1 %d 0 0 0 0 0 0" % j)
265 
266  # Print empty se layers
267  if semax == -1:
268  for j in range(12): # hardcoded
269  printSE[i].append("0 1 %d 0 0 0 0 0 0" % j)
270 
271 
272 rmap.write("towers 6 phi 16\n\n")
273 
274 for z in range(0,6):
275  rmap.write("\n%d \n" % z)
276 
277  for i in range(0,len(printPB[z])):
278  rmap.write(printPB[z][i])
279  rmap.write("\n")
280  for i in range(0,len(printPE[z])):
281  rmap.write(printPE[z][i])
282  rmap.write("\n")
283  for i in range(0,len(printSB[z])):
284  rmap.write(printSB[z][i])
285  rmap.write("\n")
286  for i in range(0,len(printSE[z])):
287  rmap.write(printSE[z][i])
288  rmap.write("\n")
289 
290 rmap.close()
max
#define max(a, b)
Definition: cfImp.cxx:41
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
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
min
#define min(a, b)
Definition: cfImp.cxx:40
Trk::open
@ open
Definition: BinningType.h:40
PyJobTransforms.trfLogger
Logging configuration for ATLAS job transforms.
readCCLHist.float
float
Definition: readCCLHist.py:83