Belle II Software development
ParticleGunFull.py
1#!/usr/bin/env python3
2
3
10
11from basf2 import set_log_level, register_module, process, LogLevel, \
12 set_random_seed, print_params, create_path, statistics
13
14# suppress messages and warnings during processing:
15set_log_level(LogLevel.WARNING)
16
17# to run the framework the used modules need to be registered
18particlegun = register_module('ParticleGun')
19
20# ============================================================================
21# Setting the random seed for particle generation
22set_random_seed(123)
23
24# ============================================================================
25# Setting the list of particle codes (PDG codes) for the generated particles
26# the codes are given in an array, if only one code is used, the brackets
27# should be kept:
28# particlegun.param('pdgCodes', [11])
29
30# there is no limit on how many codes can be given the particle gun will select
31# randomly amongst the PDGcodes using a uniform distribution if nTracks>0,
32# otherwise there will be one particle for each code in the list default is
33# [-11, 11]
34particlegun.param('pdgCodes', [-11, 11])
35
36# ============================================================================
37# Setting the number of tracks to be generated per event: this number can be
38# any int>=0 default is 1
39particlegun.param('nTracks', 10)
40
41# a value o 0 means that a track should be created for each entry in the
42# pdgCodes list, e.g. the following two lines would create two electrons and
43# one pion per event.
44# particlegun.param('pdgCodes', [11,11,211])
45# particlegun.param('nTracks', 0)
46
47# ============================================================================
48# Varying the number of tracks per event can be achieved by setting varyNTacks
49# to True. If so, the number of tracks will be randomized using possion
50# distribution around the value of nTracks. Only valid if nTracks>0, default
51# is False
52particlegun.param('varyNTracks', False)
53
54# ============================================================================
55# Each particle has a total momentum, a direction given as theta and phi and a
56# vertex position in x, y and z. For each variable we need to specify the
57# distribution and the parameters for the distribution. The number of
58# parameters depends on the chosen distribution. All available distributions
59# are listed here with required parameters are given in []. The distributions
60# with suffix "Pt" can only be used for momentum generation and the ones with
61# suffix "Cos" can only used for the generation of theta and phi.
62#
63# - fixed: always use the exact same value [value]
64# - uniform: uniform distribution between min and max [min, max]
65# - uniformPt: uniform distribution of transverse momentum between a given
66# minimal and maximum value [min, max]
67# - uniformCos: uniform distribution of the cosine of the angle between min
68# and max of the absolute value [min, max]
69# - normal: normal (gaussian) distribution around mean with width of sigma
70# [mean, sigma]
71# - normalPt: normal distribution of transverse momentum [mean, sigma]
72# - normalCos: normal distribution of the cosine of the angle, not the
73# absolute value [mean, sigma]
74# - inversePt: generate momentum to obtain a flat distribution of the track
75# curvature (inverse transverse momentum) between a minimal and
76# maximal transverse momentum, [min_pt, max_pt]
77# - polyline: create the momentum to follow an arbitrary distribution given
78# as a list of x and y coordinates. All y coordinates must be
79# non-negative and at leas one y coordinate must be positive
80# [x1, x2, ..., xn, y1, y2, ..., yn]
81# - polylinePt: same as polyline but for the transverse momentum, not the
82# total momentum.
83# [x1, x2, ..., xn, y1, y2, ..., yn]
84# - polylineCos: same as polyline but for the cosine of the angle, not the
85# absolute value
86# [x1, x2, ..., xn, y1, y2, ..., yn]
87# - discrete: a discrete spectrum consisting of possible values xi and their
88# weights wi (useful e.g. for radioactive sources)
89# [x1, x2, ..., xn, w1, w2, ..., wn]
90
91# ============================================================================
92# Momentum generation
93#
94# The default is a uniform momentum distribution between 0.05 and 3 GeV
95particlegun.param('momentumGeneration', 'uniform')
96particlegun.param('momentumParams', [0.05, 3])
97
98# we could also generate a fixed momentum of 1 GeV
99# particlegun.param('momentumGeneration', "fixed")
100# particlegun.param('momentumParams', [1.0])
101
102# or we could generate a normal distributed transverse momentum around 2 GeV
103# with a width of 0.5 GeV
104# particlegun.param('momentumGeneration', "normalPt")
105# particlegun.param('momentumParams', [2.0, 0.5])
106
107# to generate the momentum according to a cadmium 109
108# source we could use
109# particlegun.param('momentumGeneration', 'discreteSpectrum'),
110# particlegun.param('momentumParams', [
111# 22.1e-6, 25.0e-6, 88.0e-6, # photon energies
112# 82.6, 14.7, 3.65, # weights
113# ])
114
115# ============================================================================
116# polar angle, theta
117# The default is a uniform theta distribution between 17 and 150 degree
118
119particlegun.param('thetaGeneration', 'uniform')
120particlegun.param('thetaParams', [17, 150])
121
122# We could also generate between 17 and 150 degrees with a flat distribution in
123# cos(theta)
124# particlegun.param('thetaGeneration', 'uniformCos')
125# particlegun.param('thetaParams', [17, 150])
126
127# or we could create a theta angle between 17 and 150 degree where the
128# cos(theta) distribution is flat
129# particlegun.param('thetaGeneration', "normal")
130# particlegun.param('thetaParams', [90,5])
131
132# Finally, we could use numpy to create events where the cos(theta)
133# distribution follows a parabolic shape:
134# import numpy as np
135# # Create a set of 1000 x values spread uniformly over -1, 1
136# x = np.linspace(-1,1,1000)
137# # Create the corresponding distribution
138# y = 1-x**2
139# # Set the parameters by concatenating the x and y positions
140# particlegun.param('thetaGeneration', "polylineCos")
141# particlegun.param('thetaParams', list(x) + list(y))
142
143# ============================================================================
144# azimuth angle, phi
145# The default is a uniform theta distribution between 0 and 360 degree
146
147particlegun.param('phiGeneration', 'uniform')
148particlegun.param('phiParams', [0, 360])
149
150# or we could create a normal distributed phi angle around 90 degrees with a
151# width of 5 degrees
152# particlegun.param('phiGeneration', "normal")
153# particlegun.param('phiParams', [90,5])
154
155# ============================================================================
156# Vertex generation
157# The default is a fixed vertex at (0,0,0) for all tracks
158particlegun.param('vertexGeneration', 'fixed')
159particlegun.param('xVertexParams', [0])
160particlegun.param('yVertexParams', [0])
161particlegun.param('zVertexParams', [0])
162
163# We could also generate a normal distributed vertex with mean at (0,0,0) and
164# width of 10µm, 60nm and 190 µm in x, y and z respectively
165# particlegun.param('vertexGeneration', 'fixed')
166# particlegun.param('xVertexParams', [0, 10e-4])
167# particlegun.param('yVertexParams', [0, 60e-7])
168# particlegun.param('zVertexParams', [0, 190e-4])
169
170# We can also specify a different distribution for any of the three axes, e.g.
171# to make the z-vertex uniform between -10 and 10 cm and the xy vertex normal
172# distributed around zero with a width of 0.1 cm we could use
173# particlegun.param('vertexGeneration', 'normal')
174# particlegun.param('xVertexParams', [0, 0.1])
175# particlegun.param('yVertexParams', [0, 0.1])
176# particlegun.param('zVertexGeneration', 'uniform')
177# particlegun.param('zVertexParams', [-10, 10])
178
179# ============================================================================
180# Setting independent vertices for each particle The default is to create one
181# event vertex for all particles per event. By setting independentVertices to
182# True, a new vertex will be created for each particle default is False
183particlegun.param('independentVertices', False)
184
185# ============================================================================
186# Print the parameters of the particle gun
187print_params(particlegun)
188
189# ============================================================================
190# Now lets create the necessary modules to perform a simulation
191#
192# Create Event information
193eventinfosetter = register_module('EventInfoSetter')
194# Show progress of processing
195progress = register_module('Progress')
196# Load parameters
197gearbox = register_module('Gearbox')
198# Create geometry
199geometry = register_module('Geometry')
200# Save output of simulation
201output = register_module('RootOutput')
202
203# Setting the option for all non particle gun modules: want to process 100 MC
204# events
205eventinfosetter.param({'evtNumList': [100], 'runList': [1]})
206
207# Set output filename
208output.param('outputFileName', 'ParticleGunOutput.root')
209
210# ============================================================================
211main = create_path()
212main.add_module(eventinfosetter)
213main.add_module(progress)
214main.add_module(particlegun)
215
216main.add_module(output)
217
218# Process events
219process(main)
220
221# Print call statistics
222print(statistics)