ATLAS Offline Software
Loading...
Searching...
No Matches
createLinkingScheme.py
Go to the documentation of this file.
1#!/usr/bin/env python
2import numpy as np
3import pandas as pd
4
6 import argparse
7 parser = argparse.ArgumentParser(description="create GBTS linking table")
8 parser.add_argument("-o", "--output_file", default="binTables_ITK_RUN4.txt", help="output table for use by GBTS (default=%(default)s)")
9 parser.add_argument("-p", "--min-link-probability", type=float, default=0.01, help="minimum linking probability (default=%(default)s, LRT=0.001)")
10 parser.add_argument("-e", "--eta-bins", type=float, default=0.2, help="eta bin width (default=%(default)s)")
11 parser.add_argument("-a", "--no-symmetrize", action='store_true', help="do not symmetrize by adding missing layers on the other side")
12 parser.add_argument("-i", "--write-inverse-linking-scheme", action='store_true', help="write inverse linking scheme table")
13 parser.add_argument("-w", "--write-scheme-file", default="inverse_linking_scheme_ITK_RUN4.txt", help="inverse linking scheme table file name (default=%(default)s)")
14 parser.add_argument("input_csv", nargs="?", default="layer_connection_table.csv", help="input CSV layer connection table, output from Athena (default=%(default)s)")
15 return parser.parse_args()
16
18 def __init__(self, id):
19 self.id = id
20 self.nTargets = 0
21
22
23class Pair(object):
24 def __init__(self, src, dst, iter):
25 self.src = src
26 self.dst = dst
27 self.iter = iter
28
29args = parseArgs()
30
31print(f"Read {args.input_csv}")
32
33link_df = pd.read_csv(args.input_csv)
34link_df_orig_len = len(link_df)
35
36link_df = link_df.drop(link_df[link_df['probability'] < args.min_link_probability].index)
37
38print(f"Drop {link_df_orig_len-len(link_df)} ({link_df_orig_len}->{len(link_df)}) links with p<{args.min_link_probability}")
39
40layer_from_idx = link_df['from'].values
41layer_to_idx = link_df['to'].values
42
43layer_idx = np.unique(np.concatenate((layer_from_idx, layer_to_idx)))
44
45# fill layer dictionary
46
47layer_dict = {}
48
49for id in layer_idx:
50 layer_dict[id] = Layer(id)
51
52dst_idx = link_df['from']
53
54for idx in dst_idx:
55 l = layer_dict[idx]
56 l.nTargets += 1
57
58src_dict = {k: v for k, v in layer_dict.items() if v.nTargets == 0}
59dst_dict = {k: v for k, v in layer_dict.items() if v.nTargets > 0}
60
61# Iteration 1
62iter = 1
63
64pair_dict = {}
65dst_layer_dict = {}
66src_layer_dict = {}
67
68print('Creating stages...')
69
70while (True):
71 print('Iteration ', iter, 'N src', len(src_dict), 'N dst', len(dst_dict))
72
73 pair_dict[iter] = []
74 src_layer_dict[iter] = []
75 for key, lay in src_dict.items():
76 src_layer_dict[iter].append(key)
77 dst_layer_dict[iter] = []
78
79 for key, lay in src_dict.items():
80 dst_df = link_df[link_df['to'] == key]
81 dst_vec = dst_df['from']
82 for d in dst_vec:
83 dst_dict[d].nTargets -= 1
84 pair_dict[iter].append(Pair(key, d, iter))
85 dst_layer_dict[iter].append(d)
86
87 src_dict = {k: v for k, v in dst_dict.items() if v.nTargets == 0}
88 dst_dict = {k: v for k, v in dst_dict.items() if v.nTargets > 0}
89 dst_layer_dict[iter] = list(set(dst_layer_dict[iter]))
90 iter += 1
91 if len(src_dict) == 0 or len(dst_dict) == 0:
92 break
93
94print('Last iteration ', iter, 'N src', len(src_dict), 'N dst', len(dst_dict))
95
96# symmetrization
97if not args.no_symmetrize:
98 missing_links = []
99
100 for stage, coll in pair_dict.items():
101 for p1 in coll:
102 if p1.src < 20000:
103 continue
104 if p1.dst < 20000:
105 continue
106
107 src_vol = p1.src // 10000
108 dst_vol = p1.dst // 10000
109
110 skip = src_vol == 8 and dst_vol == 8
111 if skip:
112 continue
113 src_rem1 = p1.src % 10000
114 dst_rem1 = p1.dst % 10000
115
116 isPaired = False
117
118 for p2 in coll:
119 if p1 == p2:
120 continue
121 src_rem2 = p2.src % 10000
122 dst_rem2 = p2.dst % 10000
123 if src_rem1 == src_rem2 and dst_rem1 == dst_rem2:
124 isPaired = True
125 break
126 if isPaired:
127 continue
128 # generate and store missing link
129
130 new_src = -1
131
132 if src_vol == 8:
133 new_src = p1.src
134 elif src_vol == 7:
135 new_src = 90000 + src_rem1
136 elif src_vol == 9:
137 new_src = 70000 + src_rem1
138
139 new_dst = -1
140
141 if dst_vol == 8:
142 new_dst = p1.dst
143 elif dst_vol == 7:
144 new_dst = 90000 + dst_rem1
145 elif dst_vol == 9:
146 new_dst = 70000 + dst_rem1
147
148 missing_links.append([stage, new_src, new_dst])
149
150 for ml in missing_links:
151 pair_dict[ml[0]].append(Pair(ml[1], ml[2], ml[0]))
152
153nConnsTotal = 0
154
155for i, coll in pair_dict.items():
156 nConnsTotal += len(coll)
157
158if args.write_inverse_linking_scheme:
159 link_file = open(args.write_scheme_file, 'w')
160 link_file.write('%d\n' % (len(pair_dict)))
161 for i, coll in pair_dict.items():
162 link_file.write('%d %d\n' % (i, len(coll)))
163 for p in coll:
164 link_file.write('%d %d \n' % (p.src, p.dst))
165
166 link_file.close()
167
168print(f"Write output file {args.output_file} with {nConnsTotal} connections and eta bin width {args.eta_bins}")
169
170bin_table = open(args.output_file, 'w')
171
172bin_table.write(f"{nConnsTotal} {args.eta_bins}\n")
173
174conn_counter = 0
175
176for stage, coll in pair_dict.items():
177 for p in coll:
178 bin_table.write('%d %d %d %d 1 1 100\n' % (conn_counter, stage, p.src, p.dst))
179 bin_table.write('100\n')
180 conn_counter += 1
181
182bin_table.close()
183
184print('all done!')
void print(char *figname, TCanvas *c1)
__init__(self, src, dst, iter)
STL class.