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