Belle II Software development
particlegun.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#include <generators/particlegun/ParticleGun.h>
9#include <gtest/gtest.h>
10#include <iostream>
11using namespace std;
12
13namespace Belle2 {
19 class ParticleGunTest : public ::testing::Test {
20 protected:
22 virtual void SetUp()
23 {
28 parameters.thetaParams = {1, 1};
29 parameters.phiParams = {1, 1};
30 }
35
37 void checkVariable(const std::string& name, const std::map<int, bool>& allowed, ParticleGun::EDistribution& dist,
38 std::vector<double>& pars);
39 };
40
41 void ParticleGunTest::checkVariable(const std::string& name, const std::map<int, bool>& allowed, ParticleGun::EDistribution& dist,
42 std::vector<double>& pars)
43 {
45 parameters.pdgCodes = {11};
46 for (auto item : allowed) {
47 dist = (ParticleGun::EDistribution) item.first;
48 //We need at least for parameters for some distributions.
49 pars = {1, 1, 1, 1};
50 //Check if distribution is accepted
51 ASSERT_EQ(item.second, pgun.setParameters(parameters)) << name << ": " << item.first << ", " << item.second;
52 if (item.second) {
53 //And if it is accepted, check number of parameters
54 for (int i = 0; i < 10; ++i) {
55 pars.resize(i, 1);
56 int minpar = (item.first == ParticleGun::c_fixedValue) ? 1 : 2;
57 //Polyline needs at least two points, so four parameters
59 || dist == ParticleGun::c_polylineCosDistribution) minpar = 4;
60 bool valid = i >= minpar;
61 //discrete and polyline need even number of parameters
64 valid &= i % 2 == 0;
65 }
66 //Check if numberof parameters is accepted
67 ASSERT_EQ(valid, pgun.setParameters(parameters)) << name << " " << pars.size();
68 }
69 }
70 //Check that zeros are forbidden for inversePt spectrum
71 if (item.first == ParticleGun::c_inversePtDistribution && item.second) {
72 pars = {1, 1};
73 ASSERT_TRUE(pgun.setParameters(parameters));
74 pars = {1, 0};
75 ASSERT_FALSE(pgun.setParameters(parameters)) << name << ": " << parameters.momentumParams[0] << ", " <<
77 pars = {0, 1};
78 ASSERT_FALSE(pgun.setParameters(parameters)) << name << ": " << parameters.momentumParams[0] << ", " <<
80 }
81 //Check polyline stuff
83 || dist == ParticleGun::c_polylineCosDistribution) && item.second) {
84 pars = {0, 1, 0, 1};
85 ASSERT_TRUE(pgun.setParameters(parameters));
86 //x needs to be ascending
87 pars = {1, 0, 0, 1};
88 ASSERT_FALSE(pgun.setParameters(parameters));
89 //at least one positive y value
90 pars = {0, 1, 0, 0};
91 ASSERT_FALSE(pgun.setParameters(parameters));
92 //no negative y values
93 pars = {0, 1, 1, -1};
94 ASSERT_FALSE(pgun.setParameters(parameters));
95 }
96 //Check discrete spectrum has non-negative weights
98 pars = {0, 1, 0, 1};
99 ASSERT_TRUE(pgun.setParameters(parameters));
100 //at least one weight must be positive
101 pars = {0, 1, 0, 0};
102 ASSERT_FALSE(pgun.setParameters(parameters));
103 //no negative weights
104 pars = {0, 1, 1, -1};
105 ASSERT_FALSE(pgun.setParameters(parameters));
106 }
107 }
108 std::cout << std::endl;
109 }
110
112 TEST_F(ParticleGunTest, MomentumDistributions)
113 {
114 std::map<int, bool> distributions = {
115 {ParticleGun::c_fixedValue, true},
116 {ParticleGun::c_uniformDistribution, true},
117 {ParticleGun::c_uniformPtDistribution, true},
118 {ParticleGun::c_uniformCosDistribution, false},
119 {ParticleGun::c_normalDistribution, true},
120 {ParticleGun::c_normalPtDistribution, true},
121 {ParticleGun::c_normalCosDistribution, false},
122 {ParticleGun::c_discreteSpectrum, true},
123 {ParticleGun::c_inversePtDistribution, true},
124 {ParticleGun::c_polylineDistribution, true},
125 {ParticleGun::c_polylinePtDistribution, true},
126 {ParticleGun::c_polylineCosDistribution, false}
127 };
128 checkVariable("momentum", distributions, parameters.momentumDist, parameters.momentumParams);
129 }
130
132 TEST_F(ParticleGunTest, VertexDistributions)
133 {
134 std::map<int, bool> distributions = {
135 {ParticleGun::c_fixedValue, true},
136 {ParticleGun::c_uniformDistribution, true},
137 {ParticleGun::c_uniformPtDistribution, false},
138 {ParticleGun::c_uniformCosDistribution, false},
139 {ParticleGun::c_normalDistribution, true},
140 {ParticleGun::c_normalPtDistribution, false},
141 {ParticleGun::c_normalCosDistribution, false},
142 {ParticleGun::c_discreteSpectrum, true},
143 {ParticleGun::c_inversePtDistribution, false},
144 {ParticleGun::c_polylineDistribution, true},
145 {ParticleGun::c_polylinePtDistribution, false},
146 {ParticleGun::c_polylineCosDistribution, false}
147 };
148 checkVariable("xvertex", distributions, parameters.xVertexDist, parameters.xVertexParams);
149 checkVariable("yvertex", distributions, parameters.yVertexDist, parameters.yVertexParams);
150 checkVariable("zvertex", distributions, parameters.zVertexDist, parameters.zVertexParams);
151 }
152
154 TEST_F(ParticleGunTest, AngularDistributions)
155 {
156 std::map<int, bool> distributions = {
157 {ParticleGun::c_fixedValue, true},
158 {ParticleGun::c_uniformDistribution, true},
159 {ParticleGun::c_uniformPtDistribution, false},
160 {ParticleGun::c_uniformCosDistribution, true},
161 {ParticleGun::c_normalDistribution, true},
162 {ParticleGun::c_normalPtDistribution, false},
163 {ParticleGun::c_normalCosDistribution, true},
164 {ParticleGun::c_discreteSpectrum, true},
165 {ParticleGun::c_inversePtDistribution, false},
166 {ParticleGun::c_polylineDistribution, true},
167 {ParticleGun::c_polylinePtDistribution, false},
168 {ParticleGun::c_polylineCosDistribution, true}
169 };
170 checkVariable("theta", distributions, parameters.thetaDist, parameters.thetaParams);
171 checkVariable("phi", distributions, parameters.phiDist, parameters.phiParams);
172 }
174} // namespace
The fixture for testing the Particlegun.
Definition: particlegun.cc:19
ParticleGun::Parameters parameters
Variable parameters.
Definition: particlegun.cc:34
virtual void SetUp()
Set up parameters which work for all distributions.
Definition: particlegun.cc:22
ParticleGun pgun
Variable of pgun.
Definition: particlegun.cc:32
Class to generate tracks in the particle gun and store them in a MCParticle graph.
Definition: ParticleGun.h:26
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_polylinePtDistribution
Same as polylineDistribution but for the transverse momentum.
Definition: ParticleGun.h:59
@ 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_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
bool setParameters(const Parameters &parameters)
Set the parameters for generating the Particles.
void checkVariable(const std::string &name, const std::map< int, bool > &allowed, ParticleGun::EDistribution &dist, std::vector< double > &pars)
check one of the variables given a list of allowed and excluded distributions
Definition: particlegun.cc:41
Abstract base class for different kinds of events.
STL namespace.
Struct to keep all necessary parameters for the particle gun.
Definition: ParticleGun.h:65
std::vector< int > pdgCodes
List of PDG particle codes to pick from when generating particles.
Definition: ParticleGun.h:83
std::vector< double > thetaParams
Parameters for the polar angle generation, meaning depends on chosen distribution.
Definition: ParticleGun.h:89
std::vector< double > momentumParams
Parameters for the momentum generation, meaning depends on chosen distribution.
Definition: ParticleGun.h:85
std::vector< double > xVertexParams
Parameters for the x vertex generation, meaning depends on chosen distribution.
Definition: ParticleGun.h:91
std::vector< double > zVertexParams
Parameters for the z vertex generation, meaning depends on chosen distribution.
Definition: ParticleGun.h:95
std::vector< double > phiParams
Parameters for the azimuth angle generation, meaning depends on chosen distribution.
Definition: ParticleGun.h:87
std::vector< double > yVertexParams
Parameters for the y vertex generation, meaning depends on chosen distribution.
Definition: ParticleGun.h:93