Belle II Software  release-08-01-10
checkTBCOutput.py
1 #!/usr/bin/env python3
2 
3 
10 
11 # ---------------------------------------------------------------------------------------------
12 # Check TBC output root files for the deeps in sample occupancies.
13 #
14 # usage: basf2 checkTBCOutput.py <path_to_files>
15 # <path_to_files> can include wildcards (prepended by backslash), example: tbc/\*/\*.root
16 # ---------------------------------------------------------------------------------------------
17 
18 import sys
19 import glob
20 from ROOT import TFile
21 from basf2.utils import pretty_print_table
22 
23 
24 if len(sys.argv) < 2:
25  print('usage: basf2', sys.argv[0], '<path_to_files>')
26  print(' <path_to_files> can include wildcards (prepended by backslash), example: tbc/\\*/\\*.root')
27  sys.exit()
28 
29 files = glob.glob(sys.argv[1])
30 if len(files) == 0:
31  print('No root files found in ' + sys.argv[1])
32  sys.exit()
33 
34 
35 def num_below_cut(h):
36  '''
37  Returns the number of samples with low occupancy.
38  :param h: histogram of occupancy (number of calpulses per sample)
39  '''
40 
41  h.Rebin(2)
42  cut = h.Integral() / h.GetNbinsX() * 0.8
43  s = 0
44  n = 0
45  for i in range(h.GetNbinsX()):
46  y = h.GetBinContent(i + 1)
47  if y > cut:
48  s += y
49  n += 1
50  cut = s / n * 0.7
51  n = 0
52  for i in range(h.GetNbinsX()):
53  y = h.GetBinContent(i + 1)
54  if y < cut:
55  n += 1
56  return n
57 
58 
59 def print_table(title, table):
60  '''
61  Prints the table.
62  :param title: table title
63  :param table: table
64  '''
65 
66  print()
67  print(title)
68  table_rows = [['bs' + str(b) + ' '] + [str(table[s][b]) for s in range(16)] for b in range(4)]
69  table_rows.insert(0, [''] + ['s' + str(s) for s in range(1, 17)])
70  pretty_print_table(table_rows, [4 for i in range(17)])
71 
72 
73 ncal = 0
74 nlow = 0
75 numcal = [[0 for bs in range(4)] for slot in range(16)]
76 numlow = [[0 for bs in range(4)] for slot in range(16)]
77 for fileName in sorted(files):
78  try:
79  slot_bs = fileName.split('/')[-1].split('-')[0].split('tbcSlot')[1].split('_')
80  slot = int(slot_bs[0])
81  bs = int(slot_bs[1])
82  print(fileName, 'slot =', slot, 'bs =', bs)
83  except BaseException:
84  print(fileName, '--> file ignored')
85  continue
86  tfile = TFile.Open(fileName)
87  if not tfile:
88  print('--> Error: cannot open this file')
89  continue
90  success = tfile.Get("success")
91  if not success:
92  print('--> Error: histogram named success not found')
93  continue
94  nc = 0
95  for chan in range(success.GetNbinsX()):
96  if success.GetBinContent(chan + 1) == 0:
97  continue
98  ncal += 1
99  nc += 1
100  numcal[slot-1][bs] += 1
101  hname = 'sampleOccup_ch' + str(chan)
102  h = tfile.Get(hname)
103  if not h:
104  print('--> Error: sample occupancy histogram for channel ' + str(chan) + ' not found')
105  continue
106  n = num_below_cut(h)
107  if n > 2:
108  print(' ' + h.GetTitle(), 'is low in', 2 * n, '/ 256 samples')
109  nlow += 1
110  numlow[slot-1][bs] += 1
111  print('-->', nc, 'channels calibrated')
112 
113 print_table('Number of successfully calibrated channels:', numcal)
114 print_table('Number of channels with deeps in sample occupancy:', numlow)
115 
116 print('\nCalibration summary: ')
117 print(' - successfully calibrated channels:', ncal, '/', 512 * 16)
118 print(' - constants maybe not reliable for', nlow, '/', ncal, 'channels.',
119  'If this fraction is large, check calpulse selection window in runTBC.py.')