Belle II Software  release-05-01-25
mdst_class_versions.py
1 #!/usr/bin/env python3
2 
3 """
4 Check that all the classdef versions and class checksums are consistent to prevent accidental mismatch:
5 
6 If one forgets to increase the ClassDef but the streamer info checksum changes this will trigger a test failure. We can distuingish
7 
8 1. ClassDef version changed unnecessary
9 2. forgot to change ClassDef
10 3. both changed
11 """
12 
13 import sys
14 from b2test_utils import get_object_with_name, get_streamer_checksums
15 
16 
17 EXPECTED_CHECKSUMS = {
18  'Belle2::Const::DetectorSet': (1, 1222446098),
19  'Belle2::ECLCluster': (14, 932603982),
20  'Belle2::EventLevelClusteringInfo': (1, 162935545),
21  'Belle2::EventLevelTrackingInfo': (2, 1667284927),
22  'Belle2::EventMetaData': (4, 2999207747),
23  'Belle2::FileMetaData': (10, 3436593892),
24  'Belle2::KLMCluster': (2, 2615188022),
25  'Belle2::KlId': (2, 230716330),
26  'Belle2::PIDLikelihood': (3, 36434623),
27  'Belle2::RelationContainer': (1, 1725678837),
28  'Belle2::RelationElement': (1, 1883389510),
29  'Belle2::RelationsInterface<TObject>': (0, 3862127315),
30  'Belle2::SoftwareTriggerResult': (5, 241059817),
31  'Belle2::TRGSummary': (7, 1658421299),
32  'Belle2::Track': (4, 839781593),
33  'Belle2::TrackFitResult': (8, 1247854432),
34  'Belle2::V0': (3, 4006259140),
35 }
36 
37 
38 OBJECT_NAMES = [
39  "FileMetaData",
40  "EventMetaData",
41  "RelationContainer",
42  "Track",
43  "V0",
44  "TrackFitResult",
45  "EventLevelTrackingInfo",
46  "PIDLikelihood",
47  "ECLCluster",
48  "EventLevelClusteringInfo",
49  "KLMCluster",
50  "KlId",
51  "TRGSummary",
52  "SoftwareTriggerResult",
53 ]
54 
55 if __name__ == "__main__":
56  # Now get
57 
58  objects = [get_object_with_name(object_name)() for object_name in OBJECT_NAMES]
59 
60  # Check the checksums of every entry and
61 
62  problems = []
63 
64 
65  found = get_streamer_checksums(objects)
66  # we don't care about TObject ...
67  if 'TObject' in found:
68  del found['TObject']
69 
70  # print list we found to be able to copy pasta
71  print("found_checksums = {\n " + "\n ".join(f"{k!r}: {v!r}," for k, v in sorted(found.items())) + "\n}")
72  for key, (version, checksum) in found.items():
73  if key not in EXPECTED_CHECKSUMS:
74  problems.append(f"There is no {key} in the checksum dictionary!")
75  continue
76 
77 
81  expected_version, expected_checksum = EXPECTED_CHECKSUMS[key]
82 
83  if expected_version != version and expected_checksum == checksum:
84  problems.append(f"The version for {key} has changed (expected={expected_version}, found={version}) "
85  f"while the checksum has not. This probably means the ClassDef version was increased unnecessarily. "
86  f"If this is intentional please update the expected version in this test")
87  elif expected_version == version and expected_checksum != checksum:
88  problems.append(f"The checksum for {key} has changed (expected={expected_checksum}, found={checksum}) "
89  f"while the version has not. This probably means you forgot to increase the ClassDef version "
90  f"after changing the class. Please update the ClassDef version and the expected values in this test")
91  elif expected_version != version and expected_checksum != checksum:
92  problems.append(f"The version and checksum for {key} have changed, (expected version={expected_version}, "
93  f"checksum={expected_checksum}, found version={version}, checksum={checksum}). "
94  f"Please update the expected values")
95  del EXPECTED_CHECKSUMS[key]
96 
97  for remaining, (version, checksum) in EXPECTED_CHECKSUMS.items():
98  problems.append(f"Additional class expected but not needed: {remaining} (version {version}, checksum {checksum})")
99 
100  if not problems:
101  print("Check finished")
102  else:
103  problems = "\n\t * ".join([""] + problems)
104  print("Check failed: Most likely a mdst class has been modified and the list of expected checksums needs to be adjusted:",
105  problems)
106  sys.exit(1)