Belle II Software  light-2303-iriomote
test_particleExtractorFromROE.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 import unittest
13 import tempfile
14 import basf2
15 import b2test_utils
16 import modularAnalysis as ma
17 from ROOT import TFile, Belle2
18 
19 
20 class TestParticleExtractorFromROE(unittest.TestCase):
21  """The unit test for ParticleExtractorFromROE module """
22 
23  def test_roepath(self):
24  """ Test the extractParticlesFromROE function in roe_path """
25 
26  testFile = tempfile.NamedTemporaryFile()
27 
28  main = basf2.create_path()
29 
30  inputfile = b2test_utils.require_file('analysis/tests/100_noBKG_B0ToPiPiPi0.root', py_case=self)
31  ma.inputMdst(inputfile, path=main)
32 
33  ma.fillParticleList('pi+:sig', 'pionID > 0.5', path=main)
34 
35  ma.fillParticleList('gamma:all', '', path=main)
36  ma.reconstructDecay('pi0:sig -> gamma:all gamma:all', '0.125 < InvM < 0.145', path=main)
37 
38  ma.reconstructDecay('B0:sig -> pi-:sig pi+:sig pi0:sig', '', path=main)
39 
40  ma.matchMCTruth('B0:sig', path=main)
41  ma.applyCuts('B0:sig', 'isSignal==1', path=main)
42 
43  ma.buildRestOfEvent('B0:sig', fillWithMostLikely=True, path=main)
44 
45  roe_path = basf2.create_path()
46  deadEndPath = basf2.create_path()
47  ma.signalSideParticleFilter('B0:sig', '', roe_path, deadEndPath)
48 
49  plists = ['%s:in_roe' % ptype for ptype in ['pi+', 'gamma', 'K_L0', 'K+', 'p+', 'e+', 'mu+']]
50  ma.extractParticlesFromROE(plists, maskName='all', path=roe_path)
51 
52  charged_inROE = ['nParticlesInList(%s:in_roe)' % ptype for ptype in ['pi+', 'K+', 'p+', 'e+', 'mu+']]
53  neutral_inROE = ['nParticlesInList(%s:in_roe)' % ptype for ptype in ['gamma', 'K_L0']]
54 
55  default = ['nParticlesInList(pi+:all)', 'nParticlesInList(gamma:all)', 'nParticlesInList(K_L0:roe_default)']
56  mostLikely = ['nParticlesInList(%s:mostlikely_roe)' % ptype for ptype in ['K+', 'p+', 'e+', 'mu+']]
57 
58  ma.variablesToNtuple('', charged_inROE + neutral_inROE + default + mostLikely,
59  filename=testFile.name,
60  path=roe_path)
61 
62  main.for_each('RestOfEvent', 'RestOfEvents', roe_path)
63 
64  basf2.process(main)
65 
66  ntuplefile = TFile(testFile.name)
67  ntuple = ntuplefile.Get('variables')
68 
69  self.assertFalse(ntuple.GetEntries() == 0, "Ntuple is empty.")
70 
71  nEntries = ntuple.GetEntries()
72 
73  # Number of pi+:all = Number of charged-FSPs in ROE + 2-pions in signal side
74  cut = Belle2.MakeROOTCompatible.makeROOTCompatible('nParticlesInList(pi+:all)') + ' == ' \
75  '(' + '+'.join([Belle2.MakeROOTCompatible.makeROOTCompatible(charged) for charged in charged_inROE]) + ' + 2)'
76  nPass_chargedCheck = ntuple.GetEntries(cut)
77  self.assertFalse(nPass_chargedCheck < nEntries, "Charged particles are not correctly extracted")
78 
79  # Number of mostLikely = Number of charged in ROE
80  cut = Belle2.MakeROOTCompatible.makeROOTCompatible('nParticlesInList(e+:mostlikely_roe)') + ' == ' \
81  + Belle2.MakeROOTCompatible.makeROOTCompatible('nParticlesInList(e+:in_roe)')
82  nPass_electronCheck = ntuple.GetEntries(cut)
83  self.assertFalse(nPass_electronCheck < nEntries, "MostLikely option seems broken")
84 
85  def test_mainpath(self):
86  """ Test the extractParticlesFromROE function in main path """
87 
88  testFile = tempfile.NamedTemporaryFile()
89 
90  main = basf2.create_path()
91 
92  inputfile = b2test_utils.require_file('analysis/tests/100_noBKG_B0ToPiPiPi0.root', py_case=self)
93  ma.inputMdst(inputfile, path=main)
94 
95  ma.fillParticleList('pi+:sig', 'pionID > 0.5', path=main)
96 
97  ma.fillParticleList('gamma:all', '', path=main)
98  ma.reconstructDecay('pi0:sig -> gamma:all gamma:all', '0.125 < InvM < 0.145', path=main)
99 
100  ma.reconstructDecay('B0:sig -> pi-:sig pi+:sig pi0:sig', '', path=main)
101 
102  ma.matchMCTruth('B0:sig', path=main)
103  ma.applyCuts('B0:sig', 'isSignal==1', path=main) # there must be only 1 candidate
104 
105  ma.buildRestOfEvent('B0:sig', fillWithMostLikely=True, path=main)
106 
107  plists = ['%s:in_roe' % ptype for ptype in ['pi+', 'gamma', 'K_L0', 'K+', 'p+', 'e+', 'mu+']]
108  ma.extractParticlesFromROE(plists, maskName='all', path=main, signalSideParticleList='B0:sig')
109 
110  charged_inROE = ['nParticlesInList(%s:in_roe)' % ptype for ptype in ['pi+', 'K+', 'p+', 'e+', 'mu+']]
111  neutral_inROE = ['nParticlesInList(%s:in_roe)' % ptype for ptype in ['gamma', 'K_L0']]
112 
113  default = ['nParticlesInList(pi+:all)', 'nParticlesInList(gamma:all)', 'nParticlesInList(K_L0:roe_default)']
114  mostLikely = ['nParticlesInList(%s:mostlikely_roe)' % ptype for ptype in ['K+', 'p+', 'e+', 'mu+']]
115 
116  ma.variablesToNtuple('B0:sig', charged_inROE + neutral_inROE + default + mostLikely,
117  filename=testFile.name,
118  path=main)
119 
120  basf2.process(main)
121 
122  ntuplefile = TFile(testFile.name)
123  ntuple = ntuplefile.Get('variables')
124 
125  self.assertFalse(ntuple.GetEntries() == 0, "Ntuple is empty.")
126 
127  nEntries = ntuple.GetEntries()
128 
129  # Number of pi+:all = Number of charged-FSPs in ROE + 2-pions in signal side
130  cut = Belle2.MakeROOTCompatible.makeROOTCompatible('nParticlesInList(pi+:all)') + ' == ' \
131  '(' + '+'.join([Belle2.MakeROOTCompatible.makeROOTCompatible(charged) for charged in charged_inROE]) + ' + 2)'
132  nPass_chargedCheck = ntuple.GetEntries(cut)
133  self.assertFalse(nPass_chargedCheck < nEntries, "Charged particles are not correctly extracted")
134 
135  # Number of mostLikely = Number of charged in ROE
136  cut = Belle2.MakeROOTCompatible.makeROOTCompatible('nParticlesInList(e+:mostlikely_roe)') + ' == ' \
137  + Belle2.MakeROOTCompatible.makeROOTCompatible('nParticlesInList(e+:in_roe)')
138  nPass_electronCheck = ntuple.GetEntries(cut)
139  self.assertFalse(nPass_electronCheck < nEntries, "MostLikely option seems broken")
140 
141 
142 if __name__ == '__main__':
144  unittest.main()
static std::string makeROOTCompatible(std::string str)
Remove special characters that ROOT dislikes in branch names, e.g.
def require_file(filename, data_type="", py_case=None)
Definition: __init__.py:54
def clean_working_directory()
Definition: __init__.py:185