ATLAS Offline Software
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 
5 from os import listdir
6 from os.path import join as pjoin
7 import xml.etree.cElementTree as cElementTree
8 
9 from .events import process_iovs
10 from .sugar import define_iov_type, IOVSet, RunLumi, RunLumiType
11 
12 @define_iov_type
13 def GRL_IOV():
14  "Represent a good run region"
15 
16 def 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 
23 def load_grl_iovs_any(*files):
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 
38 def 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 
42 def load_grl_string(data, IOVSet_class=IOVSet):
43  xml = cElementTree.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 
55 def 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 
84 def 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
94 def 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 
99 def 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 
106 if __name__ == "__main__":
107  test()
python.grl.load_grl
def load_grl(xml_file, IOVSet_class=IOVSet)
Definition: DQUtils/python/grl.py:38
vtune_athena.format
format
Definition: vtune_athena.py:14
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
python.dummyaccess.listdir
def listdir(dirname)
Definition: dummyaccess.py:6
python.grl.load_grl_string
def load_grl_string(data, IOVSet_class=IOVSet)
Definition: DQUtils/python/grl.py:42
Cut::all
@ all
Definition: SUSYToolsAlg.cxx:67
python.grl.test
def test()
Definition: DQUtils/python/grl.py:99
python.sugar.runlumi.RunLumi
RunLumi
Definition: runlumi.py:131
python.grl.load_grl_iovs_any
def load_grl_iovs_any(*files)
Definition: DQUtils/python/grl.py:23
python.grl.grl_iovs_from_xml
def grl_iovs_from_xml(*args, **kwargs)
Definition: DQUtils/python/grl.py:94
python.events.process_iovs
def process_iovs(*iovsets)
Definition: events.py:30
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename T::value_type > sorted(T begin, T end)
Helper function to create a sorted vector from an unsorted one.
python.grl.grl_from_dir
def grl_from_dir(xmldir)
Definition: DQUtils/python/grl.py:16
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
ReadBadBitsFromCool.warn
warn
Definition: ReadBadBitsFromCool.py:43
Trk::open
@ open
Definition: BinningType.h:40
python.grl.make_grl
def make_grl(iovset, name="unknown", version="unknown")
Definition: DQUtils/python/grl.py:55
python.grl.grl_contains_run_lb
def grl_contains_run_lb(grl, runlb)
Definition: DQUtils/python/grl.py:84
python.grl.GRL_IOV
def GRL_IOV()
Definition: DQUtils/python/grl.py:13