10Test the tool b2file-mix and make sure it works.
17import b2test_utils
as b2tu
21 '''Class for populating the events with some random numbers.'''
27 self.
evt = ROOT.Belle2.PyStoreObj(
'EventMetaData')
34 self.
evt.setGeneratedWeight(ROOT.gRandom.Rndm())
37def path_for_test_file(events, output_name, seed):
38 b2.set_random_seed(seed)
40 main.add_module(
'EventInfoSetter', expList=0, runList=0, evtNumList=events)
42 main.add_module(
'RootOutput', outputFileName=output_name)
46@contextlib.contextmanager
47def open_root(filename, mode='READ'):
50 f = ROOT.TFile.Open(filename, mode)
58if __name__ ==
'__main__':
60 with b2tu.clean_working_directory():
63 file_name_a =
'test_a.root'
64 file_name_b =
'test_b.root'
65 file_name_ab =
'test_ab.root'
68 b2tu.run_in_subprocess(target=path_for_test_file, events=events, output_name=file_name_a, seed=
'aaa')
69 b2tu.run_in_subprocess(target=path_for_test_file, events=events, output_name=file_name_b, seed=
'bbb')
70 subprocess.check_call([
'b2file-merge', file_name_ab, file_name_a, file_name_b])
73 subprocess.check_call([
'b2file-mix', file_name_a,
'-o',
'test1.root'])
74 metadata = b2.get_file_metadata(
'test1.root')
75 assert (metadata.getNEvents() == events)
78 subprocess.check_call([
'b2file-mix', file_name_a,
'-o',
'test2.root',
'-n',
'100'])
79 metadata = b2.get_file_metadata(
'test2.root')
80 assert (metadata.getNEvents() == 100)
83 subprocess.check_call([
'b2file-mix', file_name_a,
'-o',
'test3.root',
'--exp',
'114'])
84 metadata = b2.get_file_metadata(
'test3.root')
85 assert (metadata.getExperimentLow() == 114)
88 subprocess.check_call([
'b2file-mix', file_name_a, file_name_b,
'-o',
'test4.root',
'--seed',
'abc'])
89 metadata = b2.get_file_metadata(
'test4.root')
90 assert (metadata.getRandomSeed() ==
'abc')
91 subprocess.check_call([
'b2file-mix', file_name_a, file_name_b,
'-o',
'test5.root',
'--seed',
'abc'])
92 subprocess.check_call([
'b2file-mix', file_name_a, file_name_b,
'-o',
'test6.root',
'--seed',
'def'])
100 with open_root(file_name_ab)
as fileab:
101 treeab = fileab.Get(
'tree')
102 with open_root(
'test4.root')
as file4:
103 tree4 = file4.Get(
'tree')
104 with open_root(
'test5.root')
as file5:
105 tree5 = file5.Get(
'tree')
106 with open_root(
'test6.root')
as file6:
107 tree6 = file6.Get(
'tree')
108 for i, (eventab, event4, event5, event6)
in enumerate(zip(treeab, tree4, tree5, tree6)):
109 if i < 10
or i > 1990:
111 weightab = eventab.EventMetaData.getGeneratedWeight()
112 weight4 = event4.EventMetaData.getGeneratedWeight()
113 weight5 = event5.EventMetaData.getGeneratedWeight()
114 weight6 = event6.EventMetaData.getGeneratedWeight()
115 assert (weightab != weight4)
116 assert (weightab != weight6)
117 assert (weight4 == weight5)
118 counts += (weight4 != weight6)
120 assert (counts > 1800)