Belle II Software  release-08-01-10
createTSLUT.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 import numpy as np
13 
14 """
15 Create a left/right LUT from a table of true left/right counts for each pattern.
16 (see generateTrueLRTable.py)
17 
18 Condition for left/right:
19 
20 left: nBkg <= b * nTotal and nLeft > p * nMC + 3 * sqrt(p * (1 - p) * nMC)
21 right: nBkg <= b * nTotal and nRight > p * nMC + 3 * sqrt(p * (1 - p) * nMC)
22 unknown: otherwise
23 """
24 
25 # load table with true left/right
26 # shape: [[nTrueRight, nTrueLeft, nTrueBkg], ...]
27 innerTrueLRTable = np.loadtxt('innerTrueLRTable_Bkg1.0_1.dat')
28 outerTrueLRTable = np.loadtxt('outerTrueLRTable_Bkg1.0_1.dat')
29 for i in range(2, 5):
30  innerTrueLRTable += np.loadtxt('innerTrueLRTable_Bkg1.0_%d.dat' % i)
31  outerTrueLRTable += np.loadtxt('outerTrueLRTable_Bkg1.0_%d.dat' % i)
32 
33 # define thresholds for left/right
34 b = 0.8
35 p = 0.7
36 
37 # filenames for the new LUTs
38 innerLUTFilename = "innerLUT_Bkg_p%.2f_b%.2f.coe" % (p, b)
39 outerLUTFilename = "outerLUT_Bkg_p%.2f_b%.2f.coe" % (p, b)
40 
41 
42 def isValidInnerPattern(pattern):
43  masks = [(1 << 1),
44  (1 << 2) + (1 << 3),
45  (1 << 4) + (1 << 5) + (1 << 6),
46  (1 << 7) + (1 << 8) + (1 << 9) + (1 << 10),
47  (1 << 11) + (1 << 12) + (1 << 13) + (1 << 14) + (1 << 15)]
48  nLayers = 0
49  for i in range(5):
50  if pattern & masks[i]:
51  nLayers += 1
52  return (nLayers >= 4)
53 
54 
55 def isValidOuterPattern(pattern):
56  masks = [(1 << 1) + (1 << 2) + (1 << 3),
57  (1 << 4) + (1 << 5),
58  (1 << 6),
59  (1 << 7) + (1 << 8),
60  (1 << 9) + (1 << 10) + (1 << 11)]
61  nLayers = 0
62  for i in range(5):
63  if pattern & masks[i]:
64  nLayers += 1
65  return (nLayers >= 4)
66 
67 
68 def createLUT(TrueLRTable, inner):
69  LUT = np.zeros(len(TrueLRTable))
70  # loop over patterns and check fraction of correct left/right
71  for pattern, trueLR in enumerate(TrueLRTable):
72  # check if pattern is valid
73  if inner:
74  if not isValidInnerPattern(pattern):
75  continue
76  else:
77  if not isValidOuterPattern(pattern):
78  continue
79  if trueLR[2] > b * np.sum(trueLR):
80  LUT[pattern] = 3
81  continue
82  threshold = p * np.sum(trueLR[:2]) + 3 * np.sqrt(p * (1 - p) * np.sum(trueLR[:2]))
83  if trueLR[0] > threshold:
84  LUT[pattern] = 1
85  elif trueLR[1] > threshold:
86  LUT[pattern] = 2
87  else:
88  LUT[pattern] = 3
89  return LUT
90 
91 
92 innerLUT = createLUT(innerTrueLRTable, inner=True)
93 outerLUT = createLUT(outerTrueLRTable, inner=False)
94 
95 # save the resulting LUTs
96 innerLUTFile = open(innerLUTFilename, 'w')
97 innerLUTFile.write("memory_initialization_radix=10;\n")
98 innerLUTFile.write("memory_initialization_vector=\n")
99 innerLUTFile.write(",\n".join("%d" % i for i in innerLUT))
100 innerLUTFile.write(";\n\n")
101 
102 outerLUTFile = open(outerLUTFilename, 'w')
103 outerLUTFile.write("memory_initialization_radix=10;\n")
104 outerLUTFile.write("memory_initialization_vector=\n")
105 outerLUTFile.write(",\n".join("%d" % i for i in outerLUT))
106 outerLUTFile.write(";\n\n")