Belle II Software development
readout.py
1#!/usr/bin/env python3
2
3
10
11import basf2 as b2
12
13from ROOT import Belle2
14import numpy as np
15import sys
16# from testpkg.bitstring import BitArray
17from bitstring import BitArray
18import pickle
19import re
20
21
22# we need to modify this to get data from different coppers
23
24# 4 2Ds
25# hslb = (('11000001', 'b'),
26# ('11000001', 'a'),
27# ('11000002', 'a'),
28# ('11000002', 'b'))
29
30# 2D0 and NN
31
32hslb = (('11000001', 'b'),
33 ('11000003', 'b'),
34 )
35
36
37integrity_check = False
38
39[steering, srootFile] = sys.argv[:2]
40if len(sys.argv) >= 3:
41 pickleSigFile = sys.argv[2]
42 isPickleFile = pickleSigFile[pickleSigFile.rfind('.'):] == '.p'
43else:
44 pickleSigFile = None
45 isPickleFile = False
46
47pickleIt = len(sys.argv) == 2 or isPickleFile
48
49
50def finesse(x): return ord(x) - ord('a')
51
52
53def copper(x): return int(x, 16)
54
55
56hslb = [(copper(ele[0]), finesse(ele[1])) for ele in hslb]
57
58
59def join(ary):
60 return BitArray([]).join([BitArray(uint=i, length=32) for i in ary])
61
62
63data = []
64meta = []
65
66
67class MinModule(b2.Module):
68
69 """
70 Example module to drop into ipython and create some objects to look at.
71 If you just want to start IPython and create PyStoreArray etc.
72 interactively in your own steering file, the 'Interactive' module
73 might be of interest.
74 """
75
76 def event(self):
77 """
78 reimplement Module::event()
79 """
80 self.return_value(0)
81 # function namespace caching
82 frombuffer = np.frombuffer
83 GetNodeID = Belle2.RawTRG.GetNodeID
84 GetDetectorNwords = Belle2.RawTRG.GetDetectorNwords
85 GetDetectorBuffer = Belle2.RawTRG.GetDetectorBuffer
86 trgary = Belle2.PyStoreArray("RawTRGs")
87
88 evtmeta = Belle2.PyStoreObj("EventMetaData")
89
90 # integrity check
91 if integrity_check:
92 print(len(trgary), 'copper(s)')
93 for evt in trgary:
94 for entry in range(evt.GetNumEntries()): # flattened. Usually only 1 entry
95 print(f'{GetNodeID(evt, entry):0x}')
96 for bid in range(4):
97 if (GetNodeID(evt, entry), bid) in hslb:
98 count = GetDetectorNwords(evt, entry, bid)
99 print(bid, "exist, ", count, "words.")
100 return
101 dataList = []
102 for evt in trgary:
103 # assuming smaller Copper ID comes first
104 for entry in range(evt.GetNumEntries()): # flattened. Usually only 1 entry
105 # print('{:0x}'.format(GetNodeID(evt, entry)))
106 for bid in range(4):
107 if (GetNodeID(evt, entry), bid) not in hslb:
108 continue
109 count = GetDetectorNwords(evt, entry, bid)
110 if count == 0:
111 # continue
112 pass
113 bf = GetDetectorBuffer(evt, entry, bid)
114 ary = frombuffer(bf, np.uintc, count)
115 # obsolete: skipping dummy buffers
116 # if '{:0x}'.format(ary[0])[:4] == 'dddd':
117 # self.return_value(1)
118 # else:
119 # return
120 self.return_value(1)
121 dataList.append(join(ary))
122
123 event = evtmeta.getEvent()
124 run = evtmeta.getRun()
125 subrun = evtmeta.getSubrun()
126 meta.append((event, run, subrun))
127 data.append(dataList)
128
129
130# Set the log level to show only error and fatal messages
131# set_log_level(LogLevel.ERROR)
132b2.set_log_level(b2.LogLevel.INFO)
133
134# Create main path
135main = b2.create_path()
136
137# input
138if srootFile[-5:] == 'sroot':
139 root_input = b2.register_module('SeqRootInput')
140else:
141 root_input = b2.register_module('RootInput')
142root_input.param('inputFileName', srootFile)
143
144prog = b2.register_module('Progress')
145
146# Add modules to main path
147main.add_module(root_input)
148main.add_module(prog)
149
150readout = MinModule()
151main.add_module(readout)
152
153emptypath = b2.create_path()
154readout.if_false(emptypath)
155
156# check signal file before processing
157if not pickleIt:
158 import b2vcd_48
159 vcdFile = sys.argv[3] if len(sys.argv) >= 4 else re.sub(r'.+/', '', re.sub(r'sroot', 'vcd', srootFile))
160 with open(pickleSigFile) as fin:
161 evtsize = [int(width) for width in fin.readline().split()]
162 b2.B2INFO('Interpreting B2L data format with dimension ' + str(evtsize))
163 atlas = b2vcd_48.makeAtlas(fin.read(), evtsize)
164
165# Process all events
166b2.process(main)
167
168if pickleIt:
169 pica = pickleSigFile if isPickleFile else re.sub(r'.+/', 'ana/', re.sub(r'sroot', 'p', srootFile))
170 wfp = open(pica, 'wb')
171 pickle.dump(data, wfp, protocol=2)
172 pickle.dump(meta, wfp, protocol=2)
173 wfp.close()
174
175print(b2.statistics)
176
177if pickleIt:
178 b2.B2INFO('Output pickle file ' + pica + ' saved.')
179else:
180 b2vcd_48.writeVCD(meta, data, atlas, vcdFile, evtsize)
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67