Belle II Software development
test_reconstructmcdecay.py
1#!/usr/bin/env python3
2
3
10
11import unittest
12import tempfile
13from basf2 import create_path, register_module
14import b2test_utils
15from modularAnalysis import fillParticleListFromMC, reconstructMCDecay, inputMdst, findMCDecay
16from ROOT import TFile
17
18
19class TestNewMCDecayFinder(unittest.TestCase):
20 """The unit test"""
21
23 """Reconstruct/search for an MC decay chain using the reconstructMCDecay tool."""
24
25 testFile = tempfile.NamedTemporaryFile()
26
27 main = create_path()
28
29 inputfile = b2test_utils.require_file(
30 'analysis/1000_B_DstD0Kpi_skimmed.root', 'validation', py_case=self)
31 inputMdst(inputfile, path=main)
32
33 fillParticleListFromMC('gamma:primaryMC', 'mcPrimary', path=main)
34 fillParticleListFromMC('K+:primaryMC', 'mcPrimary', path=main)
35 fillParticleListFromMC('pi+:primaryMC', 'mcPrimary', path=main)
36
38 'B0:DstD0Kpi =direct=> [D*+:MC =direct=> [D0:MC =direct=> K-:primaryMC pi+:primaryMC ] pi+:primaryMC] pi-:primaryMC',
39 '',
40 path=main)
41
42 ntupler = register_module('VariablesToNtuple')
43 ntupler.param('fileName', testFile.name)
44 ntupler.param('treeName', 'ntuple')
45 ntupler.param('variables', ['isSignal'])
46 ntupler.param('particleList', 'B0:DstD0Kpi')
47 main.add_module(ntupler)
48
50
51 ntuplefile = TFile(testFile.name)
52 ntuple = ntuplefile.Get('ntuple')
53
54 self.assertFalse(ntuple.GetEntries() == 0, "Ntuple is empty.")
55
56 allBkg = ntuple.GetEntries("isSignal == 0")
57 allSig = ntuple.GetEntries("isSignal > 0")
58
59 print(f"True candidates {allSig}")
60 print(f"False candidates {allBkg}")
61
62 sig_expected = 406
63
64 self.assertTrue(
65 allSig == sig_expected,
66 f"n_sig expected: {sig_expected} found: {sig_expected}.")
67 self.assertTrue(allBkg == 0, f"n_bkg expected 0, found: {allBkg}.")
68
69 print("Test passed, cleaning up.")
70
72 """Reconstruct/search for an MC decay chain using the reconstructMCDecay tool."""
73
74 testFile = tempfile.NamedTemporaryFile()
75
76 main = create_path()
77
78 inputfile = b2test_utils.require_file('Bdto3Ks_MC13aBG1_train.root', 'examples', py_case=self)
79 inputMdst(inputfile, path=main)
80
81 fillParticleListFromMC('pi+:primaryMC', 'mcPrimary', path=main)
82 reconstructMCDecay('K_S0:MC =direct=> pi+:primaryMC pi-:primaryMC', '', path=main)
83 reconstructMCDecay('B0:3KS =direct=> K_S0:MC K_S0:MC K_S0:MC', '', path=main)
84
85 ntuplerKS = register_module('VariablesToNtuple')
86 ntuplerKS.param('fileName', testFile.name)
87 ntuplerKS.param('treeName', 'ntuple')
88 ntuplerKS.param('variables', ['isSignal'])
89 ntuplerKS.param('particleList', 'B0:3KS')
90 main.add_module(ntuplerKS)
91
93
94 ntuplefile = TFile(testFile.name)
95 ntuple = ntuplefile.Get('ntuple')
96
97 self.assertFalse(ntuple.GetEntries() == 0, "Ntuple is empty.")
98
99 allBkg = ntuple.GetEntries("isSignal == 0")
100 allSig = ntuple.GetEntries("isSignal > 0")
101
102 print(f"True candidates {allSig}")
103 print(f"False candidates {allBkg}")
104
105 sig_expected = 10
106
107 self.assertTrue(
108 allSig == sig_expected,
109 f"n_sig expected: {sig_expected} found: {sig_expected}.")
110 self.assertTrue(allBkg == 0, f"n_bkg expected 0, found: {allBkg}.")
111
112 print("Test passed, cleaning up.")
113
115 """Reconstruct/search for an MC decay chain using the findMCDecay tool."""
116
117 testFile = tempfile.NamedTemporaryFile()
118
119 main = create_path()
120
121 inputfile = b2test_utils.require_file(
122 'analysis/1000_B_DstD0Kpi_skimmed.root', 'validation', py_case=self)
123 inputMdst(inputfile, path=main)
124
125 findMCDecay(
126 'B0:DstD0Kpi',
127 'B0 =direct=> [D*+ =direct=> [D0 =direct=> K- pi+ ] pi+] pi-',
128 path=main)
129
130 ntupler = register_module('VariablesToNtuple')
131 ntupler.param('fileName', testFile.name)
132 ntupler.param('variables', ['isSignal'])
133 ntupler.param('particleList', 'B0:DstD0Kpi')
134 main.add_module(ntupler)
135
137
138 ntuplefile = TFile(testFile.name)
139 ntuple = ntuplefile.Get('ntuple')
140
141 self.assertFalse(ntuple.GetEntries() == 0, "Ntuple is empty.")
142
143 allBkg = ntuple.GetEntries("isSignal == 0")
144 allSig = ntuple.GetEntries("isSignal > 0")
145
146 print(f"True candidates {allSig}")
147 print(f"False candidates {allBkg}")
148
149 sig_expected = 406
150
151 self.assertTrue(
152 allSig == sig_expected,
153 f"n_sig expected: {sig_expected} found: {sig_expected}.")
154 self.assertTrue(allBkg == 0, f"n_bkg expected 0, found: {allBkg}.")
155
156 print("Test passed, cleaning up.")
157
159 """Reconstruct/search for an MC decay chain using the findMCDecay tool."""
160
161 testFile = tempfile.NamedTemporaryFile()
162
163 main = create_path()
164
165 inputfile = b2test_utils.require_file('Bdto3Ks_MC13aBG1_train.root', 'examples', py_case=self)
166 inputMdst(inputfile, path=main)
167
168 findMCDecay(
169 'B0:3KS',
170 'B0 -> [K_S0 -> pi+ pi-] [K_S0 -> pi+ pi-] [K_S0 -> pi+ pi-]',
171 path=main)
172
173 ntupler = register_module('VariablesToNtuple')
174 ntupler.param('fileName', testFile.name)
175 ntupler.param('variables', ['isSignal'])
176 ntupler.param('particleList', 'B0:3KS')
177 main.add_module(ntupler)
178
180
181 ntuplefile = TFile(testFile.name)
182 ntuple = ntuplefile.Get('ntuple')
183
184 self.assertFalse(ntuple.GetEntries() == 0, "Ntuple is empty.")
185
186 allBkg = ntuple.GetEntries("isSignal == 0")
187 allSig = ntuple.GetEntries("isSignal > 0")
188
189 print(f"True candidates {allSig}")
190 print(f"False candidates {allBkg}")
191
192 sig_expected = 10
193
194 self.assertTrue(
195 allSig == sig_expected,
196 f"n_sig expected: {sig_expected} found: {sig_expected}.")
197 self.assertTrue(allBkg == 0, f"n_bkg expected 0, found: {allBkg}.")
198
199 print("Test passed, cleaning up.")
200
201
202if __name__ == '__main__':
204 unittest.main()
def require_file(filename, data_type="", py_case=None)
Definition: __init__.py:54
def clean_working_directory()
Definition: __init__.py:189
def safe_process(*args, **kwargs)
Definition: __init__.py:236