Belle II Software development
treeFit_referenced_charged_tree_2dBeam.py
1#!/usr/bin/env python3
2
3
10import unittest
11import tempfile
12import basf2
13import b2test_utils
14import modularAnalysis as ma
15from ROOT import TFile
16
17
18class TestTreeFits(unittest.TestCase):
19 """The unit test case for TreeFitter"""
20
21 def testFit(self):
22 """Run the test fit"""
23
24 testFile = tempfile.NamedTemporaryFile()
25
26 main = basf2.create_path()
27
28 inputfile = b2test_utils.require_file(
29 'analysis/1000_B_DstD0Kpi_skimmed.root', 'validation', py_case=self)
30 ma.inputMdst(inputfile, path=main)
31
32 ma.fillParticleList('pi+:a', 'pionID > 0.5', path=main)
33 ma.fillParticleList('K+:a', 'kaonID > 0.5', path=main)
34
35 ma.reconstructDecay('D0:rec -> K-:a pi+:a', '', 0, path=main)
36 ma.reconstructDecay('D*+:rec -> D0:rec pi+:a', '', 0, path=main)
37 ma.reconstructDecay('B0:rec -> D*+:rec pi-:a', ' InvM > 5', 0, path=main)
38 ma.matchMCTruth('B0:rec', path=main)
39
40 conf = 0
41 main.add_module('TreeFitter',
42 particleList='B0:rec',
43 confidenceLevel=conf,
44 massConstraintList=[],
45 massConstraintListParticlename=[],
46 expertUseReferencing=True,
47 ipConstraint=True,
48 originDimension=2,
49 updateAllDaughters=False)
50
51 ntupler = basf2.register_module('VariablesToNtuple')
52 ntupler.param('fileName', testFile.name)
53 ntupler.param('variables', ['chiProb', 'M', 'isSignal'])
54 ntupler.param('particleList', 'B0:rec')
55 main.add_module(ntupler)
56
57 basf2.process(main)
58
59 ntuplefile = TFile(testFile.name)
60 ntuple = ntuplefile.Get('ntuple')
61
62 self.assertFalse(ntuple.GetEntries() == 0, "Ntuple is empty.")
63
64 allBkg = ntuple.GetEntries("isSignal == 0")
65 allSig = ntuple.GetEntries("isSignal > 0")
66
67 truePositives = ntuple.GetEntries("(chiProb > 0) && (isSignal > 0)")
68 falsePositives = ntuple.GetEntries("(chiProb > 0) && (isSignal == 0)")
69
70 mustBeZero = ntuple.GetEntries(f"(chiProb < {conf})")
71
72 print(f"True fit survivors: {truePositives} out of {allSig} true candidates")
73 print(f"False fit survivors: {falsePositives} out of {allBkg} false candidates")
74
75 self.assertFalse(truePositives == 0, "No signal survived the fit.")
76
77 self.assertTrue(falsePositives <= 1595, f"Background rejection {falsePositives} out of {allBkg}")
78
79 self.assertTrue(truePositives == 156, f"Signal rejection too high {truePositives} out of {allSig}")
80 self.assertFalse(mustBeZero, f"We should have dropped all candidates with confidence level less than {conf}.")
81
82 print("Test passed, cleaning up.")
83
84
85if __name__ == '__main__':
87 unittest.main()
def require_file(filename, data_type="", py_case=None)
Definition: __init__.py:54
def clean_working_directory()
Definition: __init__.py:189