Belle II Software  release-08-01-10
treeFit_referenced_tree_with_neutral_custom_origin.py
1 #!/usr/bin/env python3
2 
3 
10 import unittest
11 import tempfile
12 import basf2
13 import b2test_utils
14 import modularAnalysis as ma
15 from ROOT import TFile
16 
17 
18 class 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_DstD0Kpipi0_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.fillParticleList('gamma:a', 'E > 0.08', path=main)
36  ma.reconstructDecay('pi0:a -> gamma:a gamma:a', '0.125 < InvM < 0.145', 0, path=main)
37 
38  ma.reconstructDecay('D0:rec -> K-:a pi+:a pi0:a', '', 0, path=main)
39  ma.reconstructDecay('D*+:rec -> D0:rec pi+:a', '', 0, path=main)
40  ma.reconstructDecay('B0:rec -> D*+:rec pi-:a', ' InvM > 5', 0, path=main)
41  ma.matchMCTruth('B0:rec', path=main)
42 
43  conf = 0
44  main.add_module('TreeFitter',
45  particleList='B0:rec',
46  confidenceLevel=conf,
47  massConstraintList=[],
48  massConstraintListParticlename=[],
49  expertUseReferencing=True,
50  ipConstraint=False,
51  updateAllDaughters=False,
52  customOriginConstraint=True,
53  # just to test this doesn't crash
54  customOriginVertex=[0, 0, 0],
55  customOriginCovariance=[1, 0, 0, 0, 1, 0, 0, 0, 1]
56  )
57 
58  ntupler = basf2.register_module('VariablesToNtuple')
59  ntupler.param('fileName', testFile.name)
60  ntupler.param('variables', ['chiProb', 'M', 'isSignal'])
61  ntupler.param('particleList', 'B0:rec')
62  main.add_module(ntupler)
63 
64  basf2.process(main)
65 
66  ntuplefile = TFile(testFile.name)
67  ntuple = ntuplefile.Get('ntuple')
68 
69  self.assertFalse(ntuple.GetEntries() == 0, "Ntuple is empty.")
70 
71  allBkg = ntuple.GetEntries("isSignal == 0")
72  allSig = ntuple.GetEntries("isSignal > 0")
73 
74  truePositives = ntuple.GetEntries("(chiProb > 0) && (isSignal > 0)")
75  falsePositives = ntuple.GetEntries("(chiProb > 0) && (isSignal == 0)")
76 
77  mustBeZero = ntuple.GetEntries(f"(chiProb < {conf})")
78 
79  print(f"True fit survivors: {truePositives} out of {allSig} true candidates")
80  print(f"False fit survivors: {falsePositives} out of {allBkg} false candidates")
81 
82  self.assertFalse(truePositives == 0, "No signal survived the fit.")
83 
84  self.assertTrue(falsePositives < 3032, f"Too many false positives: {falsePositives} out of {allBkg} total bkg events.")
85 
86  self.assertTrue(truePositives > 64, "Signal rejection too high")
87  self.assertFalse(mustBeZero, f"We should have dropped all candidates with confidence level less than {conf}.")
88 
89  print("Test passed, cleaning up.")
90 
91 
92 if __name__ == '__main__':
94  unittest.main()
def require_file(filename, data_type="", py_case=None)
Definition: __init__.py:54
def clean_working_directory()
Definition: __init__.py:189