Belle II Software light-2406-ragdoll
test_VariablesToNtuple.py
1#!/usr/bin/env python3
2
3
10
11import os
12import basf2
13import ROOT
14import b2test_utils
15
16inputFile = b2test_utils.require_file('mdst14.root', 'validation')
17path = basf2.create_path()
18path.add_module('RootInput', inputFileName=inputFile)
19path.add_module('ParticleLoader', decayStrings=['e+'])
20path.add_module('ParticleLoader', decayStrings=['gamma'])
21path.add_module('ParticleListManipulator', outputListName='gamma', inputListNames=['gamma:all'], cut='clusterE > 2.5')
22
23# Write out electron id and momentum of all true electron candidates and every 10th wrong electron candidate
24path.add_module('VariablesToNtuple',
25 particleList='e+:all',
26 variables=['electronID', 'p', 'isSignal'],
27 sampling=('isSignal', {1: 0, 0: 20}),
28 fileName='particleListNtuple.root',
29 treeName='electronListTree')
30
31# Write out two V2NT trees to the same file
32path.add_module('VariablesToNtuple',
33 particleList='gamma',
34 variables=['clusterE', 'p', 'isSignal'],
35 fileName='particleListNtuple.root', # as above
36 treeName='photonListTree')
37
38# Write out number of tracks and ecl-clusters in every event, except for events with 12 tracks where we take only every 100th events
39path.add_module('VariablesToNtuple',
40 particleList='',
41 variables=['nTracks', 'nKLMClusters'],
42 sampling=('nTracks', {12: 10}),
43 fileName='eventNtuple.root',
44 treeName='eventTree')
45
46# try to write out candidate counters even though they're already there
47path.add_module('VariablesToNtuple',
48 particleList='',
49 variables=['expNum', 'runNum', 'evtNum'],
50 fileName='countersNtuple.root',
51 treeName='countersTree')
52
53
55 basf2.process(path)
56
57 # Testing
58 assert os.path.isfile('particleListNtuple.root'), "particleListNtuple.root wasn't created"
59 f = ROOT.TFile('particleListNtuple.root')
60 t1 = f.Get('electronListTree')
61 t2 = f.Get('photonListTree')
62 assert bool(t1), "electronListTree isn't contained in file"
63 assert bool(t2), "photonListTree isn't contained in file"
64 assert t1.GetEntries() > 0, "electronListTree contains zero entries"
65 assert t2.GetEntries() > 0, "photonListTree contains zero entries"
66 assert t1.GetListOfBranches().Contains('electronID'), "electronID branch is missing from electronListTree"
67 assert t1.GetListOfBranches().Contains('p'), "p branch is missing from electronListTree"
68 assert t1.GetListOfBranches().Contains('__weight__'), "weight branch is missing from electronListTree"
69 assert t1.GetListOfBranches().Contains('__event__'), "event number branch is missing from electronList tree"
70 assert t1.GetListOfBranches().Contains('__run__'), "run number branch is missing from electronList tree"
71 assert t1.GetListOfBranches().Contains('__experiment__'), "experiment number branch is missing from electronList tree"
72 assert t1.GetListOfBranches().Contains('__production__'), "production number branch is missing from electronList tree"
73 assert t1.GetListOfBranches().Contains('__candidate__'), "candidate number branch is missing from electronList tree"
74 assert t1.GetListOfBranches().Contains('__ncandidates__'), "candidate count branch is missing from electronList tree"
75
76 assert t2.GetListOfBranches().Contains('clusterE'), "clusterEnergy branch is missing from photonListTree"
77 assert t2.GetListOfBranches().Contains('p'), "p branch is missing from photonListTree"
78 assert t2.GetListOfBranches().Contains('__weight__'), "weight branch is missing from photonListTree"
79 assert t2.GetListOfBranches().Contains('__event__'), "event number branch is missing from photonList tree"
80 assert t2.GetListOfBranches().Contains('__run__'), "run number branch is missing from photonList tree"
81 assert t2.GetListOfBranches().Contains('__experiment__'), "experiment number branch is missing from photonList tree"
82 assert t2.GetListOfBranches().Contains('__production__'), "production number branch is missing from photonList tree"
83 assert t2.GetListOfBranches().Contains('__candidate__'), "candidate number branch is missing from photonList tree"
84 assert t2.GetListOfBranches().Contains('__ncandidates__'), "candidate count branch is missing from photonList tree"
85
86 nSignal = 0
87 nBckgrd = 0
88 for event in t1:
89 if event.isSignal == 1:
90 assert event.__weight__ == 1, f"Expected weight 1 for a true electron candidate got {event.__weight__}"
91 nSignal += 1
92 else:
93 assert event.__weight__ == 20, f"Expected weight 20 for a wrong electron candidate got {event.__weight__}"
94 nBckgrd += 1
95 assert nBckgrd < nSignal, "Expected less background than signal due to the large sampling rate"
96
97 for event in t2:
98 assert event.__weight__ == 1, f"Expected weight 1 for all photon candidates got {event.__weight__}"
99
100 assert os.path.isfile('eventNtuple.root'), "eventNtuple.root wasn't created"
101 f = ROOT.TFile('eventNtuple.root')
102 t = f.Get('eventTree')
103 assert bool(t), "eventTree isn't contained in file"
104 assert t.GetListOfBranches().Contains('nTracks'), "nTracks branch is missing"
105 assert t.GetListOfBranches().Contains('nKLMClusters'), "nKLMClusters branch is missing"
106 assert t.GetListOfBranches().Contains('__weight__'), "weight branch is missing"
107 assert t.GetListOfBranches().Contains('__event__'), "event number branch is missing"
108 assert t.GetListOfBranches().Contains('__run__'), "run number branch is missing"
109 assert t.GetListOfBranches().Contains('__experiment__'), "experiment number branch is missing"
110 assert t.GetListOfBranches().Contains('__production__'), "production number branch is missing"
111 assert not t.GetListOfBranches().Contains('__candidate__'), "candidate number branch is present in eventwise tree"
112 assert not t.GetListOfBranches().Contains('__ncandidates__'), "candidate count branch is present in eventwise tree"
113
114 t.GetEntry(0)
115 assert t.__run__ == 0, "run number not as expected"
116 assert t.__experiment__ == 1003, "experiment number not as expected"
117 assert t.__event__ == 1, "event number not as expected"
118 assert t.__production__ == 0, "production number not as expected"
119
120 nTracks_12 = 0
121 nTracks_11 = 0
122 for event in t:
123 if event.nTracks == 12:
124 assert event.__weight__ == 10, f"Expected weight 10 in an event with 12 tracks got {event.__weight__}"
125 nTracks_12 += 1
126 else:
127 assert event.__weight__ == 1, f"Expected weight 1 in an event with unequal 12 tracks got {event.__weight__}"
128 if event.nTracks == 11:
129 nTracks_11 += 1
130 assert nTracks_12 * 5 < nTracks_11, "Expected much less events with 12 tracks than with 11, due to the large sampling rate"
131
132 assert os.path.isfile('countersNtuple.root'), "eventNtuple.root wasn't created"
133 f = ROOT.TFile('countersNtuple.root')
134 t = f.Get('countersTree')
135 assert bool(t), "countersTree isn't contained in file"
136 assert t.GetListOfBranches().Contains('__event__'), "event number branch is missing"
137 assert t.GetListOfBranches().Contains('__run__'), "run number branch is missing"
138 assert t.GetListOfBranches().Contains('__experiment__'), "experiment number branch is missing"
139
140 t.GetEntry(0)
141 assert t.__run__ == 0, "run number not as expected"
142 assert t.__experiment__ == 1003, "experiment number not as expected"
143 assert t.__event__ == 1, "event number not as expected"
144 assert t.__production__ == 0, "production number not as expected"
145
146 t.GetEntry(9)
147 assert t.__run__ == 0, "run number not as expected"
148 assert t.__experiment__ == 1003, "experiment number not as expected"
149 assert t.__event__ == 10, "event number not as expected"
150 assert t.__production__ == 0, "production number not as expected"
def require_file(filename, data_type="", py_case=None)
Definition: __init__.py:54
def clean_working_directory()
Definition: __init__.py:189