Belle II Software  release-06-02-00
treeFit_referenced_single_vertex.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 ROOT import TFile
17 
18 
19 class TestTreeFits(unittest.TestCase):
20  """The unit test case for TreeFitter"""
21 
22  def testFit(self):
23  """Run the test fit"""
24 
25  testFile = tempfile.NamedTemporaryFile()
26 
27  main = basf2.create_path()
28 
29  inputfile = b2test_utils.require_file(
30  'analysis/1000_B_Jpsi_ks_pipi.root', 'validation', py_case=self)
31  ma.inputMdst('default', inputfile, path=main)
32 
33  ma.fillParticleList('mu+', 'muonID > 0.5', path=main)
34 
35  ma.reconstructDecay('J/psi:all -> mu+ mu-', '', 0, path=main)
36  ma.matchMCTruth('J/psi:all', path=main)
37 
38  conf = 0
39  main.add_module('TreeFitter',
40  particleList='J/psi:all',
41  confidenceLevel=conf,
42  massConstraintList=[],
43  massConstraintListParticlename=[],
44  expertUseReferencing=True,
45  ipConstraint=False,
46  updateAllDaughters=False)
47 
48  ntupler = basf2.register_module('VariablesToNtuple')
49  ntupler.param('fileName', testFile.name)
50  ntupler.param('variables', ['chiProb', 'M', 'isSignal'])
51  ntupler.param('particleList', 'J/psi:all')
52  main.add_module(ntupler)
53 
54  basf2.process(main)
55 
56  ntuplefile = TFile(testFile.name)
57  ntuple = ntuplefile.Get('ntuple')
58 
59  self.assertFalse(ntuple.GetEntries() == 0, "Ntuple is empty.")
60 
61  allBkg = ntuple.GetEntries("isSignal == 0")
62  allSig = ntuple.GetEntries("isSignal > 0")
63 
64  truePositives = ntuple.GetEntries("(chiProb > 0) && (isSignal > 0)")
65  falsePositives = ntuple.GetEntries("(chiProb > 0) && (isSignal == 0)")
66 
67  mustBeZero = ntuple.GetEntries(f"(chiProb < {conf})")
68 
69  print(f"True fit survivors: {truePositives} out of {allSig} true candidates")
70  print(f"False fit survivors: {falsePositives} out of {allBkg} false candidates")
71 
72  self.assertFalse(truePositives == 0, "No signal survived the fit.")
73 
74  self.assertTrue(falsePositives < 1521, f"Too many false positives: {falsePositives} out of {allBkg} total bkg events.")
75 
76  self.assertTrue(truePositives > 741, "Signal rejection too high")
77 
78  self.assertFalse(mustBeZero, f"We should have dropped all candidates with confidence level less than {conf}.")
79 
80  print("Test passed, cleaning up.")
81 
82 
83 if __name__ == '__main__':
84  unittest.main()
def require_file(filename, data_type="", py_case=None)
Definition: __init__.py:54