Belle II Software development
test_VariablesToHistogram.py
1#!/usr/bin/env python3
2
3
10
11import os
12import basf2
13import ROOT
14import b2test_utils
15
16inputFile = b2test_utils.require_file('mdst16.root', 'validation')
17path = basf2.create_path()
18path.add_module('RootInput', inputFileName=inputFile)
19path.add_module('ParticleLoader', decayStrings=['e+'])
20
21# Write out electron id and mc information of all electron candidates into histograms
22path.add_module('VariablesToHistogram',
23 particleList='e+:all',
24 variables=[('electronID', 100, 0, 1), ('isSignal', 2, -0.5, 1.5)],
25 variables_2d=[('mcErrors', 513, -0.5, 512.5, 'isSignal', 2, -0.5, 1.5)],
26 fileName='particleListNtuple.root')
27
28# Write out number of tracks and ecl-clusters in every event into histograms
29path.add_module('VariablesToHistogram',
30 particleList='',
31 variables=[('nTracks', 31, -0.5, 30.5), ('nKLMClusters', 51, -0.5, 50.5)],
32 variables_2d=[('nTracks', 31, -0.5, 30.5, 'nKLMClusters', 51, -0.5, 50.5)],
33 fileName='eventNtuple.root')
34
35
37 basf2.process(path)
38
39 # Testing
40 assert os.path.isfile('particleListNtuple.root'), "particleListNtuple.root wasn't created"
41 f = ROOT.TFile('particleListNtuple.root')
42 t = f.Get('electronID')
43 assert bool(t), "electronID histogram isn't contained in file"
44 assert t.GetBinContent(1) > t.GetBinContent(
45 100), 'Expected fewer candidates with a highest electronID compared to lowest electronID'
46 t = f.Get('isSignal')
47 assert bool(t), "isSignal histogram isn't contained in file"
48 assert t.GetBinContent(1) > t.GetBinContent(2), 'Expected more background than signal'
49 signal_1d = t.GetBinContent(2)
50 t = f.Get('mcErrorsisSignal')
51 assert bool(t), "mcErrorsisSignal histogram isn't contained in file"
52
53 signal_2d = []
54 for i in range(513):
55 if i >= 4:
56 assert t.GetBinContent(i, 2) == 0, 'Expected no entries for mcError >= 4 and true electron candidates'
57 else:
58 assert t.GetBinContent(i, 1) == 0, 'Expected no entries for mcError < 4 and false electron candidates'
59 signal_2d.append(t.GetBinContent(i, 2))
60 assert signal_1d == sum(signal_2d), f'Expected same amount of signal in 1D and 2D histograms got {signal_1d} {sum(signal_2d)}'
61
62 assert os.path.isfile('eventNtuple.root'), "eventNtuple.root wasn't created"
63 f = ROOT.TFile('eventNtuple.root')
64 t = f.Get('nTracks')
65 assert bool(t), "nTracks histogram isn't contained in file"
66 assert t.GetBinContent(13) > t.GetBinContent(1), 'Expected more 12 track events than 1 track events'
67 assert t.GetBinContent(13) > t.GetBinContent(2), 'Expected more 12 track events than 2 track events'
68 assert t.GetBinContent(13) > t.GetBinContent(3), 'Expected more 12 track events than 3 track events'
69 assert t.GetBinContent(13) > t.GetBinContent(4), 'Expected more 12 track events than 4 track events'
70 assert t.GetBinContent(13) > t.GetBinContent(26), 'Expected more 12 track events than 26 track events'
71 assert t.GetBinContent(13) > t.GetBinContent(27), 'Expected more 12 track events than 27 track events'
72 assert t.GetBinContent(13) > t.GetBinContent(28), 'Expected more 12 track events than 28 track events'
73 assert t.GetBinContent(13) > t.GetBinContent(29), 'Expected more 12 track events than 29 track events'
74 ntracks_12_1d = t.GetBinContent(13)
75
76 t = f.Get('nKLMClusters')
77 assert bool(t), "nKLMClusters histogram isn't contained in file"
78
79 t = f.Get('nTracksnKLMClusters')
80 assert bool(t), "nTracksnKLMClusters 2d histogram isn't contained in file"
81 ntracks_12_2d = []
82 for i in range(53):
83 ntracks_12_2d.append(t.GetBinContent(13, i))
84 assert ntracks_12_1d == sum(ntracks_12_2d), \
85 f'Expected same amount of events with 12 tracks in 1D and 2D histograms got {ntracks_12_1d} {sum(ntracks_12_2d)}'
def require_file(filename, data_type="", py_case=None)
Definition: __init__.py:54
def clean_working_directory()
Definition: __init__.py:189