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