14import modularAnalysis
as ma
15from variables
import variables
as vm
19basf2.set_random_seed(
'aSeed')
23 """The unit test case for TreeFitter"""
26 """Run the test fit"""
28 testFile = tempfile.NamedTemporaryFile()
30 main = basf2.create_path()
33 ma.fillParticleList(
'pi+:a',
'pionID > 0.5', path=main)
34 ma.fillParticleList(
'K+:a',
'kaonID > 0.5', path=main)
37 "D-:tag -> K+:a pi-:a pi-:a",
38 cut=
'1.859 < M < 1.879 and useCMSFrame(p)>2.0',
48 ma.reconstructDecay(
'Z0:rec -> D+:sig D-:tag pi-:a pi+:a',
'', path=main)
49 ma.matchMCTruth(
'Z0:rec', path=main)
51 ma.setBeamConstrainedMomentum(
53 '^Z0:rec -> D+:sig D-:tag pi-:a pi+:a',
54 'Z0:rec -> D+:sig D-:tag pi-:a pi+:a',
58 ma.setBeamConstrainedMomentum(
60 'Z0:rec -> ^D+:sig D-:tag pi-:a pi+:a',
61 'Z0:rec -> D+:sig ^D-:tag ^pi-:a ^pi+:a',
66 main.add_module(
'TreeFitter',
67 particleList=
'Z0:rec',
69 massConstraintList=[],
70 massConstraintListParticlename=[],
71 expertBeamConstraintPDG=23,
72 treatAsInvisible=
'Z0:rec -> ^D+:sig D-:tag pi-:a pi+:a',
74 updateAllDaughters=
True)
76 ma.applyCuts(
'Z0:rec',
77 "1.6<daughter(0,M)<2.2",
81 ma.rankByHighest(
"Z0:rec",
86 vm.addAlias(
"signalD",
"daughter(0,isSignalAcceptMissingNeutrino)")
88 ntupler = basf2.register_module(
'VariablesToNtuple')
89 ntupler.param(
'fileName', testFile.name)
90 ntupler.param(
'variables', [
'chiProb',
'M',
'signalD'])
91 ntupler.param(
'particleList',
'Z0:rec')
92 main.add_module(ntupler)
96 ntuplefile = TFile(testFile.name)
97 ntuple = ntuplefile.Get(
'ntuple')
99 self.assertFalse(ntuple.GetEntries() == 0,
"Ntuple is empty.")
101 allBkg = ntuple.GetEntries(
"signalD == 0")
102 allSig = ntuple.GetEntries(
"signalD > 0")
104 truePositives = ntuple.GetEntries(
"(chiProb > 0) && (signalD > 0)")
105 falsePositives = ntuple.GetEntries(
"(chiProb > 0) && (signalD == 0)")
107 mustBeZero = ntuple.GetEntries(f
"(chiProb < {conf})")
109 print(f
"True fit survivors: {truePositives} out of {allSig} true candidates")
110 print(f
"False fit survivors: {falsePositives} out of {allBkg} false candidates")
112 self.assertFalse(truePositives == 0,
"No signal survived the fit.")
114 self.assertTrue(falsePositives < 1,
"Background rejection too small.")
116 self.assertTrue(truePositives > 8,
"Signal rejection too high")
117 self.assertFalse(mustBeZero, f
"We should have dropped all candidates with confidence level less than {conf}.")
119 print(
"Test passed, cleaning up.")
122if __name__ ==
'__main__':
def require_file(filename, data_type="", py_case=None)
def clean_working_directory()