Belle II Software development
ParticleGunModule.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <generators/modules/ParticleGunModule.h>
10#include <framework/gearbox/Unit.h>
11#include <framework/datastore/StoreArray.h>
12#include <boost/algorithm/string.hpp>
13
14using namespace std;
15using namespace Belle2;
16
17//-----------------------------------------------------------------
18// Register the Module
19//-----------------------------------------------------------------
20REG_MODULE(ParticleGun);
21
22//-----------------------------------------------------------------
23// Implementation
24//-----------------------------------------------------------------
25
27{
28 //Set module properties
29 setDescription(R"DOC(
30Particle gun to generate simple tracks.
31This module allows to generate simple events where all tracks have the same
32momentum, angular and vertex distributions. Several distributions are
33available for momentum, phi, theta and vertex position generation:
34
35fixed:
36 Fixed value, only one parameter has to be specified
37 ``[value]``
38uniform:
39 Uniform between two given values
40 ``[min, max]``
41uniformPt:
42 Generate flat transverse momentum pt
43 ``[min_pt, max_pt]``
44uniformCos:
45 Generate uniformly in the cosine, e.g. flat in cos(theta). Parameters are
46 still the minimum and maximum angle (**not the cosine of the angle**)
47 ``[min_theta, max_theta]``
48uniformLog:
49 Generate uniformly in the logarithm. Parameters are still the normal
50 values
51 ``[min, max]``
52uniformLogPt:
53 Like uniformLog but for the transverse momentum.
54normal:
55 Normal (Gaussian) distributed
56 ``[mean, width]``
57normalPt:
58 Generate normal distributed transverse momentum pt
59 ``[mean_pt, width_pt]``
60normalCos:
61 Generate normal distributed cosine of the angle
62 ``[mean, width]``
63polyline:
64 Generate according to a pdf given as polyline, first the sorted x
65 coordinates and then the non-negative y coordinates
66 ``[x1, x2, x3, ... xn, y1, y2, y3, ..., yn]``
67polylinePt:
68 Like polyline but for pt, not p
69polylineCos:
70 Like polyline, but for the cos(), not the absolute value
71inversePt:
72 Generate uniformly in the inverse of pt, that is uniform in track
73 curvature
74 ``[min_pt, max_pt]``
75discrete:
76 Discrete Spectrum given as a list of weights and values (useful for
77 radioactive sources)
78 ``[weight1, value1, weight2, value2, ...]``
79discretePt:
80 same as above but for transverse momentum
81)DOC");
83
84 //Set default values for parameters
85 m_parameters.pdgCodes = { -11, 11};
86 m_parameters.momentumParams = {0.05, 3.0};
87 m_parameters.phiParams = {0.0, 360.0};
88 m_parameters.thetaParams = {17.0, 150.0};
89 m_parameters.xVertexParams = {0.0, 10 * Unit::um};
90 m_parameters.yVertexParams = {0.0, 59 * Unit::nm};
91 m_parameters.zVertexParams = {0.0, 190 * Unit::um};
92 m_parameters.timeParams = {0.0 * Unit::ns};
93
94 //Parameter definition
95 addParam("nTracks", m_parameters.nTracks,
96 "The number of tracks to be generated per event. If <=0, one particle will "
97 "be created for each entry in 'pdgCodes'. Otherwise N particles will be "
98 "created and the Particle ID for each particle will be picked randomly "
99 "from 'pdgCodes'", 1.0);
100 addParam("pdgCodes", m_parameters.pdgCodes,
101 "PDG codes for generated particles", m_parameters.pdgCodes);
102 addParam("varyNTracks", m_parameters.varyNumberOfTracks,
103 "If true, the number of tracks per event is varied using a Poisson "
104 "distribution. Only used if 'nTracks'>0", false);
105 addParam("momentumGeneration", m_momentumDist,
106 "Momentum distribution: one of fixed, uniform, normal, polyline, uniformLog, uniformPt, "
107 "normalPt, inversePt, polylinePt, uniformLogPt or discrete", string("uniform"));
108 addParam("phiGeneration", m_phiDist,
109 "Phi distribution: one of fixed, uniform, normal, normalCos, polyline, uniformCos, "
110 "polylineCos or discrete", string("uniform"));
111 addParam("thetaGeneration", m_thetaDist,
112 "Theta distribution: one of fixed, uniform, normal, normalCos, polyline, uniformCos, "
113 "polylineCos or discrete", string("uniform"));
114 addParam("timeGeneration", m_timeDist,
115 "Time distribution: one of fixed, uniform, normal, normalCos, polyline, uniformCos, "
116 "polylineCos or discrete", string("fixed"));
117 addParam("vertexGeneration", m_vertexDist,
118 "Vertex (x,y,z) distribution: one of fixed, uniform, normal, polyline or "
119 "discrete", string("fixed"));
120 addParam("xVertexGeneration", m_xVertexDist,
121 "X vertex distribution: same options as 'vertexGeneration'. If this parameter "
122 "is not specified the value from 'vertexGeneration' is used", string(""));
123 addParam("yVertexGeneration", m_yVertexDist,
124 "Y vertex distribution: same options as 'vertexGeneration'. If this parameter "
125 "is not specified the value from 'vertexGeneration' is used", string(""));
126 addParam("zVertexGeneration", m_zVertexDist,
127 "Z vertex distribution: same options as 'vertexGeneration'. If this parameter "
128 "is not specified the value from 'vertexGeneration' is used", string(""));
129 addParam("independentVertices", m_parameters.independentVertices,
130 "If false, all tracks of one event will start from the same vertex, "
131 "otherwise a new vertex is generated for every particle", false);
132 addParam("fixedMomentumPerEvent", m_parameters.fixedMomentumPerEvent,
133 "generates particle momentum according to the specified "
134 "distribution and assigns this momentum to all particles generated "
135 "for one event", false);
136 addParam("momentumParams", m_parameters.momentumParams,
137 "Parameters for the momentum generation. Meaning of the parameters "
138 "depends on the chosen distribution", m_parameters.momentumParams);
139 addParam("phiParams", m_parameters.phiParams,
140 "Parameters for the phi generation in degrees. Meaning of the parameters "
141 "depends on the chosen distribution", m_parameters.phiParams);
142 addParam("thetaParams", m_parameters.thetaParams,
143 "Parameters for the theta generation in degrees. Meaning of the parameters "
144 "depends on the chosen distribution", m_parameters.thetaParams);
145 addParam("xVertexParams", m_parameters.xVertexParams,
146 "Parameters for the x vertex generation. Meaning of the parameters "
147 "depends on the chosen distribution", m_parameters.xVertexParams);
148 addParam("yVertexParams", m_parameters.yVertexParams,
149 "Parameters for the y vertex generation. Meaning of the parameters "
150 "depends on the chosen distribution", m_parameters.yVertexParams);
151 addParam("zVertexParams", m_parameters.zVertexParams,
152 "Parameters for the z vertex generation. Meaning of the parameters "
153 "depends on the chosen distribution", m_parameters.zVertexParams);
154 addParam("timeParams", m_parameters.timeParams,
155 "Time offset", m_parameters.timeParams);
156}
157
159{
160 boost::to_lower(name);
161 boost::trim(name);
162 if (name == "fixed") return ParticleGun::c_fixedValue;
163 if (name == "uniform") return ParticleGun::c_uniformDistribution;
164 if (name == "uniformpt") return ParticleGun::c_uniformPtDistribution;
165 if (name == "uniformcos") return ParticleGun::c_uniformCosDistribution;
166 if (name == "uniformlog") return ParticleGun::c_uniformLogDistribution;
167 if (name == "uniformlogpt") return ParticleGun::c_uniformLogPtDistribution;
168 if (name == "normal") return ParticleGun::c_normalDistribution;
169 if (name == "normalpt") return ParticleGun::c_normalPtDistribution;
170 if (name == "normalcos") return ParticleGun::c_normalCosDistribution;
171 if (name == "discrete") return ParticleGun::c_discreteSpectrum;
172 if (name == "discretept") return ParticleGun::c_discretePtSpectrum;
173 if (name == "inversept") return ParticleGun::c_inversePtDistribution;
174 if (name == "polyline") return ParticleGun::c_polylineDistribution;
175 if (name == "polylinept") return ParticleGun::c_polylinePtDistribution;
176 if (name == "polylinecos") return ParticleGun::c_polylineCosDistribution;
177 B2ERROR("Unknown distribution '" << name << "', using fixed");
179}
180
182{
183 //Initialize MCParticle collection
184 StoreArray<MCParticle> mcparticle;
185 mcparticle.registerInDataStore();
186
187 //Convert string representations to distribution values
195 if (getParam<std::string>("xVertexGeneration").isSetInSteering()) {
197 }
198 if (getParam<std::string>("yVertexGeneration").isSetInSteering()) {
200 }
201 if (getParam<std::string>("zVertexGeneration").isSetInSteering()) {
203 }
204
205 //Convert degree to radian
208 for (double& angle : m_parameters.thetaParams) angle *= Unit::deg;
209 }
212 for (double& angle : m_parameters.phiParams) angle *= Unit::deg;
213 }
214
215 //Assign parameters
216 m_particleGun.setParameters(m_parameters);
217}
218
220{
221 try {
222 m_particleGraph.clear();
223 m_particleGun.generateEvent(m_particleGraph);
224 m_particleGraph.generateList();
225 } catch (runtime_error& e) {
226 B2ERROR(e.what());
227 }
228}
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition Module.cc:208
Module()
Constructor.
Definition Module.cc:30
@ c_Input
This module is an input module (reads data).
Definition Module.h:78
MCParticleGraph m_particleGraph
Particle graph to generate MCParticle list.
std::string m_xVertexDist
String representation of the x vertex distribution.
std::string m_zVertexDist
String representation of the z vertex distribution.
void initialize() override
Initializes the module.
void event() override
Method is called for each event.
std::string m_timeDist
String representation of the time distribution.
ParticleGun::Parameters m_parameters
Parameters of the particle gun.
std::string m_momentumDist
String representation of the momentum distribution.
std::string m_vertexDist
String representation of the vertex distribution.
std::string m_thetaDist
String representation of the polar angle distribution.
ParticleGun::EDistribution convertDistribution(std::string name)
function to convert the strings the uses sets in the parameter list to the internal encoding
std::string m_phiDist
String representation of the azimuth angle distribution.
ParticleGun m_particleGun
Instance of the particle gun.
std::string m_yVertexDist
String representation of the y vertex distribution.
EDistribution
enum containing all known distributions available for generation of values
Definition ParticleGun.h:29
@ c_polylineDistribution
Distribution given as list of (x,y) points.
Definition ParticleGun.h:57
@ c_uniformLogPtDistribution
Uniform distribution of the logarithm of the Pt, parameters are min and max value.
Definition ParticleGun.h:41
@ c_normalDistribution
Normal distribution, parameters are mean and sigma.
Definition ParticleGun.h:43
@ c_normalCosDistribution
Normal distribution of the cosinge, parameters are mean and sigma.
Definition ParticleGun.h:47
@ c_uniformPtDistribution
Uniform distribution of Pt, parameters are min and max value.
Definition ParticleGun.h:35
@ c_polylinePtDistribution
Same as polylineDistribution but for the transverse momentum.
Definition ParticleGun.h:59
@ c_discretePtSpectrum
Discrete pt spectrum, parameters are first the values and then the weights (non-negative) for each va...
Definition ParticleGun.h:51
@ c_uniformDistribution
Uniform distribution, parameters are min and max value.
Definition ParticleGun.h:33
@ c_discreteSpectrum
Discrete spectrum, parameters are first the values and then the weights (non-negative) for each value...
Definition ParticleGun.h:49
@ c_fixedValue
Fixed value, no random generation at all, 1 parameter.
Definition ParticleGun.h:31
@ c_normalPtDistribution
Normal distribution of Pt, parameters are mean and sigma.
Definition ParticleGun.h:45
@ c_uniformCosDistribution
Uniform distribution of the cosine of the values, parameters are min and max value.
Definition ParticleGun.h:37
@ c_inversePtDistribution
Distribution uniform in the inverse pt distribution, that is uniform in track curvature.
Definition ParticleGun.h:53
@ c_polylineCosDistribution
Same as polylineDistribution but for the cosine of the angle.
Definition ParticleGun.h:61
@ c_uniformLogDistribution
Uniform distribution of the logarithm of the values, parameters are min and max value.
Definition ParticleGun.h:39
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
Accessor to arrays stored in the data store.
Definition StoreArray.h:113
static const double deg
degree to radians
Definition Unit.h:109
static const double nm
[nanometers]
Definition Unit.h:72
static const double um
[micrometers]
Definition Unit.h:71
static const double ns
Standard of [time].
Definition Unit.h:48
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition Module.h:559
ModuleParam< T > & getParam(const std::string &name) const
Returns a reference to a parameter.
Definition Module.h:572
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
Abstract base class for different kinds of events.
STL namespace.