Belle II Software development
eventSimulationBhabha.py
1#!/usr/bin/env python3
2
3
10
11
33
34
35import basf2 as b2
36from beamparameters import add_beamparameters
37from simulation import add_simulation
38
39import os
40import sys
41import random
42
43from pxd import add_pxd_reconstruction
44from svd import add_svd_reconstruction
45from generators import add_babayaganlo_generator
46
47# ---------------------------------------------------------------------------------------
48
49# Set Random Seed for reproducable simulation. 0 means really random.
50rndseed = 12345
51# assume the first argument is the random seed
52if(len(sys.argv) > 1):
53 rndseed = sys.argv[1]
54
55outputDir = './'
56# assume second argument is the output directory
57if(len(sys.argv) > 2):
58 outputDir = sys.argv[2]
59
60b2.set_random_seed(rndseed)
61
62
63# Set log level. Can be overridden with the "-l LEVEL" flag for basf2.
64b2.set_log_level(b2.LogLevel.ERROR)
65
66# ---------------------------------------------------------------------------------------
67main = b2.create_path()
68
69eventinfosetter = b2.register_module('EventInfoSetter')
70# default phase3 geometry:
71exp_number = 0
72# if environment variable is set then phase2 (aka Beast2) geometry will be taken
73if os.environ.get('USE_BEAST2_GEOMETRY'):
74 exp_number = 1002
75eventinfosetter.param("expList", [exp_number])
76main.add_module(eventinfosetter)
77
78eventinfoprinter = b2.register_module('EventInfoPrinter')
79main.add_module(eventinfoprinter)
80
81progress = b2.register_module('Progress')
82main.add_module(progress)
83
84# ---------------------------------------------------------------------------------------
85# Simulation Settings:
86
87
88# randomize the vertex position (flatly distributed) to make the sectormap more robust wrt. changing beam position
89# minima and maxima of the beam position given in cm
90random.seed(rndseed)
91vertex_x_min = -0.1
92vertex_x_max = 0.1
93vertex_y_min = -0.1
94vertex_y_max = 0.1
95vertex_z_min = -0.5
96vertex_z_max = 0.5
97
98vertex_x = random.uniform(vertex_x_min, vertex_x_max)
99vertex_y = random.uniform(vertex_y_min, vertex_y_max)
100vertex_z = random.uniform(vertex_z_min, vertex_z_max)
101
102print("WARNING: setting non-default beam vertex at x= " + str(vertex_x) + " y= " + str(vertex_y) + " z= " + str(vertex_z))
103
104# Particle Gun:
105# One can add more particle gun modules if wanted.
106
107# additional flatly smear the muon vertex between +/- this value
108vertex_delta = 0.005 # in cm
109
110particlegun = b2.register_module('ParticleGun')
111particlegun.logging.log_level = b2.LogLevel.WARNING
112param_pGun = {
113 'pdgCodes': [13, -13], # 13 = muon --> negatively charged!
114 'nTracks': 8,
115 'momentumGeneration': 'uniform',
116 'momentumParams': [0.1, 7],
117 'vertexGeneration': 'uniform',
118 'xVertexParams': [vertex_x - vertex_delta, vertex_x + vertex_delta], # in cm...
119 'yVertexParams': [vertex_y - vertex_delta, vertex_y + vertex_delta],
120 'zVertexParams': [vertex_z - vertex_delta, vertex_z + vertex_delta]
121}
122
123particlegun.param(param_pGun)
124main.add_module(particlegun)
125
126# EvtGen Simulation:
127# TODO: There are newer convenience functions for this task -> Include them!
128# Beam parameters
129beamparameters = add_beamparameters(main, "Y4S")
130beamparameters.param("vertex", [vertex_x, vertex_y, vertex_z])
131# print_params(beamparameters)
132
133# evtgenInput = register_module('EvtGenInput')
134# evtgenInput.logging.log_level = LogLevel.WARNING
135# main.add_module(evtgenInput)
136
137# generate some Bhabha
138add_babayaganlo_generator(path=main, finalstate='ee')
139
140
141# ---------------------------------------------------------------------------------------
142
143
144# Detector Simulation:
145add_simulation(path=main,
146 usePXDDataReduction=False, # for training one does not want the data reduction
147 components=None) # dont specify components because else not the whole geometry will be loaded!
148
149# needed for fitting
150main.add_module('SetupGenfitExtrapolation')
151
152# this adds the clusters for PXD and SVD (was done by the usePXDDataReduction previously) which are needed in the next steps
153add_pxd_reconstruction(path=main)
154add_svd_reconstruction(path=main)
155
156# ---------------------------------------------------------------------------------------
157# Setting up the MC based track finder.
158mctrackfinder = b2.register_module('TrackFinderMCTruthRecoTracks')
159mctrackfinder.param('UseCDCHits', False)
160mctrackfinder.param('UseSVDHits', True)
161# Always use PXD hits! For SVD only, these will be filtered later when converting to SPTrackCand
162# 15.02.2018: deactivated PXD again as we dont do pxd track finding anymore and to not bias the fit
163mctrackfinder.param('UsePXDHits', False)
164mctrackfinder.param('Smearing', False)
165mctrackfinder.param('MinimalNDF', 6)
166mctrackfinder.param('WhichParticles', ['primary'])
167mctrackfinder.param('RecoTracksStoreArrayName', 'MCRecoTracks')
168# set up the track finder to only use the first half loop of the track and discard all other hits
169mctrackfinder.param('UseNLoops', 0.5)
170mctrackfinder.param('discardAuxiliaryHits', True)
171main.add_module(mctrackfinder)
172
173# include a track fit into the chain (sequence adopted from the tracking scripts)
174# Correct time seed: Do I need it for VXD only tracks ????
175main.add_module("IPTrackTimeEstimator", recoTracksStoreArrayName="MCRecoTracks", useFittedInformation=False)
176# track fitting
177daffitter = b2.register_module("DAFRecoFitter")
178daffitter.param('recoTracksStoreArrayName', "MCRecoTracks")
179# daffitter.logging.log_level = LogLevel.DEBUG
180main.add_module(daffitter)
181# also used in the tracking sequence (multi hypothesis)
182# may be overkill
183main.add_module('TrackCreator', recoTrackColName="MCRecoTracks", pdgCodes=[211, 321, 2212])
184
185
186# build the name of the output file
187outputFileName = outputDir + './'
188if os.environ.get('USE_BEAST2_GEOMETRY'):
189 outputFileName += "SimEvts_Beast2"
190else:
191 outputFileName += "SimEvts_Belle2"
192outputFileName += '_' + str(rndseed) + '.root'
193
194# Root output. Default filename can be overriden with '-o' basf2 option.
195rootOutput = b2.register_module('RootOutput')
196rootOutput.param('outputFileName', outputFileName)
197# to save some space exclude everything except stuff needed for tracking
198rootOutput.param('excludeBranchNames', ["ARICHAeroHits",
199 "ARICHDigits",
200 "ARICHSimHits",
201 "BKLMDigits",
202 "BKLMSimHitPositions",
203 "BKLMSimHits",
204 "CDCHits",
205 "CDCHits4Trg",
206 "CDCSimHits",
207 "CDCSimHitsToCDCHits4Trg",
208 "ECLDigits",
209 "ECLDsps",
210 "ECLHits",
211 "ECLSimHits",
212 "ECLTrigs",
213 "ECLDiodeHits",
214 "EKLMDigits",
215 "EKLMSimHits",
216 "TOPBarHits",
217 "TOPDigits",
218 "TOPRawDigits",
219 "TOPSimHits"
220 ])
221main.add_module(rootOutput)
222
223b2.log_to_file('createSim.log', append=False)
224
225b2.print_path(main)
226
227b2.process(main)
228print(b2.statistics)