Belle II Software  release-05-01-25
copylists.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 import unittest
5 import root_pandas
6 import basf2 as b2
7 import b2test_utils as b2tu
8 import modularAnalysis as ma
9 from variables import variables as vm
10 
11 
12 def dump_3_v2nts(names_of_three_lists, path):
13  """Dump each of the three vpho lists into a VariablesToNtuple on path"""
14  first, second, third = names_of_three_lists
15  vs = ['d0_mdstIndex', 'd0_p']
16  ma.variablesToNtuple("vpho:" + first, vs, treename=first, path=path)
17  ma.variablesToNtuple("vpho:" + second, vs, treename=second, path=path)
18  ma.variablesToNtuple("vpho:" + third, vs, treename=third, path=path)
19 
20 
21 def run_copylists():
22  """A simple basf2 analysis job to do some list copying"""
23 
24  # aliases for useful for manual debugging
25  vm.addAlias("d0_mdstIndex", "daughter(0, mdstIndex)")
26  vm.addAlias("d0_p", "daughter(0, p)")
27 
28  # analysis input
29  pa = b2.Path()
30  ma.inputMdst("default", b2.find_file("analysis/tests/mdst.root"), path=pa)
31  ma.fillParticleList("pi+", "", path=pa)
32  ma.fillParticleList("K+", "", path=pa)
33 
34  # first test: check that merging two lists with
35  # identical daughters removes duplicates
36  ma.reconstructDecay("vpho:a -> K+", "", path=pa, allowChargeViolation=True)
37  ma.reconstructDecay("vpho:b -> K+", "", path=pa, allowChargeViolation=True)
38  ma.copyLists("vpho:ab", ["vpho:a", "vpho:b"], path=pa)
39  dump_3_v2nts(["a", "b", "ab"], path=pa)
40 
41  # second test: check that two lists with different daughters (but from the
42  # same underlying mdst object) are included twice (as expected)
43  ma.reconstructDecay("vpho:c -> K+", "", path=pa, allowChargeViolation=True)
44  ma.reconstructDecay("vpho:d -> pi-", "", path=pa, allowChargeViolation=True)
45  ma.copyLists("vpho:cd", ["vpho:c", "vpho:d"], path=pa)
46  dump_3_v2nts(["c", "d", "cd"], path=pa)
47 
48  # third test: check that two lists with the same daughers in different
49  # orders doesn't double count
50  # (they are different Belle2::Particles but we should match them)
51  ma.reconstructDecay("vpho:e -> K+ pi-", "", path=pa)
52  ma.reconstructDecay("vpho:f -> pi- K+", "", path=pa)
53  ma.copyLists("vpho:ef", ["vpho:e", "vpho:f"], path=pa)
54  dump_3_v2nts(["e", "f", "ef"], path=pa)
55 
56  b2tu.safe_process(pa, 1)
57 
58 
59 class TestCopyLists(unittest.TestCase):
60  """Analyse the output of ``run_copylists``"""
61 
62  def _count(self, listname):
63  """Open the ntuple output and count the number of entries in the list."""
64  df = root_pandas.read_root("ntuple.root", listname)
65  return len(df)
66 
68  """Merging two lists with identical daughters should not double count."""
69  self.assertEqual(self._count("a"), self._count("b"))
70  self.assertEqual(self._count("a"), self._count("ab"))
71  self.assertEqual(self._count("b"), self._count("ab"))
72 
74  """Merging two lists with different daughters but with the same mdst
75  object should include each of them twice."""
76  self.assertEqual(self._count("c"), self._count("d"))
77  self.assertEqual(self._count("c") + self._count("d"), self._count("cd"))
78 
80  """Merging two lists with daughters in a different order should not double count."""
81  self.assertEqual(self._count("e"), self._count("e"))
82  self.assertEqual(self._count("e"), self._count("ef"))
83 
84 
85 if __name__ == "__main__":
86  with b2tu.clean_working_directory():
87  run_copylists()
88  unittest.main()
copylists.TestCopyLists._count
def _count(self, listname)
Definition: copylists.py:62
copylists.TestCopyLists.test_merge_two_lists_with_different_mdst_objects
def test_merge_two_lists_with_different_mdst_objects(self)
Definition: copylists.py:73
copylists.TestCopyLists
Definition: copylists.py:59
copylists.TestCopyLists.test_different_daughter_order
def test_different_daughter_order(self)
Definition: copylists.py:79
copylists.TestCopyLists.test_merge_two_lists_with_identical_daughters
def test_merge_two_lists_with_identical_daughters(self)
Definition: copylists.py:67