Belle II Software  release-08-01-10
local_metadata_provider.py
1 #!/usr/bin/env python3
2 
3 
10 
11 import basf2 as b2 # noqa
12 from conditions_db.local_metadata import LocalMetadataProvider
13 from conditions_db import PayloadInformation
14 from b2test_utils import clean_working_directory
15 import ROOT
16 from ROOT import Belle2 # noqa
17 
18 ROOT.gInterpreter.Declare("#include <framework/database/LocalMetadataProvider.h>")
19 
20 
22  """
23  Simple class to obtain all valid payloads matching a given set of names for
24  a given experiment and run and compare the python and the C++
25  implementation.
26  """
27  @staticmethod
28  def create_cpp_metadataprovider(filename, globaltags):
29  """
30  Jump through some hoops to create a C++ instance of the local metadata
31  provider so that we can check the framework implementation and compare
32  it to the python version
33  """
34  states = ROOT.std.set("string")()
35  states.insert("PUBLISHED")
36  tags = ROOT.std.vector("string")()
37  for tag in globaltags:
38  tags.push_back(tag)
39  localcpp = Belle2.Conditions.LocalMetadataProvider(filename, states)
40  localcpp.setTags(tags)
41  return localcpp
42 
43  def __init__(self, filename, globaltag, payloads):
44  """Create the local metadata providers for the given globaltag and payload names"""
45 
46  self._globaltag_globaltag = globaltag
47 
48  self._payloads_payloads = payloads
49 
50  self._localpy_localpy = LocalMetadataProvider(filename)
51 
52  self._localcpp_localcpp = self.create_cpp_metadataprovidercreate_cpp_metadataprovider(filename, [globaltag])
53 
54  def check(self, exp, run):
55  """Check if the c++ and python version return the same payload revisions
56  If not raise a ValueError, if yes return the list of payloads and their
57  revision valid for the given exp,run"""
58  from_py = [(p.name, p.revision) for p in self._localpy_localpy.get_all_iovs(self._globaltag_globaltag, exp=exp, run=run)
59  if p.name in self._payloads_payloads]
60  info = ROOT.std.vector("Belle2::Conditions::PayloadMetadata")()
61  for name in self._payloads_payloads:
62  info.push_back(Belle2.Conditions.PayloadMetadata(name, False))
63  self._localcpp_localcpp.getPayloads(exp, run, info)
64  from_cpp = [(p.name, p.revision) for p in info
65  if p.revision > 0]
66  from_py.sort()
67  from_cpp.sort()
68  if from_py != from_cpp:
69  raise ValueError(f"Payloads not equal: {from_py} != {from_cpp}")
70  return from_py
71 
72 
73 if __name__ == "__main__":
74  with clean_working_directory() as dirname:
75  # so we can easily create multiple globaltags in a local file to check different iovs
76  local = LocalMetadataProvider("testdatabase.sqlite", "overwrite")
77  local.add_globaltag(1, "test1", "PUBLISHED", [
78  PayloadInformation(1, "test", 1, "", "", "", None, (45, 221, 49, -1))
79  ])
80  # check a single run validity
81  local.add_globaltag(2, "test2", "PUBLISHED", [
82  PayloadInformation(1, "test", 1, "", "", "", None, (0, 1, 0, 1))
83  ])
84  # and unlimited runs in same experiment
85  local.add_globaltag(3, "test3", "PUBLISHED", [
86  PayloadInformation(1, "test", 1, "", "", "", None, (1, 1, 1, -1))
87  ])
88  # or also the first run in the next experiment
89  local.add_globaltag(4, "test4", "PUBLISHED", [
90  PayloadInformation(1, "test", 1, "", "", "", None, (1, 1, 2, 0))
91  ])
92  # or an unbound iov
93  local.add_globaltag(5, "test5", "PUBLISHED", [
94  PayloadInformation(1, "test", 1, "", "", "", None, (1, 1, -1, -1))
95  ])
96 
97  comparer = LocalMetadataComparison("testdatabase.sqlite", "test1", ["test"])
98  for exp, run in [(44, 1), (44, 9999), (45, 0), (45, 220), (45, 221), (45, 9999), (46, 0), (46, 221),
99  (49, 0), (49, 9999), (50, 0), (50, 221), (50, 9999)]:
100  valid = [("test", 1)] if (45, 221) <= (exp, run) < (50, 0) else []
101  assert comparer.check(exp, run) == valid, f"Couldn't find payload for {exp},{run}"
102 
103  comparer = LocalMetadataComparison("testdatabase.sqlite", "test2", ["test"])
104  for exp, run in [(0, 0), (0, 1), (0, 2), (0, 9999),
105  (1, 0), (1, 1), (1, 2), (1, 9999), (9999, 9999)]:
106  valid = [("test", 1)] if (exp, run) == (0, 1) else []
107  assert comparer.check(exp, run) == valid, f"Couldn't find payload for {exp},{run}"
108 
109  comparer = LocalMetadataComparison("testdatabase.sqlite", "test3", ["test"])
110  for exp, run in [(0, 0), (0, 1), (0, 2), (0, 9999),
111  (1, 0), (1, 1), (1, 2), (1, 9999),
112  (243, 0), (245, 9999999), (9999, 9999)]:
113  valid = [("test", 1)] if (1, 1) <= (exp, run) < (2, 0) else []
114  assert comparer.check(exp, run) == valid, f"Couldn't find payload for {exp},{run}"
115 
116  comparer = LocalMetadataComparison("testdatabase.sqlite", "test4", ["test"])
117  for exp, run in [(0, 0), (0, 1), (0, 2), (0, 9999),
118  (1, 0), (1, 1), (1, 2), (1, 9999),
119  (243, 0), (245, 9999999), (9999, 9999)]:
120  valid = [("test", 1)] if (1, 1) <= (exp, run) <= (2, 0) else []
121  assert comparer.check(exp, run) == valid, f"Couldn't find payload for {exp},{run}"
122 
123  comparer = LocalMetadataComparison("testdatabase.sqlite", "test5", ["test"])
124  for exp, run in [(0, 0), (0, 1), (0, 2), (0, 9999),
125  (1, 0), (1, 1), (1, 2), (1, 9999),
126  (2, 0), (2, 1), (2, 2), (2, 9999),
127  (243, 0), (245, 9999999), (9999, 9999)]:
128  valid = [("test", 1)] if (1, 1) <= (exp, run) else []
129  assert comparer.check(exp, run) == valid, f"Couldn't find payload for {exp},{run}"
Class to obtain metadata of all valid payloads from a local SQLite file instead of the central server...
def __init__(self, filename, globaltag, payloads)
_localcpp
Local metadata provider, C++ version.
_localpy
Local metadata provider, python version.
Simple struct to group all information necessary for a single payload.