Belle II Software  release-06-02-00
chargeConjugation.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 import unittest
12 import tempfile
13 import basf2
14 import b2test_utils
15 import modularAnalysis as ma
16 from variables import variables
17 from ROOT import TFile
18 
19 
20 class TestParticleCombiner(unittest.TestCase):
21  """The unit test case for the ParticleCombinerModule"""
22 
24  """Reconstruct decays with and without charge conjugation"""
25 
26  testFile = tempfile.NamedTemporaryFile()
27  # make logging more reproducible by replacing some strings.
28  # Also make sure the testfile name is replaced if necessary
29  b2test_utils.configure_logging_for_tests({testFile.name: "${testfile}"})
30 
31  main = basf2.create_path()
32  ma.inputMdst('default', b2test_utils.require_file('analysis/tests/mdst.root'), path=main)
33 
34  ma.fillParticleList('pi+:pionlike', 'pionID > 0.5', path=main)
35  ma.fillParticleList('K+:kaonlike', 'kaonID > 0.5', path=main)
36 
37  # combine all kaons with all pions for both charge configurations (K- pi+ and K+ pi-)
38  ma.reconstructDecay('D0:all -> K-:kaonlike pi+:pionlike', '', chargeConjugation=True, path=main)
39 
40  # only reconstruct D0
41  ma.reconstructDecay('D0:particle -> K-:kaonlike pi+:pionlike', '', chargeConjugation=False, path=main)
42 
43  # only reconstruct anti-D0
44  ma.reconstructDecay('anti-D0:anti-particle -> K+:kaonlike pi-:pionlike', '', chargeConjugation=False, path=main)
45 
46  # use particle's charge to separate D0 flavors,
47  ma.reconstructDecay(
48  'D0:positiveFlavour -> K-:kaonlike pi+:pionlike',
49  'daughter(0, charge) < 0',
50  chargeConjugation=True,
51  path=main)
52  ma.reconstructDecay(
53  'D0:negativeFlavour -> K-:kaonlike pi+:pionlike',
54  'daughter(0, charge) > 0',
55  chargeConjugation=True,
56  path=main)
57 
58  variables.addAlias('nDs', 'nParticlesInList(D0:all)')
59  variables.addAlias('nD0s', 'nParticlesInList(D0:particle)')
60  variables.addAlias('nAntiD0s', 'nParticlesInList(anti-D0:anti-particle)')
61  variables.addAlias('nPositiveFlavourDs', 'nParticlesInList(D0:positiveFlavour)')
62  variables.addAlias('nNegativeFlavourDs', 'nParticlesInList(D0:negativeFlavour)')
63 
64  allvariables = ['nDs', 'nD0s', 'nAntiD0s', 'nPositiveFlavourDs', 'nNegativeFlavourDs']
65  ma.variablesToNtuple('', variables=allvariables, filename=testFile.name, path=main)
66 
67  basf2.process(main)
68 
69  ntuplefile = TFile(testFile.name)
70  ntuple = ntuplefile.Get('variables')
71 
72  self.assertFalse(ntuple.GetEntries() == 0, "Ntuple is empty.")
73 
74  # sum of D0s and D0bars should be equal to all D0s
75  if (ntuple.GetEntries() != ntuple.GetEntries("nDs == nD0s + nAntiD0s")):
76  ntuple.Scan("nDs:nD0s:nAntiD0s:nPositiveFlavourDs:nNegativeFlavourDs", "", "", 10)
77  self.assertFalse(True, "Number of D0s does not agree with sum of both flavors!")
78 
79  # D0s should be combinations of K- and pi+ only
80  if (ntuple.GetEntries() != ntuple.GetEntries("nD0s == nPositiveFlavourDs")):
81  ntuple.Scan("nDs:nD0s:nAntiD0s:nPositiveFlavourDs:nNegativeFlavourDs", "", "", 10)
82  self.assertFalse(True, "Number of D0s does not agree with number of K- pi+ combinations!")
83 
84  # D0bars should be combinations of K+ and pi- only
85  if (ntuple.GetEntries() != ntuple.GetEntries("nAntiD0s == nNegativeFlavourDs")):
86  ntuple.Scan("nDs:nD0s:nAntiD0s:nPositiveFlavourDs:nNegativeFlavourDs", "", "", 10)
87  self.assertFalse(True, "Number of anti D0s does not agree with number of K+ pi- combinations!")
88 
89  print("Test passed, cleaning up.")
90 
91 
92 if __name__ == '__main__':
93  unittest.main()
def configure_logging_for_tests(user_replacements=None)
Definition: __init__.py:106
def require_file(filename, data_type="", py_case=None)
Definition: __init__.py:54