8 from ROOT
import Belle2
11 path = basf2.create_path()
12 path.add_module(
'RootInput', inputFileName=inputFile)
13 path.add_module(
'ParticleLoader', decayStringsWithCuts=[(
'e+',
'')])
14 path.add_module(
'ParticleLoader', decayStringsWithCuts=[(
'gamma',
'clusterE > 2.5')])
17 path.add_module(
'VariablesToNtuple',
19 variables=[
'electronID',
'p',
'isSignal'],
20 sampling=(
'isSignal', {1: 0, 0: 20}),
21 fileName=
'particleListNtuple.root',
22 treeName=
'electronListTree')
25 path.add_module(
'VariablesToNtuple',
27 variables=[
'clusterE',
'p',
'isSignal'],
28 fileName=
'particleListNtuple.root',
29 treeName=
'photonListTree')
32 path.add_module(
'VariablesToNtuple',
34 variables=[
'nTracks',
'nKLMClusters'],
35 sampling=(
'nTracks', {12: 10}),
36 fileName=
'eventNtuple.root',
40 path.add_module(
'VariablesToNtuple',
42 variables=[
'expNum',
'runNum',
'evtNum'],
43 fileName=
'countersNtuple.root',
44 treeName=
'countersTree')
51 assert os.path.isfile(
'particleListNtuple.root'),
"particleListNtuple.root wasn't created"
52 f = ROOT.TFile(
'particleListNtuple.root')
53 t1 = f.Get(
'electronListTree')
54 t2 = f.Get(
'photonListTree')
55 assert bool(t1),
"electronListTree isn't contained in file"
56 assert bool(t2),
"photonListTree isn't contained in file"
57 assert t1.GetEntries() > 0,
"electronListTree contains zero entries"
58 assert t2.GetEntries() > 0,
"photonListTree contains zero entries"
59 assert t1.GetListOfBranches().Contains(
'electronID'),
"electronID branch is missing from electronListTree"
60 assert t1.GetListOfBranches().Contains(
'p'),
"p branch is missing from electronListTree"
61 assert t1.GetListOfBranches().Contains(
'__weight__'),
"weight branch is missing from electronListTree"
62 assert t1.GetListOfBranches().Contains(
'__event__'),
"event number branch is missing from electronList tree"
63 assert t1.GetListOfBranches().Contains(
'__run__'),
"run number branch is missing from electronList tree"
64 assert t1.GetListOfBranches().Contains(
'__experiment__'),
"experiment number branch is missing from electronList tree"
65 assert t1.GetListOfBranches().Contains(
'__candidate__'),
"candidate number branch is missing from electronList tree"
66 assert t1.GetListOfBranches().Contains(
'__ncandidates__'),
"candidate count branch is missing from electronList tree"
68 assert t2.GetListOfBranches().Contains(
'clusterE'),
"clusterEnergy branch is missing from photonListTree"
69 assert t2.GetListOfBranches().Contains(
'p'),
"p branch is missing from photonListTree"
70 assert t2.GetListOfBranches().Contains(
'__weight__'),
"weight branch is missing from photonListTree"
71 assert t2.GetListOfBranches().Contains(
'__event__'),
"event number branch is missing from photonList tree"
72 assert t2.GetListOfBranches().Contains(
'__run__'),
"run number branch is missing from photonList tree"
73 assert t2.GetListOfBranches().Contains(
'__experiment__'),
"experiment number branch is missing from photonList tree"
74 assert t2.GetListOfBranches().Contains(
'__candidate__'),
"candidate number branch is missing from photonList tree"
75 assert t2.GetListOfBranches().Contains(
'__ncandidates__'),
"candidate count branch is missing from photonList tree"
80 if event.isSignal == 1:
81 assert event.__weight__ == 1,
"Expected weight 1 for a true electron candidate got {}".format(event.__weight__)
84 assert event.__weight__ == 20,
"Expected weight 20 for a wrong electron candidate got {}".format(event.__weight__)
86 assert nBckgrd < nSignal,
"Expected less background than signal due to the large sampling rate"
89 assert event.__weight__ == 1,
"Expected weight 1 for all photon candidates got {}".format(event.__weight__)
91 assert os.path.isfile(
'eventNtuple.root'),
"eventNtuple.root wasn't created"
92 f = ROOT.TFile(
'eventNtuple.root')
93 t = f.Get(
'eventTree')
94 assert bool(t),
"eventTree isn't contained in file"
95 assert t.GetListOfBranches().Contains(
'nTracks'),
"nTracks branch is missing"
96 assert t.GetListOfBranches().Contains(
'nKLMClusters'),
"nKLMClusters branch is missing"
97 assert t.GetListOfBranches().Contains(
'__weight__'),
"weight branch is missing"
98 assert t.GetListOfBranches().Contains(
'__event__'),
"event number branch is missing"
99 assert t.GetListOfBranches().Contains(
'__run__'),
"run number branch is missing"
100 assert t.GetListOfBranches().Contains(
'__experiment__'),
"experiment number branch is missing"
101 assert not t.GetListOfBranches().Contains(
'__candidate__'),
"candidate number branch is present in eventwise tree"
102 assert not t.GetListOfBranches().Contains(
'__ncandidates__'),
"candidate count branch is present in eventwise tree"
105 assert t.__run__ == 0,
"run number not as expected"
106 assert t.__experiment__ == 0,
"experiment number not as expected"
107 assert t.__event__ == 1,
"event number not as expected"
112 if event.nTracks == 12:
113 assert event.__weight__ == 10,
"Expected weight 10 in an event with 12 tracks got {}".format(event.__weight__)
116 assert event.__weight__ == 1,
"Expected weight 1 in an event with unequal 12 tracks got {}".format(event.__weight__)
117 if event.nTracks == 11:
119 assert nTracks_12 * 5 < nTracks_11,
"Expected much less events with 12 tracks than with 11, due to the large sampling rate"
121 assert os.path.isfile(
'countersNtuple.root'),
"eventNtuple.root wasn't created"
122 f = ROOT.TFile(
'countersNtuple.root')
123 t = f.Get(
'countersTree')
124 assert bool(t),
"countersTree isn't contained in file"
125 assert t.GetListOfBranches().Contains(
'__event__'),
"event number branch is missing"
126 assert t.GetListOfBranches().Contains(
'__run__'),
"run number branch is missing"
127 assert t.GetListOfBranches().Contains(
'__experiment__'),
"experiment number branch is missing"
130 assert t.__run__ == 0,
"run number not as expected"
131 assert t.__experiment__ == 0,
"experiment number not as expected"
132 assert t.__event__ == 1,
"event number not as expected"
135 assert t.__run__ == 0,
"run number not as expected"
136 assert t.__experiment__ == 0,
"experiment number not as expected"
137 assert t.__event__ == 10,
"event number not as expected"