ATLAS Offline Software
virtual_calculator.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 
3 from logging import getLogger
4 
5 from .virtual_logic import DefectLogic; log = getLogger("DQDefects.virtual_defect_calculator")
6 
7 from collections import defaultdict
8 
9 from DQUtils import process_iovs_changed
10 from DQUtils.sugar import IOVSet, RunLumi
11 
12 from DQDefects import DEFECT_IOV
13 
14 import six
15 
16 from collections.abc import Container
17 from typing import Tuple, Union, Optional, Iterable, Generator, Mapping
18 
19 
20 def generate_virtual_defects(by_channel: Mapping[str, IOVSet], logics: Iterable[DefectLogic],
21  since_cr: Union[int, Tuple[int, int], RunLumi],
22  until_cr: Union[int, Tuple[int, int], RunLumi],
23  ignore: Optional[Container[str]]
24  ) -> Generator[Tuple[RunLumi, RunLumi, Mapping[str, DEFECT_IOV]], None, None]:
25  """
26  An iterator which emits (since, until, {channel name : iov})
27 
28  It also computes the state of the virtual flags for each (since, until)
29  region.
30  """
31  all_channels = list(by_channel.keys()) + [l.name for l in logics]
32  states = dict((channel, None) for channel in all_channels)
33 
34  channels, iovsets = zip(*sorted(six.iteritems(by_channel)))
35 
36  for since, until, current_iovs, changes in process_iovs_changed(*iovsets):
37  # Update things that changed since last iteration in the states dict
38  for change in changes:
39  if ignore is not None and channels[change] in ignore:
40  continue
41  states[channels[change]] = current_iovs[change]
42 
43  # If we're completely outside the calculation range, don't yield iovs
44  if until <= since_cr or since >= until_cr:
45  continue
46 
47  # Update flag calculation result
48  for logic in logics:
49  if ignore and logic.name in ignore:
50  continue
51  states[logic.name] = logic.evaluate(states)
52 
53  yield since, until, states
54 
55 def bad_iov(since: RunLumi, until: RunLumi) -> bool:
56  """
57  Skip some commonly emitted nonsensical IoVs.
58  """
59  return (
60  # Skip 1-lb long iovs on run borders
61  (until - since == 1 and since.run != until.run) or
62  # Skip LB 0->1 IoVs
63  (since.lumi == 0 and until.lumi == 1 and since.run == until.run)
64  )
65 
66 def calculate_virtual_defects(primary_iovs: IOVSet, evaluation_order: Iterable[DefectLogic],
67  virtual_output_channels: Iterable[str], primary_output_channels: Iterable[str],
68  since: Optional[Union[int, Tuple[int, int], RunLumi]],
69  until: Optional[Union[int, Tuple[int, int], RunLumi]],
70  ignore: Optional[Container[str]]) -> IOVSet:
71  """
72  Returns a list of IoVs for a given query in the normal COOL order
73  (sorted by channelID, then by since)
74  """
75 
76  if since is None: since = 0
77  if until is None: until = 2**63-1
78  since, until = RunLumi(since), RunLumi(until)
79 
80  result = defaultdict(IOVSet)
81 
82  primary_by_channel = primary_iovs.by_channel
83 
84  # Copy desired primary channels to the result
85  for primary_channel, primary_iovs in six.iteritems(primary_by_channel):
86  if primary_channel in primary_output_channels:
87  if ignore is not None and primary_channel in ignore:
88  continue
89  result[primary_channel] = primary_iovs
90 
91  args = primary_by_channel, evaluation_order, since, until, ignore
92 
93  # Skip vfgen loop if there is no virtual logic to compute
94  vfgen = [] if not evaluation_order else generate_virtual_defects(*args)
95 
96  for since, until, virtual_iovs in vfgen:
97  if bad_iov(since, until):
98  continue
99 
100  for output_name in virtual_output_channels:
101  # Consider the state of each desired output for this
102  # (since, until) and write it to output_iovs
103  iov = virtual_iovs.get(output_name)
104  if iov and iov.present:
105  result[output_name].add(since, until, *iov[2:])
106 
107  # Sort them by traditional COOL sort ordering (by channelId first,
108  # then by since. `iovs` are already ordered by since.)
109  result_list = IOVSet()
110  for _, iovs in sorted(six.iteritems(result)):
111  result_list.extend(iovs.solidify(DEFECT_IOV))
112 
113  return result_list
python.virtual_calculator.calculate_virtual_defects
IOVSet calculate_virtual_defects(IOVSet primary_iovs, Iterable[DefectLogic] evaluation_order, Iterable[str] virtual_output_channels, Iterable[str] primary_output_channels, Optional[Union[int, Tuple[int, int], RunLumi]] since, Optional[Union[int, Tuple[int, int], RunLumi]] until, Optional[Container[str]] ignore)
Definition: virtual_calculator.py:66
python.events.process_iovs_changed
def process_iovs_changed(*iovsets)
Definition: events.py:74
python.sugar.runlumi.RunLumi
RunLumi
Definition: runlumi.py:131
add
bool add(const std::string &hname, TKey *tobj)
Definition: fastadd.cxx:55
python.virtual_calculator.bad_iov
bool bad_iov(RunLumi since, RunLumi until)
Definition: virtual_calculator.py:55
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
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.virtual_calculator.generate_virtual_defects
Generator[Tuple[RunLumi, RunLumi, Mapping[str, DEFECT_IOV]], None, None] generate_virtual_defects(Mapping[str, IOVSet] by_channel, Iterable[DefectLogic] logics, Union[int, Tuple[int, int], RunLumi] since_cr, Union[int, Tuple[int, int], RunLumi] until_cr, Optional[Container[str]] ignore)
Definition: virtual_calculator.py:20
python.CaloCondLogger.getLogger
def getLogger(name="CaloCond")
Definition: CaloCondLogger.py:16