Belle II Software  release-05-01-25
test_VariablesToNtuple.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 import os
5 import basf2
6 import ROOT
7 import b2test_utils
8 from ROOT import Belle2
9 
10 inputFile = b2test_utils.require_file('mdst12.root', 'validation')
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')])
15 
16 # Write out electron id and momentum of all true electron candidates and every 10th wrong electron candidate
17 path.add_module('VariablesToNtuple',
18  particleList='e+',
19  variables=['electronID', 'p', 'isSignal'],
20  sampling=('isSignal', {1: 0, 0: 20}),
21  fileName='particleListNtuple.root',
22  treeName='electronListTree')
23 
24 # Write out two V2NT trees to the same file
25 path.add_module('VariablesToNtuple',
26  particleList='gamma',
27  variables=['clusterE', 'p', 'isSignal'],
28  fileName='particleListNtuple.root', # as above
29  treeName='photonListTree')
30 
31 # Write out number of tracks and ecl-clusters in every event, except for events with 12 tracks where we take only every 100th events
32 path.add_module('VariablesToNtuple',
33  particleList='',
34  variables=['nTracks', 'nKLMClusters'],
35  sampling=('nTracks', {12: 10}),
36  fileName='eventNtuple.root',
37  treeName='eventTree')
38 
39 # try to write out candidate counters even though they're already there
40 path.add_module('VariablesToNtuple',
41  particleList='',
42  variables=['expNum', 'runNum', 'evtNum'],
43  fileName='countersNtuple.root',
44  treeName='countersTree')
45 
46 
48  basf2.process(path)
49 
50  # Testing
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"
67 
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"
76 
77  nSignal = 0
78  nBckgrd = 0
79  for event in t1:
80  if event.isSignal == 1:
81  assert event.__weight__ == 1, "Expected weight 1 for a true electron candidate got {}".format(event.__weight__)
82  nSignal += 1
83  else:
84  assert event.__weight__ == 20, "Expected weight 20 for a wrong electron candidate got {}".format(event.__weight__)
85  nBckgrd += 1
86  assert nBckgrd < nSignal, "Expected less background than signal due to the large sampling rate"
87 
88  for event in t2:
89  assert event.__weight__ == 1, "Expected weight 1 for all photon candidates got {}".format(event.__weight__)
90 
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"
103 
104  t.GetEntry(0)
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"
108 
109  nTracks_12 = 0
110  nTracks_11 = 0
111  for event in t:
112  if event.nTracks == 12:
113  assert event.__weight__ == 10, "Expected weight 10 in an event with 12 tracks got {}".format(event.__weight__)
114  nTracks_12 += 1
115  else:
116  assert event.__weight__ == 1, "Expected weight 1 in an event with unequal 12 tracks got {}".format(event.__weight__)
117  if event.nTracks == 11:
118  nTracks_11 += 1
119  assert nTracks_12 * 5 < nTracks_11, "Expected much less events with 12 tracks than with 11, due to the large sampling rate"
120 
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"
128 
129  t.GetEntry(0)
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"
133 
134  t.GetEntry(9)
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"
basf2.process
def process(path, max_event=0)
Definition: __init__.py:25
b2test_utils.clean_working_directory
def clean_working_directory()
Definition: __init__.py:176
b2test_utils.require_file
def require_file(filename, data_type="", py_case=None)
Definition: __init__.py:47