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