ATLAS Offline Software
TurnDataReader.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 
3 #
4 # TurnDataReader
5 #
6 # Contents:
7 # TurnDataReader - horrible hack to read in turn data for luminosity scan data from text files
8 # provided by Nitesh. In time this will be replaced by a correct storage of this
9 # data in COOL, but at the moment this is the best we have...
10 #
11 
12 from __future__ import print_function
13 import re
14 import sys
15 
17 
18  def __init__(self, infile=None):
19 
20  self.verbose = False
21 
22  # File to read data from
23  self.infile = infile
24 
25  # dict keyed by pseudoLB number containing dict by algorithm with data Tuple
26  # (turns, counts) = self.data[lb][algName]
27  self.data = None
28 
29  def readData(self):
30 
31  self.data = None
32  if self.infile is None:
33  print('TurnDataReader.readData() called with infile == None!')
34  return self.data
35 
36  # Open input file
37  try:
38  f = open(self.infile, 'r')
39  except IOError as e:
40  print('Error opening file', self.infile)
41  print(e)
42  return self.data
43 
44  # Initialize our new dictionary
45  self.data = dict()
46 
47  # Must parse lines looking for either LB records or algorithm type
48 
49  # pseudoLB record (number followed by two text records
50  matchlb = re.compile(r'^([0-9]+) (\S+) (\S+)$')
51 
52  # algorithm name (only thing on the line)
53  matchalg = re.compile(r'^(\S+)$')
54 
55  # Current algorithm read in file
56  algId = None
57 
58  for line in f.readlines():
59 
60  if self.verbose: print(line, end=' ')
61 
62  m = matchlb.search(line)
63  if m:
64  if self.verbose: print('Found data record match:', m.group(1), m.group(2), m.group(3))
65  lb = int(m.group(1))
66  # Hack to realign these properly. This will probably bite us in the ass someday
67  lb -= 1
68 
69  if m.group(2) == 'nan':
70  counts = 0.
71  else:
72  counts = float(m.group(2))
73 
74  if m.group(3) == 'nan':
75  turns = 0.
76  else:
77  turns = float(m.group(3))
78 
79  # Do we have a valid algorithm?
80  if algId is None:
81  print('TurnDataReader.readData() found data record with no algorithm defined in', self.infile)
82  print(line)
83  sys.exit()
84 
85  # save data
86  if lb not in self.data:
87  self.data[lb] = dict()
88 
89  self.data[lb][algId] = (turns, counts)
90 
91  if self.verbose:
92  print('Alg:', algId, 'LB:', lb, 'Turns:', turns, 'Counts:', counts)
93  continue
94 
95  m = matchalg.search(line)
96  if m:
97  if self.verbose: print('Found new algorithm:', m.group(1))
98 
99  # Decode Nitesh's into my values, just hardcode here
100  algId = m.group(1)
101  if algId is None:
102  print('TurnDataReader.readData() found unrecognized algorithm name in', self.infile)
103  print(line)
104  sys.exit()
105 
106  f.close()
107  return self.data
108 
109 # Test by running from command line
110 if __name__ == '__main__':
111 
112  dr = TurnDataReader('fill1386.txt')
113  dr.verbose = True
114  dr.readData()
python.TurnDataReader.TurnDataReader.data
data
Definition: TurnDataReader.py:27
python.TurnDataReader.TurnDataReader.__init__
def __init__(self, infile=None)
Definition: TurnDataReader.py:18
python.TurnDataReader.TurnDataReader.infile
infile
Definition: TurnDataReader.py:23
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
python.TurnDataReader.TurnDataReader.verbose
verbose
Definition: TurnDataReader.py:20
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
python.TurnDataReader.TurnDataReader.readData
def readData(self)
Definition: TurnDataReader.py:29
Trk::open
@ open
Definition: BinningType.h:40
python.TurnDataReader.TurnDataReader
Definition: TurnDataReader.py:16
readCCLHist.float
float
Definition: readCCLHist.py:83