ATLAS Offline Software
Loading...
Searching...
No Matches
DQUtils/python/grl.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
5from os import listdir
6from os.path import join as pjoin
7import xml.etree.ElementTree as ET
8
9from .events import process_iovs
10from .sugar import define_iov_type, IOVSet, RunLumi, RunLumiType
11
12@define_iov_type
13def GRL_IOV():
14 "Represent a good run region"
15
16def grl_from_dir(xmldir):
17 """
18 Loads valid IOV ranges if they appear in any grl file whose name ends with ".xml"
19 """
20 return load_grl_iovs_any(*(pjoin(xmldir, f)
21 for f in listdir(xmldir) if f.endswith(".xml")))
22
24 """
25 Use IOV ranges from the input `files` xmls if the lumirange is set in any file
26 """
27 grl_iovsets = [grl_iovs_from_xml(f) for f in files]
28
29 assert all(i.ordered for i in grl_iovsets)
30
31 result = IOVSet()
32 for since, until, grl_states in process_iovs(*grl_iovsets):
33 if any(grl_states):
34 result.add(since, until)
35
36 return result.solidify(GRL_IOV)
37
38def load_grl(xml_file, IOVSet_class=IOVSet):
39 with open(xml_file, "rb") as fd:
40 return load_grl_string(fd.read(), IOVSet_class)
41
42def load_grl_string(data, IOVSet_class=IOVSet):
43 xml = ET.fromstring(data)
44 result = []
45
46 for lbc in xml.iter('LumiBlockCollection'):
47 run = int(lbc.find('Run').text)
48 for lbr in lbc.findall('LBRange'):
49 since, until = int(lbr.get('Start')), int(lbr.get('End')) + 1
50
51 result.append((RunLumi(run, since), RunLumi(run, until)))
52
53 return IOVSet_class(map(GRL_IOV._make, sorted(result)))
54
55def make_grl(iovset, name="unknown", version="unknown"):
56 assert len(iovset.channels) <= 1
57
58 from datetime import datetime
59 from textwrap import dedent
60
61 result = [dedent("""
62 <?xml version="1.0" ?>
63 <!DOCTYPE LumiRangeCollection
64 SYSTEM 'http://atlas-runquery.cern.ch/LumiRangeCollection.dtd'>
65 <!-- Good-runs-list created by DQUtils on {time} -->
66 <LumiRangeCollection>
67 <NamedLumiRange>
68 <Name>{name}</Name>
69 <Version>{version}</Version>""".format(
70 name=name, version=version,
71 time=datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))).strip()]
72
73 for run, iovs in sorted(iovset.by_run.items()):
74 result.append(" <LumiBlockCollection>")
75 result.append(" <Run>%i</Run>" % run)
76 for iov in iovs:
77 arg = iov.since.lumi, iov.until.lumi-1
78 result.append(' <LBRange Start="%i" End="%i"/>' % arg)
79 result.append(" </LumiBlockCollection>")
80 result.append(" </NamedLumiRange>")
81 result.append("</LumiRangeCollection>")
82 return "\n".join(result)
83
84def grl_contains_run_lb(grl, runlb):
85 # takes IOVSet of GRL_IOV, says whether runlb is in it
86 # runlb can be RunLumi type or tuple pair
87 if isinstance(runlb, RunLumiType):
88 runlb_ = runlb
89 else:
90 runlb_ = RunLumi(*runlb)
91 return any(_.contains_point(runlb_) for _ in grl)
92
93# Deprecated alias
94def grl_iovs_from_xml(*args, **kwargs):
95 from warnings import warn
96 warn("grl_iovs_from_xml was renamed to load_grl", DeprecationWarning)
97 return load_grl(*args, **kwargs)
98
99def test():
100 path = "/afs/cern.ch/user/b/beate/public/DQAna/StableBeams-periodC1.xml"
101 iovs = grl_iovs_from_xml(path)
102
103 from pprint import pprint
104 pprint(iovs)
105
106if __name__ == "__main__":
107 test()
STL class.
grl_contains_run_lb(grl, runlb)
grl_iovs_from_xml(*args, **kwargs)
load_grl_string(data, IOVSet_class=IOVSet)
make_grl(iovset, name="unknown", version="unknown")
load_grl(xml_file, IOVSet_class=IOVSet)
load_grl_iovs_any(*files)