Belle II Software  release-06-01-15
showFEWaveforms.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 # ---------------------------------------------------------------------------------------
13 # Display of waveforms with feature extraction points superimposed
14 # Unpacker is set for Interim FE format v2.1
15 # Usage: basf2 showFEWaveforms.py -i <file_name.sroot>
16 # ---------------------------------------------------------------------------------------
17 
18 # avoid race conditions beetween pyroot and GUI thread
19 from ROOT import PyConfig
20 PyConfig.StartGuiThread = False # noqa
21 
22 import basf2 as b2
23 from ROOT import Belle2
24 from ROOT import TH1F, TCanvas, TGraph
25 
26 
27 class WFDisplay(b2.Module):
28 
29  '''
30  Simple event display of waveforms with feature extraction points
31  '''
32 
33 
34  hist = [TH1F('h' + str(i), 'wf', 64 * 4, 0.0, 64.0 * 4) for i in range(4)]
35  for i in range(4):
36  hist[i].SetXTitle("sample number")
37  hist[i].SetYTitle("sample value [ADC counts]")
38  hist[i].SetLabelSize(0.06, "XY")
39  hist[i].SetTitleSize(0.07, "XY")
40  hist[i].SetTitleOffset(0.7, "XY")
41 
42  gpaint = [TGraph() for i in range(4)]
43 
44  graphs = [[TGraph(5)] for i in range(4)]
45 
46  tlpfgraphs = [[TGraph(2)] for i in range(4)]
47 
48  c1 = TCanvas('c1', 'WF event display', 800, 800)
49 
50  pdfFile = 'waveforms'
51 
52  def initialize(self):
53  ''' Initialize the Module: open the canvas. '''
54 
55  self.c1c1.Divide(1, 4)
56  self.c1c1.Show()
57  print('WARNING: Template Fit is still under development and currently returns wrong position for rising edge!.')
58 
59  def wait(self):
60  ''' wait for user respond '''
61 
62  user_input = input("Press Enter to continue, P to print, or Q to quit ").lower().strip()
63  if user_input == "p":
64  filename = self.pdfFilepdfFilepdfFile + '.pdf'
65  self.c1c1.SaveAs(filename)
66  print('Canvas saved to file:', filename)
67  return False
68  elif user_input == "q":
69  evtMetaData = Belle2.PyStoreObj('EventMetaData')
70  evtMetaData.obj().setEndOfData()
71  return True
72  else:
73  return False
74 
75  def draw(self, k, event, run):
76  ''' Draw histograms and wait for user respond '''
77 
78  self.c1c1.Clear()
79  self.c1c1.Divide(1, 4)
80  title = 'WF event display:' + ' run ' + str(run) + ' event ' \
81  + str(event)
82  self.c1c1.SetTitle(title)
83  for i in range(k):
84  self.c1c1.cd(i + 1)
85  self.histhist[i].Draw()
86  if self.gpaintgpaint[i].GetN() > 0:
87  self.gpaintgpaint[i].Draw("same")
88  for graph in self.graphsgraphs[i]:
89  graph.Draw("sameP")
90  for tlpfgraph in self.tlpfgraphstlpfgraphs[i]:
91  tlpfgraph.Draw("sameP")
92  self.c1c1.Update()
93  stat = self.waitwait()
94  return stat
95 
96  def set_gpaint(self, waveform, k):
97  ''' construct a graph to paint waveform differently after window discontinuity '''
98 
99  if waveform.areWindowsInOrder():
100  self.gpaintgpaint[k].Set(0)
101 
102  windows = waveform.getStorageWindows()
103  i0 = windows.size()
104  for i in range(1, windows.size()):
105  diff = windows[i] - windows[i - 1]
106  if diff < 0:
107  diff += 512
108  if diff != 1:
109  i0 = i
110  break
111  n = (windows.size() - i0) * 64 * 2
112  self.gpaintgpaint[k].Set(n)
113  low = i0 * 64
114  for i in range(low, self.histhist[k].GetNbinsX()):
115  x = self.histhist[k].GetBinCenter(i + 1)
116  dx = self.histhist[k].GetBinWidth(i + 1) / 2
117  y = self.histhist[k].GetBinContent(i + 1)
118  ii = (i - low) * 2
119  self.gpaintgpaint[k].SetPoint(ii, x - dx, y)
120  self.gpaintgpaint[k].SetPoint(ii + 1, x + dx, y)
121 
122  def event(self):
123  '''
124  Event processor: fill the histograms, display them,
125  wait for user respond.
126  '''
127 
128  evtMetaData = Belle2.PyStoreObj('EventMetaData')
129  event = evtMetaData.obj().getEvent()
130  run = evtMetaData.obj().getRun()
131 
132  waveforms = Belle2.PyStoreArray('TOPRawWaveforms')
133 
134  k = 0
135  # nHits = 0
136  fname = 'waveforms_run' + str(run) + '_event' + str(event) + '_chan'
137 
138  self.pdfFilepdfFilepdfFile = fname
139  for waveform in waveforms:
140  slot = waveform.getModuleID()
141  chan = waveform.getChannel()
142  self.pdfFilepdfFilepdfFile = self.pdfFilepdfFilepdfFile + '-S' + str(slot) + '_' + str(chan)
143  wf = waveform.getWaveform()
144  self.histhist[k].Reset()
145  numSamples = waveform.getSize()
146  startSample = waveform.getStartSample()
147  self.histhist[k].SetBins(numSamples, float(startSample), float(startSample + numSamples))
148  title = 'S' + str(slot) + ' chan ' + str(chan) + ' win'
149  if waveform.getStorageWindows().empty():
150  title += ' ' + str(waveform.getStorageWindow())
151  else:
152  for window in waveform.getStorageWindows():
153  title += ' ' + str(window)
154  self.histhist[k].SetTitle(title)
155  self.histhist[k].SetStats(False)
156  self.histhist[k].SetLineColor(8)
157 
158  i = 0
159  for sample in wf:
160  i = i + 1
161  self.histhist[k].SetBinContent(i, sample)
162 
163  self.gpaintgpaint[k].SetLineColor(9)
164  self.set_gpaintset_gpaint(waveform, k)
165 
166  rawDigits = waveform.getRelationsWith("TOPRawDigits")
167  self.graphsgraphs[k].clear()
168  self.tlpfgraphstlpfgraphs[k].clear()
169  for raw in rawDigits:
170  graph = TGraph(5)
171  graph.SetMarkerStyle(24)
172  graph.SetPoint(0, raw.getSampleRise() + 0.5, raw.getValueRise0())
173  graph.SetPoint(1, raw.getSampleRise() + 1.5, raw.getValueRise1())
174  graph.SetPoint(2, raw.getSamplePeak() + 0.5, raw.getValuePeak())
175  graph.SetPoint(3, raw.getSampleFall() + 0.5, raw.getValueFall0())
176  graph.SetPoint(4, raw.getSampleFall() + 1.5, raw.getValueFall1())
177  if raw.isMadeOffline():
178  graph.SetMarkerStyle(5)
179  if raw.isFEValid() and not raw.isAtWindowDiscontinuity():
180  graph.SetMarkerColor(2)
181  if raw.isPedestalJump():
182  graph.SetMarkerColor(3)
183  else:
184  graph.SetMarkerColor(4)
185  self.graphsgraphs[k].append(graph)
186 
187  tlpfResult = raw.getRelated("TOPTemplateFitResults")
188  if tlpfResult:
189  tlpfgraph = TGraph(2)
190  tlpfgraph.SetMarkerStyle(25)
191 # tlpfgraph.SetPoint(0, tlpfResult.getMean(), tlpfResult.getAmplitude() + tlpfResult.getBackgroundOffset())
192 # tlpfgraph.SetPoint(1, tlpfResult.getMean(), tlpfResult.getBackgroundOffset())
193  print('Template Fit Chisquare: ', tlpfResult.getChisquare())
194  self.tlpfgraphstlpfgraphs[k].append(tlpfgraph)
195 
196  k = k + 1
197  if k == 4:
198  stat = self.drawdraw(k, event, run)
199  if stat:
200  return
201  k = 0
202  self.pdfFilepdfFilepdfFile = fname
203 
204  if k > 0:
205  self.drawdraw(k, event, run)
206 
207 
208 b2.set_log_level(b2.LogLevel.INFO)
209 
210 # Define a global tag (note: the one given bellow will become out-dated!)
211 b2.use_central_database('data_reprocessing_proc8')
212 
213 # Create path
214 main = b2.create_path()
215 
216 # input
217 roinput = b2.register_module('SeqRootInput')
218 # roinput = register_module('RootInput')
219 main.add_module(roinput)
220 
221 # conversion from RawCOPPER or RawDataBlock to RawTOP (needed only in PocketDAQ)
222 rawconverter = b2.register_module('Convert2RawDet')
223 main.add_module(rawconverter)
224 
225 # Initialize TOP geometry parameters (creation of Geant geometry is not needed)
226 main.add_module('TOPGeometryParInitializer')
227 
228 # Unpacking
229 unpack = b2.register_module('TOPUnpacker')
230 main.add_module(unpack)
231 
232 # Add multiple hits from waveforms
233 featureExtractor = b2.register_module('TOPWaveformFeatureExtractor')
234 main.add_module(featureExtractor)
235 
236 # Convert to TOPDigits
237 converter = b2.register_module('TOPRawDigitConverter')
238 converter.param('useSampleTimeCalibration', False)
239 converter.param('useChannelT0Calibration', False)
240 converter.param('useModuleT0Calibration', False)
241 converter.param('useCommonT0Calibration', False)
242 main.add_module(converter)
243 
244 # Display waveforms
245 main.add_module(WFDisplay())
246 
247 # Print progress
248 progress = b2.register_module('Progress')
249 main.add_module(progress)
250 
251 # Process events
252 b2.process(main)
253 
254 # Print statistics
255 print(b2.statistics)
a (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:56
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67
def draw(self, k, event, run)
list graphs
graphs for FE points
string pdfFile
output file name
list gpaint
graphs for painting waveforms after the gap in window number
list tlpfgraphs
graphs for template fit points
list hist
1D histograms
def set_gpaint(self, waveform, k)
static ExpRun getRun(map< ExpRun, pair< double, double >> runs, double t)
Get exp number + run number from time.
Definition: Splitter.cc:264