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