Belle II Software  release-08-01-10
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 
29 import basf2 as b2
30 import sys
31 import os
32 from background import add_output
33 
34 # read parameters
35 
36 argvs = sys.argv
37 argc = len(argvs)
38 
39 if 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
47 elif 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'
55 elif 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'
63 elif 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]
71 else:
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 
86 if phase == 3:
87  subdir = 'phase3/'
88 elif phase == 31:
89  subdir = 'phase3-early/'
90 elif phase == 32:
91  subdir = 'phase3-Run2/'
92 elif phase == 2:
93  subdir = 'phase2/'
94 else:
95  print('phase ', phase, 'not supported')
96  sys.exit()
97 
98 # set parameters
99 
100 bgType = bgType + '_' + accRing
101 sadFile = 'input/' + subdir + bgType + '.root'
102 realTime = float(equivTime) * 1000
103 fname = bgType + '_' + sampleType + '-phase' + str(phase) + '-' + num
104 outputFile = outdir + '/' + fname + '.root'
105 
106 # check for the existence of a SAD file
107 
108 if 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 
114 if not os.path.exists(outdir):
115  os.makedirs(outdir)
116 
117 # log message
118 b2.B2RESULT('SAD file (input): ' + sadFile)
119 b2.B2RESULT('Output file: ' + outputFile)
120 b2.B2RESULT('Corresponds to ' + equivTime + ' us of running phase ' + str(phase))
121 
122 # Suppress messages and warnings during processing:
123 b2.set_log_level(b2.LogLevel.RESULT)
124 
125 # Create path
126 main = b2.create_path()
127 
128 # Event info setter
129 # Set some large number of events - processing will be stopped by BG generator
130 eventinfosetter = b2.register_module('EventInfoSetter')
131 eventinfosetter.param('evtNumList', [10000000])
132 main.add_module(eventinfosetter)
133 
134 # Gearbox
135 gearbox = b2.register_module('Gearbox')
136 if phase == 2:
137  gearbox.param('fileName', 'geometry/Beast2_phase2.xml')
138 elif phase == 31:
139  gearbox.param('fileName', 'geometry/Belle2_earlyPhase3.xml')
140 elif phase == 32:
141  gearbox.param('fileName', 'geometry/Belle2_Run2.xml')
142 if 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  ])
154 main.add_module(gearbox)
155 
156 # BG generator
157 generator = b2.register_module('BeamBkgGenerator')
158 generator.param('fileName', sadFile)
159 generator.param('ringName', accRing)
160 generator.param('realTime', realTime)
161 main.add_module(generator)
162 
163 # Geant geometry
164 geometry = b2.register_module('Geometry')
165 geometry.param('useDB', False)
166 # add beast detectors
167 if sampleType == 'study' and (phase == 31 or phase == 32):
168  geometry.param('additionalComponents', ["BEAMABORT", "MICROTPC", "CLAWS", "HE3TUBE"])
169 main.add_module(geometry)
170 
171 # Geant simulation
172 fullsim = b2.register_module('FullSim')
173 if 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
178 main.add_module(fullsim)
179 
180 # Show progress of processing
181 progress = b2.register_module('Progress')
182 main.add_module(progress)
183 
184 # Output
185 if phase == 31 or phase == 32:
186  phase = 3
187 add_output(main, bgType, realTime, sampleType, phase, fileName=outputFile)
188 
189 # Process events
190 b2.process(main)
191 
192 # Print call statistics
193 print(b2.statistics)