Belle II Software development
validate.py
1#!/usr/bin/env python3
2
3
10
11"""
12<header>
13 <output>timeDistributions.root</output>
14 <contact>marko.staric@ijs.si</contact>
15 <description>Runs BG mixer and makes validation histograms</description>
16</header>
17"""
18
19import basf2 as b2
20import os
21import glob
22import math
23from ROOT import Belle2
24from ROOT import TH1F, TFile, TNamed
25
26b2.set_random_seed(123452)
27
28
29class BGHistogrammer(b2.Module):
30
31 '''
32 Make validation histograms for BG mixer.
33 '''
34
35
36 simHits = [
37 'PXDSimHits',
38 'SVDSimHits',
39 'CDCSimHits',
40 'TOPSimHits',
41 'ARICHSimHits',
42 'ECLHits',
43 'KLMSimHits',
44 ]
45
46 n = len(simHits)
47
48 def initialize(self):
49 ''' Initialize the Module: set histogram axis titles, description and check'''
50
51
52 self.hist = [TH1F('h' + str(i), self.simHits[i], 400, -20, 20) for i in range(self.n)]
53
54 for i in range(self.n):
55 self.hist[i].GetXaxis().SetTitle('time [#mus]')
56 self.hist[i].GetYaxis().SetTitle('entries/bin')
57 descr = TNamed('Description', 'Time distribution of ' + self.simHits[i] +
58 ' for mixed background')
59 self.hist[i].GetListOfFunctions().Add(descr)
60 check = TNamed('Check', 'Distribution must be flat in the time window. ' +
61 'Bin statistics is not Poissonian.')
62 self.hist[i].GetListOfFunctions().Add(check)
63 contact = TNamed('Contact', 'marko.staric@ijs.si')
64 self.hist[i].GetListOfFunctions().Add(contact)
65 options = TNamed('MetaOptions', 'shifter')
66 self.hist[i].GetListOfFunctions().Add(options)
67
68 def event(self):
69 ''' Event processor: fill histograms '''
70
71 for i in range(self.n):
72 hits = Belle2.PyStoreArray(self.simHits[i])
73 for hit in hits:
74 time = hit.getGlobalTime() / 1000
75 self.hist[i].Fill(time)
76
77 def setErrors(self, hist):
78 ''' Set bin errors - they are not Poissonian'''
79 hmax = hist.GetMaximum()
80 temp = TH1F('temp', 'temporary', 100, 0, hmax * 1.1)
81 for i in range(hist.GetNbinsX()):
82 h = hist.GetBinContent(i + 1)
83 if h > hmax * 0.2:
84 temp.Fill(h)
85 mean = temp.GetMean()
86 rms = temp.GetStdDev()
87 if mean > 0:
88 for i in range(hist.GetNbinsX()):
89 h = hist.GetBinContent(i + 1)
90 e = 0
91 if h > 0:
92 e = rms * math.sqrt(h / mean)
93 hist.SetBinError(i + 1, e)
94
95 def terminate(self):
96 """ Write histograms to the file."""
97
98 tfile = TFile('timeDistributions.root', 'recreate')
99 for i in range(self.n):
100 self.setErrors(self.hist[i])
101 self.hist[i].Write()
102 tfile.Close()
103
104
105# Create path
106main = b2.create_path()
107
108# Set number of events to generate
109eventinfosetter = b2.register_module('EventInfoSetter')
110eventinfosetter.param({'evtNumList': [100], 'runList': [1]})
111main.add_module(eventinfosetter)
112
113# Gearbox: access to database (xml files)
114gearbox = b2.register_module('Gearbox')
115main.add_module(gearbox)
116
117# Geometry
118geometry = b2.register_module('Geometry')
119main.add_module(geometry)
120
121# mix beam background
122bg = None
123if 'BELLE2_BACKGROUND_MIXING_DIR' in os.environ:
124 bg = glob.glob(os.environ['BELLE2_BACKGROUND_MIXING_DIR'] + '/*.root')
125 if bg is None:
126 b2.B2FATAL('No beam background samples found in folder ' +
127 os.environ['BELLE2_BACKGROUND_MIXING_DIR'])
128 b2.B2INFO('Using background samples from ' + os.environ['BELLE2_BACKGROUND_MIXING_DIR'])
129 bkgmixer = b2.register_module('BeamBkgMixer')
130 bkgmixer.param('backgroundFiles', bg)
131 main.add_module(bkgmixer)
132else:
133 b2.B2FATAL('variable BELLE2_BACKGROUND_MIXING_DIR is not set')
134
135# fill validation histograms
136main.add_module(BGHistogrammer())
137
138# Show progress of processing
139main.add_module('Progress')
140
141# Process events
142b2.process(main)
143
144# Print call statistics
145print(b2.statistics)
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
def setErrors(self, hist)
Definition: validate.py:77
list simHits
list of SimHits to be histogramed
Definition: validate.py:36
hist
list of histograms
Definition: validate.py:52
def initialize(self)
Definition: validate.py:48
len n
length of simHits list
Definition: validate.py:46