ATLAS Offline Software
LumiGRLParser.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 
3 #
4 # LumiGRLParser.py
5 #
6 # Utility to parse GRL XML file and provide access to useful information in python
7 #
8 from __future__ import print_function
9 import xml.etree.cElementTree as ET
10 
12 
13  def __init__(self, grlfile=None):
14 
15  # Holds the element tree for the parsed XML file
16  self.tree = None
17 
18  # Name of NamedLumiRange to consider (None works if only one NamedLumiRange is present)
19  self.lumiRangeName = None
20  self.lumiRange = None
21 
22  # List of (integer) run numbers
23  self.runList = []
24 
25  # List of COOL IOV ranges (start, end)
26  # run << 32 & LB
27  self.iovList = []
28 
29  # Dictionary keyed by runnum containing a list of IOV ranges
30  self.iovDict = dict()
31 
32  if grlfile is not None:
33  self.parseFile(grlfile)
34 
35 
36  def parseFile(self, filename):
37 
38  self.tree = ET.parse(filename)
39 
40  self.runList = []
41  self.iovList = []
42  self.iovDict = dict()
43 
44  # Return list of runs in GRL
45  if not self.selectRange():
46  print('LumiGRLParser.parseFile(%s) - Error selecting valid range!' % filename)
47  return
48 
49  # Now, go through this range and find run numbers
50  for lbc in self.lumiRange.findall('LumiBlockCollection'):
51  runnum = int(lbc.findtext('Run', '-1'))
52  if runnum < 0:
53  print("LumiGRLParser.parseFile(%s) - Couldn't find Run in valid LumiBlockCollection!" % filename)
54  continue
55 
56  self.runList.append(runnum)
57  self.iovDict[runnum] = []
58 
59  for lbr in lbc.findall('LBRange'):
60  lbstart = int(lbr.get('Start', '-1'))
61  lbend = int(lbr.get('End', '-1'))
62 
63  if lbstart < 0 or lbend < 0:
64  print("LumiGRLParser.parseFile(%s) - Couldn't find LBRange attributes for run %d!" % (filename, runnum))
65  continue
66 
67  # Must add one to make IOVRange exclusive at end
68  lbend += 1
69  self.iovDict[runnum].append((lbstart, lbend))
70 
71  iovstart = (runnum<<32)+lbstart
72  iovend = (runnum<<32)+lbend
73  self.iovList.append((iovstart, iovend))
74 
75  # Finally sort these
76  self.runList.sort()
77  self.iovList.sort()
78 
79  def selectRange(self):
80 
81  self.lumiRange = None
82 
83  if self.tree is None:
84  print('LumiGRLParser.selectRange() - no tree found!')
85  return False
86 
87  lumiRangeList = self.tree.findall('NamedLumiRange')
88  if len(lumiRangeList) == 1:
89 
90  if self.lumiRangeName is not None:
91  name = lumiRangeList[0].findtext('Name', '')
92 
93  if name != self.lumiRangeName:
94  print("LumiGRLParser.selectRange() - Can't find %s in GRL, only %s!" % (self.lumiRangeName, name))
95  return False
96 
97  self.lumiRange = lumiRangeList[0]
98  return True
99 
100  elif len(lumiRangeList) == 0:
101 
102  print('LumiGRLParser.selectRange() - No NamedLumiRange object found!')
103  return False
104 
105  # More than one range found
106  if self.lumiRangeName is None:
107  print('LumiGRLParser.selectRange() - %d NamedLumiRange objects found, but no lumiRangeName specified!' % len(lumiRangeList))
108  return False
109 
110 
111  # More than one, match by name
112  found = False
113  for lbr in lumiRangeList:
114  name = lbr.findtext('Name', '')
115 
116  if self.lumiRangeName == name:
117  found = True
118  self.lumiRange = lbr
119  break
120 
121  if not found:
122  print("LumiGRLParser.selectRange() - Couldn't find %s in available NamedLumiRange objects!" % self.lumiRangeName)
123  return False
124 
125  return True
126 
127  # Return sorted list of lumi blocks present in specified run
128  def lumiBlockList(self, runnum):
129 
130  lbList = []
131  for iov in self.iovDict.get(runnum, []):
132  lbList.extend(range(iov[0], iov[1]))
133 
134  return lbList
135 
python.LumiGRLParser.LumiGRLParser.parseFile
def parseFile(self, filename)
Definition: LumiGRLParser.py:36
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
python.LumiGRLParser.LumiGRLParser.lumiRangeName
lumiRangeName
Definition: LumiGRLParser.py:19
python.LumiGRLParser.LumiGRLParser.lumiBlockList
def lumiBlockList(self, runnum)
Definition: LumiGRLParser.py:128
python.LumiGRLParser.LumiGRLParser
Definition: LumiGRLParser.py:11
python.LumiGRLParser.LumiGRLParser.tree
tree
Definition: LumiGRLParser.py:16
python.LumiGRLParser.LumiGRLParser.__init__
def __init__(self, grlfile=None)
Definition: LumiGRLParser.py:13
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
python.LumiGRLParser.LumiGRLParser.selectRange
def selectRange(self)
Definition: LumiGRLParser.py:79
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70
python.LumiGRLParser.LumiGRLParser.lumiRange
lumiRange
Definition: LumiGRLParser.py:20
python.LumiGRLParser.LumiGRLParser.iovList
iovList
Definition: LumiGRLParser.py:27
python.LumiGRLParser.LumiGRLParser.iovDict
iovDict
Definition: LumiGRLParser.py:30
python.LumiGRLParser.LumiGRLParser.runList
runList
Definition: LumiGRLParser.py:23