Belle II Software  release-08-01-10
simpleEventDisplay.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 # avoid race conditions beetween pyroot and GUI thread
13 from ROOT import PyConfig
14 PyConfig.StartGuiThread = False # noqa
15 
16 import basf2 as b2
17 import os
18 import glob
19 import sys
20 
21 from ROOT import Belle2
22 from ROOT import TCanvas, TH1F
23 from simulation import add_simulation
24 from reconstruction import add_reconstruction
25 
26 
27 # ------------------------------------------------------------------------------
28 # An example of a simple event display, made by using a python module.
29 # It displays timing distributions of modules produced by a generic BBbar event.
30 # Red histograms are marking the modules with at least one track impact.
31 # ------------------------------------------------------------------------------
32 
33 class TOPDisplay(b2.Module):
34 
35  '''
36  Simple event display for TOP.
37  It displays single event timing distributions of modules using TOPDigits.
38  Distributions of modules with the track impact are shown in red color.
39  '''
40 
41 
42  hist = [TH1F('h' + str(i), 'module#' + str(i), 128, 0.0, 256.0) for i in
43  range(16)]
44 
45  c1 = TCanvas('c1', 'TOP event display', 1000, 800)
46 
47  def initialize(self):
48  ''' Initialize the Module: open the canvas. '''
49 
50  self.c1c1.Divide(4, 4)
51  self.c1c1.Show()
52 
53  def event(self):
54  '''
55  Event processor: reset and fill the histograms, display them,
56  wait for user respond.
57  '''
58 
59  for i in range(16):
60  self.histhist[i].Reset()
61  self.histhist[i].SetFillColor(0)
62  self.histhist[i].SetLineColor(1)
63 
64  likelihoods = Belle2.PyStoreArray('TOPLikelihoods')
65  for likelihood in likelihoods:
66  if likelihood.getFlag() == 1:
67  exthit = likelihood.getRelated('ExtHits')
68  try:
69  moduleID = exthit.getCopyID()
70  self.histhist[moduleID - 1].SetFillColor(2)
71  self.histhist[moduleID - 1].SetLineColor(2)
72  except BaseException:
73  b2.B2ERROR('No relation to ExtHit')
74 
75  digits = Belle2.PyStoreArray('TOPDigits')
76  for digit in digits:
77  moduleID = digit.getModuleID()
78  tdc = digit.getRawTime()
79  self.histhist[moduleID - 1].Fill(tdc)
80 
81  for i in range(16):
82  self.c1c1.cd(i + 1)
83  self.histhist[i].Draw()
84 
85  self.c1c1.Update()
86 
87  # wait for user respond
88  user_input = input("Press Enter to continue or Q to quit ").lower().strip()
89  if user_input == "q":
90  evtMetaData = Belle2.PyStoreObj('EventMetaData')
91  evtMetaData.obj().setEndOfData()
92 
93 
94 # Check if the display is set (needed for canvas)
95 if 'DISPLAY' not in os.environ:
96  print('DISPLAY variable is not set')
97  print('- to set it in bash: export DISPLAY=:0')
98  print('- to set it in csh: setenv DISPLAY :0')
99  sys.exit()
100 
101 # Suppress messages other than errors
102 b2.set_log_level(b2.LogLevel.ERROR)
103 
104 # Create path
105 main = b2.create_path()
106 
107 # Set number of events to generate
108 eventinfosetter = b2.register_module('EventInfoSetter')
109 eventinfosetter.param('evtNumList', [1000])
110 main.add_module(eventinfosetter)
111 
112 # generate BBbar events
113 evtgeninput = b2.register_module('EvtGenInput')
114 main.add_module(evtgeninput)
115 
116 # Detector simulation
117 bg = None
118 if 'BELLE2_BACKGROUND_DIR' in os.environ:
119  bg = glob.glob(os.environ['BELLE2_BACKGROUND_DIR'] + '/*.root')
120 add_simulation(main, bkgfiles=bg)
121 
122 # Reconstruction
123 add_reconstruction(main)
124 
125 # dispay
126 main.add_module(TOPDisplay())
127 
128 # Show progress of processing
129 progress = b2.register_module('Progress')
130 main.add_module(progress)
131 
132 # Process events
133 b2.process(main)
134 
135 # Print call statistics
136 print(b2.statistics)
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67