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 # 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 
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, 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
48 elif 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'
57 elif 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'
66 elif 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'
75 elif 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]
84 else:
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 
91 inputSAD = "/group/belle2/BGcampaigns/SAD/forG4/"
92 bgType = bgType + '_' + accRing
93 sadFile = inputSAD + bgType + '_' + sad + '.root'
94 realTime = float(equivTime) * 1000
95 fname = bgType + '_' + sampleType + '-phase' + str(phase) + '-' + num
96 outputFile = outdir + '/' + fname + '.root'
97 
98 # check for the existance of a SAD file
99 
100 if 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 
106 if not os.path.exists(outdir):
107  os.makedirs(outdir)
108 
109 # log message
110 b2.B2RESULT('Output file: ' + outputFile)
111 b2.B2RESULT('Corresponds to ' + equivTime + ' us of running phase ' + str(phase))
112 
113 # Suppress messages and warnings during processing:
114 b2.set_log_level(b2.LogLevel.RESULT)
115 
116 # Create path
117 main = b2.create_path()
118 
119 # Event info setter
120 # Set some large number of events - processing will be stopped by BG generator
121 eventinfosetter = b2.register_module('EventInfoSetter')
122 eventinfosetter.param('evtNumList', [10000000])
123 main.add_module(eventinfosetter)
124 
125 # Gearbox
126 gearbox = b2.register_module('Gearbox')
127 if phase == 2:
128  gearbox.param('fileName', 'geometry/Beast2_phase2.xml')
129 elif phase == 31:
130  gearbox.param('fileName', 'geometry/Belle2_earlyPhase3.xml')
131 if 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  ])
142 main.add_module(gearbox)
143 
144 # BG generator
145 generator = b2.register_module('BeamBkgGenerator')
146 generator.param('fileName', sadFile)
147 generator.param('ringName', accRing)
148 generator.param('realTime', realTime)
149 main.add_module(generator)
150 
151 # Geant geometry
152 geometry = b2.register_module('Geometry')
153 geometry.param('useDB', False)
154 if phase == 31:
155  geometry.param('additionalComponents', ['BEAMABORT', 'MICROTPC', 'CLAWS', 'HE3TUBE'])
156 main.add_module(geometry)
157 
158 # Geant simulation
159 fullsim = b2.register_module('FullSim')
160 if 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
165 main.add_module(fullsim)
166 
167 # Show progress of processing
168 progress = b2.register_module('Progress')
169 main.add_module(progress)
170 
171 # Output
172 if 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
176 excludeBranch = ['MCParticles']
177 add_output(main, bgType, realTime, sampleType, phase, fileName=outputFile, excludeBranches=excludeBranch)
178 
179 
180 # Process events
181 b2.process(main)
182 
183 # Print call statistics
184 print(b2.statistics)