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