ATLAS Offline Software
MenuAlignmentTools.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2 import numpy as np
3 from AthenaCommon.Logging import logging
4 import itertools
5 
6 log = logging.getLogger( __name__ )
7 
8 # Warning: If the grouping of signatures changes in *one* of these functions,
9 # it needs to change in ALL of them
10 # They should all group using the same dictionary
11 
12 
13 # this defines the order of the signatures groups to align. The map to the signatures is in the TriggerMenuMT.HLT.Menu.SignatureDicts
14 grouping_order = ['AllTag', 'JetMET', 'MinBias', 'Beamspot', 'MuonnoL1', 'AllProbe']
15 
16 from TriggerMenuMT.HLT.Menu.SignatureDicts import getSignatureGroupingDict
17 the_signature_grouping = getSignatureGroupingDict()
18 
20  return grouping_order
21 
22 def get_alignment_group_from_pattern(signature, extra):
23 
24  signature_for_alignment = signature + extra
25 
26  log.debug("[get_alignment_group_from_pattern] Searching for alignment group for %s",signature_for_alignment)
27 
28  if signature_for_alignment in the_signature_grouping.keys():
29  return the_signature_grouping[signature_for_alignment]
30  elif signature in the_signature_grouping.keys():
31  log.debug("[get_alignment_group_from_pattern] Falling back to signature alignment grouping for %s (%s)",signature, extra)
32  return the_signature_grouping[signature]
33  else:
34  log.debug("[get_alignment_group_from_pattern] No dedicated alignment grouping for signature %s (%s)",signature, extra)
35  return signature
36 
37 def remove_duplicates(config_tuples):
38  # move a list like this [(5, 'Tau'), (6, 'Tau'), (1, 'JetMET')]
39  # .... to this [(6, 'Tau'), (1, 'JetMET')]
40  # one per group, and keep the max length
41  list_of_groups = [x[1] for x in config_tuples]
42 
43  if len(list_of_groups) == len(set(list_of_groups)):
44  return config_tuples
45 
46  else:
47  unique_list = {}
48  for ag_length, ag in config_tuples:
49  if ag not in unique_list or ag_length > unique_list[ag]:
50  unique_list[ag] = ag_length
51 
52  unique_config_tuples = [(ag_length, ag) for ag, ag_length in unique_list.items()]
53  return unique_config_tuples
54 
55 class MenuAlignment():
56  """ Class to hold/calculate chain alignment """
57  def __init__(self, combinations_in_menu, groups_to_align, length_of_configs):
58 
59  self.combinations_in_menu = combinations_in_menu
60  self.length_of_configs = length_of_configs
61  self.groups_to_align = groups_to_align
62 
63  self.signature_dict = {}
65 
66  self.sets_to_align = {}
67 
68  # first we make a dictionary of the ordering, based on the_signature_grouping
69  # e.g. 1 electron+photon, 2 muon, 3 tau, 4 jet/met/b-jet, 5 noL1 muons
70  igrp = 0
71  for value in the_signature_grouping.values():
72  if value not in self.signature_dict:
73  self.signature_dict[value] = igrp
74  igrp += 1
75 
76  self.inverse_sig_dict = {v: k for k, v in self.signature_dict.items()}
77 
78  # Chains can be grouped together, e.g. e chains, mu chains, gamma chains.
79  # Chains are grouped if we want their steps to run in parallel, e.g. if they have shared
80  # sequences, like e+gamma chains use the same calo clustering step.
81  # Each group is called alignment group. These can also split signatures: muons have
82  # two alignment groups, Muon and MuonnoL1 (chainDict['signature']+chainDict['extra'])
83 
84  # With combined chains, e.g. e+mu, we can define in the_signature_grouping if we want
85  # the two legs to run in parallel (put them in the same alignment group, e.g. EgammaMuon),
86  # or in series (to save CPU - don't run slow muons until the electron decision has been made)
87 
88  # The alignment groups have a global ordering defined in the_signature_grouping
89  # But we only want to make all these triggers run in series if the combinations exist
90  # in the menu - i.e. do not put Tau after Egamma and Muon unless there are combined
91  # Tau chains that necessitate this ordering.
92 
93  # Here, we use a list of all the combined chain signature combinations in the menu and the signature
94  # groupings/orderings we've defined in the_signature_grouping to calculate which signatures
95  # need to be ordered compared to which others.
96  # The function returns a dictionary of an ordered list of signatures each signature belongs to
97  # e.g. 'Muon' : ['Egamma','Muon']
98 
99  def analyse_combinations(self): #combinations_in_menu, alignmentGroups_in_combinations):
100 
101  # need to find out of if an alignment group, or anything in combination with that
102  # aligment group, is in combination with any other alignment group.
103 
104  the_matrix = np.eye((len(self.signature_dict)))
105 
106  for comb in self.combinations_in_menu:
107  for comb_pair in list(itertools.combinations(comb,2)):
108  the_matrix[self.signature_dict[comb_pair[0]]][self.signature_dict[comb_pair[1]]] = 1
109  the_matrix[self.signature_dict[comb_pair[1]]][self.signature_dict[comb_pair[0]]] = 1
110 
111  _,eigenvecs = np.linalg.eig(the_matrix)
112  # eigenvecs: The normalized (unit length) eigenvectors, such that the column v[:,i]
113  # is the eigenvector corresponding to the eigenvalue w[i].
114  # so we do the transpose!
115  eigenvecs = np.transpose(eigenvecs)
116 
117  # find the indices filled by each eigenvector, and make a unique list of these
118  pre_unique_sets = list(set([tuple(np.nonzero(eigenvec)[0]) for eigenvec in eigenvecs]))
119 
120  # remove any that are subsets of another (e.g. because something like (1,1,1), (1,-1,1),(0,1,1)
121  # could be an answer but (0,1,1) clearly is not spanning a unique subspace from (1,1,1))
122  unique_sets = []
123  for aset in pre_unique_sets:
124  if len(unique_sets) == 0 :
125  unique_sets +=[aset]
126  else:
127  inlist = True
128  for ibset,bset in enumerate(unique_sets):
129  if aset == bset:
130  inlist = True
131  break
132  elif set(aset).issubset(set(bset)):
133  inlist = True
134  break
135  elif set(bset).issubset(set(aset)):
136  unique_sets.pop(ibset)
137  unique_sets += [aset]
138  inlist = True
139  break
140  else:
141  inlist = False
142  if not inlist:
143  unique_sets +=[aset]
144 
145  # convert the indices back to actual signature names
146  unique_by_sig = [[ self.inverse_sig_dict[sig_int] for sig_int in setlist ] for setlist in unique_sets]
147 
148  sig_to_set = {}
149  for sig in self.groups_to_align:
150  for aset in unique_by_sig:
151  if sig in aset:
152  sig_to_set[sig] = aset
153 
154  self.sets_to_align = sig_to_set
155 
156  return
157 
158  def alignment_group_is_alone(self, alignment_grp):
159  # no need to align lonely alignment groups
160  # the latter condition should never happen, because we only put in alignment groups that are
161  # in combined chains, and thus will need *some* aligning. but good to check in any case.
162  if alignment_grp not in self.sets_to_align:
163  return True
164  elif len(self.sets_to_align[alignment_grp]) == 1:
165  return True
166  return False
167 
168  def alignment_group_is_first(self, alignment_grp):
169 
170  # if it's the first chain in the set to be aligned, again - nothing to do here.
171  if alignment_grp == self.sets_to_align[alignment_grp][0]:
172  return True
173  return False
174 
175  def get_groups_to_align(self, alignment_grp):
176 
177  all_groups_to_align = self.sets_to_align[alignment_grp]
178  index_of_chain_group = self.sets_to_align[alignment_grp].index(alignment_grp)
179 
180  # we don't need to add empty steps for alignment groups that happen *after* the
181  # one our chain belongs to - so just cut off at the chain's alignment group here.
182  return all_groups_to_align[:index_of_chain_group]
183 
184 
185  def single_align(self, chainDict, chainConfig):
186 
187  if len(set(chainDict['alignmentGroups'])) != 1:
188  log.error("Cannot call single_align on chain %s with alignment groups %s",
189  chainDict['chainName'], ",".join(chainDict['alignmentGroups']))
190  raise Exception("Will not proceed, the chain is not suitable for single alignment.")
191 
192  alignment_grp = chainDict['alignmentGroups'][0]
193 
194  if self.alignment_group_is_alone(alignment_grp):
195  log.debug("Finished with retrieving chain configuration for chain %s", chainDict['chainName'])
196  chainConfig.numberAllSteps()
197  elif self.alignment_group_is_first(alignment_grp):
198  # if it's the first chain in the set to be aligned, again - nothing to do here,
199  # since this is single_align
200  log.debug("Finished with retrieving chain configuration for chain %s", chainDict['chainName'])
201  chainConfig.numberAllSteps()
202  else:
203  # now we know that empty steps are necessary before this chain. we can loop through and add accordingly
204  aligngroups_set = self.get_groups_to_align(alignment_grp)
205  # but we want to do this in reverse
206  aligngroups_set.reverse()
207 
208  for align_grp_to_align in aligngroups_set:
209  chainConfig.insertEmptySteps('Empty'+align_grp_to_align+'Align',self.length_of_configs[align_grp_to_align],0)
210  log.debug("Finished with retrieving chain configuration for chain %s", chainDict['chainName'])
211  chainConfig.numberAllSteps()
212 
213  return chainConfig
214 
215  def multi_align(self, chainDict, chainConfig, lengthOfChainConfigs):
216 
217  lengthOfChainConfigs = remove_duplicates(lengthOfChainConfigs)
218 
219  alignment_grps = chainDict['alignmentGroups']
220 
221  #check for a few bad conditions first:
222  if not set(alignment_grps).issubset(self.sets_to_align):
223  log.error(" one of the alignmentGroups in %s is not available in the sets to align dictionary!", alignment_grps)
224  raise Exception("MenuAlignment.analyse_combinations() needs checking, this should never happen.")
225  elif len(set([tuple(self.sets_to_align[x]) for x in alignment_grps])) != 1:
226  log.error(" the alignmentGroups %s point to different sets in the sets to align dictionary", alignment_grps)
227  for x in alignment_grps:
228  log.error(" Set: %s, group %s", self.sets_to_align[x] , x)
229  raise Exception("MenuAlignment.analyse_combinations() needs checking, this should never happen.")
230 
231  #now we know that all alignmentGroups points to the same set, so just use the first entry
232  if len(self.sets_to_align[alignment_grps[0]]) == 2:
233 
234  # if the pair is on its own, then we just make sure the first signature's number
235  # of steps is equal to the max in that signature (so the next signature starts at the right step)
236 
237  # not a dictionary because we could have a chain mu_jet_munoL1? Not sure. But don't want to
238  # overwrite duplicates yet.
239  # probably, at some point, will need to divide this beyond signature but instead as unique sequence within a signature.
240  # munoL1 is already one case...
241  length_firstgrp = 0
242  max_length_firstgrp = self.length_of_configs[self.sets_to_align[alignment_grps[0]][0]]
243 
244  for config_length,config_grp in lengthOfChainConfigs:
245  if config_grp == self.sets_to_align[alignment_grps[0]][0]:
246  length_firstgrp = config_length
247 
248  if length_firstgrp < max_length_firstgrp:
249  #too short! need to add padding steps between two alignment groups...
250  needed_steps = max_length_firstgrp - length_firstgrp
251  chainConfig.insertEmptySteps('Empty'+self.sets_to_align[alignment_grps[0]][0]+'Align',needed_steps,length_firstgrp)
252 
253  elif length_firstgrp > max_length_firstgrp:
254  log.error("%s first signature length %d is greater than the max calculated, %d",chainDict.name,length_firstgrp, max_length_firstgrp)
255  raise Exception("Probably something went wrong in GenerateMenuMT.generateChains()!")
256 
257  #this should probably work for signatures > 2, but might be a few gotchas (and errors need updating)
258  #Can't properly test until ATR-22206 is resolved.
259  elif len(self.sets_to_align[alignment_grps[0]]) > 2:
260  if not set(alignment_grps).issubset(self.sets_to_align):
261  log.error(" one of the alignmentGroups in %s is not available in the sets to align dictionary!", alignment_grps)
262  raise Exception("MenuAlignment.analyse_combinations() needs checking, this should never happen.")
263  elif len(set([tuple(self.sets_to_align[x]) for x in alignment_grps])) != 1:
264  log.error(" the alignmentGroups %s point to different sets in the sets to align dictionary", alignment_grps)
265  for x in alignment_grps:
266  log.error(" Set: %s, group %s", self.sets_to_align[x] , x)
267  raise Exception("MenuAlignment.analyse_combinations() needs checking, this should never happen.")
268 
269  # first, we need to convert alignment_grps from the order it is based on the chain naming
270  # convention into the order it needs to be for the alignment
271  # these are not always the same thing!
272 
273  # takes the values of the ordering dictionary and creates a list of unique values
274  # so it isn't just e.g. [egamma, egamma, egamma, JetMET, JetMET] but actually
275  # [egamma, JetMET]
276  alignment_grp_ordering = get_alignment_group_ordering()
277  alignment_grps_ordered = [x for x in alignment_grp_ordering if x in alignment_grps]
278 
279  # we need to know which alignment_grps are in the chain in which order. Assume this is always stored correctly.
280  # (this should be true, once it is sorted! If not, there is a bug.)
281  # never need to add empty steps to the last leg - it can end at a different
282  # time (be a different number of steps) - no problem.
283  # ignore any signatures after the end of those in this chain
284  aligngroups_set = self.get_groups_to_align(alignment_grps_ordered[-1])
285  aligngroups_set.reverse()
286  grp_masks = [x in alignment_grps_ordered for x in aligngroups_set]
287  grp_lengths = []
288  for align_grp,grp_in_chain in zip(aligngroups_set,grp_masks):
289  if grp_in_chain:
290  for config_length,config_grp in lengthOfChainConfigs:
291  if config_grp == align_grp:
292  grp_lengths += [config_length]
293  else:
294  grp_lengths += [0]
295 
296  for istep,(align_grp,grp_in_chain,length_in_chain) in enumerate(zip(aligngroups_set,grp_masks,grp_lengths)):
297  # We're working our way backwards through the chain
298  # need to know how many steps are already before us!
299  n_steps_before_grp = 0
300  if istep < len(grp_lengths)-1:
301  n_steps_before_grp = sum(grp_lengths[istep+1:])
302  max_length_grp = self.length_of_configs[align_grp]
303  if grp_in_chain:
304  if length_in_chain < max_length_grp:
305  #too short! gotta add padding steps between two alignmentGroups...
306  needed_steps = max_length_grp - length_in_chain
307  start_step = n_steps_before_grp + length_in_chain
308  chainConfig.insertEmptySteps('Empty'+align_grp+'Align',needed_steps,start_step)
309  else:
310  # this sig isn't in the chain, but we still will need empty steps for it
311  # always add them to the start, because we're running in reverse order
312  chainConfig.insertEmptySteps('Empty'+align_grp+'Align',self.length_of_configs[align_grp],n_steps_before_grp)
313  else:
314  log.error("Should never reach this point. Ordered alignmentGroups: %s, sets_to_align: %s",alignment_grps_ordered,self.sets_to_align)
315  raise Exception("MenuAlignment.multi_align() needs checking, this should never happen.")
316 
317  log.debug("Finished with retrieving chain configuration for chain %s", chainDict['chainName'])
318  chainConfig.numberAllSteps()
319 
320  return chainConfig
index
Definition: index.py:1
MenuAlignmentTools.MenuAlignment.analyse_combinations
def analyse_combinations(self)
Definition: MenuAlignmentTools.py:99
MenuAlignmentTools.MenuAlignment.sets_to_align
sets_to_align
Definition: MenuAlignmentTools.py:66
MenuAlignmentTools.MenuAlignment.signature_dict
signature_dict
Definition: MenuAlignmentTools.py:63
MenuAlignmentTools.MenuAlignment.alignment_group_is_alone
def alignment_group_is_alone(self, alignment_grp)
Definition: MenuAlignmentTools.py:158
MenuAlignmentTools.MenuAlignment
Definition: MenuAlignmentTools.py:55
MenuAlignmentTools.MenuAlignment.__init__
def __init__(self, combinations_in_menu, groups_to_align, length_of_configs)
Definition: MenuAlignmentTools.py:57
MenuAlignmentTools.MenuAlignment.single_align
def single_align(self, chainDict, chainConfig)
Definition: MenuAlignmentTools.py:185
convertTimingResiduals.sum
sum
Definition: convertTimingResiduals.py:55
MenuAlignmentTools.remove_duplicates
def remove_duplicates(config_tuples)
Definition: MenuAlignmentTools.py:37
MenuAlignmentTools.get_alignment_group_ordering
def get_alignment_group_ordering()
Definition: MenuAlignmentTools.py:19
MenuAlignmentTools.MenuAlignment.get_groups_to_align
def get_groups_to_align(self, alignment_grp)
Definition: MenuAlignmentTools.py:175
MenuAlignmentTools.MenuAlignment.multi_align
def multi_align(self, chainDict, chainConfig, lengthOfChainConfigs)
Definition: MenuAlignmentTools.py:215
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
MenuAlignmentTools.MenuAlignment.groups_to_align
groups_to_align
Definition: MenuAlignmentTools.py:61
python.HLT.Menu.SignatureDicts.getSignatureGroupingDict
def getSignatureGroupingDict()
Definition: SignatureDicts.py:53
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
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
MenuAlignmentTools.MenuAlignment.length_of_configs
length_of_configs
Definition: MenuAlignmentTools.py:60
TrigJetMonitorAlgorithm.items
items
Definition: TrigJetMonitorAlgorithm.py:71
MenuAlignmentTools.get_alignment_group_from_pattern
def get_alignment_group_from_pattern(signature, extra)
Definition: MenuAlignmentTools.py:22
MenuAlignmentTools.MenuAlignment.combinations_in_menu
combinations_in_menu
Definition: MenuAlignmentTools.py:59
MenuAlignmentTools.MenuAlignment.alignment_group_is_first
def alignment_group_is_first(self, alignment_grp)
Definition: MenuAlignmentTools.py:168
MenuAlignmentTools.MenuAlignment.inverse_sig_dict
inverse_sig_dict
Definition: MenuAlignmentTools.py:64