ATLAS Offline Software
ros-hitstats-to-json.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
4 #
5 
6 # This script parses outputs of HLTMPPU ROS hit simulation, converts them
7 # to JSON and appends to extra-results.json
8 
9 import logging
10 import re
11 import sys
12 import os.path
13 import json
14 from collections import OrderedDict
15 from enum import IntEnum
16 
17 
18 class LastUpdatedOrderedDict(OrderedDict):
19  'Store items in the order the keys were last added'
20 
21  def __setitem__(self, key, value):
22  if key in self:
23  del self[key]
24  OrderedDict.__setitem__(self, key, value)
25 
26 
27 class LookUpDict(IntEnum):
28  'Defines columns in hitstats file'
29  ROS_NAME = 0
30  NUM_ROBS = 1
31  TOTAL_HITS_PER_EVT = 2
32  TOTAL_ROB_FRAC_PER_EVT = 3
33  TOTAL_BYTES_PER_EVT = 4
34  NOBLD_HITS_PER_EVT = 5
35  NOBLD_ROB_FRAC_PER_EVT = 6
36  NOBLD_BYTES_PER_EVT = 7
37  EVBLD_HITS_PER_EVT = 8
38  EVBLD_ROB_FRAC_PER_EVT = 9
39  EVBLD_BYTES_PER_EVT = 10
40 
41 
42 def get_lines(pattern, filename):
43  if not os.path.isfile(filename):
44  logging.warning("Cannot open file {}".format(filename))
45  return None
46  with open(filename) as logfile:
47  lines = re.findall("{}.*$".format(pattern),
48  logfile.read(), re.MULTILINE)
49  if len(lines) == 0:
50  logging.warning("Could not find pattern \"{}\" in file {}".format(pattern, filename))
51  return None
52  return lines
53 
54 
55 def split_lines(pattern, lines):
56  result = []
57  for line in lines:
58  split_result = []
59  for token in re.split(pattern, line):
60  token = token.replace(' ', '')
61  if not token == '':
62  split_result.append(token)
63  result.append(split_result)
64  return result
65 
66 
67 def main():
68  logging.basicConfig(stream=sys.stdout,
69  format='%(levelname)-8s %(message)s',
70  level=logging.INFO)
71 
72  ros_stats = LastUpdatedOrderedDict()
73 
74  lines = get_lines(r'^\s*ROS-', 'ros_hitstats_reject.txt')
75  table = split_lines(r'\||,', lines)
76 
77  for row in table:
78  name = row[LookUpDict.ROS_NAME]
79  hit_rate = row[LookUpDict.TOTAL_ROB_FRAC_PER_EVT]
80  data_rate = row[LookUpDict.TOTAL_BYTES_PER_EVT]
81  ros_stats[name] = LastUpdatedOrderedDict({
82  'hits-per-evt': hit_rate,
83  'kbytes-per-evt': float(data_rate)/1000
84  })
85 
86  data = LastUpdatedOrderedDict()
87  output_file = 'extra-results.json'
88  if os.path.isfile(output_file):
89  with open(output_file) as f:
90  data.update(json.load(f, object_pairs_hook=LastUpdatedOrderedDict))
91 
92  data.update({'ros-stats': ros_stats})
93 
94  with open(output_file, 'w') as f:
95  json.dump(data, f, indent=4)
96 
97 
98 if "__main__" in __name__:
99  sys.exit(main())
ros-hitstats-to-json.main
def main()
Definition: ros-hitstats-to-json.py:67
ros-hitstats-to-json.split_lines
def split_lines(pattern, lines)
Definition: ros-hitstats-to-json.py:55
vtune_athena.format
format
Definition: vtune_athena.py:14
ros-hitstats-to-json.LookUpDict
Definition: ros-hitstats-to-json.py:27
Trk::open
@ open
Definition: BinningType.h:40
ros-hitstats-to-json.LastUpdatedOrderedDict
Definition: ros-hitstats-to-json.py:18
ros-hitstats-to-json.LastUpdatedOrderedDict.__setitem__
def __setitem__(self, key, value)
Definition: ros-hitstats-to-json.py:21
readCCLHist.float
float
Definition: readCCLHist.py:83
ros-hitstats-to-json.get_lines
def get_lines(pattern, filename)
Definition: ros-hitstats-to-json.py:42