Belle II Software  release-05-01-25
allparticlecombiner.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 import unittest
4 import tempfile
5 import basf2
6 import b2test_utils
7 import modularAnalysis as ma
8 from variables import variables
9 from vertex import kFit
10 from ROOT import Belle2
11 from ROOT import TFile
12 from ROOT import TNtuple
13 
14 
15 class TestAllParticleCombiner(unittest.TestCase):
16  """The unit test case for the AllParticleCombiner"""
17 
18  def testCombination(self):
19  """Run the test fit"""
20 
21  testFile = tempfile.NamedTemporaryFile()
22  # make logging more reproducible by replacing some strings.
23  # Also make sure the testfile name is replaced if necessary
24  b2test_utils.configure_logging_for_tests({testFile.name: "${testfile}"})
25 
26  basf2.set_random_seed("1234")
27  main = basf2.create_path()
28  inputfile = b2test_utils.require_file(
29  'analysis/tests/mdst.root', py_case=self)
30  main.add_module(
31  'RootInput',
32  inputFileNames=[inputfile],
33  logLevel=basf2.LogLevel.ERROR)
34 
35  ma.fillParticleList('pi+:fromPV', 'dr < 2 and abs(dz) < 5', path=main)
36 
37  ma.variablesToExtraInfo('pi+:fromPV', {'medianValueInList(pi+:fromPV, dz)': 'medianDZ'}, path=main)
38  variables.addAlias('medianDzFromPV', 'extraInfo(medianDZ)')
39  variables.addAlias('dzFromMedianDz', 'formula(dz - medianDzFromPV)')
40  ma.applyCuts('pi+:fromPV', 'abs(dzFromMedianDz) < 0.05', path=main)
41 
42  ma.combineAllParticles(['pi+:fromPV'], 'vpho:PVFit', path=main)
43 
44  kFit('vpho:PVFit', conf_level=-1, fit_type='vertex', constraint='iptube', path=main)
45 
46  variables.addAlias('PVX', 'x')
47  variables.addAlias('PVY', 'y')
48  variables.addAlias('PVZ', 'z')
49  variables.addAlias('nGoodTracksFromPV', 'nParticlesInList(pi+:fromPV)')
50 
51  main.add_module(
52  'VariablesToNtuple',
53  particleList='vpho:PVFit',
54  fileName=testFile.name,
55  variables=[
56  'PVX',
57  'PVY',
58  'PVZ',
59  'chiProb',
60  'nGoodTracksFromPV',
61  'distance'])
62 
63  ma.summaryOfLists(['vpho:PVFit'], path=main)
64 
65  basf2.process(main)
66 
67  ntuplefile = TFile(testFile.name)
68  ntuple = ntuplefile.Get('ntuple')
69 
70  self.assertFalse(ntuple.GetEntries() == 0, "Ntuple is empty.")
71 
72  converged = ntuple.GetEntries("chiProb > 0")
73 
74  self.assertFalse(converged == 0, "No fit converged.")
75 
76  print("Test passed, cleaning up.")
77 
78 
79 if __name__ == '__main__':
80  unittest.main()
allparticlecombiner.TestAllParticleCombiner.testCombination
def testCombination(self)
Definition: allparticlecombiner.py:18
b2test_utils.configure_logging_for_tests
def configure_logging_for_tests(user_replacements=None)
Definition: __init__.py:99
basf2.process
def process(path, max_event=0)
Definition: __init__.py:25
allparticlecombiner.TestAllParticleCombiner
Definition: allparticlecombiner.py:15
b2test_utils.require_file
def require_file(filename, data_type="", py_case=None)
Definition: __init__.py:47