Belle II Software  release-08-01-10
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 
91  # integrity check
92  if integrity_check:
93  print(len(trgary), 'copper(s)')
94  for evt in trgary:
95  for entry in range(evt.GetNumEntries()): # flattened. Usually only 1 entry
96  print('{:0x}'.format(GetNodeID(evt, entry)))
97  for bid in range(4):
98  if (GetNodeID(evt, entry), bid) in hslb:
99  count = GetDetectorNwords(evt, entry, bid)
100  print(bid, "exist, ", count, "words.")
101  return
102  dataList = []
103  for evt in trgary:
104  # assuming smaller Copper ID comes first
105  for entry in range(evt.GetNumEntries()): # flattened. Usually only 1 entry
106  # print('{:0x}'.format(GetNodeID(evt, entry)))
107  for bid in range(4):
108  if (GetNodeID(evt, entry), bid) not in hslb:
109  continue
110  count = GetDetectorNwords(evt, entry, bid)
111  if count == 0:
112  # continue
113  pass
114  bf = GetDetectorBuffer(evt, entry, bid)
115  ary = frombuffer(bf, np.uintc, count)
116  # obsolete: skipping dummy buffers
117  # if '{:0x}'.format(ary[0])[:4] == 'dddd':
118  # self.return_value(1)
119  # else:
120  # return
121  self.return_value(1)
122  dataList.append(join(ary))
123 
124  event = evtmeta.getEvent()
125  run = evtmeta.getRun()
126  subrun = evtmeta.getSubrun()
127  meta.append((event, run, subrun))
128  data.append(dataList)
129 
130 
131 # Set the log level to show only error and fatal messages
132 # set_log_level(LogLevel.ERROR)
133 b2.set_log_level(b2.LogLevel.INFO)
134 
135 # Create main path
136 main = b2.create_path()
137 
138 # input
139 if srootFile[-5:] == 'sroot':
140  root_input = b2.register_module('SeqRootInput')
141 else:
142  root_input = b2.register_module('RootInput')
143 root_input.param('inputFileName', srootFile)
144 
145 prog = b2.register_module('Progress')
146 
147 # Add modules to main path
148 main.add_module(root_input)
149 main.add_module(prog)
150 
151 readout = MinModule()
152 main.add_module(readout)
153 
154 emptypath = b2.create_path()
155 readout.if_false(emptypath)
156 
157 # check signal file before processing
158 if not pickleIt:
159  import b2vcd_48
160  vcdFile = sys.argv[3] if len(sys.argv) >= 4 else re.sub(r'.+/', '', re.sub(r'sroot', 'vcd', srootFile))
161  with open(pickleSigFile) as fin:
162  evtsize = [int(width) for width in fin.readline().split()]
163  b2.B2INFO('Interpreting B2L data format with dimension ' + str(evtsize))
164  atlas = b2vcd_48.makeAtlas(fin.read(), evtsize)
165 
166 # Process all events
167 b2.process(main)
168 
169 if pickleIt:
170  pica = pickleSigFile if isPickleFile else re.sub(r'.+/', 'ana/', re.sub(r'sroot', 'p', srootFile))
171  wfp = open(pica, 'wb')
172  pickle.dump(data, wfp, protocol=2)
173  pickle.dump(meta, wfp, protocol=2)
174  wfp.close()
175 
176 print(b2.statistics)
177 
178 if pickleIt:
179  b2.B2INFO('Output pickle file ' + pica + ' saved.')
180 else:
181  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