Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
root_event_diff_np.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file root_event_diff_np.py
1 import sys
2 import argparse
3 import numpy as np
4 import uproot
5 
6 
7 parser = argparse.ArgumentParser()
8 parser.add_argument("a")
9 parser.add_argument("b")
10 parser.add_argument("--event-nr", default="event_nr")
11 parser.add_argument("--fail-fast", action="store_true")
12 args = parser.parse_args()
13 
14 a_data = uproot.open(args.a)
15 b_data = uproot.open(args.b)
16 event_nr = args.event_nr
17 fail_fast = args.fail_fast
18 
19 a_sort_index = np.argsort(a_data[event_nr].array(library="np"), kind="stable")
20 b_sort_index = np.argsort(b_data[event_nr].array(library="np"), kind="stable")
21 
22 np.set_printoptions(linewidth=np.inf)
23 
24 
25 def cmp(a, b):
26  if np.isscalar(a):
27  if a != b:
28  return False
29  elif type(a) == np.ndarray:
30  if not np.array_equal(a, b, equal_nan=True):
31  return False
32  else:
33  for aa, bb in zip(a, b):
34  if not cmp(aa, bb):
35  return False
36  return True
37 
38 
39 failed_events = set()
40 failed_keys = set()
41 for key in a_data.keys():
42  if key == event_nr:
43  continue
44 
45  a_vals = a_data[key].array(library="np")
46  a_vals = a_vals[a_sort_index]
47 
48  b_vals = b_data[key].array(library="np")
49  b_vals = b_vals[b_sort_index]
50 
51  for event, (a, b) in enumerate(zip(a_vals, b_vals)):
52  if not cmp(a, b):
53  print(f"event {event} failed for {key}")
54  print(f"a {a}")
55  print(f"b {b}")
56  print()
57 
58  if fail_fast:
59  sys.exit(1)
60  failed_events.add(event)
61  failed_keys.add(key)
62 
63 if failed_events or failed_keys:
64  print("summary")
65  print("failed events: " + " ".join(map(str, sorted(failed_events))))
66  print("failed keys: " + " ".join(failed_keys))
67  sys.exit(1)
68 
69 sys.exit(0)