Belle II Software  release-06-02-00
validateOverlay.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 """
13 <header>
14  <output>overlayPlots.root</output>
15  <contact>marko.staric@ijs.si</contact>
16  <description>Runs BG overlay and makes validation histograms</description>
17 </header>
18 """
19 
20 import basf2 as b2
21 from simulation import add_simulation
22 import os
23 import glob
24 from ROOT import Belle2
25 from ROOT import TH1F, TFile, TNamed
26 
27 b2.set_random_seed(123452)
28 
29 
30 class Histogrammer(b2.Module):
31 
32  '''
33  Make validation histograms for BG overlay.
34  '''
35 
36  def initialize(self):
37  ''' Initialize the Module: book histograms and set descriptions and checks'''
38 
39 
40  self.histhist = []
41  self.histhist.append(TH1F('PXDDigits', 'PXDDigits (no data reduction)',
42  100, 30000, 40000))
43  self.histhist.append(TH1F('SVDShaperDigits', 'SVDShaperDigits', 100, 0, 4000))
44  self.histhist.append(TH1F('CDCHits', 'CDCHits', 100, 0, 6000))
45  self.histhist.append(TH1F('TOPDigits', 'TOPDigits', 100, 0, 2000))
46  self.histhist.append(TH1F('ARICHDigits', 'ARICHDigits', 100, 0, 300))
47  self.histhist.append(TH1F('ECLDigits', 'ECLDigits, m_Amp > 500 (roughly 25 MeV)',
48  100, 0, 200))
49  self.histhist.append(TH1F('KLMDigits', 'KLMDigits', 150, 0, 150))
50 
51  for h in self.histhist:
52  h.SetXTitle('number of digits in event')
53  h.SetYTitle('entries per bin')
54  descr = TNamed('Description', 'Number of background ' + h.GetName() +
55  ' per event (with BG overlay only and no event generator)')
56  h.GetListOfFunctions().Add(descr)
57  check = TNamed('Check', 'Distribution must agree with its reference')
58  h.GetListOfFunctions().Add(check)
59  contact = TNamed('Contact', 'marko.staric@ijs.si')
60  h.GetListOfFunctions().Add(contact)
61  options = TNamed('MetaOptions', 'shifter')
62  h.GetListOfFunctions().Add(options)
63 
64  def event(self):
65  ''' Event processor: fill histograms '''
66 
67  for h in self.histhist:
68  digits = Belle2.PyStoreArray(h.GetName())
69  if h.GetName() == 'ECLDigits':
70  n = 0
71  for digit in digits:
72  if digit.getAmp() > 500: # roughly 25 MeV
73  n += 1
74  h.Fill(n)
75  else:
76  h.Fill(digits.getEntries())
77 
78  def terminate(self):
79  """ Write histograms to file."""
80 
81  tfile = TFile('overlayPlots.root', 'recreate')
82  for h in self.histhist:
83  h.Write()
84  tfile.Close()
85 
86 
87 bg = None
88 if 'BELLE2_BACKGROUND_DIR' in os.environ:
89  bg = glob.glob(os.environ['BELLE2_BACKGROUND_DIR'] + '/*.root')
90  if bg is None:
91  b2.B2FATAL('No beam background samples found in folder ' +
92  os.environ['BELLE2_BACKGROUND_DIR'])
93  b2.B2INFO('Using background samples from ' + os.environ['BELLE2_BACKGROUND_DIR'])
94 else:
95  b2.B2FATAL('variable BELLE2_BACKGROUND_DIR is not set')
96 
97 # Create path
98 main = b2.create_path()
99 
100 # Set number of events to generate
101 eventinfosetter = b2.register_module('EventInfoSetter')
102 eventinfosetter.param({'evtNumList': [1000], 'runList': [1]})
103 main.add_module(eventinfosetter)
104 
105 # Simulation
106 add_simulation(main, bkgfiles=bg, bkgOverlay=True, usePXDDataReduction=False, forceSetPXDDataReduction=True,
107  simulateT0jitter=False)
108 
109 # Make histograms
110 main.add_module(Histogrammer())
111 
112 # Show progress of processing
113 progress = b2.register_module('Progress')
114 main.add_module(progress)
115 
116 # Process events
117 b2.process(main)
118 
119 # Print call statistics
120 print(b2.statistics)
a (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:56