Belle II Software development
generateSADBg.py
1#!/usr/bin/env python3
2
3
10
11# ------------------------------------------------------------------------------------
12# BG simulation using SAD input files
13#
14# uses phase2 SAD files from /group/belle2/BGcampaigns/SAD/forG4/
15#
16# usage:
17# basf2 generateSADBg.py bgType accRing equivTime_us num [sampleType phase outdir]
18# arguments:
19# bgType Coulomb, Touschek or Brems
20# accRing LER or HER
21# equivTime_us equivalent SuperKEKB running time in micro-seconds
22# num output file number
23# sampleType one of: study, usual, PXD, ECL
24# phase 2, 31 (= early phase 3) or 3
25# sad SAD file name from /home/belle/luka/public/SAD without bg type and ".root"
26# outdir output directory path
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, Touschek, Brems
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 # 2 or 3
46 sad = 'phase2.1.4_collimators_1'
47 outdir = 'output' # output directory path
48elif argc == 6:
49 bgType = argvs[1]
50 accRing = argvs[2]
51 equivTime = argvs[3]
52 num = argvs[4]
53 sampleType = argvs[5]
54 phase = 3
55 sad = 'phase2.1.4_collimators_1'
56 outdir = 'output'
57elif argc == 7:
58 bgType = argvs[1]
59 accRing = argvs[2]
60 equivTime = argvs[3]
61 num = argvs[4]
62 sampleType = argvs[5]
63 phase = int(argvs[6])
64 sad = 'phase2.1.4_collimators_1'
65 outdir = 'output'
66elif argc == 8:
67 bgType = argvs[1]
68 accRing = argvs[2]
69 equivTime = argvs[3]
70 num = argvs[4]
71 sampleType = argvs[5]
72 phase = int(argvs[6])
73 sad = argvs[7]
74 outdir = 'output'
75elif argc == 9:
76 bgType = argvs[1]
77 accRing = argvs[2]
78 equivTime = argvs[3]
79 num = argvs[4]
80 sampleType = argvs[5]
81 phase = int(argvs[6])
82 sad = argvs[7]
83 outdir = argvs[8]
84else:
85 print('usage:')
86 print('basf2', argvs[0],
87 '(Touschek,Coulomb,Brems) (HER,LER) equivTime_us num [(study,usual,ECL,PXD) phase outdir]')
88 sys.exit()
89
90
91inputSAD = "/group/belle2/BGcampaigns/SAD/forG4/"
92bgType = bgType + '_' + accRing
93sadFile = inputSAD + bgType + '_' + sad + '.root'
94realTime = float(equivTime) * 1000
95fname = bgType + '_' + sampleType + '-phase' + str(phase) + '-' + num
96outputFile = outdir + '/' + fname + '.root'
97
98# check for the existance of a SAD file
99
100if not os.path.exists(sadFile):
101 b2.B2ERROR('SAD file ' + sadFile + ' not found')
102 sys.exit()
103
104# make output directory if it doesn't exist
105
106if not os.path.exists(outdir):
107 os.makedirs(outdir)
108
109# log message
110b2.B2RESULT('Output file: ' + outputFile)
111b2.B2RESULT('Corresponds to ' + equivTime + ' us of running phase ' + str(phase))
112
113# Suppress messages and warnings during processing:
114b2.set_log_level(b2.LogLevel.RESULT)
115
116# Create path
117main = b2.create_path()
118
119# Event info setter
120# Set some large number of events - processing will be stopped by BG generator
121eventinfosetter = b2.register_module('EventInfoSetter')
122eventinfosetter.param('evtNumList', [10000000])
123main.add_module(eventinfosetter)
124
125# Gearbox
126gearbox = b2.register_module('Gearbox')
127if phase == 2:
128 gearbox.param('fileName', 'geometry/Beast2_phase2.xml')
129elif phase == 31:
130 gearbox.param('fileName', 'geometry/Belle2_earlyPhase3.xml')
131if sampleType == 'study':
132 gearbox.param('override', [
133 ("/DetectorComponent[@name='PXD']//ActiveChips", 'true', ''),
134 ("/DetectorComponent[@name='PXD']//SeeNeutrons", 'true', ''),
135 ("/DetectorComponent[@name='SVD']//ActiveChips", 'true', ''),
136 ("/DetectorComponent[@name='SVD']//SeeNeutrons", 'true', ''),
137 ("/DetectorComponent[@name='TOP']//BeamBackgroundStudy", '1', ''),
138 ("/DetectorComponent[@name='ARICH']//BeamBackgroundStudy", '1', ''),
139 ("/DetectorComponent[@name='ECL']//BeamBackgroundStudy", '1', ''),
140 ("/DetectorComponent[@name='KLM']//BeamBackgroundStudy", '1', ''),
141 ])
142main.add_module(gearbox)
143
144# BG generator
145generator = b2.register_module('BeamBkgGenerator')
146generator.param('fileName', sadFile)
147generator.param('ringName', accRing)
148generator.param('realTime', realTime)
149main.add_module(generator)
150
151# Geant geometry
152geometry = b2.register_module('Geometry')
153geometry.param('useDB', False)
154if phase == 31:
155 geometry.param('additionalComponents', ['BEAMABORT', 'MICROTPC', 'CLAWS', 'HE3TUBE'])
156main.add_module(geometry)
157
158# Geant simulation
159fullsim = b2.register_module('FullSim')
160if sampleType == 'study':
161 fullsim.param('PhysicsList', 'FTFP_BERT_HP')
162 fullsim.param('UICommandsAtIdle', ['/process/inactivate nKiller'])
163 fullsim.param('StoreAllSecondaries', True)
164 fullsim.param('SecondariesEnergyCut', 0.000001) # [MeV] need for CDC EB neutron flux
165main.add_module(fullsim)
166
167# Show progress of processing
168progress = b2.register_module('Progress')
169main.add_module(progress)
170
171# Output
172if phase == 31:
173 phase = 3
174
175# to not store MCParticles use this, if you want MCParticles comment these two lines and uncomment the line after
176excludeBranch = ['MCParticles']
177add_output(main, bgType, realTime, sampleType, phase, fileName=outputFile, excludeBranches=excludeBranch)
178
179
180# Process events
181b2.process(main)
182
183# Print call statistics
184print(b2.statistics)