Belle II Software  release-05-02-19
include_exclude_branches.py
1 import basf2 as b2
2 import b2test_utils as b2u
3 import ROOT
4 
5 
6 def filter_input_output(input_file_name, output_file_name, include_branches=None, exclude_branches=None):
7 
8  b2u.configure_logging_for_tests()
9  b2.set_random_seed('Forza Livorno!!!')
10  b2.B2INFO('We are testing the following configuration:',
11  include_branches=include_branches,
12  exclude_branches=exclude_branches)
13  child_path = b2.Path()
14  child_path.add_module('RootInput',
15  inputFileName=input_file_name,
16  collectStatistics=True)
17  if include_branches is not None:
18  b2.set_module_parameters(child_path,
19  'RootInput',
20  branchNames=include_branches)
21  if exclude_branches is not None:
22  b2.set_module_parameters(child_path,
23  'RootInput',
24  excludeBranchNames=exclude_branches)
25  child_path.add_module('RootOutput',
26  outputFileName=output_file_name)
27  b2.process(child_path)
28 
29 
30 def check_file_content(file_name, input_branches):
31 
32  f = ROOT.TFile.Open(file_name, 'READ')
33  if f.IsZombie():
34  b2.B2FATAL(f'Cannot open the input file {file_name}.')
35  tree = f.Get('tree')
36  file_branches = [branch.GetName() for branch in tree.GetListOfBranches()]
37  f.Close()
38  return set(input_branches) <= set(file_branches)
39 
40 
41 if __name__ == '__main__':
42 
43  with b2u.clean_working_directory():
44  # Let's check the input file
45  input_file_name = b2.find_file('framework/tests/mdst_with_muid.root')
46  assert (0 == b2u.run_in_subprocess(input_file_name, 'mdst_all_branches.root', None, None,
47  target=filter_input_output))
48  all_branches = ['Tracks', 'ECLClusters', 'TracksToECLClusters', 'Muids', 'TracksToMuids']
49  assert (1 == check_file_content(input_file_name, all_branches))
50  # Let's exclude Muids.
51  file_to_check = 'mdst_without_muid.root'
52  assert (0 == b2u.run_in_subprocess(input_file_name, file_to_check, None, ['Muids'],
53  target=filter_input_output))
54  assert (0 == check_file_content(file_to_check, ['Muids']))
55  assert (0 == check_file_content(file_to_check, ['TracksToMuids']))
56  assert (1 == check_file_content(file_to_check, ['Tracks', 'ECLClusters']))
57  # Let's include only Muids.
58  file_to_check = 'mdst_only_muid.root'
59  assert (0 == b2u.run_in_subprocess(input_file_name, file_to_check, ['Muids'], None,
60  target=filter_input_output))
61  # The dictionary is not found, so it can't be stored.
62  assert (0 == check_file_content(file_to_check, ['Muids']))
63  # Let's include only Tracks and ECLClusters.
64  assert (0 == b2u.run_in_subprocess(input_file_name, file_to_check, ['Tracks', 'ECLClusters'], None,
65  target=filter_input_output))
66  assert (0 == check_file_content(file_to_check, ['Muids', 'TracksToMuids']))
67  assert (1 == check_file_content(file_to_check, ['Tracks', 'ECLClusters', 'TracksToECLClusters']))