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