From 4ad7a9594c89e100be3d90f5edac7e1651fcc169 Mon Sep 17 00:00:00 2001 From: "Wood, Tony" Date: Thu, 28 May 2026 15:31:09 -0400 Subject: [PATCH 1/2] Rework to allow construction of lattice from non-neighboring XML sections. --- .../sns_linac_lattice_factory.py | 57 ++++++++++--------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/py/orbit/py_linac/linac_parsers/sns_linac_lattice_factory.py b/py/orbit/py_linac/linac_parsers/sns_linac_lattice_factory.py index 8d5ff039..1049ae66 100644 --- a/py/orbit/py_linac/linac_parsers/sns_linac_lattice_factory.py +++ b/py/orbit/py_linac/linac_parsers/sns_linac_lattice_factory.py @@ -10,6 +10,7 @@ import os import sys import math +import collections.abc # import the XmlDataAdaptor XML parser from orbit.utils.xml import XmlDataAdaptor @@ -447,34 +448,38 @@ def positionComp(node_da): linacAccLattice.initialize() return linacAccLattice - def filterSequences_and_OptionalCheck(self, accSeq_da_arr, names): + def filterSequences_and_OptionalCheck( + self, + accSeq_da_arr: collections.abc.Sequence[XmlDataAdaptor], + names: collections.abc.Sequence[str] + ) -> collections.abc.Sequence[XmlDataAdaptor]: """ - This method will filter the sequences according to names list - and check the order of sequences in names. - All sequences should be in the right order. For SNS linac is - just a linac. It returns the filtered array with data adapters - with the names in the names array. + Filter sequence adaptors by name and preserve the requested order. + + Parameters + ---------- + accSeq_da_arr : collections.abc.Sequence[XmlDataAdaptor] + Available sequence data adaptors. + names : collections.abc.Sequence[str] + Sequence names to select, in the required output order. + + Returns + ------- + collections.abc.Sequence[XmlDataAdaptor] + Sequence adaptors matching ``names``, returned in the same order + as provided in ``names``. """ - seqencesLocal = accSeq_da_arr - seqencesLocalNames = [] - for seq_da in seqencesLocal: - seqencesLocalNames.append(seq_da.getName()) - ind_old = -1 - count = 0 - for name in names: - ind = seqencesLocalNames.index(name) - if ind < 0 or (count > 0 and ind != (ind_old + 1)): - msg = "The LinacLatticeFactory method getLinacAccLattice(names): sequence names array is wrong!" - msg = msg + os.linesep - msg = msg + "existing names=" + str(seqencesLocalNames) - msg = msg + os.linesep - msg = msg + "sequence names=" + str(names) - orbitFinalize(msg) - ind_old = ind - count += 1 - ind_start = seqencesLocalNames.index(names[0]) - accSeq_da_arr = accSeq_da_arr[ind_start : ind_start + len(names)] - return accSeq_da_arr + sequence_map = {seq_da.getName(): seq_da for seq_da in accSeq_da_arr} + missing_sequences = [n for n in names if n not in sequence_map] + if missing_sequences: + msg = os.linesep.join([ + "The LinacLatticeFactory method getLinacAccLattice(names): sequence names array is wrong!", + f"existing names={seqencesLocalNames}", + f"sequence names={names}", + ]) + orbitFinalize(msg) + return [sequence_map[n] for n in names] + def makeDataAdaptorforLinacLattice(self, linacAccLattice): """ From 2f744101864cfe0ae7255d0c36c2659e52d98229 Mon Sep 17 00:00:00 2001 From: "Wood, Tony" Date: Thu, 28 May 2026 15:48:00 -0400 Subject: [PATCH 2/2] Rename variable --- py/orbit/py_linac/linac_parsers/sns_linac_lattice_factory.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/orbit/py_linac/linac_parsers/sns_linac_lattice_factory.py b/py/orbit/py_linac/linac_parsers/sns_linac_lattice_factory.py index 1049ae66..e574a092 100644 --- a/py/orbit/py_linac/linac_parsers/sns_linac_lattice_factory.py +++ b/py/orbit/py_linac/linac_parsers/sns_linac_lattice_factory.py @@ -474,7 +474,7 @@ def filterSequences_and_OptionalCheck( if missing_sequences: msg = os.linesep.join([ "The LinacLatticeFactory method getLinacAccLattice(names): sequence names array is wrong!", - f"existing names={seqencesLocalNames}", + f"existing names={list(sequence_map)}", f"sequence names={names}", ]) orbitFinalize(msg)