Belle II Software  release-08-01-10
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 
19 import basf2 as b2
20 import os
21 import glob
22 import math
23 from ROOT import Belle2
24 from ROOT import TH1F, TFile, TNamed
25 
26 b2.set_random_seed(123452)
27 
28 
29 class 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.histhist = [TH1F('h' + str(i), self.simHitssimHits[i], 400, -20, 20) for i in range(self.nn)]
53 
54  for i in range(self.nn):
55  self.histhist[i].GetXaxis().SetTitle('time [#mus]')
56  self.histhist[i].GetYaxis().SetTitle('entries/bin')
57  descr = TNamed('Description', 'Time distribution of ' + self.simHitssimHits[i] +
58  ' for mixed background')
59  self.histhist[i].GetListOfFunctions().Add(descr)
60  check = TNamed('Check', 'Distribution must be flat in the time window. ' +
61  'Bin statistics is not Poissonian.')
62  self.histhist[i].GetListOfFunctions().Add(check)
63  contact = TNamed('Contact', 'marko.staric@ijs.si')
64  self.histhist[i].GetListOfFunctions().Add(contact)
65  options = TNamed('MetaOptions', 'shifter')
66  self.histhist[i].GetListOfFunctions().Add(options)
67 
68  def event(self):
69  ''' Event processor: fill histograms '''
70 
71  for i in range(self.nn):
72  hits = Belle2.PyStoreArray(self.simHitssimHits[i])
73  for hit in hits:
74  time = hit.getGlobalTime() / 1000
75  self.histhist[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.nn):
100  self.setErrorssetErrors(self.histhist[i])
101  self.histhist[i].Write()
102  tfile.Close()
103 
104 
105 # Create path
106 main = b2.create_path()
107 
108 # Set number of events to generate
109 eventinfosetter = b2.register_module('EventInfoSetter')
110 eventinfosetter.param({'evtNumList': [100], 'runList': [1]})
111 main.add_module(eventinfosetter)
112 
113 # Gearbox: access to database (xml files)
114 gearbox = b2.register_module('Gearbox')
115 main.add_module(gearbox)
116 
117 # Geometry
118 geometry = b2.register_module('Geometry')
119 main.add_module(geometry)
120 
121 # mix beam background
122 bg = None
123 if '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)
132 else:
133  b2.B2FATAL('variable BELLE2_BACKGROUND_MIXING_DIR is not set')
134 
135 # fill validation histograms
136 main.add_module(BGHistogrammer())
137 
138 # Show progress of processing
139 main.add_module('Progress')
140 
141 # Process events
142 b2.process(main)
143 
144 # Print call statistics
145 print(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
n
length of simHits list
Definition: validate.py:46