Belle II Software development
B2A904-LookUpTableCreation.py
1#!/usr/bin/env python3
2
3
10
11
19
20import basf2 as b2
21import random
22
23# Add some bin constructors
24
25
26def make_1D_bin(name, min_val, max_val):
27 return {name: [min_val, max_val]}
28
29
30def make_2D_bin(bin_x, bin_y):
31 bin_2d = bin_x.copy()
32 bin_2d.update(bin_y)
33 return bin_2d
34
35
36def make_3D_bin(bin_x, bin_y, bin_z):
37 bin_3d = bin_x.copy()
38 bin_3d.update(bin_y).update(bin_z)
39 return bin_3d
40
41
42# Define bin ranges. Bins may be of different size
43# They shouldn't event cover the whole parameter space
44bins_x = [make_1D_bin("p", 0, 2),
45 make_1D_bin("p", 2, 3),
46 make_1D_bin("p", 3, 4)]
47
48bins_y = [make_1D_bin("pz", 0, 1),
49 make_1D_bin("pz", 1, 5)]
50
51
52# Here would be input from the experts.
53# We don't have real calibration tables yet
54# So we use randomized info.
55# Bin IDs will be automatically assigned as follows:
56#
57# p|0-2|2-3|3-4|
58# pz| | | |
59# -------------
60# 0-1| 0 | 2 | 4 |
61# ---------------|
62# 1-5| 1 | 3 | 5 |
63# -------------
64
65tableIDNotSpec = []
66random.seed()
67for xbin in bins_x:
68 for ybin in bins_y:
69 weightInfo = {}
70 weightInfo["Weight"] = float(random.randint(0, 100)) / 100
71 weightInfo["StatErr"] = float(random.randint(100, 200)) / 100
72 weightInfo["SystErr"] = float(10)
73 tableIDNotSpec.append([weightInfo, make_2D_bin(xbin, ybin)])
74
75# Now let's assign binIDs manually (to be able to assign identical IDs for different bins)
76# Let's do this structure of bins (out-of-range bin -1 is assigned later):
77#
78# p|0-2|2-3|3-4|
79# pz| | | |
80# ---------------|
81# | |
82# ------------- |
83# 0-1| 43| 45| | |
84# ------------ | |
85# 1-5| 44| 46| |
86# ------------- |
87# | -1|
88# ---------------|
89tableIDSpec = []
90random.seed()
91binID = 42
92weightInfo = {}
93for xbin in bins_x:
94 for ybin in bins_y:
95 if binID < 46:
96 binID += 1
97 weightInfo = {}
98 weightInfo["Weight"] = float(random.randint(0, 100)) / 100
99 weightInfo["StatErr"] = float(random.randint(100, 200)) / 100
100 weightInfo["SystErr"] = float(10)
101 tableIDSpec.append([[weightInfo, make_2D_bin(xbin, ybin)], binID])
102
103# And of course let's define out-of-range bin info
104outOfRangeWeightInfo = {}
105outOfRangeWeightInfo["Weight"] = -1
106outOfRangeWeightInfo["StatErr"] = -1
107outOfRangeWeightInfo["SystErr"] = -1
108
109# Now, let's configure table creator
110addtable = b2.register_module('ParticleWeightingLookUpCreator')
111addtable.param('tableIDSpec', tableIDSpec)
112addtable.param('outOfRangeWeight', outOfRangeWeightInfo)
113addtable.param('experimentHigh', 1000)
114addtable.param('experimentLow', 0)
115addtable.param('runHigh', 1000)
116addtable.param('runLow', 0)
117addtable.param('tableName', "ParticleReweighting:TestMomentum")
118
119addtable2 = b2.register_module('ParticleWeightingLookUpCreator')
120addtable2.param('tableIDNotSpec', tableIDNotSpec)
121addtable2.param('outOfRangeWeight', outOfRangeWeightInfo)
122addtable2.param('experimentHigh', 1000)
123addtable2.param('experimentLow', 0)
124addtable2.param('runHigh', 1000)
125addtable2.param('runLow', 0)
126addtable2.param('tableName', "ParticleReweighting:TestMomentum2")
127
128eventinfosetter = b2.register_module('EventInfoSetter')
129eventinfosetter.param('evtNumList', [10])
130eventinfosetter.param('runList', [0])
131eventinfosetter.param('expList', [0])
132
133my_path = b2.create_path()
134my_path.add_module(addtable)
135my_path.add_module(addtable2)
136my_path.add_module(eventinfosetter)
137
138b2.process(my_path)