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