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