Belle II Software development
generateSADBg.py
1#!/usr/bin/env python3
2
3
10
11# ------------------------------------------------------------------------------------
12# BG simulation using SAD input files
13#
14# make a link to the SAD files before running it (at KEKCC):
15# (ln -s /home/belle/staric/public/basf2/SADfiles input - sorry this files are outdated!)
16#
17# usage:
18# basf2 generateSADBg.py bgType accRing equivTime_us num [sampleType phase outdir]
19# arguments:
20# bgType Coulomb, Coulomb_base, Coulomb_dynamic, Touschek, Brems, Brems_base, Brems_dynamic
21# accRing LER or HER
22# equivTime_us equivalent SuperKEKB running time in micro-seconds
23# num output file number
24# sampleType one of: study, usual, PXD, ECL (D = usual)
25# phase 2, 31 (= early phase 3, ie Run 1), 32 (= Run 2) or 3 (D = 3)
26# outdir output directory path (D = output)
27# -------------------------------------------------------------------------------------
28
29import basf2 as b2
30import sys
31import os
32from background import add_output
33
34# read parameters
35
36argvs = sys.argv
37argc = len(argvs)
38
39if argc == 5:
40 bgType = argvs[1] # Coulomb, Coulomb_base, Coulomb_dynamic, Touschek, Brems, Brems_base, Brems_dynamic
41 accRing = argvs[2] # LER or HER
42 equivTime = argvs[3] # equivalent SuperKEKB running time in micro-seconds
43 num = argvs[4] # output file number
44 sampleType = 'usual' # study, usual, PXD, ECL
45 phase = 3 # phase number
46 outdir = 'output' # output directory path
47elif argc == 6:
48 bgType = argvs[1]
49 accRing = argvs[2]
50 equivTime = argvs[3]
51 num = argvs[4]
52 sampleType = argvs[5]
53 phase = 3
54 outdir = 'output'
55elif argc == 7:
56 bgType = argvs[1]
57 accRing = argvs[2]
58 equivTime = argvs[3]
59 num = argvs[4]
60 sampleType = argvs[5]
61 phase = int(argvs[6])
62 outdir = 'output'
63elif argc == 8:
64 bgType = argvs[1]
65 accRing = argvs[2]
66 equivTime = argvs[3]
67 num = argvs[4]
68 sampleType = argvs[5]
69 phase = int(argvs[6])
70 outdir = argvs[7]
71else:
72 print('Usage:')
73 print(' basf2', argvs[0], 'bgType accRing equivTime_us num [sampleType phase outdir]')
74 print('Arguments:')
75 print(' bgType Coulomb, Coulomb_base, Coulomb_dynamic, Touschek, Brems, Brems_base, Brems_dynamic')
76 print(' accRing LER or HER')
77 print(' equivTime_us equivalent SuperKEKB running time in micro-seconds')
78 print(' num output file number')
79 print(' sampleType one of: study, usual, PXD, ECL (D = usual)')
80 print(' phase 2, 31 (= early phase 3, ie Run 1), 32 (= Run 2) or 3 (D = 3)')
81 print(' outdir output directory path (D = output)')
82 sys.exit()
83
84# set sub-directories of SAD files
85
86if phase == 3:
87 subdir = 'phase3/'
88elif phase == 31:
89 subdir = 'phase3-early/'
90elif phase == 32:
91 subdir = 'phase3-Run2/'
92elif phase == 2:
93 subdir = 'phase2/'
94else:
95 print('phase ', phase, 'not supported')
96 sys.exit()
97
98# set parameters
99
100bgType = bgType + '_' + accRing
101sadFile = 'input/' + subdir + bgType + '.root'
102realTime = float(equivTime) * 1000
103fname = bgType + '_' + sampleType + '-phase' + str(phase) + '-' + num
104outputFile = outdir + '/' + fname + '.root'
105
106# check for the existence of a SAD file
107
108if not os.path.exists(sadFile):
109 b2.B2ERROR('SAD file ' + sadFile + ' not found')
110 sys.exit()
111
112# make output directory if it doesn't exist
113
114if not os.path.exists(outdir):
115 os.makedirs(outdir)
116
117# log message
118b2.B2RESULT('SAD file (input): ' + sadFile)
119b2.B2RESULT('Output file: ' + outputFile)
120b2.B2RESULT('Corresponds to ' + equivTime + ' us of running phase ' + str(phase))
121
122# Suppress messages and warnings during processing:
123b2.set_log_level(b2.LogLevel.RESULT)
124
125# Create path
126main = b2.create_path()
127
128# Event info setter
129# Set some large number of events - processing will be stopped by BG generator
130eventinfosetter = b2.register_module('EventInfoSetter')
131eventinfosetter.param('evtNumList', [10000000])
132main.add_module(eventinfosetter)
133
134# Gearbox
135gearbox = b2.register_module('Gearbox')
136if phase == 2:
137 gearbox.param('fileName', 'geometry/Beast2_phase2.xml')
138elif phase == 31:
139 gearbox.param('fileName', 'geometry/Belle2_earlyPhase3.xml')
140elif phase == 32:
141 gearbox.param('fileName', 'geometry/Belle2_Run2.xml')
142if sampleType == 'study':
143 gearbox.param('override', [
144 ("/DetectorComponent[@name='PXD']//ActiveChips", 'true', ''),
145 ("/DetectorComponent[@name='PXD']//SeeNeutrons", 'true', ''),
146 ("/DetectorComponent[@name='SVD']//ActiveChips", 'true', ''),
147 ("/DetectorComponent[@name='SVD']//SeeNeutrons", 'true', ''),
148 ("/DetectorComponent[@name='TOP']//BeamBackgroundStudy", '1', ''),
149 ("/DetectorComponent[@name='ARICH']//BeamBackgroundStudy", '1', ''),
150 ("/DetectorComponent[@name='ECL']//BeamBackgroundStudy", '1', ''),
151 ("/DetectorComponent[@name='KLM']//BKLM/BeamBackgroundStudy", '1', ''),
152 ("/DetectorComponent[@name='KLM']//EKLM/BeamBackgroundStudy", '1', ''),
153 ])
154main.add_module(gearbox)
155
156# BG generator
157generator = b2.register_module('BeamBkgGenerator')
158generator.param('fileName', sadFile)
159generator.param('ringName', accRing)
160generator.param('realTime', realTime)
161main.add_module(generator)
162
163# Geant geometry
164geometry = b2.register_module('Geometry')
165geometry.param('useDB', False)
166# add beast detectors
167if sampleType == 'study' and (phase == 31 or phase == 32):
168 geometry.param('additionalComponents', ["BEAMABORT", "MICROTPC", "CLAWS", "HE3TUBE"])
169main.add_module(geometry)
170
171# Geant simulation
172fullsim = b2.register_module('FullSim')
173if sampleType == 'study':
174 fullsim.param('PhysicsList', 'FTFP_BERT_HP')
175 fullsim.param('UICommandsAtIdle', ['/process/inactivate nKiller'])
176 fullsim.param('StoreAllSecondaries', True)
177 fullsim.param('SecondariesEnergyCut', 0.000001) # [MeV] need for CDC EB neutron flux
178main.add_module(fullsim)
179
180# Show progress of processing
181progress = b2.register_module('Progress')
182main.add_module(progress)
183
184# Output
185if phase == 31 or phase == 32:
186 phase = 3
187add_output(main, bgType, realTime, sampleType, phase, fileName=outputFile)
188
189# Process events
190b2.process(main)
191
192# Print call statistics
193print(b2.statistics)