ATLAS Offline Software
ros2rob_from_partition.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
4 
5 from __future__ import print_function
6 import eformat
7 from pm.project import Project
8 from argparse import ArgumentParser
9 from types import MethodType
10 from sys import stdout, stderr
11 from datetime import datetime
12 
13 # get the arg parser
14 def argparser():
15  parser = ArgumentParser(description='Produce a ros2rob map, as a python '
16  'dictionary, from a given partition.')
17  parser.add_argument('--database_file', '-d', required=True,
18  help='A partition filename (e.g. ATLAS.data.xml).')
19  parser.add_argument('--partition', '-p', required=True,
20  help='A partition filename. The name of the partition '
21  'that is read from pfile (e.g. ATLAS).')
22  parser.add_argument('--output_file', '-o',
23  help='The output filename. The name of the file to which '
24  'the ros2rob map is written. If omitted, stdout is '
25  'used (e.g. myros2rob.py).')
27  return parser
28 
30  """
31  Alter an ArgumentParser so that it shows a help msg whenever there is an
32  error in the command line
33  """
34  def error(self, msg):
35  print('error: %s\n' % msg, file=stderr)
36  self.print_help()
37  exit(2)
38  parser.error = MethodType(error, parser)
39 
40 def get_roses(pfname, pname):
41  """
42  Get all the ROSes and swRODs in the partition
43  """
44  partition = Project(pfname).getObject('Partition', pname)
45  roses = partition.get('ROS')
46  swrods = partition.get('SwRodApplication')
47  return roses + swrods
48 
49 def get_ros2rob(roses):
50  """
51  Get the ros2rob map from the ROS list
52  """
53  ros2rob = {}
54  for ros in roses:
55  if ros.id in ros2rob:
56  print("WARNING: %s is repeated in the partition: ignoring "
57  "second occurrence", file=stderr)
58  else:
59  ros2rob[ros.id] = get_robs(ros)
60  ros2rob['RoIBuilder'] = get_roib_robs()
61  return ros2rob
62 
63 def get_robs(ros):
64  """
65  Get the list of ROBs that correspond to a ROS
66  """
67  return [eformat.helper.SourceIdentifier(rol.Id).code()
68  for robin in ros.Contains for rol in robin.Contains]
69 
71  """
72  Get a hardcoded list of RoIBuilder ROB IDs which are not listed in the partition
73  """
74  return [
75  # CTP
76  0x770001,
77  # L1Calo
78  0x7300a8, 0x7300a9, 0x7300aa, 0x7300ab, 0x7500ac, 0x7500ad,
79  # L1Topo
80  0x910081, 0x910091, 0x910082, 0x910092]
81 
82 def print_ros2rob(ros2rob, out):
83  """
84  Print the ros2rob map as an easily readable/editable python dictionary
85  """
86  print("ros2rob = {", file=out)
87  count = 0
88  for k, v in ros2rob.items():
89  count += 1
90  print("\t'%s': \n\t[" % k, file=out)
91  for i in range(len(v)):
92  print("\t\t%s" % hex(v[i]), end=' ', file=out)
93  if i+1 != len(v):
94  print(",", file=out)
95  else:
96  print("\n\t]", end=' ', file=out)
97  if count != len(ros2rob):
98  print(",", file=out)
99  print("\n}", file=out)
100 
101 def print_header(dbfile, outname, out):
102  header = f"""
103 #
104 # Copyright (C) 2002-{datetime.now().year} CERN for the benefit of the ATLAS collaboration
105 #
106 '''
107 @file {outname}
108 @brief Store ROS to ROB map extracted from {dbfile}
109 '''
110 """
111  print(header, file=out)
112 
113 def print_footer(out):
114  footer = """
115 class ROSToROBMap:
116 \tdef __init__(self):
117 \t\tself.data = ros2rob
118 
119 \tdef get_mapping(self):
120 \t\treturn self.data
121 """
122  print(footer, file=out)
123 
124 # main
125 if __name__ == '__main__':
127  out = open(args.output_file, 'w') if args.output_file else stdout
128  print("# Extracting ROS2ROB map", file=stderr)
129  print_header(args.database_file, args.output_file, out)
130  print_ros2rob(get_ros2rob(get_roses(args.database_file, args.partition)), out)
131  print_footer(out)
132 
ros2rob_from_partition.get_roib_robs
def get_roib_robs()
Definition: ros2rob_from_partition.py:70
ros2rob_from_partition.print_ros2rob
def print_ros2rob(ros2rob, out)
Definition: ros2rob_from_partition.py:82
ros2rob_from_partition.get_roses
def get_roses(pfname, pname)
Definition: ros2rob_from_partition.py:40
NSWL1::Project
Polygon Project(const Polygon &p, float Zinit, float Zfin)
Definition: GeoUtils.cxx:19
ros2rob_from_partition.argparser
def argparser()
Definition: ros2rob_from_partition.py:14
histSizes.code
code
Definition: histSizes.py:129
ros2rob_from_partition.make_parser_print_help_on_error
def make_parser_print_help_on_error(parser)
Definition: ros2rob_from_partition.py:29
ros2rob_from_partition.print_footer
def print_footer(out)
Definition: ros2rob_from_partition.py:113
ros2rob_from_partition.get_ros2rob
def get_ros2rob(roses)
Definition: ros2rob_from_partition.py:49
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
calibdata.exit
exit
Definition: calibdata.py:236
ros2rob_from_partition.get_robs
def get_robs(ros)
Definition: ros2rob_from_partition.py:63
Trk::open
@ open
Definition: BinningType.h:40
ros2rob_from_partition.print_header
def print_header(dbfile, outname, out)
Definition: ros2rob_from_partition.py:101
confTool.parse_args
def parse_args()
Definition: confTool.py:35
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70
get_generator_info.error
error
Definition: get_generator_info.py:40