ATLAS Offline Software
FPGATrackSimHelperFunctions.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 # Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 
4 import re
5 
6 def convertRegionsExpressionToArray(expression, min_value=0, max_value=1279):
7  # Check if the expression is a single integer
8  if str(expression).isdigit():
9  num = int(expression)
10  if min_value <= num <= max_value:
11  return [num] # Return the single integer as a list
12  else:
13  raise ValueError(f"Invalid choise: {expression}. Number out of range [{min_value}-{max_value}].")
14 
15  # Start with all numbers within min_value to max_value
16  numbers = set(range(min_value, max_value + 1))
17 
18  include_set = set()
19  exclude_set = set()
20 
21  print(f"Initial expression: {str(expression)}")
22  if isinstance(expression, tuple) or isinstance(expression, list):
23  expression = ",".join(map(str, expression))
24 
25  parts = expression.split(",") # Handle single-element expressions
26  print(f"Parts after split: {parts}")
27  for part in parts:
28  part = part.strip()
29 
30  # Exclude regions (e.g. "!5-30")
31  if part.startswith("!"):
32  exclude_set.update(parse_range_or_wildcard(part[1:], min_value, max_value))
33  else:
34  include_set.update(parse_range_or_wildcard(part, min_value, max_value))
35 
36  # Apply inclusion if non-empty; otherwise, use full range
37  result = include_set if include_set else numbers
38  result -= exclude_set # Remove exclusions
39 
40  # Ensure final result stays within valid bounds
41  result = {num for num in result if min_value <= num <= max_value}
42  return list(sorted(result)) # Always return a sorted list
43 
44 def parse_range_or_wildcard(expr, min_value, max_value):
45  # Check for range (e.g., "5-30")
46  if "-" in expr and "*" not in expr:
47  start, end = map(int, expr.split("-"))
48  start, end = max(min_value, start), min(max_value, end) # Apply min/max limits
49  return set(range(start, end + 1))
50 
51  # Check for explicit number
52  if expr.isdigit():
53  num = int(expr)
54  return {num} if min_value <= num <= max_value else set() # Ignore if out of range
55 
56  # Handle wildcards
57  pattern = "^" + expr.replace("*", ".*") + "$"
58  regex = re.compile(pattern)
59 
60  return {num for num in range(min_value, max_value + 1) if regex.match(str(num))}
61 
62 if __name__ == "__main__":
63  # Cases to validate
64  test_cases = [
65  ("5", [5]), # Single number
66  ("5-10", [5, 6, 7, 8, 9, 10]), # Range
67  ("!5-10", list(range(0, 5)) + list(range(11, 1280))), # Exclusion range
68  ("5,10,15", [5, 10, 15]), # Multiple numbers
69  ("5-10,!7", [5, 6, 8, 9, 10]), # Range with exclusion
70  ("*", list(range(1280))), # Wildcard for all
71  ("!5", list(range(0, 5)) + list(range(6, 1280))), # Exclude single number
72  ("5-10,!7-8", [5, 6, 9, 10]), # Range with exclusion range
73  ("1*", [num for num in range(1280) if re.match(r"^1.*$", str(num))]), # Wildcard pattern
74  ("!9*", [num for num in range(1280) if not re.match(r"^9.*$", str(num))]), # Exclude wildcard pattern
75  ]
76 
77  for expression, expected in test_cases:
78  try:
79  result = convertRegionsExpressionToArray(expression)
80  assert result == expected, f"Test failed for {expression}: {result} != {expected}"
81  except Exception as e:
82  print(f"Test failed for {expression}: {e}")
83  import sys
84  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.
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
FPGATrackSimHelperFunctions.convertRegionsExpressionToArray
def convertRegionsExpressionToArray(expression, min_value=0, max_value=1279)
Definition: FPGATrackSimHelperFunctions.py:6
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:194
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
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
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
str
Definition: BTagTrackIpAccessor.cxx:11
FPGATrackSimHelperFunctions.parse_range_or_wildcard
def parse_range_or_wildcard(expr, min_value, max_value)
Definition: FPGATrackSimHelperFunctions.py:44